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