Merge branch 'master_MDL-45726' of https://github.com/danmarsden/moodle
[moodle.git] / user / profile.php
blob88fb9a78f3bb4ea18939ba010bbbbb3939f89204
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);
45 $PAGE->set_url('/user/profile.php', array('id' => $userid));
47 if (!empty($CFG->forceloginforprofiles)) {
48 require_login();
49 if (isguestuser()) {
50 $SESSION->wantsurl = $PAGE->url->out(false);
51 redirect(get_login_url());
53 } else if (!empty($CFG->forcelogin)) {
54 require_login();
57 $userid = $userid ? $userid : $USER->id; // Owner of the page.
58 if ((!$user = $DB->get_record('user', array('id' => $userid))) || ($user->deleted)) {
59 $PAGE->set_context(context_system::instance());
60 echo $OUTPUT->header();
61 if (!$user) {
62 echo $OUTPUT->notification(get_string('invaliduser', 'error'));
63 } else {
64 echo $OUTPUT->notification(get_string('userdeleted'));
66 echo $OUTPUT->footer();
67 die;
70 $currentuser = ($user->id == $USER->id);
71 $context = $usercontext = context_user::instance($userid, MUST_EXIST);
73 if (!$currentuser &&
74 !empty($CFG->forceloginforprofiles) &&
75 !has_capability('moodle/user:viewdetails', $context) &&
76 !has_coursecontact_role($userid)) {
78 // Course managers can be browsed at site level. If not forceloginforprofiles, allow access (bug #4366).
79 $struser = get_string('user');
80 $PAGE->set_context(context_system::instance());
81 $PAGE->set_title("$SITE->shortname: $struser"); // Do not leak the name.
82 $PAGE->set_heading("$SITE->shortname: $struser");
83 $PAGE->set_url('/user/profile.php', array('id' => $userid));
84 $PAGE->navbar->add($struser);
85 echo $OUTPUT->header();
86 echo $OUTPUT->notification(get_string('usernotavailable', 'error'));
87 echo $OUTPUT->footer();
88 exit;
91 // Get the profile page. Should always return something unless the database is broken.
92 if (!$currentpage = my_get_page($userid, MY_PAGE_PUBLIC)) {
93 print_error('mymoodlesetup');
96 if (!$currentpage->userid) {
97 $context = context_system::instance(); // A trick so that we even see non-sticky blocks.
100 $PAGE->set_context($context);
101 $PAGE->set_pagelayout('mypublic');
102 $PAGE->set_pagetype('user-profile');
104 // Set up block editing capabilities.
105 if (isguestuser()) { // Guests can never edit their profile.
106 $USER->editing = $edit = 0; // Just in case.
107 $PAGE->set_blocks_editing_capability('moodle/my:configsyspages'); // unlikely :).
108 } else {
109 if ($currentuser) {
110 $PAGE->set_blocks_editing_capability('moodle/user:manageownblocks');
111 } else {
112 $PAGE->set_blocks_editing_capability('moodle/user:manageblocks');
116 if (has_capability('moodle/user:viewhiddendetails', $context)) {
117 $hiddenfields = array();
118 } else {
119 $hiddenfields = array_flip(explode(',', $CFG->hiddenuserfields));
122 if (has_capability('moodle/site:viewuseridentity', $context)) {
123 $identityfields = array_flip(explode(',', $CFG->showuseridentity));
124 } else {
125 $identityfields = array();
128 // Start setting up the page.
129 $strpublicprofile = get_string('publicprofile');
131 $PAGE->blocks->add_region('content');
132 $PAGE->set_subpage($currentpage->id);
133 $PAGE->set_title(fullname($user).": $strpublicprofile");
134 $PAGE->set_heading(fullname($user).": $strpublicprofile");
136 if (!$currentuser) {
137 $PAGE->navigation->extend_for_user($user);
138 if ($node = $PAGE->settingsnav->get('userviewingsettings'.$user->id)) {
139 $node->forceopen = true;
141 } else if ($node = $PAGE->settingsnav->get('usercurrentsettings', navigation_node::TYPE_CONTAINER)) {
142 $node->forceopen = true;
144 if ($node = $PAGE->settingsnav->get('root')) {
145 $node->forceopen = false;
149 // Toggle the editing state and switches.
150 if ($PAGE->user_allowed_editing()) {
151 if ($reset !== null) {
152 if (!is_null($userid)) {
153 if (!$currentpage = my_reset_page($userid, MY_PAGE_PUBLIC, 'user-profile')) {
154 print_error('reseterror', 'my');
156 redirect(new moodle_url('/user/profile.php'));
158 } else if ($edit !== null) { // Editing state was specified.
159 $USER->editing = $edit; // Change editing state.
160 if (!$currentpage->userid && $edit) {
161 // If we are viewing a system page as ordinary user, and the user turns
162 // editing on, copy the system pages as new user pages, and get the
163 // new page record.
164 if (!$currentpage = my_copy_page($USER->id, MY_PAGE_PUBLIC, 'user-profile')) {
165 print_error('mymoodlesetup');
167 $PAGE->set_context($usercontext);
168 $PAGE->set_subpage($currentpage->id);
170 } else { // Editing state is in session.
171 if ($currentpage->userid) { // It's a page we can edit, so load from session.
172 if (!empty($USER->editing)) {
173 $edit = 1;
174 } else {
175 $edit = 0;
177 } else { // It's a system page and they are not allowed to edit system pages.
178 $USER->editing = $edit = 0; // Disable editing completely, just to be safe.
182 // Add button for editing page.
183 $params = array('edit' => !$edit);
185 $resetbutton = '';
186 $resetstring = get_string('resetpage', 'my');
187 $reseturl = new moodle_url("$CFG->wwwroot/user/profile.php", array('edit' => 1, 'reset' => 1));
189 if (!$currentpage->userid) {
190 // Viewing a system page -- let the user customise it.
191 $editstring = get_string('updatemymoodleon');
192 $params['edit'] = 1;
193 } else if (empty($edit)) {
194 $editstring = get_string('updatemymoodleon');
195 $resetbutton = $OUTPUT->single_button($reseturl, $resetstring);
196 } else {
197 $editstring = get_string('updatemymoodleoff');
198 $resetbutton = $OUTPUT->single_button($reseturl, $resetstring);
201 $url = new moodle_url("$CFG->wwwroot/user/profile.php", $params);
202 $button = $OUTPUT->single_button($url, $editstring);
203 $PAGE->set_button($resetbutton . $button);
205 } else {
206 $USER->editing = $edit = 0;
209 // HACK WARNING! This loads up all this page's blocks in the system context.
210 if ($currentpage->userid == 0) {
211 $CFG->blockmanagerclass = 'my_syspage_block_manager';
214 // Trigger a user profile viewed event.
215 $event = \core\event\user_profile_viewed::create(array(
216 'objectid' => $user->id,
217 'relateduserid' => $user->id,
218 'context' => $usercontext
220 $event->add_record_snapshot('user', $user);
221 $event->trigger();
223 // TODO WORK OUT WHERE THE NAV BAR IS!
224 echo $OUTPUT->header();
225 echo '<div class="userprofile">';
228 // Print the standard content of this page, the basic profile info.
229 echo $OUTPUT->heading(fullname($user));
231 if (is_mnet_remote_user($user)) {
232 $sql = "SELECT h.id, h.name, h.wwwroot,
233 a.name as application, a.display_name
234 FROM {mnet_host} h, {mnet_application} a
235 WHERE h.id = ? AND h.applicationid = a.id";
237 $remotehost = $DB->get_record_sql($sql, array($user->mnethostid));
238 $a = new stdclass();
239 $a->remotetype = $remotehost->display_name;
240 $a->remotename = $remotehost->name;
241 $a->remoteurl = $remotehost->wwwroot;
243 echo $OUTPUT->box(get_string('remoteuserinfo', 'mnet', $a), 'remoteuserinfo');
246 echo '<div class="userprofilebox clearfix"><div class="profilepicture">';
247 echo $OUTPUT->user_picture($user, array('size' => 100));
248 echo '</div>';
250 echo '<div class="descriptionbox"><div class="description">';
251 // Print the description.
252 if ($user->description && !isset($hiddenfields['description'])) {
253 if (!empty($CFG->profilesforenrolledusersonly) && !$currentuser &&
254 !$DB->record_exists('role_assignments', array('userid' => $user->id))) {
255 echo get_string('profilenotshown', 'moodle');
256 } else {
257 $user->description = file_rewrite_pluginfile_urls($user->description, 'pluginfile.php', $usercontext->id, 'user',
258 'profile', null);
259 $options = array('overflowdiv' => true);
260 echo format_text($user->description, $user->descriptionformat, $options);
263 echo '</div>';
266 // Print all the little details in a list.
267 echo html_writer::start_tag('dl', array('class' => 'list'));
268 if (!isset($hiddenfields['country']) && $user->country) {
269 echo html_writer::tag('dt', get_string('country'));
270 echo html_writer::tag('dd', get_string($user->country, 'countries'));
273 if (!isset($hiddenfields['city']) && $user->city) {
274 echo html_writer::tag('dt', get_string('city'));
275 echo html_writer::tag('dd', $user->city);
278 if (isset($identityfields['address']) && $user->address) {
279 echo html_writer::tag('dt', get_string('address'));
280 echo html_writer::tag('dd', $user->address);
283 if (isset($identityfields['phone1']) && $user->phone1) {
284 echo html_writer::tag('dt', get_string('phone'));
285 echo html_writer::tag('dd', $user->phone1);
288 if (isset($identityfields['phone2']) && $user->phone2) {
289 echo html_writer::tag('dt', get_string('phone2'));
290 echo html_writer::tag('dd', $user->phone2);
293 if (isset($identityfields['institution']) && $user->institution) {
294 echo html_writer::tag('dt', get_string('institution'));
295 echo html_writer::tag('dd', $user->institution);
298 if (isset($identityfields['department']) && $user->department) {
299 echo html_writer::tag('dt', get_string('department'));
300 echo html_writer::tag('dd', $user->department);
303 if (isset($identityfields['idnumber']) && $user->idnumber) {
304 echo html_writer::tag('dt', get_string('idnumber'));
305 echo html_writer::tag('dd', $user->idnumber);
308 if (isset($identityfields['email']) and ($currentuser
309 or $user->maildisplay == 1
310 or has_capability('moodle/course:useremail', $context)
311 or ($user->maildisplay == 2 and enrol_sharing_course($user, $USER)))) {
312 echo html_writer::tag('dt', get_string('email'));
313 echo html_writer::tag('dd', obfuscate_mailto($user->email, ''));
316 if ($user->url && !isset($hiddenfields['webpage'])) {
317 $url = $user->url;
318 if (strpos($user->url, '://') === false) {
319 $url = 'http://'. $url;
321 $webpageurl = new moodle_url($url);
322 echo html_writer::tag('dt', get_string('webpage'));
323 echo html_writer::tag('dd', html_writer::link($webpageurl, s($user->url)));
326 if ($user->icq && !isset($hiddenfields['icqnumber'])) {
327 $imurl = new moodle_url('http://web.icq.com/wwp', array('uin' => $user->icq) );
328 $iconurl = new moodle_url('http://web.icq.com/whitepages/online', array('icq' => $user->icq, 'img' => '5'));
329 $statusicon = html_writer::tag('img', '', array('src' => $iconurl, 'class' => 'icon icon-post', 'alt' => get_string('status')));
330 echo html_writer::tag('dt', get_string('icqnumber'));
331 echo html_writer::tag('dd', html_writer::link($imurl, s($user->icq) . $statusicon));
334 if ($user->skype && !isset($hiddenfields['skypeid'])) {
335 $imurl = 'skype:'.urlencode($user->skype).'?call';
336 $iconurl = new moodle_url('http://mystatus.skype.com/smallicon/'.$user->skype);
337 if (strpos($CFG->httpswwwroot, 'https:') === 0) {
338 // Bad luck, skype devs are lazy to set up SSL on their servers - see MDL-37233.
339 $statusicon = '';
340 } else {
341 $statusicon = html_writer::empty_tag('img',
342 array('src' => $iconurl, 'class' => 'icon icon-post', 'alt' => get_string('status')));
344 echo html_writer::tag('dt', get_string('skypeid'));
345 echo html_writer::tag('dd', html_writer::link($imurl, s($user->skype) . $statusicon));
347 if ($user->yahoo && !isset($hiddenfields['yahooid'])) {
348 $imurl = new moodle_url('http://edit.yahoo.com/config/send_webmesg', array('.target' => $user->yahoo, '.src' => 'pg'));
349 $iconurl = new moodle_url('http://opi.yahoo.com/online', array('u' => $user->yahoo, 'm' => 'g', 't' => '0'));
350 $statusicon = html_writer::tag('img', '',
351 array('src' => $iconurl, 'class' => 'iconsmall icon-post', 'alt' => get_string('status')));
352 echo html_writer::tag('dt', get_string('yahooid'));
353 echo html_writer::tag('dd', html_writer::link($imurl, s($user->yahoo) . $statusicon));
355 if ($user->aim && !isset($hiddenfields['aimid'])) {
356 $imurl = 'aim:goim?screenname='.urlencode($user->aim);
357 echo html_writer::tag('dt', get_string('aimid'));
358 echo html_writer::tag('dd', html_writer::link($imurl, s($user->aim)));
360 if ($user->msn && !isset($hiddenfields['msnid'])) {
361 echo html_writer::tag('dt', get_string('msnid'));
362 echo html_writer::tag('dd', s($user->msn));
365 // Print the Custom User Fields.
366 profile_display_fields($user->id);
369 if (!isset($hiddenfields['mycourses'])) {
370 if ($mycourses = enrol_get_all_users_courses($user->id, true, null, 'visible DESC, sortorder ASC')) {
371 $shown = 0;
372 $courselisting = '';
373 foreach ($mycourses as $mycourse) {
374 if ($mycourse->category) {
375 context_helper::preload_from_record($mycourse);
376 $ccontext = context_course::instance($mycourse->id);
377 $class = '';
378 if ($mycourse->visible == 0) {
379 if (!has_capability('moodle/course:viewhiddencourses', $ccontext)) {
380 continue;
382 $class = 'class="dimmed"';
384 $courselisting .= "<a href=\"{$CFG->wwwroot}/user/view.php?id={$user->id}&amp;course={$mycourse->id}\" $class >" .
385 $ccontext->get_context_name(false) . "</a>, ";
387 $shown++;
388 if ($shown == 20) {
389 $courselisting .= "...";
390 break;
393 echo html_writer::tag('dt', get_string('courseprofiles'));
394 echo html_writer::tag('dd', rtrim($courselisting, ', '));
397 if (!isset($hiddenfields['firstaccess'])) {
398 if ($user->firstaccess) {
399 $datestring = userdate($user->firstaccess)."&nbsp; (".format_time(time() - $user->firstaccess).")";
400 } else {
401 $datestring = get_string("never");
403 echo html_writer::tag('dt', get_string('firstaccess'));
404 echo html_writer::tag('dd', $datestring);
406 if (!isset($hiddenfields['lastaccess'])) {
407 if ($user->lastaccess) {
408 $datestring = userdate($user->lastaccess)."&nbsp; (".format_time(time() - $user->lastaccess).")";
409 } else {
410 $datestring = get_string("never");
412 echo html_writer::tag('dt', get_string('lastaccess'));
413 echo html_writer::tag('dd', $datestring);
416 if (has_capability('moodle/user:viewlastip', $usercontext) && !isset($hiddenfields['lastip'])) {
417 if ($user->lastip) {
418 $iplookupurl = new moodle_url('/iplookup/index.php', array('ip' => $user->lastip, 'user' => $USER->id));
419 $ipstring = html_writer::link($iplookupurl, $user->lastip);
420 } else {
421 $ipstring = get_string("none");
423 echo html_writer::tag('dt', get_string('lastip'));
424 echo html_writer::tag('dd', $ipstring);
427 // Printing tagged interests.
428 if (!empty($CFG->usetags)) {
429 if ($interests = tag_get_tags_csv('user', $user->id) ) {
430 echo html_writer::tag('dt', get_string('interests'));
431 echo html_writer::tag('dd', $interests);
435 if (!isset($hiddenfields['suspended'])) {
436 if ($user->suspended) {
437 echo html_writer::tag('dt', '&nbsp;');
438 echo html_writer::tag('dd', get_string('suspended', 'auth'));
442 require_once($CFG->libdir . '/badgeslib.php');
443 if (!empty($CFG->enablebadges)) {
444 profile_display_badges($user->id);
447 echo html_writer::end_tag('dl');
448 echo "</div></div>"; // Closing desriptionbox and userprofilebox.
450 echo $OUTPUT->custom_block_region('content');
452 // Print messaging link if allowed.
453 if (isloggedin() && has_capability('moodle/site:sendmessage', $context)
454 && !empty($CFG->messaging) && !isguestuser() && !isguestuser($user) && ($USER->id != $user->id)) {
455 echo '<div class="messagebox">';
456 echo '<a href="'.$CFG->wwwroot.'/message/index.php?id='.$user->id.'">'.get_string('messageselectadd').'</a>';
457 echo '</div>';
460 echo '</div>'; // Userprofile class.
461 echo $OUTPUT->footer();