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