Merge branch 'wip-mdl-48190-m27' of https://github.com/rajeshtaneja/moodle into MOODL...
[moodle.git] / user / view.php
blob48e56c65a368ebe750423bb80dae45792434896c
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).
33 $showallcourses = optional_param('showallcourses', 0, PARAM_INT);
35 // See your own profile by default.
36 if (empty($id)) {
37 require_login();
38 $id = $USER->id;
41 if ($courseid == SITEID) { // Since Moodle 2.0 all site-level profiles are shown by profile.php.
42 redirect($CFG->wwwroot.'/user/profile.php?id='.$id); // Immediate redirect.
45 $PAGE->set_url('/user/view.php', array('id' => $id, 'course' => $courseid));
47 $user = $DB->get_record('user', array('id' => $id), '*', MUST_EXIST);
48 $course = $DB->get_record('course', array('id' => $courseid), '*', MUST_EXIST);
49 $currentuser = ($user->id == $USER->id);
51 $systemcontext = context_system::instance();
52 $coursecontext = context_course::instance($course->id);
53 $usercontext = context_user::instance($user->id, IGNORE_MISSING);
55 // Require login first.
56 if (isguestuser($user)) {
57 // Can not view profile of guest - thre is nothing to see there.
58 print_error('invaliduserid');
61 if (!empty($CFG->forceloginforprofiles)) {
62 require_login(); // We can not log in to course due to the parent hack below.
64 // Guests do not have permissions to view anyone's profile if forceloginforprofiles is set.
65 if (isguestuser()) {
66 $SESSION->wantsurl = $PAGE->url->out(false);
67 redirect(get_login_url());
71 $PAGE->set_context($coursecontext);
72 $PAGE->set_course($course);
73 $PAGE->set_pagetype('course-view-' . $course->format); // To get the blocks exactly like the course.
74 $PAGE->add_body_class('path-user'); // So we can style it independently.
75 $PAGE->set_other_editing_capability('moodle/course:manageactivities');
77 $isparent = false;
79 if (!$currentuser and !$user->deleted
80 and $DB->record_exists('role_assignments', array('userid' => $USER->id, 'contextid' => $usercontext->id))
81 and has_capability('moodle/user:viewdetails', $usercontext)) {
82 // TODO: very ugly hack - do not force "parents" to enrol into course their child is enrolled in,
83 // this way they may access the profile where they get overview of grades and child activity in course,
84 // please note this is just a guess!
85 require_login();
86 $isparent = true;
87 $PAGE->navigation->set_userid_for_parent_checks($id);
88 } else {
89 // Normal course.
90 require_login($course);
91 // What to do with users temporary accessing this course? should they see the details?
94 $strpersonalprofile = get_string('personalprofile');
95 $strparticipants = get_string("participants");
96 $struser = get_string("user");
98 $fullname = fullname($user, has_capability('moodle/site:viewfullnames', $coursecontext));
100 // Now test the actual capabilities and enrolment in course.
101 if ($currentuser) {
102 if (!is_viewing($coursecontext) && !is_enrolled($coursecontext)) {
103 // Need to have full access to a course to see the rest of own info.
104 echo $OUTPUT->header();
105 echo $OUTPUT->heading(get_string('notenrolled', '', $fullname));
106 if (!empty($_SERVER['HTTP_REFERER'])) {
107 echo $OUTPUT->continue_button($_SERVER['HTTP_REFERER']);
109 echo $OUTPUT->footer();
110 die;
113 } else {
114 // Somebody else.
115 $PAGE->set_title("$strpersonalprofile: ");
116 $PAGE->set_heading("$strpersonalprofile: ");
118 // Check course level capabilities.
119 if (!has_capability('moodle/user:viewdetails', $coursecontext) && // Normal enrolled user or mnager.
120 ($user->deleted or !has_capability('moodle/user:viewdetails', $usercontext))) { // Usually parent.
121 print_error('cannotviewprofile');
124 if (!is_enrolled($coursecontext, $user->id)) {
125 // TODO: the only potential problem is that managers and inspectors might post in forum, but the link
126 // to profile would not work - maybe a new capability - moodle/user:freely_acessile_profile_for_anybody
127 // or test for course:inspect capability.
128 if (has_capability('moodle/role:assign', $coursecontext)) {
129 $PAGE->navbar->add($fullname);
130 echo $OUTPUT->header();
131 echo $OUTPUT->heading(get_string('notenrolled', '', $fullname));
132 } else {
133 echo $OUTPUT->header();
134 $PAGE->navbar->add($struser);
135 echo $OUTPUT->heading(get_string('notenrolledprofile'));
137 if (!empty($_SERVER['HTTP_REFERER'])) {
138 echo $OUTPUT->continue_button($_SERVER['HTTP_REFERER']);
140 echo $OUTPUT->footer();
141 exit;
144 // If groups are in use and enforced throughout the course, then make sure we can meet in at least one course level group.
145 if (groups_get_course_groupmode($course) == SEPARATEGROUPS and $course->groupmodeforce
146 and !has_capability('moodle/site:accessallgroups', $coursecontext) and !has_capability('moodle/site:accessallgroups', $coursecontext, $user->id)) {
147 if (!isloggedin() or isguestuser()) {
148 // Do not use require_login() here because we might have already used require_login($course).
149 redirect(get_login_url());
151 $mygroups = array_keys(groups_get_all_groups($course->id, $USER->id, $course->defaultgroupingid, 'g.id, g.name'));
152 $usergroups = array_keys(groups_get_all_groups($course->id, $user->id, $course->defaultgroupingid, 'g.id, g.name'));
153 if (!array_intersect($mygroups, $usergroups)) {
154 print_error("groupnotamember", '', "../course/view.php?id=$course->id");
159 $PAGE->set_title("$course->fullname: $strpersonalprofile: $fullname");
160 $PAGE->set_heading($course->fullname);
161 $PAGE->set_pagelayout('standard');
163 // Locate the users settings in the settings navigation and force it open.
164 // This MUST be done after we've set up the page as it is going to cause theme and output to initialise.
165 if (!$currentuser) {
166 $PAGE->navigation->extend_for_user($user);
167 if ($node = $PAGE->settingsnav->get('userviewingsettings'.$user->id)) {
168 $node->forceopen = true;
170 } else if ($node = $PAGE->settingsnav->get('usercurrentsettings', navigation_node::TYPE_CONTAINER)) {
171 $node->forceopen = true;
173 if ($node = $PAGE->settingsnav->get('courseadmin')) {
174 $node->forceopen = false;
177 echo $OUTPUT->header();
179 echo '<div class="userprofile">';
181 echo $OUTPUT->heading(fullname($user).' ('.format_string($course->shortname, true, array('context' => $coursecontext)).')');
183 if ($user->deleted) {
184 echo $OUTPUT->heading(get_string('userdeleted'));
185 if (!has_capability('moodle/user:update', $coursecontext)) {
186 echo $OUTPUT->footer();
187 die;
191 // OK, security out the way, now we are showing the user.
192 // Trigger a user profile viewed event.
193 $event = \core\event\user_profile_viewed::create(array(
194 'objectid' => $user->id,
195 'relateduserid' => $user->id,
196 'courseid' => $course->id,
197 'context' => $coursecontext,
198 'other' => array(
199 'courseid' => $course->id,
200 'courseshortname' => $course->shortname,
201 'coursefullname' => $course->fullname
204 $event->add_record_snapshot('user', $user);
205 $event->trigger();
207 // Get the hidden field list.
208 if (has_capability('moodle/user:viewhiddendetails', $coursecontext)) {
209 $hiddenfields = array();
210 } else {
211 $hiddenfields = array_flip(explode(',', $CFG->hiddenuserfields));
214 if (is_mnet_remote_user($user)) {
215 $sql = "SELECT h.id, h.name, h.wwwroot,
216 a.name as application, a.display_name
217 FROM {mnet_host} h, {mnet_application} a
218 WHERE h.id = ? AND h.applicationid = a.id";
220 $remotehost = $DB->get_record_sql($sql, array($user->mnethostid));
221 $a = new stdclass();
222 $a->remotetype = $remotehost->display_name;
223 $a->remotename = $remotehost->name;
224 $a->remoteurl = $remotehost->wwwroot;
226 echo $OUTPUT->box(get_string('remoteuserinfo', 'mnet', $a), 'remoteuserinfo');
229 echo '<div class="userprofilebox clearfix"><div class="profilepicture">';
230 echo $OUTPUT->user_picture($user, array('size' => 100));
231 echo '</div>';
233 // Print the description.
234 echo '<div class="descriptionbox"><div class="description">';
235 if ($user->description && !isset($hiddenfields['description'])) {
236 if (!empty($CFG->profilesforenrolledusersonly) && !$DB->record_exists('role_assignments', array('userid' => $id))) {
237 echo get_string('profilenotshown', 'moodle');
238 } else {
239 if ($courseid == SITEID) {
240 $user->description = file_rewrite_pluginfile_urls($user->description, 'pluginfile.php', $usercontext->id, 'user', 'profile', null);
241 } else {
242 // We have to make a little detour thought the course context to verify the access control for course profile.
243 $user->description = file_rewrite_pluginfile_urls($user->description, 'pluginfile.php', $coursecontext->id, 'user', 'profile', $user->id);
245 $options = array('overflowdiv' => true);
246 echo format_text($user->description, $user->descriptionformat, $options);
249 echo '</div>';
252 // Print all the little details in a list.
254 echo html_writer::start_tag('dl', array('class' => 'list'));
255 // Show email if any of the following conditions match.
256 // 1. User is viewing his own profile.
257 // 2. Has allowed everyone to see email
258 // 3. User has allowed course members to can see email and current user is in same course
259 // 4. Has either course:viewhiddenuserfields or site:viewuseridentity capability.
260 if ($currentuser
261 or $user->maildisplay == 1
262 or ($user->maildisplay == 2 && is_enrolled($coursecontext, $USER))
263 or has_capability('moodle/course:viewhiddenuserfields', $coursecontext)
264 or has_capability('moodle/site:viewuseridentity', $coursecontext)) {
265 echo html_writer::tag('dt', get_string('email'));
266 echo html_writer::tag('dd', obfuscate_mailto($user->email, ''));
269 // Show last time this user accessed this course.
270 if (!isset($hiddenfields['lastaccess'])) {
271 if ($lastaccess = $DB->get_record('user_lastaccess', array('userid' => $user->id, 'courseid' => $course->id))) {
272 $datestring = userdate($lastaccess->timeaccess)."&nbsp; (".format_time(time() - $lastaccess->timeaccess).")";
273 } else {
274 $datestring = get_string("never");
276 echo html_writer::tag('dt', get_string('lastcourseaccess'));
277 echo html_writer::tag('dd', $datestring);
280 // Show roles in this course.
281 if ($rolestring = get_user_roles_in_course($id, $course->id)) {
282 echo html_writer::tag('dt', get_string('roles'));
283 echo html_writer::tag('dd', $rolestring);
286 // Show groups this user is in.
287 if (!isset($hiddenfields['groups'])) {
288 $accessallgroups = has_capability('moodle/site:accessallgroups', $coursecontext);
289 if ($usergroups = groups_get_all_groups($course->id, $user->id)) {
290 $groupstr = '';
291 foreach ($usergroups as $group) {
292 if ($course->groupmode == SEPARATEGROUPS and !$accessallgroups and $user->id != $USER->id) {
293 if (!groups_is_member($group->id, $user->id)) {
294 continue;
298 if ($course->groupmode != NOGROUPS) {
299 $groupstr .= ' <a href="'.$CFG->wwwroot.'/user/index.php?id='.$course->id.'&amp;group='.$group->id.'">'.format_string($group->name).'</a>,';
300 } else {
301 $groupstr .= ' '.format_string($group->name); // The user/index.php shows groups only when course in group mode.
304 if ($groupstr !== '') {
305 echo html_writer::tag('dt', get_string('group'));
306 echo html_writer::tag('dd', rtrim($groupstr, ', '));
311 // Show other courses they may be in.
312 if (!isset($hiddenfields['mycourses'])) {
313 if ($mycourses = enrol_get_all_users_courses($user->id, true, null, 'visible DESC,sortorder ASC')) {
314 $shown = 0;
315 $courselisting = '';
316 foreach ($mycourses as $mycourse) {
317 if ($mycourse->category) {
318 context_helper::preload_from_record($mycourse);
319 $ccontext = context_course::instance($mycourse->id);
320 $cfullname = $ccontext->get_context_name(false);
321 if ($mycourse->id != $course->id) {
322 $linkattributes = null;
323 if ($mycourse->visible == 0) {
324 if (!has_capability('moodle/course:viewhiddencourses', $ccontext)) {
325 continue;
327 $linkattributes['class'] = 'dimmed';
329 $params = array('id' => $user->id, 'course' => $mycourse->id);
330 if ($showallcourses) {
331 $params['showallcourses'] = 1;
333 $url = new moodle_url('/user/view.php', $params);
334 $courselisting .= html_writer::link($url, $ccontext->get_context_name(false), $linkattributes);
335 $courselisting .= ', ';
336 } else {
337 $courselisting .= $cfullname . ", ";
338 $PAGE->navbar->add($cfullname);
341 $shown++;
342 if (!$showallcourses && $shown >= 20) {
343 $url = new moodle_url('/user/view.php', array('id' => $user->id, 'course' => $courseid, 'showallcourses' => 1));
344 $courselisting .= html_writer::link($url, '...', array('title' => get_string('viewmore')));
345 break;
348 echo html_writer::tag('dt', get_string('courseprofiles'));
349 echo html_writer::tag('dd', rtrim($courselisting, ', '));
353 if (!empty($CFG->enablebadges) && !empty($CFG->badges_allowcoursebadges)) {
354 profile_display_badges($user->id, $courseid);
357 if (!isset($hiddenfields['suspended'])) {
358 if ($user->suspended) {
359 echo html_writer::tag('dt', "&nbsp;");
360 echo html_writer::tag('dd', get_string('suspended', 'auth'));
364 if (has_capability('moodle/user:viewlastip', $usercontext) && !isset($hiddenfields['lastip'])) {
365 if ($user->lastip) {
366 $iplookupurl = new moodle_url('/iplookup/index.php', array('ip' => $user->lastip, 'user' => $USER->id));
367 $ipstring = html_writer::link($iplookupurl, $user->lastip);
368 } else {
369 $ipstring = get_string("none");
371 echo html_writer::tag('dt', get_string('lastip'));
372 echo html_writer::tag('dd', $ipstring);
374 echo html_writer::end_tag('dl');
375 echo "</div></div>"; // Closing desriptionbox and userprofilebox.
376 // Print messaging link if allowed.
377 if (isloggedin() && has_capability('moodle/site:sendmessage', $usercontext)
378 && !empty($CFG->messaging) && !isguestuser() && !isguestuser($user) && ($USER->id != $user->id)) {
379 echo '<div class="messagebox">';
380 $sendmessageurl = new moodle_url('/message/index.php', array('id' => $user->id));
381 if ($courseid) {
382 $sendmessageurl->param('viewing', MESSAGE_VIEW_COURSE. $courseid);
384 echo html_writer::link($sendmessageurl, get_string('messageselectadd'));
385 echo '</div>';
388 if ($currentuser || has_capability('moodle/user:viewdetails', $usercontext) || has_coursecontact_role($id)) {
389 echo '<div class="fullprofilelink">';
390 echo html_writer::link($CFG->wwwroot.'/user/profile.php?id='.$id, get_string('fullprofile'));
391 echo '</div>';
394 // TODO Add more useful overview info for teachers here, see below.
395 // Show links to notes made about this student (must click to display, for privacy).
396 // Recent comments made in this course.
397 // Recent blogs associated with this course and items in it.
401 echo '</div>'; // Userprofile class.
403 echo $OUTPUT->footer();