MDL-67264 core_course: Activity chooser new feature
[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
c78dbe03
AG
58$PAGE->set_context($personalcontext);
59
a0b15989
PS
60$PAGE->set_url('/course/user.php', array('id'=>$id, 'user'=>$user->id, 'mode'=>$mode));
61
df997f84 62require_login();
d5814f4e 63$PAGE->set_pagelayout('report');
df997f84 64if (has_capability('moodle/user:viewuseractivitiesreport', $personalcontext) and !is_enrolled($coursecontext)) {
d9cb06dc 65 // do not require parents to be enrolled in courses ;-)
66 $PAGE->set_course($course);
67} else {
68 require_login($course);
69}
6bb91986 70
d9cb06dc 71if ($user->deleted) {
72 echo $OUTPUT->header();
73 echo $OUTPUT->heading(get_string('userdeleted'));
74 echo $OUTPUT->footer();
75 die;
76}
f5fc83e8 77
d9cb06dc 78// prepare list of allowed modes
79$myreports = ($course->showreports and $USER->id == $user->id);
80$anyreport = has_capability('moodle/user:viewuseractivitiesreport', $personalcontext);
358e2a0b 81
d9cb06dc 82$modes = array();
358e2a0b 83
abbee05e
FM
84// Used for grade reports, it represents whether we should be viewing the report as ourselves, or as the targetted user.
85$viewasuser = false;
86
d9cb06dc 87if (has_capability('moodle/grade:viewall', $coursecontext)) {
88 //ok - can view all course grades
89 $modes[] = 'grade';
358e2a0b 90
d9cb06dc 91} else if ($course->showgrades and $user->id == $USER->id and has_capability('moodle/grade:view', $coursecontext)) {
92 //ok - can view own grades
93 $modes[] = 'grade';
358e2a0b 94
d9cb06dc 95} else if ($course->showgrades and has_capability('moodle/grade:viewall', $personalcontext)) {
96 // ok - can view grades of this user - parent most probably
97 $modes[] = 'grade';
abbee05e 98 $viewasuser = true;
3c7da16d 99
d9cb06dc 100} else if ($course->showgrades and $anyreport) {
101 // ok - can view grades of this user - parent most probably
102 $modes[] = 'grade';
abbee05e 103 $viewasuser = true;
d9cb06dc 104}
358e2a0b 105
d9cb06dc 106if (empty($modes)) {
107 require_capability('moodle/user:viewuseractivitiesreport', $personalcontext);
108}
358e2a0b 109
d9cb06dc 110if (!in_array($mode, $modes)) {
7e85563d 111 // forbidden or non-existent mode
d9cb06dc 112 $mode = reset($modes);
113}
85a6e76c 114
f49151c3
PS
115$eventdata = array(
116 'context' => $coursecontext,
117 'relateduserid' => $user->id,
118 'other' => array('mode' => $mode),
119);
68a72358
AG
120$event = \core\event\course_user_report_viewed::create($eventdata);
121$event->trigger();
f9903ed0 122
d9cb06dc 123$stractivityreport = get_string("activityreport");
7a7e209d
SH
124
125$PAGE->navigation->extend_for_user($user);
870815fa 126$PAGE->navigation->set_userid_for_parent_checks($user->id); // see MDL-25805 for reasons and for full commit reference for reversal when fixed.
d9cb06dc 127$PAGE->set_title("$course->shortname: $stractivityreport ($mode)");
c78dbe03 128$PAGE->set_heading(fullname($user));
f9a0ea69 129
d9cb06dc 130switch ($mode) {
131 case "grade":
c9451960
AG
132 // Change the navigation to point to the my grade node (If we are a student).
133 if ($USER->id == $user->id) {
134 require_once($CFG->dirroot . '/user/lib.php');
c78dbe03
AG
135 // Make the dashboard active so that it shows up in the navbar correctly.
136 $gradenode = $PAGE->settingsnav->find('dashboard', null)->make_active();
4887d152 137 // Get the correct 'Grades' url to point to.
c9451960 138 $activeurl = user_mygrades_url();
4887d152 139 $navbar = $PAGE->navbar->add(get_string('grades', 'grades'), $activeurl, navigation_node::TYPE_SETTING);
c78dbe03 140 $activenode = $navbar->add($course->shortname);
c9451960
AG
141 $activenode->make_active();
142 // Find the course node and collapse it.
143 $coursenode = $PAGE->navigation->find($course->id, navigation_node::TYPE_COURSE);
144 $coursenode->collapse = true;
145 $coursenode->make_inactive();
f495187d 146 $url = new moodle_url('/course/user.php', array('id' => $id, 'user' => $user->id, 'mode' => $mode));
c78dbe03
AG
147 $reportnode = $activenode->add(get_string('pluginname', 'gradereport_user'), $url);
148 } else {
12261113
AG
149 if ($course->id == SITEID) {
150 $activenode = $PAGE->navigation->find('user' . $user->id, null);
151 } else {
4b937932 152 $currentcoursenode = $PAGE->navigation->find($course->id, navigation_node::TYPE_COURSE);
12261113
AG
153 $activenode = $currentcoursenode->find_active_node();
154 }
4b937932 155
c78dbe03 156 // Check to see if the active node is a user name.
12261113 157 if (!preg_match('/^user\d{0,}$/', $activenode->key)) { // No user name found.
c78dbe03
AG
158 $userurl = new moodle_url('/user/view.php', array('id' => $user->id, 'course' => $course->id));
159 // Add the user name.
160 $PAGE->navbar->add(fullname($user), $userurl, navigation_node::TYPE_SETTING);
161 }
12261113 162 $PAGE->navbar->add(get_string('report'));
45367bdf 163 $gradeurl = new moodle_url('/course/user.php', array('id' => $id, 'user' => $user->id, 'mode' => $mode));
c78dbe03
AG
164 // Add the 'grades' node to the navbar.
165 $navbar = $PAGE->navbar->add(get_string('grades', 'grades'), $gradeurl, navigation_node::TYPE_SETTING);
c9451960
AG
166 }
167 echo $OUTPUT->header();
168
d9cb06dc 169 if (empty($CFG->grade_profilereport) or !file_exists($CFG->dirroot.'/grade/report/'.$CFG->grade_profilereport.'/lib.php')) {
170 $CFG->grade_profilereport = 'user';
171 }
172 require_once $CFG->libdir.'/gradelib.php';
173 require_once $CFG->dirroot.'/grade/lib.php';
174 require_once $CFG->dirroot.'/grade/report/'.$CFG->grade_profilereport.'/lib.php';
175
176 $functionname = 'grade_report_'.$CFG->grade_profilereport.'_profilereport';
177 if (function_exists($functionname)) {
abbee05e 178 $functionname($course, $user, $viewasuser);
d9cb06dc 179 }
180 break;
181
d9cb06dc 182 break;
183 default:
e35bac3e
SL
184 // 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.
185 // Display the page header to avoid breaking the navigation. A course/user.php review will be done in MDL-49939.
c9451960 186 echo $OUTPUT->header();
d9cb06dc 187}
f9903ed0 188
7468bf01 189
d9cb06dc 190echo $OUTPUT->footer();