2 // Library of useful functions
5 $COURSE_MAX_LOG_DISPLAY = 150; // days
7 $COURSE_TEACHER_COLOR = "#990000"; // To hilight certain items that are teacher-only
9 $COURSE_LIVELOG_REFRESH = 60; // Seconds
12 function print_log_selector_form($course, $selecteduser=0, $selecteddate="today") {
16 // Get all the possible users
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";
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";
34 if ($guest = get_user_info_from_db("username", "guest")) {
35 $users["$guest->id"] = "$guest->firstname $guest->lastname";
40 if ($ccc = get_records_sql("SELECT * FROM course ORDER BY fullname")) {
41 foreach ($ccc as $cc) {
43 $courses["$cc->id"] = "$cc->fullname";
45 $courses["$cc->id"] = " $cc->fullname (Site)";
54 // Get all the possible dates
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
58 $timenow = time(); // GMT
60 // What day is it now for the user, and when is midnight that day (in GMT).
61 $timemidnight = $today = usergetmidnight($timenow);
63 // Put today up the top of the list
64 $dates = array("$timemidnight" => get_string("today").", ".userdate($timenow, "%e %B %Y") );
66 if (! $course->startdate) {
67 $course->startdate = $course->timecreated;
71 while ($timemidnight > $course->startdate and $numdates < 365) {
72 $timemidnight = $timemidnight - 86400;
73 $timenow = $timenow - 86400;
74 $dates["$timemidnight"] = userdate($timenow, "%A, %e %B %Y");
78 if ($selecteddate == "today") {
79 $selecteddate = $today;
83 echo "<FORM ACTION=log.php METHOD=get>";
85 choose_from_menu ($courses, "id", $course->id, "");
87 echo "<INPUT TYPE=hidden NAME=id VALUE=\"$course->id\">";
89 if ($course->category) {
90 choose_from_menu ($users, "user", $selecteduser, get_string("allparticipants") );
92 choose_from_menu ($dates, "date", $selecteddate, get_string("alldays"));
93 echo "<INPUT TYPE=submit VALUE=\"".get_string("showtheselogs")."\">";
98 function make_log_url($module, $url) {
106 return "/$module/$url";
109 return "/mod/$module/$url";
115 function print_log($course, $user=0, $date=0, $order="ORDER BY l.time ASC") {
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.
119 if ($course->category) {
120 $selector = "WHERE l.course='$course->id' AND l.user = u.id";
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";
132 $selector .= " AND l.user = '$user'";
136 $enddate = $date + 86400;
137 $selector .= " AND l.time > '$date' AND l.time < '$enddate'";
140 if (!$logs = get_records_sql("SELECT l.*, u.firstname, u.lastname, u.picture
141 FROM log l, user u $selector $order")){
142 notify("No logs found!");
143 print_footer($course);
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) {
154 if ($ld = get_record_sql("SELECT * FROM log_display WHERE module='$log->module' AND action='$log->action'")) {
155 $log->info = get_field($ld->mtable, $ld->field, "id", $log->info);
159 if (! $course->category) {
160 echo "<TD NOWRAP><FONT SIZE=2><A HREF=\"view.php?id=$log->course\">".$courses[$log->course]."</A></TD>";
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>";
164 echo "<TD NOWRAP><FONT SIZE=2>";
165 link_to_popup_window("$CFG->wwwroot/lib/ipatlas/plot.php?address=$log->ip&user=$log->user", "ipatlas","$log->ip", 400, 700);
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>";
169 link_to_popup_window( make_log_url($log->module,$log->url), "fromloglive","$log->module $log->action", 400, 600);
171 echo "<TD NOWRAP><FONT SIZE=2>$log->info</TD>";
178 function print_all_courses($category="all", $style="full", $maxcount=999) {
181 if ($category == "all") {
182 $courses = get_records_sql("SELECT * FROM course WHERE category > 0 ORDER BY fullname ASC");
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]);
196 $courses = get_records("course", "category", $category, "fullname ASC");
200 if ($style == "minimal") {
202 $icon = "<IMG SRC=\"pix/i/course.gif\" HEIGHT=16 WIDTH=16 ALT=\"".get_string("course")."\">";
203 foreach ($courses as $course) {
204 $moddata[]="<A TITLE=\"$course->shortname\" HREF=\"$CFG->wwwroot/course/view.php?id=$course->id\">$course->fullname</A>";
206 if ($count++ >= $maxcount) {
210 $fulllist = "<P><A HREF=\"$CFG->wwwroot/course/\">".get_string("fulllistofcourses")."</A>...";
211 print_side_block("", $moddata, "$fulllist", $modicon);
214 foreach ($courses as $course) {
215 print_course($course);
221 echo "<H3>".get_string("nocoursesyet")."</H3>";
226 function print_course($course) {
230 if (! $site = get_site()) {
231 error("Could not find a site!");
234 print_simple_box_start("CENTER", "100%");
236 echo "<TABLE WIDTH=100%>";
237 echo "<TR VALIGN=top>";
238 echo "<TD VALIGN=top WIDTH=50%>";
239 echo "<P><FONT SIZE=3><B><A TITLE=\"".get_string("entercourse")."\"
240 HREF=\"$CFG->wwwroot/course/view.php?id=$course->id\">$course->fullname</A></B></FONT></P>";
241 if ($teachers = get_course_teachers($course->id)) {
242 echo "<P><FONT SIZE=1>\n";
243 foreach ($teachers as $teacher) {
244 if ($teacher->authority > 0) {
245 if (!$teacher->role) {
246 $teacher->role = $course->teacher;
248 echo "$teacher->role: <A HREF=\"$CFG->wwwroot/user/view.php?id=$teacher->id&course=$site->id\">$teacher->firstname $teacher->lastname</A><BR>";
253 if ($course->guest) {
254 echo "<A TITLE=\"".get_string("allowguests")."\" HREF=\"$CFG->wwwroot/course/view.php?id=$course->id\">";
255 echo "<IMG VSPACE=4 ALT=\"\" HEIGHT=16 WIDTH=16 BORDER=0 SRC=\"$CFG->wwwroot/user/user.gif\"></A> ";
257 if ($course->password) {
258 echo "<A TITLE=\"".get_string("requireskey")."\" HREF=\"$CFG->wwwroot/course/view.php?id=$course->id\">";
259 echo "<IMG VSPACE=4 ALT=\"\" HEIGHT=16 WIDTH=16 BORDER=0 SRC=\"$CFG->wwwroot/pix/i/key.gif\"></A>";
263 echo "</TD><TD VALIGN=top WIDTH=50%>";
264 echo "<P><FONT SIZE=2>".text_to_html($course->summary)."</FONT></P>";
268 print_simple_box_end();
271 function print_headline($text, $size=2) {
272 echo "<B><FONT SIZE=\"$size\">$text</FONT></B><BR>\n";
275 function 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
280 global $CFG, $USER, $COURSE_TEACHER_COLOR;
282 if (! $USER->lastlogin ) {
283 echo "<P ALIGN=CENTER><FONT SIZE=1>";
284 print_string("welcometocourse", "", $course->shortname);
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");
294 if (! $logs = get_records_sql("SELECT * FROM log WHERE time > '$USER->lastlogin' AND course = '$course->id' ORDER BY time ASC")) {
299 // Firstly, have there been any new enrolments?
303 foreach ($logs as $log) {
304 if ($log->module == "course" and $log->action == "enrol") {
306 print_headline(get_string("newusers").":");
310 $user = get_record("user", "id", $log->info);
311 if (isstudent($course->id, $user->id)) {
312 echo "<P><FONT SIZE=1><A HREF=\"../user/view.php?id=$user->id&course=$course->id\">$user->firstname $user->lastname</A></FONT></P>";
317 // Next, have there been any changes to the course structure?
319 foreach ($logs as $log) {
320 if ($log->module == "course") {
321 if ($log->action == "add mod" or $log->action == "update mod" or $log->action == "delete mod") {
322 $info = split(" ", $log->info);
323 $modname = get_field($info[0], "name", "id", $info[1]);
325 switch ($log->action) {
327 $stradded = get_string("added", "moodle", get_string("modulename", $info[0]));
328 $changelist["$log->info"] = array ("operation" => "add", "text" => "$stradded:<BR><A HREF=\"$CFG->wwwroot/course/$log->url\">$modname</A>");
331 $strupdated = get_string("updated", "moodle", get_string("modulename", $info[0]));
332 if (! $changelist["$log->info"]) {
333 $changelist["$log->info"] = array ("operation" => "update", "text" => "$strupdated:<BR><A HREF=\"$CFG->wwwroot/course/$log->url\">$modname</A>");
337 if ($changelist["$log->info"]["operation"] == "add") {
338 $changelist["$log->info"] = NULL;
340 $strdeleted = get_string("deletedactivity", "moodle", get_string("modulename", $info[0]));
341 $changelist["$log->info"] = array ("operation" => "delete", "text" => $strdeleted);
350 foreach ($changelist as $changeinfo => $change) {
352 $changes[$changeinfo] = $change;
355 if (count($changes) > 0) {
356 print_headline(get_string("courseupdates").":");
358 foreach ($changes as $changeinfo => $change) {
359 echo "<P><FONT SIZE=1>".$change["text"]."</FONT></P>";
365 // Now display new things from each module
367 $mods = get_list_of_plugins("mod");
369 foreach ($mods as $mod) {
370 include_once("$CFG->dirroot/mod/$mod/lib.php");
371 $print_recent_activity = $mod."_print_recent_activity";
372 if (function_exists($print_recent_activity)) {
373 $modcontent = $print_recent_activity($logs, isteacher($course->id));
382 echo "<FONT SIZE=2>".get_string("nothingnew")."</FONT>";
387 function get_array_of_activities($courseid) {
388 // For a given course, returns an array of course activity objects
389 // Each item in the array contains he following properties:
390 // cm - course module id
391 // mod - name of the module (eg forum)
392 // section - the number of the section (eg week or topic)
393 // name - the name of the instance
397 if (!$rawmods = get_records_sql("SELECT cm.*, m.name as modname
398 FROM modules m, course_modules cm
399 WHERE cm.course = '$courseid'
401 AND cm.module = m.id ") ) {
405 if ($sections = get_records("course_sections", "course", $courseid, "section ASC")) {
406 foreach ($sections as $section) {
407 if ($section->sequence) {
408 $sequence = explode(",", $section->sequence);
409 foreach ($sequence as $seq) {
410 $mod[$seq]->cm = $rawmods[$seq]->id;
411 $mod[$seq]->mod = $rawmods[$seq]->modname;
412 $mod[$seq]->section = $section->section;
413 $mod[$seq]->name = urlencode(get_field($rawmods[$seq]->modname, "name", "id", $rawmods[$seq]->instance));
424 function get_all_mods($courseid, &$mods, &$modnames, &$modnamesplural, &$modnamesused) {
425 // Returns a number of useful structures for course displays
427 $mods = NULL; // course modules indexed by id
428 $modnames = NULL; // all course module names
429 $modnamesplural= NULL; // all course module names (plural form)
430 $modnamesused = NULL; // course module names used
432 if ($allmods = get_records_sql("SELECT * FROM modules") ) {
433 foreach ($allmods as $mod) {
434 $modnames[$mod->name] = get_string("modulename", "$mod->name");
435 $modnamesplural[$mod->name] = get_string("modulenameplural", "$mod->name");
439 error("No modules are installed!");
442 if ($rawmods = get_records_sql("SELECT cm.*, m.name as modname
443 FROM modules m, course_modules cm
444 WHERE cm.course = '$courseid'
446 AND cm.module = m.id ") ) {
447 foreach($rawmods as $mod) { // Index the mods
448 $mods[$mod->id] = $mod;
449 $mods[$mod->id]->modfullname = $modnames[$mod->modname];
450 $modnamesused[$mod->modname] = $modnames[$mod->modname];
452 asort($modnamesused);
456 function get_all_sections($courseid) {
458 return get_records_sql("SELECT section, id, course, summary, sequence
460 WHERE course = '$courseid'
464 function get_all_categories() {
465 return get_records_sql("SELECT * FROM course_categories ORDER by name");
468 function print_section($course, $section, $mods, $modnamesused, $absolute=false, $width="100%") {
471 $modinfo = unserialize($course->modinfo);
473 echo "<TABLE WIDTH=\"$width\"><TR><TD>\n";
474 if ($section->sequence) {
476 $sectionmods = explode(",", $section->sequence);
478 foreach ($sectionmods as $modnumber) {
479 $mod = $mods[$modnumber];
480 $instancename = urldecode($modinfo[$modnumber]->name);
481 echo "<IMG SRC=\"$CFG->wwwroot/mod/$mod->modname/icon.gif\" HEIGHT=16 WIDTH=16 ALT=\"$mod->modfullname\">";
482 echo " <FONT SIZE=2><A TITLE=\"$mod->modfullname\"";
483 echo " HREF=\"$CFG->wwwroot/mod/$mod->modname/view.php?id=$mod->id\">$instancename</A></FONT>";
484 if (isediting($course->id)) {
485 echo make_editing_buttons($mod->id, $absolute);
490 echo "</TD></TR></TABLE><BR>\n\n";
493 function print_side_block($heading="", $list=NULL, $footer="", $icons=NULL, $width=180) {
495 echo "<TABLE WIDTH=\"$width\">\n";
496 echo "<TR><TD COLSPAN=2><P><B><FONT SIZE=2>$heading</TD></TR>\n";
498 foreach($list as $key => $string) {
499 echo "<TR><TD VALIGN=top WIDTH=12>";
505 echo "</TD>\n<TD WIDTH=100% VALIGN=top>";
506 echo "<P><FONT SIZE=2>$string</FONT></P>";
511 echo "<TR><TD></TD><TD ALIGN=left><P><FONT SIZE=2>$footer</TD></TR>\n";
513 echo "</TABLE><BR>\n\n";
516 function print_admin_links ($siteid, $width=180) {
519 print_simple_box(get_string("administration"), $align="CENTER", $width, $color="$THEME->cellheading");
520 $icon = "<IMG SRC=\"$CFG->wwwroot/pix/i/settings.gif\" HEIGHT=16 WIDTH=16 ALT=\"\">";
521 $moddata[]="<A HREF=\"$CFG->wwwroot/admin/config.php\">".get_string("configvariables")."</A>";
523 $moddata[]="<A HREF=\"$CFG->wwwroot/admin/site.php\">".get_string("sitesettings")."</A>";
525 $moddata[]="<A HREF=\"$CFG->wwwroot/course/log.php?id=$siteid\">".get_string("sitelogs")."</A>";
527 $moddata[]="<A HREF=\"$CFG->wwwroot/theme/index.php\">".get_string("choosetheme")."</A>";
529 $moddata[]="<A HREF=\"$CFG->wwwroot/admin/lang.php\">".get_string("checklanguage")."</A>";
531 if (file_exists("$CFG->dirroot/admin/$CFG->dbtype")) {
532 $moddata[]="<A HREF=\"$CFG->wwwroot/admin/$CFG->dbtype/\">".get_string("managedatabase")."</A>";
537 $moddata[]="<A HREF=\"$CFG->wwwroot/course/edit.php\">".get_string("addnewcourse")."</A>";
539 $moddata[]="<A HREF=\"$CFG->wwwroot/course/categories.php\">".get_string("categories")."</A>";
541 $moddata[]="<A HREF=\"$CFG->wwwroot/course/teacher.php\">".get_string("assignteachers")."</A>";
543 $moddata[]="<A HREF=\"$CFG->wwwroot/course/delete.php\">".get_string("deletecourse")."</A>";
547 $moddata[]="<A HREF=\"$CFG->wwwroot/admin/user.php?newuser=true\">".get_string("addnewuser")."</A>";
549 $moddata[]="<A HREF=\"$CFG->wwwroot/admin/user.php\">".get_string("edituser")."</A>";
551 $fulladmin = "<P><A HREF=\"$CFG->wwwroot/admin/\">".get_string("admin")."</A>...";
552 print_side_block("", $moddata, "$fulladmin", $modicon, $width);
553 echo "<IMG SRC=\"$CFG->wwwroot/pix/spacer.gif\" WIDTH=\"$width\" HEIGHT=1><BR>";
556 function print_course_admin_links($course, $width=180) {
560 $adminicon[]="<IMG SRC=\"$CFG->wwwroot/pix/i/edit.gif\" HEIGHT=16 WIDTH=16 ALT=\"\">";
561 if (isediting($course->id)) {
562 $admindata[]="<A HREF=\"view.php?id=$course->id&edit=off\">".get_string("turneditingoff")."</A>";
564 $admindata[]="<A HREF=\"view.php?id=$course->id&edit=on\">".get_string("turneditingon")."</A>";
566 $admindata[]="<A HREF=\"edit.php?id=$course->id\">".get_string("settings")."...</A>";
567 $adminicon[]="<IMG SRC=\"$CFG->wwwroot/pix/i/settings.gif\" HEIGHT=16 WIDTH=16 ALT=\"\">";
568 if (!$course->teachers) {
569 $course->teachers = get_string("defaultcourseteachers");
571 $admindata[]="<A HREF=\"teachers.php?id=$course->id\">$course->teachers...</A>";
572 $adminicon[]="<IMG SRC=\"$CFG->wwwroot/pix/i/settings.gif\" HEIGHT=16 WIDTH=16 ALT=\"\">";
574 $admindata[]="<A HREF=\"grades.php?id=$course->id\">".get_string("grades")."...</A>";
575 $adminicon[]="<IMG SRC=\"$CFG->wwwroot/pix/i/grades.gif\" HEIGHT=16 WIDTH=16 ALT=\"\">";
577 $admindata[]="<A HREF=\"log.php?id=$course->id\">".get_string("logs")."...</A>";
578 $adminicon[]="<IMG SRC=\"$CFG->wwwroot/pix/i/log.gif\" HEIGHT=16 WIDTH=16 ALT=\"\">";
579 $admindata[]="<A HREF=\"$CFG->wwwroot/files/index.php?id=$course->id\">".get_string("files")."...</A>";
580 $adminicon[]="<IMG SRC=\"$CFG->wwwroot/files/pix/files.gif\" HEIGHT=16 WIDTH=16 ALT=\"\">";
582 $admindata[]="<A HREF=\"$CFG->wwwroot/doc/view.php?id=$course->id&file=teacher.html\">".get_string("help")."...</A>";
583 $adminicon[]="<IMG SRC=\"$CFG->wwwroot/mod/resource/icon.gif\" HEIGHT=16 WIDTH=16 ALT=\"\">";
586 if ($teacherforum = forum_get_course_forum($course->id, "teacher")) {
587 $admindata[]="<A HREF=\"$CFG->wwwroot/mod/forum/view.php?f=$teacherforum->id\">".get_string("nameteacher", "forum")."</A>";
588 $adminicon[]="<IMG SRC=\"$CFG->wwwroot/mod/forum/icon.gif\" HEIGHT=16 WIDTH=16 ALT=\"\">";
591 print_simple_box(get_string("administration"), $align="CENTER", $width, $color="$THEME->cellheading");
592 print_side_block("", $admindata, "", $adminicon, $width);
595 function print_course_categories($categories, $selected="none", $width=180) {
596 global $CFG, $THEME, $USER;
598 if ($selected == "index") { // Print comprehensive index of categories with courses
599 if ($courses = get_records_sql("SELECT * FROM course WHERE category > 0 ORDER BY shortname")) {
600 if (isset($USER->id) and !isadmin()) {
601 print_simple_box_start("LEFT", "100%");
602 print_heading("<A HREF=\"course/index.php?category=my\">".get_string("mycourses")."</A>", "LEFT");
605 foreach ($courses as $key => $course) {
606 if (isteacher($course->id) or isstudent($course->id)) {
607 echo "<IMG SRC=\"$CFG->wwwroot/pix/i/course.gif\" HEIGHT=16 WIDTH=16> <A HREF=\"$CFG->wwwroot/course/view.php?id=$course->id\">$course->shortname</A> ";
612 print_string("nocoursesyet");
615 print_simple_box_end();
618 foreach ($categories as $category) {
619 print_simple_box_start("CENTER", "100%");
620 print_heading("<A HREF=\"course/index.php?category=$category->id\">$category->name</A>", "LEFT");
623 foreach ($courses as $key => $course) {
624 if ($course->category == $category->id) {
625 echo "<IMG SRC=\"$CFG->wwwroot/pix/i/course.gif\" HEIGHT=16 WIDTH=16> <A HREF=\"$CFG->wwwroot/course/view.php?id=$course->id\">$course->shortname</A> ";
626 unset($courses[$key]);
631 print_string("nocoursesyet");
634 print_simple_box_end();
639 } else { // Print short list of categories only
640 foreach ($categories as $cat) {
641 $caticon[]="<IMG SRC=\"$CFG->wwwroot/pix/i/course.gif\" HEIGHT=16 WIDTH=16>";
642 if ($cat->id == $selected) {
643 $catdata[]="$cat->name";
645 $catdata[]="<A HREF=\"$CFG->wwwroot/course/index.php?category=$cat->id\">$cat->name</A>";
648 $catdata[] = "<A HREF=\"$CFG->wwwroot/course/index.php?category=all\">".get_string("fulllistofcourses")."</A>";
649 if (isset($USER->id)) {
650 $catdata[] = "<A HREF=\"$CFG->wwwroot/course/index.php?category=my\">".get_string("mycourses")."</A>";
652 print_side_block("", $catdata, $showall.$mine, $caticon, $width);
656 function print_log_graph($course, $userid=0, $type="course.png", $date=0) {
658 echo "<IMG BORDER=0 SRC=\"$CFG->wwwroot/course/loggraph.php?id=$course->id&user=$userid&type=$type&date=$date\">";
663 /// MODULE FUNCTIONS /////////////////////////////////////////////////////////////////
665 function add_course_module($mod) {
668 $mod->added = time();
670 return insert_record("course_modules", $mod);
673 function add_mod_to_section($mod) {
674 // Returns the course_sections ID where the mod is inserted
677 if ($section = get_record_sql("SELECT * FROM course_sections
678 WHERE course = '$mod->course' AND section = '$mod->section'") ) {
680 if ($section->sequence) {
681 $newsequence = "$section->sequence,$mod->coursemodule";
683 $newsequence = "$mod->coursemodule";
685 if (set_field("course_sections", "sequence", $newsequence, "id", $section->id)) {
686 return $section->id; // Return course_sections ID that was used.
691 } else { // Insert a new record
692 $section->course = $mod->course;
693 $section->section = $mod->section;
694 $section->summary = "";
695 $section->sequence = $mod->coursemodule;
696 return insert_record("course_sections", $section);
700 function delete_course_module($mod) {
701 return set_field("course_modules", "deleted", 1, "id", $mod);
704 function delete_mod_from_section($mod, $section) {
707 if ($section = get_record("course_sections", "id", "$section") ) {
709 $modarray = explode(",", $section->sequence);
711 if ($key = array_keys ($modarray, $mod)) {
712 array_splice($modarray, $key[0], 1);
713 $newsequence = implode(",", $modarray);
714 return set_field("course_sections", "sequence", $newsequence, "id", $section->id);
725 function move_module($id, $move) {
732 if (! $cm = get_record("course_modules", "id", $id)) {
733 error("This course module doesn't exist");
736 if (! $thissection = get_record("course_sections", "id", $cm->section)) {
737 error("This course section doesn't exist");
740 $mods = explode(",", $thissection->sequence);
743 $pos = array_keys($mods, $cm->id);
746 if ($len == 0 || count($pos) == 0 ) {
747 error("Very strange. Could not find the required module in this section.");
754 $first = ($thepos == 0);
755 $last = ($thepos == $len - 1);
758 if ($move < 0) { // Moving the module up
761 if ($thissection->section == 1) { // First section, do nothing
763 } else { // Push onto end of previous section
764 $prevsectionnumber = $thissection->section - 1;
765 if (! $prevsection = get_record_sql("SELECT * FROM course_sections
766 WHERE course='$thissection->course'
767 AND section='$prevsectionnumber' ")) {
768 error("Previous section ($prevsection->id) doesn't exist");
771 if ($prevsection->sequence) {
772 $newsequence = "$prevsection->sequence,$cm->id";
774 $newsequence = "$cm->id";
777 if (! set_field("course_sections", "sequence", $newsequence, "id", $prevsection->id)) {
778 error("Previous section could not be updated");
781 if (! set_field("course_modules", "section", $prevsection->id, "id", $cm->id)) {
782 error("Module could not be updated");
785 array_splice($mods, 0, 1);
786 $newsequence = implode(",", $mods);
787 if (! set_field("course_sections", "sequence", $newsequence, "id", $thissection->id)) {
788 error("Module could not be updated");
794 } else { // move up within this section
795 $swap = $mods[$thepos-1];
796 $mods[$thepos-1] = $mods[$thepos];
797 $mods[$thepos] = $swap;
799 $newsequence = implode(",", $mods);
800 if (! set_field("course_sections", "sequence", $newsequence, "id", $thissection->id)) {
801 error("This section could not be updated");
806 } else { // Moving the module down
809 $nextsectionnumber = $thissection->section + 1;
810 if ($nextsection = get_record_sql("SELECT * FROM course_sections
811 WHERE course='$thissection->course'
812 AND section='$nextsectionnumber' ")) {
814 if ($nextsection->sequence) {
815 $newsequence = "$cm->id,$nextsection->sequence";
817 $newsequence = "$cm->id";
820 if (! set_field("course_sections", "sequence", $newsequence, "id", $nextsection->id)) {
821 error("Next section could not be updated");
824 if (! set_field("course_modules", "section", $nextsection->id, "id", $cm->id)) {
825 error("Module could not be updated");
828 array_splice($mods, $thepos, 1);
829 $newsequence = implode(",", $mods);
830 if (! set_field("course_sections", "sequence", $newsequence, "id", $thissection->id)) {
831 error("This section could not be updated");
835 } else { // There is no next section, so just return
839 } else { // move down within this section
840 $swap = $mods[$thepos+1];
841 $mods[$thepos+1] = $mods[$thepos];
842 $mods[$thepos] = $swap;
844 $newsequence = implode(",", $mods);
845 if (! set_field("course_sections", "sequence", $newsequence, "id", $thissection->id)) {
846 error("This section could not be updated");
853 function make_editing_buttons($moduleid, $absolute=false) {
856 $delete = get_string("delete");
857 $moveup = get_string("moveup");
858 $movedown = get_string("movedown");
859 $update = get_string("update");
862 $path = "$CFG->wwwroot/course/";
866 return "
867 <A TITLE=\"$delete\" HREF=\"".$path."mod.php?delete=$moduleid\"><IMG
868 SRC=".$path."../pix/t/delete.gif BORDER=0></A>
869 <A TITLE=\"$moveup\" HREF=\"".$path."mod.php?id=$moduleid&move=-1\"><IMG
870 SRC=".$path."../pix/t/up.gif BORDER=0></A>
871 <A TITLE=\"$movedown\" HREF=\"".$path."mod.php?id=$moduleid&move=1\"><IMG
872 SRC=".$path."../pix/t/down.gif BORDER=0></A>
873 <A TITLE=\"$update\" HREF=\"".$path."mod.php?update=$moduleid\"><IMG
874 SRC=".$path."../pix/t/edit.gif BORDER=0></A>";