file AutoFormat.RemoveSpansWithoutAttributes.txt was added on branch MOODLE_19_STABLE...
[moodle.git] / user / view.php
blobf5d6a04d94cd4bf4609d2733225b2e04d9e976c9
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/role:assign', $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('remoteappuser', $remotehost->application)." <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 $has_courseid = ($course->id != SITEID);
224 if (!$has_courseid && !empty($CFG->profilesforenrolledusersonly) && !record_exists('role_assignments', 'userid', $id)) {
225 echo get_string('profilenotshown', 'moodle').'<hr />';
226 } else {
227 echo format_text($user->description, FORMAT_MOODLE)."<hr />";
231 // Print all the little details in a list
233 echo '<table class="list">';
235 if (! isset($hiddenfields['country']) && $user->country) {
236 $countries = get_list_of_countries();
237 print_row(get_string('country') . ':', $countries[$user->country]);
240 if (! isset($hiddenfields['city']) && $user->city) {
241 print_row(get_string('city') . ':', $user->city);
244 if (has_capability('moodle/user:viewhiddendetails', $coursecontext)) {
245 if ($user->address) {
246 print_row(get_string("address").":", "$user->address");
248 if ($user->phone1) {
249 print_row(get_string("phone").":", "$user->phone1");
251 if ($user->phone2) {
252 print_row(get_string("phone2").":", "$user->phone2");
256 if ($user->maildisplay == 1 or
257 ($user->maildisplay == 2 and ($course->id != SITEID) and !isguest()) or
258 has_capability('moodle/course:useremail', $coursecontext)) {
260 $emailswitch = '';
262 if (has_capability('moodle/course:useremail', $coursecontext) or $currentuser) { /// Can use the enable/disable email stuff
263 if (!empty($enable) and confirm_sesskey()) { /// Recieved a parameter to enable the email address
264 set_field('user', 'emailstop', 0, 'id', $user->id);
265 $user->emailstop = 0;
267 if (!empty($disable) and confirm_sesskey()) { /// Recieved a parameter to disable the email address
268 set_field('user', 'emailstop', 1, 'id', $user->id);
269 $user->emailstop = 1;
273 if (has_capability('moodle/course:useremail', $coursecontext)) { /// Can use the enable/disable email stuff
274 if ($user->emailstop) {
275 $switchparam = 'enable';
276 $switchtitle = get_string('emaildisable');
277 $switchclick = get_string('emailenableclick');
278 $switchpix = 'emailno.gif';
279 } else {
280 $switchparam = 'disable';
281 $switchtitle = get_string('emailenable');
282 $switchclick = get_string('emaildisableclick');
283 $switchpix = 'email.gif';
285 $emailswitch = "&nbsp;<a title=\"$switchclick\" ".
286 "href=\"view.php?id=$user->id&amp;course=$course->id&amp;$switchparam=1&amp;sesskey=".sesskey()."\">".
287 "<img src=\"$CFG->pixpath/t/$switchpix\" alt=\"$switchclick\" /></a>";
289 } else if ($currentuser) { /// Can only re-enable an email this way
290 if ($user->emailstop) { // Include link that tells how to re-enable their email
291 $switchparam = 'enable';
292 $switchtitle = get_string('emaildisable');
293 $switchclick = get_string('emailenableclick');
295 $emailswitch = "&nbsp;(<a title=\"$switchclick\" ".
296 "href=\"view.php?id=$user->id&amp;course=$course->id&amp;enable=1&amp;sesskey=".sesskey()."\">$switchtitle</a>)";
300 print_row(get_string("email").":", obfuscate_mailto($user->email, '', $user->emailstop)."$emailswitch");
303 if ($user->url && !isset($hiddenfields['webpage'])) {
304 $url = $user->url;
305 if (strpos($user->url, '://') === false) {
306 $url = 'http://'. $url;
308 print_row(get_string("webpage") .":", "<a href=\"$url\">$user->url</a>");
311 if ($user->icq && !isset($hiddenfields['icqnumber'])) {
312 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>");
315 if ($user->skype && !isset($hiddenfields['skypeid'])) {
316 print_row(get_string('skypeid').':','<a href="callto:'.urlencode($user->skype).'">'.s($user->skype).
317 ' <img src="http://mystatus.skype.com/smallicon/'.urlencode($user->skype).'" alt="'.get_string('status').'" '.
318 ' /></a>');
320 if ($user->yahoo && !isset($hiddenfields['yahooid'])) {
321 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>");
323 if ($user->aim && !isset($hiddenfields['aimid'])) {
324 print_row(get_string('aimid').':', '<a href="aim:goim?screenname='.s($user->aim).'">'.s($user->aim).'</a>');
326 if ($user->msn && !isset($hiddenfields['msnid'])) {
327 print_row(get_string('msnid').':', s($user->msn));
330 /// Print the Custom User Fields
331 profile_display_fields($user->id);
334 if (!isset($hiddenfields['mycourses'])) {
335 if ($mycourses = get_my_courses($user->id, 'visible DESC,sortorder ASC', null, false, 21)) {
336 $shown=0;
337 $courselisting = '';
338 foreach ($mycourses as $mycourse) {
339 if ($mycourse->category) {
340 if ($mycourse->id != $course->id){
341 $class = '';
342 if ($mycourse->visible == 0) {
343 // get_my_courses will filter courses $USER cannot see
344 // if we get one with visible 0 it just means it's hidden
345 // ... but not from $USER
346 $class = 'class="dimmed"';
348 $courselisting .= "<a href=\"{$CFG->wwwroot}/user/view.php?id={$user->id}&amp;course={$mycourse->id}\" $class >"
349 . format_string($mycourse->fullname) . "</a>, ";
351 else {
352 $courselisting .= format_string($mycourse->fullname) . ", ";
355 $shown++;
356 if($shown==20) {
357 $courselisting.= "...";
358 break;
361 print_row(get_string('courses').':', rtrim($courselisting,', '));
364 if (!isset($hiddenfields['firstaccess'])) {
365 if ($user->firstaccess) {
366 $datestring = userdate($user->firstaccess)."&nbsp; (".format_time(time() - $user->firstaccess).")";
367 } else {
368 $datestring = get_string("never");
370 print_row(get_string("firstaccess").":", $datestring);
372 if (!isset($hiddenfields['lastaccess'])) {
373 if ($user->lastaccess) {
374 $datestring = userdate($user->lastaccess)."&nbsp; (".format_time(time() - $user->lastaccess).")";
375 } else {
376 $datestring = get_string("never");
378 print_row(get_string("lastaccess").":", $datestring);
380 /// printing roles
382 if ($rolestring = get_user_roles_in_context($id, $coursecontext)) {
383 print_row(get_string('roles').':', format_string($rolestring, false));
386 /// Printing groups
387 if (!isset($hiddenfields['groups'])) {
388 $isseparategroups = ($course->groupmode == SEPARATEGROUPS and !has_capability('moodle/site:accessallgroups', $coursecontext));
389 if (!$isseparategroups){
390 if ($usergroups = groups_get_all_groups($course->id, $user->id)){
391 $groupstr = '';
392 foreach ($usergroups as $group){
393 $groupstr .= ' <a href="'.$CFG->wwwroot.'/user/index.php?id='.$course->id.'&amp;group='.$group->id.'">'.format_string($group->name).'</a>,';
395 print_row(get_string("group").":", rtrim($groupstr, ', '));
399 /// End of printing groups
401 /// Printing Interests
402 if( !empty($CFG->usetags)) {
403 if ( $interests = tag_get_tags_csv('user', $user->id) ) {
404 print_row(get_string('interests') .": ", $interests);
407 /// End of Printing Interests
409 echo "</table>";
411 echo "</td></tr></table>";
413 $userauth = get_auth_plugin($user->auth);
415 $passwordchangeurl = false;
416 if ($currentuser and $userauth->can_change_password() and !isguestuser() and has_capability('moodle/user:changeownpassword', $systemcontext)) {
417 if (!$passwordchangeurl = $userauth->change_password_url()) {
418 if (empty($CFG->loginhttps)) {
419 $passwordchangeurl = "$CFG->wwwroot/login/change_password.php";
420 } else {
421 $passwordchangeurl = str_replace('http:', 'https:', $CFG->wwwroot.'/login/change_password.php');
426 // Print other functions
427 echo '<div class="buttons">';
429 if ($passwordchangeurl) {
430 $params = array('id'=>$course->id);
432 if (!empty($USER->realuser)) {
433 $passwordchangeurl = ''; // do not use actual change password url - might contain sensitive data
434 } else {
435 $parts = explode('?', $passwordchangeurl);
436 $passwordchangeurl = reset($parts);
437 $after = next($parts);
438 preg_match_all('/([^&=]+)=([^&=]+)/', $after, $matches);
439 if (count($matches)) {
440 foreach($matches[0] as $key=>$match) {
441 $params[$matches[1][$key]] = $matches[2][$key];
445 echo "<form action=\"$passwordchangeurl\" method=\"get\">";
446 echo "<div>";
447 foreach($params as $key=>$value) {
448 echo '<input type="hidden" name="'.$key.'" value="'.s($value).'" />';
450 if (!empty($USER->realuser)) {
451 // changing of password when "Logged in as" is not allowed
452 echo "<input type=\"submit\" value=\"".get_string("changepassword")."\" disabled=\"disabled\" />";
453 } else {
454 echo "<input type=\"submit\" value=\"".get_string("changepassword")."\" />";
456 echo "</div>";
457 echo "</form>";
460 if ($course->id != SITEID && empty($course->metacourse)) { // Mostly only useful at course level
462 $canunenrol = false;
464 if ($user->id == $USER->id) { // Myself
465 $canunenrol = has_capability('moodle/course:view', $coursecontext, NULL) && // Course participant
466 has_capability('moodle/role:unassignself', $coursecontext, NULL, false) && // Can unassign myself
467 get_user_roles($coursecontext, $user->id, false); // Must have role in course
469 } else if (has_capability('moodle/role:assign', $coursecontext, NULL)) { // I can assign roles
470 if ($roles = get_user_roles($coursecontext, $user->id, false)) {
471 $canunenrol = true;
472 foreach($roles as $role) {
473 if (!user_can_assign($coursecontext, $role->roleid)) {
474 $canunenrol = false; // I can not unassign all roles in this course :-(
475 break;
481 if ($canunenrol) {
482 echo '<form action="'.$CFG->wwwroot.'/course/unenrol.php" method="get">';
483 echo '<div>';
484 echo '<input type="hidden" name="id" value="'.$course->id.'" />';
485 echo '<input type="hidden" name="user" value="'.$user->id.'" />';
486 echo '<input type="submit" value="'.s(get_string('unenrolme', '', $course->shortname)).'" />';
487 echo '</div>';
488 echo '</form>';
492 if (!$user->deleted and $USER->id != $user->id && empty($USER->realuser) && has_capability('moodle/user:loginas', $coursecontext) &&
493 ! has_capability('moodle/site:doanything', $coursecontext, $user->id, false)) {
494 echo '<form action="'.$CFG->wwwroot.'/course/loginas.php" method="get">';
495 echo '<div>';
496 echo '<input type="hidden" name="id" value="'.$course->id.'" />';
497 echo '<input type="hidden" name="user" value="'.$user->id.'" />';
498 echo '<input type="hidden" name="sesskey" value="'.sesskey().'" />';
499 echo '<input type="submit" value="'.get_string('loginas').'" />';
500 echo '</div>';
501 echo '</form>';
504 if (!$user->deleted and !empty($CFG->messaging) and !isguest() and has_capability('moodle/site:sendmessage', get_context_instance(CONTEXT_SYSTEM))) {
505 if (!empty($USER->id) and ($USER->id == $user->id)) {
506 if ($countmessages = count_records('message', 'useridto', $user->id)) {
507 $messagebuttonname = get_string("messages", "message")."($countmessages)";
508 } else {
509 $messagebuttonname = get_string("messages", "message");
511 echo "<form onclick=\"this.target='message'\" action=\"../message/index.php\" method=\"get\">";
512 echo "<div>";
513 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);\" />";
514 echo "</div>";
515 echo "</form>";
516 } else {
517 echo "<form onclick=\"this.target='message$user->id'\" action=\"../message/discussion.php\" method=\"get\">";
518 echo "<div>";
519 echo "<input type=\"hidden\" name=\"id\" value=\"$user->id\" />";
520 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);\" />";
521 echo "</div>";
522 echo "</form>";
525 // Authorize.net: User Payments
526 if ($course->enrol == 'authorize' || (empty($course->enrol) && $CFG->enrol == 'authorize')) {
527 echo "<form action=\"../enrol/authorize/index.php\" method=\"get\">";
528 echo "<div>";
529 echo "<input type=\"hidden\" name=\"course\" value=\"$course->id\" />";
530 echo "<input type=\"hidden\" name=\"user\" value=\"$user->id\" />";
531 echo "<input type=\"submit\" value=\"".get_string('payments')."\" />";
532 echo "</div>";
533 echo "</form>";
535 echo "</div>\n";
537 if ($CFG->debugdisplay && debugging('', DEBUG_DEVELOPER) && $USER->id == $user->id) { // Show user object
538 echo '<hr />';
539 print_heading('DEBUG MODE: User session variables');
540 print_object($USER);
543 print_footer($course);
545 /// Functions ///////
547 function print_row($left, $right) {
548 echo "\n<tr><td class=\"label c0\">$left</td><td class=\"info c1\">$right</td></tr>\n";