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