Merge branch 'MDL-47990_27' of git://github.com/timhunt/moodle into MOODLE_27_STABLE
[moodle.git] / user / profile.php
blob48a368a9bc3f65c5bf357946e839aa181d86dcf1
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 $SESSION->wantsurl = $PAGE->url->out(false);
53 $PAGE->set_context(context_system::instance());
54 echo $OUTPUT->header();
55 echo $OUTPUT->confirm(get_string('guestcantaccessprofiles', 'error'),
56 get_login_url(),
57 $CFG->wwwroot);
58 echo $OUTPUT->footer();
59 die;
61 } else if (!empty($CFG->forcelogin)) {
62 require_login();
65 $userid = $userid ? $userid : $USER->id; // Owner of the page.
66 if ((!$user = $DB->get_record('user', array('id' => $userid))) || ($user->deleted)) {
67 $PAGE->set_context(context_system::instance());
68 echo $OUTPUT->header();
69 if (!$user) {
70 echo $OUTPUT->notification(get_string('invaliduser', 'error'));
71 } else {
72 echo $OUTPUT->notification(get_string('userdeleted'));
74 echo $OUTPUT->footer();
75 die;
78 $currentuser = ($user->id == $USER->id);
79 $context = $usercontext = context_user::instance($userid, MUST_EXIST);
81 if (!$currentuser &&
82 !empty($CFG->forceloginforprofiles) &&
83 !has_capability('moodle/user:viewdetails', $context) &&
84 !has_coursecontact_role($userid)) {
86 // Course managers can be browsed at site level. If not forceloginforprofiles, allow access (bug #4366).
87 $struser = get_string('user');
88 $PAGE->set_context(context_system::instance());
89 $PAGE->set_title("$SITE->shortname: $struser"); // Do not leak the name.
90 $PAGE->set_heading("$SITE->shortname: $struser");
91 $PAGE->set_url('/user/profile.php', array('id' => $userid));
92 $PAGE->navbar->add($struser);
93 echo $OUTPUT->header();
94 echo $OUTPUT->notification(get_string('usernotavailable', 'error'));
95 echo $OUTPUT->footer();
96 exit;
99 // Get the profile page. Should always return something unless the database is broken.
100 if (!$currentpage = my_get_page($userid, MY_PAGE_PUBLIC)) {
101 print_error('mymoodlesetup');
104 if (!$currentpage->userid) {
105 $context = context_system::instance(); // A trick so that we even see non-sticky blocks.
108 $PAGE->set_context($context);
109 $PAGE->set_pagelayout('mypublic');
110 $PAGE->set_pagetype('user-profile');
112 // Set up block editing capabilities.
113 if (isguestuser()) { // Guests can never edit their profile.
114 $USER->editing = $edit = 0; // Just in case.
115 $PAGE->set_blocks_editing_capability('moodle/my:configsyspages'); // unlikely :).
116 } else {
117 if ($currentuser) {
118 $PAGE->set_blocks_editing_capability('moodle/user:manageownblocks');
119 } else {
120 $PAGE->set_blocks_editing_capability('moodle/user:manageblocks');
124 if (has_capability('moodle/user:viewhiddendetails', $context)) {
125 $hiddenfields = array();
126 } else {
127 $hiddenfields = array_flip(explode(',', $CFG->hiddenuserfields));
130 if (has_capability('moodle/site:viewuseridentity', $context)) {
131 $identityfields = array_flip(explode(',', $CFG->showuseridentity));
132 } else {
133 $identityfields = array();
136 // Start setting up the page.
137 $strpublicprofile = get_string('publicprofile');
139 $PAGE->blocks->add_region('content');
140 $PAGE->set_subpage($currentpage->id);
141 $PAGE->set_title(fullname($user).": $strpublicprofile");
142 $PAGE->set_heading(fullname($user).": $strpublicprofile");
144 if (!$currentuser) {
145 $PAGE->navigation->extend_for_user($user);
146 if ($node = $PAGE->settingsnav->get('userviewingsettings'.$user->id)) {
147 $node->forceopen = true;
149 } else if ($node = $PAGE->settingsnav->get('usercurrentsettings', navigation_node::TYPE_CONTAINER)) {
150 $node->forceopen = true;
152 if ($node = $PAGE->settingsnav->get('root')) {
153 $node->forceopen = false;
157 // Toggle the editing state and switches.
158 if ($PAGE->user_allowed_editing()) {
159 if ($reset !== null) {
160 if (!is_null($userid)) {
161 if (!$currentpage = my_reset_page($userid, MY_PAGE_PUBLIC, 'user-profile')) {
162 print_error('reseterror', 'my');
164 redirect(new moodle_url('/user/profile.php', array('id' => $userid)));
166 } else if ($edit !== null) { // Editing state was specified.
167 $USER->editing = $edit; // Change editing state.
168 if (!$currentpage->userid && $edit) {
169 // If we are viewing a system page as ordinary user, and the user turns
170 // editing on, copy the system pages as new user pages, and get the
171 // new page record.
172 if (!$currentpage = my_copy_page($userid, MY_PAGE_PUBLIC, 'user-profile')) {
173 print_error('mymoodlesetup');
175 $PAGE->set_context($usercontext);
176 $PAGE->set_subpage($currentpage->id);
178 } else { // Editing state is in session.
179 if ($currentpage->userid) { // It's a page we can edit, so load from session.
180 if (!empty($USER->editing)) {
181 $edit = 1;
182 } else {
183 $edit = 0;
185 } else { // It's a system page and they are not allowed to edit system pages.
186 $USER->editing = $edit = 0; // Disable editing completely, just to be safe.
190 // Add button for editing page.
191 $params = array('edit' => !$edit, 'id' => $userid);
193 $resetbutton = '';
194 $resetstring = get_string('resetpage', 'my');
195 $reseturl = new moodle_url("$CFG->wwwroot/user/profile.php", array('edit' => 1, 'reset' => 1, 'id' => $userid));
197 if (!$currentpage->userid) {
198 // Viewing a system page -- let the user customise it.
199 $editstring = get_string('updatemymoodleon');
200 $params['edit'] = 1;
201 } else if (empty($edit)) {
202 $editstring = get_string('updatemymoodleon');
203 $resetbutton = $OUTPUT->single_button($reseturl, $resetstring);
204 } else {
205 $editstring = get_string('updatemymoodleoff');
206 $resetbutton = $OUTPUT->single_button($reseturl, $resetstring);
209 $url = new moodle_url("$CFG->wwwroot/user/profile.php", $params);
210 $button = $OUTPUT->single_button($url, $editstring);
211 $PAGE->set_button($resetbutton . $button);
213 } else {
214 $USER->editing = $edit = 0;
217 // HACK WARNING! This loads up all this page's blocks in the system context.
218 if ($currentpage->userid == 0) {
219 $CFG->blockmanagerclass = 'my_syspage_block_manager';
222 // Trigger a user profile viewed event.
223 $event = \core\event\user_profile_viewed::create(array(
224 'objectid' => $user->id,
225 'relateduserid' => $user->id,
226 'context' => $usercontext
228 $event->add_record_snapshot('user', $user);
229 $event->trigger();
231 // TODO WORK OUT WHERE THE NAV BAR IS!
232 echo $OUTPUT->header();
233 echo '<div class="userprofile">';
236 // Print the standard content of this page, the basic profile info.
237 echo $OUTPUT->heading(fullname($user));
239 if (is_mnet_remote_user($user)) {
240 $sql = "SELECT h.id, h.name, h.wwwroot,
241 a.name as application, a.display_name
242 FROM {mnet_host} h, {mnet_application} a
243 WHERE h.id = ? AND h.applicationid = a.id";
245 $remotehost = $DB->get_record_sql($sql, array($user->mnethostid));
246 $a = new stdclass();
247 $a->remotetype = $remotehost->display_name;
248 $a->remotename = $remotehost->name;
249 $a->remoteurl = $remotehost->wwwroot;
251 echo $OUTPUT->box(get_string('remoteuserinfo', 'mnet', $a), 'remoteuserinfo');
254 echo '<div class="userprofilebox clearfix"><div class="profilepicture">';
255 echo $OUTPUT->user_picture($user, array('size' => 100));
256 echo '</div>';
258 echo '<div class="descriptionbox"><div class="description">';
259 // Print the description.
260 if ($user->description && !isset($hiddenfields['description'])) {
261 if (!empty($CFG->profilesforenrolledusersonly) && !$currentuser &&
262 !$DB->record_exists('role_assignments', array('userid' => $user->id))) {
263 echo get_string('profilenotshown', 'moodle');
264 } else {
265 $user->description = file_rewrite_pluginfile_urls($user->description, 'pluginfile.php', $usercontext->id, 'user',
266 'profile', null);
267 $options = array('overflowdiv' => true);
268 echo format_text($user->description, $user->descriptionformat, $options);
271 echo '</div>';
274 // Print all the little details in a list.
275 echo html_writer::start_tag('dl', array('class' => 'list'));
276 if (!isset($hiddenfields['country']) && $user->country) {
277 echo html_writer::tag('dt', get_string('country'));
278 echo html_writer::tag('dd', get_string($user->country, 'countries'));
281 if (!isset($hiddenfields['city']) && $user->city) {
282 echo html_writer::tag('dt', get_string('city'));
283 echo html_writer::tag('dd', $user->city);
286 if (isset($identityfields['address']) && $user->address) {
287 echo html_writer::tag('dt', get_string('address'));
288 echo html_writer::tag('dd', $user->address);
291 if (isset($identityfields['phone1']) && $user->phone1) {
292 echo html_writer::tag('dt', get_string('phone'));
293 echo html_writer::tag('dd', $user->phone1);
296 if (isset($identityfields['phone2']) && $user->phone2) {
297 echo html_writer::tag('dt', get_string('phone2'));
298 echo html_writer::tag('dd', $user->phone2);
301 if (isset($identityfields['institution']) && $user->institution) {
302 echo html_writer::tag('dt', get_string('institution'));
303 echo html_writer::tag('dd', $user->institution);
306 if (isset($identityfields['department']) && $user->department) {
307 echo html_writer::tag('dt', get_string('department'));
308 echo html_writer::tag('dd', $user->department);
311 if (isset($identityfields['idnumber']) && $user->idnumber) {
312 echo html_writer::tag('dt', get_string('idnumber'));
313 echo html_writer::tag('dd', $user->idnumber);
316 if (isset($identityfields['email']) and ($currentuser
317 or $user->maildisplay == 1
318 or has_capability('moodle/course:useremail', $context)
319 or ($user->maildisplay == 2 and enrol_sharing_course($user, $USER)))) {
320 echo html_writer::tag('dt', get_string('email'));
321 echo html_writer::tag('dd', obfuscate_mailto($user->email, ''));
324 if ($user->url && !isset($hiddenfields['webpage'])) {
325 $url = $user->url;
326 if (strpos($user->url, '://') === false) {
327 $url = 'http://'. $url;
329 $webpageurl = new moodle_url($url);
330 echo html_writer::tag('dt', get_string('webpage'));
331 echo html_writer::tag('dd', html_writer::link($webpageurl, s($user->url)));
334 if ($user->icq && !isset($hiddenfields['icqnumber'])) {
335 $imurl = new moodle_url('http://web.icq.com/wwp', array('uin' => $user->icq) );
336 $iconurl = new moodle_url('http://web.icq.com/whitepages/online', array('icq' => $user->icq, 'img' => '5'));
337 $statusicon = html_writer::tag('img', '', array('src' => $iconurl, 'class' => 'icon icon-post', 'alt' => get_string('status')));
338 echo html_writer::tag('dt', get_string('icqnumber'));
339 echo html_writer::tag('dd', html_writer::link($imurl, s($user->icq) . $statusicon));
342 if ($user->skype && !isset($hiddenfields['skypeid'])) {
343 $imurl = 'skype:'.urlencode($user->skype).'?call';
344 $iconurl = new moodle_url('http://mystatus.skype.com/smallicon/'.urlencode($user->skype));
345 if (strpos($CFG->httpswwwroot, 'https:') === 0) {
346 // Bad luck, skype devs are lazy to set up SSL on their servers - see MDL-37233.
347 $statusicon = '';
348 } else {
349 $statusicon = html_writer::empty_tag('img',
350 array('src' => $iconurl, 'class' => 'icon icon-post', 'alt' => get_string('status')));
352 echo html_writer::tag('dt', get_string('skypeid'));
353 echo html_writer::tag('dd', html_writer::link($imurl, s($user->skype) . $statusicon));
355 if ($user->yahoo && !isset($hiddenfields['yahooid'])) {
356 $imurl = new moodle_url('http://edit.yahoo.com/config/send_webmesg', array('.target' => $user->yahoo, '.src' => 'pg'));
357 $iconurl = new moodle_url('http://opi.yahoo.com/online', array('u' => $user->yahoo, 'm' => 'g', 't' => '0'));
358 $statusicon = html_writer::tag('img', '',
359 array('src' => $iconurl, 'class' => 'iconsmall icon-post', 'alt' => get_string('status')));
360 echo html_writer::tag('dt', get_string('yahooid'));
361 echo html_writer::tag('dd', html_writer::link($imurl, s($user->yahoo) . $statusicon));
363 if ($user->aim && !isset($hiddenfields['aimid'])) {
364 $imurl = 'aim:goim?screenname='.urlencode($user->aim);
365 echo html_writer::tag('dt', get_string('aimid'));
366 echo html_writer::tag('dd', html_writer::link($imurl, s($user->aim)));
368 if ($user->msn && !isset($hiddenfields['msnid'])) {
369 echo html_writer::tag('dt', get_string('msnid'));
370 echo html_writer::tag('dd', s($user->msn));
373 // Print the Custom User Fields.
374 profile_display_fields($user->id);
377 if (!isset($hiddenfields['mycourses'])) {
378 if ($mycourses = enrol_get_all_users_courses($user->id, true, null, 'visible DESC, sortorder ASC')) {
379 $shown = 0;
380 $courselisting = '';
381 foreach ($mycourses as $mycourse) {
382 if ($mycourse->category) {
383 context_helper::preload_from_record($mycourse);
384 $ccontext = context_course::instance($mycourse->id);
385 $linkattributes = null;
386 if ($mycourse->visible == 0) {
387 if (!has_capability('moodle/course:viewhiddencourses', $ccontext)) {
388 continue;
390 $linkattributes['class'] = 'dimmed';
392 $params = array('id' => $user->id, 'course' => $mycourse->id);
393 if ($showallcourses) {
394 $params['showallcourses'] = 1;
396 $url = new moodle_url('/user/view.php', $params);
397 $courselisting .= html_writer::link($url, $ccontext->get_context_name(false), $linkattributes);
398 $courselisting .= ', ';
400 $shown++;
401 if (!$showallcourses && $shown == $CFG->navcourselimit) {
402 $url = new moodle_url('/user/profile.php', array('id' => $user->id, 'showallcourses' => 1));
403 $courselisting .= html_writer::link($url, '...', array('title' => get_string('viewmore')));
404 break;
407 echo html_writer::tag('dt', get_string('courseprofiles'));
408 echo html_writer::tag('dd', rtrim($courselisting, ', '));
411 if (!isset($hiddenfields['firstaccess'])) {
412 if ($user->firstaccess) {
413 $datestring = userdate($user->firstaccess)."&nbsp; (".format_time(time() - $user->firstaccess).")";
414 } else {
415 $datestring = get_string("never");
417 echo html_writer::tag('dt', get_string('firstsiteaccess'));
418 echo html_writer::tag('dd', $datestring);
420 if (!isset($hiddenfields['lastaccess'])) {
421 if ($user->lastaccess) {
422 $datestring = userdate($user->lastaccess)."&nbsp; (".format_time(time() - $user->lastaccess).")";
423 } else {
424 $datestring = get_string("never");
426 echo html_writer::tag('dt', get_string('lastsiteaccess'));
427 echo html_writer::tag('dd', $datestring);
430 if (has_capability('moodle/user:viewlastip', $usercontext) && !isset($hiddenfields['lastip'])) {
431 if ($user->lastip) {
432 $iplookupurl = new moodle_url('/iplookup/index.php', array('ip' => $user->lastip, 'user' => $user->id));
433 $ipstring = html_writer::link($iplookupurl, $user->lastip);
434 } else {
435 $ipstring = get_string("none");
437 echo html_writer::tag('dt', get_string('lastip'));
438 echo html_writer::tag('dd', $ipstring);
441 // Printing tagged interests.
442 if (!empty($CFG->usetags)) {
443 if ($interests = tag_get_tags_csv('user', $user->id) ) {
444 echo html_writer::tag('dt', get_string('interests'));
445 echo html_writer::tag('dd', $interests);
449 if (!isset($hiddenfields['suspended'])) {
450 if ($user->suspended) {
451 echo html_writer::tag('dt', '&nbsp;');
452 echo html_writer::tag('dd', get_string('suspended', 'auth'));
456 require_once($CFG->libdir . '/badgeslib.php');
457 if (!empty($CFG->enablebadges)) {
458 profile_display_badges($user->id);
461 echo html_writer::end_tag('dl');
462 echo "</div></div>"; // Closing desriptionbox and userprofilebox.
464 echo $OUTPUT->custom_block_region('content');
466 // Print messaging link if allowed.
467 if (isloggedin() && has_capability('moodle/site:sendmessage', $context)
468 && !empty($CFG->messaging) && !isguestuser() && !isguestuser($user) && ($USER->id != $user->id)) {
469 echo '<div class="messagebox">';
470 echo '<a href="'.$CFG->wwwroot.'/message/index.php?id='.$user->id.'">'.get_string('messageselectadd').'</a>';
471 echo '</div>';
474 echo '</div>'; // Userprofile class.
475 echo $OUTPUT->footer();