Should be $COURSE not $course
[moodle-linuxchix.git] / user / view.php
blob9b3610f733c9dca7acf705470b4f7b51e6b9c341
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 // make sure user can view this student's profile
51 if ($USER->id != $user->id
52 && !has_capability('moodle/user:viewdetails', $coursecontext)
53 && !has_capability('moodle/user:viewdetails', $usercontext)) {
54 error('You can not view the profile of this user');
57 if (!empty($CFG->forceloginforprofiles)) {
58 require_login();
59 if (isguest()) {
60 redirect("$CFG->wwwroot/login/index.php");
64 $strpersonalprofile = get_string('personalprofile');
65 $strparticipants = get_string("participants");
66 $struser = get_string("user");
68 $fullname = fullname($user, has_capability('moodle/site:viewfullnames', $coursecontext));
70 /// If the user being shown is not ourselves, then make sure we are allowed to see them!
72 if (!$currentuser) {
73 if ($course->id == SITEID) { // Reduce possibility of "browsing" userbase at site level
74 if ($CFG->forceloginforprofiles and !isteacherinanycourse() and !isteacherinanycourse($user->id) and !has_capability('moodle/user:viewdetails', $usercontext)) { // Teachers can browse and be browsed at site level. If not forceloginforprofiles, allow access (bug #4366)
75 print_header("$strpersonalprofile: ", "$strpersonalprofile: ",
76 "<a href=\"index.php?id=$course->id\">$strparticipants</a> -> $struser",
77 "", "", true, "&nbsp;", navmenu($course));
78 print_heading(get_string('usernotavailable', 'error'));
79 print_footer($course);
80 exit;
82 } else { // Normal course
83 if (!has_capability('moodle/course:view', $coursecontext, $user->id, false)) {
84 if (has_capability('moodle/course:view', $coursecontext)) {
85 print_header("$strpersonalprofile: ", "$strpersonalprofile: ",
86 "<a href=\"../course/view.php?id=$course->id\">$course->shortname</a> ->
87 <a href=\"index.php?id=$course->id\">$strparticipants</a> -> $fullname",
88 "", "", true, "&nbsp;", navmenu($course));
89 print_heading(get_string('notenrolled', '', $fullname));
90 } else {
91 print_header("$strpersonalprofile: ", "$strpersonalprofile: ",
92 "<a href=\"../course/view.php?id=$course->id\">$course->shortname</a> ->
93 <a href=\"index.php?id=$course->id\">$strparticipants</a> -> $struser",
94 "", "", true, "&nbsp;", navmenu($course));
95 print_heading(get_string('notenrolledprofile'));
97 print_continue($_SERVER['HTTP_REFERER']);
98 print_footer($course);
99 exit;
104 // If groups are in use, make sure we can see that group
105 if (groupmode($course) == SEPARATEGROUPS and !has_capability('moodle/site:accessallgroups', $coursecontext)) {
106 require_login();
108 ///this is changed because of mygroupid
109 $gtrue = (bool)groups_get_groups_for_user($user->id, $course->id);
110 /*TODO: $gtrue = false;
111 if ($mygroups = mygroupid($course->id)){
112 foreach ($mygroups as $group){
113 if (ismember($group, $user->id)){
114 $gtrue = true;
118 if (!$gtrue) {
119 print_header("$strpersonalprofile: ", "$strpersonalprofile: ",
120 "<a href=\"../course/view.php?id=$course->id\">$course->shortname</a> ->
121 <a href=\"index.php?id=$course->id\">$strparticipants</a>",
122 "", "", true, "&nbsp;", navmenu($course));
123 error(get_string("groupnotamember"), "../course/view.php?id=$course->id");
129 /// We've established they can see the user's name at least, so what about the rest?
131 if ($course->id != SITEID) {
132 print_header("$strpersonalprofile: $fullname", "$strpersonalprofile: $fullname",
133 "<a href=\"../course/view.php?id=$course->id\">$course->shortname</a> ->
134 <a href=\"index.php?id=$course->id\">$strparticipants</a> -> $fullname",
135 "", "", true, "&nbsp;", navmenu($course));
136 } else {
137 print_header("$course->fullname: $strpersonalprofile: $fullname", $course->fullname,
138 "$fullname", "", "", true, "&nbsp;", navmenu($course));
142 if (($course->id != SITEID) and ! isguest() ) { // Need to have access to a course to see that info
143 if (!has_capability('moodle/course:view', $coursecontext, $user->id)) {
144 print_heading(get_string('notenrolled', '', $fullname));
145 print_footer($course);
146 die;
150 if ($user->deleted) {
151 print_heading(get_string('userdeleted'));
154 /// OK, security out the way, now we are showing the user
156 add_to_log($course->id, "user", "view", "view.php?id=$user->id&course=$course->id", "$user->id");
158 if ($course->id != SITEID) {
159 if ($lastaccess = get_record('user_lastaccess', 'userid', $user->id, 'courseid', $course->id)) {
160 $user->lastaccess = $lastaccess->timeaccess;
165 /// Get the hidden field list
166 if (has_capability('moodle/user:viewhiddendetails', $coursecontext)) {
167 $hiddenfields = array();
168 } else {
169 $hiddenfields = array_flip(explode(',', $CFG->hiddenuserfields));
172 /// Print tabs at top
173 /// This same call is made in:
174 /// /user/view.php
175 /// /user/edit.php
176 /// /course/user.php
178 $currenttab = 'profile';
179 $showroles = 1;
180 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->id, $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 (($user->city or $user->country) and (!isset($hiddenfields['city']) or !isset($hiddenfields['country']))) {
231 $location = '';
232 if ($user->city && !isset($hiddenfields['city'])) {
233 $location .= $user->city;
235 if (!empty($countries[$user->country]) && !isset($hiddenfields['country'])) {
236 if ($user->city && !isset($hiddenfields['country'])) {
237 $location .= ', ';
239 $countries = get_list_of_countries();
240 $location .= $countries[$user->country];
242 print_row(get_string("city").":", $location);
245 if (has_capability('moodle/user:viewhiddendetails', $coursecontext)) {
246 if ($user->address) {
247 print_row(get_string("address").":", "$user->address");
249 if ($user->phone1) {
250 print_row(get_string("phone").":", "$user->phone1");
252 if ($user->phone2) {
253 print_row(get_string("phone").":", "$user->phone2");
257 if ($user->maildisplay == 1 or
258 ($user->maildisplay == 2 and ($course->id != SITEID) and !isguest()) or
259 has_capability('moodle/course:useremail', $coursecontext)) {
261 $emailswitch = '';
263 if (has_capability('moodle/course:useremail', $coursecontext) or $currentuser) { /// Can use the enable/disable email stuff
264 if (!empty($enable)) { /// Recieved a parameter to enable the email address
265 set_field('user', 'emailstop', 0, 'id', $user->id);
266 $user->emailstop = 0;
268 if (!empty($disable)) { /// Recieved a parameter to disable the email address
269 set_field('user', 'emailstop', 1, 'id', $user->id);
270 $user->emailstop = 1;
274 if (has_capability('moodle/course:useremail', $coursecontext)) { /// Can use the enable/disable email stuff
275 if ($user->emailstop) {
276 $switchparam = 'enable';
277 $switchtitle = get_string('emaildisable');
278 $switchclick = get_string('emailenableclick');
279 $switchpix = 'emailno.gif';
280 } else {
281 $switchparam = 'disable';
282 $switchtitle = get_string('emailenable');
283 $switchclick = get_string('emaildisableclick');
284 $switchpix = 'email.gif';
286 $emailswitch = "&nbsp;<a title=\"$switchclick\" ".
287 "href=\"view.php?id=$user->id&amp;course=$course->id&amp;$switchparam=1\">".
288 "<img src=\"$CFG->pixpath/t/$switchpix\" alt=\"$switchclick\" /></a>";
290 } else if ($currentuser) { /// Can only re-enable an email this way
291 if ($user->emailstop) { // Include link that tells how to re-enable their email
292 $switchparam = 'enable';
293 $switchtitle = get_string('emaildisable');
294 $switchclick = get_string('emailenableclick');
296 $emailswitch = "&nbsp;(<a title=\"$switchclick\" ".
297 "href=\"view.php?id=$user->id&amp;course=$course->id&amp;enable=1\">$switchtitle</a>)";
301 print_row(get_string("email").":", obfuscate_mailto($user->email, '', $user->emailstop)."$emailswitch");
304 if ($user->url && !isset($hiddenfields['webpage'])) {
305 print_row(get_string("webpage").":", "<a href=\"$user->url\">$user->url</a>");
308 if ($user->icq && !isset($hiddenfields['icqnumber'])) {
309 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>");
312 if ($user->skype && !isset($hiddenfields['skypeid'])) {
313 print_row(get_string('skypeid').':','<a href="callto:'.urlencode($user->skype).'">'.s($user->skype).
314 ' <img src="http://mystatus.skype.com/smallicon/'.urlencode($user->skype).'" alt="'.get_string('status').'" '.
315 ' /></a>');
317 if ($user->yahoo && !isset($hiddenfields['yahooid'])) {
318 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>");
320 if ($user->aim && !isset($hiddenfields['aimid'])) {
321 print_row(get_string('aimid').':', '<a href="aim:goim?screenname='.s($user->aim).'">'.s($user->aim).'</a>');
323 if ($user->msn && !isset($hiddenfields['msnid'])) {
324 print_row(get_string('msnid').':', s($user->msn));
327 /// Print the Custom User Fields
328 profile_display_fields($user->id);
331 if ($mycourses = get_my_courses($user->id, null, null, false, 21)) {
332 $shown=0;
333 $courselisting = '';
334 foreach ($mycourses as $mycourse) {
335 if ($mycourse->visible and $mycourse->category) {
336 if ($mycourse->id != $course->id){
337 $courselisting .= "<a href=\"$CFG->wwwroot/user/view.php?id=$user->id&amp;course=$mycourse->id\">"
338 . format_string($mycourse->fullname) . "</a>, ";
340 else {
341 $courselisting .= format_string($mycourse->fullname) . ", ";
344 $shown++;
345 if($shown==20) {
346 $courselisting.= "...";
347 break;
350 print_row(get_string('courses').':', rtrim($courselisting,', '));
353 if (!isset($hiddenfields['lastaccess'])) {
354 if ($user->lastaccess) {
355 $datestring = userdate($user->lastaccess)."&nbsp; (".format_time(time() - $user->lastaccess).")";
356 } else {
357 $datestring = get_string("never");
359 print_row(get_string("lastaccess").":", $datestring);
361 /// printing roles
363 if ($rolestring = get_user_roles_in_context($id, $coursecontext->id)) {
364 print_row(get_string('roles').':', format_string($rolestring, false));
367 /// Printing groups
368 $isseparategroups = ($course->groupmode == SEPARATEGROUPS and $course->groupmodeforce and
369 !has_capability('moodle/site:accessallgroups', $coursecontext));
370 if (!$isseparategroups){
371 if ($usergroups = user_group($course->id, $user->id)){
372 $groupstr = '';
373 foreach ($usergroups as $group){
374 $groupstr .= ' <a href="'.$CFG->wwwroot.'/user/index.php?id='.$course->id.'&amp;group='.$group->id.'">'.$group->name.'</a>,';
376 print_row(get_string("group").":", rtrim($groupstr, ', '));
379 /// End of printing groups
381 /// Printing Interests
382 if( !empty($CFG->usetags)) {
383 $interests = get_item_tags('user', $user->id);
385 $instereststr = tag_links_csv($interests);
387 if ($interests) {
388 print_row(get_string('interests').": ",rtrim($instereststr));
391 /// End of Printing Interests
393 echo "</table>";
395 echo "</td></tr></table>";
397 $userauth = get_auth_plugin($user->auth);
399 $passwordchangeurl = false;
400 if ($currentuser and $userauth->can_change_password() and !isguestuser() and has_capability('moodle/user:changeownpassword', $systemcontext)) {
401 if (!$passwordchangeurl = $userauth->change_password_url()) {
402 if (empty($CFG->loginhttps)) {
403 $passwordchangeurl = "$CFG->wwwroot/login/change_password.php";
404 } else {
405 $passwordchangeurl = str_replace('http:', 'https:', $CFG->wwwroot.'/login/change_password.php');
410 // Print other functions
411 echo '<div class="buttons">';
413 if ($passwordchangeurl) {
414 $params = array('id'=>$course->id);
416 if (!empty($USER->realuser)) {
417 $passwordchangeurl = ''; // do not use actual change password url - might contain sensitive data
418 } else {
419 $parts = explode('?', $passwordchangeurl);
420 $passwordchangeurl = reset($parts);
421 $after = next($parts);
422 preg_match_all('/([^&=]+)=([^&=]+)/', $after, $matches);
423 if (count($matches)) {
424 foreach($matches[0] as $key=>$match) {
425 $params[$matches[1][$key]] = $matches[2][$key];
429 echo "<form action=\"$passwordchangeurl\" method=\"get\">";
430 echo "<div>";
431 foreach($params as $key=>$value) {
432 echo '<input type="hidden" name="'.$key.'" value="'.s($value).'" />';
434 if (!empty($USER->realuser)) {
435 // changing of password when "Logged in as" is not allowed
436 echo "<input type=\"submit\" value=\"".get_string("changepassword")."\" disabled=\"disabled\" />";
437 } else {
438 echo "<input type=\"submit\" value=\"".get_string("changepassword")."\" />";
440 echo "</div>";
441 echo "</form>";
444 if ($course->id != SITEID && empty($course->metacourse)) { // Mostly only useful at course level
446 if (($user->id == $USER->id && // Myself
447 has_capability('moodle/course:view', $coursecontext, NULL) && // Course participant
448 has_capability('moodle/role:unassignself', $coursecontext, NULL, false)) // Can unassign myself
450 (has_capability('moodle/role:assign', $coursecontext, NULL) && // I can assign roles
451 get_user_roles($coursecontext, $user->id)) ) { // This user has roles
453 echo '<form action="../course/unenrol.php" method="get">';
454 echo "<div>";
455 echo '<input type="hidden" name="id" value="'.$course->id.'" />';
456 echo '<input type="hidden" name="user" value="'.$user->id.'" />';
457 echo '<input type="submit" value="'.get_string('unenrolme', '', $course->shortname).'" />';
458 echo "</div>";
459 echo '</form>';
463 if ($USER->id != $user->id && empty($USER->realuser) && has_capability('moodle/user:loginas', $coursecontext) &&
464 ! has_capability('moodle/site:doanything', $coursecontext, $user->id, false)) {
465 echo '<form action="'.$CFG->wwwroot.'/course/loginas.php" method="get">';
466 echo "<div>";
467 echo '<input type="hidden" name="id" value="'.$course->id.'" />';
468 echo '<input type="hidden" name="user" value="'.$user->id.'" />';
469 echo '<input type="hidden" name="sesskey" value="'.sesskey().'" />';
470 echo '<input type="submit" value="'.get_string('loginas').'" />';
471 echo "</div>";
472 echo '</form>';
475 if (!empty($CFG->messaging) and !isguest()) {
476 if (!empty($USER->id) and ($USER->id == $user->id)) {
477 if ($countmessages = count_records('message', 'useridto', $user->id)) {
478 $messagebuttonname = get_string("messages", "message")."($countmessages)";
479 } else {
480 $messagebuttonname = get_string("messages", "message");
482 echo "<form onclick=\"this.target='message'\" action=\"../message/index.php\" method=\"get\">";
483 echo "<div>";
484 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);\" />";
485 echo "</div>";
486 echo "</form>";
487 } else {
488 echo "<form onclick=\"this.target='message$user->id'\" action=\"../message/discussion.php\" method=\"get\">";
489 echo "<div>";
490 echo "<input type=\"hidden\" name=\"id\" value=\"$user->id\" />";
491 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);\" />";
492 echo "</div>";
493 echo "</form>";
496 // Authorize.net: User Payments
497 if ($course->enrol == 'authorize' || (empty($course->enrol) && $CFG->enrol == 'authorize')) {
498 echo "<form action=\"../enrol/authorize/index.php\" method=\"get\">";
499 echo "<div>";
500 echo "<input type=\"hidden\" name=\"course\" value=\"$course->id\" />";
501 echo "<input type=\"hidden\" name=\"user\" value=\"$user->id\" />";
502 echo "<input type=\"submit\" value=\"".get_string('payments')."\" />";
503 echo "</div>";
504 echo "</form>";
506 echo "</div>\n";
508 if ($CFG->debugdisplay && debugging('', DEBUG_DEVELOPER) && $USER->id == $user->id) { // Show user object
509 echo '<hr />';
510 print_heading('DEBUG MODE: User session variables');
511 print_object($USER);
514 print_footer($course);
516 /// Functions ///////
518 function print_row($left, $right) {
519 echo "\n<tr><td class=\"label c0\">$left</td><td class=\"info c1\">$right</td></tr>\n";