MDL-41369 Set proper context and navigation path for user messages
[moodle.git] / user / view.php
CommitLineData
ce221eb5 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 profile for a particular user
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 user
24 */
25
26require_once("../config.php");
27require_once($CFG->dirroot.'/user/profile/lib.php');
28require_once($CFG->dirroot.'/tag/lib.php');
99d19c13 29require_once($CFG->libdir . '/filelib.php');
ce221eb5 30
4f0c2d00
PS
31$id = optional_param('id', 0, PARAM_INT); // user id
32$courseid = optional_param('course', SITEID, PARAM_INT); // course id (defaults to Site)
ce221eb5 33
03d9401e 34if (empty($id)) { // See your own profile by default
ce221eb5 35 require_login();
36 $id = $USER->id;
37}
f9903ed0 38
03d9401e
MD
39if ($courseid == SITEID) { // Since Moodle 2.0 all site-level profiles are shown by profile.php
40 redirect($CFG->wwwroot.'/user/profile.php?id='.$id); // Immediate redirect
ce221eb5 41}
03d9401e 42
81b58cc2 43$PAGE->set_url('/user/view.php', array('id'=>$id,'course'=>$courseid));
e41ddc4b 44
4f0c2d00 45$user = $DB->get_record('user', array('id'=>$id), '*', MUST_EXIST);
74df2951 46$course = $DB->get_record('course', array('id'=>$courseid), '*', MUST_EXIST);
03d9401e 47$currentuser = ($user->id == $USER->id);
f9903ed0 48
43731030
FM
49$systemcontext = context_system::instance();
50$coursecontext = context_course::instance($course->id);
51$usercontext = context_user::instance($user->id, IGNORE_MISSING);
1deff123 52
4f0c2d00
PS
53// Require login first
54if (isguestuser($user)) {
55 // can not view profile of guest - thre is nothing to see there
56 print_error('invaliduserid');
ce221eb5 57}
bad59bc0 58
81b58cc2 59if (!empty($CFG->forceloginforprofiles)) {
3ecc63e9
FM
60 require_login(); // We can not log in to course due to the parent hack below.
61
62 // Guests do not have permissions to view anyone's profile if forceloginforprofiles is set.
63 if (isguestuser()) {
64 $SESSION->wantsurl = $PAGE->url->out(false);
65 redirect(get_login_url());
66 }
81b58cc2
PS
67}
68
03d9401e 69$PAGE->set_context($coursecontext);
f5c1e621 70$PAGE->set_course($course);
99cca847
MD
71$PAGE->set_pagetype('course-view-' . $course->format); // To get the blocks exactly like the course
72$PAGE->add_body_class('path-user'); // So we can style it independently
03d9401e 73$PAGE->set_other_editing_capability('moodle/course:manageactivities');
cb4f6179 74
4f0c2d00 75$isparent = false;
0be6f678 76
589071ba 77if (!$currentuser and !$user->deleted
4f0c2d00
PS
78 and $DB->record_exists('role_assignments', array('userid'=>$USER->id, 'contextid'=>$usercontext->id))
79 and has_capability('moodle/user:viewdetails', $usercontext)) {
80 // TODO: very ugly hack - do not force "parents" to enrol into course their child is enrolled in,
81 // this way they may access the profile where they get overview of grades and child activity in course,
82 // please note this is just a guess!
ce221eb5 83 require_login();
4f0c2d00 84 $isparent = true;
870815fa 85 $PAGE->navigation->set_userid_for_parent_checks($id);
4f0c2d00
PS
86} else {
87 // normal course
88 require_login($course);
81b58cc2 89 // what to do with users temporary accessing this course? should they see the details?
ce221eb5 90}
f9903ed0 91
ce221eb5 92$strpersonalprofile = get_string('personalprofile');
93$strparticipants = get_string("participants");
94$struser = get_string("user");
f9903ed0 95
ce221eb5 96$fullname = fullname($user, has_capability('moodle/site:viewfullnames', $coursecontext));
f1603208 97
4f0c2d00
PS
98/// Now test the actual capabilities and enrolment in course
99if ($currentuser) {
100 // me
4b715423 101 if (!is_viewing($coursecontext) && !is_enrolled($coursecontext)) { // Need to have full access to a course to see the rest of own info
4f0c2d00
PS
102 echo $OUTPUT->header();
103 echo $OUTPUT->heading(get_string('notenrolled', '', $fullname));
104 if (!empty($_SERVER['HTTP_REFERER'])) {
105 echo $OUTPUT->continue_button($_SERVER['HTTP_REFERER']);
106 }
107 echo $OUTPUT->footer();
108 die;
109 }
caa8363f 110
4f0c2d00
PS
111} else {
112 // somebody else
ce221eb5 113 $PAGE->set_title("$strpersonalprofile: ");
114 $PAGE->set_heading("$strpersonalprofile: ");
caa8363f 115
03d9401e
MD
116 // check course level capabilities
117 if (!has_capability('moodle/user:viewdetails', $coursecontext) && // normal enrolled user or mnager
589071ba 118 ($user->deleted or !has_capability('moodle/user:viewdetails', $usercontext))) { // usually parent
03d9401e
MD
119 print_error('cannotviewprofile');
120 }
4f0c2d00 121
03d9401e
MD
122 if (!is_enrolled($coursecontext, $user->id)) {
123 // TODO: the only potential problem is that managers and inspectors might post in forum, but the link
124 // to profile would not work - maybe a new capability - moodle/user:freely_acessile_profile_for_anybody
125 // or test for course:inspect capability
126 if (has_capability('moodle/role:assign', $coursecontext)) {
127 $PAGE->navbar->add($fullname);
128 echo $OUTPUT->header();
129 echo $OUTPUT->heading(get_string('notenrolled', '', $fullname));
130 } else {
131 echo $OUTPUT->header();
132 $PAGE->navbar->add($struser);
133 echo $OUTPUT->heading(get_string('notenrolledprofile'));
ce221eb5 134 }
03d9401e
MD
135 if (!empty($_SERVER['HTTP_REFERER'])) {
136 echo $OUTPUT->continue_button($_SERVER['HTTP_REFERER']);
fa22fd5f 137 }
03d9401e
MD
138 echo $OUTPUT->footer();
139 exit;
ce221eb5 140 }
499e78b4 141
4f0c2d00
PS
142 // If groups are in use and enforced throughout the course, then make sure we can meet in at least one course level group
143 if (groups_get_course_groupmode($course) == SEPARATEGROUPS and $course->groupmodeforce
144 and !has_capability('moodle/site:accessallgroups', $coursecontext) and !has_capability('moodle/site:accessallgroups', $coursecontext, $user->id)) {
145 if (!isloggedin() or isguestuser()) {
146 // do not use require_login() here because we might have already used require_login($course)
147 redirect(get_login_url());
148 }
149 $mygroups = array_keys(groups_get_all_groups($course->id, $USER->id, $course->defaultgroupingid, 'g.id, g.name'));
150 $usergroups = array_keys(groups_get_all_groups($course->id, $user->id, $course->defaultgroupingid, 'g.id, g.name'));
151 if (!array_intersect($mygroups, $usergroups)) {
ce221eb5 152 print_error("groupnotamember", '', "../course/view.php?id=$course->id");
dd780a3b 153 }
154 }
ce221eb5 155}
0be6f678 156
8b03c251
SH
157$PAGE->set_title("$course->fullname: $strpersonalprofile: $fullname");
158$PAGE->set_heading($course->fullname);
159$PAGE->set_pagelayout('standard');
f5ecf2e9 160
8b03c251
SH
161// Locate the users settings in the settings navigation and force it open.
162// This MUST be done after we've set up the page as it is going to cause theme and output to initialise.
87c215de
SH
163if (!$currentuser) {
164 $PAGE->navigation->extend_for_user($user);
5d07e957 165 if ($node = $PAGE->settingsnav->get('userviewingsettings'.$user->id)) {
87c215de
SH
166 $node->forceopen = true;
167 }
168} else if ($node = $PAGE->settingsnav->get('usercurrentsettings', navigation_node::TYPE_CONTAINER)) {
169 $node->forceopen = true;
170}
171if ($node = $PAGE->settingsnav->get('courseadmin')) {
172 $node->forceopen = false;
173}
174
ce221eb5 175echo $OUTPUT->header();
99cca847
MD
176
177echo '<div class="userprofile">';
178
8ebbb06a 179echo $OUTPUT->heading(fullname($user).' ('.format_string($course->shortname, true, array('context' => $coursecontext)).')');
b1d530d2 180
ce221eb5 181if ($user->deleted) {
182 echo $OUTPUT->heading(get_string('userdeleted'));
183 if (!has_capability('moodle/user:update', $coursecontext)) {
184 echo $OUTPUT->footer();
185 die;
bb09fb11 186 }
ce221eb5 187}
bb09fb11 188
bad59bc0 189/// OK, security out the way, now we are showing the user
190
ce221eb5 191add_to_log($course->id, "user", "view", "view.php?id=$user->id&course=$course->id", "$user->id");
bad59bc0 192
4801fe93 193/// Get the hidden field list
ce221eb5 194if (has_capability('moodle/user:viewhiddendetails', $coursecontext)) {
195 $hiddenfields = array();
196} else {
197 $hiddenfields = array_flip(explode(',', $CFG->hiddenuserfields));
198}
f9903ed0 199
ce221eb5 200if (is_mnet_remote_user($user)) {
5db29f49
DM
201 $sql = "SELECT h.id, h.name, h.wwwroot,
202 a.name as application, a.display_name
203 FROM {mnet_host} h, {mnet_application} a
204 WHERE h.id = ? AND h.applicationid = a.id";
ce8c75ee 205
ce221eb5 206 $remotehost = $DB->get_record_sql($sql, array($user->mnethostid));
5db29f49
DM
207 $a = new stdclass();
208 $a->remotetype = $remotehost->display_name;
209 $a->remotename = $remotehost->name;
210 $a->remoteurl = $remotehost->wwwroot;
0be6f678 211
5db29f49 212 echo $OUTPUT->box(get_string('remoteuserinfo', 'mnet', $a), 'remoteuserinfo');
ce221eb5 213}
56f52742 214
e4f0c78a 215echo '<div class="userprofilebox clearfix"><div class="profilepicture">';
03d9401e
MD
216echo $OUTPUT->user_picture($user, array('size'=>100));
217echo '</div>';
f9903ed0 218
03d9401e 219// Print the description
e4f0c78a 220echo '<div class="descriptionbox"><div class="description">';
ce221eb5 221if ($user->description && !isset($hiddenfields['description'])) {
03d9401e 222 if (!empty($CFG->profilesforenrolledusersonly) && !$DB->record_exists('role_assignments', array('userid'=>$id))) {
99cca847 223 echo get_string('profilenotshown', 'moodle');
ce221eb5 224 } else {
64f93798
PS
225 if ($courseid == SITEID) {
226 $user->description = file_rewrite_pluginfile_urls($user->description, 'pluginfile.php', $usercontext->id, 'user', 'profile', null);
227 } else {
228 // we have to make a little detour thought the course context to verify the access control for course profile
229 $user->description = file_rewrite_pluginfile_urls($user->description, 'pluginfile.php', $coursecontext->id, 'user', 'profile', $user->id);
230 }
367a75fa
SH
231 $options = array('overflowdiv'=>true);
232 echo format_text($user->description, $user->descriptionformat, $options);
f9903ed0 233 }
ce221eb5 234}
03d9401e
MD
235echo '</div>';
236
f9903ed0 237
ce221eb5 238// Print all the little details in a list
f9903ed0 239
6e5d002e 240echo html_writer::start_tag('dl', array('class'=>'list'));
96214a49
RT
241// Show email if any of the following conditions match.
242// 1. User is viewing his own profile.
243// 2. Has allowed everyone to see email
244// 3. User has allowed course members to can see email and current user is in same course
245// 4. Has either course:viewhiddenuserfields or site:viewuseridentity capability.
8d22e342 246if ($currentuser
96214a49
RT
247 or $user->maildisplay == 1
248 or ($user->maildisplay == 2 && is_enrolled($coursecontext, $USER))
249 or has_capability('moodle/course:viewhiddenuserfields', $coursecontext)
250 or has_capability('moodle/site:viewuseridentity', $coursecontext)) {
6e5d002e
JF
251 echo html_writer::tag('dt', get_string('email'));
252 echo html_writer::tag('dd', obfuscate_mailto($user->email, ''));
8d22e342
AD
253}
254
99cca847
MD
255// Show last time this user accessed this course
256if (!isset($hiddenfields['lastaccess'])) {
257 if ($lastaccess = $DB->get_record('user_lastaccess', array('userid'=>$user->id, 'courseid'=>$course->id))) {
258 $datestring = userdate($lastaccess->timeaccess)."&nbsp; (".format_time(time() - $lastaccess->timeaccess).")";
259 } else {
260 $datestring = get_string("never");
f9903ed0 261 }
6e5d002e
JF
262 echo html_writer::tag('dt', get_string('lastaccess'));
263 echo html_writer::tag('dd', $datestring);
ce221eb5 264}
f9903ed0 265
99cca847
MD
266// Show roles in this course
267if ($rolestring = get_user_roles_in_course($id, $course->id)) {
6e5d002e
JF
268 echo html_writer::tag('dt', get_string('roles'));
269 echo html_writer::tag('dd', $rolestring);
ce221eb5 270}
766d2bf3 271
99cca847
MD
272// Show groups this user is in
273if (!isset($hiddenfields['groups'])) {
a732add0
PS
274 $accessallgroups = has_capability('moodle/site:accessallgroups', $coursecontext);
275 if ($usergroups = groups_get_all_groups($course->id, $user->id)) {
276 $groupstr = '';
277 foreach ($usergroups as $group){
278 if ($course->groupmode == SEPARATEGROUPS and !$accessallgroups and $user->id != $USER->id) {
279 if (!groups_is_member($group->id, $user->id)) {
280 continue;
281 }
282 }
283
284 if ($course->groupmode != NOGROUPS) {
99cca847 285 $groupstr .= ' <a href="'.$CFG->wwwroot.'/user/index.php?id='.$course->id.'&amp;group='.$group->id.'">'.format_string($group->name).'</a>,';
a732add0
PS
286 } else {
287 $groupstr .= ' '.format_string($group->name); // the user/index.php shows groups only when course in group mode
99cca847 288 }
a732add0
PS
289 }
290 if ($groupstr !== '') {
6e5d002e
JF
291 echo html_writer::tag('dt', get_string('group'));
292 echo html_writer::tag('dd', rtrim($groupstr, ', '));
99cca847 293 }
f9903ed0 294 }
ce221eb5 295}
f9903ed0 296
99cca847 297// Show other courses they may be in
ce221eb5 298if (!isset($hiddenfields['mycourses'])) {
9ffd29ce 299 if ($mycourses = enrol_get_all_users_courses($user->id, true, NULL, 'visible DESC,sortorder ASC')) {
99cca847 300 $shown = 0;
ce221eb5 301 $courselisting = '';
302 foreach ($mycourses as $mycourse) {
303 if ($mycourse->category) {
31f40864
TL
304 context_helper::preload_from_record($mycourse);
305 $ccontext = context_course::instance($mycourse->id);
306 $cfullname = $ccontext->get_context_name(false);
ce221eb5 307 if ($mycourse->id != $course->id){
308 $class = '';
309 if ($mycourse->visible == 0) {
df997f84
PS
310 if (!has_capability('moodle/course:viewhiddencourses', $ccontext)) {
311 continue;
312 }
ce221eb5 313 $class = 'class="dimmed"';
472b647a 314 }
ce221eb5 315 $courselisting .= "<a href=\"{$CFG->wwwroot}/user/view.php?id={$user->id}&amp;course={$mycourse->id}\" $class >"
91d284c1 316 . $cfullname . "</a>, ";
99cca847 317 } else {
91d284c1
SH
318 $courselisting .= $cfullname . ", ";
319 $PAGE->navbar->add($cfullname);
9c72928d 320 }
321 }
ce221eb5 322 $shown++;
99cca847
MD
323 if ($shown >= 20) {
324 $courselisting .= "...";
ce221eb5 325 break;
326 }
9c72928d 327 }
6e5d002e
JF
328 echo html_writer::tag('dt', get_string('courseprofiles'));
329 echo html_writer::tag('dd', rtrim($courselisting,', '));
9c72928d 330 }
ce221eb5 331}
f2f085ee 332
4ad72c28
PS
333if (!isset($hiddenfields['suspended'])) {
334 if ($user->suspended) {
6e5d002e
JF
335 echo html_writer::tag('dt', "&nbsp;");
336 echo html_writer::tag('dd', get_string('suspended', 'auth'));
4ad72c28
PS
337 }
338}
6e5d002e
JF
339echo html_writer::end_tag('dl');
340echo "</div></div>"; // Closing desriptionbox and userprofilebox.
42ec733c
AD
341// Print messaging link if allowed
342if (isloggedin() && has_capability('moodle/site:sendmessage', $usercontext)
343 && !empty($CFG->messaging) && !isguestuser() && !isguestuser($user) && ($USER->id != $user->id)) {
344 echo '<div class="messagebox">';
0e1bd87c
MG
345 $sendmessageurl = new moodle_url('/message/index.php', array('id' => $user->id));
346 if ($courseid) {
347 $sendmessageurl->param('viewing', MESSAGE_VIEW_COURSE. $courseid);
348 }
349 echo html_writer::link($sendmessageurl, get_string('messageselectadd'));
42ec733c
AD
350 echo '</div>';
351}
352
46ea652f
SH
353if ($currentuser || has_capability('moodle/user:viewdetails', $usercontext) || has_coursecontact_role($id)) {
354 echo '<div class="fullprofilelink">';
355 echo html_writer::link($CFG->wwwroot.'/user/profile.php?id='.$id, get_string('fullprofile'));
356 echo '</div>';
357}
fa22fd5f 358
99cca847 359/// TODO Add more useful overview info for teachers here, see below
1e1c51a3 360
99cca847 361/// Show links to notes made about this student (must click to display, for privacy)
f9903ed0 362
99cca847 363/// Recent comments made in this course
210560e3 364
99cca847 365/// Recent blogs associated with this course and items in it
3086f3f6 366
3de34ccf 367
99cca847
MD
368
369echo '</div>'; // userprofile class
3de34ccf 370
4e516138 371echo $OUTPUT->footer();