MDL-25805 Roles propagated ugly hack for parent seeing child activities into navigati...
[moodle.git] / course / user.php
CommitLineData
d9cb06dc 1<?php
2
3// This file is part of Moodle - http://moodle.org/
4//
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.
9//
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.
14//
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/>.
17
18/**
19 * Display user activity reports for a course
20 *
21 * @copyright 1999 Martin Dougiamas http://dougiamas.com
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23 * @package course
24 */
25
26require_once("../config.php");
27require_once("lib.php");
516c5eca 28require_once($CFG->libdir.'/completionlib.php');
d9cb06dc 29
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);
35
a6855934 36$url = new moodle_url('/course/user.php', array('id'=>$id,'user'=>$user, 'mode'=>$mode));
d9cb06dc 37if ($page !== 0) {
38 $url->param('page', $page);
39}
40if ($perpage !== 100) {
41 $url->param('perpage', $perpage);
42}
43$PAGE->set_url($url);
f9903ed0 44
d9cb06dc 45if (!$course = $DB->get_record('course', array('id'=>$id))) {
46 print_error('invalidcourseid', 'error');
47}
5bf04b36 48
d9cb06dc 49if (! $user = $DB->get_record("user", array("id"=>$user))) {
50 print_error('invaliduserid', 'error');
51}
5bf04b36 52
d9cb06dc 53require_login();
54$coursecontext = get_context_instance(CONTEXT_COURSE, $course->id);
55$personalcontext = get_context_instance(CONTEXT_USER, $user->id);
358e2a0b 56
df997f84 57require_login();
87c215de 58$PAGE->set_pagelayout('admin');
df997f84 59if (has_capability('moodle/user:viewuseractivitiesreport', $personalcontext) and !is_enrolled($coursecontext)) {
d9cb06dc 60 // do not require parents to be enrolled in courses ;-)
61 $PAGE->set_course($course);
62} else {
63 require_login($course);
64}
6bb91986 65
d9cb06dc 66if ($user->deleted) {
67 echo $OUTPUT->header();
68 echo $OUTPUT->heading(get_string('userdeleted'));
69 echo $OUTPUT->footer();
70 die;
71}
f5fc83e8 72
d9cb06dc 73// prepare list of allowed modes
74$myreports = ($course->showreports and $USER->id == $user->id);
75$anyreport = has_capability('moodle/user:viewuseractivitiesreport', $personalcontext);
358e2a0b 76
d9cb06dc 77$modes = array();
358e2a0b 78
d9cb06dc 79if ($myreports or $anyreport or has_capability('coursereport/outline:view', $coursecontext)) {
80 $modes[] = 'outline';
81}
358e2a0b 82
d9cb06dc 83if ($myreports or $anyreport or has_capability('coursereport/outline:view', $coursecontext)) {
84 $modes[] = 'complete';
85}
358e2a0b 86
d9cb06dc 87if ($myreports or $anyreport or has_capability('coursereport/log:viewtoday', $coursecontext)) {
88 $modes[] = 'todaylogs';
89}
3924b988 90
d9cb06dc 91if ($myreports or $anyreport or has_capability('coursereport/log:view', $coursecontext)) {
92 $modes[] = 'alllogs';
93}
358e2a0b 94
d9cb06dc 95if ($myreports or $anyreport or has_capability('coursereport/stats:view', $coursecontext)) {
96 $modes[] = 'stats';
97}
358e2a0b 98
d9cb06dc 99if (has_capability('moodle/grade:viewall', $coursecontext)) {
100 //ok - can view all course grades
101 $modes[] = 'grade';
358e2a0b 102
d9cb06dc 103} else if ($course->showgrades and $user->id == $USER->id and has_capability('moodle/grade:view', $coursecontext)) {
104 //ok - can view own grades
105 $modes[] = 'grade';
358e2a0b 106
d9cb06dc 107} else if ($course->showgrades and has_capability('moodle/grade:viewall', $personalcontext)) {
108 // ok - can view grades of this user - parent most probably
109 $modes[] = 'grade';
3c7da16d 110
d9cb06dc 111} else if ($course->showgrades and $anyreport) {
112 // ok - can view grades of this user - parent most probably
113 $modes[] = 'grade';
114}
358e2a0b 115
2be4d090
MD
116// Course completion tab
117if (!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
119
120 // Decide if singular or plural
121 if ($course->id == SITEID) {
122 $modes[] = 'coursecompletions';
123 } else {
124 $modes[] = 'coursecompletion';
125 }
126}
127
128
d9cb06dc 129if (empty($modes)) {
130 require_capability('moodle/user:viewuseractivitiesreport', $personalcontext);
131}
358e2a0b 132
d9cb06dc 133if (!in_array($mode, $modes)) {
7e85563d 134 // forbidden or non-existent mode
d9cb06dc 135 $mode = reset($modes);
136}
85a6e76c 137
d9cb06dc 138add_to_log($course->id, "course", "user report", "user.php?id=$course->id&amp;user=$user->id&amp;mode=$mode", "$user->id");
f9903ed0 139
d9cb06dc 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);
a2ab3b05 148
d9cb06dc 149$link = null;
150if ($course->id != SITEID && has_capability('moodle/course:viewparticipants', $coursecontext)) {
a6855934 151 $link = new moodle_url('/user/index.php', array('id'=>$course->id));
d9cb06dc 152}
7a7e209d
SH
153
154$PAGE->navigation->extend_for_user($user);
b9bcad24 155$PAGE->navigation->set_userid_for_parent_nav_hack($user->id); // see MDL-25805 for reasons and for full commit reference for reversal when fixed.
d9cb06dc 156$PAGE->set_title("$course->shortname: $stractivityreport ($mode)");
157$PAGE->set_heading($course->fullname);
158echo $OUTPUT->header();
f9a0ea69 159
d9cb06dc 160switch ($mode) {
161 case "grade":
162 if (empty($CFG->grade_profilereport) or !file_exists($CFG->dirroot.'/grade/report/'.$CFG->grade_profilereport.'/lib.php')) {
163 $CFG->grade_profilereport = 'user';
164 }
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';
168
169 $functionname = 'grade_report_'.$CFG->grade_profilereport.'_profilereport';
170 if (function_exists($functionname)) {
171 $functionname($course, $user);
172 }
173 break;
174
175 case "todaylogs" :
176 echo '<div class="graph">';
177 print_log_graph($course, $user->id, "userday.png");
178 echo '</div>';
179 print_log($course, $user->id, usergetmidnight(time()), "l.time DESC", $page, $perpage,
180 "user.php?id=$course->id&amp;user=$user->id&amp;mode=$mode");
181 break;
182
183 case "alllogs" :
184 echo '<div class="graph">';
185 print_log_graph($course, $user->id, "usercourse.png");
186 echo '</div>';
187 print_log($course, $user->id, 0, "l.time DESC", $page, $perpage,
188 "user.php?id=$course->id&amp;user=$user->id&amp;mode=$mode");
189 break;
190 case 'stats':
191
192 if (empty($CFG->enablestats)) {
193 print_error('statsdisable', 'error');
194 }
195
196 require_once($CFG->dirroot.'/lib/statslib.php');
197
198 $statsstatus = stats_check_uptodate($course->id);
199 if ($statsstatus !== NULL) {
200 echo $OUTPUT->notification($statsstatus);
201 }
202
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');
206
207 if (empty($earliestday)) $earliestday = time();
208 if (empty($earliestweek)) $earliestweek = time();
209 if (empty($earliestmonth)) $earliestmonth = time();
210
211 $now = stats_get_base_daily();
212 $lastweekend = stats_get_base_weekly();
213 $lastmonthend = stats_get_base_monthly();
214
215 $timeoptions = stats_get_time_options($now,$lastweekend,$lastmonthend,$earliestday,$earliestweek,$earliestmonth);
216
217 if (empty($timeoptions)) {
218 print_error('nostatstodisplay', '', $CFG->wwwroot.'/course/user.php?id='.$course->id.'&user='.$user->id.'&mode=outline');
219 }
220
221 // use the earliest.
222 $time = array_pop(array_keys($timeoptions));
223
224 $param = stats_get_parameters($time,STATS_REPORT_USER_VIEW,$course->id,STATS_MODE_DETAILED);
225 $params = $param->params;
226
227 $param->table = 'user_'.$param->table;
228
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!!
234
235 if (empty($stats)) {
236 print_error('nostatstodisplay', '', $CFG->wwwroot.'/course/user.php?id='.$course->id.'&user='.$user->id.'&mode=outline');
237 }
238
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>';
242 }
243
244 // What the heck is this about? -- MD
245 $stats = stats_fix_zeros($stats,$param->timeafter,$param->table,(!empty($param->line2)),(!empty($param->line3)));
246
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 = '';
255 }
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
259 continue;
f3221af9 260 }
d9cb06dc 261 $a = array(userdate($stat->timeend,get_string('strftimedate'),$CFG->timezone),$stat->line1);
262 $a[] = $stat->line2;
263 $a[] = $stat->line3;
264 $table->data[] = $a;
265 }
16be8974 266 echo html_writer::table($table);
d9cb06dc 267 break;
268
269 case "outline" :
270 case "complete" :
271 get_all_mods($course->id, $mods, $modnames, $modnamesplural, $modnamesused);
272 $sections = get_all_sections($course->id);
273
274 for ($i=0; $i<=$course->numsections; $i++) {
275
276 if (isset($sections[$i])) { // should always be true
277
278 $section = $sections[$i];
279 $showsection = (has_capability('moodle/course:viewhiddensections', $coursecontext) or $section->visible or !$course->hiddensections);
280
281 if ($showsection) { // prevent hidden sections in user activity. Thanks to Geoff Wilbert!
282
283 if ($section->sequence) {
284 echo '<div class="section">';
285 echo '<h2>';
7487c856
SH
286 echo get_section_name($course, $section);
287 echo "</h2>";
f3221af9 288
d9cb06dc 289 echo '<div class="content">';
7468bf01 290
d9cb06dc 291 if ($mode == "outline") {
292 echo "<table cellpadding=\"4\" cellspacing=\"0\">";
293 }
7468bf01 294
d9cb06dc 295 $sectionmods = explode(",", $section->sequence);
296 foreach ($sectionmods as $sectionmod) {
297 if (empty($mods[$sectionmod])) {
298 continue;
7af6281f 299 }
d9cb06dc 300 $mod = $mods[$sectionmod];
4f91674c 301
d9cb06dc 302 if (empty($mod->visible)) {
303 continue;
4f91674c 304 }
305
d9cb06dc 306 $instance = $DB->get_record("$mod->modname", array("id"=>$mod->instance));
307 $libfile = "$CFG->dirroot/mod/$mod->modname/lib.php";
0be6f678 308
d9cb06dc 309 if (file_exists($libfile)) {
310 require_once($libfile);
359b138b 311
d9cb06dc 312 switch ($mode) {
313 case "outline":
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);
318 }
319 break;
320 case "complete":
321 $user_complete = $mod->modname."_user_complete";
322 if (function_exists($user_complete)) {
b50d110e 323 $image = $OUTPUT->pix_icon('icon', $mod->modfullname, 'mod_'.$mod->modname, array('class'=>'icon'));
d9cb06dc 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>";
327
328 ob_start();
329
330 echo "<ul>";
331 $user_complete($course, $user, $mod, $instance);
332 echo "</ul>";
333
334 $output = ob_get_contents();
335 ob_end_clean();
336
337 if (str_replace(' ', '', $output) != '<ul></ul>') {
338 echo $output;
1757627a 339 }
359b138b 340 }
d9cb06dc 341 break;
1757627a 342 }
7468bf01 343 }
344 }
d9cb06dc 345
346 if ($mode == "outline") {
347 echo "</table>";
7468bf01 348 }
d9cb06dc 349 echo '</div>'; // content
350 echo '</div>'; // section
7468bf01 351 }
600149be 352 }
f9903ed0 353 }
d9cb06dc 354 }
2be4d090
MD
355 break;
356 case "coursecompletion":
357 case "coursecompletions":
358
359 // Display course completion user report
2be4d090
MD
360
361 // Grab all courses the user is enrolled in and their completion status
362 $sql = "
363 SELECT DISTINCT
364 c.id AS id
365 FROM
ffe50258 366 {course} c
2be4d090 367 INNER JOIN
ffe50258 368 {context} con
2be4d090 369 ON con.instanceid = c.id
b64a827c 370 INNER JOIN
371 {role_assignments} ra
372 ON ra.contextid = con.id
1c6ed505 373 INNER JOIN
374 {enrol} e
375 ON c.id = e.courseid
376 INNER JOIN
377 {user_enrolments} ue
b64a827c 378 ON e.id = ue.enrolid AND ra.userid = ue.userid
2be4d090
MD
379 AND ra.userid = {$user->id}
380 ";
381
382 // Get roles that are tracked by course completion
1c6ed505 383 if ($roles = $CFG->gradebookroles) {
2be4d090
MD
384 $sql .= '
385 AND ra.roleid IN ('.$roles.')
386 ';
387 }
388
389 $sql .= '
390 WHERE
391 con.contextlevel = '.CONTEXT_COURSE.'
392 AND c.enablecompletion = 1
393 ';
394
395
396 // If we are looking at a specific course
397 if ($course->id != 1) {
398 $sql .= '
399 AND c.id = '.(int)$course->id.'
400 ';
401 }
402
403 // Check if result is empty
a0c3fc63
EL
404 $rs = $DB->get_recordset_sql($sql);
405 if (!$rs->valid()) {
2be4d090
MD
406
407 if ($course->id != 1) {
408 $error = get_string('nocompletions', 'coursereport_completion');
409 } else {
410 $error = get_string('nocompletioncoursesenroled', 'coursereport_completion');
411 }
412
413 echo $OUTPUT->notification($error);
a0c3fc63 414 $rs->close(); // not going to loop (but break), close rs
2be4d090
MD
415 break;
416 }
417
418 // Categorize courses by their status
419 $courses = array(
420 'inprogress' => array(),
421 'complete' => array(),
422 'unstarted' => array()
423 );
424
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);
428
429 // Is course complete?
430 $coursecomplete = $c_info->is_course_complete($user->id);
431
432 // Has this user completed any criteria?
433 $criteriacomplete = $c_info->count_course_user_data($user->id);
434
435 if ($coursecomplete) {
436 $courses['complete'][] = $c_info;
437 } else if ($criteriacomplete) {
438 $courses['inprogress'][] = $c_info;
439 } else {
440 $courses['unstarted'][] = $c_info;
441 }
442 }
a0c3fc63 443 $rs->close(); // after loop, close rs
2be4d090
MD
444
445 // Loop through course status groups
446 foreach ($courses as $type => $infos) {
447
448 // If there are courses with this status
449 if (!empty($infos)) {
450
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>';
458
459 if ($type === 'complete') {
460 echo '<th class="c4 header" scope="col">'.get_string('completiondate', 'coursereport_completion').'</th>';
461 }
462
463 echo '</tr>';
464
465 // For each course
466 foreach ($infos as $c_info) {
467
468 // Get course info
ffe50258 469 $c_course = $DB->get_record('course', array('id' => $c_info->course_id));
2be4d090
MD
470 $course_name = $c_course->fullname;
471
472 // Get completions
473 $completions = $c_info->get_completions($user->id);
474
475 // Save row data
476 $rows = array();
477
478 // For aggregating activity completion
479 $activities = array();
480 $activities_complete = 0;
7e85563d 481
2be4d090
MD
482 // For aggregating prerequisites
483 $prerequisites = array();
484 $prerequisites_complete = 0;
485
486 // Loop through course criteria
487 foreach ($completions as $completion) {
488 $criteria = $completion->get_criteria();
489 $complete = $completion->is_complete();
490
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;
494
495 if ($complete) {
496 $activities_complete++;
497 }
498
499 continue;
500 }
501
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;
505
506 if ($complete) {
507 $prerequisites_complete++;
508 }
509
510 continue;
511 }
512
513 $row = array();
514 $row['title'] = $criteria->get_title();
515 $row['status'] = $completion->get_status();
516 $rows[] = $row;
517 }
518
519 // Aggregate activities
520 if (!empty($activities)) {
521
522 $row = array();
523 $row['title'] = get_string('activitiescomplete', 'coursereport_completion');
524 $row['status'] = $activities_complete.' of '.count($activities);
525 $rows[] = $row;
526 }
527
528 // Aggregate prerequisites
529 if (!empty($prerequisites)) {
530
531 $row = array();
532 $row['title'] = get_string('prerequisitescompleted', 'completion');
533 $row['status'] = $prerequisites_complete.' of '.count($prerequisites);
534 array_splice($rows, 0, 0, array($row));
535 }
536
537 $first_row = true;
538
539 // Print table
540 foreach ($rows as $row) {
541
542 // Display course name on first row
543 if ($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>';
545 } else {
546 echo '<tr><td class="c0"></td>';
547 }
548
549 echo '<td class="c1">';
550 echo $row['title'];
551 echo '</td><td class="c2">';
552
553 switch ($row['status']) {
554 case 'Yes':
555 echo get_string('complete');
556 break;
557
558 case 'No':
559 echo get_string('incomplete', 'coursereport_completion');
560 break;
561
562 default:
563 echo $row['status'];
564 }
565
566 // Display link on first row
567 echo '</td><td class="c3">';
568 if ($first_row) {
569 echo '<a href="'.$CFG->wwwroot.'/blocks/completionstatus/details.php?course='.$c_course->id.'&user='.$user->id.'">'.get_string('detailedview', 'coursereport_completion').'</a>';
570 }
571 echo '</td>';
572
573 // Display completion date for completed courses on first row
574 if ($type === 'complete' && $first_row) {
575 $params = array(
576 'userid' => $user->id,
577 'course' => $c_course->id
578 );
579
580 $ccompletion = new completion_completion($params);
581 echo '<td class="c4">'.userdate($ccompletion->timecompleted, '%e %B %G').'</td>';
582 }
583
584 $first_row = false;
585 echo '</tr>';
586 }
587 }
588
589 echo '</table>';
590 }
591
592 }
593
d9cb06dc 594 break;
595 default:
596 // can not be reached ;-)
597}
f9903ed0 598
7468bf01 599
d9cb06dc 600echo $OUTPUT->footer();
f9903ed0 601
7468bf01 602
359b138b 603function print_outline_row($mod, $instance, $result) {
e63f88c9 604 global $OUTPUT;
bad35380 605
b5d0cafc 606 $image = "<img src=\"" . $OUTPUT->pix_url('icon', $mod->modname) . "\" class=\"icon\" alt=\"$mod->modfullname\" />";
24f2b65a 607
608 echo "<tr>";
609 echo "<td valign=\"top\">$image</td>";
fbe31d22 610 echo "<td valign=\"top\" style=\"width:300\">";
24f2b65a 611 echo " <a title=\"$mod->modfullname\"";
46043aff 612 echo " href=\"../mod/$mod->modname/view.php?id=$mod->id\">".format_string($instance->name,true)."</a></td>";
24f2b65a 613 echo "<td>&nbsp;&nbsp;&nbsp;</td>";
1530c97c 614 echo "<td valign=\"top\">";
359b138b 615 if (isset($result->info)) {
616 echo "$result->info";
617 } else {
fbe31d22 618 echo "<p style=\"text-align:center\">-</p>";
359b138b 619 }
24f2b65a 620 echo "</td>";
621 echo "<td>&nbsp;&nbsp;&nbsp;</td>";
20affd15 622 if (!empty($result->time)) {
359b138b 623 $timeago = format_time(time() - $result->time);
fbe31d22 624 echo "<td valign=\"top\" style=\"white-space: nowrap\">".userdate($result->time)." ($timeago)</td>";
359b138b 625 }
24f2b65a 626 echo "</tr>";
7468bf01 627}
628