3 // This file is part of Moodle - http://moodle.org/
5 // Moodle is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
10 // Moodle is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
19 * 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
26 require_once("../config.php");
27 require_once($CFG->dirroot
.'/user/profile/lib.php');
28 require_once($CFG->dirroot
.'/tag/lib.php');
29 require_once($CFG->libdir
. '/filelib.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 if (empty($id)) { // See your own profile by default
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.
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');
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!
86 $PAGE->navigation
->set_userid_for_parent_checks($id);
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
102 if (!is_viewing($coursecontext) && !is_enrolled($coursecontext)) { // 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();
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));
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();
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.
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();
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,
198 'courseid' => $course->id
,
199 'courseshortname' => $course->shortname
,
200 'coursefullname' => $course->fullname
203 $event->add_record_snapshot('user', $user);
206 // Get the hidden field list.
207 if (has_capability('moodle/user:viewhiddendetails', $coursecontext)) {
208 $hiddenfields = array();
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
));
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));
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');
238 if ($courseid == SITEID
) {
239 $user->description
= file_rewrite_pluginfile_urls($user->description
, 'pluginfile.php', $usercontext->id
, 'user', 'profile', null);
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);
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.
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
)." (".format_time(time() - $lastaccess->timeaccess
).")";
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
)) {
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
)) {
297 if ($course->groupmode
!= NOGROUPS
) {
298 $groupstr .= ' <a href="'.$CFG->wwwroot
.'/user/index.php?id='.$course->id
.'&group='.$group->id
.'">'.format_string($group->name
).'</a>,';
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')) {
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 $linkattributes = null;
322 if ($mycourse->visible
== 0) {
323 if (!has_capability('moodle/course:viewhiddencourses', $ccontext)) {
326 $linkattributes['class'] = 'dimmed';
328 $params = array('id' => $user->id
, 'course' => $mycourse->id
);
329 if ($showallcourses) {
330 $params['showallcourses'] = 1;
332 $url = new moodle_url('/user/view.php', $params);
333 $courselisting .= html_writer
::link($url, $ccontext->get_context_name(false), $linkattributes);
334 $courselisting .= ', ';
336 $courselisting .= $cfullname . ", ";
337 $PAGE->navbar
->add($cfullname);
341 if (!$showallcourses && $shown >= 20) {
342 $url = new moodle_url('/user/view.php', array('id' => $user->id
, 'course' => $courseid, 'showallcourses' => 1));
343 $courselisting .= html_writer
::link($url, '...', array('title' => get_string('viewmore')));
347 echo html_writer
::tag('dt', get_string('courseprofiles'));
348 echo html_writer
::tag('dd', rtrim($courselisting,', '));
352 if (!isset($hiddenfields['suspended'])) {
353 if ($user->suspended
) {
354 echo html_writer
::tag('dt', " ");
355 echo html_writer
::tag('dd', get_string('suspended', 'auth'));
358 echo html_writer
::end_tag('dl');
359 echo "</div></div>"; // Closing desriptionbox and userprofilebox.
360 // Print messaging link if allowed
361 if (isloggedin() && has_capability('moodle/site:sendmessage', $usercontext)
362 && !empty($CFG->messaging
) && !isguestuser() && !isguestuser($user) && ($USER->id
!= $user->id
)) {
363 echo '<div class="messagebox">';
364 $sendmessageurl = new moodle_url('/message/index.php', array('id' => $user->id
));
366 $sendmessageurl->param('viewing', MESSAGE_VIEW_COURSE
. $courseid);
368 echo html_writer
::link($sendmessageurl, get_string('messageselectadd'));
372 if ($currentuser ||
has_capability('moodle/user:viewdetails', $usercontext) ||
has_coursecontact_role($id)) {
373 echo '<div class="fullprofilelink">';
374 echo html_writer
::link($CFG->wwwroot
.'/user/profile.php?id='.$id, get_string('fullprofile'));
378 /// TODO Add more useful overview info for teachers here, see below
380 /// Show links to notes made about this student (must click to display, for privacy)
382 /// Recent comments made in this course
384 /// Recent blogs associated with this course and items in it
388 echo '</div>'; // userprofile class
390 echo $OUTPUT->footer();