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