on-demand release 4.0dev+
[moodle.git] / course / user.php
CommitLineData
d9cb06dc 1<?php
d9cb06dc 2// This file is part of Moodle - http://moodle.org/
3//
4// Moodle is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// Moodle is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
16
17/**
18 * Display user activity reports for a course
19 *
20 * @copyright 1999 Martin Dougiamas http://dougiamas.com
21 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
22 * @package course
23 */
24
25require_once("../config.php");
26require_once("lib.php");
27
28$id = required_param('id',PARAM_INT); // course id
29$user = required_param('user',PARAM_INT); // user id
30$mode = optional_param('mode', "todaylogs", PARAM_ALPHA);
d9cb06dc 31
a6855934 32$url = new moodle_url('/course/user.php', array('id'=>$id,'user'=>$user, 'mode'=>$mode));
f9903ed0 33
74df2951 34$course = $DB->get_record('course', array('id'=>$id), '*', MUST_EXIST);
a260c847 35$user = $DB->get_record("user", array("id"=>$user, 'deleted'=>0), '*', MUST_EXIST);
5bf04b36 36
4d00fded
PS
37if ($mode === 'outline' or $mode === 'complete') {
38 $url = new moodle_url('/report/outline/user.php', array('id'=>$user->id, 'course'=>$course->id, 'mode'=>$mode));
39 redirect($url);
a260c847
PS
40}
41if ($mode === 'todaylogs' or $mode === 'alllogs') {
fad8e024
PS
42 $logmode = ($mode === 'todaylogs') ? 'today' : 'all';
43 $url = new moodle_url('/report/log/user.php', array('id'=>$user->id, 'course'=>$course->id, 'mode'=>$logmode));
44 redirect($url);
a260c847
PS
45}
46if ($mode === 'stats') {
beda8fa8
PS
47 $url = new moodle_url('/report/stats/user.php', array('id'=>$user->id, 'course'=>$course->id));
48 redirect($url);
4d00fded 49}
a260c847
PS
50if ($mode === 'coursecompletions' or $mode === 'coursecompletion') {
51 $url = new moodle_url('/report/completion/user.php', array('id'=>$user->id, 'course'=>$course->id));
52 redirect($url);
53}
4d00fded 54
9a5e297b
AA
55$coursecontext = context_course::instance($course->id);
56$personalcontext = context_user::instance($user->id);
358e2a0b 57
d000b16a
MG
58if ($id == SITEID) {
59 $PAGE->set_context($personalcontext);
60 $PAGE->set_heading(fullname($user));
61} else {
62 $PAGE->set_context($coursecontext);
63 $PAGE->set_secondary_active_tab('participants');
64 $PAGE->set_heading($course->fullname);
65}
c78dbe03 66
a0b15989
PS
67$PAGE->set_url('/course/user.php', array('id'=>$id, 'user'=>$user->id, 'mode'=>$mode));
68
df997f84 69require_login();
d5814f4e 70$PAGE->set_pagelayout('report');
df997f84 71if (has_capability('moodle/user:viewuseractivitiesreport', $personalcontext) and !is_enrolled($coursecontext)) {
d9cb06dc 72 // do not require parents to be enrolled in courses ;-)
73 $PAGE->set_course($course);
74} else {
75 require_login($course);
76}
6bb91986 77
d9cb06dc 78if ($user->deleted) {
79 echo $OUTPUT->header();
80 echo $OUTPUT->heading(get_string('userdeleted'));
81 echo $OUTPUT->footer();
82 die;
83}
f5fc83e8 84
d9cb06dc 85// prepare list of allowed modes
86$myreports = ($course->showreports and $USER->id == $user->id);
87$anyreport = has_capability('moodle/user:viewuseractivitiesreport', $personalcontext);
358e2a0b 88
d9cb06dc 89$modes = array();
358e2a0b 90
abbee05e
FM
91// Used for grade reports, it represents whether we should be viewing the report as ourselves, or as the targetted user.
92$viewasuser = false;
93
d9cb06dc 94if (has_capability('moodle/grade:viewall', $coursecontext)) {
95 //ok - can view all course grades
96 $modes[] = 'grade';
358e2a0b 97
d9cb06dc 98} else if ($course->showgrades and $user->id == $USER->id and has_capability('moodle/grade:view', $coursecontext)) {
99 //ok - can view own grades
100 $modes[] = 'grade';
358e2a0b 101
d9cb06dc 102} else if ($course->showgrades and has_capability('moodle/grade:viewall', $personalcontext)) {
103 // ok - can view grades of this user - parent most probably
104 $modes[] = 'grade';
abbee05e 105 $viewasuser = true;
3c7da16d 106
d9cb06dc 107} else if ($course->showgrades and $anyreport) {
108 // ok - can view grades of this user - parent most probably
109 $modes[] = 'grade';
abbee05e 110 $viewasuser = true;
d9cb06dc 111}
358e2a0b 112
d9cb06dc 113if (empty($modes)) {
114 require_capability('moodle/user:viewuseractivitiesreport', $personalcontext);
115}
358e2a0b 116
d9cb06dc 117if (!in_array($mode, $modes)) {
7e85563d 118 // forbidden or non-existent mode
d9cb06dc 119 $mode = reset($modes);
120}
85a6e76c 121
f49151c3
PS
122$eventdata = array(
123 'context' => $coursecontext,
124 'relateduserid' => $user->id,
125 'other' => array('mode' => $mode),
126);
68a72358
AG
127$event = \core\event\course_user_report_viewed::create($eventdata);
128$event->trigger();
f9903ed0 129
d9cb06dc 130$stractivityreport = get_string("activityreport");
7a7e209d
SH
131
132$PAGE->navigation->extend_for_user($user);
870815fa 133$PAGE->navigation->set_userid_for_parent_checks($user->id); // see MDL-25805 for reasons and for full commit reference for reversal when fixed.
d9cb06dc 134$PAGE->set_title("$course->shortname: $stractivityreport ($mode)");
f9a0ea69 135
d9cb06dc 136switch ($mode) {
137 case "grade":
c9451960
AG
138 // Change the navigation to point to the my grade node (If we are a student).
139 if ($USER->id == $user->id) {
140 require_once($CFG->dirroot . '/user/lib.php');
c78dbe03
AG
141 // Make the dashboard active so that it shows up in the navbar correctly.
142 $gradenode = $PAGE->settingsnav->find('dashboard', null)->make_active();
4887d152 143 // Get the correct 'Grades' url to point to.
c9451960 144 $activeurl = user_mygrades_url();
4887d152 145 $navbar = $PAGE->navbar->add(get_string('grades', 'grades'), $activeurl, navigation_node::TYPE_SETTING);
c78dbe03 146 $activenode = $navbar->add($course->shortname);
c9451960
AG
147 $activenode->make_active();
148 // Find the course node and collapse it.
149 $coursenode = $PAGE->navigation->find($course->id, navigation_node::TYPE_COURSE);
150 $coursenode->collapse = true;
151 $coursenode->make_inactive();
f495187d 152 $url = new moodle_url('/course/user.php', array('id' => $id, 'user' => $user->id, 'mode' => $mode));
c78dbe03
AG
153 $reportnode = $activenode->add(get_string('pluginname', 'gradereport_user'), $url);
154 } else {
12261113
AG
155 if ($course->id == SITEID) {
156 $activenode = $PAGE->navigation->find('user' . $user->id, null);
157 } else {
4b937932 158 $currentcoursenode = $PAGE->navigation->find($course->id, navigation_node::TYPE_COURSE);
12261113
AG
159 $activenode = $currentcoursenode->find_active_node();
160 }
4b937932 161
c78dbe03 162 // Check to see if the active node is a user name.
12261113 163 if (!preg_match('/^user\d{0,}$/', $activenode->key)) { // No user name found.
c78dbe03
AG
164 $userurl = new moodle_url('/user/view.php', array('id' => $user->id, 'course' => $course->id));
165 // Add the user name.
166 $PAGE->navbar->add(fullname($user), $userurl, navigation_node::TYPE_SETTING);
167 }
12261113 168 $PAGE->navbar->add(get_string('report'));
45367bdf 169 $gradeurl = new moodle_url('/course/user.php', array('id' => $id, 'user' => $user->id, 'mode' => $mode));
c78dbe03
AG
170 // Add the 'grades' node to the navbar.
171 $navbar = $PAGE->navbar->add(get_string('grades', 'grades'), $gradeurl, navigation_node::TYPE_SETTING);
c9451960
AG
172 }
173 echo $OUTPUT->header();
174
d000b16a
MG
175 if ($course->id !== SITEID) {
176 $backurl = new moodle_url('/user/view.php', ['id' => $user->id, 'course' => $course->id]);
177 echo $OUTPUT->single_button($backurl, get_string('back'), 'get', ['class' => 'mb-3']);
178 $userheading = array(
179 'heading' => fullname($user, has_capability('moodle/site:viewfullnames', $PAGE->context)),
180 'user' => $user,
181 'usercontext' => $personalcontext,
182 );
183 echo $OUTPUT->context_header($userheading, 2);
184
185 echo $OUTPUT->heading(get_string('grades', 'moodle'), 2, 'main mt-4 mb-4');
186 }
187
d9cb06dc 188 if (empty($CFG->grade_profilereport) or !file_exists($CFG->dirroot.'/grade/report/'.$CFG->grade_profilereport.'/lib.php')) {
189 $CFG->grade_profilereport = 'user';
190 }
191 require_once $CFG->libdir.'/gradelib.php';
192 require_once $CFG->dirroot.'/grade/lib.php';
193 require_once $CFG->dirroot.'/grade/report/'.$CFG->grade_profilereport.'/lib.php';
194
1964d68f 195 // User must be able to view this grade report.
6d18f136
PH
196 if (!$viewasuser) {
197 require_capability('gradereport/' . $CFG->grade_profilereport . ':view', $coursecontext);
198 }
1964d68f 199
d9cb06dc 200 $functionname = 'grade_report_'.$CFG->grade_profilereport.'_profilereport';
201 if (function_exists($functionname)) {
abbee05e 202 $functionname($course, $user, $viewasuser);
d9cb06dc 203 }
204 break;
205
d9cb06dc 206 default:
e35bac3e
SL
207 // It's unlikely to reach this piece of code, as the mode is never empty and it sets mode as grade in most of the cases.
208 // Display the page header to avoid breaking the navigation. A course/user.php review will be done in MDL-49939.
c9451960 209 echo $OUTPUT->header();
d9cb06dc 210}
f9903ed0 211
7468bf01 212
d9cb06dc 213echo $OUTPUT->footer();