Merge branch 'MDL-48860_27' of git://github.com/timhunt/moodle into MOODLE_27_STABLE
[moodle.git] / user / profile.php
blob82f2a79d85965576ddb3d62193cdc319e7cdd7a9
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 * Public Profile -- a user's public profile page
20 * - each user can currently have their own page (cloned from system and then customised)
21 * - users can add any blocks they want
22 * - the administrators can define a default site public profile for users who have
23 * not created their own public profile
25 * This script implements the user's view of the public profile, and allows editing
26 * of the public profile.
28 * @package core_user
29 * @copyright 2010 Remote-Learner.net
30 * @author Hubert Chathi <hubert@remote-learner.net>
31 * @author Olav Jordan <olav.jordan@remote-learner.net>
32 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
35 require_once(dirname(__FILE__) . '/../config.php');
36 require_once($CFG->dirroot . '/my/lib.php');
37 require_once($CFG->dirroot . '/tag/lib.php');
38 require_once($CFG->dirroot . '/user/profile/lib.php');
39 require_once($CFG->libdir.'/filelib.php');
41 $userid = optional_param('id', 0, PARAM_INT);
42 $edit = optional_param('edit', null, PARAM_BOOL); // Turn editing on and off.
43 $reset = optional_param('reset', null, PARAM_BOOL);
44 $showallcourses = optional_param('showallcourses', 0, PARAM_INT);
46 $PAGE->set_url('/user/profile.php', array('id' => $userid));
48 if (!empty($CFG->forceloginforprofiles)) {
49 require_login();
50 if (isguestuser()) {
51 $PAGE->set_context(context_system::instance());
52 echo $OUTPUT->header();
53 echo $OUTPUT->confirm(get_string('guestcantaccessprofiles', 'error'),
54 get_login_url(),
55 $CFG->wwwroot);
56 echo $OUTPUT->footer();
57 die;
59 } else if (!empty($CFG->forcelogin)) {
60 require_login();
63 $userid = $userid ? $userid : $USER->id; // Owner of the page.
64 if ((!$user = $DB->get_record('user', array('id' => $userid))) || ($user->deleted)) {
65 $PAGE->set_context(context_system::instance());
66 echo $OUTPUT->header();
67 if (!$user) {
68 echo $OUTPUT->notification(get_string('invaliduser', 'error'));
69 } else {
70 echo $OUTPUT->notification(get_string('userdeleted'));
72 echo $OUTPUT->footer();
73 die;
76 $currentuser = ($user->id == $USER->id);
77 $context = $usercontext = context_user::instance($userid, MUST_EXIST);
79 if (!$currentuser &&
80 !empty($CFG->forceloginforprofiles) &&
81 !has_capability('moodle/user:viewdetails', $context) &&
82 !has_coursecontact_role($userid)) {
84 // Course managers can be browsed at site level. If not forceloginforprofiles, allow access (bug #4366).
85 $struser = get_string('user');
86 $PAGE->set_context(context_system::instance());
87 $PAGE->set_title("$SITE->shortname: $struser"); // Do not leak the name.
88 $PAGE->set_heading("$SITE->shortname: $struser");
89 $PAGE->set_url('/user/profile.php', array('id' => $userid));
90 $PAGE->navbar->add($struser);
91 echo $OUTPUT->header();
92 echo $OUTPUT->notification(get_string('usernotavailable', 'error'));
93 echo $OUTPUT->footer();
94 exit;
97 // Get the profile page. Should always return something unless the database is broken.
98 if (!$currentpage = my_get_page($userid, MY_PAGE_PUBLIC)) {
99 print_error('mymoodlesetup');
102 if (!$currentpage->userid) {
103 $context = context_system::instance(); // A trick so that we even see non-sticky blocks.
106 $PAGE->set_context($context);
107 $PAGE->set_pagelayout('mypublic');
108 $PAGE->set_pagetype('user-profile');
110 // Set up block editing capabilities.
111 if (isguestuser()) { // Guests can never edit their profile.
112 $USER->editing = $edit = 0; // Just in case.
113 $PAGE->set_blocks_editing_capability('moodle/my:configsyspages'); // unlikely :).
114 } else {
115 if ($currentuser) {
116 $PAGE->set_blocks_editing_capability('moodle/user:manageownblocks');
117 } else {
118 $PAGE->set_blocks_editing_capability('moodle/user:manageblocks');
122 if (has_capability('moodle/user:viewhiddendetails', $context)) {
123 $hiddenfields = array();
124 } else {
125 $hiddenfields = array_flip(explode(',', $CFG->hiddenuserfields));
128 if (has_capability('moodle/site:viewuseridentity', $context)) {
129 $identityfields = array_flip(explode(',', $CFG->showuseridentity));
130 } else {
131 $identityfields = array();
134 // Start setting up the page.
135 $strpublicprofile = get_string('publicprofile');
137 $PAGE->blocks->add_region('content');
138 $PAGE->set_subpage($currentpage->id);
139 $PAGE->set_title(fullname($user).": $strpublicprofile");
140 $PAGE->set_heading(fullname($user).": $strpublicprofile");
142 if (!$currentuser) {
143 $PAGE->navigation->extend_for_user($user);
144 if ($node = $PAGE->settingsnav->get('userviewingsettings'.$user->id)) {
145 $node->forceopen = true;
147 } else if ($node = $PAGE->settingsnav->get('usercurrentsettings', navigation_node::TYPE_CONTAINER)) {
148 $node->forceopen = true;
150 if ($node = $PAGE->settingsnav->get('root')) {
151 $node->forceopen = false;
155 // Toggle the editing state and switches.
156 if ($PAGE->user_allowed_editing()) {
157 if ($reset !== null) {
158 if (!is_null($userid)) {
159 if (!$currentpage = my_reset_page($userid, MY_PAGE_PUBLIC, 'user-profile')) {
160 print_error('reseterror', 'my');
162 redirect(new moodle_url('/user/profile.php', array('id' => $userid)));
164 } else if ($edit !== null) { // Editing state was specified.
165 $USER->editing = $edit; // Change editing state.
166 if (!$currentpage->userid && $edit) {
167 // If we are viewing a system page as ordinary user, and the user turns
168 // editing on, copy the system pages as new user pages, and get the
169 // new page record.
170 if (!$currentpage = my_copy_page($userid, MY_PAGE_PUBLIC, 'user-profile')) {
171 print_error('mymoodlesetup');
173 $PAGE->set_context($usercontext);
174 $PAGE->set_subpage($currentpage->id);
176 } else { // Editing state is in session.
177 if ($currentpage->userid) { // It's a page we can edit, so load from session.
178 if (!empty($USER->editing)) {
179 $edit = 1;
180 } else {
181 $edit = 0;
183 } else { // It's a system page and they are not allowed to edit system pages.
184 $USER->editing = $edit = 0; // Disable editing completely, just to be safe.
188 // Add button for editing page.
189 $params = array('edit' => !$edit, 'id' => $userid);
191 $resetbutton = '';
192 $resetstring = get_string('resetpage', 'my');
193 $reseturl = new moodle_url("$CFG->wwwroot/user/profile.php", array('edit' => 1, 'reset' => 1, 'id' => $userid));
195 if (!$currentpage->userid) {
196 // Viewing a system page -- let the user customise it.
197 $editstring = get_string('updatemymoodleon');
198 $params['edit'] = 1;
199 } else if (empty($edit)) {
200 $editstring = get_string('updatemymoodleon');
201 $resetbutton = $OUTPUT->single_button($reseturl, $resetstring);
202 } else {
203 $editstring = get_string('updatemymoodleoff');
204 $resetbutton = $OUTPUT->single_button($reseturl, $resetstring);
207 $url = new moodle_url("$CFG->wwwroot/user/profile.php", $params);
208 $button = $OUTPUT->single_button($url, $editstring);
209 $PAGE->set_button($resetbutton . $button);
211 } else {
212 $USER->editing = $edit = 0;
215 // HACK WARNING! This loads up all this page's blocks in the system context.
216 if ($currentpage->userid == 0) {
217 $CFG->blockmanagerclass = 'my_syspage_block_manager';
220 // Trigger a user profile viewed event.
221 $event = \core\event\user_profile_viewed::create(array(
222 'objectid' => $user->id,
223 'relateduserid' => $user->id,
224 'context' => $usercontext
226 $event->add_record_snapshot('user', $user);
227 $event->trigger();
229 // TODO WORK OUT WHERE THE NAV BAR IS!
230 echo $OUTPUT->header();
231 echo '<div class="userprofile">';
234 // Print the standard content of this page, the basic profile info.
235 echo $OUTPUT->heading(fullname($user));
237 if (is_mnet_remote_user($user)) {
238 $sql = "SELECT h.id, h.name, h.wwwroot,
239 a.name as application, a.display_name
240 FROM {mnet_host} h, {mnet_application} a
241 WHERE h.id = ? AND h.applicationid = a.id";
243 $remotehost = $DB->get_record_sql($sql, array($user->mnethostid));
244 $a = new stdclass();
245 $a->remotetype = $remotehost->display_name;
246 $a->remotename = $remotehost->name;
247 $a->remoteurl = $remotehost->wwwroot;
249 echo $OUTPUT->box(get_string('remoteuserinfo', 'mnet', $a), 'remoteuserinfo');
252 echo '<div class="userprofilebox clearfix"><div class="profilepicture">';
253 echo $OUTPUT->user_picture($user, array('size' => 100));
254 echo '</div>';
256 echo '<div class="descriptionbox"><div class="description">';
257 // Print the description.
258 if ($user->description && !isset($hiddenfields['description'])) {
259 if (!empty($CFG->profilesforenrolledusersonly) && !$currentuser &&
260 !$DB->record_exists('role_assignments', array('userid' => $user->id))) {
261 echo get_string('profilenotshown', 'moodle');
262 } else {
263 $user->description = file_rewrite_pluginfile_urls($user->description, 'pluginfile.php', $usercontext->id, 'user',
264 'profile', null);
265 $options = array('overflowdiv' => true);
266 echo format_text($user->description, $user->descriptionformat, $options);
269 echo '</div>';
272 // Print all the little details in a list.
273 echo html_writer::start_tag('dl', array('class' => 'list'));
274 if (!isset($hiddenfields['country']) && $user->country) {
275 echo html_writer::tag('dt', get_string('country'));
276 echo html_writer::tag('dd', get_string($user->country, 'countries'));
279 if (!isset($hiddenfields['city']) && $user->city) {
280 echo html_writer::tag('dt', get_string('city'));
281 echo html_writer::tag('dd', $user->city);
284 if (isset($identityfields['address']) && $user->address) {
285 echo html_writer::tag('dt', get_string('address'));
286 echo html_writer::tag('dd', $user->address);
289 if (isset($identityfields['phone1']) && $user->phone1) {
290 echo html_writer::tag('dt', get_string('phone'));
291 echo html_writer::tag('dd', $user->phone1);
294 if (isset($identityfields['phone2']) && $user->phone2) {
295 echo html_writer::tag('dt', get_string('phone2'));
296 echo html_writer::tag('dd', $user->phone2);
299 if (isset($identityfields['institution']) && $user->institution) {
300 echo html_writer::tag('dt', get_string('institution'));
301 echo html_writer::tag('dd', $user->institution);
304 if (isset($identityfields['department']) && $user->department) {
305 echo html_writer::tag('dt', get_string('department'));
306 echo html_writer::tag('dd', $user->department);
309 if (isset($identityfields['idnumber']) && $user->idnumber) {
310 echo html_writer::tag('dt', get_string('idnumber'));
311 echo html_writer::tag('dd', $user->idnumber);
314 if (isset($identityfields['email']) and ($currentuser
315 or $user->maildisplay == 1
316 or has_capability('moodle/course:useremail', $context)
317 or ($user->maildisplay == 2 and enrol_sharing_course($user, $USER)))) {
318 echo html_writer::tag('dt', get_string('email'));
319 echo html_writer::tag('dd', obfuscate_mailto($user->email, ''));
322 if ($user->url && !isset($hiddenfields['webpage'])) {
323 $url = $user->url;
324 if (strpos($user->url, '://') === false) {
325 $url = 'http://'. $url;
327 $webpageurl = new moodle_url($url);
328 echo html_writer::tag('dt', get_string('webpage'));
329 echo html_writer::tag('dd', html_writer::link($webpageurl, s($user->url)));
332 if ($user->icq && !isset($hiddenfields['icqnumber'])) {
333 $imurl = new moodle_url('http://web.icq.com/wwp', array('uin' => $user->icq) );
334 $iconurl = new moodle_url('http://web.icq.com/whitepages/online', array('icq' => $user->icq, 'img' => '5'));
335 $statusicon = html_writer::tag('img', '', array('src' => $iconurl, 'class' => 'icon icon-post', 'alt' => get_string('status')));
336 echo html_writer::tag('dt', get_string('icqnumber'));
337 echo html_writer::tag('dd', html_writer::link($imurl, s($user->icq) . $statusicon));
340 if ($user->skype && !isset($hiddenfields['skypeid'])) {
341 $imurl = 'skype:'.urlencode($user->skype).'?call';
342 $iconurl = new moodle_url('http://mystatus.skype.com/smallicon/'.urlencode($user->skype));
343 if (strpos($CFG->httpswwwroot, 'https:') === 0) {
344 // Bad luck, skype devs are lazy to set up SSL on their servers - see MDL-37233.
345 $statusicon = '';
346 } else {
347 $statusicon = html_writer::empty_tag('img',
348 array('src' => $iconurl, 'class' => 'icon icon-post', 'alt' => get_string('status')));
350 echo html_writer::tag('dt', get_string('skypeid'));
351 echo html_writer::tag('dd', html_writer::link($imurl, s($user->skype) . $statusicon));
353 if ($user->yahoo && !isset($hiddenfields['yahooid'])) {
354 $imurl = new moodle_url('http://edit.yahoo.com/config/send_webmesg', array('.target' => $user->yahoo, '.src' => 'pg'));
355 $iconurl = new moodle_url('http://opi.yahoo.com/online', array('u' => $user->yahoo, 'm' => 'g', 't' => '0'));
356 $statusicon = html_writer::tag('img', '',
357 array('src' => $iconurl, 'class' => 'iconsmall icon-post', 'alt' => get_string('status')));
358 echo html_writer::tag('dt', get_string('yahooid'));
359 echo html_writer::tag('dd', html_writer::link($imurl, s($user->yahoo) . $statusicon));
361 if ($user->aim && !isset($hiddenfields['aimid'])) {
362 $imurl = 'aim:goim?screenname='.urlencode($user->aim);
363 echo html_writer::tag('dt', get_string('aimid'));
364 echo html_writer::tag('dd', html_writer::link($imurl, s($user->aim)));
366 if ($user->msn && !isset($hiddenfields['msnid'])) {
367 echo html_writer::tag('dt', get_string('msnid'));
368 echo html_writer::tag('dd', s($user->msn));
371 // Print the Custom User Fields.
372 profile_display_fields($user->id);
375 if (!isset($hiddenfields['mycourses'])) {
376 if ($mycourses = enrol_get_all_users_courses($user->id, true, null, 'visible DESC, sortorder ASC')) {
377 $shown = 0;
378 $courselisting = '';
379 foreach ($mycourses as $mycourse) {
380 if ($mycourse->category) {
381 context_helper::preload_from_record($mycourse);
382 $ccontext = context_course::instance($mycourse->id);
383 $linkattributes = null;
384 if ($mycourse->visible == 0) {
385 if (!has_capability('moodle/course:viewhiddencourses', $ccontext)) {
386 continue;
388 $linkattributes['class'] = 'dimmed';
390 $params = array('id' => $user->id, 'course' => $mycourse->id);
391 if ($showallcourses) {
392 $params['showallcourses'] = 1;
394 $url = new moodle_url('/user/view.php', $params);
395 $courselisting .= html_writer::link($url, $ccontext->get_context_name(false), $linkattributes);
396 $courselisting .= ', ';
398 $shown++;
399 if (!$showallcourses && $shown == $CFG->navcourselimit) {
400 $url = new moodle_url('/user/profile.php', array('id' => $user->id, 'showallcourses' => 1));
401 $courselisting .= html_writer::link($url, '...', array('title' => get_string('viewmore')));
402 break;
405 echo html_writer::tag('dt', get_string('courseprofiles'));
406 echo html_writer::tag('dd', rtrim($courselisting, ', '));
409 if (!isset($hiddenfields['firstaccess'])) {
410 if ($user->firstaccess) {
411 $datestring = userdate($user->firstaccess)."&nbsp; (".format_time(time() - $user->firstaccess).")";
412 } else {
413 $datestring = get_string("never");
415 echo html_writer::tag('dt', get_string('firstsiteaccess'));
416 echo html_writer::tag('dd', $datestring);
418 if (!isset($hiddenfields['lastaccess'])) {
419 if ($user->lastaccess) {
420 $datestring = userdate($user->lastaccess)."&nbsp; (".format_time(time() - $user->lastaccess).")";
421 } else {
422 $datestring = get_string("never");
424 echo html_writer::tag('dt', get_string('lastsiteaccess'));
425 echo html_writer::tag('dd', $datestring);
428 if (has_capability('moodle/user:viewlastip', $usercontext) && !isset($hiddenfields['lastip'])) {
429 if ($user->lastip) {
430 $iplookupurl = new moodle_url('/iplookup/index.php', array('ip' => $user->lastip, 'user' => $user->id));
431 $ipstring = html_writer::link($iplookupurl, $user->lastip);
432 } else {
433 $ipstring = get_string("none");
435 echo html_writer::tag('dt', get_string('lastip'));
436 echo html_writer::tag('dd', $ipstring);
439 // Printing tagged interests.
440 if (!empty($CFG->usetags)) {
441 if ($interests = tag_get_tags_csv('user', $user->id) ) {
442 echo html_writer::tag('dt', get_string('interests'));
443 echo html_writer::tag('dd', $interests);
447 if (!isset($hiddenfields['suspended'])) {
448 if ($user->suspended) {
449 echo html_writer::tag('dt', '&nbsp;');
450 echo html_writer::tag('dd', get_string('suspended', 'auth'));
454 require_once($CFG->libdir . '/badgeslib.php');
455 if (!empty($CFG->enablebadges)) {
456 profile_display_badges($user->id);
459 echo html_writer::end_tag('dl');
460 echo "</div></div>"; // Closing desriptionbox and userprofilebox.
462 echo $OUTPUT->custom_block_region('content');
464 // Print messaging link if allowed.
465 if (isloggedin() && has_capability('moodle/site:sendmessage', $context)
466 && !empty($CFG->messaging) && !isguestuser() && !isguestuser($user) && ($USER->id != $user->id)) {
467 echo '<div class="messagebox">';
468 echo '<a href="'.$CFG->wwwroot.'/message/index.php?id='.$user->id.'">'.get_string('messageselectadd').'</a>';
469 echo '</div>';
472 echo '</div>'; // Userprofile class.
473 echo $OUTPUT->footer();