MDL-10585:
[moodle-linuxchix.git] / course / report / stats / report.php
blob1f03ace798075ce056bbe911273341b06d939ce6
1 <?php
3 $courses = get_courses('all','c.shortname','c.id,c.shortname,c.fullname');
4 $courseoptions = array();
6 foreach ($courses as $c) {
7 $context = get_context_instance(CONTEXT_COURSE, $c->id);
9 if (has_capability('moodle/site:viewreports', $context)) {
10 $courseoptions[$c->id] = $c->shortname;
14 $reportoptions = stats_get_report_options($course->id, $mode);
15 $timeoptions = report_stats_timeoptions($mode);
16 if (empty($timeoptions)) {
17 error(get_string('nostatstodisplay'), $CFG->wwwroot.'/course/view.php?id='.$course->id);
20 $table->width = '*';
22 if ($mode == STATS_MODE_DETAILED) {
23 $param = stats_get_parameters($time,null,$course->id,$mode); // we only care about the table and the time string (if we have time)
25 $sql = 'SELECT DISTINCT s.userid,s.roleid,r.name AS rolename,r.sortorder,u.firstname,u.lastname,u.idnumber
26 FROM '.$CFG->prefix.'stats_user_'.$param->table.' s
27 JOIN '.$CFG->prefix.'user u ON u.id = s.userid
28 JoIN '.$CFG->prefix.'role r ON s.roleid = r.id
29 WHERE courseid = '.$course->id
30 . ((!empty($param->stattype)) ? ' AND stattype = \''.$param->stattype.'\'' : '')
31 . ((!empty($time)) ? ' AND timeend >= '.$param->timeafter : '')
32 .' ORDER BY r.sortorder';
34 if (!$us = get_records_sql($sql)) {
35 error('Cannot enter detailed view: No users found for this course.');
38 foreach ($us as $u) {
39 $users[$u->userid] = $u->rolename.' - '.fullname($u,true);
42 $table->align = array('left','left','left','left','left','left','left','left');
43 $table->data[] = array(get_string('course'),choose_from_menu($courseoptions,'course',$course->id,'','','',true),
44 get_string('users'),choose_from_menu($users,'userid',$userid,'','','',true),
45 get_string('statsreporttype'),choose_from_menu($reportoptions,'report',($report == 5) ? $report.$roleid : $report,'','','',true),
46 get_string('statstimeperiod'),choose_from_menu($timeoptions,'time',$time,'','','',true),
47 '<input type="submit" value="'.get_string('view').'" />') ;
48 } else if ($mode == STATS_MODE_RANKED) {
49 $table->align = array('left','left','left','left','left','left');
50 $table->data[] = array(get_string('statsreporttype'),choose_from_menu($reportoptions,'report',($report == 5) ? $report.$roleid : $report,'','','',true),
51 get_string('statstimeperiod'),choose_from_menu($timeoptions,'time',$time,'','','',true),
52 '<input type="submit" value="'.get_string('view').'" />') ;
53 } else if ($mode == STATS_MODE_GENERAL) {
54 $table->align = array('left','left','left','left','left','left','left');
55 $table->data[] = array(get_string('course'),choose_from_menu($courseoptions,'course',$course->id,'','','',true),
56 get_string('statsreporttype'),choose_from_menu($reportoptions,'report',($report == 5) ? $report.$roleid : $report,'','','',true),
57 get_string('statstimeperiod'),choose_from_menu($timeoptions,'time',$time,'','','',true),
58 '<input type="submit" value="'.get_string('view').'" />') ;
61 echo '<form action="index.php" method="post">'."\n"
62 .'<fieldset class="invisiblefieldset">'."\n"
63 .'<input type="hidden" name="mode" value="'.$mode.'" />'."\n";
65 print_table($table);
67 echo '</fieldset>';
68 echo '</form>';
70 if (!empty($report) && !empty($time)) {
71 if ($report == STATS_REPORT_LOGINS && $course->id != SITEID) {
72 error('This type of report is only available for the site course');
74 $timesql =
75 $param = stats_get_parameters($time,$report,$course->id,$mode);
77 if ($mode == STATS_MODE_DETAILED) {
78 $param->table = 'user_'.$param->table;
80 if (!empty($param->sql)) {
81 $sql = $param->sql;
82 } else {
83 $sql = 'SELECT '.((empty($param->fieldscomplete)) ? 'id,roleid,timeend,' : '').$param->fields
84 .' FROM '.$CFG->prefix.'stats_'.$param->table.' WHERE '
85 .(($course->id == SITEID) ? '' : ' courseid = '.$course->id.' AND ')
86 .((!empty($userid)) ? ' userid = '.$userid.' AND ' : '')
87 .((!empty($roleid)) ? ' roleid = '.$roleid.' AND ' : '')
88 . ((!empty($param->stattype)) ? ' stattype = \''.$param->stattype.'\' AND ' : '')
89 .' timeend >= '.$param->timeafter
90 .' '.$param->extras
91 .' ORDER BY timeend DESC';
94 $stats = get_records_sql($sql);
96 if (empty($stats)) {
97 notify(get_string('statsnodata'));
99 } else {
101 $stats = stats_fix_zeros($stats,$param->timeafter,$param->table,(!empty($param->line2)));
103 print_heading(format_string($course->shortname).' - '.get_string('statsreport'.$report)
104 .((!empty($user)) ? ' '.get_string('statsreportforuser').' ' .fullname($user,true) : '')
105 .((!empty($roleid)) ? ' '.get_field('role','name','id',$roleid) : ''));
108 if (empty($CFG->gdversion)) {
109 echo "(".get_string("gdneed").")";
110 } else {
111 if ($mode == STATS_MODE_DETAILED) {
112 echo '<center><img src="'.$CFG->wwwroot.'/course/report/stats/graph.php?mode='.$mode.'&course='.$course->id.'&time='.$time.'&report='.$report.'&userid='.$userid.'" alt="'.get_string('statisticsgraph').'" /></center>';
113 } else {
114 echo '<center><img src="'.$CFG->wwwroot.'/course/report/stats/graph.php?mode='.$mode.'&course='.$course->id.'&time='.$time.'&report='.$report.'&roleid='.$roleid.'" alt="'.get_string('statisticsgraph').'" /></center>';
118 $table = new StdClass;
119 $table->align = array('left','center','center','center');
120 $param->table = str_replace('user_','',$param->table);
121 $table->head = array(get_string('periodending','moodle',$param->table));
122 if (empty($param->crosstab)) {
123 $table->head[] = $param->line1;
124 if (!empty($param->line2)) {
125 $table->head[] = $param->line2;
128 if (empty($param->crosstab)) {
129 foreach ($stats as $stat) {
130 $a = array(userdate($stat->timeend-(60*60*24),get_string('strftimedate'),$CFG->timezone),$stat->line1);
131 if (isset($stat->line2)) {
132 $a[] = $stat->line2;
134 if (empty($CFG->loglifetime) || ($stat->timeend-(60*60*24)) >= (time()-60*60*24*$CFG->loglifetime)) {
135 $a[] = '<a href="'.$CFG->wwwroot.'/course/report/log/index.php?id='.
136 $course->id.'&chooselog=1&showusers=1&showcourses=1&user='
137 .$userid.'&date='.usergetmidnight($stat->timeend-(60*60*24)).'">'
138 .get_string('course').' ' .get_string('logs').'</a>&nbsp;';
140 $table->data[] = $a;
142 } else {
143 $data = array();
144 $roles = array();
145 $times = array();
146 $missedlines = array();
147 foreach ($stats as $stat) {
148 if (!empty($stat->zerofixed)) {
149 $missedlines[] = $stat->timeend;
151 $data[$stat->timeend][$stat->roleid] = $stat->line1;
152 if ($stat->roleid != 0) {
153 if (!array_key_exists($stat->roleid,$roles)) {
154 $roles[$stat->roleid] = get_field('role','name','id',$stat->roleid);
157 if (!array_key_exists($stat->timeend,$times)) {
158 $times[$stat->timeend] = userdate($stat->timeend,get_string('strftimedate'),$CFG->timezone);
161 foreach ($data as $time => $rolesdata) {
162 if (in_array($time,$missedlines)) {
163 $rolesdata = array();
164 foreach ($roles as $roleid => $guff) {
165 if ($roleid == 0 ) {
166 continue;
168 $rolesdata[$roleid] = 0;
171 else {
172 foreach (array_keys($roles) as $r) {
173 if (!array_key_exists($r, $rolesdata)) {
174 $rolesdata[$r] = 0;
178 krsort($rolesdata);
179 $row = array_merge(array($times[$time]),$rolesdata);
180 if (empty($CFG->loglifetime) || ($stat->timeend-(60*60*24)) >= (time()-60*60*24*$CFG->loglifetime)) {
181 $row[] = '<a href="'.$CFG->wwwroot.'/course/report/log/index.php?id='
182 .$course->id.'&chooselog=1&showusers=1&showcourses=1&user='.$userid
183 .'&date='.usergetmidnight($time-(60*60*24)).'">'
184 .get_string('course').' ' .get_string('logs').'</a>&nbsp;';
186 $table->data[] = $row;
188 krsort($roles);
189 $table->head = array_merge($table->head,$roles);
191 $table->head[] = get_string('logs');
192 if (!empty($lastrecord)) {
193 $lastrecord[] = $lastlink;
194 $table->data[] = $lastrecord;
196 print_table($table);