Improvements to logs for admin user (can view any course)
[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
f9903ed0 11function print_log_selector_form($course, $selecteduser=0, $selecteddate="today") {
12
9481285b 13 global $USER, $CFG;
14
f9903ed0 15 // Get all the possible users
16 $users = array();
720a43ce 17
18 if ($course->category) {
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 }
f9903ed0 32 }
33 }
720a43ce 34
35 if (isadmin()) {
36 if ($ccc = get_records_sql("SELECT * FROM course ORDER BY fullname")) {
37 foreach ($ccc as $cc) {
38 $courses["$cc->id"] = "$cc->fullname";
39 }
f9903ed0 40 }
41 }
42
43 asort($users);
44
45 // Get all the possible dates
9481285b 46 // Note that we are keeping track of real (GMT) time and user time
47 // User time is only used in displays - all calcs and passing is GMT
48
49 $timenow = time(); // GMT
50
51 // What day is it now for the user, and when is midnight that day (in GMT).
9604ccb1 52 $timemidnight = $today = usergetmidnight($timenow);
9481285b 53
54 // Put today up the top of the list
55 $dates = array("$timemidnight" => "Today, ".userdate($timenow, "j F Y") );
56
57 if (! $course->startdate) {
58 $course->startdate = $course->timecreated;
59 }
f9903ed0 60
9481285b 61 $numdates = 1;
62 while ($timemidnight > $course->startdate and $numdates < 365) {
f9903ed0 63 $timemidnight = $timemidnight - 86400;
9481285b 64 $timenow = $timenow - 86400;
65 $dates["$timemidnight"] = userdate($timenow, "l, j F Y");
66 $numdates++;
f9903ed0 67 }
68
69 if ($selecteddate == "today") {
70 $selecteddate = $today;
71 }
72
73 echo "<CENTER>";
74 echo "<FORM ACTION=log.php METHOD=get>";
720a43ce 75 if (isadmin()) {
76 choose_from_menu ($courses, "id", $course->id, "All courses");
77 } else {
78 echo "<INPUT TYPE=hidden NAME=id VALUE=\"$course->id\">";
79 }
80 if ($course->category) {
81 choose_from_menu ($users, "user", $selecteduser, "All participants");
82 }
f9903ed0 83 choose_from_menu ($dates, "date", $selecteddate, "Any day");
84 echo "<INPUT TYPE=submit VALUE=\"Show these logs\">";
85 echo "</FORM>";
86 echo "</CENTER>";
87}
88
600149be 89function make_log_url($module, $url) {
90 switch ($module) {
91 case "course":
92 case "user":
93 case "file":
94 case "login":
95 case "lib":
96 case "admin":
97 return "/$module/$url";
98 break;
99 default:
100 return "/mod/$module/$url";
101 break;
102 }
103}
104
105
f9903ed0 106function print_log($course, $user=0, $date=0, $order="ORDER BY l.time ASC") {
9481285b 107// It is assumed that $date is the GMT time of midnight for that day,
108// and so the next 86400 seconds worth of logs are printed.
f9903ed0 109
720a43ce 110 if ($course->category) {
111 $selector = "WHERE l.course='$course->id' AND l.user = u.id";
112 } else {
113 $selector = "WHERE l.user = u.id"; // Show all courses
114 if ($ccc = get_records_sql("SELECT * FROM course ORDER BY fullname")) {
115 foreach ($ccc as $cc) {
116 $courses[$cc->id] = "$cc->shortname";
117 }
118 }
119 }
f9903ed0 120
121 if ($user) {
122 $selector .= " AND l.user = '$user'";
123 }
124
125 if ($date) {
126 $enddate = $date + 86400;
127 $selector .= " AND l.time > '$date' AND l.time < '$enddate'";
128 }
129
130 if (!$logs = get_records_sql("SELECT l.*, u.firstname, u.lastname, u.picture
600149be 131 FROM log l, user u $selector $order")){
f9903ed0 132 notify("No logs found!");
133 print_footer($course);
134 exit;
135 }
136
137 $count=0;
138 $tt = getdate(time());
139 $today = mktime (0, 0, 0, $tt["mon"], $tt["mday"], $tt["year"]);
140 echo "<P ALIGN=CENTER>Displaying ".count($logs)." records</P>";
141 echo "<TABLE BORDER=0 ALIGN=center CELLPADDING=3 CELLSPACING=3>";
142 foreach ($logs as $log) {
600149be 143
144 if ($ld = get_record_sql("SELECT * FROM log_display WHERE module='$log->module' AND action='$log->action'")) {
145 $log->info = get_field($ld->table, $ld->field, "id", $log->info);
146 }
147
f9903ed0 148 echo "<TR>";
720a43ce 149 if (! $course->category) {
150 echo "<TD><FONT SIZE=2><A HREF=\"view.php?id=$log->course\">".$courses[$log->course]."</A></TD>";
151 }
873960de 152 echo "<TD ALIGN=right><FONT SIZE=2>".userdate($log->time, "l")."</TD>";
153 echo "<TD><FONT SIZE=2>".userdate($log->time, "j M Y, h:i A")."</TD>";
f3ecd2c8 154 echo "<TD><FONT SIZE=2><A TITLE=\"$log->ip\" HREF=\"../user/view.php?id=$log->user&course=$log->course\"><B>$log->firstname $log->lastname</B></TD>";
f9903ed0 155 echo "<TD><FONT SIZE=2>";
600149be 156 link_to_popup_window( make_log_url($log->module,$log->url), "fromloglive","$log->module $log->action", 400, 600);
f9903ed0 157 echo "</TD>";
600149be 158 echo "<TD><FONT SIZE=2>$log->info</TD>";
f9903ed0 159 echo "</TR>";
160 }
161 echo "</TABLE>";
162}
163
164
d887b5a7 165function print_all_courses($cat=1) {
166
167 if ($courses = get_records("course", "category", $cat, "fullname ASC")) {
168 foreach ($courses as $course) {
169 print_course($course);
170 echo "<BR>\n";
171 }
172
173 } else {
174 echo "<H3>No courses have been defined yet</H3>";
175 }
176}
177
178
f9903ed0 179function print_course($course) {
180
d887b5a7 181 global $CFG;
182
f9903ed0 183 if (! $site = get_record("course", "category", "0") ) {
184 error("Could not find a site!");
185 }
186
d887b5a7 187 print_simple_box_start("CENTER", "100%");
f9903ed0 188
189 echo "<TABLE WIDTH=100%>";
da5c172a 190 echo "<TR VALIGN=top>";
191 echo "<TD VALIGN=top WIDTH=50%>";
9481285b 192 echo "<P><FONT SIZE=3><B><A TITLE=\"Click to enter this course\"
193 HREF=\"$CFG->wwwroot/course/view.php?id=$course->id\">$course->fullname</A></B></FONT></P>";
f9903ed0 194 if ($teachers = get_records_sql("SELECT u.* FROM user u, user_teachers t
195 WHERE u.id = t.user AND t.course = '$course->id'
f51ab26b 196 ORDER BY t.authority ASC")) {
f9903ed0 197
198 echo "<P><FONT SIZE=1>\n";
199 foreach ($teachers as $teacher) {
d887b5a7 200 echo "$course->teacher: <A HREF=\"$CFG->wwwroot/user/view.php?id=$teacher->id&course=$site->id\">$teacher->firstname $teacher->lastname</A><BR>";
f9903ed0 201 }
202 echo "</FONT></P>";
da5c172a 203 }
204 if ($course->guest or ($course->password == "")) {
cbc14629 205 echo "<A TITLE=\"This course allows guest users\" HREF=\"$CFG->wwwroot/course/view.php?id=$course->id\">";
d887b5a7 206 echo "<IMG VSPACE=4 ALT=\"\" HEIGHT=16 WIDTH=16 BORDER=0 SRC=\"$CFG->wwwroot/user/user.gif\"></A>&nbsp;&nbsp;";
da5c172a 207 }
208 if ($course->password) {
cbc14629 209 echo "<A TITLE=\"This course requires an enrolment key\" HREF=\"$CFG->wwwroot/course/view.php?id=$course->id\">";
d887b5a7 210 echo "<IMG VSPACE=4 ALT=\"\" HEIGHT=16 WIDTH=16 BORDER=0 SRC=\"$CFG->wwwroot/pix/i/key.gif\"></A>";
da5c172a 211 }
212
213
214 echo "</TD><TD VALIGN=top WIDTH=50%>";
215 echo "<P><FONT SIZE=2>".text_to_html($course->summary)."</FONT></P>";
216 echo "</TD></TR>";
217 echo "</TABLE>";
f9903ed0 218
da5c172a 219 print_simple_box_end();
f9903ed0 220}
221
600149be 222function print_headline($text, $size=2) {
223 echo "<B><FONT SIZE=\"$size\">$text</FONT></B><BR>\n";
224}
225
226function print_recent_activity($course) {
227 // $course is an object
228 // This function trawls through the logs looking for
229 // anything new since the user's last login
230
231 global $CFG, $USER;
232
233 if (! $USER->lastlogin ) {
234 echo "<P>Welcome to the course! Here you will find a list of what's new since your last login.</P>";
235 return;
236 }
237
238 if (! $logs = get_records_sql("SELECT * FROM log WHERE time > '$USER->lastlogin' AND course = '$course->id' ORDER BY time ASC")) {
239 return;
240 }
241
242
243 // Firstly, have there been any new enrolments?
244
245 $heading = false;
246 $content = false;
247 foreach ($logs as $log) {
248 if ($log->module == "course" and $log->action == "enrol") {
249 if (! $heading) {
ef25340c 250 print_headline("New users:");
600149be 251 $heading = true;
252 $content = true;
253 }
254 $user = get_record("user", "id", $log->info);
ef25340c 255 echo "<P><FONT SIZE=1><A HREF=\"../user/view.php?id=$user->id&course=$course->id\">$user->firstname $user->lastname</A></FONT></P>";
600149be 256 }
257 }
258
259 // Next, have there been any changes to the course structure?
260
600149be 261 foreach ($logs as $log) {
262 if ($log->module == "course") {
263 if ($log->action == "add mod" or $log->action == "update mod" or $log->action == "delete mod") {
600149be 264 $info = split(" ", $log->info);
265 $modname = get_field($info[0], "name", "id", $info[1]);
266
267 if ($info[0] == "discuss") {
83b0b773 268 $info[0] = "discussion"; // nasty hack, really.
600149be 269 }
270
600149be 271 switch ($log->action) {
272 case "add mod":
ef25340c 273 $changelist["$log->info"] = array ("operation" => "add", "text" => "Added a ".$info[0].":<BR><A HREF=\"$CFG->wwwroot/course/$log->url\">$modname</A>");
600149be 274 break;
275 case "update mod":
ef25340c 276 if (! $changelist["$log->info"]) {
277 $changelist["$log->info"] = array ("operation" => "update", "text" => "Updated the ".$info[0].":<BR><A HREF=\"$CFG->wwwroot/course/$log->url\">$modname</A>");
278 }
600149be 279 break;
280 case "delete mod":
ef25340c 281 if ($changelist["$log->info"]["operation"] == "add") {
282 $changelist["$log->info"] = NULL;
283 } else {
284 $changelist["$log->info"] = array ("operation" => "delete", "text" => "Deleted a ".$info[0]);
285 }
600149be 286 break;
287 }
ef25340c 288 }
289 }
290 }
291
292 if ($changelist) {
293 foreach ($changelist as $changeinfo => $change) {
294 if ($change) {
295 $changes[$changeinfo] = $change;
296 }
297 }
298 if (count($changes) > 0) {
299 print_headline("Course changes:");
300 $content = true;
301 foreach ($changes as $changeinfo => $change) {
302 echo "<P><FONT SIZE=1>".$change["text"]."</FONT></P>";
600149be 303 }
304 }
305 }
306
307
308 // Now all we need to know are the new posts.
309
ef25340c 310 $heading = false;
600149be 311 foreach ($logs as $log) {
312
313 if ($log->module == "discuss") {
314 $post = NULL;
315
316 if ($log->action == "add post") {
317 $post = get_record_sql("SELECT p.*, u.firstname, u.lastname,
318 u.email, u.picture, u.id as userid
319 FROM discuss_posts p, user u
320 WHERE p.id = '$log->info' AND p.user = u.id");
321
322 } else if ($log->action == "add") {
323 $post = get_record_sql("SELECT p.*, u.firstname, u.lastname,
324 u.email, u.picture, u.id as userid
325 FROM discuss d, discuss_posts p, user u
326 WHERE d.id = '$log->info' AND d.firstpost = p.id AND p.user = u.id");
327 }
328
329 if ($post) {
330 if (! $heading) {
ef25340c 331 print_headline("Discussion Posts:");
600149be 332 $heading = true;
333 $content = true;
334 }
ef25340c 335 echo "<P><FONT SIZE=1>$post->firstname $post->lastname:<BR>";
336 echo "\"<A HREF=\"$CFG->wwwroot/mod/discuss/$log->url\">";
600149be 337 if ($log->action == "add") {
ef25340c 338 echo "<B>$post->subject</B>";
600149be 339 } else {
ef25340c 340 echo "$post->subject";
600149be 341 }
ef25340c 342 echo "</A>\"</FONT></P>";
600149be 343 }
344
345 }
346 }
347
348 if (! $content) {
349 echo "<FONT SIZE=2>Nothing new since your last login</FONT>";
350 }
351
352}
353
e1360728 354
355function unenrol_student_in_course($user, $course) {
356 global $db;
357
358 return $db->Execute("DELETE FROM user_students WHERE user = '$user' AND course = '$course'");
359}
360
361
362
363function enrol_student_in_course($user, $course) {
364 global $db;
365
366 $timenow = time();
367
368 $rs = $db->Execute("INSERT INTO user_students (user, course, start, end, time)
369 VALUES ($user, $course, 0, 0, $timenow)");
370 if ($rs) {
371 return true;
372 } else {
373 return false;
374 }
375}
376
f9903ed0 377?>