Tweak teacher colour
[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 ) {
265 echo "<P>Welcome to the course! Here you will find a list of what's new since your last login.</P>";
266 return;
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) {
ef25340c 281 print_headline("New users:");
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":
ef25340c 300 $changelist["$log->info"] = array ("operation" => "add", "text" => "Added a ".$info[0].":<BR><A HREF=\"$CFG->wwwroot/course/$log->url\">$modname</A>");
600149be 301 break;
302 case "update mod":
ef25340c 303 if (! $changelist["$log->info"]) {
304 $changelist["$log->info"] = array ("operation" => "update", "text" => "Updated the ".$info[0].":<BR><A HREF=\"$CFG->wwwroot/course/$log->url\">$modname</A>");
305 }
600149be 306 break;
307 case "delete mod":
ef25340c 308 if ($changelist["$log->info"]["operation"] == "add") {
309 $changelist["$log->info"] = NULL;
310 } else {
311 $changelist["$log->info"] = array ("operation" => "delete", "text" => "Deleted a ".$info[0]);
312 }
600149be 313 break;
314 }
ef25340c 315 }
316 }
317 }
318
319 if ($changelist) {
320 foreach ($changelist as $changeinfo => $change) {
321 if ($change) {
322 $changes[$changeinfo] = $change;
323 }
324 }
325 if (count($changes) > 0) {
326 print_headline("Course changes:");
327 $content = true;
328 foreach ($changes as $changeinfo => $change) {
329 echo "<P><FONT SIZE=1>".$change["text"]."</FONT></P>";
600149be 330 }
331 }
332 }
333
334
335 // Now all we need to know are the new posts.
336
ef25340c 337 $heading = false;
600149be 338 foreach ($logs as $log) {
339
501cdbd8 340 if ($log->module == "forum") {
600149be 341 $post = NULL;
342
36d4a9a1 343 if ($log->action == "add post") {
f9730aef 344 $post = get_record_sql("SELECT p.*, d.forum, u.firstname, u.lastname,
600149be 345 u.email, u.picture, u.id as userid
501cdbd8 346 FROM forum_discussions d, forum_posts p, user u
347 WHERE p.id = '$log->info' AND d.id = p.discussion AND p.user = u.id");
600149be 348
36d4a9a1 349 } else if ($log->action == "add discussion") {
f9730aef 350 $post = get_record_sql("SELECT p.*, d.forum, u.firstname, u.lastname,
600149be 351 u.email, u.picture, u.id as userid
501cdbd8 352 FROM forum_discussions d, forum_posts p, user u
600149be 353 WHERE d.id = '$log->info' AND d.firstpost = p.id AND p.user = u.id");
354 }
355
356 if ($post) {
f9730aef 357
358 $teacherpost = "";
359 if ($forum = get_record("forum", "id", $post->forum) ) {
360 if ($forum->type == "teacher") {
361 if (!isteacher($course->id)) {
362 continue;
363 } else {
87ffed0d 364 $teacherpost = "COLOR=$COURSETEACHERCOLOR";
f9730aef 365 }
366 }
367 }
600149be 368 if (! $heading) {
ef25340c 369 print_headline("Discussion Posts:");
600149be 370 $heading = true;
371 $content = true;
372 }
2c0411e2 373 $date = userdate($post->modified, "%e %b, %H:%M");
374 echo "<P><FONT SIZE=1 $teacherpost>$date - $post->firstname $post->lastname<BR>";
501cdbd8 375 echo "\"<A HREF=\"$CFG->wwwroot/mod/forum/$log->url\">";
600149be 376 if ($log->action == "add") {
ef25340c 377 echo "<B>$post->subject</B>";
600149be 378 } else {
ef25340c 379 echo "$post->subject";
600149be 380 }
ef25340c 381 echo "</A>\"</FONT></P>";
600149be 382 }
383
384 }
385 }
386
387 if (! $content) {
388 echo "<FONT SIZE=2>Nothing new since your last login</FONT>";
389 }
390
391}
392
e1360728 393
394function unenrol_student_in_course($user, $course) {
395 global $db;
396
397 return $db->Execute("DELETE FROM user_students WHERE user = '$user' AND course = '$course'");
398}
399
400
401
402function enrol_student_in_course($user, $course) {
403 global $db;
404
405 $timenow = time();
406
407 $rs = $db->Execute("INSERT INTO user_students (user, course, start, end, time)
408 VALUES ($user, $course, 0, 0, $timenow)");
409 if ($rs) {
410 return true;
411 } else {
412 return false;
413 }
414}
415
90845098 416function get_all_mods($courseid, &$mods, &$modnames, &$modnamesplural, &$modnamesused) {
417// Returns a number of useful structures for course displays
7468bf01 418
90845098 419 $mods = NULL; // course modules indexed by id
420 $modnames = NULL; // all course module names
94361e02 421 $modnamesplural= NULL; // all course module names (plural form)
90845098 422 $modnamesused = NULL; // course module names used
7468bf01 423
90845098 424 if ($allmods = get_records_sql("SELECT * FROM modules") ) {
425 foreach ($allmods as $mod) {
426 $modnames[$mod->name] = get_string("modulename", "$mod->name");
427 $modnamesplural[$mod->name] = get_string("modulenameplural", "$mod->name");
428 }
429 asort($modnames);
430 } else {
431 error("No modules are installed!");
432 }
433
434 if ($rawmods = get_records_sql("SELECT cm.*, m.name as modname
435 FROM modules m, course_modules cm
436 WHERE cm.course = '$courseid'
437 AND cm.deleted = '0'
94361e02 438 AND cm.module = m.id ") ) {
7468bf01 439 foreach($rawmods as $mod) { // Index the mods
440 $mods[$mod->id] = $mod;
90845098 441 $mods[$mod->id]->modfullname = $modnames[$mod->modname];
442 $modnamesused[$mod->modname] = $modnames[$mod->modname];
7468bf01 443 }
90845098 444 asort($modnamesused);
7468bf01 445 }
7468bf01 446}
447
448function get_all_sections($courseid) {
449
450 return get_records_sql("SELECT section, id, course, summary, sequence
451 FROM course_sections
452 WHERE course = '$courseid'
453 ORDER BY section");
454}
455
94361e02 456function print_section($courseid, $section, $mods, $modnamesused, $absolute=false) {
457 global $CFG;
458
459
a7ad3ea6 460 echo "<TABLE WIDTH=100%><TR><TD>\n";
94361e02 461 if ($section->sequence) {
462
463 $sectionmods = explode(",", $section->sequence);
464
465 foreach ($sectionmods as $modnumber) {
466 $mod = $mods[$modnumber];
467 $instancename = get_field("$mod->modname", "name", "id", "$mod->instance");
468 echo "<IMG SRC=\"$CFG->wwwroot/mod/$mod->modname/icon.gif\" HEIGHT=16 WIDTH=16 ALT=\"$mod->modfullname\">";
a7ad3ea6 469 echo " <FONT SIZE=2><A TITLE=\"$mod->modfullname\"";
470 echo " HREF=\"$CFG->wwwroot/mod/$mod->modname/view.php?id=$mod->id\">$instancename</A></FONT>";
94361e02 471 if (isediting($courseid)) {
472 echo make_editing_buttons($mod->id, $absolute);
473 }
474 echo "<BR>\n";
475 }
476 }
47ef8795 477 echo "</TD></TR></TABLE><BR>\n\n";
a7ad3ea6 478}
479
480function print_side_block($heading="", $list=NULL, $footer="", $icons=NULL) {
481
482 echo "<TABLE WIDTH=100%>\n";
483 echo "<TR><TD COLSPAN=2><P><B><FONT SIZE=2>$heading</TD></TR>\n";
484 if ($list) {
485 foreach($list as $key => $string) {
486 echo "<TR><TD VALIGN=top WIDTH=12>";
487 if ($icons[$key]) {
488 echo $icons[$key];
489 } else {
490 echo "";
491 }
492 echo "</TD>\n<TD WIDTH=100% VALIGN=top>";
493 echo "<P><FONT SIZE=2>$string</FONT></P>";
494 echo "</TD></TR>\n";
495 }
496 }
497 if ($footer) {
498 echo "<TR><TD></TD><TD ALIGN=left><P><FONT SIZE=2>$footer</TD></TR>\n";
499 }
500 echo "</TABLE><BR>\n\n";
94361e02 501}
502
adecd142 503function print_admin_links ($siteid) {
2b25f2a0 504 global $THEME, $CFG;
505
506 print_simple_box(get_string("administration"), $align="CENTER", $width="100%", $color="$THEME->cellheading");
507 $icon = "<IMG SRC=\"$CFG->wwwroot/pix/i/settings.gif\" HEIGHT=16 WIDTH=16 ALT=\"\">";
adecd142 508 $moddata[]="<A HREF=\"$CFG->wwwroot/course/log.php?id=$siteid\">".get_string("sitelogs")."</A>";
2b25f2a0 509 $modicon[]=$icon;
510 $moddata[]="<A HREF=\"$CFG->wwwroot/admin/site.php\">".get_string("sitesettings")."</A>";
511 $modicon[]=$icon;
512 $moddata[]="<A HREF=\"$CFG->wwwroot/course/edit.php\">".get_string("addnewcourse")."</A>";
513 $modicon[]=$icon;
514 $moddata[]="<A HREF=\"$CFG->wwwroot/course/teacher.php\">".get_string("assignteachers")."</A>";
515 $modicon[]=$icon;
516 $moddata[]="<A HREF=\"$CFG->wwwroot/course/delete.php\">".get_string("deletecourse")."</A>";
517 $modicon[]=$icon;
518 $moddata[]="<A HREF=\"$CFG->wwwroot/admin/user.php?newuser=true\">".get_string("addnewuser")."</A>";
519 $modicon[]=$icon;
520 $moddata[]="<A HREF=\"$CFG->wwwroot/admin/user.php\">".get_string("edituser")."</A>";
521 $modicon[]=$icon;
522 $fulladmin = "<P><A HREF=\"$CFG->wwwroot/admin/\">".get_string("admin")."</A>...";
523 print_side_block("", $moddata, "$fulladmin", $modicon);
524 echo "<IMG SRC=\"$CFG->wwwroot/pix/spacer.gif\" WIDTH=200 HEIGHT=0><BR>";
525}
526
527
94361e02 528
2b8cef80 529function print_log_graph($course, $userid=0, $type="course.png", $date=0) {
530 global $CFG;
531 echo "<IMG BORDER=0 SRC=\"$CFG->wwwroot/course/loggraph.php?id=$course->id&user=$userid&type=$type&date=$date\">";
532}
533
11b0c469 534
535
536/// MODULE FUNCTIONS /////////////////////////////////////////////////////////////////
537
538function add_course_module($mod) {
539 GLOBAL $db;
540
541 $timenow = time();
542
543 if (!$rs = $db->Execute("INSERT into course_modules
544 SET course = '$mod->course',
545 module = '$mod->module',
546 instance = '$mod->instance',
547 section = '$mod->section',
548 added = '$timenow' ")) {
549 return 0;
550 }
551
552 // Get it out again - this is the most compatible way to determine the ID
553 if ($rs = $db->Execute("SELECT id FROM course_modules
554 WHERE module = $mod->module AND added = $timenow")) {
555 return $rs->fields[0];
556 } else {
557 return 0;
558 }
559
560}
561
562function add_mod_to_section($mod) {
563// Returns the course_sections ID where the mod is inserted
564 GLOBAL $db;
565
566 if ($cw = get_record_sql("SELECT * FROM course_sections
567 WHERE course = '$mod->course' AND section = '$mod->section'") ) {
568
569 if ($cw->sequence) {
570 $newsequence = "$cw->sequence,$mod->coursemodule";
571 } else {
572 $newsequence = "$mod->coursemodule";
573 }
574 if (!$rs = $db->Execute("UPDATE course_sections SET sequence = '$newsequence' WHERE id = '$cw->id'")) {
575 return 0;
576 } else {
577 return $cw->id; // Return course_sections ID that was used.
578 }
579
580 } else { // Insert a new record
581 if (!$rs = $db->Execute("INSERT into course_sections
582 SET course = '$mod->course',
583 section = '$mod->section',
584 summary = '',
585 sequence = '$mod->coursemodule' ")) {
586 return 0;
587 }
588 // Get it out again - this is the most compatible way to determine the ID
589 if ($rs = $db->Execute("SELECT id FROM course_sections
590 WHERE course = '$mod->course' AND section = '$mod->section'")) {
591 return $rs->fields[0];
592 } else {
593 return 0;
594 }
595 }
596}
597
598function delete_course_module($mod) {
599 return set_field("course_modules", "deleted", 1, "id", $mod);
600}
601
602function delete_mod_from_section($mod, $section) {
603 GLOBAL $db;
604
605 if ($cw = get_record("course_sections", "id", "$section") ) {
606
607 $modarray = explode(",", $cw->sequence);
608
609 if ($key = array_keys ($modarray, $mod)) {
610 array_splice($modarray, $key[0], 1);
611 $newsequence = implode(",", $modarray);
612 return set_field("course_sections", "sequence", $newsequence, "id", $cw->id);
613 } else {
614 return false;
615 }
616
617 } else {
618 return false;
619 }
620}
621
622
623function move_module($id, $move) {
624 GLOBAL $db;
625
626 if (!$move) {
627 return true;
628 }
629
630 if (! $cm = get_record("course_modules", "id", $id)) {
631 error("This course module doesn't exist");
632 }
633
634 if (! $thissection = get_record("course_sections", "id", $cm->section)) {
635 error("This course section doesn't exist");
636 }
637
638 $mods = explode(",", $thissection->sequence);
639
640 $len = count($mods);
641 $pos = array_keys($mods, $cm->id);
642 $thepos = $pos[0];
643
644 if ($len == 0 || count($pos) == 0 ) {
645 error("Very strange. Could not find the required module in this section.");
646 }
647
648 if ($len == 1) {
649 $first = true;
650 $last = true;
651 } else {
652 $first = ($thepos == 0);
653 $last = ($thepos == $len - 1);
654 }
655
656 if ($move < 0) { // Moving the module up
657
658 if ($first) {
659 if ($thissection->section == 1) { // First section, do nothing
660 return true;
661 } else { // Push onto end of previous section
662 $prevsectionnumber = $thissection->section - 1;
663 if (! $prevsection = get_record_sql("SELECT * FROM course_sections
664 WHERE course='$thissection->course'
665 AND section='$prevsectionnumber' ")) {
666 error("Previous section ($prevsection->id) doesn't exist");
667 }
668
669 if ($prevsection->sequence) {
670 $newsequence = "$prevsection->sequence,$cm->id";
671 } else {
672 $newsequence = "$cm->id";
673 }
674
675 if (! set_field("course_sections", "sequence", $newsequence, "id", $prevsection->id)) {
676 error("Previous section could not be updated");
677 }
678
679 if (! set_field("course_modules", "section", $prevsection->id, "id", $cm->id)) {
680 error("Module could not be updated");
681 }
682
683 array_splice($mods, 0, 1);
684 $newsequence = implode(",", $mods);
685 if (! set_field("course_sections", "sequence", $newsequence, "id", $thissection->id)) {
686 error("Module could not be updated");
687 }
688
689 return true;
690
691 }
692 } else { // move up within this section
693 $swap = $mods[$thepos-1];
694 $mods[$thepos-1] = $mods[$thepos];
695 $mods[$thepos] = $swap;
696
697 $newsequence = implode(",", $mods);
698 if (! set_field("course_sections", "sequence", $newsequence, "id", $thissection->id)) {
699 error("This section could not be updated");
700 }
701 return true;
702 }
703
704 } else { // Moving the module down
705
706 if ($last) {
707 $nextsectionnumber = $thissection->section + 1;
708 if ($nextsection = get_record_sql("SELECT * FROM course_sections
709 WHERE course='$thissection->course'
710 AND section='$nextsectionnumber' ")) {
711
712 if ($nextsection->sequence) {
713 $newsequence = "$cm->id,$nextsection->sequence";
714 } else {
715 $newsequence = "$cm->id";
716 }
717
718 if (! set_field("course_sections", "sequence", $newsequence, "id", $nextsection->id)) {
719 error("Next section could not be updated");
720 }
721
722 if (! set_field("course_modules", "section", $nextsection->id, "id", $cm->id)) {
723 error("Module could not be updated");
724 }
725
726 array_splice($mods, $thepos, 1);
727 $newsequence = implode(",", $mods);
728 if (! set_field("course_sections", "sequence", $newsequence, "id", $thissection->id)) {
729 error("This section could not be updated");
730 }
731 return true;
732
733 } else { // There is no next section, so just return
734 return true;
735
736 }
737 } else { // move down within this section
738 $swap = $mods[$thepos+1];
739 $mods[$thepos+1] = $mods[$thepos];
740 $mods[$thepos] = $swap;
741
742 $newsequence = implode(",", $mods);
743 if (! set_field("course_sections", "sequence", $newsequence, "id", $thissection->id)) {
744 error("This section could not be updated");
745 }
746 return true;
747 }
748 }
749}
750
94361e02 751function make_editing_buttons($moduleid, $absolute=false) {
752 global $CFG;
753
90845098 754 $delete = get_string("delete");
755 $moveup = get_string("moveup");
756 $movedown = get_string("movedown");
757 $update = get_string("update");
94361e02 758
759 if ($absolute) {
760 $path = "$CFG->wwwroot/course/";
761 } else {
762 $path = "";
763 }
90845098 764 return "&nbsp; &nbsp;
94361e02 765 <A HREF=\"".$path."mod.php?delete=$moduleid\"><IMG
766 SRC=".$path."../pix/t/delete.gif BORDER=0 ALT=\"$delete\"></A>
767 <A HREF=\"".$path."mod.php?id=$moduleid&move=-1\"><IMG
768 SRC=".$path."../pix/t/up.gif BORDER=0 ALT=\"$moveup\"></A>
769 <A HREF=\"".$path."mod.php?id=$moduleid&move=1\"><IMG
770 SRC=".$path."../pix/t/down.gif BORDER=0 ALT=\"$movedown\"></A>
771 <A HREF=\"".$path."mod.php?update=$moduleid\"><IMG
772 SRC=".$path."../pix/t/edit.gif BORDER=0 ALT=\"$update\"></A>";
90845098 773}
774
f9903ed0 775?>