MDL-39720 added missing setType() in moodleform
[moodle.git] / user / profile.php
blobc3cade4e3ce39c46af1ebc1898c1ab12d37a6f5d
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 $user = $DB->get_record('user', array('id' => $userid));
61 if ($user->deleted) {
62 $PAGE->set_context(context_system::instance());
63 echo $OUTPUT->header();
64 echo $OUTPUT->notification(get_string('userdeleted'));
65 echo $OUTPUT->footer();
66 die;
69 $currentuser = ($user->id == $USER->id);
70 $context = $usercontext = context_user::instance($userid, MUST_EXIST);
72 if (!$currentuser &&
73 !empty($CFG->forceloginforprofiles) &&
74 !has_capability('moodle/user:viewdetails', $context) &&
75 !has_coursecontact_role($userid)) {
77 // Course managers can be browsed at site level. If not forceloginforprofiles, allow access (bug #4366)
78 $struser = get_string('user');
79 $PAGE->set_context(context_system::instance());
80 $PAGE->set_title("$SITE->shortname: $struser"); // Do not leak the name
81 $PAGE->set_heading("$SITE->shortname: $struser");
82 $PAGE->set_url('/user/profile.php', array('id'=>$userid));
83 $PAGE->navbar->add($struser);
84 echo $OUTPUT->header();
85 echo $OUTPUT->notification(get_string('usernotavailable', 'error'));
86 echo $OUTPUT->footer();
87 exit;
90 // Get the profile page. Should always return something unless the database is broken.
91 if (!$currentpage = my_get_page($userid, MY_PAGE_PUBLIC)) {
92 print_error('mymoodlesetup');
95 if (!$currentpage->userid) {
96 $context = context_system::instance(); // A trick so that we even see non-sticky blocks
99 $PAGE->set_context($context);
100 $PAGE->set_pagelayout('mydashboard');
101 $PAGE->set_pagetype('user-profile');
103 // Set up block editing capabilities
104 if (isguestuser()) { // Guests can never edit their profile
105 $USER->editing = $edit = 0; // Just in case
106 $PAGE->set_blocks_editing_capability('moodle/my:configsyspages'); // unlikely :)
107 } else {
108 if ($currentuser) {
109 $PAGE->set_blocks_editing_capability('moodle/user:manageownblocks');
110 } else {
111 $PAGE->set_blocks_editing_capability('moodle/user:manageblocks');
115 if (has_capability('moodle/user:viewhiddendetails', $context)) {
116 $hiddenfields = array();
117 } else {
118 $hiddenfields = array_flip(explode(',', $CFG->hiddenuserfields));
121 if (has_capability('moodle/site:viewuseridentity', $context)) {
122 $identityfields = array_flip(explode(',', $CFG->showuseridentity));
123 } else {
124 $identityfields = array();
127 // Start setting up the page
128 $strpublicprofile = get_string('publicprofile');
130 $PAGE->blocks->add_region('content');
131 $PAGE->set_subpage($currentpage->id);
132 $PAGE->set_title(fullname($user).": $strpublicprofile");
133 $PAGE->set_heading(fullname($user).": $strpublicprofile");
135 if (!$currentuser) {
136 $PAGE->navigation->extend_for_user($user);
137 if ($node = $PAGE->settingsnav->get('userviewingsettings'.$user->id)) {
138 $node->forceopen = true;
140 } else if ($node = $PAGE->settingsnav->get('usercurrentsettings', navigation_node::TYPE_CONTAINER)) {
141 $node->forceopen = true;
143 if ($node = $PAGE->settingsnav->get('root')) {
144 $node->forceopen = false;
148 // Toggle the editing state and switches
149 if ($PAGE->user_allowed_editing()) {
150 if ($edit !== null) { // Editing state was specified
151 $USER->editing = $edit; // Change editing state
152 if (!$currentpage->userid && $edit) {
153 // If we are viewing a system page as ordinary user, and the user turns
154 // editing on, copy the system pages as new user pages, and get the
155 // new page record
156 if (!$currentpage = my_copy_page($USER->id, MY_PAGE_PUBLIC, 'user-profile')) {
157 print_error('mymoodlesetup');
159 $PAGE->set_context($usercontext);
160 $PAGE->set_subpage($currentpage->id);
162 } else { // Editing state is in session
163 if ($currentpage->userid) { // It's a page we can edit, so load from session
164 if (!empty($USER->editing)) {
165 $edit = 1;
166 } else {
167 $edit = 0;
169 } else { // It's a system page and they are not allowed to edit system pages
170 $USER->editing = $edit = 0; // Disable editing completely, just to be safe
174 // Add button for editing page
175 $params = array('edit' => !$edit);
177 if (!$currentpage->userid) {
178 // viewing a system page -- let the user customise it
179 $editstring = get_string('updatemymoodleon');
180 $params['edit'] = 1;
181 } else if (empty($edit)) {
182 $editstring = get_string('updatemymoodleon');
183 } else {
184 $editstring = get_string('updatemymoodleoff');
187 $url = new moodle_url("$CFG->wwwroot/user/profile.php", $params);
188 $button = $OUTPUT->single_button($url, $editstring);
189 $PAGE->set_button($button);
191 } else {
192 $USER->editing = $edit = 0;
195 // HACK WARNING! This loads up all this page's blocks in the system context
196 if ($currentpage->userid == 0) {
197 $CFG->blockmanagerclass = 'my_syspage_block_manager';
200 // TODO WORK OUT WHERE THE NAV BAR IS!
202 echo $OUTPUT->header();
203 echo '<div class="userprofile">';
206 // Print the standard content of this page, the basic profile info
208 echo $OUTPUT->heading(fullname($user));
210 if (is_mnet_remote_user($user)) {
211 $sql = "SELECT h.id, h.name, h.wwwroot,
212 a.name as application, a.display_name
213 FROM {mnet_host} h, {mnet_application} a
214 WHERE h.id = ? AND h.applicationid = a.id";
216 $remotehost = $DB->get_record_sql($sql, array($user->mnethostid));
217 $a = new stdclass();
218 $a->remotetype = $remotehost->display_name;
219 $a->remotename = $remotehost->name;
220 $a->remoteurl = $remotehost->wwwroot;
222 echo $OUTPUT->box(get_string('remoteuserinfo', 'mnet', $a), 'remoteuserinfo');
225 echo '<div class="userprofilebox clearfix"><div class="profilepicture">';
226 echo $OUTPUT->user_picture($user, array('size'=>100));
227 echo '</div>';
229 echo '<div class="descriptionbox"><div class="description">';
230 // Print the description
232 if ($user->description && !isset($hiddenfields['description'])) {
233 if (!empty($CFG->profilesforenrolledusersonly) && !$currentuser && !$DB->record_exists('role_assignments', array('userid'=>$user->id))) {
234 echo get_string('profilenotshown', 'moodle');
235 } else {
236 $user->description = file_rewrite_pluginfile_urls($user->description, 'pluginfile.php', $usercontext->id, 'user', 'profile', null);
237 $options = array('overflowdiv'=>true);
238 echo format_text($user->description, $user->descriptionformat, $options);
241 echo '</div>';
244 // Print all the little details in a list
246 echo html_writer::start_tag('dl', array('class'=>'list'));
247 if (!isset($hiddenfields['country']) && $user->country) {
248 echo html_writer::tag('dt', get_string('country'));
249 echo html_writer::tag('dd', get_string($user->country, 'countries'));
252 if (!isset($hiddenfields['city']) && $user->city) {
253 echo html_writer::tag('dt', get_string('city'));
254 echo html_writer::tag('dd', $user->city);
257 if (isset($identityfields['address']) && $user->address) {
258 echo html_writer::tag('dt', get_string('address'));
259 echo html_writer::tag('dd', $user->address);
262 if (isset($identityfields['phone1']) && $user->phone1) {
263 echo html_writer::tag('dt', get_string('phone'));
264 echo html_writer::tag('dd', $user->phone1);
267 if (isset($identityfields['phone2']) && $user->phone2) {
268 echo html_writer::tag('dt', get_string('phone2'));
269 echo html_writer::tag('dd', $user->phone2);
272 if (isset($identityfields['institution']) && $user->institution) {
273 echo html_writer::tag('dt', get_string('institution'));
274 echo html_writer::tag('dd', $user->institution);
277 if (isset($identityfields['department']) && $user->department) {
278 echo html_writer::tag('dt', get_string('department'));
279 echo html_writer::tag('dd', $user->department);
282 if (isset($identityfields['idnumber']) && $user->idnumber) {
283 echo html_writer::tag('dt', get_string('idnumber'));
284 echo html_writer::tag('dd', $user->idnumber);
287 if (isset($identityfields['email']) and ($currentuser
288 or $user->maildisplay == 1
289 or has_capability('moodle/course:useremail', $context)
290 or ($user->maildisplay == 2 and enrol_sharing_course($user, $USER)))) {
291 echo html_writer::tag('dt', get_string('email'));
292 echo html_writer::tag('dd', obfuscate_mailto($user->email, ''));
295 if ($user->url && !isset($hiddenfields['webpage'])) {
296 $url = $user->url;
297 if (strpos($user->url, '://') === false) {
298 $url = 'http://'. $url;
300 $webpageurl = new moodle_url($url);
301 echo html_writer::tag('dt', get_string('webpage'));
302 echo html_writer::tag('dd', html_writer::link($webpageurl, s($user->url)));
305 if ($user->icq && !isset($hiddenfields['icqnumber'])) {
306 $imurl = new moodle_url('http://web.icq.com/wwp', array('uin'=>$user->icq) );
307 $iconurl = new moodle_url('http://web.icq.com/whitepages/online', array('icq'=>$user->icq, 'img'=>'5'));
308 $statusicon = html_writer::tag('img', '', array('src'=>$iconurl, 'class'=>'icon icon-post', 'alt'=>get_string('status')));
309 echo html_writer::tag('dt', get_string('icqnumber'));
310 echo html_writer::tag('dd', html_writer::link($imurl, s($user->icq) . $statusicon));
313 if ($user->skype && !isset($hiddenfields['skypeid'])) {
314 $imurl = 'skype:'.urlencode($user->skype).'?call';
315 $iconurl = new moodle_url('http://mystatus.skype.com/smallicon/'.$user->skype);
316 if (strpos($CFG->httpswwwroot, 'https:') === 0) {
317 // Bad luck, skype devs are lazy to set up SSL on their servers - see MDL-37233.
318 $statusicon = '';
319 } else {
320 $statusicon = html_writer::empty_tag('img', array('src'=>$iconurl, 'class'=>'icon icon-post', 'alt'=>get_string('status')));
322 echo html_writer::tag('dt', get_string('skypeid'));
323 echo html_writer::tag('dd', html_writer::link($imurl, s($user->skype) . $statusicon));
325 if ($user->yahoo && !isset($hiddenfields['yahooid'])) {
326 $imurl = new moodle_url('http://edit.yahoo.com/config/send_webmesg', array('.target'=>$user->yahoo, '.src'=>'pg'));
327 $iconurl = new moodle_url('http://opi.yahoo.com/online', array('u'=>$user->yahoo, 'm'=>'g', 't'=>'0'));
328 $statusicon = html_writer::tag('img', '', array('src'=>$iconurl, 'class'=>'iconsmall icon-post', 'alt'=>get_string('status')));
329 echo html_writer::tag('dt', get_string('yahooid'));
330 echo html_writer::tag('dd', html_writer::link($imurl, s($user->yahoo) . $statusicon));
332 if ($user->aim && !isset($hiddenfields['aimid'])) {
333 $imurl = 'aim:goim?screenname='.urlencode($user->aim);
334 echo html_writer::tag('dt', get_string('aimid'));
335 echo html_writer::tag('dd', html_writer::link($imurl, s($user->aim)));
337 if ($user->msn && !isset($hiddenfields['msnid'])) {
338 echo html_writer::tag('dt', get_string('msnid'));
339 echo html_writer::tag('dd', s($user->msn));
342 /// Print the Custom User Fields
343 profile_display_fields($user->id);
346 if (!isset($hiddenfields['mycourses'])) {
347 if ($mycourses = enrol_get_all_users_courses($user->id, true, NULL, 'visible DESC,sortorder ASC')) {
348 $shown=0;
349 $courselisting = '';
350 foreach ($mycourses as $mycourse) {
351 if ($mycourse->category) {
352 context_helper::preload_from_record($mycourse);
353 $ccontext = context_course::instance($mycourse->id);
354 $class = '';
355 if ($mycourse->visible == 0) {
356 if (!has_capability('moodle/course:viewhiddencourses', $ccontext)) {
357 continue;
359 $class = 'class="dimmed"';
361 $courselisting .= "<a href=\"{$CFG->wwwroot}/user/view.php?id={$user->id}&amp;course={$mycourse->id}\" $class >" . $ccontext->get_context_name(false) . "</a>, ";
363 $shown++;
364 if($shown==20) {
365 $courselisting.= "...";
366 break;
369 echo html_writer::tag('dt', get_string('courseprofiles'));
370 echo html_writer::tag('dd', rtrim($courselisting,', '));
373 if (!isset($hiddenfields['firstaccess'])) {
374 if ($user->firstaccess) {
375 $datestring = userdate($user->firstaccess)."&nbsp; (".format_time(time() - $user->firstaccess).")";
376 } else {
377 $datestring = get_string("never");
379 echo html_writer::tag('dt', get_string('firstaccess'));
380 echo html_writer::tag('dd', $datestring);
382 if (!isset($hiddenfields['lastaccess'])) {
383 if ($user->lastaccess) {
384 $datestring = userdate($user->lastaccess)."&nbsp; (".format_time(time() - $user->lastaccess).")";
385 } else {
386 $datestring = get_string("never");
388 echo html_writer::tag('dt', get_string('lastaccess'));
389 echo html_writer::tag('dd', $datestring);
392 /// Printing tagged interests
393 if (!empty($CFG->usetags)) {
394 if ($interests = tag_get_tags_csv('user', $user->id) ) {
395 echo html_writer::tag('dt', get_string('interests'));
396 echo html_writer::tag('dd', $interests);
400 if (!isset($hiddenfields['suspended'])) {
401 if ($user->suspended) {
402 echo html_writer::tag('dt', '&nbsp;');
403 echo html_writer::tag('dd', get_string('suspended', 'auth'));
407 require_once($CFG->libdir . '/badgeslib.php');
408 if (!empty($CFG->enablebadges)) {
409 profile_display_badges($user->id);
412 echo html_writer::end_tag('dl');
413 echo "</div></div>"; // Closing desriptionbox and userprofilebox.
414 echo '<div id="region-content" class="block-region"><div class="region-content">';
415 echo $OUTPUT->blocks_for_region('content');
416 echo '</div></div>';
418 // Print messaging link if allowed
419 if (isloggedin() && has_capability('moodle/site:sendmessage', $context)
420 && !empty($CFG->messaging) && !isguestuser() && !isguestuser($user) && ($USER->id != $user->id)) {
421 echo '<div class="messagebox">';
422 echo '<a href="'.$CFG->wwwroot.'/message/index.php?id='.$user->id.'">'.get_string('messageselectadd').'</a>';
423 echo '</div>';
426 if ($CFG->debugdisplay && debugging('', DEBUG_DEVELOPER) && $currentuser) { // Show user object
427 echo '<br /><br /><hr />';
428 echo $OUTPUT->heading('DEBUG MODE: User session variables');
429 print_object($USER);
432 echo '</div>'; // userprofile class
433 echo $OUTPUT->footer();