Changes to add login/logout button to the right-hand top corner of every page
[moodle.git] / course / lib.php
blob90be1537e8a02ed8f2471a35cd8a6c43f4d393f3
1 <? // $Id$
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") {
14 global $USER, $CFG;
16 // Get all the possible users
17 $users = array();
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";
36 if (isadmin()) {
37 if ($ccc = get_records_sql("SELECT * FROM course ORDER BY fullname")) {
38 foreach ($ccc as $cc) {
39 if ($cc->category) {
40 $courses["$cc->id"] = "$cc->fullname";
41 } else {
42 $courses["$cc->id"] = " $cc->fullname (Site)";
46 asort($courses);
49 asort($users);
51 // Get all the possible dates
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
55 $timenow = time(); // GMT
57 // What day is it now for the user, and when is midnight that day (in GMT).
58 $timemidnight = $today = usergetmidnight($timenow);
60 // Put today up the top of the list
61 $dates = array("$timemidnight" => get_string("today").", ".userdate($timenow, "%e %B %Y") );
63 if (! $course->startdate) {
64 $course->startdate = $course->timecreated;
67 $numdates = 1;
68 while ($timemidnight > $course->startdate and $numdates < 365) {
69 $timemidnight = $timemidnight - 86400;
70 $timenow = $timenow - 86400;
71 $dates["$timemidnight"] = userdate($timenow, "%A, %e %B %Y");
72 $numdates++;
75 if ($selecteddate == "today") {
76 $selecteddate = $today;
79 echo "<CENTER>";
80 echo "<FORM ACTION=log.php METHOD=get>";
81 if (isadmin()) {
82 choose_from_menu ($courses, "id", $course->id, "");
83 } else {
84 echo "<INPUT TYPE=hidden NAME=id VALUE=\"$course->id\">";
86 if ($course->category) {
87 choose_from_menu ($users, "user", $selecteduser, get_string("allparticipants") );
89 choose_from_menu ($dates, "date", $selecteddate, get_string("alldays"));
90 echo "<INPUT TYPE=submit VALUE=\"".get_string("showtheselogs")."\">";
91 echo "</FORM>";
92 echo "</CENTER>";
95 function 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;
112 function print_log($course, $user=0, $date=0, $order="ORDER BY l.time ASC") {
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.
116 if ($course->category) {
117 $selector = "WHERE l.course='$course->id' AND l.user = u.id";
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";
128 if ($user) {
129 $selector .= " AND l.user = '$user'";
132 if ($date) {
133 $enddate = $date + 86400;
134 $selector .= " AND l.time > '$date' AND l.time < '$enddate'";
137 if (!$logs = get_records_sql("SELECT l.*, u.firstname, u.lastname, u.picture
138 FROM log l, user u $selector $order")){
139 notify("No logs found!");
140 print_footer($course);
141 exit;
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) {
151 if ($ld = get_record_sql("SELECT * FROM log_display WHERE module='$log->module' AND action='$log->action'")) {
152 $log->info = get_field($ld->mtable, $ld->field, "id", $log->info);
155 echo "<TR NOWRAP>";
156 if (! $course->category) {
157 echo "<TD NOWRAP><FONT SIZE=2><A HREF=\"view.php?id=$log->course\">".$courses[$log->course]."</A></TD>";
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>";
164 link_to_popup_window( make_log_url($log->module,$log->url), "fromloglive","$log->module $log->action", 400, 600);
165 echo "</TD>";
166 echo "<TD NOWRAP><FONT SIZE=2>$log->info</TD>";
167 echo "</TR>";
169 echo "</TABLE>";
173 function print_all_courses($cat=1, $style="full", $maxcount=999) {
174 global $CFG;
176 if ($courses = get_records("course", "category", $cat, "fullname ASC")) {
177 if ($style == "minimal") {
178 $count = 0;
179 $icon = "<IMG SRC=\"pix/i/course.gif\" HEIGHT=16 WIDTH=16 ALT=\"".get_string("course")."\">";
180 foreach ($courses as $course) {
181 $moddata[]="<A TITLE=\"$course->shortname\" HREF=\"$CFG->wwwroot/course/view.php?id=$course->id\">$course->fullname</A>";
182 $modicon[]=$icon;
183 if ($count++ >= $maxcount) {
184 break;
187 $fulllist = "<P><A HREF=\"$CFG->wwwroot/course/\">".get_string("fulllistofcourses")."</A>...";
188 print_side_block("", $moddata, "$fulllist", $modicon);
190 } else {
191 foreach ($courses as $course) {
192 print_course($course);
193 echo "<BR>\n";
197 } else {
198 echo "<H3>No courses have been defined yet</H3>";
203 function print_course($course) {
205 global $CFG;
207 if (! $site = get_site()) {
208 error("Could not find a site!");
211 print_simple_box_start("CENTER", "100%");
213 echo "<TABLE WIDTH=100%>";
214 echo "<TR VALIGN=top>";
215 echo "<TD VALIGN=top WIDTH=50%>";
216 echo "<P><FONT SIZE=3><B><A TITLE=\"".get_string("entercourse")."\"
217 HREF=\"$CFG->wwwroot/course/view.php?id=$course->id\">$course->fullname</A></B></FONT></P>";
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'
220 ORDER BY t.authority ASC")) {
222 echo "<P><FONT SIZE=1>\n";
223 foreach ($teachers as $teacher) {
224 echo "$course->teacher: <A HREF=\"$CFG->wwwroot/user/view.php?id=$teacher->id&course=$site->id\">$teacher->firstname $teacher->lastname</A><BR>";
226 echo "</FONT></P>";
228 if ($course->guest or ($course->password == "")) {
229 echo "<A TITLE=\"".get_string("allowguests")."\" HREF=\"$CFG->wwwroot/course/view.php?id=$course->id\">";
230 echo "<IMG VSPACE=4 ALT=\"\" HEIGHT=16 WIDTH=16 BORDER=0 SRC=\"$CFG->wwwroot/user/user.gif\"></A>&nbsp;&nbsp;";
232 if ($course->password) {
233 echo "<A TITLE=\"".get_string("requireskey")."\" HREF=\"$CFG->wwwroot/course/view.php?id=$course->id\">";
234 echo "<IMG VSPACE=4 ALT=\"\" HEIGHT=16 WIDTH=16 BORDER=0 SRC=\"$CFG->wwwroot/pix/i/key.gif\"></A>";
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>";
243 print_simple_box_end();
246 function print_headline($text, $size=2) {
247 echo "<B><FONT SIZE=\"$size\">$text</FONT></B><BR>\n";
250 function 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
255 global $CFG, $USER, $COURSE_TEACHER_COLOR;
257 if (! $USER->lastlogin ) {
258 echo "<P ALIGN=CENTER><FONT SIZE=1>";
259 print_string("welcometocourse", "", $course->shortname);
260 echo "</FONT></P>";
261 return;
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>";
269 if (! $logs = get_records_sql("SELECT * FROM log WHERE time > '$USER->lastlogin' AND course = '$course->id' ORDER BY time ASC")) {
270 return;
274 // Firstly, have there been any new enrolments?
276 $heading = false;
277 $content = false;
278 foreach ($logs as $log) {
279 if ($log->module == "course" and $log->action == "enrol") {
280 if (! $heading) {
281 print_headline(get_string("newusers").":");
282 $heading = true;
283 $content = true;
285 $user = get_record("user", "id", $log->info);
286 echo "<P><FONT SIZE=1><A HREF=\"../user/view.php?id=$user->id&course=$course->id\">$user->firstname $user->lastname</A></FONT></P>";
290 // Next, have there been any changes to the course structure?
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") {
295 $info = split(" ", $log->info);
296 $modname = get_field($info[0], "name", "id", $info[1]);
298 switch ($log->action) {
299 case "add mod":
300 $stradded = get_string("added", "moodle", get_string("modulename", $info[0]));
301 $changelist["$log->info"] = array ("operation" => "add", "text" => "$stradded:<BR><A HREF=\"$CFG->wwwroot/course/$log->url\">$modname</A>");
302 break;
303 case "update mod":
304 $strupdated = get_string("updated", "moodle", get_string("modulename", $info[0]));
305 if (! $changelist["$log->info"]) {
306 $changelist["$log->info"] = array ("operation" => "update", "text" => "$strupdated:<BR><A HREF=\"$CFG->wwwroot/course/$log->url\">$modname</A>");
308 break;
309 case "delete mod":
310 if ($changelist["$log->info"]["operation"] == "add") {
311 $changelist["$log->info"] = NULL;
312 } else {
313 $strdeleted = get_string("deletedactivity", "moodle", get_string("modulename", $info[0]));
314 $changelist["$log->info"] = array ("operation" => "delete", "text" => $strdeleted);
316 break;
322 if ($changelist) {
323 foreach ($changelist as $changeinfo => $change) {
324 if ($change) {
325 $changes[$changeinfo] = $change;
328 if (count($changes) > 0) {
329 print_headline(get_string("courseupdates").":");
330 $content = true;
331 foreach ($changes as $changeinfo => $change) {
332 echo "<P><FONT SIZE=1>".$change["text"]."</FONT></P>";
338 // Now all we need to know are the new posts.
340 $heading = false;
341 foreach ($logs as $log) {
343 if ($log->module == "forum") {
344 $post = NULL;
346 if ($log->action == "add post") {
347 $post = get_record_sql("SELECT p.*, d.forum, u.firstname, u.lastname,
348 u.email, u.picture, u.id as userid
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");
352 } else if ($log->action == "add discussion") {
353 $post = get_record_sql("SELECT p.*, d.forum, u.firstname, u.lastname,
354 u.email, u.picture, u.id as userid
355 FROM forum_discussions d, forum_posts p, user u
356 WHERE d.id = '$log->info' AND d.firstpost = p.id AND p.user = u.id");
359 if ($post) {
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 {
367 $teacherpost = "COLOR=$COURSE_TEACHER_COLOR";
371 if (! $heading) {
372 print_headline(get_string("newforumposts").":");
373 $heading = true;
374 $content = true;
376 $date = userdate($post->modified, "%e %b, %H:%M");
377 echo "<P><FONT SIZE=1 $teacherpost>$date - $post->firstname $post->lastname<BR>";
378 echo "\"<A HREF=\"$CFG->wwwroot/mod/forum/$log->url\">";
379 if ($log->action == "add") {
380 echo "<B>$post->subject</B>";
381 } else {
382 echo "$post->subject";
384 echo "</A>\"</FONT></P>";
390 if (! $content) {
391 echo "<FONT SIZE=2>".get_string("nothingnew")."</FONT>";
396 function unenrol_student_in_course($user, $course) {
397 global $db;
399 return $db->Execute("DELETE FROM user_students WHERE user = '$user' AND course = '$course'");
404 function enrol_student_in_course($user, $course) {
405 global $db;
407 $timenow = time();
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;
418 function get_all_mods($courseid, &$mods, &$modnames, &$modnamesplural, &$modnamesused) {
419 // Returns a number of useful structures for course displays
421 $mods = NULL; // course modules indexed by id
422 $modnames = NULL; // all course module names
423 $modnamesplural= NULL; // all course module names (plural form)
424 $modnamesused = NULL; // course module names used
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");
431 asort($modnames);
432 } else {
433 error("No modules are installed!");
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'
440 AND cm.module = m.id ") ) {
441 foreach($rawmods as $mod) { // Index the mods
442 $mods[$mod->id] = $mod;
443 $mods[$mod->id]->modfullname = $modnames[$mod->modname];
444 $modnamesused[$mod->modname] = $modnames[$mod->modname];
446 asort($modnamesused);
450 function get_all_sections($courseid) {
452 return get_records_sql("SELECT section, id, course, summary, sequence
453 FROM course_sections
454 WHERE course = '$courseid'
455 ORDER BY section");
458 function print_section($courseid, $section, $mods, $modnamesused, $absolute=false) {
459 global $CFG;
462 echo "<TABLE WIDTH=100%><TR><TD>\n";
463 if ($section->sequence) {
465 $sectionmods = explode(",", $section->sequence);
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\">";
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>";
473 if (isediting($courseid)) {
474 echo make_editing_buttons($mod->id, $absolute);
476 echo "<BR>\n";
479 echo "</TD></TR></TABLE><BR>\n\n";
482 function print_side_block($heading="", $list=NULL, $footer="", $icons=NULL) {
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 "";
494 echo "</TD>\n<TD WIDTH=100% VALIGN=top>";
495 echo "<P><FONT SIZE=2>$string</FONT></P>";
496 echo "</TD></TR>\n";
499 if ($footer) {
500 echo "<TR><TD></TD><TD ALIGN=left><P><FONT SIZE=2>$footer</TD></TR>\n";
502 echo "</TABLE><BR>\n\n";
505 function print_admin_links ($siteid) {
506 global $THEME, $CFG;
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=\"\">";
510 $moddata[]="<A HREF=\"$CFG->wwwroot/course/log.php?id=$siteid\">".get_string("sitelogs")."</A>";
511 $modicon[]=$icon;
512 $moddata[]="<A HREF=\"$CFG->wwwroot/admin/site.php\">".get_string("sitesettings")."</A>";
513 $modicon[]=$icon;
514 $moddata[]="<A HREF=\"$CFG->wwwroot/course/edit.php\">".get_string("addnewcourse")."</A>";
515 $modicon[]=$icon;
516 $moddata[]="<A HREF=\"$CFG->wwwroot/course/teacher.php\">".get_string("assignteachers")."</A>";
517 $modicon[]=$icon;
518 $moddata[]="<A HREF=\"$CFG->wwwroot/course/delete.php\">".get_string("deletecourse")."</A>";
519 $modicon[]=$icon;
520 $moddata[]="<A HREF=\"$CFG->wwwroot/admin/user.php?newuser=true\">".get_string("addnewuser")."</A>";
521 $modicon[]=$icon;
522 $moddata[]="<A HREF=\"$CFG->wwwroot/admin/user.php\">".get_string("edituser")."</A>";
523 $modicon[]=$icon;
524 $fulladmin = "<P><A HREF=\"$CFG->wwwroot/admin/\">".get_string("admin")."</A>...";
525 print_side_block("", $moddata, "$fulladmin", $modicon);
526 echo "<IMG SRC=\"$CFG->wwwroot/pix/spacer.gif\" WIDTH=200 HEIGHT=0><BR>";
531 function print_log_graph($course, $userid=0, $type="course.png", $date=0) {
532 global $CFG;
533 echo "<IMG BORDER=0 SRC=\"$CFG->wwwroot/course/loggraph.php?id=$course->id&user=$userid&type=$type&date=$date\">";
538 /// MODULE FUNCTIONS /////////////////////////////////////////////////////////////////
540 function add_course_module($mod) {
541 GLOBAL $db;
543 $timenow = time();
545 if (!$rs = $db->Execute("INSERT into course_modules
546 SET course = '$mod->course',
547 module = '$mod->module',
548 instance = '$mod->instance',
549 section = '$mod->section',
550 added = '$timenow' ")) {
551 return 0;
554 // Get it out again - this is the most compatible way to determine the ID
555 if ($rs = $db->Execute("SELECT id FROM course_modules
556 WHERE module = $mod->module AND added = $timenow")) {
557 return $rs->fields[0];
558 } else {
559 return 0;
564 function add_mod_to_section($mod) {
565 // Returns the course_sections ID where the mod is inserted
566 GLOBAL $db;
568 if ($cw = get_record_sql("SELECT * FROM course_sections
569 WHERE course = '$mod->course' AND section = '$mod->section'") ) {
571 if ($cw->sequence) {
572 $newsequence = "$cw->sequence,$mod->coursemodule";
573 } else {
574 $newsequence = "$mod->coursemodule";
576 if (!$rs = $db->Execute("UPDATE course_sections SET sequence = '$newsequence' WHERE id = '$cw->id'")) {
577 return 0;
578 } else {
579 return $cw->id; // Return course_sections ID that was used.
582 } else { // Insert a new record
583 if (!$rs = $db->Execute("INSERT into course_sections
584 SET course = '$mod->course',
585 section = '$mod->section',
586 summary = '',
587 sequence = '$mod->coursemodule' ")) {
588 return 0;
590 // Get it out again - this is the most compatible way to determine the ID
591 if ($rs = $db->Execute("SELECT id FROM course_sections
592 WHERE course = '$mod->course' AND section = '$mod->section'")) {
593 return $rs->fields[0];
594 } else {
595 return 0;
600 function delete_course_module($mod) {
601 return set_field("course_modules", "deleted", 1, "id", $mod);
604 function delete_mod_from_section($mod, $section) {
605 GLOBAL $db;
607 if ($cw = get_record("course_sections", "id", "$section") ) {
609 $modarray = explode(",", $cw->sequence);
611 if ($key = array_keys ($modarray, $mod)) {
612 array_splice($modarray, $key[0], 1);
613 $newsequence = implode(",", $modarray);
614 return set_field("course_sections", "sequence", $newsequence, "id", $cw->id);
615 } else {
616 return false;
619 } else {
620 return false;
625 function move_module($id, $move) {
626 GLOBAL $db;
628 if (!$move) {
629 return true;
632 if (! $cm = get_record("course_modules", "id", $id)) {
633 error("This course module doesn't exist");
636 if (! $thissection = get_record("course_sections", "id", $cm->section)) {
637 error("This course section doesn't exist");
640 $mods = explode(",", $thissection->sequence);
642 $len = count($mods);
643 $pos = array_keys($mods, $cm->id);
644 $thepos = $pos[0];
646 if ($len == 0 || count($pos) == 0 ) {
647 error("Very strange. Could not find the required module in this section.");
650 if ($len == 1) {
651 $first = true;
652 $last = true;
653 } else {
654 $first = ($thepos == 0);
655 $last = ($thepos == $len - 1);
658 if ($move < 0) { // Moving the module up
660 if ($first) {
661 if ($thissection->section == 1) { // First section, do nothing
662 return true;
663 } else { // Push onto end of previous section
664 $prevsectionnumber = $thissection->section - 1;
665 if (! $prevsection = get_record_sql("SELECT * FROM course_sections
666 WHERE course='$thissection->course'
667 AND section='$prevsectionnumber' ")) {
668 error("Previous section ($prevsection->id) doesn't exist");
671 if ($prevsection->sequence) {
672 $newsequence = "$prevsection->sequence,$cm->id";
673 } else {
674 $newsequence = "$cm->id";
677 if (! set_field("course_sections", "sequence", $newsequence, "id", $prevsection->id)) {
678 error("Previous section could not be updated");
681 if (! set_field("course_modules", "section", $prevsection->id, "id", $cm->id)) {
682 error("Module could not be updated");
685 array_splice($mods, 0, 1);
686 $newsequence = implode(",", $mods);
687 if (! set_field("course_sections", "sequence", $newsequence, "id", $thissection->id)) {
688 error("Module could not be updated");
691 return true;
694 } else { // move up within this section
695 $swap = $mods[$thepos-1];
696 $mods[$thepos-1] = $mods[$thepos];
697 $mods[$thepos] = $swap;
699 $newsequence = implode(",", $mods);
700 if (! set_field("course_sections", "sequence", $newsequence, "id", $thissection->id)) {
701 error("This section could not be updated");
703 return true;
706 } else { // Moving the module down
708 if ($last) {
709 $nextsectionnumber = $thissection->section + 1;
710 if ($nextsection = get_record_sql("SELECT * FROM course_sections
711 WHERE course='$thissection->course'
712 AND section='$nextsectionnumber' ")) {
714 if ($nextsection->sequence) {
715 $newsequence = "$cm->id,$nextsection->sequence";
716 } else {
717 $newsequence = "$cm->id";
720 if (! set_field("course_sections", "sequence", $newsequence, "id", $nextsection->id)) {
721 error("Next section could not be updated");
724 if (! set_field("course_modules", "section", $nextsection->id, "id", $cm->id)) {
725 error("Module could not be updated");
728 array_splice($mods, $thepos, 1);
729 $newsequence = implode(",", $mods);
730 if (! set_field("course_sections", "sequence", $newsequence, "id", $thissection->id)) {
731 error("This section could not be updated");
733 return true;
735 } else { // There is no next section, so just return
736 return true;
739 } else { // move down within this section
740 $swap = $mods[$thepos+1];
741 $mods[$thepos+1] = $mods[$thepos];
742 $mods[$thepos] = $swap;
744 $newsequence = implode(",", $mods);
745 if (! set_field("course_sections", "sequence", $newsequence, "id", $thissection->id)) {
746 error("This section could not be updated");
748 return true;
753 function make_editing_buttons($moduleid, $absolute=false) {
754 global $CFG;
756 $delete = get_string("delete");
757 $moveup = get_string("moveup");
758 $movedown = get_string("movedown");
759 $update = get_string("update");
761 if ($absolute) {
762 $path = "$CFG->wwwroot/course/";
763 } else {
764 $path = "";
766 return "&nbsp; &nbsp;
767 <A TITLE=\"$delete\" HREF=\"".$path."mod.php?delete=$moduleid\"><IMG
768 SRC=".$path."../pix/t/delete.gif BORDER=0></A>
769 <A TITLE=\"$moveup\" HREF=\"".$path."mod.php?id=$moduleid&move=-1\"><IMG
770 SRC=".$path."../pix/t/up.gif BORDER=0></A>
771 <A TITLE=\"$movedown\" HREF=\"".$path."mod.php?id=$moduleid&move=1\"><IMG
772 SRC=".$path."../pix/t/down.gif BORDER=0></A>
773 <A TITLE=\"$update\" HREF=\"".$path."mod.php?update=$moduleid\"><IMG
774 SRC=".$path."../pix/t/edit.gif BORDER=0></A>";