MDL-35780 Administration: email display on user profile view page will respect user...
[moodle.git] / user / view.php
CommitLineData
ce221eb5 1<?php
2
3// This file is part of Moodle - http://moodle.org/
4//
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.
9//
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.
14//
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/>.
17
18/**
19 * Display profile for a particular user
20 *
21 * @copyright 1999 Martin Dougiamas http://dougiamas.com
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23 * @package user
24 */
25
26require_once("../config.php");
27require_once($CFG->dirroot.'/user/profile/lib.php');
28require_once($CFG->dirroot.'/tag/lib.php');
99d19c13 29require_once($CFG->libdir . '/filelib.php');
ce221eb5 30
4f0c2d00
PS
31$id = optional_param('id', 0, PARAM_INT); // user id
32$courseid = optional_param('course', SITEID, PARAM_INT); // course id (defaults to Site)
ce221eb5 33
03d9401e 34if (empty($id)) { // See your own profile by default
ce221eb5 35 require_login();
36 $id = $USER->id;
37}
f9903ed0 38
03d9401e
MD
39if ($courseid == SITEID) { // Since Moodle 2.0 all site-level profiles are shown by profile.php
40 redirect($CFG->wwwroot.'/user/profile.php?id='.$id); // Immediate redirect
ce221eb5 41}
03d9401e 42
81b58cc2 43$PAGE->set_url('/user/view.php', array('id'=>$id,'course'=>$courseid));
e41ddc4b 44
4f0c2d00
PS
45$user = $DB->get_record('user', array('id'=>$id), '*', MUST_EXIST);
46$course = $DB->get_record('course', array('id'=>$courseid), '*', MUST_EXIST);
03d9401e 47$currentuser = ($user->id == $USER->id);
f9903ed0 48
ce221eb5 49$systemcontext = get_context_instance(CONTEXT_SYSTEM);
03d9401e 50$coursecontext = get_context_instance(CONTEXT_COURSE, $course->id);
589071ba 51$usercontext = get_context_instance(CONTEXT_USER, $user->id, IGNORE_MISSING);
1deff123 52
4f0c2d00
PS
53// Require login first
54if (isguestuser($user)) {
55 // can not view profile of guest - thre is nothing to see there
56 print_error('invaliduserid');
ce221eb5 57}
bad59bc0 58
81b58cc2 59if (!empty($CFG->forceloginforprofiles)) {
ce8df92d 60 require_login(); // we can not log in to course due to the parent hack below
81b58cc2
PS
61}
62
03d9401e 63$PAGE->set_context($coursecontext);
f5c1e621 64$PAGE->set_course($course);
99cca847
MD
65$PAGE->set_pagetype('course-view-' . $course->format); // To get the blocks exactly like the course
66$PAGE->add_body_class('path-user'); // So we can style it independently
03d9401e 67$PAGE->set_other_editing_capability('moodle/course:manageactivities');
cb4f6179 68
4f0c2d00 69$isparent = false;
0be6f678 70
589071ba 71if (!$currentuser and !$user->deleted
4f0c2d00
PS
72 and $DB->record_exists('role_assignments', array('userid'=>$USER->id, 'contextid'=>$usercontext->id))
73 and has_capability('moodle/user:viewdetails', $usercontext)) {
74 // TODO: very ugly hack - do not force "parents" to enrol into course their child is enrolled in,
75 // this way they may access the profile where they get overview of grades and child activity in course,
76 // please note this is just a guess!
ce221eb5 77 require_login();
4f0c2d00 78 $isparent = true;
870815fa 79 $PAGE->navigation->set_userid_for_parent_checks($id);
4f0c2d00
PS
80} else {
81 // normal course
82 require_login($course);
81b58cc2 83 // what to do with users temporary accessing this course? should they see the details?
ce221eb5 84}
f9903ed0 85
ce221eb5 86$strpersonalprofile = get_string('personalprofile');
87$strparticipants = get_string("participants");
88$struser = get_string("user");
f9903ed0 89
ce221eb5 90$fullname = fullname($user, has_capability('moodle/site:viewfullnames', $coursecontext));
f1603208 91
4f0c2d00
PS
92/// Now test the actual capabilities and enrolment in course
93if ($currentuser) {
94 // me
4b715423 95 if (!is_viewing($coursecontext) && !is_enrolled($coursecontext)) { // Need to have full access to a course to see the rest of own info
4f0c2d00
PS
96 echo $OUTPUT->header();
97 echo $OUTPUT->heading(get_string('notenrolled', '', $fullname));
98 if (!empty($_SERVER['HTTP_REFERER'])) {
99 echo $OUTPUT->continue_button($_SERVER['HTTP_REFERER']);
100 }
101 echo $OUTPUT->footer();
102 die;
103 }
caa8363f 104
4f0c2d00
PS
105} else {
106 // somebody else
ce221eb5 107 $PAGE->set_title("$strpersonalprofile: ");
108 $PAGE->set_heading("$strpersonalprofile: ");
caa8363f 109
03d9401e
MD
110 // check course level capabilities
111 if (!has_capability('moodle/user:viewdetails', $coursecontext) && // normal enrolled user or mnager
589071ba 112 ($user->deleted or !has_capability('moodle/user:viewdetails', $usercontext))) { // usually parent
03d9401e
MD
113 print_error('cannotviewprofile');
114 }
4f0c2d00 115
03d9401e
MD
116 if (!is_enrolled($coursecontext, $user->id)) {
117 // TODO: the only potential problem is that managers and inspectors might post in forum, but the link
118 // to profile would not work - maybe a new capability - moodle/user:freely_acessile_profile_for_anybody
119 // or test for course:inspect capability
120 if (has_capability('moodle/role:assign', $coursecontext)) {
121 $PAGE->navbar->add($fullname);
122 echo $OUTPUT->header();
123 echo $OUTPUT->heading(get_string('notenrolled', '', $fullname));
124 } else {
125 echo $OUTPUT->header();
126 $PAGE->navbar->add($struser);
127 echo $OUTPUT->heading(get_string('notenrolledprofile'));
ce221eb5 128 }
03d9401e
MD
129 if (!empty($_SERVER['HTTP_REFERER'])) {
130 echo $OUTPUT->continue_button($_SERVER['HTTP_REFERER']);
fa22fd5f 131 }
03d9401e
MD
132 echo $OUTPUT->footer();
133 exit;
ce221eb5 134 }
499e78b4 135
4f0c2d00
PS
136 // If groups are in use and enforced throughout the course, then make sure we can meet in at least one course level group
137 if (groups_get_course_groupmode($course) == SEPARATEGROUPS and $course->groupmodeforce
138 and !has_capability('moodle/site:accessallgroups', $coursecontext) and !has_capability('moodle/site:accessallgroups', $coursecontext, $user->id)) {
139 if (!isloggedin() or isguestuser()) {
140 // do not use require_login() here because we might have already used require_login($course)
141 redirect(get_login_url());
142 }
143 $mygroups = array_keys(groups_get_all_groups($course->id, $USER->id, $course->defaultgroupingid, 'g.id, g.name'));
144 $usergroups = array_keys(groups_get_all_groups($course->id, $user->id, $course->defaultgroupingid, 'g.id, g.name'));
145 if (!array_intersect($mygroups, $usergroups)) {
ce221eb5 146 print_error("groupnotamember", '', "../course/view.php?id=$course->id");
dd780a3b 147 }
148 }
ce221eb5 149}
0be6f678 150
e3ac1261
SH
151$PAGE->set_title("$course->fullname: $strpersonalprofile: $fullname");
152$PAGE->set_heading($course->fullname);
153$PAGE->set_pagelayout('standard');
f5ecf2e9 154
e3ac1261
SH
155// Locate the users settings in the settings navigation and force it open.
156// This MUST be done after we've set up the page as it is going to cause theme and output to initialise.
87c215de
SH
157if (!$currentuser) {
158 $PAGE->navigation->extend_for_user($user);
5d07e957 159 if ($node = $PAGE->settingsnav->get('userviewingsettings'.$user->id)) {
87c215de
SH
160 $node->forceopen = true;
161 }
162} else if ($node = $PAGE->settingsnav->get('usercurrentsettings', navigation_node::TYPE_CONTAINER)) {
163 $node->forceopen = true;
164}
165if ($node = $PAGE->settingsnav->get('courseadmin')) {
166 $node->forceopen = false;
167}
168
ce221eb5 169echo $OUTPUT->header();
99cca847
MD
170
171echo '<div class="userprofile">';
172
8ebbb06a 173echo $OUTPUT->heading(fullname($user).' ('.format_string($course->shortname, true, array('context' => $coursecontext)).')');
b1d530d2 174
ce221eb5 175if ($user->deleted) {
176 echo $OUTPUT->heading(get_string('userdeleted'));
177 if (!has_capability('moodle/user:update', $coursecontext)) {
178 echo $OUTPUT->footer();
179 die;
bb09fb11 180 }
ce221eb5 181}
bb09fb11 182
bad59bc0 183/// OK, security out the way, now we are showing the user
184
ce221eb5 185add_to_log($course->id, "user", "view", "view.php?id=$user->id&course=$course->id", "$user->id");
bad59bc0 186
4801fe93 187/// Get the hidden field list
ce221eb5 188if (has_capability('moodle/user:viewhiddendetails', $coursecontext)) {
189 $hiddenfields = array();
190} else {
191 $hiddenfields = array_flip(explode(',', $CFG->hiddenuserfields));
192}
f9903ed0 193
ce221eb5 194if (is_mnet_remote_user($user)) {
5db29f49
DM
195 $sql = "SELECT h.id, h.name, h.wwwroot,
196 a.name as application, a.display_name
197 FROM {mnet_host} h, {mnet_application} a
198 WHERE h.id = ? AND h.applicationid = a.id";
ce8c75ee 199
ce221eb5 200 $remotehost = $DB->get_record_sql($sql, array($user->mnethostid));
5db29f49
DM
201 $a = new stdclass();
202 $a->remotetype = $remotehost->display_name;
203 $a->remotename = $remotehost->name;
204 $a->remoteurl = $remotehost->wwwroot;
0be6f678 205
5db29f49 206 echo $OUTPUT->box(get_string('remoteuserinfo', 'mnet', $a), 'remoteuserinfo');
ce221eb5 207}
56f52742 208
e4f0c78a 209echo '<div class="userprofilebox clearfix"><div class="profilepicture">';
03d9401e
MD
210echo $OUTPUT->user_picture($user, array('size'=>100));
211echo '</div>';
f9903ed0 212
03d9401e 213// Print the description
e4f0c78a 214echo '<div class="descriptionbox"><div class="description">';
ce221eb5 215if ($user->description && !isset($hiddenfields['description'])) {
03d9401e 216 if (!empty($CFG->profilesforenrolledusersonly) && !$DB->record_exists('role_assignments', array('userid'=>$id))) {
99cca847 217 echo get_string('profilenotshown', 'moodle');
ce221eb5 218 } else {
64f93798
PS
219 if ($courseid == SITEID) {
220 $user->description = file_rewrite_pluginfile_urls($user->description, 'pluginfile.php', $usercontext->id, 'user', 'profile', null);
221 } else {
222 // we have to make a little detour thought the course context to verify the access control for course profile
223 $user->description = file_rewrite_pluginfile_urls($user->description, 'pluginfile.php', $coursecontext->id, 'user', 'profile', $user->id);
224 }
367a75fa
SH
225 $options = array('overflowdiv'=>true);
226 echo format_text($user->description, $user->descriptionformat, $options);
f9903ed0 227 }
ce221eb5 228}
03d9401e
MD
229echo '</div>';
230
f9903ed0 231
ce221eb5 232// Print all the little details in a list
f9903ed0 233
03d9401e 234echo '<table class="list" summary="">';
f9903ed0 235
a786cf46
RT
236// Show email if any of the following conditions match.
237// 1. User is viewing his own profile.
238// 2. Has allowed everyone to see email
239// 3. User has allowed course members to can see email and current user is in same course
240// 4. Has either course:viewhiddenuserfields or site:viewuseridentity capability.
8d22e342 241if ($currentuser
a786cf46
RT
242 or $user->maildisplay == 1
243 or ($user->maildisplay == 2 && is_enrolled($coursecontext, $USER))
244 or has_capability('moodle/course:viewhiddenuserfields', $coursecontext)
245 or has_capability('moodle/site:viewuseridentity', $coursecontext)) {
8d22e342
AD
246 print_row(get_string("email").":", obfuscate_mailto($user->email, ''));
247}
248
99cca847
MD
249// Show last time this user accessed this course
250if (!isset($hiddenfields['lastaccess'])) {
251 if ($lastaccess = $DB->get_record('user_lastaccess', array('userid'=>$user->id, 'courseid'=>$course->id))) {
252 $datestring = userdate($lastaccess->timeaccess)."&nbsp; (".format_time(time() - $lastaccess->timeaccess).")";
253 } else {
254 $datestring = get_string("never");
f9903ed0 255 }
99cca847 256 print_row(get_string("lastaccess").":", $datestring);
ce221eb5 257}
f9903ed0 258
99cca847
MD
259// Show roles in this course
260if ($rolestring = get_user_roles_in_course($id, $course->id)) {
261 print_row(get_string('roles').':', $rolestring);
ce221eb5 262}
766d2bf3 263
99cca847
MD
264// Show groups this user is in
265if (!isset($hiddenfields['groups'])) {
a732add0
PS
266 $accessallgroups = has_capability('moodle/site:accessallgroups', $coursecontext);
267 if ($usergroups = groups_get_all_groups($course->id, $user->id)) {
268 $groupstr = '';
269 foreach ($usergroups as $group){
270 if ($course->groupmode == SEPARATEGROUPS and !$accessallgroups and $user->id != $USER->id) {
271 if (!groups_is_member($group->id, $user->id)) {
272 continue;
273 }
274 }
275
276 if ($course->groupmode != NOGROUPS) {
99cca847 277 $groupstr .= ' <a href="'.$CFG->wwwroot.'/user/index.php?id='.$course->id.'&amp;group='.$group->id.'">'.format_string($group->name).'</a>,';
a732add0
PS
278 } else {
279 $groupstr .= ' '.format_string($group->name); // the user/index.php shows groups only when course in group mode
99cca847 280 }
a732add0
PS
281 }
282 if ($groupstr !== '') {
99cca847
MD
283 print_row(get_string("group").":", rtrim($groupstr, ', '));
284 }
f9903ed0 285 }
ce221eb5 286}
f9903ed0 287
99cca847 288// Show other courses they may be in
ce221eb5 289if (!isset($hiddenfields['mycourses'])) {
9ffd29ce 290 if ($mycourses = enrol_get_all_users_courses($user->id, true, NULL, 'visible DESC,sortorder ASC')) {
99cca847 291 $shown = 0;
ce221eb5 292 $courselisting = '';
293 foreach ($mycourses as $mycourse) {
294 if ($mycourse->category) {
defbd5dd
TL
295 context_helper::preload_from_record($mycourse);
296 $ccontext = context_course::instance($mycourse->id);
297 $cfullname = $ccontext->get_context_name(false);
ce221eb5 298 if ($mycourse->id != $course->id){
299 $class = '';
300 if ($mycourse->visible == 0) {
df997f84
PS
301 if (!has_capability('moodle/course:viewhiddencourses', $ccontext)) {
302 continue;
303 }
ce221eb5 304 $class = 'class="dimmed"';
472b647a 305 }
ce221eb5 306 $courselisting .= "<a href=\"{$CFG->wwwroot}/user/view.php?id={$user->id}&amp;course={$mycourse->id}\" $class >"
91d284c1 307 . $cfullname . "</a>, ";
99cca847 308 } else {
91d284c1
SH
309 $courselisting .= $cfullname . ", ";
310 $PAGE->navbar->add($cfullname);
9c72928d 311 }
312 }
ce221eb5 313 $shown++;
99cca847
MD
314 if ($shown >= 20) {
315 $courselisting .= "...";
ce221eb5 316 break;
317 }
9c72928d 318 }
03d9401e 319 print_row(get_string('courseprofiles').':', rtrim($courselisting,', '));
9c72928d 320 }
ce221eb5 321}
f2f085ee 322
4ad72c28
PS
323if (!isset($hiddenfields['suspended'])) {
324 if ($user->suspended) {
325 print_row('', get_string('suspended', 'auth'));
326 }
327}
328
e4f0c78a 329echo "</table></div></div>";
fa22fd5f 330
42ec733c
AD
331// Print messaging link if allowed
332if (isloggedin() && has_capability('moodle/site:sendmessage', $usercontext)
333 && !empty($CFG->messaging) && !isguestuser() && !isguestuser($user) && ($USER->id != $user->id)) {
334 echo '<div class="messagebox">';
335 echo '<a href="'.$CFG->wwwroot.'/message/index.php?id='.$user->id.'">'.get_string('messageselectadd').'</a>';
336 echo '</div>';
337}
338
46ea652f
SH
339if ($currentuser || has_capability('moodle/user:viewdetails', $usercontext) || has_coursecontact_role($id)) {
340 echo '<div class="fullprofilelink">';
341 echo html_writer::link($CFG->wwwroot.'/user/profile.php?id='.$id, get_string('fullprofile'));
342 echo '</div>';
343}
fa22fd5f 344
99cca847 345/// TODO Add more useful overview info for teachers here, see below
1e1c51a3 346
99cca847 347/// Show links to notes made about this student (must click to display, for privacy)
f9903ed0 348
99cca847 349/// Recent comments made in this course
210560e3 350
99cca847 351/// Recent blogs associated with this course and items in it
3086f3f6 352
3de34ccf 353
99cca847
MD
354
355echo '</div>'; // userprofile class
3de34ccf 356
ce221eb5 357echo $OUTPUT->footer();
f9903ed0 358
359/// Functions ///////
360
361function print_row($left, $right) {
9152878e 362 echo "\n<tr><th class=\"label c0\">$left</th><td class=\"info c1\">$right</td></tr>\n";
f9903ed0 363}
364
aa6c1ced 365