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 * Display profile for a particular user
21 * @copyright 1999 Martin Dougiamas http://dougiamas.com
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 require_once("../config.php");
27 require_once($CFG->dirroot.'/user/profile/lib.php');
28 require_once($CFG->dirroot.'/tag/lib.php');
30 $id = optional_param('id', 0, PARAM_INT); // user id
31 $course = optional_param('course', SITEID, PARAM_INT); // course id (defaults to Site)
32 $enable = optional_param('enable', 0, PARAM_BOOL); // enable email
33 $disable = optional_param('disable', 0, PARAM_BOOL); // disable email
35 if (empty($id)) { // See your own profile by default
40 $url = new moodle_url('/user/view.php', array('id'=>$id));
41 if ($course != SITEID) {
42 $url->param('course', $course);
45 $url->param('enable', $enable);
48 $url->param('disable', $disable);
52 if (! $user = $DB->get_record("user", array("id"=>$id))) {
53 print_error('invaliduserid');
56 if (! $course = $DB->get_record("course", array("id"=>$course))) {
57 print_error('invalidcourseid');
60 // special hack for cli installer - continue to site settings
61 $systemcontext = get_context_instance(CONTEXT_SYSTEM);
62 if ($SITE->shortname === '' and has_capability('moodle/site:config', $systemcontext)) {
63 redirect($CFG->wwwroot .'/'. $CFG->admin .'/index.php');
66 /// Make sure the current user is allowed to see this user
68 if (empty($USER->id)) {
71 $currentuser = ($user->id == $USER->id);
74 if ($course->id == SITEID) {
75 $coursecontext = $systemcontext; // SYSTEM context
77 $coursecontext = get_context_instance(CONTEXT_COURSE, $course->id); // Course context
79 $usercontext = get_context_instance(CONTEXT_USER, $user->id); // User context
81 $PAGE->set_context($usercontext);
83 if (!empty($CFG->forcelogin) || $course->id != SITEID) {
84 // do not force parents to enrol
85 if (!$DB->get_record('role_assignments', array('userid'=>$USER->id, 'contextid'=>$usercontext->id))) {
86 require_login($course->id);
90 if (!empty($CFG->forceloginforprofiles)) {
92 if (has_capability('moodle/legacy:guest', $systemcontext, 0, false)) {
93 redirect(get_login_url());
97 $strpersonalprofile = get_string('personalprofile');
98 $strparticipants = get_string("participants");
99 $struser = get_string("user");
101 $fullname = fullname($user, has_capability('moodle/site:viewfullnames', $coursecontext));
104 if (has_capability('moodle/course:viewparticipants', $coursecontext) || has_capability('moodle/site:viewparticipants', $systemcontext)) {
105 $link = new moodle_url("/user/index.php", array('id'=>$course->id));
108 /// If the user being shown is not ourselves, then make sure we are allowed to see them!
111 $PAGE->set_title("$strpersonalprofile: ");
112 $PAGE->set_heading("$strpersonalprofile: ");
114 if ($course->id == SITEID) { // Reduce possibility of "browsing" userbase at site level
115 if ($CFG->forceloginforprofiles and !isteacherinanycourse()
116 and !isteacherinanycourse($user->id)
117 and !has_capability('moodle/user:viewdetails', $usercontext)) { // Teachers can browse and be browsed at site level. If not forceloginforprofiles, allow access (bug #4366)
119 $PAGE->navbar->add($struser);
120 echo $OUTPUT->header();
121 echo $OUTPUT->heading(get_string('usernotavailable', 'error'));
122 echo $OUTPUT->footer();
125 } else { // Normal course
126 // check capabilities
127 if (!has_capability('moodle/user:viewdetails', $coursecontext) &&
128 !has_capability('moodle/user:viewdetails', $usercontext)) {
129 print_error('cannotviewprofile');
132 if (!has_capability('moodle/course:view', $coursecontext, $user->id, false)) {
133 if (has_capability('moodle/role:assign', $coursecontext)) {
134 $PAGE->navbar->add($fullname);
135 echo $OUTPUT->heading(get_string('notenrolled', $fullname));
137 $PAGE->navbar->add($struser);
138 echo $OUTPUT->heading(get_string('notenrolledprofile'));
140 echo $OUTPUT->continue_button($_SERVER['HTTP_REFERER']);
141 echo $OUTPUT->footer();
147 // If groups are in use, make sure we can see that group
148 if (groups_get_course_groupmode($course) == SEPARATEGROUPS and !has_capability('moodle/site:accessallgroups', $coursecontext)) {
150 ///this is changed because of mygroupid
151 $gtrue = (bool)groups_get_all_groups($course->id, $user->id);
153 print_error("groupnotamember", '', "../course/view.php?id=$course->id");
159 /// We've established they can see the user's name at least, so what about the rest?
161 $PAGE->navigation->extend_for_user($user);
162 $PAGE->set_title("$course->fullname: $strpersonalprofile: $fullname");
163 $PAGE->set_heading($course->fullname);
164 $PAGE->set_pagelayout('standard');
165 echo $OUTPUT->header();
167 if (($course->id != SITEID) and ! has_capability('moodle/legacy:guest', $systemcontext, 0, false) ) { // Need to have access to a course to see that info
168 if (!has_capability('moodle/course:view', $coursecontext, $user->id)) {
169 echo $OUTPUT->heading(get_string('notenrolled', '', $fullname));
170 echo $OUTPUT->footer();
175 if ($user->deleted) {
176 echo $OUTPUT->heading(get_string('userdeleted'));
177 if (!has_capability('moodle/user:update', $coursecontext)) {
178 echo $OUTPUT->footer();
183 /// OK, security out the way, now we are showing the user
185 add_to_log($course->id, "user", "view", "view.php?id=$user->id&course=$course->id", "$user->id");
187 if ($course->id != SITEID) {
188 $user->lastaccess = false;
189 if ($lastaccess = $DB->get_record('user_lastaccess', array('userid'=>$user->id, 'courseid'=>$course->id))) {
190 $user->lastaccess = $lastaccess->timeaccess;
195 /// Get the hidden field list
196 if (has_capability('moodle/user:viewhiddendetails', $coursecontext)) {
197 $hiddenfields = array();
199 $hiddenfields = array_flip(explode(',', $CFG->hiddenuserfields));
202 /// Print tabs at top
203 /// This same call is made in:
208 $currenttab = 'profile';
210 if (!$user->deleted) {
214 if (is_mnet_remote_user($user)) {
216 SELECT DISTINCT h.id, h.name, h.wwwroot,
217 a.name as application, a.display_name
218 FROM {mnet_host} h, {mnet_application} a
219 WHERE h.id = ? AND h.applicationid = a.id
220 ORDER BY a.display_name, h.name";
222 $remotehost = $DB->get_record_sql($sql, array($user->mnethostid));
224 echo '<p class="errorboxcontent">'.get_string('remoteappuser', $remotehost->application)." <br />\n";
225 if ($USER->id == $user->id) {
226 if ($remotehost->application =='moodle') {
227 echo "Remote {$remotehost->display_name}: <a href=\"{$remotehost->wwwroot}/user/edit.php\">{$remotehost->name}</a> ".get_string('editremoteprofile')." </p>\n";
229 echo "Remote {$remotehost->display_name}: <a href=\"{$remotehost->wwwroot}/\">{$remotehost->name}</a> ".get_string('gotoyourserver')." </p>\n";
232 echo "Remote {$remotehost->display_name}: <a href=\"{$remotehost->wwwroot}/\">{$remotehost->name}</a></p>\n";
236 echo '<table width="80%" class="userinfobox" summary="">';
238 echo '<td class="side">';
239 echo $OUTPUT->user_picture($user, array('courseid'=>$course->id, 'size'=>100));
240 echo '</td><td class="content">';
242 // Print the description
244 if ($user->description && !isset($hiddenfields['description'])) {
245 $has_courseid = ($course->id != SITEID);
246 if (!$has_courseid && !empty($CFG->profilesforenrolledusersonly) && !$DB->record_exists('role_assignments', array('userid'=>$id))) {
247 echo get_string('profilenotshown', 'moodle').'<hr />';
250 $user->description = file_rewrite_pluginfile_urls($user->description, 'pluginfile.php', $usercontext->id, 'user_profile', $id);
251 echo format_text($user->description, $user->descriptionformat)."<hr />";
255 // Print all the little details in a list
257 echo '<table class="list">';
259 if (! isset($hiddenfields['country']) && $user->country) {
260 $countries = get_list_of_countries();
261 print_row(get_string('country') . ':', $countries[$user->country]);
264 if (! isset($hiddenfields['city']) && $user->city) {
265 print_row(get_string('city') . ':', $user->city);
268 if (has_capability('moodle/user:viewhiddendetails', $coursecontext)) {
269 if ($user->address) {
270 print_row(get_string("address").":", "$user->address");
273 print_row(get_string("phone").":", "$user->phone1");
276 print_row(get_string("phone2").":", "$user->phone2");
280 if ($user->maildisplay == 1 or
281 ($user->maildisplay == 2 and ($course->id != SITEID) and !isguestuser()) or
282 has_capability('moodle/course:useremail', $coursecontext)) {
286 if (has_capability('moodle/course:useremail', $coursecontext) or $currentuser) { /// Can use the enable/disable email stuff
287 if (!empty($enable)) { /// Recieved a parameter to enable the email address
288 $DB->set_field('user', 'emailstop', 0, array('id'=>$user->id));
289 $user->emailstop = 0;
291 if (!empty($disable)) { /// Recieved a parameter to disable the email address
292 $DB->set_field('user', 'emailstop', 1, array('id'=>$user->id));
293 $user->emailstop = 1;
297 if (has_capability('moodle/course:useremail', $coursecontext)) { /// Can use the enable/disable email stuff
298 if ($user->emailstop) {
299 $switchparam = 'enable';
300 $switchtitle = get_string('emaildisable');
301 $switchclick = get_string('emailenableclick');
302 $switchpix = 't/emailno';
304 $switchparam = 'disable';
305 $switchtitle = get_string('emailenable');
306 $switchclick = get_string('emaildisableclick');
307 $switchpix = 't/email';
309 $emailswitch = " <a title=\"$switchclick\" ".
310 "href=\"view.php?id=$user->id&course=$course->id&$switchparam=1\">".
311 "<img src=\"" . $OUTPUT->pix_url("$switchpix") . "\" alt=\"$switchclick\" /></a>";
313 } else if ($currentuser) { /// Can only re-enable an email this way
314 if ($user->emailstop) { // Include link that tells how to re-enable their email
315 $switchparam = 'enable';
316 $switchtitle = get_string('emaildisable');
317 $switchclick = get_string('emailenableclick');
319 $emailswitch = " (<a title=\"$switchclick\" ".
320 "href=\"view.php?id=$user->id&course=$course->id&enable=1\">$switchtitle</a>)";
324 print_row(get_string("email").":", obfuscate_mailto($user->email, '', $user->emailstop)."$emailswitch");
327 if ($user->url && !isset($hiddenfields['webpage'])) {
329 if (strpos($user->url, '://') === false) {
330 $url = 'http://'. $url;
332 print_row(get_string("webpage") .":", "<a href=\"$url\">$user->url</a>");
335 if ($user->icq && !isset($hiddenfields['icqnumber'])) {
336 print_row(get_string('icqnumber').':',"<a href=\"http://web.icq.com/wwp?uin=$user->icq\">$user->icq <img src=\"http://web.icq.com/whitepages/online?icq=$user->icq&img=5\" alt=\"\" /></a>");
339 if ($user->skype && !isset($hiddenfields['skypeid'])) {
340 print_row(get_string('skypeid').':','<a href="callto:'.urlencode($user->skype).'">'.s($user->skype).
341 ' <img src="http://mystatus.skype.com/smallicon/'.urlencode($user->skype).'" alt="'.get_string('status').'" '.
344 if ($user->yahoo && !isset($hiddenfields['yahooid'])) {
345 print_row(get_string('yahooid').':', '<a href="http://edit.yahoo.com/config/send_webmesg?.target='.urlencode($user->yahoo).'&.src=pg">'.s($user->yahoo)." <img src=\"http://opi.yahoo.com/online?u=".urlencode($user->yahoo)."&m=g&t=0\" alt=\"\"></a>");
347 if ($user->aim && !isset($hiddenfields['aimid'])) {
348 print_row(get_string('aimid').':', '<a href="aim:goim?screenname='.s($user->aim).'">'.s($user->aim).'</a>');
350 if ($user->msn && !isset($hiddenfields['msnid'])) {
351 print_row(get_string('msnid').':', s($user->msn));
354 /// Print the Custom User Fields
355 profile_display_fields($user->id);
358 if (!isset($hiddenfields['mycourses'])) {
359 if ($mycourses = get_my_courses($user->id, 'visible DESC,sortorder ASC', null, false, 21)) {
362 foreach ($mycourses as $mycourse) {
363 if ($mycourse->category) {
364 if ($mycourse->id != $course->id){
366 if ($mycourse->visible == 0) {
367 // get_my_courses will filter courses $USER cannot see
368 // if we get one with visible 0 it just means it's hidden
369 // ... but not from $USER
370 $class = 'class="dimmed"';
372 $courselisting .= "<a href=\"{$CFG->wwwroot}/user/view.php?id={$user->id}&course={$mycourse->id}\" $class >"
373 . format_string($mycourse->fullname) . "</a>, ";
376 $courselisting .= format_string($mycourse->fullname) . ", ";
381 $courselisting.= "...";
385 print_row(get_string('courses').':', rtrim($courselisting,', '));
388 if (!isset($hiddenfields['firstaccess'])) {
389 if ($user->firstaccess) {
390 $datestring = userdate($user->firstaccess)." (".format_time(time() - $user->firstaccess).")";
392 $datestring = get_string("never");
394 print_row(get_string("firstaccess").":", $datestring);
396 if (!isset($hiddenfields['lastaccess'])) {
397 if ($user->lastaccess) {
398 $datestring = userdate($user->lastaccess)." (".format_time(time() - $user->lastaccess).")";
400 $datestring = get_string("never");
402 print_row(get_string("lastaccess").":", $datestring);
406 if ($rolestring = get_user_roles_in_context($id, $coursecontext)) {
407 print_row(get_string('roles').':', format_string($rolestring, false));
411 if (!isset($hiddenfields['groups'])) {
412 $isseparategroups = ($course->groupmode == SEPARATEGROUPS and !has_capability('moodle/site:accessallgroups', $coursecontext));
413 if (!$isseparategroups){
414 if ($usergroups = groups_get_all_groups($course->id, $user->id)){
416 foreach ($usergroups as $group){
417 $groupstr .= ' <a href="'.$CFG->wwwroot.'/user/index.php?id='.$course->id.'&group='.$group->id.'">'.format_string($group->name).'</a>,';
419 print_row(get_string("group").":", rtrim($groupstr, ', '));
423 /// End of printing groups
425 /// Printing Interests
426 if( !empty($CFG->usetags)) {
427 if ( $interests = tag_get_tags_csv('user', $user->id) ) {
428 print_row(get_string('interests') .": ", $interests);
431 /// End of Printing Interests
435 echo "</td></tr></table>";
437 $userauth = get_auth_plugin($user->auth);
439 $passwordchangeurl = false;
440 if ($currentuser and $userauth->can_change_password() and !isguestuser() and has_capability('moodle/user:changeownpassword', $systemcontext)) {
441 if (!$passwordchangeurl = $userauth->change_password_url()) {
442 if (empty($CFG->loginhttps)) {
443 $passwordchangeurl = "$CFG->wwwroot/login/change_password.php";
445 $passwordchangeurl = str_replace('http:', 'https:', $CFG->wwwroot.'/login/change_password.php');
450 // Print other functions
451 echo '<div class="buttons">';
453 if ($passwordchangeurl) {
454 $params = array('id'=>$course->id);
456 if (session_is_loggedinas()) {
457 $passwordchangeurl = ''; // do not use actual change password url - might contain sensitive data
459 $parts = explode('?', $passwordchangeurl);
460 $passwordchangeurl = reset($parts);
461 $after = next($parts);
462 preg_match_all('/([^&=]+)=([^&=]+)/', $after, $matches);
463 if (count($matches)) {
464 foreach($matches[0] as $key=>$match) {
465 $params[$matches[1][$key]] = $matches[2][$key];
469 echo "<form action=\"$passwordchangeurl\" method=\"get\">";
471 foreach($params as $key=>$value) {
472 echo '<input type="hidden" name="'.$key.'" value="'.s($value).'" />';
474 if (session_is_loggedinas()) {
475 // changing of password when "Logged in as" is not allowed
476 echo "<input type=\"submit\" value=\"".get_string("changepassword")."\" disabled=\"disabled\" />";
478 echo "<input type=\"submit\" value=\"".get_string("changepassword")."\" />";
484 if ($course->id != SITEID && empty($course->metacourse)) { // Mostly only useful at course level
488 if ($user->id == $USER->id) { // Myself
489 $canunenrol = has_capability('moodle/course:view', $coursecontext, NULL) && // Course participant
490 has_capability('moodle/role:unassignself', $coursecontext, NULL, false) && // Can unassign myself
491 get_user_roles($coursecontext, $user->id, false); // Must have role in course
493 } else if (has_capability('moodle/role:assign', $coursecontext, NULL)) { // I can assign roles
494 if ($roles = get_user_roles($coursecontext, $user->id, false)) {
496 foreach($roles as $role) {
497 if (!user_can_assign($coursecontext, $role->roleid)) {
498 $canunenrol = false; // I can not unassign all roles in this course :-(
506 echo '<form action="'.$CFG->wwwroot.'/course/unenrol.php" method="get">';
508 echo '<input type="hidden" name="id" value="'.$course->id.'" />';
509 echo '<input type="hidden" name="user" value="'.$user->id.'" />';
510 echo '<input type="submit" value="'.s(get_string('unenrolme', '', $course->shortname)).'" />';
516 if (!$user->deleted and $USER->id != $user->id && !session_is_loggedinas() && has_capability('moodle/user:loginas', $coursecontext) &&
517 ! has_capability('moodle/site:doanything', $coursecontext, $user->id, false)) {
518 echo '<form action="'.$CFG->wwwroot.'/course/loginas.php" method="get">';
520 echo '<input type="hidden" name="id" value="'.$course->id.'" />';
521 echo '<input type="hidden" name="user" value="'.$user->id.'" />';
522 echo '<input type="hidden" name="sesskey" value="'.sesskey().'" />';
523 echo '<input type="submit" value="'.get_string('loginas').'" />';
528 if (!$user->deleted and !empty($CFG->messaging) and !isguestuser() and has_capability('moodle/site:sendmessage', $systemcontext)) {
529 if (!empty($USER->id) and ($USER->id == $user->id)) {
530 if ($countmessages = $DB->count_records('message', array('useridto'=>$user->id))) {
531 $messagebuttonname = get_string("messages", "message")."($countmessages)";
533 $messagebuttonname = get_string("messages", "message");
535 echo "<form onclick=\"this.target='message'\" action=\"../message/index.php\" method=\"get\">";
537 echo "<input type=\"submit\" value=\"$messagebuttonname\" onclick=\"return openpopup('/message/index.php', 'message', 'menubar=0,location=0,scrollbars,status,resizable,width=400,height=500', 0);\" />";
541 echo "<form onclick=\"this.target='message$user->id'\" action=\"../message/discussion.php\" method=\"get\">";
543 echo "<input type=\"hidden\" name=\"id\" value=\"$user->id\" />";
544 echo "<input type=\"submit\" value=\"".get_string("sendmessage", "message")."\" onclick=\"return openpopup('/message/discussion.php?id=$user->id', 'message_$user->id', 'menubar=0,location=0,scrollbars,status,resizable,width=400,height=500', 0);\" />";
549 // Authorize.net: User Payments
550 if ($course->enrol == 'authorize' || (empty($course->enrol) && $CFG->enrol == 'authorize')) {
551 echo "<form action=\"../enrol/authorize/index.php\" method=\"get\">";
553 echo "<input type=\"hidden\" name=\"course\" value=\"$course->id\" />";
554 echo "<input type=\"hidden\" name=\"user\" value=\"$user->id\" />";
555 echo "<input type=\"submit\" value=\"".get_string('payments')."\" />";
561 if ($CFG->debugdisplay && debugging('', DEBUG_DEVELOPER) && $USER->id == $user->id) { // Show user object
563 echo $OUTPUT->heading('DEBUG MODE: User session variables');
567 echo $OUTPUT->footer();
569 /// Functions ///////
571 function print_row($left, $right) {
572 echo "\n<tr><td class=\"label c0\">$left</td><td class=\"info c1\">$right</td></tr>\n";