MDL-45961 Customise this page button on user profile page only works correctly for...
[moodle.git] / user / profile.php
CommitLineData
03d9401e 1<?php
03d9401e
MD
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.
13//
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/>.
16
17/**
18 * Public Profile -- a user's public profile page
19 *
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
24 *
25 * This script implements the user's view of the public profile, and allows editing
26 * of the public profile.
27 *
a2ed6e69 28 * @package core_user
03d9401e
MD
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
33 */
34
35require_once(dirname(__FILE__) . '/../config.php');
36require_once($CFG->dirroot . '/my/lib.php');
da84c149 37require_once($CFG->dirroot . '/tag/lib.php');
03d9401e 38require_once($CFG->dirroot . '/user/profile/lib.php');
50542ff0 39require_once($CFG->libdir.'/filelib.php');
03d9401e 40
9594f50a
JO
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);
03d9401e 45
a2ed6e69 46$PAGE->set_url('/user/profile.php', array('id' => $userid));
81b58cc2 47
03d9401e
MD
48if (!empty($CFG->forceloginforprofiles)) {
49 require_login();
50 if (isguestuser()) {
81b58cc2 51 $SESSION->wantsurl = $PAGE->url->out(false);
03d9401e
MD
52 redirect(get_login_url());
53 }
54} else if (!empty($CFG->forcelogin)) {
55 require_login();
56}
57
a2ed6e69 58$userid = $userid ? $userid : $USER->id; // Owner of the page.
243ab45c 59if ((!$user = $DB->get_record('user', array('id' => $userid))) || ($user->deleted)) {
43731030 60 $PAGE->set_context(context_system::instance());
d05e39b0 61 echo $OUTPUT->header();
243ab45c
MS
62 if (!$user) {
63 echo $OUTPUT->notification(get_string('invaliduser', 'error'));
64 } else {
65 echo $OUTPUT->notification(get_string('userdeleted'));
66 }
d05e39b0
PS
67 echo $OUTPUT->footer();
68 die;
69}
70
03d9401e 71$currentuser = ($user->id == $USER->id);
43731030 72$context = $usercontext = context_user::instance($userid, MUST_EXIST);
03d9401e
MD
73
74if (!$currentuser &&
04aec3da 75 !empty($CFG->forceloginforprofiles) &&
eb8d85c1 76 !has_capability('moodle/user:viewdetails', $context) &&
1291a79f 77 !has_coursecontact_role($userid)) {
181991e7 78
a2ed6e69 79 // Course managers can be browsed at site level. If not forceloginforprofiles, allow access (bug #4366).
03d9401e 80 $struser = get_string('user');
43731030 81 $PAGE->set_context(context_system::instance());
a2ed6e69 82 $PAGE->set_title("$SITE->shortname: $struser"); // Do not leak the name.
03d9401e 83 $PAGE->set_heading("$SITE->shortname: $struser");
a2ed6e69 84 $PAGE->set_url('/user/profile.php', array('id' => $userid));
03d9401e
MD
85 $PAGE->navbar->add($struser);
86 echo $OUTPUT->header();
9ce84113 87 echo $OUTPUT->notification(get_string('usernotavailable', 'error'));
03d9401e
MD
88 echo $OUTPUT->footer();
89 exit;
90}
91
92// Get the profile page. Should always return something unless the database is broken.
93if (!$currentpage = my_get_page($userid, MY_PAGE_PUBLIC)) {
94 print_error('mymoodlesetup');
95}
96
97if (!$currentpage->userid) {
a2ed6e69 98 $context = context_system::instance(); // A trick so that we even see non-sticky blocks.
03d9401e
MD
99}
100
101$PAGE->set_context($context);
fb6bf33b 102$PAGE->set_pagelayout('mypublic');
03d9401e
MD
103$PAGE->set_pagetype('user-profile');
104
a2ed6e69
SH
105// Set up block editing capabilities.
106if (isguestuser()) { // Guests can never edit their profile.
107 $USER->editing = $edit = 0; // Just in case.
108 $PAGE->set_blocks_editing_capability('moodle/my:configsyspages'); // unlikely :).
03d9401e
MD
109} else {
110 if ($currentuser) {
111 $PAGE->set_blocks_editing_capability('moodle/user:manageownblocks');
112 } else {
113 $PAGE->set_blocks_editing_capability('moodle/user:manageblocks');
114 }
115}
116
eb8d85c1
SH
117if (has_capability('moodle/user:viewhiddendetails', $context)) {
118 $hiddenfields = array();
119} else {
c6b44cb4 120 $hiddenfields = array_flip(explode(',', $CFG->hiddenuserfields));
eb8d85c1 121}
03d9401e 122
222ada37 123if (has_capability('moodle/site:viewuseridentity', $context)) {
c6b44cb4 124 $identityfields = array_flip(explode(',', $CFG->showuseridentity));
222ada37
AA
125} else {
126 $identityfields = array();
127}
128
a2ed6e69 129// Start setting up the page.
03d9401e
MD
130$strpublicprofile = get_string('publicprofile');
131
03d9401e
MD
132$PAGE->blocks->add_region('content');
133$PAGE->set_subpage($currentpage->id);
b45ac028
MD
134$PAGE->set_title(fullname($user).": $strpublicprofile");
135$PAGE->set_heading(fullname($user).": $strpublicprofile");
87c215de
SH
136
137if (!$currentuser) {
138 $PAGE->navigation->extend_for_user($user);
5d07e957 139 if ($node = $PAGE->settingsnav->get('userviewingsettings'.$user->id)) {
87c215de 140 $node->forceopen = true;
c07dfe70 141 }
87c215de
SH
142} else if ($node = $PAGE->settingsnav->get('usercurrentsettings', navigation_node::TYPE_CONTAINER)) {
143 $node->forceopen = true;
144}
145if ($node = $PAGE->settingsnav->get('root')) {
146 $node->forceopen = false;
c07dfe70 147}
03d9401e
MD
148
149
a2ed6e69 150// Toggle the editing state and switches.
03d9401e 151if ($PAGE->user_allowed_editing()) {
41dcfbf1
MA
152 if ($reset !== null) {
153 if (!is_null($userid)) {
a2ed6e69 154 if (!$currentpage = my_reset_page($userid, MY_PAGE_PUBLIC, 'user-profile')) {
41dcfbf1
MA
155 print_error('reseterror', 'my');
156 }
157 redirect(new moodle_url('/user/profile.php'));
158 }
a2ed6e69
SH
159 } else if ($edit !== null) { // Editing state was specified.
160 $USER->editing = $edit; // Change editing state.
03d9401e
MD
161 if (!$currentpage->userid && $edit) {
162 // If we are viewing a system page as ordinary user, and the user turns
163 // editing on, copy the system pages as new user pages, and get the
a2ed6e69 164 // new page record.
6288c923 165 if (!$currentpage = my_copy_page($userid, MY_PAGE_PUBLIC, 'user-profile')) {
03d9401e
MD
166 print_error('mymoodlesetup');
167 }
168 $PAGE->set_context($usercontext);
169 $PAGE->set_subpage($currentpage->id);
170 }
a2ed6e69
SH
171 } else { // Editing state is in session.
172 if ($currentpage->userid) { // It's a page we can edit, so load from session.
03d9401e
MD
173 if (!empty($USER->editing)) {
174 $edit = 1;
175 } else {
176 $edit = 0;
177 }
a2ed6e69
SH
178 } else { // It's a system page and they are not allowed to edit system pages.
179 $USER->editing = $edit = 0; // Disable editing completely, just to be safe.
03d9401e
MD
180 }
181 }
182
a2ed6e69 183 // Add button for editing page.
6288c923 184 $params = array('edit' => !$edit, 'id' => $userid);
03d9401e 185
41dcfbf1
MA
186 $resetbutton = '';
187 $resetstring = get_string('resetpage', 'my');
6288c923 188 $reseturl = new moodle_url("$CFG->wwwroot/user/profile.php", array('edit' => 1, 'reset' => 1, 'id' => $userid));
41dcfbf1 189
03d9401e 190 if (!$currentpage->userid) {
a2ed6e69 191 // Viewing a system page -- let the user customise it.
03d9401e
MD
192 $editstring = get_string('updatemymoodleon');
193 $params['edit'] = 1;
194 } else if (empty($edit)) {
195 $editstring = get_string('updatemymoodleon');
41dcfbf1 196 $resetbutton = $OUTPUT->single_button($reseturl, $resetstring);
03d9401e
MD
197 } else {
198 $editstring = get_string('updatemymoodleoff');
41dcfbf1 199 $resetbutton = $OUTPUT->single_button($reseturl, $resetstring);
03d9401e
MD
200 }
201
202 $url = new moodle_url("$CFG->wwwroot/user/profile.php", $params);
203 $button = $OUTPUT->single_button($url, $editstring);
41dcfbf1 204 $PAGE->set_button($resetbutton . $button);
03d9401e
MD
205
206} else {
207 $USER->editing = $edit = 0;
208}
209
a2ed6e69 210// HACK WARNING! This loads up all this page's blocks in the system context.
03d9401e
MD
211if ($currentpage->userid == 0) {
212 $CFG->blockmanagerclass = 'my_syspage_block_manager';
213}
214
b0ad512f
SL
215// Trigger a user profile viewed event.
216$event = \core\event\user_profile_viewed::create(array(
217 'objectid' => $user->id,
218 'relateduserid' => $user->id,
219 'context' => $usercontext
220));
221$event->add_record_snapshot('user', $user);
222$event->trigger();
223
03d9401e
MD
224// TODO WORK OUT WHERE THE NAV BAR IS!
225echo $OUTPUT->header();
99cca847 226echo '<div class="userprofile">';
03d9401e
MD
227
228
a2ed6e69 229// Print the standard content of this page, the basic profile info.
03d9401e
MD
230echo $OUTPUT->heading(fullname($user));
231
232if (is_mnet_remote_user($user)) {
5db29f49
DM
233 $sql = "SELECT h.id, h.name, h.wwwroot,
234 a.name as application, a.display_name
235 FROM {mnet_host} h, {mnet_application} a
236 WHERE h.id = ? AND h.applicationid = a.id";
03d9401e
MD
237
238 $remotehost = $DB->get_record_sql($sql, array($user->mnethostid));
5db29f49
DM
239 $a = new stdclass();
240 $a->remotetype = $remotehost->display_name;
241 $a->remotename = $remotehost->name;
242 $a->remoteurl = $remotehost->wwwroot;
03d9401e 243
5db29f49 244 echo $OUTPUT->box(get_string('remoteuserinfo', 'mnet', $a), 'remoteuserinfo');
03d9401e
MD
245}
246
29ef7d4f 247echo '<div class="userprofilebox clearfix"><div class="profilepicture">';
a2ed6e69 248echo $OUTPUT->user_picture($user, array('size' => 100));
03d9401e
MD
249echo '</div>';
250
29ef7d4f 251echo '<div class="descriptionbox"><div class="description">';
a2ed6e69 252// Print the description.
03d9401e 253if ($user->description && !isset($hiddenfields['description'])) {
a2ed6e69
SH
254 if (!empty($CFG->profilesforenrolledusersonly) && !$currentuser &&
255 !$DB->record_exists('role_assignments', array('userid' => $user->id))) {
03d9401e
MD
256 echo get_string('profilenotshown', 'moodle');
257 } else {
a2ed6e69
SH
258 $user->description = file_rewrite_pluginfile_urls($user->description, 'pluginfile.php', $usercontext->id, 'user',
259 'profile', null);
260 $options = array('overflowdiv' => true);
367a75fa 261 echo format_text($user->description, $user->descriptionformat, $options);
03d9401e
MD
262 }
263}
264echo '</div>';
265
f59d8e45 266
a2ed6e69
SH
267// Print all the little details in a list.
268echo html_writer::start_tag('dl', array('class' => 'list'));
6e5d002e
JF
269if (!isset($hiddenfields['country']) && $user->country) {
270 echo html_writer::tag('dt', get_string('country'));
271 echo html_writer::tag('dd', get_string($user->country, 'countries'));
03d9401e
MD
272}
273
6e5d002e
JF
274if (!isset($hiddenfields['city']) && $user->city) {
275 echo html_writer::tag('dt', get_string('city'));
276 echo html_writer::tag('dd', $user->city);
03d9401e
MD
277}
278
222ada37 279if (isset($identityfields['address']) && $user->address) {
6e5d002e
JF
280 echo html_writer::tag('dt', get_string('address'));
281 echo html_writer::tag('dd', $user->address);
222ada37
AA
282}
283
284if (isset($identityfields['phone1']) && $user->phone1) {
6e5d002e
JF
285 echo html_writer::tag('dt', get_string('phone'));
286 echo html_writer::tag('dd', $user->phone1);
222ada37
AA
287}
288
289if (isset($identityfields['phone2']) && $user->phone2) {
6e5d002e
JF
290 echo html_writer::tag('dt', get_string('phone2'));
291 echo html_writer::tag('dd', $user->phone2);
03d9401e
MD
292}
293
222ada37 294if (isset($identityfields['institution']) && $user->institution) {
6e5d002e
JF
295 echo html_writer::tag('dt', get_string('institution'));
296 echo html_writer::tag('dd', $user->institution);
222ada37
AA
297}
298
299if (isset($identityfields['department']) && $user->department) {
6e5d002e
JF
300 echo html_writer::tag('dt', get_string('department'));
301 echo html_writer::tag('dd', $user->department);
222ada37
AA
302}
303
cc21d0ff 304if (isset($identityfields['idnumber']) && $user->idnumber) {
6e5d002e
JF
305 echo html_writer::tag('dt', get_string('idnumber'));
306 echo html_writer::tag('dd', $user->idnumber);
03d9401e
MD
307}
308
cc21d0ff 309if (isset($identityfields['email']) and ($currentuser
181991e7
PS
310 or $user->maildisplay == 1
311 or has_capability('moodle/course:useremail', $context)
cc21d0ff 312 or ($user->maildisplay == 2 and enrol_sharing_course($user, $USER)))) {
6e5d002e
JF
313 echo html_writer::tag('dt', get_string('email'));
314 echo html_writer::tag('dd', obfuscate_mailto($user->email, ''));
03d9401e
MD
315}
316
317if ($user->url && !isset($hiddenfields['webpage'])) {
318 $url = $user->url;
319 if (strpos($user->url, '://') === false) {
320 $url = 'http://'. $url;
321 }
6e5d002e
JF
322 $webpageurl = new moodle_url($url);
323 echo html_writer::tag('dt', get_string('webpage'));
324 echo html_writer::tag('dd', html_writer::link($webpageurl, s($user->url)));
03d9401e
MD
325}
326
327if ($user->icq && !isset($hiddenfields['icqnumber'])) {
a2ed6e69
SH
328 $imurl = new moodle_url('http://web.icq.com/wwp', array('uin' => $user->icq) );
329 $iconurl = new moodle_url('http://web.icq.com/whitepages/online', array('icq' => $user->icq, 'img' => '5'));
330 $statusicon = html_writer::tag('img', '', array('src' => $iconurl, 'class' => 'icon icon-post', 'alt' => get_string('status')));
6e5d002e
JF
331 echo html_writer::tag('dt', get_string('icqnumber'));
332 echo html_writer::tag('dd', html_writer::link($imurl, s($user->icq) . $statusicon));
03d9401e
MD
333}
334
335if ($user->skype && !isset($hiddenfields['skypeid'])) {
6e5d002e
JF
336 $imurl = 'skype:'.urlencode($user->skype).'?call';
337 $iconurl = new moodle_url('http://mystatus.skype.com/smallicon/'.$user->skype);
d2fec87b
PS
338 if (strpos($CFG->httpswwwroot, 'https:') === 0) {
339 // Bad luck, skype devs are lazy to set up SSL on their servers - see MDL-37233.
340 $statusicon = '';
341 } else {
a2ed6e69
SH
342 $statusicon = html_writer::empty_tag('img',
343 array('src' => $iconurl, 'class' => 'icon icon-post', 'alt' => get_string('status')));
d2fec87b 344 }
6e5d002e
JF
345 echo html_writer::tag('dt', get_string('skypeid'));
346 echo html_writer::tag('dd', html_writer::link($imurl, s($user->skype) . $statusicon));
03d9401e
MD
347}
348if ($user->yahoo && !isset($hiddenfields['yahooid'])) {
a2ed6e69
SH
349 $imurl = new moodle_url('http://edit.yahoo.com/config/send_webmesg', array('.target' => $user->yahoo, '.src' => 'pg'));
350 $iconurl = new moodle_url('http://opi.yahoo.com/online', array('u' => $user->yahoo, 'm' => 'g', 't' => '0'));
351 $statusicon = html_writer::tag('img', '',
352 array('src' => $iconurl, 'class' => 'iconsmall icon-post', 'alt' => get_string('status')));
6e5d002e
JF
353 echo html_writer::tag('dt', get_string('yahooid'));
354 echo html_writer::tag('dd', html_writer::link($imurl, s($user->yahoo) . $statusicon));
03d9401e
MD
355}
356if ($user->aim && !isset($hiddenfields['aimid'])) {
6e5d002e
JF
357 $imurl = 'aim:goim?screenname='.urlencode($user->aim);
358 echo html_writer::tag('dt', get_string('aimid'));
359 echo html_writer::tag('dd', html_writer::link($imurl, s($user->aim)));
03d9401e
MD
360}
361if ($user->msn && !isset($hiddenfields['msnid'])) {
6e5d002e
JF
362 echo html_writer::tag('dt', get_string('msnid'));
363 echo html_writer::tag('dd', s($user->msn));
03d9401e
MD
364}
365
a2ed6e69 366// Print the Custom User Fields.
03d9401e
MD
367profile_display_fields($user->id);
368
369
370if (!isset($hiddenfields['mycourses'])) {
a2ed6e69
SH
371 if ($mycourses = enrol_get_all_users_courses($user->id, true, null, 'visible DESC, sortorder ASC')) {
372 $shown = 0;
03d9401e
MD
373 $courselisting = '';
374 foreach ($mycourses as $mycourse) {
375 if ($mycourse->category) {
31f40864
TL
376 context_helper::preload_from_record($mycourse);
377 $ccontext = context_course::instance($mycourse->id);
03d9401e
MD
378 $class = '';
379 if ($mycourse->visible == 0) {
df997f84
PS
380 if (!has_capability('moodle/course:viewhiddencourses', $ccontext)) {
381 continue;
382 }
03d9401e
MD
383 $class = 'class="dimmed"';
384 }
8b05f3a3
JO
385 $params = array('id' => $user->id, 'course' => $mycourse->id);
386 if ($showallcourses) {
387 $params['showallcourses'] = 1;
388 }
389 $url = new moodle_url('/user/view.php', $params);
390 $courselisting .= html_writer::link($url, $ccontext->get_context_name(false), array('class' => $class));
391 $courselisting .= ', ';
03d9401e
MD
392 }
393 $shown++;
9594f50a 394 if (!$showallcourses && $shown == 20) {
8b05f3a3 395 $url = new moodle_url('/user/profile.php', array('id' => $user->id, 'showallcourses' => 1));
3e40c056 396 $courselisting .= html_writer::link($url, '...', array('title' => get_string('viewmore')));
03d9401e
MD
397 break;
398 }
399 }
6e5d002e 400 echo html_writer::tag('dt', get_string('courseprofiles'));
a2ed6e69 401 echo html_writer::tag('dd', rtrim($courselisting, ', '));
03d9401e
MD
402 }
403}
404if (!isset($hiddenfields['firstaccess'])) {
405 if ($user->firstaccess) {
406 $datestring = userdate($user->firstaccess)."&nbsp; (".format_time(time() - $user->firstaccess).")";
407 } else {
408 $datestring = get_string("never");
409 }
6e5d002e
JF
410 echo html_writer::tag('dt', get_string('firstaccess'));
411 echo html_writer::tag('dd', $datestring);
03d9401e
MD
412}
413if (!isset($hiddenfields['lastaccess'])) {
414 if ($user->lastaccess) {
415 $datestring = userdate($user->lastaccess)."&nbsp; (".format_time(time() - $user->lastaccess).")";
416 } else {
417 $datestring = get_string("never");
418 }
6e5d002e
JF
419 echo html_writer::tag('dt', get_string('lastaccess'));
420 echo html_writer::tag('dd', $datestring);
03d9401e
MD
421}
422
9d9361e8
DNA
423if (has_capability('moodle/user:viewlastip', $usercontext) && !isset($hiddenfields['lastip'])) {
424 if ($user->lastip) {
6288c923 425 $iplookupurl = new moodle_url('/iplookup/index.php', array('ip' => $user->lastip, 'user' => $user->id));
b80210fc 426 $ipstring = html_writer::link($iplookupurl, $user->lastip);
9d9361e8
DNA
427 } else {
428 $ipstring = get_string("none");
429 }
430 echo html_writer::tag('dt', get_string('lastip'));
431 echo html_writer::tag('dd', $ipstring);
432}
433
a2ed6e69 434// Printing tagged interests.
03d9401e
MD
435if (!empty($CFG->usetags)) {
436 if ($interests = tag_get_tags_csv('user', $user->id) ) {
6e5d002e
JF
437 echo html_writer::tag('dt', get_string('interests'));
438 echo html_writer::tag('dd', $interests);
03d9401e
MD
439 }
440}
441
4ad72c28
PS
442if (!isset($hiddenfields['suspended'])) {
443 if ($user->suspended) {
6e5d002e
JF
444 echo html_writer::tag('dt', '&nbsp;');
445 echo html_writer::tag('dd', get_string('suspended', 'auth'));
4ad72c28
PS
446 }
447}
448
27806552
YB
449require_once($CFG->libdir . '/badgeslib.php');
450if (!empty($CFG->enablebadges)) {
451 profile_display_badges($user->id);
452}
453
6e5d002e
JF
454echo html_writer::end_tag('dl');
455echo "</div></div>"; // Closing desriptionbox and userprofilebox.
225c418f
SH
456
457echo $OUTPUT->custom_block_region('content');
03d9401e 458
a2ed6e69 459// Print messaging link if allowed.
f59d8e45 460if (isloggedin() && has_capability('moodle/site:sendmessage', $context)
04aec3da 461 && !empty($CFG->messaging) && !isguestuser() && !isguestuser($user) && ($USER->id != $user->id)) {
f59d8e45
MD
462 echo '<div class="messagebox">';
463 echo '<a href="'.$CFG->wwwroot.'/message/index.php?id='.$user->id.'">'.get_string('messageselectadd').'</a>';
464 echo '</div>';
465}
466
a2ed6e69 467echo '</div>'; // Userprofile class.
27806552 468echo $OUTPUT->footer();