$CFG->preferlinegraphs will make graphs use lines instead of bars
[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     }
112     echo "<table width=\"80%\" align=\"center\" border=\"0\" cellpadding=\"1\" cellspacing=\"1\" class=\"userinfobox\">";
113     echo "<tr>";
114     echo "<td width=\"100\" valign=\"top\" class=\"userinfoboxside\">";
115     print_user_picture($user->id, $course->id, $user->picture, true, false, false);
116     echo "</td><td width=\"100%\" class=\"userinfoboxcontent\">";
119     // Print name and edit button across top
121     echo "<table width=\"100%\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\"><tr><td nowrap=\"nowrap\">";
122     echo "<h3>$fullname</h3>";
123     echo "</td><td align=\"right\">";
124     if (($currentuser and !isguest()) or isadmin()) {
125         if(empty($CFG->loginhttps)) {
126             $wwwroot = $CFG->wwwroot;
127         } else {
128             $wwwroot = str_replace('http','https',$CFG->wwwroot);
129         }
130         echo "<form action=\"$wwwroot/user/edit.php\" method=\"get\">";
131         echo "<input type=\"hidden\" name=\"id\" value=\"$id\" />";
132         echo "<input type=\"hidden\" name=\"course\" value=\"$course->id\" />";
133         echo "<input type=\"submit\" value=\"".get_string("editmyprofile")."\" />";
134         echo "</form>";
135     }
136     echo "</td></tr></table>\n\n";
139     // Print the description
141     if ($user->description) {
142         echo format_text($user->description, FORMAT_MOODLE)."<hr />";
143     }
145     // Print all the little details in a list
147     echo "<table border=\"0\" cellpadding=\"5\" cellspacing=\"2\">";
149     if ($user->city or $user->country) {
150         $countries = get_list_of_countries();
151         print_row(get_string("location").":", "$user->city, ".$countries["$user->country"]);
152     }
154     if (isteacher($course->id)) {
155         if ($user->address) {
156             print_row(get_string("address").":", "$user->address");
157         }
158         if ($user->phone1) {
159             print_row(get_string("phone").":", "$user->phone1");
160         }
161         if ($user->phone2) {
162             print_row(get_string("phone").":", "$user->phone2");
163         }
164     }
166     if ($user->maildisplay == 1 or
167        ($user->maildisplay == 2 and $course->category and !isguest()) or
168        isteacher($course->id)) {
170         $emailswitch = '';
172         if (isteacheredit($course->id) or $currentuser) {   /// Can use the enable/disable email stuff
173             if (!empty($_GET['enable'])) {     /// Recieved a parameter to enable the email address
174                 set_field('user', 'emailstop', 0, 'id', $user->id);
175                 $user->emailstop = 0;
176             }
177             if (!empty($_GET['disable'])) {     /// Recieved a parameter to disable the email address
178                 set_field('user', 'emailstop', 1, 'id', $user->id);
179                 $user->emailstop = 1;
180             }
181         }
183         if (isteacheredit($course->id)) {   /// Can use the enable/disable email stuff
184             if ($user->emailstop) {
185                 $switchparam = 'enable';
186                 $switchtitle = get_string('emaildisable');
187                 $switchclick = get_string('emailenableclick');
188                 $switchpix   = 'emailno.gif';
189             } else {
190                 $switchparam = 'disable';
191                 $switchtitle = get_string('emailenable');
192                 $switchclick = get_string('emaildisableclick');
193                 $switchpix   = 'email.gif';
194             }
195             $emailswitch = "&nbsp<a title=\"$switchclick\" ".
196                            "href=\"view.php?id=$user->id&course=$course->id&$switchparam=1\">".
197                            "<img border=\"0\" width=11 height=11 src=\"$CFG->pixpath/t/$switchpix\"></a>";
199         } else if ($currentuser) {         /// Can only re-enable an email this way
200             if ($user->emailstop) {   // Include link that tells how to re-enable their email
201                 $switchparam = 'enable';
202                 $switchtitle = get_string('emaildisable');
203                 $switchclick = get_string('emailenableclick');
205                 $emailswitch = "&nbsp(<a title=\"$switchclick\" ".
206                                "href=\"view.php?id=$user->id&course=$course->id&enable=1\">$switchtitle</a>)";
207             }
208         }
210         print_row(get_string("email").":", obfuscate_mailto($user->email, '', $user->emailstop)."$emailswitch");
211     }
213     if ($user->url) {
214         print_row(get_string("webpage").":", "<a href=\"$user->url\">$user->url</a>");
215     }
217     if ($user->icq) {
218         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>");
219     }
221     if ($user->skype) {
222         print_row(get_string('skypeid').':','<a href="callto:'.urlencode($user->skype).'">'.s($user->skype).'</a>');
223     }
224     if ($user->yahoo) {
225         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>');
226     }
227     if ($user->aim) {
228         print_row(get_string('aimid').':', '<a href="aim:goim?screenname='.s($user->aim).'">'.s($user->aim).'</a>');
229     }
230     if ($user->msn) {
231         print_row(get_string('msnid').':', s($user->msn));
232     }
234     if (isteacher($course->id)) {
235         if ($mycourses = get_my_courses($user->id)) {
236             $courselisting = '';
237             foreach ($mycourses as $mycourse) {
238                 if ($mycourse->visible and $mycourse->category) {
239                     $courselisting .= "<a href=\"$CFG->wwwroot/user/view.php?id=$user->id&amp;course=$mycourse->id\">$mycourse->fullname</a>, ";
240                 }
241             }
242             print_row(get_string('courses').':', rtrim($courselisting,', '));
243         }
244     }
246     if ($user->lastaccess) {
247         $datestring = userdate($user->lastaccess)."&nbsp (".format_time(time() - $user->lastaccess).")";
248     } else {
249         $datestring = get_string("never");
250     }
251     print_row(get_string("lastaccess").":", $datestring);
253     echo "</table>";
255     echo "</td></tr></table>";
258     $internalpassword = false;
259     if (is_internal_auth() or (!empty($CFG->{'auth_'.$USER->auth.'_stdchangepassword'}))) {
260         if (empty($CFG->loginhttps)) {
261             $internalpassword = "$CFG->wwwroot/login/change_password.php";
262         } else {
263             $internalpassword = str_replace('http','https',$CFG->wwwroot.'/login/change_password.php');
264         }
265     }
267 //  Print other functions
268     echo '<div class="buttons"><table align="center"><tr>';
269     if ($currentuser and !isguest()) {
270         if ($internalpassword ) {
271             echo "<td nowrap=\"nowrap\"><form action=\"$internalpassword\" method=\"get\">";
272             echo "<input type=\"hidden\" name=\"id\" value=\"$course->id\" />";
273             echo "<input type=\"submit\" value=\"".get_string("changepassword")."\" />";
274             echo "</form></td>";
275         } else if ( strlen($CFG->changepassword) > 1 ) {
276             echo "<td nowrap=\"nowrap\"><form action=\"$CFG->changepassword\" method=\"get\">";
277             echo "<input type=\"submit\" value=\"".get_string("changepassword")."\" />";
278             echo "</form></td>";
279         }
280     }
281     if ($course->category and
282         ((isstudent($course->id) and ($user->id == $USER->id) and !isguest() and $CFG->allowunenroll) or
283         (isteacheredit($course->id) and isstudent($course->id, $user->id))) ) {
284         echo "<td nowrap=\"nowrap\"><form action=\"../course/unenrol.php\" method=\"get\" />";
285         echo "<input type=\"hidden\" name=\"id\" value=\"$course->id\" />";
286         echo "<input type=\"hidden\" name=\"user\" value=\"$user->id\" />";
287         echo "<input type=\"submit\" value=\"".get_string("unenrolme", "", $course->shortname)."\">";
288         echo "</form></td>";
289     }
290     if (isteacher($course->id) or ($course->showreports and $USER->id == $user->id)) {
291         echo "<td nowrap=\"nowrap\"><form action=\"../course/user.php\" method=\"get\">";
292         echo "<input type=\"hidden\" name=\"id\" value=\"$course->id\" />";
293         echo "<input type=\"hidden\" name=\"user\" value=\"$user->id\" />";
294         echo "<input type=\"submit\" value=\"".get_string("activityreport")."\" />";
295         echo "</form></td>";
296     }
297     if ((isadmin() and !isadmin($user->id)) or (isteacher($course->id) and ($USER->id != $user->id) and !iscreator($user->id))) {
298         echo "<td nowrap=\"nowrap\"><form action=\"../course/loginas.php\" method=\"get\">";
299         echo "<input type=\"hidden\" name=\"id\" value=\"$course->id\" />";
300         echo "<input type=\"hidden\" name=\"user\" value=\"$user->id\" />";
301         echo "<input type=\"submit\" value=\"".get_string("loginas")."\" />";
302         echo "</form></td>";
303     }
304     if (!empty($CFG->messaging) and !isguest()) {
305         if ($USER->id == $user->id) {
306             if ($countmessages = count_records('message', 'useridto', $user->id)) {
307                 $messagebuttonname = get_string("messages", "message")."($countmessages)";
308             } else {
309                 $messagebuttonname = get_string("messages", "message");
310             }
311             echo "<td nowrap=\"nowrap\"><form target=\"message\" action=\"../message/index.php\" method=\"get\">";
312             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);\" />";
313             echo "</form></td>";
314         } else {
315             echo "<td nowrap=\"nowrap\"><form target=\"message_$user->id\" action=\"../message/user.php\" method=\"get\">";
316             echo "<input type=\"hidden\" name=\"id\" value=\"$user->id\" />";
317             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);\" />";
318             echo "</form></td>";
319         }
320     }
321     echo "<td></td>";
322     echo "</tr></table></div>\n";
324     $isseparategroups = ($course->groupmode == SEPARATEGROUPS and
325                          $course->groupmodeforce and
326                          !isteacheredit($course->id));
328     $groupid = $isseparategroups ? get_current_group($course->id) : NULL;
330     forum_print_user_discussions($course->id, $user->id, $groupid);
332     print_footer($course);
334 /// Functions ///////
336 function print_row($left, $right) {
337     echo "\n<tr><td nowrap=\"nowrap\" align=\"right\" valign=\"top\">$left</td><td align=\"left\" valign=\"top\">$right</td></tr>\n";
340 ?>