3 // This file is part of Moodle - http://moodle.org/
5 // Moodle is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
10 // Moodle is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
19 * Display user activity reports for a course
21 * @copyright 1999 Martin Dougiamas http://dougiamas.com
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 require_once("../config.php");
27 require_once("lib.php");
28 require_once($CFG->libdir
.'/completionlib.php');
30 $id = required_param('id',PARAM_INT
); // course id
31 $user = required_param('user',PARAM_INT
); // user id
32 $mode = optional_param('mode', "todaylogs", PARAM_ALPHA
);
33 $page = optional_param('page', 0, PARAM_INT
);
34 $perpage = optional_param('perpage', 100, PARAM_INT
);
36 $url = new moodle_url('/course/user.php', array('id'=>$id,'user'=>$user, 'mode'=>$mode));
38 $url->param('page', $page);
40 if ($perpage !== 100) {
41 $url->param('perpage', $perpage);
45 if (!$course = $DB->get_record('course', array('id'=>$id))) {
46 print_error('invalidcourseid', 'error');
49 if (! $user = $DB->get_record("user", array("id"=>$user))) {
50 print_error('invaliduserid', 'error');
54 $coursecontext = get_context_instance(CONTEXT_COURSE
, $course->id
);
55 $personalcontext = get_context_instance(CONTEXT_USER
, $user->id
);
58 $PAGE->set_pagelayout('admin');
59 if (has_capability('moodle/user:viewuseractivitiesreport', $personalcontext) and !is_enrolled($coursecontext)) {
60 // do not require parents to be enrolled in courses ;-)
61 $PAGE->set_course($course);
63 require_login($course);
67 echo $OUTPUT->header();
68 echo $OUTPUT->heading(get_string('userdeleted'));
69 echo $OUTPUT->footer();
73 // prepare list of allowed modes
74 $myreports = ($course->showreports
and $USER->id
== $user->id
);
75 $anyreport = has_capability('moodle/user:viewuseractivitiesreport', $personalcontext);
79 if ($myreports or $anyreport or has_capability('coursereport/outline:view', $coursecontext)) {
83 if ($myreports or $anyreport or has_capability('coursereport/outline:view', $coursecontext)) {
84 $modes[] = 'complete';
87 if ($myreports or $anyreport or has_capability('coursereport/log:viewtoday', $coursecontext)) {
88 $modes[] = 'todaylogs';
91 if ($myreports or $anyreport or has_capability('coursereport/log:view', $coursecontext)) {
95 if ($myreports or $anyreport or has_capability('coursereport/stats:view', $coursecontext)) {
99 if (has_capability('moodle/grade:viewall', $coursecontext)) {
100 //ok - can view all course grades
103 } else if ($course->showgrades
and $user->id
== $USER->id
and has_capability('moodle/grade:view', $coursecontext)) {
104 //ok - can view own grades
107 } else if ($course->showgrades
and has_capability('moodle/grade:viewall', $personalcontext)) {
108 // ok - can view grades of this user - parent most probably
111 } else if ($course->showgrades
and $anyreport) {
112 // ok - can view grades of this user - parent most probably
116 // Course completion tab
117 if (!empty($CFG->enablecompletion
) && ($course->id
== SITEID ||
!empty($course->enablecompletion
)) && // completion enabled
118 ($myreports ||
$anyreport ||
($course->id
== SITEID ||
has_capability('coursereport/completion:view', $coursecontext)))) { // permissions to view the report
120 // Decide if singular or plural
121 if ($course->id
== SITEID
) {
122 $modes[] = 'coursecompletions';
124 $modes[] = 'coursecompletion';
130 require_capability('moodle/user:viewuseractivitiesreport', $personalcontext);
133 if (!in_array($mode, $modes)) {
134 // forbidden or non-existent mode
135 $mode = reset($modes);
138 add_to_log($course->id
, "course", "user report", "user.php?id=$course->id&user=$user->id&mode=$mode", "$user->id");
140 $stractivityreport = get_string("activityreport");
141 $strparticipants = get_string("participants");
142 $stroutline = get_string("outline");
143 $strcomplete = get_string("complete");
144 $stralllogs = get_string("alllogs");
145 $strtodaylogs = get_string("todaylogs");
146 $strmode = get_string($mode);
147 $fullname = fullname($user, true);
150 if ($course->id
!= SITEID
&& has_capability('moodle/course:viewparticipants', $coursecontext)) {
151 $link = new moodle_url('/user/index.php', array('id'=>$course->id
));
154 $PAGE->navigation
->extend_for_user($user);
155 $PAGE->navigation
->set_userid_for_parent_checks($user->id
); // see MDL-25805 for reasons and for full commit reference for reversal when fixed.
156 $PAGE->set_title("$course->shortname: $stractivityreport ($mode)");
157 $PAGE->set_heading($course->fullname
);
158 echo $OUTPUT->header();
162 if (empty($CFG->grade_profilereport
) or !file_exists($CFG->dirroot
.'/grade/report/'.$CFG->grade_profilereport
.'/lib.php')) {
163 $CFG->grade_profilereport
= 'user';
165 require_once $CFG->libdir
.'/gradelib.php';
166 require_once $CFG->dirroot
.'/grade/lib.php';
167 require_once $CFG->dirroot
.'/grade/report/'.$CFG->grade_profilereport
.'/lib.php';
169 $functionname = 'grade_report_'.$CFG->grade_profilereport
.'_profilereport';
170 if (function_exists($functionname)) {
171 $functionname($course, $user);
176 echo '<div class="graph">';
177 print_log_graph($course, $user->id
, "userday.png");
179 print_log($course, $user->id
, usergetmidnight(time()), "l.time DESC", $page, $perpage,
180 "user.php?id=$course->id&user=$user->id&mode=$mode");
184 echo '<div class="graph">';
185 print_log_graph($course, $user->id
, "usercourse.png");
187 print_log($course, $user->id
, 0, "l.time DESC", $page, $perpage,
188 "user.php?id=$course->id&user=$user->id&mode=$mode");
192 if (empty($CFG->enablestats
)) {
193 print_error('statsdisable', 'error');
196 require_once($CFG->dirroot
.'/lib/statslib.php');
198 $statsstatus = stats_check_uptodate($course->id
);
199 if ($statsstatus !== NULL) {
200 echo $OUTPUT->notification($statsstatus);
203 $earliestday = $DB->get_field_sql('SELECT timeend FROM {stats_user_daily} ORDER BY timeend');
204 $earliestweek = $DB->get_field_sql('SELECT timeend FROM {stats_user_weekly} ORDER BY timeend');
205 $earliestmonth = $DB->get_field_sql('SELECT timeend FROM {stats_user_monthly} ORDER BY timeend');
207 if (empty($earliestday)) $earliestday = time();
208 if (empty($earliestweek)) $earliestweek = time();
209 if (empty($earliestmonth)) $earliestmonth = time();
211 $now = stats_get_base_daily();
212 $lastweekend = stats_get_base_weekly();
213 $lastmonthend = stats_get_base_monthly();
215 $timeoptions = stats_get_time_options($now,$lastweekend,$lastmonthend,$earliestday,$earliestweek,$earliestmonth);
217 if (empty($timeoptions)) {
218 print_error('nostatstodisplay', '', $CFG->wwwroot
.'/course/user.php?id='.$course->id
.'&user='.$user->id
.'&mode=outline');
222 $time = array_pop(array_keys($timeoptions));
224 $param = stats_get_parameters($time,STATS_REPORT_USER_VIEW
,$course->id
,STATS_MODE_DETAILED
);
225 $params = $param->params
;
227 $param->table
= 'user_'.$param->table
;
229 $sql = 'SELECT timeend,'.$param->fields
.' FROM {stats_'.$param->table
.'} WHERE '
230 .(($course->id
== SITEID
) ?
'' : ' courseid = '.$course->id
.' AND ')
231 .' userid = '.$user->id
.' AND timeend >= '.$param->timeafter
.$param->extras
232 .' ORDER BY timeend DESC';
233 $stats = $DB->get_records_sql($sql, $params); //TODO: improve these params!!
236 print_error('nostatstodisplay', '', $CFG->wwwroot
.'/course/user.php?id='.$course->id
.'&user='.$user->id
.'&mode=outline');
239 // MDL-10818, do not display broken graph when user has no permission to view graph
240 if ($myreports or has_capability('coursereport/stats:view', $coursecontext)) {
241 echo '<center><img src="'.$CFG->wwwroot
.'/course/report/stats/graph.php?mode='.STATS_MODE_DETAILED
.'&course='.$course->id
.'&time='.$time.'&report='.STATS_REPORT_USER_VIEW
.'&userid='.$user->id
.'" alt="'.get_string('statisticsgraph').'" /></center>';
244 // What the heck is this about? -- MD
245 $stats = stats_fix_zeros($stats,$param->timeafter
,$param->table
,(!empty($param->line2
)),(!empty($param->line3
)));
247 $table = new html_table();
248 $table->align
= array('left','center','center','center');
249 $param->table
= str_replace('user_','',$param->table
);
250 switch ($param->table
) {
251 case 'daily' : $period = get_string('day'); break;
252 case 'weekly' : $period = get_string('week'); break;
253 case 'monthly': $period = get_string('month', 'form'); break;
254 default : $period = '';
256 $table->head
= array(get_string('periodending','moodle',$period),$param->line1
,$param->line2
,$param->line3
);
257 foreach ($stats as $stat) {
258 if (!empty($stat->zerofixed
)) { // Don't know why this is necessary, see stats_fix_zeros above - MD
261 $a = array(userdate($stat->timeend
,get_string('strftimedate'),$CFG->timezone
),$stat->line1
);
266 echo html_writer
::table($table);
271 get_all_mods($course->id
, $mods, $modnames, $modnamesplural, $modnamesused);
272 $sections = get_all_sections($course->id
);
274 for ($i=0; $i<=$course->numsections
; $i++
) {
276 if (isset($sections[$i])) { // should always be true
278 $section = $sections[$i];
279 $showsection = (has_capability('moodle/course:viewhiddensections', $coursecontext) or $section->visible
or !$course->hiddensections
);
281 if ($showsection) { // prevent hidden sections in user activity. Thanks to Geoff Wilbert!
283 if ($section->sequence
) {
284 echo '<div class="section">';
286 echo get_section_name($course, $section);
289 echo '<div class="content">';
291 if ($mode == "outline") {
292 echo "<table cellpadding=\"4\" cellspacing=\"0\">";
295 $sectionmods = explode(",", $section->sequence
);
296 foreach ($sectionmods as $sectionmod) {
297 if (empty($mods[$sectionmod])) {
300 $mod = $mods[$sectionmod];
302 if (empty($mod->visible
)) {
306 $instance = $DB->get_record("$mod->modname", array("id"=>$mod->instance
));
307 $libfile = "$CFG->dirroot/mod/$mod->modname/lib.php";
309 if (file_exists($libfile)) {
310 require_once($libfile);
314 $user_outline = $mod->modname
."_user_outline";
315 if (function_exists($user_outline)) {
316 $output = $user_outline($course, $user, $mod, $instance);
317 print_outline_row($mod, $instance, $output);
321 $user_complete = $mod->modname
."_user_complete";
322 if (function_exists($user_complete)) {
323 $image = $OUTPUT->pix_icon('icon', $mod->modfullname
, 'mod_'.$mod->modname
, array('class'=>'icon'));
324 echo "<h4>$image $mod->modfullname: ".
325 "<a href=\"$CFG->wwwroot/mod/$mod->modname/view.php?id=$mod->id\">".
326 format_string($instance->name
,true)."</a></h4>";
331 $user_complete($course, $user, $mod, $instance);
334 $output = ob_get_contents();
337 if (str_replace(' ', '', $output) != '<ul></ul>') {
346 if ($mode == "outline") {
349 echo '</div>'; // content
350 echo '</div>'; // section
356 case "coursecompletion":
357 case "coursecompletions":
359 // Display course completion user report
361 // Grab all courses the user is enrolled in and their completion status
369 ON con.instanceid = c.id
371 {role_assignments} ra
372 ON ra.contextid = con.id
378 ON e.id = ue.enrolid AND ra.userid = ue.userid
379 AND ra.userid = {$user->id}
382 // Get roles that are tracked by course completion
383 if ($roles = $CFG->gradebookroles
) {
385 AND ra.roleid IN ('.$roles.')
391 con.contextlevel = '.CONTEXT_COURSE
.'
392 AND c.enablecompletion = 1
396 // If we are looking at a specific course
397 if ($course->id
!= 1) {
399 AND c.id = '.(int)$course->id
.'
403 // Check if result is empty
404 $rs = $DB->get_recordset_sql($sql);
407 if ($course->id
!= 1) {
408 $error = get_string('nocompletions', 'coursereport_completion');
410 $error = get_string('nocompletioncoursesenroled', 'coursereport_completion');
413 echo $OUTPUT->notification($error);
414 $rs->close(); // not going to loop (but break), close rs
418 // Categorize courses by their status
420 'inprogress' => array(),
421 'complete' => array(),
422 'unstarted' => array()
425 // Sort courses by the user's status in each
426 foreach ($rs as $course_completion) {
427 $c_info = new completion_info((object)$course_completion);
429 // Is course complete?
430 $coursecomplete = $c_info->is_course_complete($user->id
);
432 // Has this user completed any criteria?
433 $criteriacomplete = $c_info->count_course_user_data($user->id
);
435 if ($coursecomplete) {
436 $courses['complete'][] = $c_info;
437 } else if ($criteriacomplete) {
438 $courses['inprogress'][] = $c_info;
440 $courses['unstarted'][] = $c_info;
443 $rs->close(); // after loop, close rs
445 // Loop through course status groups
446 foreach ($courses as $type => $infos) {
448 // If there are courses with this status
449 if (!empty($infos)) {
451 echo '<h1 align="center">'.get_string($type, 'coursereport_completion').'</h1>';
452 echo '<table class="generalbox boxaligncenter">';
453 echo '<tr class="ccheader">';
454 echo '<th class="c0 header" scope="col">'.get_string('course').'</th>';
455 echo '<th class="c1 header" scope="col">'.get_string('requiredcriteria', 'completion').'</th>';
456 echo '<th class="c2 header" scope="col">'.get_string('status').'</th>';
457 echo '<th class="c3 header" scope="col" width="15%">'.get_string('info').'</th>';
459 if ($type === 'complete') {
460 echo '<th class="c4 header" scope="col">'.get_string('completiondate', 'coursereport_completion').'</th>';
466 foreach ($infos as $c_info) {
469 $c_course = $DB->get_record('course', array('id' => $c_info->course_id
));
470 $course_name = $c_course->fullname
;
473 $completions = $c_info->get_completions($user->id
);
478 // For aggregating activity completion
479 $activities = array();
480 $activities_complete = 0;
482 // For aggregating prerequisites
483 $prerequisites = array();
484 $prerequisites_complete = 0;
486 // Loop through course criteria
487 foreach ($completions as $completion) {
488 $criteria = $completion->get_criteria();
489 $complete = $completion->is_complete();
491 // Activities are a special case, so cache them and leave them till last
492 if ($criteria->criteriatype
== COMPLETION_CRITERIA_TYPE_ACTIVITY
) {
493 $activities[$criteria->moduleinstance
] = $complete;
496 $activities_complete++
;
502 // Prerequisites are also a special case, so cache them and leave them till last
503 if ($criteria->criteriatype
== COMPLETION_CRITERIA_TYPE_COURSE
) {
504 $prerequisites[$criteria->courseinstance
] = $complete;
507 $prerequisites_complete++
;
514 $row['title'] = $criteria->get_title();
515 $row['status'] = $completion->get_status();
519 // Aggregate activities
520 if (!empty($activities)) {
523 $row['title'] = get_string('activitiescomplete', 'coursereport_completion');
524 $row['status'] = $activities_complete.' of '.count($activities);
528 // Aggregate prerequisites
529 if (!empty($prerequisites)) {
532 $row['title'] = get_string('prerequisitescompleted', 'completion');
533 $row['status'] = $prerequisites_complete.' of '.count($prerequisites);
534 array_splice($rows, 0, 0, array($row));
540 foreach ($rows as $row) {
542 // Display course name on first row
544 echo '<tr><td class="c0"><a href="'.$CFG->wwwroot
.'/course/view.php?id='.$c_course->id
.'">'.format_string($course_name).'</a></td>';
546 echo '<tr><td class="c0"></td>';
549 echo '<td class="c1">';
551 echo '</td><td class="c2">';
553 switch ($row['status']) {
555 echo get_string('complete');
559 echo get_string('incomplete', 'coursereport_completion');
566 // Display link on first row
567 echo '</td><td class="c3">';
569 echo '<a href="'.$CFG->wwwroot
.'/blocks/completionstatus/details.php?course='.$c_course->id
.'&user='.$user->id
.'">'.get_string('detailedview', 'coursereport_completion').'</a>';
573 // Display completion date for completed courses on first row
574 if ($type === 'complete' && $first_row) {
576 'userid' => $user->id
,
577 'course' => $c_course->id
580 $ccompletion = new completion_completion($params);
581 echo '<td class="c4">'.userdate($ccompletion->timecompleted
, '%e %B %G').'</td>';
596 // can not be reached ;-)
600 echo $OUTPUT->footer();
603 function print_outline_row($mod, $instance, $result) {
606 $image = "<img src=\"" . $OUTPUT->pix_url('icon', $mod->modname
) . "\" class=\"icon\" alt=\"$mod->modfullname\" />";
609 echo "<td valign=\"top\">$image</td>";
610 echo "<td valign=\"top\" style=\"width:300\">";
611 echo " <a title=\"$mod->modfullname\"";
612 echo " href=\"../mod/$mod->modname/view.php?id=$mod->id\">".format_string($instance->name
,true)."</a></td>";
613 echo "<td> </td>";
614 echo "<td valign=\"top\">";
615 if (isset($result->info
)) {
616 echo "$result->info";
618 echo "<p style=\"text-align:center\">-</p>";
621 echo "<td> </td>";
622 if (!empty($result->time
)) {
623 $timeago = format_time(time() - $result->time
);
624 echo "<td valign=\"top\" style=\"white-space: nowrap\">".userdate($result->time
)." ($timeago)</td>";