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