Merge commit 'catalyst/MOODLE_19_STABLE' into mdl19-linuxchix
[moodle-linuxchix.git] / user / view.php
blob0d998f146896ad287faf1e10ff55ef326bd9c52a
1 <?PHP // $Id$
3 // Display profile for a particular user
5 require_once("../config.php");
6 require_once($CFG->dirroot.'/user/profile/lib.php');
7 require_once($CFG->dirroot.'/tag/lib.php');
9 $id = optional_param('id', 0, PARAM_INT); // user id
10 $course = optional_param('course', SITEID, PARAM_INT); // course id (defaults to Site)
11 $enable = optional_param('enable', ''); // enable email
12 $disable = optional_param('disable', ''); // disable email
14 if (empty($id)) { // See your own profile by default
15 require_login();
16 $id = $USER->id;
19 if (! $user = get_record("user", "id", $id) ) {
20 error("No such user in this course");
23 if (! $course = get_record("course", "id", $course) ) {
24 error("No such course id");
27 /// Make sure the current user is allowed to see this user
29 if (empty($USER->id)) {
30 $currentuser = false;
31 } else {
32 $currentuser = ($user->id == $USER->id);
35 if ($course->id == SITEID) {
36 $coursecontext = get_context_instance(CONTEXT_SYSTEM); // SYSTEM context
37 } else {
38 $coursecontext = get_context_instance(CONTEXT_COURSE, $course->id); // Course context
40 $usercontext = get_context_instance(CONTEXT_USER, $user->id); // User context
41 $systemcontext = get_context_instance(CONTEXT_SYSTEM); // SYSTEM context
43 if (!empty($CFG->forcelogin) || $course->id != SITEID) {
44 // do not force parents to enrol
45 if (!get_record('role_assignments', 'userid', $USER->id, 'contextid', $usercontext->id)) {
46 require_login($course->id);
50 if (!empty($CFG->forceloginforprofiles)) {
51 require_login();
52 if (isguest()) {
53 redirect("$CFG->wwwroot/login/index.php");
57 $strpersonalprofile = get_string('personalprofile');
58 $strparticipants = get_string("participants");
59 $struser = get_string("user");
61 $fullname = fullname($user, has_capability('moodle/site:viewfullnames', $coursecontext));
63 $navlinks = array();
64 if (has_capability('moodle/course:viewparticipants', $coursecontext) || has_capability('moodle/site:viewparticipants', $systemcontext)) {
65 $navlinks[] = array('name' => $strparticipants, 'link' => "index.php?id=$course->id", 'type' => 'misc');
68 /// If the user being shown is not ourselves, then make sure we are allowed to see them!
70 if (!$currentuser) {
71 if ($course->id == SITEID) { // Reduce possibility of "browsing" userbase at site level
72 if ($CFG->forceloginforprofiles and !isteacherinanycourse()
73 and !isteacherinanycourse($user->id)
74 and !has_capability('moodle/user:viewdetails', $usercontext)) { // Teachers can browse and be browsed at site level. If not forceloginforprofiles, allow access (bug #4366)
76 $navlinks[] = array('name' => $struser, 'link' => null, 'type' => 'misc');
77 $navigation = build_navigation($navlinks);
79 print_header("$strpersonalprofile: ", "$strpersonalprofile: ", $navigation, "", "", true, "&nbsp;", navmenu($course));
80 print_heading(get_string('usernotavailable', 'error'));
81 print_footer($course);
82 exit;
84 } else { // Normal course
85 // check capabilities
86 if (!has_capability('moodle/user:viewdetails', $coursecontext) &&
87 !has_capability('moodle/user:viewdetails', $usercontext)) {
88 print_error('cannotviewprofile');
91 if (!has_capability('moodle/course:view', $coursecontext, $user->id, false)) {
92 if (has_capability('moodle/course:view', $coursecontext)) {
93 $navlinks[] = array('name' => $fullname, 'link' => null, 'type' => 'misc');
94 $navigation = build_navigation($navlinks);
95 print_header("$strpersonalprofile: ", "$strpersonalprofile: ", $navigation, "", "", true, "&nbsp;", navmenu($course));
96 print_heading(get_string('notenrolled', '', $fullname));
97 } else {
98 $navlinks[] = array('name' => $struser, 'link' => null, 'type' => 'misc');
99 $navigation = build_navigation($navlinks);
100 print_header("$strpersonalprofile: ", "$strpersonalprofile: ", $navigation, "", "", true, "&nbsp;", navmenu($course));
101 print_heading(get_string('notenrolledprofile'));
103 print_continue($_SERVER['HTTP_REFERER']);
104 print_footer($course);
105 exit;
110 // If groups are in use, make sure we can see that group
111 if (groups_get_course_groupmode($course) == SEPARATEGROUPS and !has_capability('moodle/site:accessallgroups', $coursecontext)) {
112 require_login();
114 ///this is changed because of mygroupid
115 $gtrue = (bool)groups_get_all_groups($course->id, $user->id);
116 if (!$gtrue) {
117 $navigation = build_navigation($navlinks);
118 print_header("$strpersonalprofile: ", "$strpersonalprofile: ", $navigation, "", "", true, "&nbsp;", navmenu($course));
119 print_error("groupnotamember", '', "../course/view.php?id=$course->id");
125 /// We've established they can see the user's name at least, so what about the rest?
127 $navlinks[] = array('name' => $fullname, 'link' => null, 'type' => 'misc');
129 $navigation = build_navigation($navlinks);
131 print_header("$course->fullname: $strpersonalprofile: $fullname", $course->fullname,
132 $navigation, "", "", true, "&nbsp;", navmenu($course));
135 if (($course->id != SITEID) and ! isguest() ) { // Need to have access to a course to see that info
136 if (!has_capability('moodle/course:view', $coursecontext, $user->id)) {
137 print_heading(get_string('notenrolled', '', $fullname));
138 print_footer($course);
139 die;
143 if ($user->deleted) {
144 print_heading(get_string('userdeleted'));
145 if (!has_capability('moodle/user:update', $coursecontext)) {
146 print_footer($course);
147 die;
151 /// OK, security out the way, now we are showing the user
153 add_to_log($course->id, "user", "view", "view.php?id=$user->id&course=$course->id", "$user->id");
155 if ($course->id != SITEID) {
156 $user->lastaccess = false;
157 if ($lastaccess = get_record('user_lastaccess', 'userid', $user->id, 'courseid', $course->id)) {
158 $user->lastaccess = $lastaccess->timeaccess;
163 /// Get the hidden field list
164 if (has_capability('moodle/user:viewhiddendetails', $coursecontext)) {
165 $hiddenfields = array();
166 } else {
167 $hiddenfields = array_flip(explode(',', $CFG->hiddenuserfields));
170 /// Print tabs at top
171 /// This same call is made in:
172 /// /user/view.php
173 /// /user/edit.php
174 /// /course/user.php
176 $currenttab = 'profile';
177 $showroles = 1;
178 if (!$user->deleted) {
179 include('tabs.php');
182 if (is_mnet_remote_user($user)) {
183 $sql = "
184 SELECT DISTINCT
185 h.id,
186 h.name,
187 h.wwwroot,
188 a.name as application,
189 a.display_name
190 FROM
191 {$CFG->prefix}mnet_host h,
192 {$CFG->prefix}mnet_application a
193 WHERE
194 h.id = '{$user->mnethostid}' AND
195 h.applicationid = a.id
196 ORDER BY
197 a.display_name,
198 h.name";
200 $remotehost = get_record_sql($sql);
202 echo '<p class="errorboxcontent">'.get_string('remote'.$remotehost->application.'user')." <br />\n";
203 if ($USER->id == $user->id) {
204 if ($remotehost->application =='moodle') {
205 echo "Remote {$remotehost->display_name}: <a href=\"{$remotehost->wwwroot}/user/edit.php\">{$remotehost->name}</a> ".get_string('editremoteprofile')." </p>\n";
206 } else {
207 echo "Remote {$remotehost->display_name}: <a href=\"{$remotehost->wwwroot}/\">{$remotehost->name}</a> ".get_string('gotoyourserver')." </p>\n";
209 } else {
210 echo "Remote {$remotehost->display_name}: <a href=\"{$remotehost->wwwroot}/\">{$remotehost->name}</a></p>\n";
214 echo '<table width="80%" class="userinfobox" summary="">';
215 echo '<tr>';
216 echo '<td class="side">';
217 print_user_picture($user, $course->id, $user->picture, true, false, false);
218 echo '</td><td class="content">';
220 // Print the description
222 if ($user->description && !isset($hiddenfields['description'])) {
223 echo format_text($user->description, FORMAT_MOODLE)."<hr />";
226 // Print all the little details in a list
228 echo '<table class="list">';
230 if (! isset($hiddenfields['country']) && $user->country) {
231 $countries = get_list_of_countries();
232 print_row(get_string('country') . ':', $countries[$user->country]);
235 if (! isset($hiddenfields['city']) && $user->city) {
236 print_row(get_string('city') . ':', $user->city);
239 if (has_capability('moodle/user:viewhiddendetails', $coursecontext)) {
240 if ($user->address) {
241 print_row(get_string("address").":", "$user->address");
243 if ($user->phone1) {
244 print_row(get_string("phone").":", "$user->phone1");
246 if ($user->phone2) {
247 print_row(get_string("phone2").":", "$user->phone2");
251 if ($user->maildisplay == 1 or
252 ($user->maildisplay == 2 and ($course->id != SITEID) and !isguest()) or
253 has_capability('moodle/course:useremail', $coursecontext)) {
255 $emailswitch = '';
257 if (has_capability('moodle/course:useremail', $coursecontext) or $currentuser) { /// Can use the enable/disable email stuff
258 if (!empty($enable)) { /// Recieved a parameter to enable the email address
259 set_field('user', 'emailstop', 0, 'id', $user->id);
260 $user->emailstop = 0;
262 if (!empty($disable)) { /// Recieved a parameter to disable the email address
263 set_field('user', 'emailstop', 1, 'id', $user->id);
264 $user->emailstop = 1;
268 if (has_capability('moodle/course:useremail', $coursecontext)) { /// Can use the enable/disable email stuff
269 if ($user->emailstop) {
270 $switchparam = 'enable';
271 $switchtitle = get_string('emaildisable');
272 $switchclick = get_string('emailenableclick');
273 $switchpix = 'emailno.gif';
274 } else {
275 $switchparam = 'disable';
276 $switchtitle = get_string('emailenable');
277 $switchclick = get_string('emaildisableclick');
278 $switchpix = 'email.gif';
280 $emailswitch = "&nbsp;<a title=\"$switchclick\" ".
281 "href=\"view.php?id=$user->id&amp;course=$course->id&amp;$switchparam=1\">".
282 "<img src=\"$CFG->pixpath/t/$switchpix\" alt=\"$switchclick\" /></a>";
284 } else if ($currentuser) { /// Can only re-enable an email this way
285 if ($user->emailstop) { // Include link that tells how to re-enable their email
286 $switchparam = 'enable';
287 $switchtitle = get_string('emaildisable');
288 $switchclick = get_string('emailenableclick');
290 $emailswitch = "&nbsp;(<a title=\"$switchclick\" ".
291 "href=\"view.php?id=$user->id&amp;course=$course->id&amp;enable=1\">$switchtitle</a>)";
295 print_row(get_string("email").":", obfuscate_mailto($user->email, '', $user->emailstop)."$emailswitch");
298 if ($user->url && !isset($hiddenfields['webpage'])) {
299 $url = $user->url;
300 if (strpos($user->url, '://') === false) {
301 $url = 'http://'. $url;
303 print_row(get_string("webpage") .":", "<a href=\"$url\">$user->url</a>");
306 if ($user->icq && !isset($hiddenfields['icqnumber'])) {
307 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>");
310 if ($user->skype && !isset($hiddenfields['skypeid'])) {
311 print_row(get_string('skypeid').':','<a href="callto:'.urlencode($user->skype).'">'.s($user->skype).
312 ' <img src="http://mystatus.skype.com/smallicon/'.urlencode($user->skype).'" alt="'.get_string('status').'" '.
313 ' /></a>');
315 if ($user->yahoo && !isset($hiddenfields['yahooid'])) {
316 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>");
318 if ($user->aim && !isset($hiddenfields['aimid'])) {
319 print_row(get_string('aimid').':', '<a href="aim:goim?screenname='.s($user->aim).'">'.s($user->aim).'</a>');
321 if ($user->msn && !isset($hiddenfields['msnid'])) {
322 print_row(get_string('msnid').':', s($user->msn));
325 /// Print the Custom User Fields
326 profile_display_fields($user->id);
329 if ($mycourses = get_my_courses($user->id, null, null, false, 21)) {
330 $shown=0;
331 $courselisting = '';
332 foreach ($mycourses as $mycourse) {
333 if ($mycourse->category) {
334 if ($mycourse->id != $course->id){
335 $class = '';
336 if ($mycourse->visible == 0) {
337 // get_my_courses will filter courses $USER cannot see
338 // if we get one with visible 0 it just means it's hidden
339 // ... but not from $USER
340 $class = 'class="dimmed"';
342 $courselisting .= "<a href=\"{$CFG->wwwroot}/user/view.php?id={$user->id}&amp;course={$mycourse->id}\" $class >"
343 . format_string($mycourse->fullname) . "</a>, ";
345 else {
346 $courselisting .= format_string($mycourse->fullname) . ", ";
349 $shown++;
350 if($shown==20) {
351 $courselisting.= "...";
352 break;
355 print_row(get_string('courses').':', rtrim($courselisting,', '));
358 if (!isset($hiddenfields['lastaccess'])) {
359 if ($user->lastaccess) {
360 $datestring = userdate($user->lastaccess)."&nbsp; (".format_time(time() - $user->lastaccess).")";
361 } else {
362 $datestring = get_string("never");
364 print_row(get_string("lastaccess").":", $datestring);
366 /// printing roles
368 if ($rolestring = get_user_roles_in_context($id, $coursecontext)) {
369 print_row(get_string('roles').':', format_string($rolestring, false));
372 /// Printing groups
373 $isseparategroups = ($course->groupmode == SEPARATEGROUPS and $course->groupmodeforce and
374 !has_capability('moodle/site:accessallgroups', $coursecontext));
375 if (!$isseparategroups){
376 if ($usergroups = groups_get_all_groups($course->id, $user->id)){
377 $groupstr = '';
378 foreach ($usergroups as $group){
379 $groupstr .= ' <a href="'.$CFG->wwwroot.'/user/index.php?id='.$course->id.'&amp;group='.$group->id.'">'.format_string($group->name).'</a>,';
381 print_row(get_string("group").":", rtrim($groupstr, ', '));
384 /// End of printing groups
386 /// Printing Interests
387 if( !empty($CFG->usetags)) {
388 if ( $interests = tag_get_tags_csv('user', $user->id) ) {
389 print_row(get_string('interests') .": ", $interests);
392 /// End of Printing Interests
394 echo "</table>";
396 echo "</td></tr></table>";
398 $userauth = get_auth_plugin($user->auth);
400 $passwordchangeurl = false;
401 if ($currentuser and $userauth->can_change_password() and !isguestuser() and has_capability('moodle/user:changeownpassword', $systemcontext)) {
402 if (!$passwordchangeurl = $userauth->change_password_url()) {
403 if (empty($CFG->loginhttps)) {
404 $passwordchangeurl = "$CFG->wwwroot/login/change_password.php";
405 } else {
406 $passwordchangeurl = str_replace('http:', 'https:', $CFG->wwwroot.'/login/change_password.php');
411 // Print other functions
412 echo '<div class="buttons">';
414 if ($passwordchangeurl) {
415 $params = array('id'=>$course->id);
417 if (!empty($USER->realuser)) {
418 $passwordchangeurl = ''; // do not use actual change password url - might contain sensitive data
419 } else {
420 $parts = explode('?', $passwordchangeurl);
421 $passwordchangeurl = reset($parts);
422 $after = next($parts);
423 preg_match_all('/([^&=]+)=([^&=]+)/', $after, $matches);
424 if (count($matches)) {
425 foreach($matches[0] as $key=>$match) {
426 $params[$matches[1][$key]] = $matches[2][$key];
430 echo "<form action=\"$passwordchangeurl\" method=\"get\">";
431 echo "<div>";
432 foreach($params as $key=>$value) {
433 echo '<input type="hidden" name="'.$key.'" value="'.s($value).'" />';
435 if (!empty($USER->realuser)) {
436 // changing of password when "Logged in as" is not allowed
437 echo "<input type=\"submit\" value=\"".get_string("changepassword")."\" disabled=\"disabled\" />";
438 } else {
439 echo "<input type=\"submit\" value=\"".get_string("changepassword")."\" />";
441 echo "</div>";
442 echo "</form>";
445 if ($course->id != SITEID && empty($course->metacourse)) { // Mostly only useful at course level
447 $canunenrol = false;
449 if ($user->id == $USER->id) { // Myself
450 $canunenrol = has_capability('moodle/course:view', $coursecontext, NULL) && // Course participant
451 has_capability('moodle/role:unassignself', $coursecontext, NULL, false) && // Can unassign myself
452 get_user_roles($coursecontext, $user->id, false); // Must have role in course
454 } else if (has_capability('moodle/role:assign', $coursecontext, NULL)) { // I can assign roles
455 if ($roles = get_user_roles($coursecontext, $user->id, false)) {
456 $canunenrol = true;
457 foreach($roles as $role) {
458 if (!user_can_assign($coursecontext, $role->roleid)) {
459 $canunenrol = false; // I can not unassign all roles in this course :-(
460 break;
466 if ($canunenrol) {
467 echo '<form action="'.$CFG->wwwroot.'/course/unenrol.php" method="get">';
468 echo '<div>';
469 echo '<input type="hidden" name="id" value="'.$course->id.'" />';
470 echo '<input type="hidden" name="user" value="'.$user->id.'" />';
471 echo '<input type="submit" value="'.s(get_string('unenrolme', '', $course->shortname)).'" />';
472 echo '</div>';
473 echo '</form>';
477 if (!$user->deleted and $USER->id != $user->id && empty($USER->realuser) && has_capability('moodle/user:loginas', $coursecontext) &&
478 ! has_capability('moodle/site:doanything', $coursecontext, $user->id, false)) {
479 echo '<form action="'.$CFG->wwwroot.'/course/loginas.php" method="get">';
480 echo '<div>';
481 echo '<input type="hidden" name="id" value="'.$course->id.'" />';
482 echo '<input type="hidden" name="user" value="'.$user->id.'" />';
483 echo '<input type="hidden" name="sesskey" value="'.sesskey().'" />';
484 echo '<input type="submit" value="'.get_string('loginas').'" />';
485 echo '</div>';
486 echo '</form>';
489 if (!$user->deleted and !empty($CFG->messaging) and !isguest() and has_capability('moodle/site:sendmessage', get_context_instance(CONTEXT_SYSTEM))) {
490 if (!empty($USER->id) and ($USER->id == $user->id)) {
491 if ($countmessages = count_records('message', 'useridto', $user->id)) {
492 $messagebuttonname = get_string("messages", "message")."($countmessages)";
493 } else {
494 $messagebuttonname = get_string("messages", "message");
496 echo "<form onclick=\"this.target='message'\" action=\"../message/index.php\" method=\"get\">";
497 echo "<div>";
498 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);\" />";
499 echo "</div>";
500 echo "</form>";
501 } else {
502 echo "<form onclick=\"this.target='message$user->id'\" action=\"../message/discussion.php\" method=\"get\">";
503 echo "<div>";
504 echo "<input type=\"hidden\" name=\"id\" value=\"$user->id\" />";
505 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);\" />";
506 echo "</div>";
507 echo "</form>";
510 // Authorize.net: User Payments
511 if ($course->enrol == 'authorize' || (empty($course->enrol) && $CFG->enrol == 'authorize')) {
512 echo "<form action=\"../enrol/authorize/index.php\" method=\"get\">";
513 echo "<div>";
514 echo "<input type=\"hidden\" name=\"course\" value=\"$course->id\" />";
515 echo "<input type=\"hidden\" name=\"user\" value=\"$user->id\" />";
516 echo "<input type=\"submit\" value=\"".get_string('payments')."\" />";
517 echo "</div>";
518 echo "</form>";
520 echo "</div>\n";
522 if ($CFG->debugdisplay && debugging('', DEBUG_DEVELOPER) && $USER->id == $user->id) { // Show user object
523 echo '<hr />';
524 print_heading('DEBUG MODE: User session variables');
525 print_object($USER);
528 print_footer($course);
530 /// Functions ///////
532 function print_row($left, $right) {
533 echo "\n<tr><td class=\"label c0\">$left</td><td class=\"info c1\">$right</td></tr>\n";