2 // This file is part of Moodle - http://moodle.org/
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.
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/>.
18 * Display profile for a particular 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.
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 // Check we are not trying to view guest's profile.
56 if (isguestuser($user)) {
57 // Can not view profile of guest - thre is nothing to see there.
58 print_error('invaliduserid');
61 $PAGE->set_context($coursecontext);
63 if (!empty($CFG->forceloginforprofiles
)) {
64 require_login(); // We can not log in to course due to the parent hack below.
66 // Guests do not have permissions to view anyone's profile if forceloginforprofiles is set.
68 echo $OUTPUT->header();
69 echo $OUTPUT->confirm(get_string('guestcantaccessprofiles', 'error'),
72 echo $OUTPUT->footer();
77 $PAGE->set_course($course);
78 $PAGE->set_pagetype('course-view-' . $course->format
); // To get the blocks exactly like the course.
79 $PAGE->add_body_class('path-user'); // So we can style it independently.
80 $PAGE->set_other_editing_capability('moodle/course:manageactivities');
82 // Set the Moodle docs path explicitly because the default behaviour
83 // of inhereting the pagetype will lead to an incorrect docs location.
84 $PAGE->set_docs_path('user/profile');
88 if (!$currentuser and !$user->deleted
89 and $DB->record_exists('role_assignments', array('userid' => $USER->id
, 'contextid' => $usercontext->id
))
90 and has_capability('moodle/user:viewdetails', $usercontext)) {
91 // TODO: very ugly hack - do not force "parents" to enrol into course their child is enrolled in,
92 // this way they may access the profile where they get overview of grades and child activity in course,
93 // please note this is just a guess!
96 $PAGE->navigation
->set_userid_for_parent_checks($id);
99 require_login($course);
100 // What to do with users temporary accessing this course? should they see the details?
103 $strpersonalprofile = get_string('personalprofile');
104 $strparticipants = get_string("participants");
105 $struser = get_string("user");
107 $fullname = fullname($user, has_capability('moodle/site:viewfullnames', $coursecontext));
109 // Now test the actual capabilities and enrolment in course.
111 if (!is_viewing($coursecontext) && !is_enrolled($coursecontext)) {
112 // Need to have full access to a course to see the rest of own info.
113 echo $OUTPUT->header();
114 echo $OUTPUT->heading(get_string('notenrolled', '', $fullname));
115 $referer = clean_param($_SERVER['HTTP_REFERER'], PARAM_LOCALURL
);
116 if (!empty($referer)) {
117 echo $OUTPUT->continue_button($referer);
119 echo $OUTPUT->footer();
125 $PAGE->set_title("$strpersonalprofile: ");
126 $PAGE->set_heading("$strpersonalprofile: ");
128 // Check course level capabilities.
129 if (!has_capability('moodle/user:viewdetails', $coursecontext) && // Normal enrolled user or mnager.
130 ($user->deleted
or !has_capability('moodle/user:viewdetails', $usercontext))) { // Usually parent.
131 print_error('cannotviewprofile');
134 if (!is_enrolled($coursecontext, $user->id
)) {
135 // TODO: the only potential problem is that managers and inspectors might post in forum, but the link
136 // to profile would not work - maybe a new capability - moodle/user:freely_acessile_profile_for_anybody
137 // or test for course:inspect capability.
138 if (has_capability('moodle/role:assign', $coursecontext)) {
139 $PAGE->navbar
->add($fullname);
140 echo $OUTPUT->header();
141 echo $OUTPUT->heading(get_string('notenrolled', '', $fullname));
143 echo $OUTPUT->header();
144 $PAGE->navbar
->add($struser);
145 echo $OUTPUT->heading(get_string('notenrolledprofile'));
147 $referer = clean_param($_SERVER['HTTP_REFERER'], PARAM_LOCALURL
);
148 if (!empty($referer)) {
149 echo $OUTPUT->continue_button($referer);
151 echo $OUTPUT->footer();
155 // If groups are in use and enforced throughout the course, then make sure we can meet in at least one course level group.
156 // Except when we are a parent, in which case we would not be in any group.
157 if (groups_get_course_groupmode($course) == SEPARATEGROUPS
158 and $course->groupmodeforce
159 and !has_capability('moodle/site:accessallgroups', $coursecontext)
160 and !has_capability('moodle/site:accessallgroups', $coursecontext, $user->id
)
162 if (!isloggedin() or isguestuser()) {
163 // Do not use require_login() here because we might have already used require_login($course).
164 redirect(get_login_url());
166 $mygroups = array_keys(groups_get_all_groups($course->id
, $USER->id
, $course->defaultgroupingid
, 'g.id, g.name'));
167 $usergroups = array_keys(groups_get_all_groups($course->id
, $user->id
, $course->defaultgroupingid
, 'g.id, g.name'));
168 if (!array_intersect($mygroups, $usergroups)) {
169 print_error("groupnotamember", '', "../course/view.php?id=$course->id");
174 $PAGE->set_title("$course->fullname: $strpersonalprofile: $fullname");
175 $PAGE->set_heading($course->fullname
);
176 $PAGE->set_pagelayout('standard');
178 // Locate the users settings in the settings navigation and force it open.
179 // This MUST be done after we've set up the page as it is going to cause theme and output to initialise.
181 $PAGE->navigation
->extend_for_user($user);
182 if ($node = $PAGE->settingsnav
->get('userviewingsettings'.$user->id
)) {
183 $node->forceopen
= true;
185 } else if ($node = $PAGE->settingsnav
->get('usercurrentsettings', navigation_node
::TYPE_CONTAINER
)) {
186 $node->forceopen
= true;
188 if ($node = $PAGE->settingsnav
->get('courseadmin')) {
189 $node->forceopen
= false;
192 echo $OUTPUT->header();
194 echo '<div class="userprofile">';
196 echo $OUTPUT->heading(fullname($user).' ('.format_string($course->shortname
, true, array('context' => $coursecontext)).')');
198 if ($user->deleted
) {
199 echo $OUTPUT->heading(get_string('userdeleted'));
200 if (!has_capability('moodle/user:update', $coursecontext)) {
201 echo $OUTPUT->footer();
206 // OK, security out the way, now we are showing the user.
207 // Trigger a user profile viewed event.
208 $event = \core\event\user_profile_viewed
::create(array(
209 'objectid' => $user->id
,
210 'relateduserid' => $user->id
,
211 'courseid' => $course->id
,
212 'context' => $coursecontext,
214 'courseid' => $course->id
,
215 'courseshortname' => $course->shortname
,
216 'coursefullname' => $course->fullname
219 $event->add_record_snapshot('user', $user);
222 // Get the hidden field list.
223 if (has_capability('moodle/user:viewhiddendetails', $coursecontext)) {
224 $hiddenfields = array();
226 $hiddenfields = array_flip(explode(',', $CFG->hiddenuserfields
));
229 if (is_mnet_remote_user($user)) {
230 $sql = "SELECT h.id, h.name, h.wwwroot,
231 a.name as application, a.display_name
232 FROM {mnet_host} h, {mnet_application} a
233 WHERE h.id = ? AND h.applicationid = a.id";
235 $remotehost = $DB->get_record_sql($sql, array($user->mnethostid
));
237 $a->remotetype
= $remotehost->display_name
;
238 $a->remotename
= $remotehost->name
;
239 $a->remoteurl
= $remotehost->wwwroot
;
241 echo $OUTPUT->box(get_string('remoteuserinfo', 'mnet', $a), 'remoteuserinfo');
244 echo '<div class="userprofilebox clearfix"><div class="profilepicture">';
245 echo $OUTPUT->user_picture($user, array('size' => 100));
248 // Print the description.
249 echo '<div class="descriptionbox"><div class="description">';
250 if ($user->description
&& !isset($hiddenfields['description'])) {
251 if (!empty($CFG->profilesforenrolledusersonly
) && !$DB->record_exists('role_assignments', array('userid' => $id))) {
252 echo get_string('profilenotshown', 'moodle');
254 if ($courseid == SITEID
) {
255 $user->description
= file_rewrite_pluginfile_urls($user->description
, 'pluginfile.php', $usercontext->id
, 'user', 'profile', null);
257 // We have to make a little detour thought the course context to verify the access control for course profile.
258 $user->description
= file_rewrite_pluginfile_urls($user->description
, 'pluginfile.php', $coursecontext->id
, 'user', 'profile', $user->id
);
260 $options = array('overflowdiv' => true);
261 echo format_text($user->description
, $user->descriptionformat
, $options);
267 // Print all the little details in a list.
269 echo html_writer
::start_tag('dl', array('class' => 'list'));
270 // Show email if any of the following conditions match.
271 // 1. User is viewing his own profile.
272 // 2. Has allowed everyone to see email
273 // 3. User has allowed course members to can see email and current user is in same course
274 // 4. Has either course:viewhiddenuserfields or site:viewuseridentity capability.
276 or $user->maildisplay
== 1
277 or ($user->maildisplay
== 2 && is_enrolled($coursecontext, $USER))
278 or has_capability('moodle/course:viewhiddenuserfields', $coursecontext)
279 or has_capability('moodle/site:viewuseridentity', $coursecontext)) {
280 echo html_writer
::tag('dt', get_string('email'));
281 echo html_writer
::tag('dd', obfuscate_mailto($user->email
, ''));
284 // Show last time this user accessed this course.
285 if (!isset($hiddenfields['lastaccess'])) {
286 if ($lastaccess = $DB->get_record('user_lastaccess', array('userid' => $user->id
, 'courseid' => $course->id
))) {
287 $datestring = userdate($lastaccess->timeaccess
)." (".format_time(time() - $lastaccess->timeaccess
).")";
289 $datestring = get_string("never");
291 echo html_writer
::tag('dt', get_string('lastcourseaccess'));
292 echo html_writer
::tag('dd', $datestring);
295 // Show roles in this course.
296 if ($rolestring = get_user_roles_in_course($id, $course->id
)) {
297 echo html_writer
::tag('dt', get_string('roles'));
298 echo html_writer
::tag('dd', $rolestring);
301 // Show groups this user is in.
302 if (!isset($hiddenfields['groups'])) {
303 $accessallgroups = has_capability('moodle/site:accessallgroups', $coursecontext);
304 if ($usergroups = groups_get_all_groups($course->id
, $user->id
)) {
306 foreach ($usergroups as $group) {
307 if ($course->groupmode
== SEPARATEGROUPS
and !$accessallgroups and $user->id
!= $USER->id
) {
308 if (!groups_is_member($group->id
, $user->id
)) {
313 if ($course->groupmode
!= NOGROUPS
) {
314 $groupstr .= ' <a href="'.$CFG->wwwroot
.'/user/index.php?id='.$course->id
.'&group='.$group->id
.'">'.format_string($group->name
).'</a>,';
316 $groupstr .= ' '.format_string($group->name
); // The user/index.php shows groups only when course in group mode.
319 if ($groupstr !== '') {
320 echo html_writer
::tag('dt', get_string('group'));
321 echo html_writer
::tag('dd', rtrim($groupstr, ', '));
326 // Show other courses they may be in.
327 if (!isset($hiddenfields['mycourses'])) {
328 if ($mycourses = enrol_get_all_users_courses($user->id
, true, null, 'visible DESC,sortorder ASC')) {
331 foreach ($mycourses as $mycourse) {
332 if ($mycourse->category
) {
333 context_helper
::preload_from_record($mycourse);
334 $ccontext = context_course
::instance($mycourse->id
);
335 $cfullname = $ccontext->get_context_name(false);
336 if ($mycourse->id
!= $course->id
) {
337 $linkattributes = null;
338 if ($mycourse->visible
== 0) {
339 if (!has_capability('moodle/course:viewhiddencourses', $ccontext)) {
342 $linkattributes['class'] = 'dimmed';
344 $params = array('id' => $user->id
, 'course' => $mycourse->id
);
345 if ($showallcourses) {
346 $params['showallcourses'] = 1;
348 $url = new moodle_url('/user/view.php', $params);
349 $courselisting .= html_writer
::link($url, $ccontext->get_context_name(false), $linkattributes);
350 $courselisting .= ', ';
352 $courselisting .= $cfullname . ", ";
353 $PAGE->navbar
->add($cfullname);
357 if (!$showallcourses && $shown >= 20) {
358 $url = new moodle_url('/user/view.php', array('id' => $user->id
, 'course' => $courseid, 'showallcourses' => 1));
359 $courselisting .= html_writer
::link($url, '...', array('title' => get_string('viewmore')));
363 echo html_writer
::tag('dt', get_string('courseprofiles'));
364 echo html_writer
::tag('dd', rtrim($courselisting, ', '));
368 if (!empty($CFG->enablebadges
) && !empty($CFG->badges_allowcoursebadges
)) {
369 profile_display_badges($user->id
, $courseid);
372 if (!isset($hiddenfields['suspended'])) {
373 if ($user->suspended
) {
374 echo html_writer
::tag('dt', " ");
375 echo html_writer
::tag('dd', get_string('suspended', 'auth'));
379 if (has_capability('moodle/user:viewlastip', $usercontext) && !isset($hiddenfields['lastip'])) {
381 $iplookupurl = new moodle_url('/iplookup/index.php', array('ip' => $user->lastip
, 'user' => $USER->id
));
382 $ipstring = html_writer
::link($iplookupurl, $user->lastip
);
384 $ipstring = get_string("none");
386 echo html_writer
::tag('dt', get_string('lastip'));
387 echo html_writer
::tag('dd', $ipstring);
389 echo html_writer
::end_tag('dl');
390 echo "</div></div>"; // Closing desriptionbox and userprofilebox.
391 // Print messaging link if allowed.
392 if (isloggedin() && has_capability('moodle/site:sendmessage', $usercontext)
393 && !empty($CFG->messaging
) && !isguestuser() && !isguestuser($user) && ($USER->id
!= $user->id
)) {
394 echo '<div class="messagebox">';
395 $sendmessageurl = new moodle_url('/message/index.php', array('id' => $user->id
));
397 $sendmessageurl->param('viewing', MESSAGE_VIEW_COURSE
. $courseid);
399 echo html_writer
::link($sendmessageurl, get_string('messageselectadd'));
403 if (empty($CFG->forceloginforprofiles
) ||
$currentuser ||
has_capability('moodle/user:viewdetails', $usercontext)
404 ||
has_coursecontact_role($id)) {
405 echo '<div class="fullprofilelink">';
406 echo html_writer
::link($CFG->wwwroot
.'/user/profile.php?id='.$id, get_string('fullprofile'));
410 // TODO Add more useful overview info for teachers here, see below.
411 // Show links to notes made about this student (must click to display, for privacy).
412 // Recent comments made in this course.
413 // Recent blogs associated with this course and items in it.
417 echo '</div>'; // Userprofile class.
419 echo $OUTPUT->footer();