MDL-40200 Administration: Return the invalid user message and stop there
[moodle.git] / user / profile.php
blob3e35951ec0fc5a4cd0c027c9776e7455a3e65ada
1 <?php
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.
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/>.
18 /**
19 * Public Profile -- a user's public profile page
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
26 * This script implements the user's view of the public profile, and allows editing
27 * of the public profile.
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
37 require_once(dirname(__FILE__) . '/../config.php');
38 require_once($CFG->dirroot . '/my/lib.php');
39 require_once($CFG->dirroot . '/tag/lib.php');
40 require_once($CFG->dirroot . '/user/profile/lib.php');
41 require_once($CFG->libdir.'/filelib.php');
43 $userid = optional_param('id', 0, PARAM_INT);
44 $edit = optional_param('edit', null, PARAM_BOOL); // Turn editing on and off
46 $PAGE->set_url('/user/profile.php', array('id'=>$userid));
48 if (!empty($CFG->forceloginforprofiles)) {
49 require_login();
50 if (isguestuser()) {
51 $SESSION->wantsurl = $PAGE->url->out(false);
52 redirect(get_login_url());
54 } else if (!empty($CFG->forcelogin)) {
55 require_login();
58 $userid = $userid ? $userid : $USER->id; // Owner of the page
59 if ((!$user = $DB->get_record('user', array('id' => $userid))) || ($user->deleted)) {
60 $PAGE->set_context(context_system::instance());
61 echo $OUTPUT->header();
62 if (!$user) {
63 echo $OUTPUT->notification(get_string('invaliduser', 'error'));
64 } else {
65 echo $OUTPUT->notification(get_string('userdeleted'));
67 echo $OUTPUT->footer();
68 die;
71 $currentuser = ($user->id == $USER->id);
72 $context = $usercontext = get_context_instance(CONTEXT_USER, $userid, MUST_EXIST);
74 if (!$currentuser &&
75 !empty($CFG->forceloginforprofiles) &&
76 !has_capability('moodle/user:viewdetails', $context) &&
77 !has_coursecontact_role($userid)) {
79 // Course managers can be browsed at site level. If not forceloginforprofiles, allow access (bug #4366)
80 $struser = get_string('user');
81 $PAGE->set_context(get_context_instance(CONTEXT_SYSTEM));
82 $PAGE->set_title("$SITE->shortname: $struser"); // Do not leak the name
83 $PAGE->set_heading("$SITE->shortname: $struser");
84 $PAGE->set_url('/user/profile.php', array('id'=>$userid));
85 $PAGE->navbar->add($struser);
86 echo $OUTPUT->header();
87 echo $OUTPUT->notification(get_string('usernotavailable', 'error'));
88 echo $OUTPUT->footer();
89 exit;
92 // Get the profile page. Should always return something unless the database is broken.
93 if (!$currentpage = my_get_page($userid, MY_PAGE_PUBLIC)) {
94 print_error('mymoodlesetup');
97 if (!$currentpage->userid) {
98 $context = get_context_instance(CONTEXT_SYSTEM); // A trick so that we even see non-sticky blocks
101 $PAGE->set_context($context);
102 $PAGE->set_pagelayout('mypublic');
103 $PAGE->set_pagetype('user-profile');
105 // Set up block editing capabilities
106 if (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 :)
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');
117 if (has_capability('moodle/user:viewhiddendetails', $context)) {
118 $hiddenfields = array();
119 } else {
120 $hiddenfields = array_flip(explode(',', $CFG->hiddenuserfields));
123 if (has_capability('moodle/site:viewuseridentity', $context)) {
124 $identityfields = array_flip(explode(',', $CFG->showuseridentity));
125 } else {
126 $identityfields = array();
129 // Start setting up the page
130 $strpublicprofile = get_string('publicprofile');
132 $PAGE->blocks->add_region('content');
133 $PAGE->set_subpage($currentpage->id);
134 $PAGE->set_title(fullname($user).": $strpublicprofile");
135 $PAGE->set_heading(fullname($user).": $strpublicprofile");
137 if (!$currentuser) {
138 $PAGE->navigation->extend_for_user($user);
139 if ($node = $PAGE->settingsnav->get('userviewingsettings'.$user->id)) {
140 $node->forceopen = true;
142 } else if ($node = $PAGE->settingsnav->get('usercurrentsettings', navigation_node::TYPE_CONTAINER)) {
143 $node->forceopen = true;
145 if ($node = $PAGE->settingsnav->get('root')) {
146 $node->forceopen = false;
150 // Toggle the editing state and switches
151 if ($PAGE->user_allowed_editing()) {
152 if ($edit !== null) { // Editing state was specified
153 $USER->editing = $edit; // Change editing state
154 if (!$currentpage->userid && $edit) {
155 // If we are viewing a system page as ordinary user, and the user turns
156 // editing on, copy the system pages as new user pages, and get the
157 // new page record
158 if (!$currentpage = my_copy_page($USER->id, MY_PAGE_PUBLIC, 'user-profile')) {
159 print_error('mymoodlesetup');
161 $PAGE->set_context($usercontext);
162 $PAGE->set_subpage($currentpage->id);
164 } else { // Editing state is in session
165 if ($currentpage->userid) { // It's a page we can edit, so load from session
166 if (!empty($USER->editing)) {
167 $edit = 1;
168 } else {
169 $edit = 0;
171 } else { // It's a system page and they are not allowed to edit system pages
172 $USER->editing = $edit = 0; // Disable editing completely, just to be safe
176 // Add button for editing page
177 $params = array('edit' => !$edit);
179 if (!$currentpage->userid) {
180 // viewing a system page -- let the user customise it
181 $editstring = get_string('updatemymoodleon');
182 $params['edit'] = 1;
183 } else if (empty($edit)) {
184 $editstring = get_string('updatemymoodleon');
185 } else {
186 $editstring = get_string('updatemymoodleoff');
189 $url = new moodle_url("$CFG->wwwroot/user/profile.php", $params);
190 $button = $OUTPUT->single_button($url, $editstring);
191 $PAGE->set_button($button);
193 } else {
194 $USER->editing = $edit = 0;
197 // HACK WARNING! This loads up all this page's blocks in the system context
198 if ($currentpage->userid == 0) {
199 $CFG->blockmanagerclass = 'my_syspage_block_manager';
202 // TODO WORK OUT WHERE THE NAV BAR IS!
204 echo $OUTPUT->header();
205 echo '<div class="userprofile">';
208 // Print the standard content of this page, the basic profile info
210 echo $OUTPUT->heading(fullname($user));
212 if (is_mnet_remote_user($user)) {
213 $sql = "SELECT h.id, h.name, h.wwwroot,
214 a.name as application, a.display_name
215 FROM {mnet_host} h, {mnet_application} a
216 WHERE h.id = ? AND h.applicationid = a.id";
218 $remotehost = $DB->get_record_sql($sql, array($user->mnethostid));
219 $a = new stdclass();
220 $a->remotetype = $remotehost->display_name;
221 $a->remotename = $remotehost->name;
222 $a->remoteurl = $remotehost->wwwroot;
224 echo $OUTPUT->box(get_string('remoteuserinfo', 'mnet', $a), 'remoteuserinfo');
227 echo '<div class="userprofilebox clearfix"><div class="profilepicture">';
228 echo $OUTPUT->user_picture($user, array('size'=>100));
229 echo '</div>';
231 echo '<div class="descriptionbox"><div class="description">';
232 // Print the description
234 if ($user->description && !isset($hiddenfields['description'])) {
235 if (!empty($CFG->profilesforenrolledusersonly) && !$currentuser && !$DB->record_exists('role_assignments', array('userid'=>$user->id))) {
236 echo get_string('profilenotshown', 'moodle');
237 } else {
238 $user->description = file_rewrite_pluginfile_urls($user->description, 'pluginfile.php', $usercontext->id, 'user', 'profile', null);
239 $options = array('overflowdiv'=>true);
240 echo format_text($user->description, $user->descriptionformat, $options);
243 echo '</div>';
246 // Print all the little details in a list
248 echo '<table class="list">';
250 if (! isset($hiddenfields['country']) && $user->country) {
251 print_row(get_string('country') . ':', get_string($user->country, 'countries'));
254 if (! isset($hiddenfields['city']) && $user->city) {
255 print_row(get_string('city') . ':', $user->city);
258 if (isset($identityfields['address']) && $user->address) {
259 print_row(get_string("address").":", "$user->address");
262 if (isset($identityfields['phone1']) && $user->phone1) {
263 print_row(get_string("phone").":", "$user->phone1");
266 if (isset($identityfields['phone2']) && $user->phone2) {
267 print_row(get_string("phone2").":", "$user->phone2");
270 if (isset($identityfields['institution']) && $user->institution) {
271 print_row(get_string("institution").":", "$user->institution");
274 if (isset($identityfields['department']) && $user->department) {
275 print_row(get_string("department").":", "$user->department");
278 if (isset($identityfields['idnumber']) && $user->idnumber) {
279 print_row(get_string("idnumber").":", "$user->idnumber");
282 if (isset($identityfields['email']) and ($currentuser
283 or $user->maildisplay == 1
284 or has_capability('moodle/course:useremail', $context)
285 or ($user->maildisplay == 2 and enrol_sharing_course($user, $USER)))) {
286 print_row(get_string("email").":", obfuscate_mailto($user->email, ''));
289 if ($user->url && !isset($hiddenfields['webpage'])) {
290 $url = $user->url;
291 if (strpos($user->url, '://') === false) {
292 $url = 'http://'. $url;
294 print_row(get_string("webpage") .":", '<a href="'.s($url).'">'.s($user->url).'</a>');
297 if ($user->icq && !isset($hiddenfields['icqnumber'])) {
298 print_row(get_string('icqnumber').':',"<a href=\"http://web.icq.com/wwp?uin=".urlencode($user->icq)."\">".s($user->icq)." <img src=\"http://web.icq.com/whitepages/online?icq=".urlencode($user->icq)."&amp;img=5\" alt=\"\" /></a>");
301 if ($user->skype && !isset($hiddenfields['skypeid'])) {
302 if (strpos($CFG->httpswwwroot, 'https:') === 0) {
303 // Bad luck, skype devs are lazy to set up SSL on their servers - see MDL-37233.
304 $statusicon = '';
305 } else {
306 $statusicon = ' '.html_writer::empty_tag('img', array('src'=>'http://mystatus.skype.com/smallicon/'.urlencode($user->skype), 'alt'=>get_string('status')));
308 print_row(get_string('skypeid').':','<a href="skype:'.urlencode($user->skype).'?call">'.s($user->skype).$statusicon.'</a>');
310 if ($user->yahoo && !isset($hiddenfields['yahooid'])) {
311 print_row(get_string('yahooid').':', '<a href="http://edit.yahoo.com/config/send_webmesg?.target='.urlencode($user->yahoo).'&amp;.src=pg">'.s($user->yahoo)." <img src=\"http://opi.yahoo.com/online?u=".urlencode($user->yahoo)."&m=g&t=0\" alt=\"\"></a>");
313 if ($user->aim && !isset($hiddenfields['aimid'])) {
314 print_row(get_string('aimid').':', '<a href="aim:goim?screenname='.urlencode($user->aim).'">'.s($user->aim).'</a>');
316 if ($user->msn && !isset($hiddenfields['msnid'])) {
317 print_row(get_string('msnid').':', s($user->msn));
320 /// Print the Custom User Fields
321 profile_display_fields($user->id);
324 if (!isset($hiddenfields['mycourses'])) {
325 if ($mycourses = enrol_get_all_users_courses($user->id, true, NULL, 'visible DESC,sortorder ASC')) {
326 $shown=0;
327 $courselisting = '';
328 foreach ($mycourses as $mycourse) {
329 if ($mycourse->category) {
330 context_helper::preload_from_record($mycourse);
331 $ccontext = context_course::instance($mycourse->id);
332 $class = '';
333 if ($mycourse->visible == 0) {
334 if (!has_capability('moodle/course:viewhiddencourses', $ccontext)) {
335 continue;
337 $class = 'class="dimmed"';
339 $courselisting .= "<a href=\"{$CFG->wwwroot}/user/view.php?id={$user->id}&amp;course={$mycourse->id}\" $class >" . $ccontext->get_context_name(false) . "</a>, ";
341 $shown++;
342 if($shown==20) {
343 $courselisting.= "...";
344 break;
347 print_row(get_string('courseprofiles').':', rtrim($courselisting,', '));
350 if (!isset($hiddenfields['firstaccess'])) {
351 if ($user->firstaccess) {
352 $datestring = userdate($user->firstaccess)."&nbsp; (".format_time(time() - $user->firstaccess).")";
353 } else {
354 $datestring = get_string("never");
356 print_row(get_string("firstaccess").":", $datestring);
358 if (!isset($hiddenfields['lastaccess'])) {
359 if ($user->lastaccess) {
360 $datestring = userdate($user->lastaccess)."&nbsp; (".format_time(time() - $user->lastaccess).")";
361 } else {
362 $datestring = get_string("never");
364 print_row(get_string("lastaccess").":", $datestring);
367 /// Printing tagged interests
368 if (!empty($CFG->usetags)) {
369 if ($interests = tag_get_tags_csv('user', $user->id) ) {
370 print_row(get_string('interests') .": ", $interests);
374 if (!isset($hiddenfields['suspended'])) {
375 if ($user->suspended) {
376 print_row('', get_string('suspended', 'auth'));
380 echo "</table></div></div>";
383 echo $OUTPUT->blocks_for_region('content');
385 // Print messaging link if allowed
386 if (isloggedin() && has_capability('moodle/site:sendmessage', $context)
387 && !empty($CFG->messaging) && !isguestuser() && !isguestuser($user) && ($USER->id != $user->id)) {
388 echo '<div class="messagebox">';
389 echo '<a href="'.$CFG->wwwroot.'/message/index.php?id='.$user->id.'">'.get_string('messageselectadd').'</a>';
390 echo '</div>';
393 if ($CFG->debugdisplay && debugging('', DEBUG_DEVELOPER) && $currentuser) { // Show user object
394 echo '<br /><br /><hr />';
395 echo $OUTPUT->heading('DEBUG MODE: User session variables');
396 print_object($USER);
399 echo '</div>'; // userprofile class
400 echo $OUTPUT->footer();
403 function print_row($left, $right) {
404 echo "\n<tr><th class=\"label c0\">$left</th><td class=\"info c1\">$right</td></tr>\n";