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