course/index: Bring back Petr's MDL-9647 simplify dealing with headers/footers in...
[moodle-pu.git] / user / view.php
blob0dc5e48529c0783ede73af5158b63b0168e8a90f
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 $navlinks[] = array('name' => $strparticipants, 'link' => "index.php?id=$course->id", 'type' => 'misc');
66 /// If the user being shown is not ourselves, then make sure we are allowed to see them!
68 if (!$currentuser) {
69 if ($course->id == SITEID) { // Reduce possibility of "browsing" userbase at site level
70 if ($CFG->forceloginforprofiles and !isteacherinanycourse()
71 and !isteacherinanycourse($user->id)
72 and !has_capability('moodle/user:viewdetails', $usercontext)) { // Teachers can browse and be browsed at site level. If not forceloginforprofiles, allow access (bug #4366)
74 $navlinks[] = array('name' => $struser, 'link' => null, 'type' => 'misc');
75 $navigation = build_navigation($navlinks);
77 print_header("$strpersonalprofile: ", "$strpersonalprofile: ", $navigation, "", "", true, "&nbsp;", navmenu($course));
78 print_heading(get_string('usernotavailable', 'error'));
79 print_footer($course);
80 exit;
82 } else { // Normal course
83 // check capabilities
84 if (!has_capability('moodle/user:viewdetails', $coursecontext) &&
85 !has_capability('moodle/user:viewdetails', $usercontext)) {
86 print_error('cannotviewprofile');
89 if (!has_capability('moodle/course:view', $coursecontext, $user->id, false)) {
90 if (has_capability('moodle/course:view', $coursecontext)) {
91 $navlinks[] = array('name' => $fullname, 'link' => null, 'type' => 'misc');
92 $navigation = build_navigation($navlinks);
93 print_header("$strpersonalprofile: ", "$strpersonalprofile: ", $navigation, "", "", true, "&nbsp;", navmenu($course));
94 print_heading(get_string('notenrolled', '', $fullname));
95 } else {
96 $navlinks[] = array('name' => $struser, 'link' => null, 'type' => 'misc');
97 $navigation = build_navigation($navlinks);
98 print_header("$strpersonalprofile: ", "$strpersonalprofile: ", $navigation, "", "", true, "&nbsp;", navmenu($course));
99 print_heading(get_string('notenrolledprofile'));
101 print_continue($_SERVER['HTTP_REFERER']);
102 print_footer($course);
103 exit;
108 // If groups are in use, make sure we can see that group
109 if (groups_get_course_groupmode($course) == SEPARATEGROUPS and !has_capability('moodle/site:accessallgroups', $coursecontext)) {
110 require_login();
112 ///this is changed because of mygroupid
113 $gtrue = (bool)groups_get_all_groups($course->id, $user->id);
114 if (!$gtrue) {
115 $navigation = build_navigation($navlinks);
116 print_header("$strpersonalprofile: ", "$strpersonalprofile: ", $navigation, "", "", true, "&nbsp;", navmenu($course));
117 error(get_string("groupnotamember"), "../course/view.php?id=$course->id");
123 /// We've established they can see the user's name at least, so what about the rest?
125 $navlinks[] = array('name' => $fullname, 'link' => null, 'type' => 'misc');
127 $navigation = build_navigation($navlinks);
129 print_header("$course->fullname: $strpersonalprofile: $fullname", $course->fullname,
130 $navigation, "", "", true, "&nbsp;", navmenu($course));
133 if (($course->id != SITEID) and ! isguest() ) { // Need to have access to a course to see that info
134 if (!has_capability('moodle/course:view', $coursecontext, $user->id)) {
135 print_heading(get_string('notenrolled', '', $fullname));
136 print_footer($course);
137 die;
141 if ($user->deleted) {
142 print_heading(get_string('userdeleted'));
145 /// OK, security out the way, now we are showing the user
147 add_to_log($course->id, "user", "view", "view.php?id=$user->id&course=$course->id", "$user->id");
149 if ($course->id != SITEID) {
150 if ($lastaccess = get_record('user_lastaccess', 'userid', $user->id, 'courseid', $course->id)) {
151 $user->lastaccess = $lastaccess->timeaccess;
156 /// Get the hidden field list
157 if (has_capability('moodle/user:viewhiddendetails', $coursecontext)) {
158 $hiddenfields = array();
159 } else {
160 $hiddenfields = array_flip(explode(',', $CFG->hiddenuserfields));
163 /// Print tabs at top
164 /// This same call is made in:
165 /// /user/view.php
166 /// /user/edit.php
167 /// /course/user.php
169 $currenttab = 'profile';
170 $showroles = 1;
171 include('tabs.php');
173 if (is_mnet_remote_user($user)) {
174 $sql = "
175 SELECT DISTINCT
176 h.id,
177 h.name,
178 h.wwwroot,
179 a.name as application,
180 a.display_name
181 FROM
182 {$CFG->prefix}mnet_host h,
183 {$CFG->prefix}mnet_application a
184 WHERE
185 h.id = '{$user->mnethostid}' AND
186 h.applicationid = a.id
187 ORDER BY
188 a.display_name,
189 h.name";
191 $remotehost = get_record_sql($sql);
193 echo '<p class="errorboxcontent">'.get_string('remote'.$remotehost->application.'user')." <br />\n";
194 if ($USER->id == $user->id) {
195 if ($remotehost->application =='moodle') {
196 echo "Remote {$remotehost->display_name}: <a href=\"{$remotehost->wwwroot}/user/edit.php\">{$remotehost->name}</a> ".get_string('editremoteprofile')." </p>\n";
197 } else {
198 echo "Remote {$remotehost->display_name}: <a href=\"{$remotehost->wwwroot}/\">{$remotehost->name}</a> ".get_string('gotoyourserver')." </p>\n";
200 } else {
201 echo "Remote {$remotehost->display_name}: <a href=\"{$remotehost->wwwroot}/\">{$remotehost->name}</a></p>\n";
205 echo '<table width="80%" class="userinfobox" summary="">';
206 echo '<tr>';
207 echo '<td class="side">';
208 print_user_picture($user->id, $course->id, $user->picture, true, false, false);
209 echo '</td><td class="content">';
211 // Print the description
213 if ($user->description && !isset($hiddenfields['description'])) {
214 echo format_text($user->description, FORMAT_MOODLE)."<hr />";
217 // Print all the little details in a list
219 echo '<table class="list">';
221 if (($user->city or $user->country) and (!isset($hiddenfields['city']) or !isset($hiddenfields['country']))) {
222 $location = '';
223 if ($user->city && !isset($hiddenfields['city'])) {
224 $location .= $user->city;
226 if (!empty($countries[$user->country]) && !isset($hiddenfields['country'])) {
227 if ($user->city && !isset($hiddenfields['country'])) {
228 $location .= ', ';
230 $countries = get_list_of_countries();
231 $location .= $countries[$user->country];
233 print_row(get_string("city").":", $location);
236 if (has_capability('moodle/user:viewhiddendetails', $coursecontext)) {
237 if ($user->address) {
238 print_row(get_string("address").":", "$user->address");
240 if ($user->phone1) {
241 print_row(get_string("phone").":", "$user->phone1");
243 if ($user->phone2) {
244 print_row(get_string("phone").":", "$user->phone2");
248 if ($user->maildisplay == 1 or
249 ($user->maildisplay == 2 and ($course->id != SITEID) and !isguest()) or
250 has_capability('moodle/course:useremail', $coursecontext)) {
252 $emailswitch = '';
254 if (has_capability('moodle/course:useremail', $coursecontext) or $currentuser) { /// Can use the enable/disable email stuff
255 if (!empty($enable)) { /// Recieved a parameter to enable the email address
256 set_field('user', 'emailstop', 0, 'id', $user->id);
257 $user->emailstop = 0;
259 if (!empty($disable)) { /// Recieved a parameter to disable the email address
260 set_field('user', 'emailstop', 1, 'id', $user->id);
261 $user->emailstop = 1;
265 if (has_capability('moodle/course:useremail', $coursecontext)) { /// Can use the enable/disable email stuff
266 if ($user->emailstop) {
267 $switchparam = 'enable';
268 $switchtitle = get_string('emaildisable');
269 $switchclick = get_string('emailenableclick');
270 $switchpix = 'emailno.gif';
271 } else {
272 $switchparam = 'disable';
273 $switchtitle = get_string('emailenable');
274 $switchclick = get_string('emaildisableclick');
275 $switchpix = 'email.gif';
277 $emailswitch = "&nbsp;<a title=\"$switchclick\" ".
278 "href=\"view.php?id=$user->id&amp;course=$course->id&amp;$switchparam=1\">".
279 "<img src=\"$CFG->pixpath/t/$switchpix\" alt=\"$switchclick\" /></a>";
281 } else if ($currentuser) { /// Can only re-enable an email this way
282 if ($user->emailstop) { // Include link that tells how to re-enable their email
283 $switchparam = 'enable';
284 $switchtitle = get_string('emaildisable');
285 $switchclick = get_string('emailenableclick');
287 $emailswitch = "&nbsp;(<a title=\"$switchclick\" ".
288 "href=\"view.php?id=$user->id&amp;course=$course->id&amp;enable=1\">$switchtitle</a>)";
292 print_row(get_string("email").":", obfuscate_mailto($user->email, '', $user->emailstop)."$emailswitch");
295 if ($user->url && !isset($hiddenfields['webpage'])) {
296 print_row(get_string("webpage").":", "<a href=\"$user->url\">$user->url</a>");
299 if ($user->icq && !isset($hiddenfields['icqnumber'])) {
300 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>");
303 if ($user->skype && !isset($hiddenfields['skypeid'])) {
304 print_row(get_string('skypeid').':','<a href="callto:'.urlencode($user->skype).'">'.s($user->skype).
305 ' <img src="http://mystatus.skype.com/smallicon/'.urlencode($user->skype).'" alt="'.get_string('status').'" '.
306 ' /></a>');
308 if ($user->yahoo && !isset($hiddenfields['yahooid'])) {
309 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>");
311 if ($user->aim && !isset($hiddenfields['aimid'])) {
312 print_row(get_string('aimid').':', '<a href="aim:goim?screenname='.s($user->aim).'">'.s($user->aim).'</a>');
314 if ($user->msn && !isset($hiddenfields['msnid'])) {
315 print_row(get_string('msnid').':', s($user->msn));
318 /// Print the Custom User Fields
319 profile_display_fields($user->id);
322 if ($mycourses = get_my_courses($user->id, null, null, false, 21)) {
323 $shown=0;
324 $courselisting = '';
325 foreach ($mycourses as $mycourse) {
326 if ($mycourse->category) {
327 if ($mycourse->id != $course->id){
328 $class = '';
329 if ($mycourse->visible == 0) {
330 // get_my_courses will filter courses $USER cannot see
331 // if we get one with visible 0 it just means it's hidden
332 // ... but not from $USER
333 $class = 'class="dimmed"';
335 $courselisting .= "<a href=\"{$CFG->wwwroot}/user/view.php?id={$user->id}&amp;course={$mycourse->id}\" $class >"
336 . format_string($mycourse->fullname) . "</a>, ";
338 else {
339 $courselisting .= format_string($mycourse->fullname) . ", ";
342 $shown++;
343 if($shown==20) {
344 $courselisting.= "...";
345 break;
348 print_row(get_string('courses').':', rtrim($courselisting,', '));
351 if (!isset($hiddenfields['lastaccess'])) {
352 if ($user->lastaccess) {
353 $datestring = userdate($user->lastaccess)."&nbsp; (".format_time(time() - $user->lastaccess).")";
354 } else {
355 $datestring = get_string("never");
357 print_row(get_string("lastaccess").":", $datestring);
359 /// printing roles
361 if ($rolestring = get_user_roles_in_context($id, $coursecontext->id)) {
362 print_row(get_string('roles').':', format_string($rolestring, false));
365 /// Printing groups
366 $isseparategroups = ($course->groupmode == SEPARATEGROUPS and $course->groupmodeforce and
367 !has_capability('moodle/site:accessallgroups', $coursecontext));
368 if (!$isseparategroups){
369 if ($usergroups = groups_get_all_groups($course->id, $user->id)){
370 $groupstr = '';
371 foreach ($usergroups as $group){
372 $groupstr .= ' <a href="'.$CFG->wwwroot.'/user/index.php?id='.$course->id.'&amp;group='.$group->id.'">'.format_string($group->name).'</a>,';
374 print_row(get_string("group").":", rtrim($groupstr, ', '));
377 /// End of printing groups
379 /// Printing Interests
380 if( !empty($CFG->usetags)) {
381 $interests = get_item_tags('user', $user->id);
383 $instereststr = tag_links_csv($interests);
385 if ($interests) {
386 print_row(get_string('interests').": ",rtrim($instereststr));
389 /// End of Printing Interests
391 echo "</table>";
393 echo "</td></tr></table>";
395 $userauth = get_auth_plugin($user->auth);
397 $passwordchangeurl = false;
398 if ($currentuser and $userauth->can_change_password() and !isguestuser() and has_capability('moodle/user:changeownpassword', $systemcontext)) {
399 if (!$passwordchangeurl = $userauth->change_password_url()) {
400 if (empty($CFG->loginhttps)) {
401 $passwordchangeurl = "$CFG->wwwroot/login/change_password.php";
402 } else {
403 $passwordchangeurl = str_replace('http:', 'https:', $CFG->wwwroot.'/login/change_password.php');
408 // Print other functions
409 echo '<div class="buttons">';
411 if ($passwordchangeurl) {
412 $params = array('id'=>$course->id);
414 if (!empty($USER->realuser)) {
415 $passwordchangeurl = ''; // do not use actual change password url - might contain sensitive data
416 } else {
417 $parts = explode('?', $passwordchangeurl);
418 $passwordchangeurl = reset($parts);
419 $after = next($parts);
420 preg_match_all('/([^&=]+)=([^&=]+)/', $after, $matches);
421 if (count($matches)) {
422 foreach($matches[0] as $key=>$match) {
423 $params[$matches[1][$key]] = $matches[2][$key];
427 echo "<form action=\"$passwordchangeurl\" method=\"get\">";
428 echo "<div>";
429 foreach($params as $key=>$value) {
430 echo '<input type="hidden" name="'.$key.'" value="'.s($value).'" />';
432 if (!empty($USER->realuser)) {
433 // changing of password when "Logged in as" is not allowed
434 echo "<input type=\"submit\" value=\"".get_string("changepassword")."\" disabled=\"disabled\" />";
435 } else {
436 echo "<input type=\"submit\" value=\"".get_string("changepassword")."\" />";
438 echo "</div>";
439 echo "</form>";
442 if ($course->id != SITEID && empty($course->metacourse)) { // Mostly only useful at course level
444 if (($user->id == $USER->id && // Myself
445 has_capability('moodle/course:view', $coursecontext, NULL) && // Course participant
446 has_capability('moodle/role:unassignself', $coursecontext, NULL, false)) // Can unassign myself
448 (has_capability('moodle/role:assign', $coursecontext, NULL) && // I can assign roles
449 get_user_roles($coursecontext, $user->id)) ) { // This user has roles
451 echo '<form action="../course/unenrol.php" method="get">';
452 echo "<div>";
453 echo '<input type="hidden" name="id" value="'.$course->id.'" />';
454 echo '<input type="hidden" name="user" value="'.$user->id.'" />';
455 echo '<input type="submit" value="'.get_string('unenrolme', '', $course->shortname).'" />';
456 echo "</div>";
457 echo '</form>';
461 if ($USER->id != $user->id && empty($USER->realuser) && has_capability('moodle/user:loginas', $coursecontext) &&
462 ! has_capability('moodle/site:doanything', $coursecontext, $user->id, false)) {
463 echo '<form action="'.$CFG->wwwroot.'/course/loginas.php" method="get">';
464 echo "<div>";
465 echo '<input type="hidden" name="id" value="'.$course->id.'" />';
466 echo '<input type="hidden" name="user" value="'.$user->id.'" />';
467 echo '<input type="hidden" name="sesskey" value="'.sesskey().'" />';
468 echo '<input type="submit" value="'.get_string('loginas').'" />';
469 echo "</div>";
470 echo '</form>';
473 if (!empty($CFG->messaging) and !isguest()) {
474 if (!empty($USER->id) and ($USER->id == $user->id)) {
475 if ($countmessages = count_records('message', 'useridto', $user->id)) {
476 $messagebuttonname = get_string("messages", "message")."($countmessages)";
477 } else {
478 $messagebuttonname = get_string("messages", "message");
480 echo "<form onclick=\"this.target='message'\" action=\"../message/index.php\" method=\"get\">";
481 echo "<div>";
482 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);\" />";
483 echo "</div>";
484 echo "</form>";
485 } else {
486 echo "<form onclick=\"this.target='message$user->id'\" action=\"../message/discussion.php\" method=\"get\">";
487 echo "<div>";
488 echo "<input type=\"hidden\" name=\"id\" value=\"$user->id\" />";
489 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);\" />";
490 echo "</div>";
491 echo "</form>";
494 // Authorize.net: User Payments
495 if ($course->enrol == 'authorize' || (empty($course->enrol) && $CFG->enrol == 'authorize')) {
496 echo "<form action=\"../enrol/authorize/index.php\" method=\"get\">";
497 echo "<div>";
498 echo "<input type=\"hidden\" name=\"course\" value=\"$course->id\" />";
499 echo "<input type=\"hidden\" name=\"user\" value=\"$user->id\" />";
500 echo "<input type=\"submit\" value=\"".get_string('payments')."\" />";
501 echo "</div>";
502 echo "</form>";
504 echo "</div>\n";
506 if ($CFG->debugdisplay && debugging('', DEBUG_DEVELOPER) && $USER->id == $user->id) { // Show user object
507 echo '<hr />';
508 print_heading('DEBUG MODE: User session variables');
509 print_object($USER);
512 print_footer($course);
514 /// Functions ///////
516 function print_row($left, $right) {
517 echo "\n<tr><td class=\"label c0\">$left</td><td class=\"info c1\">$right</td></tr>\n";