3 // This file is part of Moodle - http://moodle.org/
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.
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/>.
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.
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
)) {
51 $SESSION->wantsurl
= $PAGE->url
->out(false);
52 redirect(get_login_url());
54 } else if (!empty($CFG->forcelogin
)) {
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();
63 echo $OUTPUT->notification(get_string('invaliduser', 'error'));
65 echo $OUTPUT->notification(get_string('userdeleted'));
67 echo $OUTPUT->footer();
71 $currentuser = ($user->id
== $USER->id
);
72 $context = $usercontext = context_user
::instance($userid, MUST_EXIST
);
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(context_system
::instance());
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();
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 = context_system
::instance(); // 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 :)
111 $PAGE->set_blocks_editing_capability('moodle/user:manageownblocks');
113 $PAGE->set_blocks_editing_capability('moodle/user:manageblocks');
117 if (has_capability('moodle/user:viewhiddendetails', $context)) {
118 $hiddenfields = array();
120 $hiddenfields = array_flip(explode(',', $CFG->hiddenuserfields
));
123 if (has_capability('moodle/site:viewuseridentity', $context)) {
124 $identityfields = array_flip(explode(',', $CFG->showuseridentity
));
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");
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
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
)) {
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');
183 } else if (empty($edit)) {
184 $editstring = get_string('updatemymoodleon');
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);
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
));
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));
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');
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);
246 // Print all the little details in a list
248 echo html_writer
::start_tag('dl', array('class'=>'list'));
249 if (!isset($hiddenfields['country']) && $user->country
) {
250 echo html_writer
::tag('dt', get_string('country'));
251 echo html_writer
::tag('dd', get_string($user->country
, 'countries'));
254 if (!isset($hiddenfields['city']) && $user->city
) {
255 echo html_writer
::tag('dt', get_string('city'));
256 echo html_writer
::tag('dd', $user->city
);
259 if (isset($identityfields['address']) && $user->address
) {
260 echo html_writer
::tag('dt', get_string('address'));
261 echo html_writer
::tag('dd', $user->address
);
264 if (isset($identityfields['phone1']) && $user->phone1
) {
265 echo html_writer
::tag('dt', get_string('phone'));
266 echo html_writer
::tag('dd', $user->phone1
);
269 if (isset($identityfields['phone2']) && $user->phone2
) {
270 echo html_writer
::tag('dt', get_string('phone2'));
271 echo html_writer
::tag('dd', $user->phone2
);
274 if (isset($identityfields['institution']) && $user->institution
) {
275 echo html_writer
::tag('dt', get_string('institution'));
276 echo html_writer
::tag('dd', $user->institution
);
279 if (isset($identityfields['department']) && $user->department
) {
280 echo html_writer
::tag('dt', get_string('department'));
281 echo html_writer
::tag('dd', $user->department
);
284 if (isset($identityfields['idnumber']) && $user->idnumber
) {
285 echo html_writer
::tag('dt', get_string('idnumber'));
286 echo html_writer
::tag('dd', $user->idnumber
);
289 if (isset($identityfields['email']) and ($currentuser
290 or $user->maildisplay
== 1
291 or has_capability('moodle/course:useremail', $context)
292 or ($user->maildisplay
== 2 and enrol_sharing_course($user, $USER)))) {
293 echo html_writer
::tag('dt', get_string('email'));
294 echo html_writer
::tag('dd', obfuscate_mailto($user->email
, ''));
297 if ($user->url
&& !isset($hiddenfields['webpage'])) {
299 if (strpos($user->url
, '://') === false) {
300 $url = 'http://'. $url;
302 $webpageurl = new moodle_url($url);
303 echo html_writer
::tag('dt', get_string('webpage'));
304 echo html_writer
::tag('dd', html_writer
::link($webpageurl, s($user->url
)));
307 if ($user->icq
&& !isset($hiddenfields['icqnumber'])) {
308 $imurl = new moodle_url('http://web.icq.com/wwp', array('uin'=>$user->icq
) );
309 $iconurl = new moodle_url('http://web.icq.com/whitepages/online', array('icq'=>$user->icq
, 'img'=>'5'));
310 $statusicon = html_writer
::tag('img', '', array('src'=>$iconurl, 'class'=>'icon icon-post', 'alt'=>get_string('status')));
311 echo html_writer
::tag('dt', get_string('icqnumber'));
312 echo html_writer
::tag('dd', html_writer
::link($imurl, s($user->icq
) . $statusicon));
315 if ($user->skype
&& !isset($hiddenfields['skypeid'])) {
316 $imurl = 'skype:'.urlencode($user->skype
).'?call';
317 $iconurl = new moodle_url('http://mystatus.skype.com/smallicon/'.$user->skype
);
318 if (strpos($CFG->httpswwwroot
, 'https:') === 0) {
319 // Bad luck, skype devs are lazy to set up SSL on their servers - see MDL-37233.
322 $statusicon = html_writer
::empty_tag('img', array('src'=>$iconurl, 'class'=>'icon icon-post', 'alt'=>get_string('status')));
324 echo html_writer
::tag('dt', get_string('skypeid'));
325 echo html_writer
::tag('dd', html_writer
::link($imurl, s($user->skype
) . $statusicon));
327 if ($user->yahoo
&& !isset($hiddenfields['yahooid'])) {
328 $imurl = new moodle_url('http://edit.yahoo.com/config/send_webmesg', array('.target'=>$user->yahoo
, '.src'=>'pg'));
329 $iconurl = new moodle_url('http://opi.yahoo.com/online', array('u'=>$user->yahoo
, 'm'=>'g', 't'=>'0'));
330 $statusicon = html_writer
::tag('img', '', array('src'=>$iconurl, 'class'=>'iconsmall icon-post', 'alt'=>get_string('status')));
331 echo html_writer
::tag('dt', get_string('yahooid'));
332 echo html_writer
::tag('dd', html_writer
::link($imurl, s($user->yahoo
) . $statusicon));
334 if ($user->aim
&& !isset($hiddenfields['aimid'])) {
335 $imurl = 'aim:goim?screenname='.urlencode($user->aim
);
336 echo html_writer
::tag('dt', get_string('aimid'));
337 echo html_writer
::tag('dd', html_writer
::link($imurl, s($user->aim
)));
339 if ($user->msn
&& !isset($hiddenfields['msnid'])) {
340 echo html_writer
::tag('dt', get_string('msnid'));
341 echo html_writer
::tag('dd', s($user->msn
));
344 /// Print the Custom User Fields
345 profile_display_fields($user->id
);
348 if (!isset($hiddenfields['mycourses'])) {
349 if ($mycourses = enrol_get_all_users_courses($user->id
, true, NULL, 'visible DESC,sortorder ASC')) {
352 foreach ($mycourses as $mycourse) {
353 if ($mycourse->category
) {
354 context_helper
::preload_from_record($mycourse);
355 $ccontext = context_course
::instance($mycourse->id
);
357 if ($mycourse->visible
== 0) {
358 if (!has_capability('moodle/course:viewhiddencourses', $ccontext)) {
361 $class = 'class="dimmed"';
363 $courselisting .= "<a href=\"{$CFG->wwwroot}/user/view.php?id={$user->id}&course={$mycourse->id}\" $class >" . $ccontext->get_context_name(false) . "</a>, ";
367 $courselisting.= "...";
371 echo html_writer
::tag('dt', get_string('courseprofiles'));
372 echo html_writer
::tag('dd', rtrim($courselisting,', '));
375 if (!isset($hiddenfields['firstaccess'])) {
376 if ($user->firstaccess
) {
377 $datestring = userdate($user->firstaccess
)." (".format_time(time() - $user->firstaccess
).")";
379 $datestring = get_string("never");
381 echo html_writer
::tag('dt', get_string('firstaccess'));
382 echo html_writer
::tag('dd', $datestring);
384 if (!isset($hiddenfields['lastaccess'])) {
385 if ($user->lastaccess
) {
386 $datestring = userdate($user->lastaccess
)." (".format_time(time() - $user->lastaccess
).")";
388 $datestring = get_string("never");
390 echo html_writer
::tag('dt', get_string('lastaccess'));
391 echo html_writer
::tag('dd', $datestring);
394 /// Printing tagged interests
395 if (!empty($CFG->usetags
)) {
396 if ($interests = tag_get_tags_csv('user', $user->id
) ) {
397 echo html_writer
::tag('dt', get_string('interests'));
398 echo html_writer
::tag('dd', $interests);
402 if (!isset($hiddenfields['suspended'])) {
403 if ($user->suspended
) {
404 echo html_writer
::tag('dt', ' ');
405 echo html_writer
::tag('dd', get_string('suspended', 'auth'));
409 require_once($CFG->libdir
. '/badgeslib.php');
410 if (!empty($CFG->enablebadges
)) {
411 profile_display_badges($user->id
);
414 echo html_writer
::end_tag('dl');
415 echo "</div></div>"; // Closing desriptionbox and userprofilebox.
416 echo '<div id="region-content" class="block-region"><div class="region-content">';
417 echo $OUTPUT->blocks_for_region('content');
420 // Print messaging link if allowed
421 if (isloggedin() && has_capability('moodle/site:sendmessage', $context)
422 && !empty($CFG->messaging
) && !isguestuser() && !isguestuser($user) && ($USER->id
!= $user->id
)) {
423 echo '<div class="messagebox">';
424 echo '<a href="'.$CFG->wwwroot
.'/message/index.php?id='.$user->id
.'">'.get_string('messageselectadd').'</a>';
428 echo '</div>'; // userprofile class
429 echo $OUTPUT->footer();