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