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