Merge branch 'MDL-26502_symbian' of git://github.com/stronk7/moodle into MOODLE_19_STABLE
[moodle.git] / course / report / stats / report.php
blob40a8014431b2db602c456f55cf0318c04f81ec43
1 <?php // $Id$
3 if (!defined('MOODLE_INTERNAL')) {
4 die('Direct access to this script is forbidden.'); /// It must be included from a Moodle page
7 $courses = get_courses('all','c.shortname','c.id,c.shortname,c.fullname');
8 $courseoptions = array();
10 foreach ($courses as $c) {
11 $context = get_context_instance(CONTEXT_COURSE, $c->id);
13 if (has_capability('coursereport/stats:view', $context)) {
14 $courseoptions[$c->id] = $c->shortname;
18 $reportoptions = stats_get_report_options($course->id, $mode);
19 $timeoptions = report_stats_timeoptions($mode);
20 if (empty($timeoptions)) {
21 print_error('nostatstodisplay', '', $CFG->wwwroot.'/course/view.php?id='.$course->id);
24 $table->width = 'auto';
26 if ($mode == STATS_MODE_DETAILED) {
27 $param = stats_get_parameters($time,null,$course->id,$mode); // we only care about the table and the time string (if we have time)
29 $sql = 'SELECT DISTINCT s.userid, u.firstname, u.lastname, u.idnumber
30 FROM '.$CFG->prefix.'stats_user_'.$param->table.' s
31 JOIN '.$CFG->prefix.'user u ON u.id = s.userid
32 WHERE courseid = '.$course->id
33 . ((!empty($param->stattype)) ? ' AND stattype = \''.$param->stattype.'\'' : '')
34 . ((!empty($time)) ? ' AND timeend >= '.$param->timeafter : '')
35 .' ORDER BY u.lastname, u.firstname ASC';
37 if (!$us = get_records_sql($sql)) {
38 error('Cannot enter detailed view: No users found for this course.');
41 foreach ($us as $u) {
42 $users[$u->userid] = fullname($u, true);
45 $table->align = array('left','left','left','left','left','left','left','left');
46 $table->data[] = array(get_string('course'),choose_from_menu($courseoptions,'course',$course->id,'','','',true),
47 get_string('users'),choose_from_menu($users,'userid',$userid,'','','',true),
48 get_string('statsreporttype'),choose_from_menu($reportoptions,'report',($report == 5) ? $report.$roleid : $report,'','','',true),
49 get_string('statstimeperiod'),choose_from_menu($timeoptions,'time',$time,'','','',true),
50 '<input type="submit" value="'.get_string('view').'" />') ;
51 } else if ($mode == STATS_MODE_RANKED) {
52 $table->align = array('left','left','left','left','left','left');
53 $table->data[] = array(get_string('statsreporttype'),choose_from_menu($reportoptions,'report',($report == 5) ? $report.$roleid : $report,'','','',true),
54 get_string('statstimeperiod'),choose_from_menu($timeoptions,'time',$time,'','','',true),
55 '<input type="submit" value="'.get_string('view').'" />') ;
56 } else if ($mode == STATS_MODE_GENERAL) {
57 $table->align = array('left','left','left','left','left','left','left');
58 $table->data[] = array(get_string('course'),choose_from_menu($courseoptions,'course',$course->id,'','','',true),
59 get_string('statsreporttype'),choose_from_menu($reportoptions,'report',($report == 5) ? $report.$roleid : $report,'','','',true),
60 get_string('statstimeperiod'),choose_from_menu($timeoptions,'time',$time,'','','',true),
61 '<input type="submit" value="'.get_string('view').'" />') ;
64 echo '<form action="index.php" method="post">'."\n"
65 .'<div>'."\n"
66 .'<input type="hidden" name="mode" value="'.$mode.'" />'."\n";
68 print_table($table);
70 echo '</div>';
71 echo '</form>';
73 if (!empty($report) && !empty($time)) {
74 if ($report == STATS_REPORT_LOGINS && $course->id != SITEID) {
75 error('This type of report is only available for the site course');
78 $param = stats_get_parameters($time,$report,$course->id,$mode);
80 if ($mode == STATS_MODE_DETAILED) {
81 $param->table = 'user_'.$param->table;
84 if (!empty($param->sql)) {
85 $sql = $param->sql;
86 } else {
87 $sql = 'SELECT '.((empty($param->fieldscomplete)) ? 'id,roleid,timeend,' : '').$param->fields
88 .' FROM '.$CFG->prefix.'stats_'.$param->table.' WHERE '
89 .(($course->id == SITEID) ? '' : ' courseid = '.$course->id.' AND ')
90 .((!empty($userid)) ? ' userid = '.$userid.' AND ' : '')
91 .((!empty($roleid)) ? ' roleid = '.$roleid.' AND ' : '')
92 . ((!empty($param->stattype)) ? ' stattype = \''.$param->stattype.'\' AND ' : '')
93 .' timeend >= '.$param->timeafter
94 .' '.$param->extras
95 .' ORDER BY timeend DESC';
98 $stats = get_records_sql($sql);
100 if (empty($stats)) {
101 notify(get_string('statsnodata'));
103 } else {
105 $stats = stats_fix_zeros($stats,$param->timeafter,$param->table,(!empty($param->line2)));
107 print_heading(format_string($course->shortname).' - '.get_string('statsreport'.$report)
108 .((!empty($user)) ? ' '.get_string('statsreportforuser').' ' .fullname($user,true) : '')
109 .((!empty($roleid)) ? ' '.get_field('role','name','id',$roleid) : ''));
112 if (empty($CFG->gdversion)) {
113 echo "(".get_string("gdneed").")";
114 } else {
115 if ($mode == STATS_MODE_DETAILED) {
116 echo '<div class="graph"><img src="'.$CFG->wwwroot.'/course/report/stats/graph.php?mode='.$mode.'&amp;course='.$course->id.'&amp;time='.$time.'&amp;report='.$report.'&amp;userid='.$userid.'" alt="'.get_string('statisticsgraph').'" /></div';
117 } else {
118 echo '<div class="graph"><img src="'.$CFG->wwwroot.'/course/report/stats/graph.php?mode='.$mode.'&amp;course='.$course->id.'&amp;time='.$time.'&amp;report='.$report.'&amp;roleid='.$roleid.'" alt="'.get_string('statisticsgraph').'" /></div>';
122 $table = new StdClass;
123 $table->align = array('left','center','center','center');
124 $param->table = str_replace('user_','',$param->table);
125 switch ($param->table) {
126 case 'daily' : $period = get_string('day'); break;
127 case 'weekly' : $period = get_string('week'); break;
128 case 'monthly': $period = get_string('month', 'form'); break;
129 default : $period = '';
131 $table->head = array(get_string('periodending','moodle',$period));
132 if (empty($param->crosstab)) {
133 $table->head[] = $param->line1;
134 if (!empty($param->line2)) {
135 $table->head[] = $param->line2;
138 if (empty($param->crosstab)) {
139 foreach ($stats as $stat) {
140 $a = array(userdate($stat->timeend-(60*60*24),get_string('strftimedate'),$CFG->timezone),$stat->line1);
141 if (isset($stat->line2)) {
142 $a[] = $stat->line2;
144 if (empty($CFG->loglifetime) || ($stat->timeend-(60*60*24)) >= (time()-60*60*24*$CFG->loglifetime)) {
145 if (has_capability('coursereport/log:view', get_context_instance(CONTEXT_COURSE, $course->id))) {
146 $a[] = '<a href="'.$CFG->wwwroot.'/course/report/log/index.php?id='.
147 $course->id.'&amp;chooselog=1&amp;showusers=1&amp;showcourses=1&amp;user='
148 .$userid.'&amp;date='.usergetmidnight($stat->timeend-(60*60*24)).'">'
149 .get_string('course').' ' .get_string('logs').'</a>&nbsp;';
150 } else {
151 $a[] = '';
154 $table->data[] = $a;
156 } else {
157 $data = array();
158 $roles = array();
159 $times = array();
160 $missedlines = array();
161 $rolenames = get_all_roles();
162 foreach ($rolenames as $r) {
163 $rolenames[$r->id] = $r->name;
165 $rolenames = role_fix_names($rolenames, get_context_instance(CONTEXT_COURSE, $course->id));
166 foreach ($stats as $stat) {
167 if (!empty($stat->zerofixed)) {
168 $missedlines[] = $stat->timeend;
170 $data[$stat->timeend][$stat->roleid] = $stat->line1;
171 if ($stat->roleid != 0) {
172 if (!array_key_exists($stat->roleid,$roles)) {
173 $roles[$stat->roleid] = $rolenames[$stat->roleid];
175 } else {
176 if (!array_key_exists($stat->roleid,$roles)) {
177 $roles[$stat->roleid] = get_string('all');
180 if (!array_key_exists($stat->timeend,$times)) {
181 $times[$stat->timeend] = userdate($stat->timeend,get_string('strftimedate'),$CFG->timezone);
185 foreach ($data as $time => $rolesdata) {
186 if (in_array($time,$missedlines)) {
187 $rolesdata = array();
188 foreach ($roles as $roleid => $guff) {
189 $rolesdata[$roleid] = 0;
192 else {
193 foreach (array_keys($roles) as $r) {
194 if (!array_key_exists($r, $rolesdata)) {
195 $rolesdata[$r] = 0;
199 krsort($rolesdata);
200 $row = array_merge(array($times[$time]),$rolesdata);
201 if (empty($CFG->loglifetime) || ($stat->timeend-(60*60*24)) >= (time()-60*60*24*$CFG->loglifetime)) {
202 if (has_capability('coursereport/log:view', get_context_instance(CONTEXT_COURSE, $course->id))) {
203 $row[] = '<a href="'.$CFG->wwwroot.'/course/report/log/index.php?id='
204 .$course->id.'&amp;chooselog=1&amp;showusers=1&amp;showcourses=1&amp;user='.$userid
205 .'&amp;date='.usergetmidnight($time-(60*60*24)).'">'
206 .get_string('course').' ' .get_string('logs').'</a>&nbsp;';
207 } else {
208 $row[] = '';
211 $table->data[] = $row;
213 krsort($roles);
214 $table->head = array_merge($table->head,$roles);
216 $table->head[] = get_string('logs');
217 if (!empty($lastrecord)) {
218 $lastrecord[] = $lastlink;
219 $table->data[] = $lastrecord;
221 print_table($table);