Added button to unenrol a user (course/unenrol.php)
[moodle.git] / course / lib.php
CommitLineData
f9903ed0 1<? // $Id$
2
3$MAXNEWSDISPLAY = 4;
4
5$FORMATS = array (
4567fb71 6 "1" => "Weekly layout",
7 "2" => "Social layout"
f9903ed0 8 );
9
10
11function logdate($date) {
12 return date("l, j F Y, g:i A", $date);
13}
14
15function print_log_selector_form($course, $selecteduser=0, $selecteddate="today") {
16
17 // Get all the possible users
18 $users = array();
19 if ($students = get_records_sql("SELECT u.* FROM user u, user_students s
20 WHERE s.course = '$course->id' AND s.user = u.id
21 ORDER BY u.lastaccess DESC")) {
22 foreach ($students as $student) {
23 $users["$student->id"] = "$student->firstname $student->lastname";
24 }
25 }
26 if ($teachers = get_records_sql("SELECT u.* FROM user u, user_teachers t
27 WHERE t.course = '$course->id' AND t.user = u.id
28 ORDER BY u.lastaccess DESC")) {
29 foreach ($teachers as $teacher) {
30 $users["$teacher->id"] = "$teacher->firstname $teacher->lastname";
31 }
32 }
33
34 asort($users);
35
36 // Get all the possible dates
37 $tt = getdate(time());
38 $timemidnight = $today = mktime (0, 0, 0, $tt["mon"], $tt["mday"], $tt["year"]);
39 $dates = array("$today" => "Today, ".date("j F Y", $today) );
40
41 while ($timemidnight > $course->startdate) {
42 $timemidnight = $timemidnight - 86400;
43 $dates["$timemidnight"] = date("l, j F Y", $timemidnight);
44 }
45
46 if ($selecteddate == "today") {
47 $selecteddate = $today;
48 }
49
50 echo "<CENTER>";
51 echo "<FORM ACTION=log.php METHOD=get>";
52 echo "<INPUT TYPE=hidden NAME=id VALUE=\"$course->id\">";
53 choose_from_menu ($users, "user", $selecteduser, "All participants");
54 choose_from_menu ($dates, "date", $selecteddate, "Any day");
55 echo "<INPUT TYPE=submit VALUE=\"Show these logs\">";
56 echo "</FORM>";
57 echo "</CENTER>";
58}
59
600149be 60function make_log_url($module, $url) {
61 switch ($module) {
62 case "course":
63 case "user":
64 case "file":
65 case "login":
66 case "lib":
67 case "admin":
68 return "/$module/$url";
69 break;
70 default:
71 return "/mod/$module/$url";
72 break;
73 }
74}
75
76
f9903ed0 77function print_log($course, $user=0, $date=0, $order="ORDER BY l.time ASC") {
78
79 $selector = "WHERE l.course='$course->id' AND l.user = u.id";
80
81 if ($user) {
82 $selector .= " AND l.user = '$user'";
83 }
84
85 if ($date) {
86 $enddate = $date + 86400;
87 $selector .= " AND l.time > '$date' AND l.time < '$enddate'";
88 }
89
90 if (!$logs = get_records_sql("SELECT l.*, u.firstname, u.lastname, u.picture
600149be 91 FROM log l, user u $selector $order")){
f9903ed0 92 notify("No logs found!");
93 print_footer($course);
94 exit;
95 }
96
97 $count=0;
98 $tt = getdate(time());
99 $today = mktime (0, 0, 0, $tt["mon"], $tt["mday"], $tt["year"]);
100 echo "<P ALIGN=CENTER>Displaying ".count($logs)." records</P>";
101 echo "<TABLE BORDER=0 ALIGN=center CELLPADDING=3 CELLSPACING=3>";
102 foreach ($logs as $log) {
600149be 103
104 if ($ld = get_record_sql("SELECT * FROM log_display WHERE module='$log->module' AND action='$log->action'")) {
105 $log->info = get_field($ld->table, $ld->field, "id", $log->info);
106 }
107
f9903ed0 108 echo "<TR>";
109 echo "<TD ALIGN=right><FONT SIZE=2>".date("l", $log->time)."</TD>";
110 echo "<TD><FONT SIZE=2>".date("j M Y, h:i A", $log->time)."</TD>";
111 echo "<TD><FONT SIZE=2><B>$log->firstname $log->lastname</B></TD>";
112 echo "<TD><FONT SIZE=2>";
600149be 113 link_to_popup_window( make_log_url($log->module,$log->url), "fromloglive","$log->module $log->action", 400, 600);
f9903ed0 114 echo "</TD>";
600149be 115 echo "<TD><FONT SIZE=2>$log->info</TD>";
f9903ed0 116 echo "</TR>";
117 }
118 echo "</TABLE>";
119}
120
121
122function print_course($course) {
123
124 if (! $site = get_record("course", "category", "0") ) {
125 error("Could not find a site!");
126 }
127
128 print_simple_box_start("CENTER", "80%");
129
130 echo "<TABLE WIDTH=100%>";
131 echo "<TR VALIGN=top><TD VALIGN=top WIDTH=50%>";
132 echo "<P><FONT SIZE=3><B><A HREF=\"view.php?id=$course->id\">$course->fullname</A></B></FONT></P>";
133 if ($teachers = get_records_sql("SELECT u.* FROM user u, user_teachers t
134 WHERE u.id = t.user AND t.course = '$course->id'
f51ab26b 135 ORDER BY t.authority ASC")) {
f9903ed0 136
137 echo "<P><FONT SIZE=1>\n";
138 foreach ($teachers as $teacher) {
139 echo "$course->teacher: <A HREF=\"../user/view.php?id=$teacher->id&course=$site->id\">$teacher->firstname $teacher->lastname</A><BR>";
140 }
141 echo "</FONT></P>";
142 }
143 echo "</TD><TD VALIGN=top WIDTH=50%>";
144 echo "<P><FONT SIZE=2>".text_to_html($course->summary)."</FONT></P>";
145 echo "</TD></TR>";
146 echo "</TABLE>";
147
148 print_simple_box_end();
149}
150
600149be 151function print_headline($text, $size=2) {
152 echo "<B><FONT SIZE=\"$size\">$text</FONT></B><BR>\n";
153}
154
155function print_recent_activity($course) {
156 // $course is an object
157 // This function trawls through the logs looking for
158 // anything new since the user's last login
159
160 global $CFG, $USER;
161
162 if (! $USER->lastlogin ) {
163 echo "<P>Welcome to the course! Here you will find a list of what's new since your last login.</P>";
164 return;
165 }
166
167 if (! $logs = get_records_sql("SELECT * FROM log WHERE time > '$USER->lastlogin' AND course = '$course->id' ORDER BY time ASC")) {
168 return;
169 }
170
171
172 // Firstly, have there been any new enrolments?
173
174 $heading = false;
175 $content = false;
176 foreach ($logs as $log) {
177 if ($log->module == "course" and $log->action == "enrol") {
178 if (! $heading) {
ef25340c 179 print_headline("New users:");
600149be 180 $heading = true;
181 $content = true;
182 }
183 $user = get_record("user", "id", $log->info);
ef25340c 184 echo "<P><FONT SIZE=1><A HREF=\"../user/view.php?id=$user->id&course=$course->id\">$user->firstname $user->lastname</A></FONT></P>";
600149be 185 }
186 }
187
188 // Next, have there been any changes to the course structure?
189
600149be 190 foreach ($logs as $log) {
191 if ($log->module == "course") {
192 if ($log->action == "add mod" or $log->action == "update mod" or $log->action == "delete mod") {
600149be 193 $info = split(" ", $log->info);
194 $modname = get_field($info[0], "name", "id", $info[1]);
195
196 if ($info[0] == "discuss") {
83b0b773 197 $info[0] = "discussion"; // nasty hack, really.
600149be 198 }
199
600149be 200 switch ($log->action) {
201 case "add mod":
ef25340c 202 $changelist["$log->info"] = array ("operation" => "add", "text" => "Added a ".$info[0].":<BR><A HREF=\"$CFG->wwwroot/course/$log->url\">$modname</A>");
600149be 203 break;
204 case "update mod":
ef25340c 205 if (! $changelist["$log->info"]) {
206 $changelist["$log->info"] = array ("operation" => "update", "text" => "Updated the ".$info[0].":<BR><A HREF=\"$CFG->wwwroot/course/$log->url\">$modname</A>");
207 }
600149be 208 break;
209 case "delete mod":
ef25340c 210 if ($changelist["$log->info"]["operation"] == "add") {
211 $changelist["$log->info"] = NULL;
212 } else {
213 $changelist["$log->info"] = array ("operation" => "delete", "text" => "Deleted a ".$info[0]);
214 }
600149be 215 break;
216 }
ef25340c 217 }
218 }
219 }
220
221 if ($changelist) {
222 foreach ($changelist as $changeinfo => $change) {
223 if ($change) {
224 $changes[$changeinfo] = $change;
225 }
226 }
227 if (count($changes) > 0) {
228 print_headline("Course changes:");
229 $content = true;
230 foreach ($changes as $changeinfo => $change) {
231 echo "<P><FONT SIZE=1>".$change["text"]."</FONT></P>";
600149be 232 }
233 }
234 }
235
236
237 // Now all we need to know are the new posts.
238
ef25340c 239 $heading = false;
600149be 240 foreach ($logs as $log) {
241
242 if ($log->module == "discuss") {
243 $post = NULL;
244
245 if ($log->action == "add post") {
246 $post = get_record_sql("SELECT p.*, u.firstname, u.lastname,
247 u.email, u.picture, u.id as userid
248 FROM discuss_posts p, user u
249 WHERE p.id = '$log->info' AND p.user = u.id");
250
251 } else if ($log->action == "add") {
252 $post = get_record_sql("SELECT p.*, u.firstname, u.lastname,
253 u.email, u.picture, u.id as userid
254 FROM discuss d, discuss_posts p, user u
255 WHERE d.id = '$log->info' AND d.firstpost = p.id AND p.user = u.id");
256 }
257
258 if ($post) {
259 if (! $heading) {
ef25340c 260 print_headline("Discussion Posts:");
600149be 261 $heading = true;
262 $content = true;
263 }
ef25340c 264 echo "<P><FONT SIZE=1>$post->firstname $post->lastname:<BR>";
265 echo "\"<A HREF=\"$CFG->wwwroot/mod/discuss/$log->url\">";
600149be 266 if ($log->action == "add") {
ef25340c 267 echo "<B>$post->subject</B>";
600149be 268 } else {
ef25340c 269 echo "$post->subject";
600149be 270 }
ef25340c 271 echo "</A>\"</FONT></P>";
600149be 272 }
273
274 }
275 }
276
277 if (! $content) {
278 echo "<FONT SIZE=2>Nothing new since your last login</FONT>";
279 }
280
281}
282
f9903ed0 283?>