Reverting this file to what is in the atutor trunk.
[atutor.git] / docs / mods / _standard / statistics / course_stats.php
1 <?php
2 /****************************************************************/
3 /* ATutor                                                                                                               */
4 /****************************************************************/
5 /* Copyright (c) 2002-2010                                      */
6 /* Inclusive Design Institute                                   */
7 /* http://atutor.ca                                                                                             */
8 /*                                                              */
9 /* This program is free software. You can redistribute it and/or*/
10 /* modify it under the terms of the GNU General Public License  */
11 /* as published by the Free Software Foundation.                                */
12 /****************************************************************/
13 // $Id$
14
15 define('AT_INCLUDE_PATH', '../../../include/');
16 require(AT_INCLUDE_PATH.'vitals.inc.php');
17 authenticate(AT_PRIV_ADMIN, AT_PRIV_RETURN);
18
19 $year  = intval($_GET['year']);
20 $month = intval($_GET['month']);
21
22         if ($month == 0) {
23                 $month = date('m');
24                 $year  = date('Y');
25         }
26
27         $days   = array();
28         $sql    = "SELECT * FROM ".TABLE_PREFIX."course_stats WHERE course_id=$_SESSION[course_id] AND MONTH(login_date)=$month AND YEAR(login_date)=$year ORDER BY login_date ASC";
29         $result = mysql_query($sql, $db);
30         //$today  = 1; /* we start on the 1st of the month */
31         $max_total_logins = 0;
32         $min_total_logins = (int) 99999999;
33         $total_logins = 0;
34
35         $empty = true;
36         while ($row = mysql_fetch_array($result)) {
37                 $empty = false;
38                 $row_day = substr($row['login_date'], 8, 2);
39
40                 if (substr($row_day, 0,1) == '0') {
41                         $row_day = substr($row_day, 1, 1);
42                 }
43                 
44                 while ($today < $row_day-1) {
45                         $today++;
46                         $days[$today] = array(0, 0);
47                         $min_total_logins = 0;
48                 }
49
50                 $today = $row_day; /* skip this day in the fill-in-the-blanks-loop */
51                                 
52                 $days[$row_day] = array($row['guests'], $row['members']);
53
54                 if ($max_total_logins < $row['guests']+$row['members']) {
55                         $max_total_logins = $row['guests']+$row['members'];
56                 }
57
58                 if ($min_total_logins > $row['guests']+$row['members']) {
59                         $min_total_logins = $row['guests']+$row['members'];
60                 }
61
62                 $total_logins += $row['guests']+$row['members'];
63         }
64
65         /* add zeros to the end of the month, only if it isn't the current month */
66         $now_month = date('m');
67         $now_year  = date('Y');
68         if ( (($month < $now_month) && ($now_year == $year)) || ($now_year < $year) ) {
69                 $today++;
70                 while (checkdate($month, $today,$year)) {
71                         $days[$today] = array(0, 0);
72                         $today++;
73                 }
74         }
75         $num_days = count($days);
76
77         if ($total_logins > 0) {
78                 $avg_total_logins = $total_logins/$num_days;
79         } else {
80                 $avg_total_logins = 0;
81         }
82
83         $block_height           = 10;
84         $multiplyer_height  = 5; /* should be multiples of 5 */
85
86         if ($month == 12) {
87                 $next_month = 1;
88                 $next_year  = $year + 1;
89         } else {
90                 $next_month = $month + 1;
91                 $next_year  = $year;
92         }
93
94         if ($month == 1) {
95                 $last_month = 12;
96                 $last_year  = $year - 1;
97         } else {
98                 $last_month = $month - 1;
99                 $last_year  = $year;
100         }
101
102 require(AT_INCLUDE_PATH.'header.inc.php');
103
104 ?>
105         <table cellspacing="1" cellpadding="1" border="0" class="bodyline" summary="" align="center">
106         <tr>
107                 <th colspan="2" class="cyan"><small class="bigspacer"><?php
108                         echo '<a href="'.$_SERVER['PHP_SELF'].'?month='.($last_month).SEP.'year='.$last_year.'">';
109                         echo ' '.AT_date('%F', $last_month, AT_DATE_INDEX_VALUE ); ?></a> |</small>
110                         <?php echo AT_date('%F', $month, AT_DATE_INDEX_VALUE ); ?> <small class="bigspacer">| <?php
111                         echo '<a href="'.$_SERVER['PHP_SELF'].'?month='.$next_month.SEP.'year='.$next_year.'">';
112                         echo AT_date('%F', $next_month, AT_DATE_INDEX_VALUE); ?> </a></small></th>
113         </tr>
114 <?php
115                 if (($num_days == 0) || ($empty)) {
116                         echo '<tr>';
117                         echo '<td class="row1" colspan="2">'._AT('no_month_data').'</td>';
118                         echo '</tr>';
119                         echo '</table>';
120                         require(AT_INCLUDE_PATH.'footer.inc.php');
121                         exit;
122                 }
123 ?>
124         <tr>
125                 <td class="row1" valign="top" align="right"><strong><?php echo _AT('total'); ?>:</strong></td>
126                 <td class="row1"><?php echo $total_logins; ?></td>
127         </tr>
128         <tr><td height="1" class="row2" colspan="2"></td></tr>
129         <tr>
130                 <td class="row1" valign="top" align="right"><strong><?php echo _AT('maximum'); ?>:</strong></td>
131                 <td class="row1"><?php echo $max_total_logins; ?></td>
132         </tr>
133         <tr><td height="1" class="row2" colspan="2"></td></tr>
134
135         <tr>
136                 <td class="row1" valign="top" align="right"><strong><?php echo _AT('minimum'); ?>:</strong></td>
137                 <td class="row1"><?php
138                 if ($min_total_logins < 99999999) {
139                         echo $min_total_logins; 
140                 } else {
141                         echo '0';
142                 } ?></td>
143         </tr>
144         <tr><td height="1" class="row2" colspan="2"></td></tr>
145         <tr>
146                 <td class="row1" valign="top" align="right"><strong><?php   echo _AT('average'); ?>:</strong></td>
147                 <td class="row1"><?php echo number_format($avg_total_logins, 1); ?> <?php   echo _AT('per_day'); ?></td>
148         </tr>
149         <tr><td height="1" class="row2" colspan="2"></td></tr>
150
151         <tr>
152                 <td class="row1" valign="top" align="right"><strong><?php   echo _AT('graph'); ?>:</strong></td>
153                 <td class="row1">
154                         <table border="0" cellspacing="0" cellpadding="0">
155                         <tr>
156                                 <td valign="top" class="graph1"><small><?php echo $max_total_logins; ?></small></td>
157
158 <?php
159                         foreach ($days as $day => $logins) {
160                         $dd++;
161                                 echo '<td valign="bottom" class="graph"><img src="images/clr.gif" height="'.(($max_total_logins*$multiplyer_height) % $block_height + $block_height).'" width="10" alt="" /><br /><img src="images/blue.gif" height="'.($logins[0]*$multiplyer_height).'" width="9" alt="'.$logins[0].' '._AT('guests').' ('.($logins[0]+$logins[1]).' '._AT('total').')" /><br /><img src="images/red.gif" height="'.($logins[1]*$multiplyer_height).'" width="9" alt="'.$logins[1].' '._AT('members').' ('.($logins[1]+$logins[0]).' '._AT('total').')" /><br /><small>'.$dd.'&nbsp;</small></td>';
162
163                         } while ($row = mysql_fetch_array($result));
164 ?>
165
166                         </tr>
167                         <tr>
168                                 <td valign="top"><small>0</small></td>
169                         </tr>
170                         </table>
171
172                         <small><?php  echo _AT('legend'); ?>: <img src="images/red.gif" height="10" width="10" alt="<?php echo _AT('red_members'); ?>" /> <?php   echo _AT('members'); ?>,
173                                 <img src="images/blue.gif" height="10" width="10" alt="<?php echo _AT('blue_guests'); ?>" /> <?php echo _AT('guests'); ?>.</small>
174                 </td>
175         </tr>
176         <tr><td height="1" class="row2" colspan="2"></td></tr>
177         <tr>
178                 <td class="row1" valign="top" align="right"><strong><?php echo _AT('raw_data'); ?>:</strong></td>
179                 <td class="row1" align="center">
180         
181                 <table class="data static" summary="" rules="cols">
182                 <thead>
183                 <tr>
184                         <th scope="col"><?php echo _AT('date');    ?></th>
185                         <th scope="col"><?php echo _AT('guests');  ?></th>
186                         <th scope="col"><?php echo _AT('members'); ?></th>
187                 </tr>
188                 </thead>
189                 <tbody>
190                 <?php $short_name = $month_name_con['en'][$month-1]; ?>
191                 <?php foreach ($days as $day => $logins):?>
192                         <tr>
193                                 <td><?php echo $short_name.' '.$day; ?></td>
194                                 <td><?php echo $logins[0]; ?></td>
195                                 <td><?php echo $logins[1]; ?></td>
196                         </tr>
197                 <?php endforeach; ?>
198                 <tbody>
199                 </table>
200
201                 </td>
202         </tr>
203         </table>
204 <?php require(AT_INCLUDE_PATH.'footer.inc.php'); ?>