6f31b2d8ebb0af2eb3a82a557b3fb510a8594d68
[moodle.git] / user / view.php
1 <?php
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/>.
18 /**
19  * Display profile for a particular user
20  *
21  * @copyright 1999 Martin Dougiamas  http://dougiamas.com
22  * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23  * @package user
24  */
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
36     require_login();
37     $id = $USER->id;
38 }
40 $url = new moodle_url($CFG->wwwroot.'/user/view.php', array('id'=>$id));
41 if ($course !== 0) {
42     $url->param('course', $course);
43 }
44 if ($enable !== 0) {
45     $url->param('enable', $enable);
46 }
47 if ($disable !== 0) {
48     $url->param('disable', $disable);
49 }
50 $PAGE->set_url($url);
52 if (! $user = $DB->get_record("user", array("id"=>$id))) {
53     print_error('invaliduserid');
54 }
56 if (! $course = $DB->get_record("course", array("id"=>$course))) {
57     print_error('invalidcourseid');
58 }
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');
64 }
66 /// Set up page URL for blocks etc
67 if ($course->id == SITEID) {
68     $PAGE->set_url('user/view.php', array('id' => $user->id));
69 } else {
70     $PAGE->set_url('user/view.php', array('id' => $user->id, 'course' => $course->id));
71 }
73 /// Make sure the current user is allowed to see this user
75 if (empty($USER->id)) {
76    $currentuser = false;
77 } else {
78    $currentuser = ($user->id == $USER->id);
79 }
81 if ($course->id == SITEID) {
82     $coursecontext = $systemcontext;   // SYSTEM context
83 } else {
84     $coursecontext = get_context_instance(CONTEXT_COURSE, $course->id);   // Course context
85 }
86 $usercontext   = get_context_instance(CONTEXT_USER, $user->id);       // User context
88 $PAGE->set_context($usercontext);
90 if (!empty($CFG->forcelogin) || $course->id != SITEID) {
91     // do not force parents to enrol
92     if (!$DB->get_record('role_assignments', array('userid'=>$USER->id, 'contextid'=>$usercontext->id))) {
93         require_login($course->id);
94     }
95 }
97 if (!empty($CFG->forceloginforprofiles)) {
98     require_login();
99     if (has_capability('moodle/legacy:guest', $systemcontext, 0, false)) {
100         redirect(get_login_url());
101     }
104 $strpersonalprofile = get_string('personalprofile');
105 $strparticipants = get_string("participants");
106 $struser = get_string("user");
108 $fullname = fullname($user, has_capability('moodle/site:viewfullnames', $coursecontext));
110 $link = null;
111 if (has_capability('moodle/course:viewparticipants', $coursecontext) || has_capability('moodle/site:viewparticipants', $systemcontext)) {
112     $link = new moodle_url($CFG->wwwroot."/user/index.php", array('id'=>$course->id));
114 if ($course->id===SITEID) {
115     $PAGE->navbar->ignore_active();
117 $PAGE->navbar->add($strparticipants, $link);
119 /// If the user being shown is not ourselves, then make sure we are allowed to see them!
121 if (!$currentuser) {
123     $PAGE->set_title("$strpersonalprofile: ");
124     $PAGE->set_heading("$strpersonalprofile: ");
126     if ($course->id == SITEID) {  // Reduce possibility of "browsing" userbase at site level
127         if ($CFG->forceloginforprofiles and !isteacherinanycourse()
128                 and !isteacherinanycourse($user->id)
129                 and !has_capability('moodle/user:viewdetails', $usercontext)) {  // Teachers can browse and be browsed at site level. If not forceloginforprofiles, allow access (bug #4366)
131             $PAGE->navbar->add($struser);
132             echo $OUTPUT->header();
133             echo $OUTPUT->heading(get_string('usernotavailable', 'error'));
134             echo $OUTPUT->footer();
135             exit;
136         }
137     } else {   // Normal course
138         // check capabilities
139         if (!has_capability('moodle/user:viewdetails', $coursecontext) &&
140             !has_capability('moodle/user:viewdetails', $usercontext)) {
141             print_error('cannotviewprofile');
142         }
144         if (!has_capability('moodle/course:view', $coursecontext, $user->id, false)) {
145             if (has_capability('moodle/course:view', $coursecontext)) {
146                 $PAGE->navbar->add($fullname);
147                 echo $OUTPUT->heading(get_string('notenrolled', $fullname));
148             } else {
149                 $PAGE->navbar->add($struser);
150                 echo $OUTPUT->heading(get_string('notenrolledprofile'));
151             }
152             echo $OUTPUT->continue_button($_SERVER['HTTP_REFERER']);
153             echo $OUTPUT->footer();
154             exit;
155         }
156     }
159     // If groups are in use, make sure we can see that group
160     if (groups_get_course_groupmode($course) == SEPARATEGROUPS and !has_capability('moodle/site:accessallgroups', $coursecontext)) {
161         require_login();
162         ///this is changed because of mygroupid
163         $gtrue = (bool)groups_get_all_groups($course->id, $user->id);
164         if (!$gtrue) {
165             print_error("groupnotamember", '', "../course/view.php?id=$course->id");
166         }
167     }
171 /// We've established they can see the user's name at least, so what about the rest?
173 $PAGE->navbar->add($struser);
174 $PAGE->set_title("$course->fullname: $strpersonalprofile: $fullname");
175 $PAGE->set_heading($course->fullname);
176 echo $OUTPUT->header();
178 if (($course->id != SITEID) and ! has_capability('moodle/legacy:guest', $systemcontext, 0, false) ) {   // Need to have access to a course to see that info
179     if (!has_capability('moodle/course:view', $coursecontext, $user->id)) {
180         echo $OUTPUT->heading(get_string('notenrolled', '', $fullname));
181         echo $OUTPUT->footer();
182         die;
183     }
186 if ($user->deleted) {
187     echo $OUTPUT->heading(get_string('userdeleted'));
188     if (!has_capability('moodle/user:update', $coursecontext)) {
189         echo $OUTPUT->footer();
190         die;
191     }
194 /// OK, security out the way, now we are showing the user
196 add_to_log($course->id, "user", "view", "view.php?id=$user->id&course=$course->id", "$user->id");
198 if ($course->id != SITEID) {
199     $user->lastaccess = false;
200     if ($lastaccess = $DB->get_record('user_lastaccess', array('userid'=>$user->id, 'courseid'=>$course->id))) {
201         $user->lastaccess = $lastaccess->timeaccess;
202     }
206 /// Get the hidden field list
207 if (has_capability('moodle/user:viewhiddendetails', $coursecontext)) {
208     $hiddenfields = array();
209 } else {
210     $hiddenfields = array_flip(explode(',', $CFG->hiddenuserfields));
213 /// Print tabs at top
214 /// This same call is made in:
215 ///     /user/view.php
216 ///     /user/edit.php
217 ///     /course/user.php
219 $currenttab = 'profile';
220 $showroles = 1;
221 if (!$user->deleted) {
222     include('tabs.php');
225 if (is_mnet_remote_user($user)) {
226     $sql = "
227          SELECT DISTINCT h.id, h.name, h.wwwroot,
228                          a.name as application, a.display_name
229            FROM {mnet_host} h, {mnet_application} a
230           WHERE h.id = ? AND h.applicationid = a.id
231        ORDER BY a.display_name, h.name";
233     $remotehost = $DB->get_record_sql($sql, array($user->mnethostid));
235     echo '<p class="errorboxcontent">'.get_string('remoteappuser', $remotehost->application)." <br />\n";
236     if ($USER->id == $user->id) {
237         if ($remotehost->application =='moodle') {
238             echo "Remote {$remotehost->display_name}: <a href=\"{$remotehost->wwwroot}/user/edit.php\">{$remotehost->name}</a> ".get_string('editremoteprofile')." </p>\n";
239         } else {
240             echo "Remote {$remotehost->display_name}: <a href=\"{$remotehost->wwwroot}/\">{$remotehost->name}</a> ".get_string('gotoyourserver')." </p>\n";
241         }
242     } else {
243         echo "Remote {$remotehost->display_name}: <a href=\"{$remotehost->wwwroot}/\">{$remotehost->name}</a></p>\n";
244     }
247 echo '<table width="80%" class="userinfobox" summary="">';
248 echo '<tr>';
249 echo '<td class="side">';
250 $userpic = moodle_user_picture::make($user, $course->id);
251 $userpic->size = 100;
252 echo $OUTPUT->user_picture($userpic);
253 echo '</td><td class="content">';
255 // Print the description
257 if ($user->description && !isset($hiddenfields['description'])) {
258     $has_courseid = ($course->id != SITEID);
259     if (!$has_courseid && !empty($CFG->profilesforenrolledusersonly) && !$DB->record_exists('role_assignments', array('userid'=>$id))) {
260         echo get_string('profilenotshown', 'moodle').'<hr />';
261     } else {
262         echo format_text($user->description, FORMAT_MOODLE)."<hr />";
263     }
266 // Print all the little details in a list
268 echo '<table class="list">';
270 if (! isset($hiddenfields['country']) && $user->country) {
271     $countries = get_list_of_countries();
272     print_row(get_string('country') . ':', $countries[$user->country]);
275 if (! isset($hiddenfields['city']) && $user->city) {
276     print_row(get_string('city') . ':', $user->city);
279 if (has_capability('moodle/user:viewhiddendetails', $coursecontext)) {
280     if ($user->address) {
281         print_row(get_string("address").":", "$user->address");
282     }
283     if ($user->phone1) {
284         print_row(get_string("phone").":", "$user->phone1");
285     }
286     if ($user->phone2) {
287         print_row(get_string("phone2").":", "$user->phone2");
288     }
291 if ($user->maildisplay == 1 or
292    ($user->maildisplay == 2 and ($course->id != SITEID) and !isguest()) or
293    has_capability('moodle/course:useremail', $coursecontext)) {
295     $emailswitch = '';
297     if (has_capability('moodle/course:useremail', $coursecontext) or $currentuser) {   /// Can use the enable/disable email stuff
298         if (!empty($enable)) {     /// Recieved a parameter to enable the email address
299             $DB->set_field('user', 'emailstop', 0, array('id'=>$user->id));
300             $user->emailstop = 0;
301         }
302         if (!empty($disable)) {     /// Recieved a parameter to disable the email address
303             $DB->set_field('user', 'emailstop', 1, array('id'=>$user->id));
304             $user->emailstop = 1;
305         }
306     }
308     if (has_capability('moodle/course:useremail', $coursecontext)) {   /// Can use the enable/disable email stuff
309         if ($user->emailstop) {
310             $switchparam = 'enable';
311             $switchtitle = get_string('emaildisable');
312             $switchclick = get_string('emailenableclick');
313             $switchpix   = 't/emailno';
314         } else {
315             $switchparam = 'disable';
316             $switchtitle = get_string('emailenable');
317             $switchclick = get_string('emaildisableclick');
318             $switchpix   = 't/email';
319         }
320         $emailswitch = "&nbsp;<a title=\"$switchclick\" ".
321                        "href=\"view.php?id=$user->id&amp;course=$course->id&amp;$switchparam=1\">".
322                        "<img src=\"" . $OUTPUT->old_icon_url("$switchpix") . "\" alt=\"$switchclick\" /></a>";
324     } else if ($currentuser) {         /// Can only re-enable an email this way
325         if ($user->emailstop) {   // Include link that tells how to re-enable their email
326             $switchparam = 'enable';
327             $switchtitle = get_string('emaildisable');
328             $switchclick = get_string('emailenableclick');
330             $emailswitch = "&nbsp;(<a title=\"$switchclick\" ".
331                            "href=\"view.php?id=$user->id&amp;course=$course->id&amp;enable=1\">$switchtitle</a>)";
332         }
333     }
335     print_row(get_string("email").":", obfuscate_mailto($user->email, '', $user->emailstop)."$emailswitch");
338 if ($user->url && !isset($hiddenfields['webpage'])) {
339     $url = $user->url;
340     if (strpos($user->url, '://') === false) {
341         $url = 'http://'. $url;
342     }
343     print_row(get_string("webpage") .":", "<a href=\"$url\">$user->url</a>");
346 if ($user->icq && !isset($hiddenfields['icqnumber'])) {
347     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&amp;img=5\" alt=\"\" /></a>");
350 if ($user->skype && !isset($hiddenfields['skypeid'])) {
351     print_row(get_string('skypeid').':','<a href="callto:'.urlencode($user->skype).'">'.s($user->skype).
352         ' <img src="http://mystatus.skype.com/smallicon/'.urlencode($user->skype).'" alt="'.get_string('status').'" '.
353         ' /></a>');
355 if ($user->yahoo && !isset($hiddenfields['yahooid'])) {
356     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>");
358 if ($user->aim && !isset($hiddenfields['aimid'])) {
359     print_row(get_string('aimid').':', '<a href="aim:goim?screenname='.s($user->aim).'">'.s($user->aim).'</a>');
361 if ($user->msn && !isset($hiddenfields['msnid'])) {
362     print_row(get_string('msnid').':', s($user->msn));
365 /// Print the Custom User Fields
366 profile_display_fields($user->id);
369 if (!isset($hiddenfields['mycourses'])) {
370     if ($mycourses = get_my_courses($user->id, 'visible DESC,sortorder ASC', null, false, 21)) {
371         $shown=0;
372         $courselisting = '';
373         foreach ($mycourses as $mycourse) {
374             if ($mycourse->category) {
375                 if ($mycourse->id != $course->id){
376                     $class = '';
377                     if ($mycourse->visible == 0) {
378                         // get_my_courses will filter courses $USER cannot see
379                         // if we get one with visible 0 it just means it's hidden
380                         // ... but not from $USER
381                         $class = 'class="dimmed"';
382                     }
383                     $courselisting .= "<a href=\"{$CFG->wwwroot}/user/view.php?id={$user->id}&amp;course={$mycourse->id}\" $class >"
384                         . format_string($mycourse->fullname) . "</a>, ";
385                 }
386                 else {
387                     $courselisting .= format_string($mycourse->fullname) . ", ";
388                 }
389             }
390             $shown++;
391             if($shown==20) {
392                 $courselisting.= "...";
393                 break;
394             }
395         }
396         print_row(get_string('courses').':', rtrim($courselisting,', '));
397     }
399 if (!isset($hiddenfields['firstaccess'])) {
400     if ($user->firstaccess) {
401         $datestring = userdate($user->firstaccess)."&nbsp; (".format_time(time() - $user->firstaccess).")";
402     } else {
403         $datestring = get_string("never");
404     }
405     print_row(get_string("firstaccess").":", $datestring);
407 if (!isset($hiddenfields['lastaccess'])) {
408     if ($user->lastaccess) {
409         $datestring = userdate($user->lastaccess)."&nbsp; (".format_time(time() - $user->lastaccess).")";
410     } else {
411         $datestring = get_string("never");
412     }
413     print_row(get_string("lastaccess").":", $datestring);
415 /// printing roles
417 if ($rolestring = get_user_roles_in_context($id, $coursecontext)) {
418     print_row(get_string('roles').':', format_string($rolestring, false));
421 /// Printing groups
422 if (!isset($hiddenfields['groups'])) {
423     $isseparategroups = ($course->groupmode == SEPARATEGROUPS and !has_capability('moodle/site:accessallgroups', $coursecontext));
424     if (!$isseparategroups){
425         if ($usergroups = groups_get_all_groups($course->id, $user->id)){
426             $groupstr = '';
427             foreach ($usergroups as $group){
428                 $groupstr .= ' <a href="'.$CFG->wwwroot.'/user/index.php?id='.$course->id.'&amp;group='.$group->id.'">'.format_string($group->name).'</a>,';
429             }
430             print_row(get_string("group").":", rtrim($groupstr, ', '));
431         }
432     }
434 /// End of printing groups
436 /// Printing Interests
437     if( !empty($CFG->usetags)) {
438         if ( $interests = tag_get_tags_csv('user', $user->id) ) {
439         print_row(get_string('interests') .": ", $interests);
440     }
442 /// End of Printing Interests
444 echo "</table>";
446 echo "</td></tr></table>";
448 $userauth = get_auth_plugin($user->auth);
450 $passwordchangeurl = false;
451 if ($currentuser and $userauth->can_change_password() and !isguestuser() and has_capability('moodle/user:changeownpassword', $systemcontext)) {
452     if (!$passwordchangeurl = $userauth->change_password_url()) {
453         if (empty($CFG->loginhttps)) {
454             $passwordchangeurl = "$CFG->wwwroot/login/change_password.php";
455         } else {
456             $passwordchangeurl = str_replace('http:', 'https:', $CFG->wwwroot.'/login/change_password.php');
457         }
458     }
461 //  Print other functions
462 echo '<div class="buttons">';
464 if ($passwordchangeurl) {
465     $params = array('id'=>$course->id);
467     if (session_is_loggedinas()) {
468         $passwordchangeurl = ''; // do not use actual change password url - might contain sensitive data
469     } else {
470         $parts = explode('?', $passwordchangeurl);
471         $passwordchangeurl = reset($parts);
472         $after = next($parts);
473         preg_match_all('/([^&=]+)=([^&=]+)/', $after, $matches);
474         if (count($matches)) {
475             foreach($matches[0] as $key=>$match) {
476                 $params[$matches[1][$key]] = $matches[2][$key];
477             }
478         }
479     }
480     echo "<form action=\"$passwordchangeurl\" method=\"get\">";
481     echo "<div>";
482     foreach($params as $key=>$value) {
483         echo '<input type="hidden" name="'.$key.'" value="'.s($value).'" />';
484     }
485     if (session_is_loggedinas()) {
486         // changing of password when "Logged in as" is not allowed
487         echo "<input type=\"submit\" value=\"".get_string("changepassword")."\" disabled=\"disabled\" />";
488     } else {
489         echo "<input type=\"submit\" value=\"".get_string("changepassword")."\" />";
490     }
491     echo "</div>";
492     echo "</form>";
495 if ($course->id != SITEID && empty($course->metacourse)) {   // Mostly only useful at course level
497     $canunenrol = false;
499     if ($user->id == $USER->id) { // Myself
500         $canunenrol = has_capability('moodle/course:view', $coursecontext, NULL) &&              // Course participant
501                       has_capability('moodle/role:unassignself', $coursecontext, NULL, false) && // Can unassign myself
502                       get_user_roles($coursecontext, $user->id, false);                          // Must have role in course
504     } else if (has_capability('moodle/role:assign', $coursecontext, NULL)) { // I can assign roles
505         if ($roles = get_user_roles($coursecontext, $user->id, false)) {
506             $canunenrol = true;
507             foreach($roles as $role) {
508                 if (!user_can_assign($coursecontext, $role->roleid)) {
509                     $canunenrol = false; // I can not unassign all roles in this course :-(
510                     break;
511                 }
512             }
513         }
514     }
516     if ($canunenrol) {
517         echo '<form action="'.$CFG->wwwroot.'/course/unenrol.php" method="get">';
518         echo '<div>';
519         echo '<input type="hidden" name="id" value="'.$course->id.'" />';
520         echo '<input type="hidden" name="user" value="'.$user->id.'" />';
521         echo '<input type="submit" value="'.s(get_string('unenrolme', '', $course->shortname)).'" />';
522         echo '</div>';
523         echo '</form>';
524     }
527 if (!$user->deleted and $USER->id != $user->id  && !session_is_loggedinas() && has_capability('moodle/user:loginas', $coursecontext) &&
528                              ! has_capability('moodle/site:doanything', $coursecontext, $user->id, false)) {
529     echo '<form action="'.$CFG->wwwroot.'/course/loginas.php" method="get">';
530     echo '<div>';
531     echo '<input type="hidden" name="id" value="'.$course->id.'" />';
532     echo '<input type="hidden" name="user" value="'.$user->id.'" />';
533     echo '<input type="hidden" name="sesskey" value="'.sesskey().'" />';
534     echo '<input type="submit" value="'.get_string('loginas').'" />';
535     echo '</div>';
536     echo '</form>';
539 if (!$user->deleted and !empty($CFG->messaging) and !isguest() and has_capability('moodle/site:sendmessage', $systemcontext)) {
540     if (!empty($USER->id) and ($USER->id == $user->id)) {
541         if ($countmessages = $DB->count_records('message', array('useridto'=>$user->id))) {
542             $messagebuttonname = get_string("messages", "message")."($countmessages)";
543         } else {
544             $messagebuttonname = get_string("messages", "message");
545         }
546         echo "<form onclick=\"this.target='message'\" action=\"../message/index.php\" method=\"get\">";
547         echo "<div>";
548         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);\" />";
549         echo "</div>";
550         echo "</form>";
551     } else {
552         echo "<form onclick=\"this.target='message$user->id'\" action=\"../message/discussion.php\" method=\"get\">";
553         echo "<div>";
554         echo "<input type=\"hidden\" name=\"id\" value=\"$user->id\" />";
555         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);\" />";
556         echo "</div>";
557         echo "</form>";
558     }
560 // Authorize.net: User Payments
561 if ($course->enrol == 'authorize' || (empty($course->enrol) && $CFG->enrol == 'authorize')) {
562     echo "<form action=\"../enrol/authorize/index.php\" method=\"get\">";
563     echo "<div>";
564     echo "<input type=\"hidden\" name=\"course\" value=\"$course->id\" />";
565     echo "<input type=\"hidden\" name=\"user\" value=\"$user->id\" />";
566     echo "<input type=\"submit\" value=\"".get_string('payments')."\" />";
567     echo "</div>";
568     echo "</form>";
570 echo "</div>\n";
572 if ($CFG->debugdisplay && debugging('', DEBUG_DEVELOPER) && $USER->id == $user->id) {  // Show user object
573     echo '<hr />';
574     echo $OUTPUT->heading('DEBUG MODE:  User session variables');
575     print_object($USER);
578 echo $OUTPUT->footer();
580 /// Functions ///////
582 function print_row($left, $right) {
583     echo "\n<tr><td class=\"label c0\">$left</td><td class=\"info c1\">$right</td></tr>\n";
586 ?>