Merge branch 'master_MDL-45726' of https://github.com/danmarsden/moodle
[moodle.git] / user / view.php
blob7bad594a4ff160ed8f79575796ac02068b78b898
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 'courseid' => $course->id,
196 'context' => $coursecontext,
197 'other' => array(
198 'courseid' => $course->id,
199 'courseshortname' => $course->shortname,
200 'coursefullname' => $course->fullname
203 $event->add_record_snapshot('user', $user);
204 $event->trigger();
206 // Get the hidden field list.
207 if (has_capability('moodle/user:viewhiddendetails', $coursecontext)) {
208 $hiddenfields = array();
209 } else {
210 $hiddenfields = array_flip(explode(',', $CFG->hiddenuserfields));
213 if (is_mnet_remote_user($user)) {
214 $sql = "SELECT h.id, h.name, h.wwwroot,
215 a.name as application, a.display_name
216 FROM {mnet_host} h, {mnet_application} a
217 WHERE h.id = ? AND h.applicationid = a.id";
219 $remotehost = $DB->get_record_sql($sql, array($user->mnethostid));
220 $a = new stdclass();
221 $a->remotetype = $remotehost->display_name;
222 $a->remotename = $remotehost->name;
223 $a->remoteurl = $remotehost->wwwroot;
225 echo $OUTPUT->box(get_string('remoteuserinfo', 'mnet', $a), 'remoteuserinfo');
228 echo '<div class="userprofilebox clearfix"><div class="profilepicture">';
229 echo $OUTPUT->user_picture($user, array('size' => 100));
230 echo '</div>';
232 // Print the description.
233 echo '<div class="descriptionbox"><div class="description">';
234 if ($user->description && !isset($hiddenfields['description'])) {
235 if (!empty($CFG->profilesforenrolledusersonly) && !$DB->record_exists('role_assignments', array('userid' => $id))) {
236 echo get_string('profilenotshown', 'moodle');
237 } else {
238 if ($courseid == SITEID) {
239 $user->description = file_rewrite_pluginfile_urls($user->description, 'pluginfile.php', $usercontext->id, 'user', 'profile', null);
240 } else {
241 // We have to make a little detour thought the course context to verify the access control for course profile.
242 $user->description = file_rewrite_pluginfile_urls($user->description, 'pluginfile.php', $coursecontext->id, 'user', 'profile', $user->id);
244 $options = array('overflowdiv' => true);
245 echo format_text($user->description, $user->descriptionformat, $options);
248 echo '</div>';
251 // Print all the little details in a list.
253 echo html_writer::start_tag('dl', array('class' => 'list'));
254 // Show email if any of the following conditions match.
255 // 1. User is viewing his own profile.
256 // 2. Has allowed everyone to see email
257 // 3. User has allowed course members to can see email and current user is in same course
258 // 4. Has either course:viewhiddenuserfields or site:viewuseridentity capability.
259 if ($currentuser
260 or $user->maildisplay == 1
261 or ($user->maildisplay == 2 && is_enrolled($coursecontext, $USER))
262 or has_capability('moodle/course:viewhiddenuserfields', $coursecontext)
263 or has_capability('moodle/site:viewuseridentity', $coursecontext)) {
264 echo html_writer::tag('dt', get_string('email'));
265 echo html_writer::tag('dd', obfuscate_mailto($user->email, ''));
268 // Show last time this user accessed this course.
269 if (!isset($hiddenfields['lastaccess'])) {
270 if ($lastaccess = $DB->get_record('user_lastaccess', array('userid' => $user->id, 'courseid' => $course->id))) {
271 $datestring = userdate($lastaccess->timeaccess)."&nbsp; (".format_time(time() - $lastaccess->timeaccess).")";
272 } else {
273 $datestring = get_string("never");
275 echo html_writer::tag('dt', get_string('lastaccess'));
276 echo html_writer::tag('dd', $datestring);
279 // Show roles in this course.
280 if ($rolestring = get_user_roles_in_course($id, $course->id)) {
281 echo html_writer::tag('dt', get_string('roles'));
282 echo html_writer::tag('dd', $rolestring);
285 // Show groups this user is in.
286 if (!isset($hiddenfields['groups'])) {
287 $accessallgroups = has_capability('moodle/site:accessallgroups', $coursecontext);
288 if ($usergroups = groups_get_all_groups($course->id, $user->id)) {
289 $groupstr = '';
290 foreach ($usergroups as $group) {
291 if ($course->groupmode == SEPARATEGROUPS and !$accessallgroups and $user->id != $USER->id) {
292 if (!groups_is_member($group->id, $user->id)) {
293 continue;
297 if ($course->groupmode != NOGROUPS) {
298 $groupstr .= ' <a href="'.$CFG->wwwroot.'/user/index.php?id='.$course->id.'&amp;group='.$group->id.'">'.format_string($group->name).'</a>,';
299 } else {
300 $groupstr .= ' '.format_string($group->name); // The user/index.php shows groups only when course in group mode.
303 if ($groupstr !== '') {
304 echo html_writer::tag('dt', get_string('group'));
305 echo html_writer::tag('dd', rtrim($groupstr, ', '));
310 // Show other courses they may be in.
311 if (!isset($hiddenfields['mycourses'])) {
312 if ($mycourses = enrol_get_all_users_courses($user->id, true, null, 'visible DESC,sortorder ASC')) {
313 $shown = 0;
314 $courselisting = '';
315 foreach ($mycourses as $mycourse) {
316 if ($mycourse->category) {
317 context_helper::preload_from_record($mycourse);
318 $ccontext = context_course::instance($mycourse->id);
319 $cfullname = $ccontext->get_context_name(false);
320 if ($mycourse->id != $course->id) {
321 $class = '';
322 if ($mycourse->visible == 0) {
323 if (!has_capability('moodle/course:viewhiddencourses', $ccontext)) {
324 continue;
326 $class = 'class="dimmed"';
328 $courselisting .= "<a href=\"{$CFG->wwwroot}/user/view.php?id={$user->id}&amp;course={$mycourse->id}\" $class >"
329 . $cfullname . "</a>, ";
330 } else {
331 $courselisting .= $cfullname . ", ";
332 $PAGE->navbar->add($cfullname);
335 $shown++;
336 if ($shown >= 20) {
337 $courselisting .= "...";
338 break;
341 echo html_writer::tag('dt', get_string('courseprofiles'));
342 echo html_writer::tag('dd', rtrim($courselisting, ', '));
346 if (!empty($CFG->enablebadges) && !empty($CFG->badges_allowcoursebadges)) {
347 profile_display_badges($user->id, $courseid);
350 if (!isset($hiddenfields['suspended'])) {
351 if ($user->suspended) {
352 echo html_writer::tag('dt', "&nbsp;");
353 echo html_writer::tag('dd', get_string('suspended', 'auth'));
357 if (has_capability('moodle/user:viewlastip', $usercontext) && !isset($hiddenfields['lastip'])) {
358 if ($user->lastip) {
359 $iplookupurl = new moodle_url('/iplookup/index.php', array('ip' => $user->lastip, 'user' => $USER->id));
360 $ipstring = html_writer::link($iplookupurl, $user->lastip);
361 } else {
362 $ipstring = get_string("none");
364 echo html_writer::tag('dt', get_string('lastip'));
365 echo html_writer::tag('dd', $ipstring);
367 echo html_writer::end_tag('dl');
368 echo "</div></div>"; // Closing desriptionbox and userprofilebox.
369 // Print messaging link if allowed.
370 if (isloggedin() && has_capability('moodle/site:sendmessage', $usercontext)
371 && !empty($CFG->messaging) && !isguestuser() && !isguestuser($user) && ($USER->id != $user->id)) {
372 echo '<div class="messagebox">';
373 $sendmessageurl = new moodle_url('/message/index.php', array('id' => $user->id));
374 if ($courseid) {
375 $sendmessageurl->param('viewing', MESSAGE_VIEW_COURSE. $courseid);
377 echo html_writer::link($sendmessageurl, get_string('messageselectadd'));
378 echo '</div>';
381 if ($currentuser || has_capability('moodle/user:viewdetails', $usercontext) || has_coursecontact_role($id)) {
382 echo '<div class="fullprofilelink">';
383 echo html_writer::link($CFG->wwwroot.'/user/profile.php?id='.$id, get_string('fullprofile'));
384 echo '</div>';
387 // TODO Add more useful overview info for teachers here, see below.
388 // Show links to notes made about this student (must click to display, for privacy).
389 // Recent comments made in this course.
390 // Recent blogs associated with this course and items in it.
394 echo '</div>'; // Userprofile class.
396 echo $OUTPUT->footer();