New user tabs from Shane and myself ... these bring together the
[moodle.git] / user / view.php
1 <?PHP // $Id$
3 //  Display profile for a particular user
5     require_once("../config.php");
6     require_once("../mod/forum/lib.php");
8     optional_variable($id);
9     optional_variable($course);
10     optional_variable($enable, "");
11     optional_variable($disable, "");
13     if (!empty($SESSION->wantsurl)) {
14         $wantsurl = $SESSION->wantsurl;
15         unset($SESSION->wantsurl);
16         redirect($wantsurl);
17     }
19     if (empty($id)) {         // See your own profile by default
20         require_login();
21         $id = $USER->id;
22     }
24     if (empty($course)) {     // See it at site level by default
25         $course = SITEID;
26     }
28     if (! $user = get_record("user", "id", $id) ) {
29         error("No such user in this course");
30     }
32     if (! $course = get_record("course", "id", $course) ) {
33         error("No such course id");
34     }
36     if ($course->category) {
37         require_login($course->id);
38     } else if ($CFG->forcelogin or !empty($CFG->forceloginforprofiles)) {
39         if (isguest()) {
40             redirect("$CFG->wwwroot/login/index.php");
41         }
42         require_login();
43     }
45     add_to_log($course->id, "user", "view", "view.php?id=$user->id&course=$course->id", "$user->id");
47     if ($course->id != SITEID) {
48         if ($student = get_record("user_students", "userid", $user->id, "course", $course->id)) {
49             $user->lastaccess = $student->timeaccess;
50         } else if ($teacher = get_record("user_teachers", "userid", $user->id, "course", $course->id)) {
51             $user->lastaccess = $teacher->timeaccess;
52         }
53     }
55     $fullname = fullname($user, isteacher($course->id));
56     $personalprofile = get_string("personalprofile");
57     $participants = get_string("participants");
59     if (empty($USER->id)) {
60        $currentuser = false;
61     } else {
62        $currentuser = ($user->id == $USER->id);
63     }
65     if (groupmode($course) == SEPARATEGROUPS and !isteacheredit($course->id)) {   // Groups must be kept separate
66         require_login();
68         if (!$currentuser && !isteacheredit($course->id, $user->id) && !ismember(mygroupid($course->id), $user->id)) {
69             print_header("$personalprofile: ", "$personalprofile: ",
70                          "<a href=\"../course/view.php?id=$course->id\">$course->shortname</a> ->
71                           <a href=\"index.php?id=$course->id\">$participants</a>",
72                           "", "", true, "&nbsp;", navmenu($course));
73             error(get_string("groupnotamember"), "../course/view.php?id=$course->id");
74         }
75     }
77     if ($course->id == SITEID and !$currentuser) {  // To reduce possibility of "browsing" userbase at site level
78         if (!isteacherinanycourse() and !isteacherinanycourse($user->id) ) {  // Teachers can browse and be browsed at site level
79             print_header("$personalprofile: ", "$personalprofile: ",
80                           "<a href=\"index.php?id=$course->id\">$participants</a>",
81                           "", "", true, "&nbsp;", navmenu($course));
82             print_heading(get_string('usernotavailable', 'error'));
83             print_footer($course);
84             die;
85         }
86     }
89     if ($course->category) {
90         print_header("$personalprofile: $fullname", "$personalprofile: $fullname",
91                      "<a href=\"../course/view.php?id=$course->id\">$course->shortname</a> ->
92                       <a href=\"index.php?id=$course->id\">$participants</a> -> $fullname",
93                       "", "", true, "&nbsp;", navmenu($course));
94     } else {
95         print_header("$course->fullname: $personalprofile: $fullname", "$course->fullname",
96                      "$fullname", "", "", true, "&nbsp;", navmenu($course));
97     }
100     if ($course->category and ! isguest() ) {   // Need to have access to a course to see that info
101         if (!isstudent($course->id, $user->id) && !isteacher($course->id, $user->id)) {
102             print_heading(get_string("notenrolled", "", $fullname));
103             print_footer($course);
104             die;
105         }
106     }
108     if ($user->deleted) {
109         print_heading(get_string("userdeleted"));
110     }
113 /// Print tabs at top
114 /// This same call is made in:
115 ///     /user/view.php
116 ///     /user/edit.php
117 ///     /course/user.php
118     $currenttab = 'profile';
119     include('tabs.php');
123     echo "<table width=\"80%\" align=\"center\" border=\"0\" cellspacing=\"0\" class=\"userinfobox\">";
124     echo "<tr>";
125     echo "<td width=\"100\" valign=\"top\" class=\"side\">";
126     print_user_picture($user->id, $course->id, $user->picture, true, false, false);
127     echo "</td><td width=\"100%\" class=\"content\">";
129     // Print the description
131     if ($user->description) {
132         echo format_text($user->description, FORMAT_MOODLE)."<hr />";
133     }
135     // Print all the little details in a list
137     echo '<table border="0" cellpadding="0" cellspacing="0" class="list">';
139     if ($user->city or $user->country) {
140         $countries = get_list_of_countries();
141         print_row(get_string("location").":", "$user->city, ".$countries["$user->country"]);
142     }
144     if (isteacher($course->id)) {
145         if ($user->address) {
146             print_row(get_string("address").":", "$user->address");
147         }
148         if ($user->phone1) {
149             print_row(get_string("phone").":", "$user->phone1");
150         }
151         if ($user->phone2) {
152             print_row(get_string("phone").":", "$user->phone2");
153         }
154     }
156     if ($user->maildisplay == 1 or
157        ($user->maildisplay == 2 and $course->category and !isguest()) or
158        isteacher($course->id)) {
160         $emailswitch = '';
162         if (isteacheredit($course->id) or $currentuser) {   /// Can use the enable/disable email stuff
163             if (!empty($_GET['enable'])) {     /// Recieved a parameter to enable the email address
164                 set_field('user', 'emailstop', 0, 'id', $user->id);
165                 $user->emailstop = 0;
166             }
167             if (!empty($_GET['disable'])) {     /// Recieved a parameter to disable the email address
168                 set_field('user', 'emailstop', 1, 'id', $user->id);
169                 $user->emailstop = 1;
170             }
171         }
173         if (isteacheredit($course->id)) {   /// Can use the enable/disable email stuff
174             if ($user->emailstop) {
175                 $switchparam = 'enable';
176                 $switchtitle = get_string('emaildisable');
177                 $switchclick = get_string('emailenableclick');
178                 $switchpix   = 'emailno.gif';
179             } else {
180                 $switchparam = 'disable';
181                 $switchtitle = get_string('emailenable');
182                 $switchclick = get_string('emaildisableclick');
183                 $switchpix   = 'email.gif';
184             }
185             $emailswitch = "&nbsp<a title=\"$switchclick\" ".
186                            "href=\"view.php?id=$user->id&course=$course->id&$switchparam=1\">".
187                            "<img border=\"0\" width=11 height=11 src=\"$CFG->pixpath/t/$switchpix\"></a>";
189         } else if ($currentuser) {         /// Can only re-enable an email this way
190             if ($user->emailstop) {   // Include link that tells how to re-enable their email
191                 $switchparam = 'enable';
192                 $switchtitle = get_string('emaildisable');
193                 $switchclick = get_string('emailenableclick');
195                 $emailswitch = "&nbsp(<a title=\"$switchclick\" ".
196                                "href=\"view.php?id=$user->id&course=$course->id&enable=1\">$switchtitle</a>)";
197             }
198         }
200         print_row(get_string("email").":", obfuscate_mailto($user->email, '', $user->emailstop)."$emailswitch");
201     }
203     if ($user->url) {
204         print_row(get_string("webpage").":", "<a href=\"$user->url\">$user->url</a>");
205     }
207     if ($user->icq) {
208         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\" width=\"18\" height=\"18\" border=\"0\" alt=\"\" /></a>");
209     }
211     if ($user->skype) {
212         print_row(get_string('skypeid').':','<a href="callto:'.urlencode($user->skype).'">'.s($user->skype).'</a>');
213     }
214     if ($user->yahoo) {
215         print_row(get_string('yahooid').':', '<a href="http://edit.yahoo.com/config/send_webmesg?.target='.s($user->yahoo).'&amp;.src=pg">'.s($user->yahoo).'</a>');
216     }
217     if ($user->aim) {
218         print_row(get_string('aimid').':', '<a href="aim:goim?screenname='.s($user->aim).'">'.s($user->aim).'</a>');
219     }
220     if ($user->msn) {
221         print_row(get_string('msnid').':', s($user->msn));
222     }
224     if (isteacher($course->id)) {
225         if ($mycourses = get_my_courses($user->id)) {
226             $courselisting = '';
227             foreach ($mycourses as $mycourse) {
228                 if ($mycourse->visible and $mycourse->category) {
229                     $courselisting .= "<a href=\"$CFG->wwwroot/user/view.php?id=$user->id&amp;course=$mycourse->id\">$mycourse->fullname</a>, ";
230                 }
231             }
232             print_row(get_string('courses').':', rtrim($courselisting,', '));
233         }
234     }
236     if ($user->lastaccess) {
237         $datestring = userdate($user->lastaccess)."&nbsp (".format_time(time() - $user->lastaccess).")";
238     } else {
239         $datestring = get_string("never");
240     }
241     print_row(get_string("lastaccess").":", $datestring);
243     echo "</table>";
245     echo "</td></tr></table>";
248     $internalpassword = false;
249     if (is_internal_auth() or (!empty($CFG->{'auth_'.$USER->auth.'_stdchangepassword'}))) {
250         if (empty($CFG->loginhttps)) {
251             $internalpassword = "$CFG->wwwroot/login/change_password.php";
252         } else {
253             $internalpassword = str_replace('http','https',$CFG->wwwroot.'/login/change_password.php');
254         }
255     }
257 //  Print other functions
258     echo '<div class="buttons"><table align="center"><tr>';
259     if ($currentuser and !isguest()) {
260         if ($internalpassword ) {
261             echo "<td nowrap=\"nowrap\"><form action=\"$internalpassword\" method=\"get\">";
262             echo "<input type=\"hidden\" name=\"id\" value=\"$course->id\" />";
263             echo "<input type=\"submit\" value=\"".get_string("changepassword")."\" />";
264             echo "</form></td>";
265         } else if ( strlen($CFG->changepassword) > 1 ) {
266             echo "<td nowrap=\"nowrap\"><form action=\"$CFG->changepassword\" method=\"get\">";
267             echo "<input type=\"submit\" value=\"".get_string("changepassword")."\" />";
268             echo "</form></td>";
269         }
270     }
271     if ($course->category and
272         ((isstudent($course->id) and ($user->id == $USER->id) and !isguest() and $CFG->allowunenroll) or
273         (isteacheredit($course->id) and isstudent($course->id, $user->id))) ) {
274         echo "<td nowrap=\"nowrap\"><form action=\"../course/unenrol.php\" method=\"get\" />";
275         echo "<input type=\"hidden\" name=\"id\" value=\"$course->id\" />";
276         echo "<input type=\"hidden\" name=\"user\" value=\"$user->id\" />";
277         echo "<input type=\"submit\" value=\"".get_string("unenrolme", "", $course->shortname)."\">";
278         echo "</form></td>";
279     }
280 /*    if (isteacher($course->id) or ($course->showreports and $USER->id == $user->id)) {
281         echo "<td nowrap=\"nowrap\"><form action=\"../course/user.php\" method=\"get\">";
282         echo "<input type=\"hidden\" name=\"id\" value=\"$course->id\" />";
283         echo "<input type=\"hidden\" name=\"user\" value=\"$user->id\" />";
284         echo "<input type=\"submit\" value=\"".get_string("activityreport")."\" />";
285         echo "</form></td>";
286     }
287 */
288     if ((isadmin() and !isadmin($user->id)) or (isteacher($course->id) and ($USER->id != $user->id) and !iscreator($user->id))) {
289         echo "<td nowrap=\"nowrap\"><form action=\"../course/loginas.php\" method=\"get\">";
290         echo "<input type=\"hidden\" name=\"id\" value=\"$course->id\" />";
291         echo "<input type=\"hidden\" name=\"user\" value=\"$user->id\" />";
292         echo "<input type=\"submit\" value=\"".get_string("loginas")."\" />";
293         echo "</form></td>";
294     }
295     if (!empty($CFG->messaging) and !isguest()) {
296         if (!empty($USER->id) and ($USER->id == $user->id)) {
297             if ($countmessages = count_records('message', 'useridto', $user->id)) {
298                 $messagebuttonname = get_string("messages", "message")."($countmessages)";
299             } else {
300                 $messagebuttonname = get_string("messages", "message");
301             }
302             echo "<td nowrap=\"nowrap\"><form target=\"message\" action=\"../message/index.php\" method=\"get\">";
303             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);\" />";
304             echo "</form></td>";
305         } else {
306             echo "<td nowrap=\"nowrap\"><form target=\"message_$user->id\" action=\"../message/user.php\" method=\"get\">";
307             echo "<input type=\"hidden\" name=\"id\" value=\"$user->id\" />";
308             echo "<input type=\"submit\" value=\"".get_string("sendmessage", "message")."\" onclick=\"return openpopup('/message/user.php?id=$user->id', 'message_$user->id', 'menubar=0,location=0,scrollbars,status,resizable,width=400,height=500', 0);\" />";
309             echo "</form></td>";
310         }
311     }
312     echo "<td></td>";
313     echo "</tr></table></div>\n";
316     print_footer($course);
318 /// Functions ///////
320 function print_row($left, $right) {
321     echo "\n<tr><td nowrap=\"nowrap\" align=\"right\" valign=\"top\" class=\"label c0\">$left</td><td align=\"left\" valign=\"top\" class=\"info c1\">$right</td></tr>\n";
324 ?>