MDL-20442 - now you can switch to ignore empty submits
[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);
c07dfe70
SH
111if ($node = $PAGE->settingsnav->get('userviewingsettings')) {
112 $node->forceopen = true;
113 if ($node = $PAGE->settingsnav->get('root')) {
114 $node->forceopen = false;
115 }
116}
03d9401e
MD
117
118
119// Toggle the editing state and switches
120if ($PAGE->user_allowed_editing()) {
121 if ($edit !== null) { // Editing state was specified
122 $USER->editing = $edit; // Change editing state
123 if (!$currentpage->userid && $edit) {
124 // If we are viewing a system page as ordinary user, and the user turns
125 // editing on, copy the system pages as new user pages, and get the
126 // new page record
127 if (!$currentpage = my_copy_page($USER->id, MY_PAGE_PUBLIC, 'user-profile')) {
128 print_error('mymoodlesetup');
129 }
130 $PAGE->set_context($usercontext);
131 $PAGE->set_subpage($currentpage->id);
132 }
133 } else { // Editing state is in session
134 if ($currentpage->userid) { // It's a page we can edit, so load from session
135 if (!empty($USER->editing)) {
136 $edit = 1;
137 } else {
138 $edit = 0;
139 }
140 } else { // It's a system page and they are not allowed to edit system pages
141 $USER->editing = $edit = 0; // Disable editing completely, just to be safe
142 }
143 }
144
145 // Add button for editing page
146 $params = array('edit' => !$edit);
147
148 if (!$currentpage->userid) {
149 // viewing a system page -- let the user customise it
150 $editstring = get_string('updatemymoodleon');
151 $params['edit'] = 1;
152 } else if (empty($edit)) {
153 $editstring = get_string('updatemymoodleon');
154 } else {
155 $editstring = get_string('updatemymoodleoff');
156 }
157
158 $url = new moodle_url("$CFG->wwwroot/user/profile.php", $params);
159 $button = $OUTPUT->single_button($url, $editstring);
160 $PAGE->set_button($button);
161
162} else {
163 $USER->editing = $edit = 0;
164}
165
166// HACK WARNING! This loads up all this page's blocks in the system context
167if ($currentpage->userid == 0) {
168 $CFG->blockmanagerclass = 'my_syspage_block_manager';
169}
170
171// TODO WORK OUT WHERE THE NAV BAR IS!
172echo $OUTPUT->header();
173
174
175// Print the standard content of this page, the basic profile info
176
177echo $OUTPUT->heading(fullname($user));
178
179if (is_mnet_remote_user($user)) {
180 $sql = "
181 SELECT DISTINCT h.id, h.name, h.wwwroot,
182 a.name as application, a.display_name
183 FROM {mnet_host} h, {mnet_application} a
184 WHERE h.id = ? AND h.applicationid = a.id
185 ORDER BY a.display_name, h.name";
186
187 $remotehost = $DB->get_record_sql($sql, array($user->mnethostid));
188
189 echo '<p class="errorboxcontent">'.get_string('remoteappuser', $remotehost->application)." <br />\n";
190 if ($currentuser) {
191 if ($remotehost->application =='moodle') {
192 echo "Remote {$remotehost->display_name}: <a href=\"{$remotehost->wwwroot}/user/edit.php\">{$remotehost->name}</a> ".get_string('editremoteprofile')." </p>\n";
193 } else {
194 echo "Remote {$remotehost->display_name}: <a href=\"{$remotehost->wwwroot}/\">{$remotehost->name}</a> ".get_string('gotoyourserver')." </p>\n";
195 }
196 } else {
197 echo "Remote {$remotehost->display_name}: <a href=\"{$remotehost->wwwroot}/\">{$remotehost->name}</a></p>\n";
198 }
199}
200
201echo '<div class="profilepicture">';
202echo $OUTPUT->user_picture($user, array('size'=>100));
203echo '</div>';
204
205echo '<div class="description">';
206// Print the description
207
208if ($user->description && !isset($hiddenfields['description'])) {
209 if (!empty($CFG->profilesforenrolledusersonly) && !$currentuser && !$DB->record_exists('role_assignments', array('userid'=>$user->id))) {
210 echo get_string('profilenotshown', 'moodle');
211 } else {
212 $user->description = file_rewrite_pluginfile_urls($user->description, 'pluginfile.php', $usercontext->id, 'user_profile', $user->id);
213 echo format_text($user->description, $user->descriptionformat);
214 }
215}
216echo '</div>';
217
218// Print all the little details in a list
219
220echo '<table class="list" summary="">';
221
222if (! isset($hiddenfields['country']) && $user->country) {
223 print_row(get_string('country') . ':', get_string($user->country, 'countries'));
224}
225
226if (! isset($hiddenfields['city']) && $user->city) {
227 print_row(get_string('city') . ':', $user->city);
228}
229
230if (has_capability('moodle/user:viewhiddendetails', $context)) {
231 if ($user->address) {
232 print_row(get_string("address").":", "$user->address");
233 }
234 if ($user->phone1) {
235 print_row(get_string("phone").":", "$user->phone1");
236 }
237 if ($user->phone2) {
238 print_row(get_string("phone2").":", "$user->phone2");
239 }
240}
241
242if ($user->maildisplay == 1
243 or ($user->maildisplay == 2 && !isguestuser())
244 or has_capability('moodle/course:useremail', $context)) {
245
246 $emailswitch = '';
247
248 if ($currentuser or has_capability('moodle/course:useremail', $context)) { /// Can use the enable/disable email stuff
249 if (!empty($enable) and confirm_sesskey()) { /// Recieved a parameter to enable the email address
250 $DB->set_field('user', 'emailstop', 0, array('id'=>$user->id));
251 $user->emailstop = 0;
252 }
253 if (!empty($disable) and confirm_sesskey()) { /// Recieved a parameter to disable the email address
254 $DB->set_field('user', 'emailstop', 1, array('id'=>$user->id));
255 $user->emailstop = 1;
256 }
257 }
258
259 if (has_capability('moodle/course:useremail', $context)) { /// Can use the enable/disable email stuff
260 if ($user->emailstop) {
261 $switchparam = 'enable';
262 $switchtitle = get_string('emaildisable');
263 $switchclick = get_string('emailenableclick');
264 $switchpix = 't/emailno';
265 } else {
266 $switchparam = 'disable';
267 $switchtitle = get_string('emailenable');
268 $switchclick = get_string('emaildisableclick');
269 $switchpix = 't/email';
270 }
271 $emailswitch = "&nbsp;<a title=\"$switchclick\" ".
272 "href=\"profile.php?id=$user->id&amp;$switchparam=1&amp;sesskey=".sesskey()."\">".
273 "<img src=\"" . $OUTPUT->pix_url("$switchpix") . "\" alt=\"$switchclick\" /></a>";
274
275 } else if ($currentuser) { /// Can only re-enable an email this way
276 if ($user->emailstop) { // Include link that tells how to re-enable their email
277 $switchparam = 'enable';
278 $switchtitle = get_string('emaildisable');
279 $switchclick = get_string('emailenableclick');
280
281 $emailswitch = "&nbsp;(<a title=\"$switchclick\" ".
282 "href=\"profile.php?id=$user->id&amp;enable=1&amp;sesskey=".sesskey()."\">$switchtitle</a>)";
283 }
284 }
285
286 print_row(get_string("email").":", obfuscate_mailto($user->email, '', $user->emailstop)."$emailswitch");
287}
288
289if ($user->url && !isset($hiddenfields['webpage'])) {
290 $url = $user->url;
291 if (strpos($user->url, '://') === false) {
292 $url = 'http://'. $url;
293 }
294 print_row(get_string("webpage") .":", '<a href="'.s($url).'">'.s($user->url).'</a>');
295}
296
297if ($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>");
299}
300
301if ($user->skype && !isset($hiddenfields['skypeid'])) {
302 print_row(get_string('skypeid').':','<a href="callto:'.urlencode($user->skype).'">'.s($user->skype).
303 ' <img src="http://mystatus.skype.com/smallicon/'.urlencode($user->skype).'" alt="'.get_string('status').'" '.
304 ' /></a>');
305}
306if ($user->yahoo && !isset($hiddenfields['yahooid'])) {
307 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>");
308}
309if ($user->aim && !isset($hiddenfields['aimid'])) {
310 print_row(get_string('aimid').':', '<a href="aim:goim?screenname='.urlencode($user->aim).'">'.s($user->aim).'</a>');
311}
312if ($user->msn && !isset($hiddenfields['msnid'])) {
313 print_row(get_string('msnid').':', s($user->msn));
314}
315
316/// Print the Custom User Fields
317profile_display_fields($user->id);
318
319
320if (!isset($hiddenfields['mycourses'])) {
321 if ($mycourses = get_my_courses($user->id, 'visible DESC,sortorder ASC', null, false, 21)) {
322 $shown=0;
323 $courselisting = '';
324 foreach ($mycourses as $mycourse) {
325 if ($mycourse->category) {
326 $class = '';
327 if ($mycourse->visible == 0) {
328 // get_my_courses will filter courses $USER cannot see
329 // if we get one with visible 0 it just means it's hidden
330 // ... but not from $USER
331 $class = 'class="dimmed"';
332 }
333 $courselisting .= "<a href=\"{$CFG->wwwroot}/user/view.php?id={$user->id}&amp;course={$mycourse->id}\" $class >" . format_string($mycourse->fullname) . "</a>, ";
334 }
335 $shown++;
336 if($shown==20) {
337 $courselisting.= "...";
338 break;
339 }
340 }
341 print_row(get_string('courseprofiles').':', rtrim($courselisting,', '));
342 }
343}
344if (!isset($hiddenfields['firstaccess'])) {
345 if ($user->firstaccess) {
346 $datestring = userdate($user->firstaccess)."&nbsp; (".format_time(time() - $user->firstaccess).")";
347 } else {
348 $datestring = get_string("never");
349 }
350 print_row(get_string("firstaccess").":", $datestring);
351}
352if (!isset($hiddenfields['lastaccess'])) {
353 if ($user->lastaccess) {
354 $datestring = userdate($user->lastaccess)."&nbsp; (".format_time(time() - $user->lastaccess).")";
355 } else {
356 $datestring = get_string("never");
357 }
358 print_row(get_string("lastaccess").":", $datestring);
359}
360
361/// Printing tagged interests
362if (!empty($CFG->usetags)) {
363 if ($interests = tag_get_tags_csv('user', $user->id) ) {
364 print_row(get_string('interests') .": ", $interests);
365 }
366}
367
368echo "</table>";
369
370
371echo $OUTPUT->blocks_for_region('content');
372
373echo $OUTPUT->footer();
374
375
376function print_row($left, $right) {
377 echo "\n<tr><td class=\"label c0\">$left</td><td class=\"info c1\">$right</td></tr>\n";
378}