weekly release 2.4dev
[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
45
81b58cc2
PS
46$PAGE->set_url('/user/profile.php', array('id'=>$userid));
47
03d9401e
MD
48if (!empty($CFG->forceloginforprofiles)) {
49 require_login();
50 if (isguestuser()) {
81b58cc2 51 $SESSION->wantsurl = $PAGE->url->out(false);
03d9401e
MD
52 redirect(get_login_url());
53 }
54} else if (!empty($CFG->forcelogin)) {
55 require_login();
56}
57
58$userid = $userid ? $userid : $USER->id; // Owner of the page
59$user = $DB->get_record('user', array('id' => $userid));
d05e39b0
PS
60
61if ($user->deleted) {
43731030 62 $PAGE->set_context(context_system::instance());
d05e39b0
PS
63 echo $OUTPUT->header();
64 echo $OUTPUT->heading(get_string('userdeleted'));
65 echo $OUTPUT->footer();
66 die;
67}
68
03d9401e 69$currentuser = ($user->id == $USER->id);
43731030 70$context = $usercontext = context_user::instance($userid, MUST_EXIST);
03d9401e
MD
71
72if (!$currentuser &&
04aec3da 73 !empty($CFG->forceloginforprofiles) &&
eb8d85c1 74 !has_capability('moodle/user:viewdetails', $context) &&
1291a79f 75 !has_coursecontact_role($userid)) {
181991e7 76
03d9401e
MD
77 // Course managers can be browsed at site level. If not forceloginforprofiles, allow access (bug #4366)
78 $struser = get_string('user');
43731030 79 $PAGE->set_context(context_system::instance());
b45ac028 80 $PAGE->set_title("$SITE->shortname: $struser"); // Do not leak the name
03d9401e
MD
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->heading(get_string('usernotavailable', 'error'));
86 echo $OUTPUT->footer();
87 exit;
88}
89
90// Get the profile page. Should always return something unless the database is broken.
91if (!$currentpage = my_get_page($userid, MY_PAGE_PUBLIC)) {
92 print_error('mymoodlesetup');
93}
94
95if (!$currentpage->userid) {
43731030 96 $context = context_system::instance(); // A trick so that we even see non-sticky blocks
03d9401e
MD
97}
98
99$PAGE->set_context($context);
100$PAGE->set_pagelayout('mydashboard');
101$PAGE->set_pagetype('user-profile');
102
103// Set up block editing capabilities
104if (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');
112 }
113}
114
eb8d85c1
SH
115if (has_capability('moodle/user:viewhiddendetails', $context)) {
116 $hiddenfields = array();
117} else {
118 $hiddenfields = array_flip(explode(',', $CFG->hiddenuserfields));
119}
03d9401e
MD
120
121// Start setting up the page
122$strpublicprofile = get_string('publicprofile');
123
03d9401e
MD
124$PAGE->blocks->add_region('content');
125$PAGE->set_subpage($currentpage->id);
b45ac028
MD
126$PAGE->set_title(fullname($user).": $strpublicprofile");
127$PAGE->set_heading(fullname($user).": $strpublicprofile");
87c215de
SH
128
129if (!$currentuser) {
130 $PAGE->navigation->extend_for_user($user);
5d07e957 131 if ($node = $PAGE->settingsnav->get('userviewingsettings'.$user->id)) {
87c215de 132 $node->forceopen = true;
c07dfe70 133 }
87c215de
SH
134} else if ($node = $PAGE->settingsnav->get('usercurrentsettings', navigation_node::TYPE_CONTAINER)) {
135 $node->forceopen = true;
136}
137if ($node = $PAGE->settingsnav->get('root')) {
138 $node->forceopen = false;
c07dfe70 139}
03d9401e
MD
140
141
142// Toggle the editing state and switches
143if ($PAGE->user_allowed_editing()) {
144 if ($edit !== null) { // Editing state was specified
145 $USER->editing = $edit; // Change editing state
146 if (!$currentpage->userid && $edit) {
147 // If we are viewing a system page as ordinary user, and the user turns
148 // editing on, copy the system pages as new user pages, and get the
149 // new page record
150 if (!$currentpage = my_copy_page($USER->id, MY_PAGE_PUBLIC, 'user-profile')) {
151 print_error('mymoodlesetup');
152 }
153 $PAGE->set_context($usercontext);
154 $PAGE->set_subpage($currentpage->id);
155 }
156 } else { // Editing state is in session
157 if ($currentpage->userid) { // It's a page we can edit, so load from session
158 if (!empty($USER->editing)) {
159 $edit = 1;
160 } else {
161 $edit = 0;
162 }
163 } else { // It's a system page and they are not allowed to edit system pages
164 $USER->editing = $edit = 0; // Disable editing completely, just to be safe
165 }
166 }
167
168 // Add button for editing page
169 $params = array('edit' => !$edit);
170
171 if (!$currentpage->userid) {
172 // viewing a system page -- let the user customise it
173 $editstring = get_string('updatemymoodleon');
174 $params['edit'] = 1;
175 } else if (empty($edit)) {
176 $editstring = get_string('updatemymoodleon');
177 } else {
178 $editstring = get_string('updatemymoodleoff');
179 }
180
181 $url = new moodle_url("$CFG->wwwroot/user/profile.php", $params);
182 $button = $OUTPUT->single_button($url, $editstring);
183 $PAGE->set_button($button);
184
185} else {
186 $USER->editing = $edit = 0;
187}
188
189// HACK WARNING! This loads up all this page's blocks in the system context
190if ($currentpage->userid == 0) {
191 $CFG->blockmanagerclass = 'my_syspage_block_manager';
192}
193
194// TODO WORK OUT WHERE THE NAV BAR IS!
99cca847 195
03d9401e 196echo $OUTPUT->header();
99cca847 197echo '<div class="userprofile">';
03d9401e
MD
198
199
200// Print the standard content of this page, the basic profile info
201
202echo $OUTPUT->heading(fullname($user));
203
204if (is_mnet_remote_user($user)) {
5db29f49
DM
205 $sql = "SELECT h.id, h.name, h.wwwroot,
206 a.name as application, a.display_name
207 FROM {mnet_host} h, {mnet_application} a
208 WHERE h.id = ? AND h.applicationid = a.id";
03d9401e
MD
209
210 $remotehost = $DB->get_record_sql($sql, array($user->mnethostid));
5db29f49
DM
211 $a = new stdclass();
212 $a->remotetype = $remotehost->display_name;
213 $a->remotename = $remotehost->name;
214 $a->remoteurl = $remotehost->wwwroot;
03d9401e 215
5db29f49 216 echo $OUTPUT->box(get_string('remoteuserinfo', 'mnet', $a), 'remoteuserinfo');
03d9401e
MD
217}
218
29ef7d4f 219echo '<div class="userprofilebox clearfix"><div class="profilepicture">';
03d9401e
MD
220echo $OUTPUT->user_picture($user, array('size'=>100));
221echo '</div>';
222
29ef7d4f 223echo '<div class="descriptionbox"><div class="description">';
03d9401e
MD
224// Print the description
225
226if ($user->description && !isset($hiddenfields['description'])) {
227 if (!empty($CFG->profilesforenrolledusersonly) && !$currentuser && !$DB->record_exists('role_assignments', array('userid'=>$user->id))) {
228 echo get_string('profilenotshown', 'moodle');
229 } else {
64f93798 230 $user->description = file_rewrite_pluginfile_urls($user->description, 'pluginfile.php', $usercontext->id, 'user', 'profile', null);
367a75fa
SH
231 $options = array('overflowdiv'=>true);
232 echo format_text($user->description, $user->descriptionformat, $options);
03d9401e
MD
233 }
234}
235echo '</div>';
236
f59d8e45 237
03d9401e
MD
238// Print all the little details in a list
239
240echo '<table class="list" summary="">';
241
242if (! isset($hiddenfields['country']) && $user->country) {
243 print_row(get_string('country') . ':', get_string($user->country, 'countries'));
244}
245
246if (! isset($hiddenfields['city']) && $user->city) {
247 print_row(get_string('city') . ':', $user->city);
248}
249
250if (has_capability('moodle/user:viewhiddendetails', $context)) {
251 if ($user->address) {
252 print_row(get_string("address").":", "$user->address");
253 }
254 if ($user->phone1) {
255 print_row(get_string("phone").":", "$user->phone1");
256 }
257 if ($user->phone2) {
258 print_row(get_string("phone2").":", "$user->phone2");
259 }
260}
261
181991e7
PS
262if ($currentuser
263 or $user->maildisplay == 1
264 or has_capability('moodle/course:useremail', $context)
265 or ($user->maildisplay == 2 and enrol_sharing_course($user, $USER))) {
03d9401e 266
46505ee7 267 print_row(get_string("email").":", obfuscate_mailto($user->email, ''));
03d9401e
MD
268}
269
270if ($user->url && !isset($hiddenfields['webpage'])) {
271 $url = $user->url;
272 if (strpos($user->url, '://') === false) {
273 $url = 'http://'. $url;
274 }
275 print_row(get_string("webpage") .":", '<a href="'.s($url).'">'.s($user->url).'</a>');
276}
277
278if ($user->icq && !isset($hiddenfields['icqnumber'])) {
279 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>");
280}
281
282if ($user->skype && !isset($hiddenfields['skypeid'])) {
65f78adf 283 print_row(get_string('skypeid').':','<a href="skype:'.urlencode($user->skype).'?call">'.s($user->skype).
03d9401e
MD
284 ' <img src="http://mystatus.skype.com/smallicon/'.urlencode($user->skype).'" alt="'.get_string('status').'" '.
285 ' /></a>');
286}
287if ($user->yahoo && !isset($hiddenfields['yahooid'])) {
288 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>");
289}
290if ($user->aim && !isset($hiddenfields['aimid'])) {
291 print_row(get_string('aimid').':', '<a href="aim:goim?screenname='.urlencode($user->aim).'">'.s($user->aim).'</a>');
292}
293if ($user->msn && !isset($hiddenfields['msnid'])) {
294 print_row(get_string('msnid').':', s($user->msn));
295}
296
297/// Print the Custom User Fields
298profile_display_fields($user->id);
299
300
301if (!isset($hiddenfields['mycourses'])) {
9ffd29ce 302 if ($mycourses = enrol_get_all_users_courses($user->id, true, NULL, 'visible DESC,sortorder ASC')) {
03d9401e
MD
303 $shown=0;
304 $courselisting = '';
305 foreach ($mycourses as $mycourse) {
306 if ($mycourse->category) {
307 $class = '';
308 if ($mycourse->visible == 0) {
43731030 309 $ccontext = context_course::instance($mycourse->id);
df997f84
PS
310 if (!has_capability('moodle/course:viewhiddencourses', $ccontext)) {
311 continue;
312 }
03d9401e
MD
313 $class = 'class="dimmed"';
314 }
315 $courselisting .= "<a href=\"{$CFG->wwwroot}/user/view.php?id={$user->id}&amp;course={$mycourse->id}\" $class >" . format_string($mycourse->fullname) . "</a>, ";
316 }
317 $shown++;
318 if($shown==20) {
319 $courselisting.= "...";
320 break;
321 }
322 }
323 print_row(get_string('courseprofiles').':', rtrim($courselisting,', '));
324 }
325}
326if (!isset($hiddenfields['firstaccess'])) {
327 if ($user->firstaccess) {
328 $datestring = userdate($user->firstaccess)."&nbsp; (".format_time(time() - $user->firstaccess).")";
329 } else {
330 $datestring = get_string("never");
331 }
332 print_row(get_string("firstaccess").":", $datestring);
333}
334if (!isset($hiddenfields['lastaccess'])) {
335 if ($user->lastaccess) {
336 $datestring = userdate($user->lastaccess)."&nbsp; (".format_time(time() - $user->lastaccess).")";
337 } else {
338 $datestring = get_string("never");
339 }
340 print_row(get_string("lastaccess").":", $datestring);
341}
342
343/// Printing tagged interests
344if (!empty($CFG->usetags)) {
345 if ($interests = tag_get_tags_csv('user', $user->id) ) {
346 print_row(get_string('interests') .": ", $interests);
347 }
348}
349
4ad72c28
PS
350if (!isset($hiddenfields['suspended'])) {
351 if ($user->suspended) {
352 print_row('', get_string('suspended', 'auth'));
353 }
354}
355
29ef7d4f 356echo "</table></div></div>";
03d9401e
MD
357
358
359echo $OUTPUT->blocks_for_region('content');
360
f59d8e45
MD
361// Print messaging link if allowed
362if (isloggedin() && has_capability('moodle/site:sendmessage', $context)
04aec3da 363 && !empty($CFG->messaging) && !isguestuser() && !isguestuser($user) && ($USER->id != $user->id)) {
f59d8e45
MD
364 echo '<div class="messagebox">';
365 echo '<a href="'.$CFG->wwwroot.'/message/index.php?id='.$user->id.'">'.get_string('messageselectadd').'</a>';
366 echo '</div>';
367}
368
99cca847
MD
369if ($CFG->debugdisplay && debugging('', DEBUG_DEVELOPER) && $currentuser) { // Show user object
370 echo '<br /><br /><hr />';
371 echo $OUTPUT->heading('DEBUG MODE: User session variables');
372 print_object($USER);
373}
374
375echo '</div>'; // userprofile class
03d9401e
MD
376echo $OUTPUT->footer();
377
378
379function print_row($left, $right) {
d17ba1e1 380 echo "\n<tr><th class=\"label c0\">$left</th><td class=\"info c1\">$right</td></tr>\n";
03d9401e 381}