Allow 1-minute delay (mostly good for testing)
[moodle.git] / course / lib.php
CommitLineData
f9903ed0 1<? // $Id$
97c270e9 2 // Library of useful functions
f9903ed0 3
f9903ed0 4
97c270e9 5$COURSE_MAX_LOG_DISPLAY = 150; // days
d9d1c35d 6
97c270e9 7$COURSE_TEACHER_COLOR = "#990000"; // To hilight certain items that are teacher-only
87ffed0d 8
97c270e9 9$COURSE_LIVELOG_REFRESH = 60; // Seconds
ef58b822 10
f9903ed0 11
f9903ed0 12function print_log_selector_form($course, $selecteduser=0, $selecteddate="today") {
13
9481285b 14 global $USER, $CFG;
15
f9903ed0 16 // Get all the possible users
17 $users = array();
720a43ce 18
19 if ($course->category) {
20 if ($students = get_records_sql("SELECT u.* FROM user u, user_students s
21 WHERE s.course = '$course->id' AND s.user = u.id
22 ORDER BY u.lastaccess DESC")) {
23 foreach ($students as $student) {
24 $users["$student->id"] = "$student->firstname $student->lastname";
25 }
26 }
27 if ($teachers = get_records_sql("SELECT u.* FROM user u, user_teachers t
28 WHERE t.course = '$course->id' AND t.user = u.id
29 ORDER BY u.lastaccess DESC")) {
30 foreach ($teachers as $teacher) {
31 $users["$teacher->id"] = "$teacher->firstname $teacher->lastname";
32 }
f9903ed0 33 }
122cffc9 34 if ($guest = get_user_info_from_db("username", "guest")) {
35 $users["$guest->id"] = "$guest->firstname $guest->lastname";
36 }
f9903ed0 37 }
720a43ce 38
39 if (isadmin()) {
40 if ($ccc = get_records_sql("SELECT * FROM course ORDER BY fullname")) {
41 foreach ($ccc as $cc) {
cfa5d3f2 42 if ($cc->category) {
43 $courses["$cc->id"] = "$cc->fullname";
44 } else {
45 $courses["$cc->id"] = " $cc->fullname (Site)";
46 }
720a43ce 47 }
f9903ed0 48 }
cfa5d3f2 49 asort($courses);
f9903ed0 50 }
51
52 asort($users);
53
54 // Get all the possible dates
9481285b 55 // Note that we are keeping track of real (GMT) time and user time
56 // User time is only used in displays - all calcs and passing is GMT
57
58 $timenow = time(); // GMT
59
60 // What day is it now for the user, and when is midnight that day (in GMT).
9604ccb1 61 $timemidnight = $today = usergetmidnight($timenow);
9481285b 62
63 // Put today up the top of the list
97c270e9 64 $dates = array("$timemidnight" => get_string("today").", ".userdate($timenow, "%e %B %Y") );
9481285b 65
66 if (! $course->startdate) {
67 $course->startdate = $course->timecreated;
68 }
f9903ed0 69
9481285b 70 $numdates = 1;
71 while ($timemidnight > $course->startdate and $numdates < 365) {
f9903ed0 72 $timemidnight = $timemidnight - 86400;
9481285b 73 $timenow = $timenow - 86400;
7a302afc 74 $dates["$timemidnight"] = userdate($timenow, "%A, %e %B %Y");
9481285b 75 $numdates++;
f9903ed0 76 }
77
78 if ($selecteddate == "today") {
79 $selecteddate = $today;
80 }
81
82 echo "<CENTER>";
83 echo "<FORM ACTION=log.php METHOD=get>";
720a43ce 84 if (isadmin()) {
849bc02a 85 choose_from_menu ($courses, "id", $course->id, "");
720a43ce 86 } else {
87 echo "<INPUT TYPE=hidden NAME=id VALUE=\"$course->id\">";
88 }
89 if ($course->category) {
97c270e9 90 choose_from_menu ($users, "user", $selecteduser, get_string("allparticipants") );
720a43ce 91 }
97c270e9 92 choose_from_menu ($dates, "date", $selecteddate, get_string("alldays"));
93 echo "<INPUT TYPE=submit VALUE=\"".get_string("showtheselogs")."\">";
f9903ed0 94 echo "</FORM>";
95 echo "</CENTER>";
96}
97
600149be 98function make_log_url($module, $url) {
99 switch ($module) {
100 case "course":
101 case "user":
102 case "file":
103 case "login":
104 case "lib":
105 case "admin":
106 return "/$module/$url";
107 break;
108 default:
109 return "/mod/$module/$url";
110 break;
111 }
112}
113
114
f9903ed0 115function print_log($course, $user=0, $date=0, $order="ORDER BY l.time ASC") {
9481285b 116// It is assumed that $date is the GMT time of midnight for that day,
117// and so the next 86400 seconds worth of logs are printed.
f9903ed0 118
720a43ce 119 if ($course->category) {
120 $selector = "WHERE l.course='$course->id' AND l.user = u.id";
a2ab3b05 121
720a43ce 122 } else {
123 $selector = "WHERE l.user = u.id"; // Show all courses
124 if ($ccc = get_records_sql("SELECT * FROM course ORDER BY fullname")) {
125 foreach ($ccc as $cc) {
126 $courses[$cc->id] = "$cc->shortname";
127 }
128 }
129 }
f9903ed0 130
131 if ($user) {
132 $selector .= " AND l.user = '$user'";
133 }
134
135 if ($date) {
136 $enddate = $date + 86400;
137 $selector .= " AND l.time > '$date' AND l.time < '$enddate'";
138 }
139
140 if (!$logs = get_records_sql("SELECT l.*, u.firstname, u.lastname, u.picture
600149be 141 FROM log l, user u $selector $order")){
f9903ed0 142 notify("No logs found!");
143 print_footer($course);
144 exit;
145 }
146
147 $count=0;
148 $tt = getdate(time());
149 $today = mktime (0, 0, 0, $tt["mon"], $tt["mday"], $tt["year"]);
150 echo "<P ALIGN=CENTER>Displaying ".count($logs)." records</P>";
151 echo "<TABLE BORDER=0 ALIGN=center CELLPADDING=3 CELLSPACING=3>";
152 foreach ($logs as $log) {
600149be 153
154 if ($ld = get_record_sql("SELECT * FROM log_display WHERE module='$log->module' AND action='$log->action'")) {
565f7a95 155 $log->info = get_field($ld->mtable, $ld->field, "id", $log->info);
600149be 156 }
157
f58d18bc 158 echo "<TR NOWRAP>";
720a43ce 159 if (! $course->category) {
f58d18bc 160 echo "<TD NOWRAP><FONT SIZE=2><A HREF=\"view.php?id=$log->course\">".$courses[$log->course]."</A></TD>";
720a43ce 161 }
f58d18bc 162 echo "<TD NOWRAP ALIGN=right><FONT SIZE=2>".userdate($log->time, "%A")."</TD>";
163 echo "<TD NOWRAP><FONT SIZE=2>".userdate($log->time, "%e %B %Y, %I:%M %p")."</TD>";
ebea4e27 164 echo "<TD NOWRAP><FONT SIZE=2>";
ef4743b9 165 link_to_popup_window("$CFG->wwwroot/lib/ipatlas/plot.php?address=$log->ip&user=$log->user", "ipatlas","$log->ip", 400, 700);
ebea4e27 166 echo "</TD>";
f58d18bc 167 echo "<TD NOWRAP><FONT SIZE=2><A HREF=\"../user/view.php?id=$log->user&course=$log->course\"><B>$log->firstname $log->lastname</B></TD>";
168 echo "<TD NOWRAP><FONT SIZE=2>";
600149be 169 link_to_popup_window( make_log_url($log->module,$log->url), "fromloglive","$log->module $log->action", 400, 600);
f9903ed0 170 echo "</TD>";
f58d18bc 171 echo "<TD NOWRAP><FONT SIZE=2>$log->info</TD>";
f9903ed0 172 echo "</TR>";
173 }
174 echo "</TABLE>";
175}
176
177
ba2e5d73 178function print_all_courses($category="all", $style="full", $maxcount=999) {
0a263205 179 global $CFG, $USER;
d887b5a7 180
ba2e5d73 181 if ($category == "all") {
182 $courses = get_records_sql("SELECT * FROM course WHERE category > 0 ORDER BY fullname ASC");
0a263205 183
184 } else if ($category == "my") {
185 if (isset($USER->id)) {
186 if ($courses = get_records_sql("SELECT * FROM course WHERE category > 0 ORDER BY fullname ASC")) {
187 foreach ($courses as $key => $course) {
188 if (!isteacher($course->id) and !isstudent($course->id)) {
189 unset($courses[$key]);
190 }
191 }
192 }
193 }
194
ba2e5d73 195 } else {
196 $courses = get_records("course", "category", $category, "fullname ASC");
197 }
198
199 if ($courses) {
94361e02 200 if ($style == "minimal") {
201 $count = 0;
97c270e9 202 $icon = "<IMG SRC=\"pix/i/course.gif\" HEIGHT=16 WIDTH=16 ALT=\"".get_string("course")."\">";
94361e02 203 foreach ($courses as $course) {
565f7a95 204 $moddata[]="<A TITLE=\"$course->shortname\" HREF=\"$CFG->wwwroot/course/view.php?id=$course->id\">$course->fullname</A>";
94361e02 205 $modicon[]=$icon;
206 if ($count++ >= $maxcount) {
207 break;
208 }
209 }
210 $fulllist = "<P><A HREF=\"$CFG->wwwroot/course/\">".get_string("fulllistofcourses")."</A>...";
211 print_side_block("", $moddata, "$fulllist", $modicon);
212
213 } else {
214 foreach ($courses as $course) {
215 print_course($course);
216 echo "<BR>\n";
217 }
d887b5a7 218 }
219
220 } else {
cf38360f 221 echo "<H3>".get_string("nocoursesyet")."</H3>";
d887b5a7 222 }
223}
224
225
f9903ed0 226function print_course($course) {
227
d887b5a7 228 global $CFG;
229
a83fded1 230 if (! $site = get_site()) {
f9903ed0 231 error("Could not find a site!");
232 }
233
d887b5a7 234 print_simple_box_start("CENTER", "100%");
f9903ed0 235
236 echo "<TABLE WIDTH=100%>";
da5c172a 237 echo "<TR VALIGN=top>";
238 echo "<TD VALIGN=top WIDTH=50%>";
7a302afc 239 echo "<P><FONT SIZE=3><B><A TITLE=\"".get_string("entercourse")."\"
9481285b 240 HREF=\"$CFG->wwwroot/course/view.php?id=$course->id\">$course->fullname</A></B></FONT></P>";
0c71c9ae 241 if ($teachers = get_course_teachers($course->id)) {
f9903ed0 242 echo "<P><FONT SIZE=1>\n";
243 foreach ($teachers as $teacher) {
0c71c9ae 244 if ($teacher->authority > 0) {
b4d7002e 245 if (!$teacher->role) {
246 $teacher->role = $course->teacher;
247 }
248 echo "$teacher->role: <A HREF=\"$CFG->wwwroot/user/view.php?id=$teacher->id&course=$site->id\">$teacher->firstname $teacher->lastname</A><BR>";
0c71c9ae 249 }
f9903ed0 250 }
251 echo "</FONT></P>";
da5c172a 252 }
253 if ($course->guest or ($course->password == "")) {
97c270e9 254 echo "<A TITLE=\"".get_string("allowguests")."\" HREF=\"$CFG->wwwroot/course/view.php?id=$course->id\">";
d887b5a7 255 echo "<IMG VSPACE=4 ALT=\"\" HEIGHT=16 WIDTH=16 BORDER=0 SRC=\"$CFG->wwwroot/user/user.gif\"></A>&nbsp;&nbsp;";
da5c172a 256 }
257 if ($course->password) {
97c270e9 258 echo "<A TITLE=\"".get_string("requireskey")."\" HREF=\"$CFG->wwwroot/course/view.php?id=$course->id\">";
d887b5a7 259 echo "<IMG VSPACE=4 ALT=\"\" HEIGHT=16 WIDTH=16 BORDER=0 SRC=\"$CFG->wwwroot/pix/i/key.gif\"></A>";
da5c172a 260 }
261
262
263 echo "</TD><TD VALIGN=top WIDTH=50%>";
264 echo "<P><FONT SIZE=2>".text_to_html($course->summary)."</FONT></P>";
265 echo "</TD></TR>";
266 echo "</TABLE>";
f9903ed0 267
da5c172a 268 print_simple_box_end();
f9903ed0 269}
270
600149be 271function print_headline($text, $size=2) {
272 echo "<B><FONT SIZE=\"$size\">$text</FONT></B><BR>\n";
273}
274
275function print_recent_activity($course) {
276 // $course is an object
277 // This function trawls through the logs looking for
278 // anything new since the user's last login
279
97c270e9 280 global $CFG, $USER, $COURSE_TEACHER_COLOR;
600149be 281
282 if (! $USER->lastlogin ) {
4c654ee3 283 echo "<P ALIGN=CENTER><FONT SIZE=1>";
4b1371a7 284 print_string("welcometocourse", "", $course->shortname);
4c654ee3 285 echo "</FONT></P>";
600149be 286 return;
4c654ee3 287 } else {
288 echo "<P ALIGN=CENTER><FONT SIZE=1>";
289 echo get_string("yourlastlogin").":<BR>";
290 echo userdate($USER->lastlogin, "%A, %e %b %Y, %H:%M");
291 echo "</FONT></P>";
600149be 292 }
293
294 if (! $logs = get_records_sql("SELECT * FROM log WHERE time > '$USER->lastlogin' AND course = '$course->id' ORDER BY time ASC")) {
295 return;
296 }
297
298
299 // Firstly, have there been any new enrolments?
300
301 $heading = false;
302 $content = false;
303 foreach ($logs as $log) {
304 if ($log->module == "course" and $log->action == "enrol") {
305 if (! $heading) {
4c654ee3 306 print_headline(get_string("newusers").":");
600149be 307 $heading = true;
308 $content = true;
309 }
310 $user = get_record("user", "id", $log->info);
ef25340c 311 echo "<P><FONT SIZE=1><A HREF=\"../user/view.php?id=$user->id&course=$course->id\">$user->firstname $user->lastname</A></FONT></P>";
600149be 312 }
313 }
314
315 // Next, have there been any changes to the course structure?
316
600149be 317 foreach ($logs as $log) {
318 if ($log->module == "course") {
319 if ($log->action == "add mod" or $log->action == "update mod" or $log->action == "delete mod") {
600149be 320 $info = split(" ", $log->info);
321 $modname = get_field($info[0], "name", "id", $info[1]);
322
600149be 323 switch ($log->action) {
324 case "add mod":
27038d9f 325 $stradded = get_string("added", "moodle", get_string("modulename", $info[0]));
4c654ee3 326 $changelist["$log->info"] = array ("operation" => "add", "text" => "$stradded:<BR><A HREF=\"$CFG->wwwroot/course/$log->url\">$modname</A>");
600149be 327 break;
328 case "update mod":
27038d9f 329 $strupdated = get_string("updated", "moodle", get_string("modulename", $info[0]));
ef25340c 330 if (! $changelist["$log->info"]) {
4c654ee3 331 $changelist["$log->info"] = array ("operation" => "update", "text" => "$strupdated:<BR><A HREF=\"$CFG->wwwroot/course/$log->url\">$modname</A>");
ef25340c 332 }
600149be 333 break;
334 case "delete mod":
ef25340c 335 if ($changelist["$log->info"]["operation"] == "add") {
336 $changelist["$log->info"] = NULL;
337 } else {
27038d9f 338 $strdeleted = get_string("deletedactivity", "moodle", get_string("modulename", $info[0]));
4c654ee3 339 $changelist["$log->info"] = array ("operation" => "delete", "text" => $strdeleted);
ef25340c 340 }
600149be 341 break;
342 }
ef25340c 343 }
344 }
345 }
346
347 if ($changelist) {
348 foreach ($changelist as $changeinfo => $change) {
349 if ($change) {
350 $changes[$changeinfo] = $change;
351 }
352 }
353 if (count($changes) > 0) {
4c654ee3 354 print_headline(get_string("courseupdates").":");
ef25340c 355 $content = true;
356 foreach ($changes as $changeinfo => $change) {
357 echo "<P><FONT SIZE=1>".$change["text"]."</FONT></P>";
600149be 358 }
359 }
360 }
361
362
363 // Now all we need to know are the new posts.
364
ef25340c 365 $heading = false;
600149be 366 foreach ($logs as $log) {
367
501cdbd8 368 if ($log->module == "forum") {
600149be 369 $post = NULL;
370
36d4a9a1 371 if ($log->action == "add post") {
f9730aef 372 $post = get_record_sql("SELECT p.*, d.forum, u.firstname, u.lastname,
600149be 373 u.email, u.picture, u.id as userid
501cdbd8 374 FROM forum_discussions d, forum_posts p, user u
375 WHERE p.id = '$log->info' AND d.id = p.discussion AND p.user = u.id");
600149be 376
36d4a9a1 377 } else if ($log->action == "add discussion") {
f9730aef 378 $post = get_record_sql("SELECT p.*, d.forum, u.firstname, u.lastname,
600149be 379 u.email, u.picture, u.id as userid
501cdbd8 380 FROM forum_discussions d, forum_posts p, user u
600149be 381 WHERE d.id = '$log->info' AND d.firstpost = p.id AND p.user = u.id");
382 }
383
384 if ($post) {
f9730aef 385
386 $teacherpost = "";
387 if ($forum = get_record("forum", "id", $post->forum) ) {
388 if ($forum->type == "teacher") {
389 if (!isteacher($course->id)) {
390 continue;
391 } else {
97c270e9 392 $teacherpost = "COLOR=$COURSE_TEACHER_COLOR";
f9730aef 393 }
394 }
395 }
600149be 396 if (! $heading) {
4c654ee3 397 print_headline(get_string("newforumposts").":");
600149be 398 $heading = true;
399 $content = true;
400 }
2c0411e2 401 $date = userdate($post->modified, "%e %b, %H:%M");
402 echo "<P><FONT SIZE=1 $teacherpost>$date - $post->firstname $post->lastname<BR>";
501cdbd8 403 echo "\"<A HREF=\"$CFG->wwwroot/mod/forum/$log->url\">";
600149be 404 if ($log->action == "add") {
ef25340c 405 echo "<B>$post->subject</B>";
600149be 406 } else {
ef25340c 407 echo "$post->subject";
600149be 408 }
ef25340c 409 echo "</A>\"</FONT></P>";
600149be 410 }
411
412 }
413 }
414
415 if (! $content) {
97c270e9 416 echo "<FONT SIZE=2>".get_string("nothingnew")."</FONT>";
600149be 417 }
600149be 418}
419
e1360728 420
421function unenrol_student_in_course($user, $course) {
422 global $db;
423
424 return $db->Execute("DELETE FROM user_students WHERE user = '$user' AND course = '$course'");
425}
426
427
428
429function enrol_student_in_course($user, $course) {
430 global $db;
431
432 $timenow = time();
433
434 $rs = $db->Execute("INSERT INTO user_students (user, course, start, end, time)
435 VALUES ($user, $course, 0, 0, $timenow)");
436 if ($rs) {
437 return true;
438 } else {
439 return false;
440 }
441}
442
90845098 443function get_all_mods($courseid, &$mods, &$modnames, &$modnamesplural, &$modnamesused) {
444// Returns a number of useful structures for course displays
7468bf01 445
90845098 446 $mods = NULL; // course modules indexed by id
447 $modnames = NULL; // all course module names
94361e02 448 $modnamesplural= NULL; // all course module names (plural form)
90845098 449 $modnamesused = NULL; // course module names used
7468bf01 450
90845098 451 if ($allmods = get_records_sql("SELECT * FROM modules") ) {
452 foreach ($allmods as $mod) {
453 $modnames[$mod->name] = get_string("modulename", "$mod->name");
454 $modnamesplural[$mod->name] = get_string("modulenameplural", "$mod->name");
455 }
456 asort($modnames);
457 } else {
458 error("No modules are installed!");
459 }
460
461 if ($rawmods = get_records_sql("SELECT cm.*, m.name as modname
462 FROM modules m, course_modules cm
463 WHERE cm.course = '$courseid'
464 AND cm.deleted = '0'
94361e02 465 AND cm.module = m.id ") ) {
7468bf01 466 foreach($rawmods as $mod) { // Index the mods
467 $mods[$mod->id] = $mod;
90845098 468 $mods[$mod->id]->modfullname = $modnames[$mod->modname];
469 $modnamesused[$mod->modname] = $modnames[$mod->modname];
7468bf01 470 }
90845098 471 asort($modnamesused);
7468bf01 472 }
7468bf01 473}
474
475function get_all_sections($courseid) {
476
477 return get_records_sql("SELECT section, id, course, summary, sequence
478 FROM course_sections
479 WHERE course = '$courseid'
480 ORDER BY section");
481}
482
ba2e5d73 483function get_all_categories() {
484 return get_records_sql("SELECT * FROM course_categories ORDER by name");
485}
486
19a55d67 487function print_section($courseid, $section, $mods, $modnamesused, $absolute=false, $width="100%") {
94361e02 488 global $CFG;
489
490
19a55d67 491 echo "<TABLE WIDTH=\"$width\"><TR><TD>\n";
94361e02 492 if ($section->sequence) {
493
494 $sectionmods = explode(",", $section->sequence);
495
496 foreach ($sectionmods as $modnumber) {
497 $mod = $mods[$modnumber];
498 $instancename = get_field("$mod->modname", "name", "id", "$mod->instance");
499 echo "<IMG SRC=\"$CFG->wwwroot/mod/$mod->modname/icon.gif\" HEIGHT=16 WIDTH=16 ALT=\"$mod->modfullname\">";
a7ad3ea6 500 echo " <FONT SIZE=2><A TITLE=\"$mod->modfullname\"";
501 echo " HREF=\"$CFG->wwwroot/mod/$mod->modname/view.php?id=$mod->id\">$instancename</A></FONT>";
94361e02 502 if (isediting($courseid)) {
503 echo make_editing_buttons($mod->id, $absolute);
504 }
505 echo "<BR>\n";
506 }
507 }
47ef8795 508 echo "</TD></TR></TABLE><BR>\n\n";
a7ad3ea6 509}
510
ee1ddef6 511function print_side_block($heading="", $list=NULL, $footer="", $icons=NULL, $width=180) {
a7ad3ea6 512
19a55d67 513 echo "<TABLE WIDTH=\"$width\">\n";
a7ad3ea6 514 echo "<TR><TD COLSPAN=2><P><B><FONT SIZE=2>$heading</TD></TR>\n";
515 if ($list) {
516 foreach($list as $key => $string) {
517 echo "<TR><TD VALIGN=top WIDTH=12>";
518 if ($icons[$key]) {
519 echo $icons[$key];
520 } else {
521 echo "";
522 }
523 echo "</TD>\n<TD WIDTH=100% VALIGN=top>";
524 echo "<P><FONT SIZE=2>$string</FONT></P>";
525 echo "</TD></TR>\n";
526 }
527 }
528 if ($footer) {
529 echo "<TR><TD></TD><TD ALIGN=left><P><FONT SIZE=2>$footer</TD></TR>\n";
530 }
531 echo "</TABLE><BR>\n\n";
94361e02 532}
533
670fddf1 534function print_admin_links ($siteid, $width=180) {
2b25f2a0 535 global $THEME, $CFG;
536
19a55d67 537 print_simple_box(get_string("administration"), $align="CENTER", $width, $color="$THEME->cellheading");
2b25f2a0 538 $icon = "<IMG SRC=\"$CFG->wwwroot/pix/i/settings.gif\" HEIGHT=16 WIDTH=16 ALT=\"\">";
74944b73 539 $moddata[]="<A HREF=\"$CFG->wwwroot/admin/config.php\">".get_string("configvariables")."</A>";
2b25f2a0 540 $modicon[]=$icon;
541 $moddata[]="<A HREF=\"$CFG->wwwroot/admin/site.php\">".get_string("sitesettings")."</A>";
542 $modicon[]=$icon;
74944b73 543 $moddata[]="<A HREF=\"$CFG->wwwroot/course/log.php?id=$siteid\">".get_string("sitelogs")."</A>";
544 $modicon[]=$icon;
1e3e716f 545 $moddata[]="<A HREF=\"$CFG->wwwroot/theme/index.php\">".get_string("choosetheme")."</A>";
546 $modicon[]=$icon;
31410e9a 547 $moddata[]="<A HREF=\"$CFG->wwwroot/admin/lang.php\">".get_string("checklanguage")."</A>";
548 $modicon[]=$icon;
74944b73 549 $moddata[]="<HR>";
550 $modicon[]="";
2b25f2a0 551 $moddata[]="<A HREF=\"$CFG->wwwroot/course/edit.php\">".get_string("addnewcourse")."</A>";
552 $modicon[]=$icon;
ba2e5d73 553 $moddata[]="<A HREF=\"$CFG->wwwroot/course/categories.php\">".get_string("categories")."</A>";
554 $modicon[]=$icon;
2b25f2a0 555 $moddata[]="<A HREF=\"$CFG->wwwroot/course/teacher.php\">".get_string("assignteachers")."</A>";
556 $modicon[]=$icon;
557 $moddata[]="<A HREF=\"$CFG->wwwroot/course/delete.php\">".get_string("deletecourse")."</A>";
558 $modicon[]=$icon;
74944b73 559 $moddata[]="<HR>";
560 $modicon[]="";
2b25f2a0 561 $moddata[]="<A HREF=\"$CFG->wwwroot/admin/user.php?newuser=true\">".get_string("addnewuser")."</A>";
562 $modicon[]=$icon;
563 $moddata[]="<A HREF=\"$CFG->wwwroot/admin/user.php\">".get_string("edituser")."</A>";
564 $modicon[]=$icon;
565 $fulladmin = "<P><A HREF=\"$CFG->wwwroot/admin/\">".get_string("admin")."</A>...";
19a55d67 566 print_side_block("", $moddata, "$fulladmin", $modicon, $width);
567 echo "<IMG SRC=\"$CFG->wwwroot/pix/spacer.gif\" WIDTH=\"$width\" HEIGHT=1><BR>";
2b25f2a0 568}
569
b4d7002e 570function print_course_admin_links($course, $width=180) {
44dad735 571 global $THEME, $CFG;
572
573 echo "<BR>";
574 $adminicon[]="<IMG SRC=\"$CFG->wwwroot/pix/i/edit.gif\" HEIGHT=16 WIDTH=16 ALT=\"\">";
b4d7002e 575 if (isediting($course->id)) {
576 $admindata[]="<A HREF=\"view.php?id=$course->id&edit=off\">".get_string("turneditingoff")."</A>";
44dad735 577 } else {
b4d7002e 578 $admindata[]="<A HREF=\"view.php?id=$course->id&edit=on\">".get_string("turneditingon")."</A>";
579 }
580 $admindata[]="<A HREF=\"edit.php?id=$course->id\">".get_string("settings")."...</A>";
581 $adminicon[]="<IMG SRC=\"$CFG->wwwroot/pix/i/settings.gif\" HEIGHT=16 WIDTH=16 ALT=\"\">";
582 if (!$course->teachers) {
583 $course->teachers = get_string("defaultcourseteachers");
44dad735 584 }
b4d7002e 585 $admindata[]="<A HREF=\"teachers.php?id=$course->id\">$course->teachers...</A>";
44dad735 586 $adminicon[]="<IMG SRC=\"$CFG->wwwroot/pix/i/settings.gif\" HEIGHT=16 WIDTH=16 ALT=\"\">";
b4d7002e 587
588 $admindata[]="<A HREF=\"log.php?id=$course->id\">".get_string("logs")."...</A>";
44dad735 589 $adminicon[]="<IMG SRC=\"$CFG->wwwroot/pix/i/log.gif\" HEIGHT=16 WIDTH=16 ALT=\"\">";
b4d7002e 590 $admindata[]="<A HREF=\"$CFG->wwwroot/files/index.php?id=$course->id\">".get_string("files")."...</A>";
44dad735 591 $adminicon[]="<IMG SRC=\"$CFG->wwwroot/files/pix/files.gif\" HEIGHT=16 WIDTH=16 ALT=\"\">";
592
b4d7002e 593 $admindata[]="<A HREF=\"$CFG->wwwroot/doc/view.php?id=$course->id&file=teacher.html\">".get_string("help")."...</A>";
44dad735 594 $adminicon[]="<IMG SRC=\"$CFG->wwwroot/mod/reading/icon.gif\" HEIGHT=16 WIDTH=16 ALT=\"\">";
595
b4d7002e 596
597 if ($teacherforum = forum_get_course_forum($course->id, "teacher")) {
598 $admindata[]="<A HREF=\"$CFG->wwwroot/mod/forum/view.php?f=$teacherforum->id\">".get_string("nameteacher", "forum")."</A>";
44dad735 599 $adminicon[]="<IMG SRC=\"$CFG->wwwroot/mod/forum/icon.gif\" HEIGHT=16 WIDTH=16 ALT=\"\">";
600 }
601
19a55d67 602 print_simple_box(get_string("administration"), $align="CENTER", $width, $color="$THEME->cellheading");
603 print_side_block("", $admindata, "", $adminicon, $width);
44dad735 604}
2b25f2a0 605
ba2e5d73 606function print_course_categories($categories, $selected="none", $width=180) {
0a263205 607 global $CFG, $THEME, $USER;
ba2e5d73 608
0c656181 609 if ($selected == "index") { // Print comprehensive index of categories with courses
610 if ($courses = get_records_sql("SELECT * FROM course WHERE category > 0 ORDER BY shortname")) {
611 if (isset($USER->id) and !isadmin()) {
612 print_simple_box_start("LEFT", "100%");
613 print_heading("<A HREF=\"course/index.php?category=my\">".get_string("mycourses")."</A>", "LEFT");
614 $some = false;
615 echo "<UL>";
616 foreach ($courses as $key => $course) {
617 if (isteacher($course->id) or isstudent($course->id)) {
618 echo "<IMG SRC=\"$CFG->wwwroot/pix/i/course.gif\" HEIGHT=16 WIDTH=16>&nbsp;<A HREF=\"$CFG->wwwroot/course/view.php?id=$course->id\">$course->shortname</A> &nbsp; ";
619 $some = true;
620 }
621 }
622 if (!$some) {
623 print_string("nocoursesyet");
624 }
625 echo "</UL>";
626 print_simple_box_end();
627 print_spacer(8,1);
628 }
629 foreach ($categories as $category) {
630 print_simple_box_start("LEFT", "100%");
7ef459eb 631 print_heading("<A HREF=\"course/index.php?category=$category->id\">$category->name</A>", "LEFT");
0c656181 632 $some = false;
633 echo "<UL>";
634 foreach ($courses as $key => $course) {
635 if ($course->category == $category->id) {
636 echo "<IMG SRC=\"$CFG->wwwroot/pix/i/course.gif\" HEIGHT=16 WIDTH=16>&nbsp;<A HREF=\"$CFG->wwwroot/course/view.php?id=$course->id\">$course->shortname</A> &nbsp; ";
637 unset($courses[$key]);
638 $some = true;
639 }
640 }
641 if (!$some) {
642 print_string("nocoursesyet");
643 }
644 echo "</UL>";
645 print_simple_box_end();
646 print_spacer(8,1);
647 }
ba2e5d73 648 }
0c656181 649
650 } else { // Print short list of categories only
651 foreach ($categories as $cat) {
652 $caticon[]="<IMG SRC=\"$CFG->wwwroot/pix/i/course.gif\" HEIGHT=16 WIDTH=16>";
653 if ($cat->id == $selected) {
654 $catdata[]="$cat->name";
655 } else {
656 $catdata[]="<A HREF=\"$CFG->wwwroot/course/index.php?category=$cat->id\">$cat->name</A>";
657 }
658 }
659 $catdata[] = "<A HREF=\"$CFG->wwwroot/course/index.php?category=all\">".get_string("fulllistofcourses")."</A>";
660 if (isset($USER->id)) {
661 $catdata[] = "<A HREF=\"$CFG->wwwroot/course/index.php?category=my\">".get_string("mycourses")."</A>";
662 }
663 print_side_block("", $catdata, $showall.$mine, $caticon, $width);
ba2e5d73 664 }
ba2e5d73 665}
94361e02 666
2b8cef80 667function print_log_graph($course, $userid=0, $type="course.png", $date=0) {
668 global $CFG;
669 echo "<IMG BORDER=0 SRC=\"$CFG->wwwroot/course/loggraph.php?id=$course->id&user=$userid&type=$type&date=$date\">";
670}
671
11b0c469 672
673
674/// MODULE FUNCTIONS /////////////////////////////////////////////////////////////////
675
676function add_course_module($mod) {
677 GLOBAL $db;
678
679 $timenow = time();
680
681 if (!$rs = $db->Execute("INSERT into course_modules
682 SET course = '$mod->course',
683 module = '$mod->module',
684 instance = '$mod->instance',
685 section = '$mod->section',
686 added = '$timenow' ")) {
687 return 0;
688 }
689
690 // Get it out again - this is the most compatible way to determine the ID
691 if ($rs = $db->Execute("SELECT id FROM course_modules
692 WHERE module = $mod->module AND added = $timenow")) {
693 return $rs->fields[0];
694 } else {
695 return 0;
696 }
697
698}
699
700function add_mod_to_section($mod) {
701// Returns the course_sections ID where the mod is inserted
702 GLOBAL $db;
703
704 if ($cw = get_record_sql("SELECT * FROM course_sections
705 WHERE course = '$mod->course' AND section = '$mod->section'") ) {
706
707 if ($cw->sequence) {
708 $newsequence = "$cw->sequence,$mod->coursemodule";
709 } else {
710 $newsequence = "$mod->coursemodule";
711 }
712 if (!$rs = $db->Execute("UPDATE course_sections SET sequence = '$newsequence' WHERE id = '$cw->id'")) {
713 return 0;
714 } else {
715 return $cw->id; // Return course_sections ID that was used.
716 }
717
718 } else { // Insert a new record
719 if (!$rs = $db->Execute("INSERT into course_sections
720 SET course = '$mod->course',
721 section = '$mod->section',
722 summary = '',
723 sequence = '$mod->coursemodule' ")) {
724 return 0;
725 }
726 // Get it out again - this is the most compatible way to determine the ID
727 if ($rs = $db->Execute("SELECT id FROM course_sections
728 WHERE course = '$mod->course' AND section = '$mod->section'")) {
729 return $rs->fields[0];
730 } else {
731 return 0;
732 }
733 }
734}
735
736function delete_course_module($mod) {
737 return set_field("course_modules", "deleted", 1, "id", $mod);
738}
739
740function delete_mod_from_section($mod, $section) {
741 GLOBAL $db;
742
743 if ($cw = get_record("course_sections", "id", "$section") ) {
744
745 $modarray = explode(",", $cw->sequence);
746
747 if ($key = array_keys ($modarray, $mod)) {
748 array_splice($modarray, $key[0], 1);
749 $newsequence = implode(",", $modarray);
750 return set_field("course_sections", "sequence", $newsequence, "id", $cw->id);
751 } else {
752 return false;
753 }
754
755 } else {
756 return false;
757 }
758}
759
760
761function move_module($id, $move) {
762 GLOBAL $db;
763
764 if (!$move) {
765 return true;
766 }
767
768 if (! $cm = get_record("course_modules", "id", $id)) {
769 error("This course module doesn't exist");
770 }
771
772 if (! $thissection = get_record("course_sections", "id", $cm->section)) {
773 error("This course section doesn't exist");
774 }
775
776 $mods = explode(",", $thissection->sequence);
777
778 $len = count($mods);
779 $pos = array_keys($mods, $cm->id);
780 $thepos = $pos[0];
781
782 if ($len == 0 || count($pos) == 0 ) {
783 error("Very strange. Could not find the required module in this section.");
784 }
785
786 if ($len == 1) {
787 $first = true;
788 $last = true;
789 } else {
790 $first = ($thepos == 0);
791 $last = ($thepos == $len - 1);
792 }
793
794 if ($move < 0) { // Moving the module up
795
796 if ($first) {
797 if ($thissection->section == 1) { // First section, do nothing
798 return true;
799 } else { // Push onto end of previous section
800 $prevsectionnumber = $thissection->section - 1;
801 if (! $prevsection = get_record_sql("SELECT * FROM course_sections
802 WHERE course='$thissection->course'
803 AND section='$prevsectionnumber' ")) {
804 error("Previous section ($prevsection->id) doesn't exist");
805 }
806
807 if ($prevsection->sequence) {
808 $newsequence = "$prevsection->sequence,$cm->id";
809 } else {
810 $newsequence = "$cm->id";
811 }
812
813 if (! set_field("course_sections", "sequence", $newsequence, "id", $prevsection->id)) {
814 error("Previous section could not be updated");
815 }
816
817 if (! set_field("course_modules", "section", $prevsection->id, "id", $cm->id)) {
818 error("Module could not be updated");
819 }
820
821 array_splice($mods, 0, 1);
822 $newsequence = implode(",", $mods);
823 if (! set_field("course_sections", "sequence", $newsequence, "id", $thissection->id)) {
824 error("Module could not be updated");
825 }
826
827 return true;
828
829 }
830 } else { // move up within this section
831 $swap = $mods[$thepos-1];
832 $mods[$thepos-1] = $mods[$thepos];
833 $mods[$thepos] = $swap;
834
835 $newsequence = implode(",", $mods);
836 if (! set_field("course_sections", "sequence", $newsequence, "id", $thissection->id)) {
837 error("This section could not be updated");
838 }
839 return true;
840 }
841
842 } else { // Moving the module down
843
844 if ($last) {
845 $nextsectionnumber = $thissection->section + 1;
846 if ($nextsection = get_record_sql("SELECT * FROM course_sections
847 WHERE course='$thissection->course'
848 AND section='$nextsectionnumber' ")) {
849
850 if ($nextsection->sequence) {
851 $newsequence = "$cm->id,$nextsection->sequence";
852 } else {
853 $newsequence = "$cm->id";
854 }
855
856 if (! set_field("course_sections", "sequence", $newsequence, "id", $nextsection->id)) {
857 error("Next section could not be updated");
858 }
859
860 if (! set_field("course_modules", "section", $nextsection->id, "id", $cm->id)) {
861 error("Module could not be updated");
862 }
863
864 array_splice($mods, $thepos, 1);
865 $newsequence = implode(",", $mods);
866 if (! set_field("course_sections", "sequence", $newsequence, "id", $thissection->id)) {
867 error("This section could not be updated");
868 }
869 return true;
870
871 } else { // There is no next section, so just return
872 return true;
873
874 }
875 } else { // move down within this section
876 $swap = $mods[$thepos+1];
877 $mods[$thepos+1] = $mods[$thepos];
878 $mods[$thepos] = $swap;
879
880 $newsequence = implode(",", $mods);
881 if (! set_field("course_sections", "sequence", $newsequence, "id", $thissection->id)) {
882 error("This section could not be updated");
883 }
884 return true;
885 }
886 }
887}
888
94361e02 889function make_editing_buttons($moduleid, $absolute=false) {
890 global $CFG;
891
90845098 892 $delete = get_string("delete");
893 $moveup = get_string("moveup");
894 $movedown = get_string("movedown");
895 $update = get_string("update");
94361e02 896
897 if ($absolute) {
898 $path = "$CFG->wwwroot/course/";
899 } else {
900 $path = "";
901 }
90845098 902 return "&nbsp; &nbsp;
97c270e9 903 <A TITLE=\"$delete\" HREF=\"".$path."mod.php?delete=$moduleid\"><IMG
904 SRC=".$path."../pix/t/delete.gif BORDER=0></A>
905 <A TITLE=\"$moveup\" HREF=\"".$path."mod.php?id=$moduleid&move=-1\"><IMG
906 SRC=".$path."../pix/t/up.gif BORDER=0></A>
907 <A TITLE=\"$movedown\" HREF=\"".$path."mod.php?id=$moduleid&move=1\"><IMG
908 SRC=".$path."../pix/t/down.gif BORDER=0></A>
909 <A TITLE=\"$update\" HREF=\"".$path."mod.php?update=$moduleid\"><IMG
910 SRC=".$path."../pix/t/edit.gif BORDER=0></A>";
90845098 911}
912
f9903ed0 913?>