Merge branch 'MDL-62896-master' of git://github.com/k-holland/moodle
[moodle.git] / course / user.php
blobccf39f7d1b9f02a721214398a49d830e8e97a586
1 <?php
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.
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/>.
17 /**
18 * Display user activity reports for a course
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
25 require_once("../config.php");
26 require_once("lib.php");
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);
32 $url = new moodle_url('/course/user.php', array('id'=>$id,'user'=>$user, 'mode'=>$mode));
34 $course = $DB->get_record('course', array('id'=>$id), '*', MUST_EXIST);
35 $user = $DB->get_record("user", array("id"=>$user, 'deleted'=>0), '*', MUST_EXIST);
37 if ($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);
41 if ($mode === 'todaylogs' or $mode === 'alllogs') {
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);
46 if ($mode === 'stats') {
47 $url = new moodle_url('/report/stats/user.php', array('id'=>$user->id, 'course'=>$course->id));
48 redirect($url);
50 if ($mode === 'coursecompletions' or $mode === 'coursecompletion') {
51 $url = new moodle_url('/report/completion/user.php', array('id'=>$user->id, 'course'=>$course->id));
52 redirect($url);
55 $coursecontext = context_course::instance($course->id);
56 $personalcontext = context_user::instance($user->id);
58 $PAGE->set_context($personalcontext);
60 $PAGE->set_url('/course/user.php', array('id'=>$id, 'user'=>$user->id, 'mode'=>$mode));
62 require_login();
63 $PAGE->set_pagelayout('report');
64 if (has_capability('moodle/user:viewuseractivitiesreport', $personalcontext) and !is_enrolled($coursecontext)) {
65 // do not require parents to be enrolled in courses ;-)
66 $PAGE->set_course($course);
67 } else {
68 require_login($course);
71 if ($user->deleted) {
72 echo $OUTPUT->header();
73 echo $OUTPUT->heading(get_string('userdeleted'));
74 echo $OUTPUT->footer();
75 die;
78 // prepare list of allowed modes
79 $myreports = ($course->showreports and $USER->id == $user->id);
80 $anyreport = has_capability('moodle/user:viewuseractivitiesreport', $personalcontext);
82 $modes = array();
84 // Used for grade reports, it represents whether we should be viewing the report as ourselves, or as the targetted user.
85 $viewasuser = false;
87 if (has_capability('moodle/grade:viewall', $coursecontext)) {
88 //ok - can view all course grades
89 $modes[] = 'grade';
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';
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';
98 $viewasuser = true;
100 } else if ($course->showgrades and $anyreport) {
101 // ok - can view grades of this user - parent most probably
102 $modes[] = 'grade';
103 $viewasuser = true;
106 if (empty($modes)) {
107 require_capability('moodle/user:viewuseractivitiesreport', $personalcontext);
110 if (!in_array($mode, $modes)) {
111 // forbidden or non-existent mode
112 $mode = reset($modes);
115 $eventdata = array(
116 'context' => $coursecontext,
117 'relateduserid' => $user->id,
118 'other' => array('mode' => $mode),
120 $event = \core\event\course_user_report_viewed::create($eventdata);
121 $event->trigger();
123 $stractivityreport = get_string("activityreport");
125 $PAGE->navigation->extend_for_user($user);
126 $PAGE->navigation->set_userid_for_parent_checks($user->id); // see MDL-25805 for reasons and for full commit reference for reversal when fixed.
127 $PAGE->set_title("$course->shortname: $stractivityreport ($mode)");
128 $PAGE->set_heading(fullname($user));
130 switch ($mode) {
131 case "grade":
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');
135 // Make the dashboard active so that it shows up in the navbar correctly.
136 $gradenode = $PAGE->settingsnav->find('dashboard', null)->make_active();
137 // Get the correct 'Grades' url to point to.
138 $activeurl = user_mygrades_url();
139 $navbar = $PAGE->navbar->add(get_string('grades', 'grades'), $activeurl, navigation_node::TYPE_SETTING);
140 $activenode = $navbar->add($course->shortname);
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();
146 $url = new moodle_url('/course/user.php', array('id' => $id, 'user' => $user->id, 'mode' => $mode));
147 $reportnode = $activenode->add(get_string('pluginname', 'gradereport_user'), $url);
148 } else {
149 if ($course->id == SITEID) {
150 $activenode = $PAGE->navigation->find('user' . $user->id, null);
151 } else {
152 $currentcoursenode = $PAGE->navigation->find($course->id, navigation_node::TYPE_COURSE);
153 $activenode = $currentcoursenode->find_active_node();
156 // Check to see if the active node is a user name.
157 if (!preg_match('/^user\d{0,}$/', $activenode->key)) { // No user name found.
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);
162 $PAGE->navbar->add(get_string('report'));
163 $gradeurl = new moodle_url('/course/user.php', array('id' => $id, 'user' => $user->id, 'mode' => $mode));
164 // Add the 'grades' node to the navbar.
165 $navbar = $PAGE->navbar->add(get_string('grades', 'grades'), $gradeurl, navigation_node::TYPE_SETTING);
167 echo $OUTPUT->header();
169 if (empty($CFG->grade_profilereport) or !file_exists($CFG->dirroot.'/grade/report/'.$CFG->grade_profilereport.'/lib.php')) {
170 $CFG->grade_profilereport = 'user';
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';
176 $functionname = 'grade_report_'.$CFG->grade_profilereport.'_profilereport';
177 if (function_exists($functionname)) {
178 $functionname($course, $user, $viewasuser);
180 break;
182 break;
183 default:
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.
186 echo $OUTPUT->header();
190 echo $OUTPUT->footer();