Merge branch 'MDL-44272-master' of git://github.com/ankitagarwal/moodle
[moodle.git] / user / profile.php
blob8b7153c70007f20e209e469862bfcf46d5578915
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 // TODO WORK OUT WHERE THE NAV BAR IS!
215 echo $OUTPUT->header();
216 echo '<div class="userprofile">';
219 // Print the standard content of this page, the basic profile info.
220 echo $OUTPUT->heading(fullname($user));
222 if (is_mnet_remote_user($user)) {
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";
228 $remotehost = $DB->get_record_sql($sql, array($user->mnethostid));
229 $a = new stdclass();
230 $a->remotetype = $remotehost->display_name;
231 $a->remotename = $remotehost->name;
232 $a->remoteurl = $remotehost->wwwroot;
234 echo $OUTPUT->box(get_string('remoteuserinfo', 'mnet', $a), 'remoteuserinfo');
237 echo '<div class="userprofilebox clearfix"><div class="profilepicture">';
238 echo $OUTPUT->user_picture($user, array('size' => 100));
239 echo '</div>';
241 echo '<div class="descriptionbox"><div class="description">';
242 // Print the description.
243 if ($user->description && !isset($hiddenfields['description'])) {
244 if (!empty($CFG->profilesforenrolledusersonly) && !$currentuser &&
245 !$DB->record_exists('role_assignments', array('userid' => $user->id))) {
246 echo get_string('profilenotshown', 'moodle');
247 } else {
248 $user->description = file_rewrite_pluginfile_urls($user->description, 'pluginfile.php', $usercontext->id, 'user',
249 'profile', null);
250 $options = array('overflowdiv' => true);
251 echo format_text($user->description, $user->descriptionformat, $options);
254 echo '</div>';
257 // Print all the little details in a list.
258 echo html_writer::start_tag('dl', array('class' => 'list'));
259 if (!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'));
264 if (!isset($hiddenfields['city']) && $user->city) {
265 echo html_writer::tag('dt', get_string('city'));
266 echo html_writer::tag('dd', $user->city);
269 if (isset($identityfields['address']) && $user->address) {
270 echo html_writer::tag('dt', get_string('address'));
271 echo html_writer::tag('dd', $user->address);
274 if (isset($identityfields['phone1']) && $user->phone1) {
275 echo html_writer::tag('dt', get_string('phone'));
276 echo html_writer::tag('dd', $user->phone1);
279 if (isset($identityfields['phone2']) && $user->phone2) {
280 echo html_writer::tag('dt', get_string('phone2'));
281 echo html_writer::tag('dd', $user->phone2);
284 if (isset($identityfields['institution']) && $user->institution) {
285 echo html_writer::tag('dt', get_string('institution'));
286 echo html_writer::tag('dd', $user->institution);
289 if (isset($identityfields['department']) && $user->department) {
290 echo html_writer::tag('dt', get_string('department'));
291 echo html_writer::tag('dd', $user->department);
294 if (isset($identityfields['idnumber']) && $user->idnumber) {
295 echo html_writer::tag('dt', get_string('idnumber'));
296 echo html_writer::tag('dd', $user->idnumber);
299 if (isset($identityfields['email']) and ($currentuser
300 or $user->maildisplay == 1
301 or has_capability('moodle/course:useremail', $context)
302 or ($user->maildisplay == 2 and enrol_sharing_course($user, $USER)))) {
303 echo html_writer::tag('dt', get_string('email'));
304 echo html_writer::tag('dd', obfuscate_mailto($user->email, ''));
307 if ($user->url && !isset($hiddenfields['webpage'])) {
308 $url = $user->url;
309 if (strpos($user->url, '://') === false) {
310 $url = 'http://'. $url;
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)));
317 if ($user->icq && !isset($hiddenfields['icqnumber'])) {
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')));
321 echo html_writer::tag('dt', get_string('icqnumber'));
322 echo html_writer::tag('dd', html_writer::link($imurl, s($user->icq) . $statusicon));
325 if ($user->skype && !isset($hiddenfields['skypeid'])) {
326 $imurl = 'skype:'.urlencode($user->skype).'?call';
327 $iconurl = new moodle_url('http://mystatus.skype.com/smallicon/'.$user->skype);
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 {
332 $statusicon = html_writer::empty_tag('img',
333 array('src' => $iconurl, 'class' => 'icon icon-post', 'alt' => get_string('status')));
335 echo html_writer::tag('dt', get_string('skypeid'));
336 echo html_writer::tag('dd', html_writer::link($imurl, s($user->skype) . $statusicon));
338 if ($user->yahoo && !isset($hiddenfields['yahooid'])) {
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')));
343 echo html_writer::tag('dt', get_string('yahooid'));
344 echo html_writer::tag('dd', html_writer::link($imurl, s($user->yahoo) . $statusicon));
346 if ($user->aim && !isset($hiddenfields['aimid'])) {
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)));
351 if ($user->msn && !isset($hiddenfields['msnid'])) {
352 echo html_writer::tag('dt', get_string('msnid'));
353 echo html_writer::tag('dd', s($user->msn));
356 // Print the Custom User Fields.
357 profile_display_fields($user->id);
360 if (!isset($hiddenfields['mycourses'])) {
361 if ($mycourses = enrol_get_all_users_courses($user->id, true, null, 'visible DESC, sortorder ASC')) {
362 $shown = 0;
363 $courselisting = '';
364 foreach ($mycourses as $mycourse) {
365 if ($mycourse->category) {
366 context_helper::preload_from_record($mycourse);
367 $ccontext = context_course::instance($mycourse->id);
368 $class = '';
369 if ($mycourse->visible == 0) {
370 if (!has_capability('moodle/course:viewhiddencourses', $ccontext)) {
371 continue;
373 $class = 'class="dimmed"';
375 $courselisting .= "<a href=\"{$CFG->wwwroot}/user/view.php?id={$user->id}&amp;course={$mycourse->id}\" $class >" .
376 $ccontext->get_context_name(false) . "</a>, ";
378 $shown++;
379 if ($shown == 20) {
380 $courselisting .= "...";
381 break;
384 echo html_writer::tag('dt', get_string('courseprofiles'));
385 echo html_writer::tag('dd', rtrim($courselisting, ', '));
388 if (!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");
394 echo html_writer::tag('dt', get_string('firstaccess'));
395 echo html_writer::tag('dd', $datestring);
397 if (!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");
403 echo html_writer::tag('dt', get_string('lastaccess'));
404 echo html_writer::tag('dd', $datestring);
407 if (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));
410 $ipstring = html_writer::link($iplookupurl, $user->lastip);
411 } else {
412 $ipstring = get_string("none");
414 echo html_writer::tag('dt', get_string('lastip'));
415 echo html_writer::tag('dd', $ipstring);
418 // Printing tagged interests.
419 if (!empty($CFG->usetags)) {
420 if ($interests = tag_get_tags_csv('user', $user->id) ) {
421 echo html_writer::tag('dt', get_string('interests'));
422 echo html_writer::tag('dd', $interests);
426 if (!isset($hiddenfields['suspended'])) {
427 if ($user->suspended) {
428 echo html_writer::tag('dt', '&nbsp;');
429 echo html_writer::tag('dd', get_string('suspended', 'auth'));
433 require_once($CFG->libdir . '/badgeslib.php');
434 if (!empty($CFG->enablebadges)) {
435 profile_display_badges($user->id);
438 echo html_writer::end_tag('dl');
439 echo "</div></div>"; // Closing desriptionbox and userprofilebox.
440 echo '<div id="region-content" class="block-region"><div class="region-content">';
441 echo $OUTPUT->blocks_for_region('content');
442 echo '</div></div>';
444 // Print messaging link if allowed.
445 if (isloggedin() && has_capability('moodle/site:sendmessage', $context)
446 && !empty($CFG->messaging) && !isguestuser() && !isguestuser($user) && ($USER->id != $user->id)) {
447 echo '<div class="messagebox">';
448 echo '<a href="'.$CFG->wwwroot.'/message/index.php?id='.$user->id.'">'.get_string('messageselectadd').'</a>';
449 echo '</div>';
452 echo '</div>'; // Userprofile class.
453 echo $OUTPUT->footer();