My Moodle MDL-19124 New version of My Moodle, User profiles and Course profiles,...
[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');
39require_once($CFG->dirroot . '/user/profile/lib.php');
40
41$userid = optional_param('id', 0, PARAM_INT);
42$edit = optional_param('edit', null, PARAM_BOOL); // Turn editing on and off
43
44if (!empty($CFG->forceloginforprofiles)) {
45 require_login();
46 if (isguestuser()) {
47 redirect(get_login_url());
48 }
49} else if (!empty($CFG->forcelogin)) {
50 require_login();
51}
52
53$userid = $userid ? $userid : $USER->id; // Owner of the page
54$user = $DB->get_record('user', array('id' => $userid));
55$currentuser = ($user->id == $USER->id);
56$context = $usercontext = get_context_instance(CONTEXT_USER, $userid, MUST_EXIST);
57
58if (!$currentuser &&
59 !empty($CFG->forceloginforprofiles) &&
60 !has_capability('moodle/user:viewdetails', $context) &&
61 !has_coursemanager_role($userid)) {
62 // Course managers can be browsed at site level. If not forceloginforprofiles, allow access (bug #4366)
63 $struser = get_string('user');
64 $PAGE->set_title("$SITE->shortname: $struser");
65 $PAGE->set_heading("$SITE->shortname: $struser");
66 $PAGE->set_url('/user/profile.php', array('id'=>$userid));
67 $PAGE->navbar->add($struser);
68 echo $OUTPUT->header();
69 echo $OUTPUT->heading(get_string('usernotavailable', 'error'));
70 echo $OUTPUT->footer();
71 exit;
72}
73
74// Get the profile page. Should always return something unless the database is broken.
75if (!$currentpage = my_get_page($userid, MY_PAGE_PUBLIC)) {
76 print_error('mymoodlesetup');
77}
78
79if (!$currentpage->userid) {
80 $context = get_context_instance(CONTEXT_SYSTEM); // A trick so that we even see non-sticky blocks
81}
82
83$PAGE->set_context($context);
84$PAGE->set_pagelayout('mydashboard');
85$PAGE->set_pagetype('user-profile');
86
87// Set up block editing capabilities
88if (isguestuser()) { // Guests can never edit their profile
89 $USER->editing = $edit = 0; // Just in case
90 $PAGE->set_blocks_editing_capability('moodle/my:configsyspages'); // unlikely :)
91} else {
92 if ($currentuser) {
93 $PAGE->set_blocks_editing_capability('moodle/user:manageownblocks');
94 } else {
95 $PAGE->set_blocks_editing_capability('moodle/user:manageblocks');
96 }
97}
98
99
100
101// Start setting up the page
102$strpublicprofile = get_string('publicprofile');
103
104$params = array('id'=>$userid);
105$PAGE->set_url('/user/profile.php', $params);
106$PAGE->blocks->add_region('content');
107$PAGE->set_subpage($currentpage->id);
108$PAGE->set_title("$SITE->shortname: $strpublicprofile");
109$PAGE->set_heading("$SITE->shortname: $strpublicprofile");
110$PAGE->navigation->extend_for_user($user);
111
112
113// Toggle the editing state and switches
114if ($PAGE->user_allowed_editing()) {
115 if ($edit !== null) { // Editing state was specified
116 $USER->editing = $edit; // Change editing state
117 if (!$currentpage->userid && $edit) {
118 // If we are viewing a system page as ordinary user, and the user turns
119 // editing on, copy the system pages as new user pages, and get the
120 // new page record
121 if (!$currentpage = my_copy_page($USER->id, MY_PAGE_PUBLIC, 'user-profile')) {
122 print_error('mymoodlesetup');
123 }
124 $PAGE->set_context($usercontext);
125 $PAGE->set_subpage($currentpage->id);
126 }
127 } else { // Editing state is in session
128 if ($currentpage->userid) { // It's a page we can edit, so load from session
129 if (!empty($USER->editing)) {
130 $edit = 1;
131 } else {
132 $edit = 0;
133 }
134 } else { // It's a system page and they are not allowed to edit system pages
135 $USER->editing = $edit = 0; // Disable editing completely, just to be safe
136 }
137 }
138
139 // Add button for editing page
140 $params = array('edit' => !$edit);
141
142 if (!$currentpage->userid) {
143 // viewing a system page -- let the user customise it
144 $editstring = get_string('updatemymoodleon');
145 $params['edit'] = 1;
146 } else if (empty($edit)) {
147 $editstring = get_string('updatemymoodleon');
148 } else {
149 $editstring = get_string('updatemymoodleoff');
150 }
151
152 $url = new moodle_url("$CFG->wwwroot/user/profile.php", $params);
153 $button = $OUTPUT->single_button($url, $editstring);
154 $PAGE->set_button($button);
155
156} else {
157 $USER->editing = $edit = 0;
158}
159
160// HACK WARNING! This loads up all this page's blocks in the system context
161if ($currentpage->userid == 0) {
162 $CFG->blockmanagerclass = 'my_syspage_block_manager';
163}
164
165// TODO WORK OUT WHERE THE NAV BAR IS!
166echo $OUTPUT->header();
167
168
169// Print the standard content of this page, the basic profile info
170
171echo $OUTPUT->heading(fullname($user));
172
173if (is_mnet_remote_user($user)) {
174 $sql = "
175 SELECT DISTINCT h.id, h.name, h.wwwroot,
176 a.name as application, a.display_name
177 FROM {mnet_host} h, {mnet_application} a
178 WHERE h.id = ? AND h.applicationid = a.id
179 ORDER BY a.display_name, h.name";
180
181 $remotehost = $DB->get_record_sql($sql, array($user->mnethostid));
182
183 echo '<p class="errorboxcontent">'.get_string('remoteappuser', $remotehost->application)." <br />\n";
184 if ($currentuser) {
185 if ($remotehost->application =='moodle') {
186 echo "Remote {$remotehost->display_name}: <a href=\"{$remotehost->wwwroot}/user/edit.php\">{$remotehost->name}</a> ".get_string('editremoteprofile')." </p>\n";
187 } else {
188 echo "Remote {$remotehost->display_name}: <a href=\"{$remotehost->wwwroot}/\">{$remotehost->name}</a> ".get_string('gotoyourserver')." </p>\n";
189 }
190 } else {
191 echo "Remote {$remotehost->display_name}: <a href=\"{$remotehost->wwwroot}/\">{$remotehost->name}</a></p>\n";
192 }
193}
194
195echo '<div class="profilepicture">';
196echo $OUTPUT->user_picture($user, array('size'=>100));
197echo '</div>';
198
199echo '<div class="description">';
200// Print the description
201
202if ($user->description && !isset($hiddenfields['description'])) {
203 if (!empty($CFG->profilesforenrolledusersonly) && !$currentuser && !$DB->record_exists('role_assignments', array('userid'=>$user->id))) {
204 echo get_string('profilenotshown', 'moodle');
205 } else {
206 $user->description = file_rewrite_pluginfile_urls($user->description, 'pluginfile.php', $usercontext->id, 'user_profile', $user->id);
207 echo format_text($user->description, $user->descriptionformat);
208 }
209}
210echo '</div>';
211
212// Print all the little details in a list
213
214echo '<table class="list" summary="">';
215
216if (! isset($hiddenfields['country']) && $user->country) {
217 print_row(get_string('country') . ':', get_string($user->country, 'countries'));
218}
219
220if (! isset($hiddenfields['city']) && $user->city) {
221 print_row(get_string('city') . ':', $user->city);
222}
223
224if (has_capability('moodle/user:viewhiddendetails', $context)) {
225 if ($user->address) {
226 print_row(get_string("address").":", "$user->address");
227 }
228 if ($user->phone1) {
229 print_row(get_string("phone").":", "$user->phone1");
230 }
231 if ($user->phone2) {
232 print_row(get_string("phone2").":", "$user->phone2");
233 }
234}
235
236if ($user->maildisplay == 1
237 or ($user->maildisplay == 2 && !isguestuser())
238 or has_capability('moodle/course:useremail', $context)) {
239
240 $emailswitch = '';
241
242 if ($currentuser or has_capability('moodle/course:useremail', $context)) { /// Can use the enable/disable email stuff
243 if (!empty($enable) and confirm_sesskey()) { /// Recieved a parameter to enable the email address
244 $DB->set_field('user', 'emailstop', 0, array('id'=>$user->id));
245 $user->emailstop = 0;
246 }
247 if (!empty($disable) and confirm_sesskey()) { /// Recieved a parameter to disable the email address
248 $DB->set_field('user', 'emailstop', 1, array('id'=>$user->id));
249 $user->emailstop = 1;
250 }
251 }
252
253 if (has_capability('moodle/course:useremail', $context)) { /// Can use the enable/disable email stuff
254 if ($user->emailstop) {
255 $switchparam = 'enable';
256 $switchtitle = get_string('emaildisable');
257 $switchclick = get_string('emailenableclick');
258 $switchpix = 't/emailno';
259 } else {
260 $switchparam = 'disable';
261 $switchtitle = get_string('emailenable');
262 $switchclick = get_string('emaildisableclick');
263 $switchpix = 't/email';
264 }
265 $emailswitch = "&nbsp;<a title=\"$switchclick\" ".
266 "href=\"profile.php?id=$user->id&amp;$switchparam=1&amp;sesskey=".sesskey()."\">".
267 "<img src=\"" . $OUTPUT->pix_url("$switchpix") . "\" alt=\"$switchclick\" /></a>";
268
269 } else if ($currentuser) { /// Can only re-enable an email this way
270 if ($user->emailstop) { // Include link that tells how to re-enable their email
271 $switchparam = 'enable';
272 $switchtitle = get_string('emaildisable');
273 $switchclick = get_string('emailenableclick');
274
275 $emailswitch = "&nbsp;(<a title=\"$switchclick\" ".
276 "href=\"profile.php?id=$user->id&amp;enable=1&amp;sesskey=".sesskey()."\">$switchtitle</a>)";
277 }
278 }
279
280 print_row(get_string("email").":", obfuscate_mailto($user->email, '', $user->emailstop)."$emailswitch");
281}
282
283if ($user->url && !isset($hiddenfields['webpage'])) {
284 $url = $user->url;
285 if (strpos($user->url, '://') === false) {
286 $url = 'http://'. $url;
287 }
288 print_row(get_string("webpage") .":", '<a href="'.s($url).'">'.s($user->url).'</a>');
289}
290
291if ($user->icq && !isset($hiddenfields['icqnumber'])) {
292 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>");
293}
294
295if ($user->skype && !isset($hiddenfields['skypeid'])) {
296 print_row(get_string('skypeid').':','<a href="callto:'.urlencode($user->skype).'">'.s($user->skype).
297 ' <img src="http://mystatus.skype.com/smallicon/'.urlencode($user->skype).'" alt="'.get_string('status').'" '.
298 ' /></a>');
299}
300if ($user->yahoo && !isset($hiddenfields['yahooid'])) {
301 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>");
302}
303if ($user->aim && !isset($hiddenfields['aimid'])) {
304 print_row(get_string('aimid').':', '<a href="aim:goim?screenname='.urlencode($user->aim).'">'.s($user->aim).'</a>');
305}
306if ($user->msn && !isset($hiddenfields['msnid'])) {
307 print_row(get_string('msnid').':', s($user->msn));
308}
309
310/// Print the Custom User Fields
311profile_display_fields($user->id);
312
313
314if (!isset($hiddenfields['mycourses'])) {
315 if ($mycourses = get_my_courses($user->id, 'visible DESC,sortorder ASC', null, false, 21)) {
316 $shown=0;
317 $courselisting = '';
318 foreach ($mycourses as $mycourse) {
319 if ($mycourse->category) {
320 $class = '';
321 if ($mycourse->visible == 0) {
322 // get_my_courses will filter courses $USER cannot see
323 // if we get one with visible 0 it just means it's hidden
324 // ... but not from $USER
325 $class = 'class="dimmed"';
326 }
327 $courselisting .= "<a href=\"{$CFG->wwwroot}/user/view.php?id={$user->id}&amp;course={$mycourse->id}\" $class >" . format_string($mycourse->fullname) . "</a>, ";
328 }
329 $shown++;
330 if($shown==20) {
331 $courselisting.= "...";
332 break;
333 }
334 }
335 print_row(get_string('courseprofiles').':', rtrim($courselisting,', '));
336 }
337}
338if (!isset($hiddenfields['firstaccess'])) {
339 if ($user->firstaccess) {
340 $datestring = userdate($user->firstaccess)."&nbsp; (".format_time(time() - $user->firstaccess).")";
341 } else {
342 $datestring = get_string("never");
343 }
344 print_row(get_string("firstaccess").":", $datestring);
345}
346if (!isset($hiddenfields['lastaccess'])) {
347 if ($user->lastaccess) {
348 $datestring = userdate($user->lastaccess)."&nbsp; (".format_time(time() - $user->lastaccess).")";
349 } else {
350 $datestring = get_string("never");
351 }
352 print_row(get_string("lastaccess").":", $datestring);
353}
354
355/// Printing tagged interests
356if (!empty($CFG->usetags)) {
357 if ($interests = tag_get_tags_csv('user', $user->id) ) {
358 print_row(get_string('interests') .": ", $interests);
359 }
360}
361
362echo "</table>";
363
364
365echo $OUTPUT->blocks_for_region('content');
366
367echo $OUTPUT->footer();
368
369
370function print_row($left, $right) {
371 echo "\n<tr><td class=\"label c0\">$left</td><td class=\"info c1\">$right</td></tr>\n";
372}