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