MDL-44017 Events: Added unit test for report_viewed events
[moodle.git] / user / view.php
blob2e008750b261f3f593a910b9b71bc1b2a1db0ab2
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 profile for a particular user
20 * @package core_user
21 * @copyright 1999 Martin Dougiamas http://dougiamas.com
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 require_once("../config.php");
26 require_once($CFG->dirroot.'/user/profile/lib.php');
27 require_once($CFG->dirroot.'/tag/lib.php');
28 require_once($CFG->libdir . '/filelib.php');
29 require_once($CFG->libdir . '/badgeslib.php');
31 $id = optional_param('id', 0, PARAM_INT); // User id.
32 $courseid = optional_param('course', SITEID, PARAM_INT); // course id (defaults to Site).
34 // See your own profile by default.
35 if (empty($id)) {
36 require_login();
37 $id = $USER->id;
40 if ($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.
44 $PAGE->set_url('/user/view.php', array('id' => $id, 'course' => $courseid));
46 $user = $DB->get_record('user', array('id' => $id), '*', MUST_EXIST);
47 $course = $DB->get_record('course', array('id' => $courseid), '*', MUST_EXIST);
48 $currentuser = ($user->id == $USER->id);
50 $systemcontext = context_system::instance();
51 $coursecontext = context_course::instance($course->id);
52 $usercontext = context_user::instance($user->id, IGNORE_MISSING);
54 // Require login first.
55 if (isguestuser($user)) {
56 // Can not view profile of guest - thre is nothing to see there.
57 print_error('invaliduserid');
60 if (!empty($CFG->forceloginforprofiles)) {
61 require_login(); // We can not log in to course due to the parent hack below.
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());
70 $PAGE->set_context($coursecontext);
71 $PAGE->set_course($course);
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.
74 $PAGE->set_other_editing_capability('moodle/course:manageactivities');
76 $isparent = false;
78 if (!$currentuser and !$user->deleted
79 and $DB->record_exists('role_assignments', array('userid' => $USER->id, 'contextid' => $usercontext->id))
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!
84 require_login();
85 $isparent = true;
86 $PAGE->navigation->set_userid_for_parent_checks($id);
87 } else {
88 // Normal course.
89 require_login($course);
90 // What to do with users temporary accessing this course? should they see the details?
93 $strpersonalprofile = get_string('personalprofile');
94 $strparticipants = get_string("participants");
95 $struser = get_string("user");
97 $fullname = fullname($user, has_capability('moodle/site:viewfullnames', $coursecontext));
99 // Now test the actual capabilities and enrolment in course.
100 if ($currentuser) {
101 if (!is_viewing($coursecontext) && !is_enrolled($coursecontext)) {
102 // Need to have full access to a course to see the rest of own info.
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']);
108 echo $OUTPUT->footer();
109 die;
112 } else {
113 // Somebody else.
114 $PAGE->set_title("$strpersonalprofile: ");
115 $PAGE->set_heading("$strpersonalprofile: ");
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.
120 print_error('cannotviewprofile');
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
126 // or test for course:inspect capability.
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'));
136 if (!empty($_SERVER['HTTP_REFERER'])) {
137 echo $OUTPUT->continue_button($_SERVER['HTTP_REFERER']);
139 echo $OUTPUT->footer();
140 exit;
143 // If groups are in use and enforced throughout the course, then make sure we can meet in at least one course level group.
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()) {
147 // Do not use require_login() here because we might have already used require_login($course).
148 redirect(get_login_url());
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)) {
153 print_error("groupnotamember", '', "../course/view.php?id=$course->id");
158 $PAGE->set_title("$course->fullname: $strpersonalprofile: $fullname");
159 $PAGE->set_heading($course->fullname);
160 $PAGE->set_pagelayout('standard');
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.
164 if (!$currentuser) {
165 $PAGE->navigation->extend_for_user($user);
166 if ($node = $PAGE->settingsnav->get('userviewingsettings'.$user->id)) {
167 $node->forceopen = true;
169 } else if ($node = $PAGE->settingsnav->get('usercurrentsettings', navigation_node::TYPE_CONTAINER)) {
170 $node->forceopen = true;
172 if ($node = $PAGE->settingsnav->get('courseadmin')) {
173 $node->forceopen = false;
176 echo $OUTPUT->header();
178 echo '<div class="userprofile">';
180 echo $OUTPUT->heading(fullname($user).' ('.format_string($course->shortname, true, array('context' => $coursecontext)).')');
182 if ($user->deleted) {
183 echo $OUTPUT->heading(get_string('userdeleted'));
184 if (!has_capability('moodle/user:update', $coursecontext)) {
185 echo $OUTPUT->footer();
186 die;
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(
193 'objectid' => $USER->id,
194 'relateduserid' => $user->id,
195 'context' => $usercontext,
196 'other' => array(
197 'courseid' => $course->id,
198 'courseshortname' => $course->shortname,
199 'coursefullname' => $course->fullname
202 $event->add_record_snapshot('user', $user);
203 $event->trigger();
205 // Get the hidden field list.
206 if (has_capability('moodle/user:viewhiddendetails', $coursecontext)) {
207 $hiddenfields = array();
208 } else {
209 $hiddenfields = array_flip(explode(',', $CFG->hiddenuserfields));
212 if (is_mnet_remote_user($user)) {
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";
218 $remotehost = $DB->get_record_sql($sql, array($user->mnethostid));
219 $a = new stdclass();
220 $a->remotetype = $remotehost->display_name;
221 $a->remotename = $remotehost->name;
222 $a->remoteurl = $remotehost->wwwroot;
224 echo $OUTPUT->box(get_string('remoteuserinfo', 'mnet', $a), 'remoteuserinfo');
227 echo '<div class="userprofilebox clearfix"><div class="profilepicture">';
228 echo $OUTPUT->user_picture($user, array('size' => 100));
229 echo '</div>';
231 // Print the description.
232 echo '<div class="descriptionbox"><div class="description">';
233 if ($user->description && !isset($hiddenfields['description'])) {
234 if (!empty($CFG->profilesforenrolledusersonly) && !$DB->record_exists('role_assignments', array('userid' => $id))) {
235 echo get_string('profilenotshown', 'moodle');
236 } else {
237 if ($courseid == SITEID) {
238 $user->description = file_rewrite_pluginfile_urls($user->description, 'pluginfile.php', $usercontext->id, 'user', 'profile', null);
239 } else {
240 // We have to make a little detour thought the course context to verify the access control for course profile.
241 $user->description = file_rewrite_pluginfile_urls($user->description, 'pluginfile.php', $coursecontext->id, 'user', 'profile', $user->id);
243 $options = array('overflowdiv' => true);
244 echo format_text($user->description, $user->descriptionformat, $options);
247 echo '</div>';
250 // Print all the little details in a list.
252 echo html_writer::start_tag('dl', array('class' => 'list'));
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.
258 if ($currentuser
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)) {
263 echo html_writer::tag('dt', get_string('email'));
264 echo html_writer::tag('dd', obfuscate_mailto($user->email, ''));
267 // Show last time this user accessed this course.
268 if (!isset($hiddenfields['lastaccess'])) {
269 if ($lastaccess = $DB->get_record('user_lastaccess', array('userid' => $user->id, 'courseid' => $course->id))) {
270 $datestring = userdate($lastaccess->timeaccess)."&nbsp; (".format_time(time() - $lastaccess->timeaccess).")";
271 } else {
272 $datestring = get_string("never");
274 echo html_writer::tag('dt', get_string('lastaccess'));
275 echo html_writer::tag('dd', $datestring);
278 // Show roles in this course.
279 if ($rolestring = get_user_roles_in_course($id, $course->id)) {
280 echo html_writer::tag('dt', get_string('roles'));
281 echo html_writer::tag('dd', $rolestring);
284 // Show groups this user is in.
285 if (!isset($hiddenfields['groups'])) {
286 $accessallgroups = has_capability('moodle/site:accessallgroups', $coursecontext);
287 if ($usergroups = groups_get_all_groups($course->id, $user->id)) {
288 $groupstr = '';
289 foreach ($usergroups as $group) {
290 if ($course->groupmode == SEPARATEGROUPS and !$accessallgroups and $user->id != $USER->id) {
291 if (!groups_is_member($group->id, $user->id)) {
292 continue;
296 if ($course->groupmode != NOGROUPS) {
297 $groupstr .= ' <a href="'.$CFG->wwwroot.'/user/index.php?id='.$course->id.'&amp;group='.$group->id.'">'.format_string($group->name).'</a>,';
298 } else {
299 $groupstr .= ' '.format_string($group->name); // The user/index.php shows groups only when course in group mode.
302 if ($groupstr !== '') {
303 echo html_writer::tag('dt', get_string('group'));
304 echo html_writer::tag('dd', rtrim($groupstr, ', '));
309 // Show other courses they may be in.
310 if (!isset($hiddenfields['mycourses'])) {
311 if ($mycourses = enrol_get_all_users_courses($user->id, true, null, 'visible DESC,sortorder ASC')) {
312 $shown = 0;
313 $courselisting = '';
314 foreach ($mycourses as $mycourse) {
315 if ($mycourse->category) {
316 context_helper::preload_from_record($mycourse);
317 $ccontext = context_course::instance($mycourse->id);
318 $cfullname = $ccontext->get_context_name(false);
319 if ($mycourse->id != $course->id) {
320 $class = '';
321 if ($mycourse->visible == 0) {
322 if (!has_capability('moodle/course:viewhiddencourses', $ccontext)) {
323 continue;
325 $class = 'class="dimmed"';
327 $courselisting .= "<a href=\"{$CFG->wwwroot}/user/view.php?id={$user->id}&amp;course={$mycourse->id}\" $class >"
328 . $cfullname . "</a>, ";
329 } else {
330 $courselisting .= $cfullname . ", ";
331 $PAGE->navbar->add($cfullname);
334 $shown++;
335 if ($shown >= 20) {
336 $courselisting .= "...";
337 break;
340 echo html_writer::tag('dt', get_string('courseprofiles'));
341 echo html_writer::tag('dd', rtrim($courselisting, ', '));
345 if (!empty($CFG->enablebadges) && !empty($CFG->badges_allowcoursebadges)) {
346 profile_display_badges($user->id, $courseid);
349 if (!isset($hiddenfields['suspended'])) {
350 if ($user->suspended) {
351 echo html_writer::tag('dt', "&nbsp;");
352 echo html_writer::tag('dd', get_string('suspended', 'auth'));
356 if (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));
359 $ipstring = html_writer::link($iplookupurl, $user->lastip);
360 } else {
361 $ipstring = get_string("none");
363 echo html_writer::tag('dt', get_string('lastip'));
364 echo html_writer::tag('dd', $ipstring);
366 echo html_writer::end_tag('dl');
367 echo "</div></div>"; // Closing desriptionbox and userprofilebox.
368 // Print messaging link if allowed.
369 if (isloggedin() && has_capability('moodle/site:sendmessage', $usercontext)
370 && !empty($CFG->messaging) && !isguestuser() && !isguestuser($user) && ($USER->id != $user->id)) {
371 echo '<div class="messagebox">';
372 $sendmessageurl = new moodle_url('/message/index.php', array('id' => $user->id));
373 if ($courseid) {
374 $sendmessageurl->param('viewing', MESSAGE_VIEW_COURSE. $courseid);
376 echo html_writer::link($sendmessageurl, get_string('messageselectadd'));
377 echo '</div>';
380 if ($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>';
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.
393 echo '</div>'; // Userprofile class.
395 echo $OUTPUT->footer();