In this concluding article, explore the scripts which add andremove timesheet entries to the system, and get a crash course instatistics by using these entries to generate useful resource allocationand usage reports.
No, I didn't really do that - I still need a paycheck. But every dog has his day, and mine will come soon...
Anyway, disturbing though that conversation was, I still managed to get some useful data from it. I can now state with certainty that the system should generate the following three types of reports:
a big-picture overview of the time spent by users on all active projects for a specific period of time,
a big-picture overview of the time spent on different tasks over a specific period of time;
a focused report of time spent by different users on the different components of a project for a specific period.
With this in mind, let's quickly review the code for the report generation menu item in "menu.php"
<!-- menu.php -->
<li>
<!-- generate report option -->
<form name="report" action="report.php" method="post">
<a href="javascript:submitForm(1)">Generate activity reports</a> between
<? generateDateSelector("s"); ?> and <? generateDateSelector("e"); ?> for
<select name="pid">
<option value="0"><all projects></option>
<?
// open connection to database
$connection = mysql_connect($hostname, $user, $pass) or die ("Unable to
connect!");
// get project list
$query = "SELECT pid, pname from projects";
$result = mysql_db_query($database, $query, $connection) or die ("Error in
query: $query. " . mysql_error());
while (list($pid, $pname) = mysql_fetch_row($result))
{
echo "<option value=$pid>$pname</option>";
}
mysql_free_result($result);
?>
</select>
</form>
<p>
<?
}
?>
This has all the data I need to build any of the three report
types - a starting date (created from the date variables $sd, $sm and $sy), an ending date (created from the date variables $ed, $em and $ey) and a project identifier (which may be 0 for "all projects"). When this form is submitted, it sends these variables to the script "report.php", which performs the actual report generation.
If you look at "report.php", you'll see that it's basically one gigantic "if" loop - the first part generates big-picture type reports, while the second part generates more specific reports.
<?
// report.php - generate reports
// includes
// check for valid session and valid administrator
// check for valid dates
if (!checkdate($sm, $sd, $sy) || !checkdate($em, $ed, $ey))
{
header("Location: error.php?ec=2");
exit;
}
// open connection to database
$connection = mysql_connect($hostname, $user, $pass) or die ("Unable to
connect!");
// create start and end datestamp
$sdate = $sy . "-" . $sm . "-" . $sd;
$edate = $ey . "-" . $em . "-" . $ed;
?>
<?
// main "if" loop begins
// if report required for ALL projects
if ($pid == 0)
{
// code goes here
}
// report required for a specific project
else
{
// code goes here snip!
}
// main "if" loop done - now clean up and print page footer
mysql_close($connection);
?>
<? include("footer.inc.php"); ?>
</body>
</html>
Let's write some code for the general report first - once
that's done, writing code to build a report for a specific project should be a piece of cake.