Missing parameter
[moodle.git] / course / lib.php
CommitLineData
f9903ed0 1<? // $Id$
97c270e9 2 // Library of useful functions
f9903ed0 3
f9903ed0 4
3d891989 5if (defined('COURSE_MAX_LOG_DISPLAY')) { // Being included again - should never happen!!
9ae687af 6 return;
7}
8
3d891989 9define('COURSE_MAX_LOG_DISPLAY', 150); // days
d9d1c35d 10
3d891989 11define('COURSE_MAX_LOGS_PER_PAGE', 1000); // records
1c0200e0 12
3d891989 13define('COURSE_LIVELOG_REFRESH', 60); // Seconds
87ffed0d 14
10a95b43 15define('COURSE_MAX_RECENT_PERIOD', 604800); // A week, in seconds
ef58b822 16
c2cb4545 17define("FRONTPAGENEWS", 0);
18define("FRONTPAGECOURSELIST", 1);
19define("FRONTPAGECATEGORYNAMES", 2);
f9903ed0 20
9ae687af 21
f9903ed0 22function print_log_selector_form($course, $selecteduser=0, $selecteddate="today") {
23
9481285b 24 global $USER, $CFG;
25
f9903ed0 26 // Get all the possible users
27 $users = array();
720a43ce 28
29 if ($course->category) {
30d6173e 30 if ($courseusers = get_course_users($course->id, "u.lastaccess DESC")) {
31 foreach ($courseusers as $courseuser) {
32 $users[$courseuser->id] = "$courseuser->firstname $courseuser->lastname";
33 }
f9903ed0 34 }
9fa49e22 35 if ($guest = get_guest()) {
30d6173e 36 $users[$guest->id] = "$guest->firstname $guest->lastname";
122cffc9 37 }
f9903ed0 38 }
720a43ce 39
40 if (isadmin()) {
9fa49e22 41 if ($ccc = get_records("course", "", "", "fullname")) {
720a43ce 42 foreach ($ccc as $cc) {
cfa5d3f2 43 if ($cc->category) {
44 $courses["$cc->id"] = "$cc->fullname";
45 } else {
46 $courses["$cc->id"] = " $cc->fullname (Site)";
47 }
720a43ce 48 }
f9903ed0 49 }
cfa5d3f2 50 asort($courses);
f9903ed0 51 }
52
dcde9f02 53
54 $strftimedate = get_string("strftimedate");
55 $strftimedaydate = get_string("strftimedaydate");
56
f9903ed0 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
dcde9f02 69 $dates = array("$timemidnight" => get_string("today").", ".userdate($timenow, $strftimedate) );
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;
dcde9f02 79 $dates["$timemidnight"] = userdate($timenow, $strftimedaydate);
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) {
97c270e9 95 choose_from_menu ($users, "user", $selecteduser, get_string("allparticipants") );
720a43ce 96 }
97c270e9 97 choose_from_menu ($dates, "date", $selecteddate, get_string("alldays"));
98 echo "<INPUT TYPE=submit VALUE=\"".get_string("showtheselogs")."\">";
f9903ed0 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
3d891989 124 global $CFG;
47f1da80 125
720a43ce 126 if ($course->category) {
ebc3bd2b 127 $selector = "WHERE l.course='$course->id' AND l.userid = u.id";
a2ab3b05 128
720a43ce 129 } else {
ebc3bd2b 130 $selector = "WHERE l.userid = u.id"; // Show all courses
9fa49e22 131 if ($ccc = get_courses(-1)) {
720a43ce 132 foreach ($ccc as $cc) {
133 $courses[$cc->id] = "$cc->shortname";
134 }
135 }
136 }
f9903ed0 137
138 if ($user) {
ebc3bd2b 139 $selector .= " AND l.userid = '$user'";
f9903ed0 140 }
141
142 if ($date) {
143 $enddate = $date + 86400;
144 $selector .= " AND l.time > '$date' AND l.time < '$enddate'";
145 }
146
f9d5fd3b 147 $order = $order." LIMIT ".COURSE_MAX_LOGS_PER_PAGE; // To keep it manageable
148
6aeec7f8 149 if (!$logs = get_logs($selector, $order)) {
f9903ed0 150 notify("No logs found!");
151 print_footer($course);
152 exit;
153 }
154
155 $count=0;
156 $tt = getdate(time());
157 $today = mktime (0, 0, 0, $tt["mon"], $tt["mday"], $tt["year"]);
f9d5fd3b 158 if (($totalcountlogs = count($logs)) == COURSE_MAX_LOGS_PER_PAGE) {
159 $totalcountlogs = "$totalcountlogs (+)";
1c0200e0 160 }
161
dcde9f02 162 $strftimedatetime = get_string("strftimedatetime");
163
f9d5fd3b 164 echo "<p align=center>";
1c0200e0 165 print_string("displayingrecords", "", $totalcountlogs);
f9d5fd3b 166 echo "</p>";
1c0200e0 167
f9d5fd3b 168 echo "<table border=0 align=center cellpadding=3 cellspacing=3>";
f9903ed0 169 foreach ($logs as $log) {
600149be 170
9fa49e22 171 if ($ld = get_record("log_display", "module", "$log->module", "action", "$log->action")) {
565f7a95 172 $log->info = get_field($ld->mtable, $ld->field, "id", $log->info);
600149be 173 }
174
f9d5fd3b 175 echo "<tr nowrap>";
720a43ce 176 if (! $course->category) {
f9d5fd3b 177 echo "<td nowrap><font size=2><a href=\"view.php?id=$log->course\">".$courses[$log->course]."</a></td>";
720a43ce 178 }
f9d5fd3b 179 echo "<td nowrap align=right><font size=2>".userdate($log->time, "%a")."</td>";
180 echo "<td nowrap><font size=2>".userdate($log->time, $strftimedatetime)."</td>";
dc0dc7d5 181 echo "<td nowrap><font size=2>";
47f1da80 182 link_to_popup_window("/lib/ipatlas/plot.php?address=$log->ip&user=$log->userid", "ipatlas","$log->ip", 400, 700);
f9d5fd3b 183 echo "</td>";
184 echo "<td nowrap><font size=2><a href=\"../user/view.php?id=$log->userid&course=$log->course\"><b>$log->firstname $log->lastname</b></td>";
185 echo "<td nowrap><font size=2>";
600149be 186 link_to_popup_window( make_log_url($log->module,$log->url), "fromloglive","$log->module $log->action", 400, 600);
f9d5fd3b 187 echo "</td>";
188 echo "<td nowrap><font size=2>$log->info</td>";
189 echo "</tr>";
f9903ed0 190 }
f9d5fd3b 191 echo "</table>";
f9903ed0 192}
193
194
c2cb4545 195function print_log_graph($course, $userid=0, $type="course.png", $date=0) {
196 global $CFG;
197 if (empty($CFG->gdversion)) {
198 echo "(".get_string("gdneed").")";
d887b5a7 199 } else {
c2cb4545 200 echo "<IMG BORDER=0 SRC=\"$CFG->wwwroot/course/loggraph.php?id=$course->id&user=$userid&type=$type&date=$date\">";
d887b5a7 201 }
202}
203
204
f9903ed0 205
600149be 206function print_recent_activity($course) {
207 // $course is an object
208 // This function trawls through the logs looking for
209 // anything new since the user's last login
210
3d891989 211 global $CFG, $USER;
600149be 212
213 if (! $USER->lastlogin ) {
3d891989 214 echo "<p align=center><font size=1>";
4b1371a7 215 print_string("welcometocourse", "", $course->shortname);
3d891989 216 echo "</font></p>";
600149be 217 return;
4c654ee3 218 } else {
3d891989 219 echo "<p align=center><font size=1>";
4c654ee3 220 echo get_string("yourlastlogin").":<BR>";
dcde9f02 221 echo userdate($USER->lastlogin, get_string("strftimerecentfull"));
3d891989 222 echo "</font></p>";
223 }
224
225 $timestart = $USER->lastlogin;
b1992e65 226 $timemaxrecent = time() - COURSE_MAX_RECENT_PERIOD;
227 if ($timestart < $timemaxrecent) {
228 $timestart = $timemaxrecent;
600149be 229 }
230
600149be 231
232 // Firstly, have there been any new enrolments?
233
234 $heading = false;
235 $content = false;
1b5910c4 236
237 $logs = get_records_select("log", "time > '$timestart' AND course = '$course->id' AND
238 module = 'course' AND action = 'enrol'", "time ASC");
239
240 if ($logs) {
241 foreach ($logs as $key => $log) {
600149be 242 if (! $heading) {
4c654ee3 243 print_headline(get_string("newusers").":");
600149be 244 $heading = true;
245 $content = true;
246 }
247 $user = get_record("user", "id", $log->info);
d578afc8 248 if (isstudent($course->id, $user->id)) {
3d891989 249 echo "<p><font size=1><a href=\"../user/view.php?id=$user->id&course=$course->id\">$user->firstname $user->lastname</a></font></p>";
d578afc8 250 }
600149be 251 }
252 }
253
1b5910c4 254 // Next, have there been any modifications to the course structure?
255
256 $logs = get_records_select("log", "time > '$timestart' AND course = '$course->id' AND
257 module = 'course' AND action LIKE '% mod'", "time ASC");
258
259 if ($logs) {
260 foreach ($logs as $key => $log) {
261 $info = split(" ", $log->info);
262 $modname = get_field($info[0], "name", "id", $info[1]);
263 //Create a temp valid module structure (course,id)
264 $tempmod->course = $log->course;
265 $tempmod->id = $info[1];
266 //Obtain the visible property from the instance
267 $modvisible = instance_is_visible($info[0],$tempmod);
268
269 //Only if the mod is visible
270 if ($modvisible) {
271 switch ($log->action) {
272 case "add mod":
273 $stradded = get_string("added", "moodle", get_string("modulename", $info[0]));
274 $changelist["$log->info"] = array ("operation" => "add", "text" => "$stradded:<BR><A HREF=\"$CFG->wwwroot/course/$log->url\">$modname</A>");
275 break;
276 case "update mod":
277 $strupdated = get_string("updated", "moodle", get_string("modulename", $info[0]));
278 if (empty($changelist["$log->info"])) {
279 $changelist["$log->info"] = array ("operation" => "update", "text" => "$strupdated:<BR><A HREF=\"$CFG->wwwroot/course/$log->url\">$modname</A>");
280 }
281 break;
282 case "delete mod":
283 if (!empty($changelist["$log->info"]["operation"]) and
284 $changelist["$log->info"]["operation"] == "add") {
285 $changelist["$log->info"] = NULL;
286 } else {
287 $strdeleted = get_string("deletedactivity", "moodle", get_string("modulename", $info[0]));
288 $changelist["$log->info"] = array ("operation" => "delete", "text" => $strdeleted);
289 }
290 break;
600149be 291 }
ef25340c 292 }
293 }
294 }
295
9c9f7d77 296 if (!empty($changelist)) {
ef25340c 297 foreach ($changelist as $changeinfo => $change) {
298 if ($change) {
299 $changes[$changeinfo] = $change;
300 }
301 }
302 if (count($changes) > 0) {
4c654ee3 303 print_headline(get_string("courseupdates").":");
ef25340c 304 $content = true;
305 foreach ($changes as $changeinfo => $change) {
3d891989 306 echo "<p><font size=1>".$change["text"]."</font></p>";
600149be 307 }
308 }
309 }
310
3869a2ac 311 // Now display new things from each module
600149be 312
3869a2ac 313 $mods = get_list_of_plugins("mod");
600149be 314
1b5910c4 315 $isteacher = isteacher($course->id);
316
317 foreach ($mods as $mod) { // Each module gets it's own logs and prints them
3869a2ac 318 include_once("$CFG->dirroot/mod/$mod/lib.php");
319 $print_recent_activity = $mod."_print_recent_activity";
1b5910c4 320 if (function_exists($print_recent_activity)) {
321 $modcontent = $print_recent_activity($course, $isteacher, $timestart);
3869a2ac 322 if ($modcontent) {
323 $content = true;
600149be 324 }
600149be 325 }
326 }
327
328 if (! $content) {
3d891989 329 echo "<font size=2>".get_string("nothingnew")."</font>";
600149be 330 }
600149be 331}
332
e1360728 333
d897cae4 334function get_array_of_activities($courseid) {
335// For a given course, returns an array of course activity objects
336// Each item in the array contains he following properties:
337// cm - course module id
338// mod - name of the module (eg forum)
339// section - the number of the section (eg week or topic)
340// name - the name of the instance
5867bfb5 341// visible - is the instance visible or not
86aa7ccf 342// extra - contains extra string to include in any link
d897cae4 343
344 $mod = array();
345
9fa49e22 346 if (!$rawmods = get_course_mods($courseid)) {
d897cae4 347 return NULL;
348 }
349
350 if ($sections = get_records("course_sections", "course", $courseid, "section ASC")) {
351 foreach ($sections as $section) {
74666583 352 if (!empty($section->sequence)) {
d897cae4 353 $sequence = explode(",", $section->sequence);
354 foreach ($sequence as $seq) {
7af6281f 355 if (empty($rawmods[$seq])) {
356 continue;
357 }
d897cae4 358 $mod[$seq]->cm = $rawmods[$seq]->id;
359 $mod[$seq]->mod = $rawmods[$seq]->modname;
360 $mod[$seq]->section = $section->section;
361 $mod[$seq]->name = urlencode(get_field($rawmods[$seq]->modname, "name", "id", $rawmods[$seq]->instance));
fec5a6a6 362 $mod[$seq]->visible = $rawmods[$seq]->visible;
86aa7ccf 363 $mod[$seq]->extra = "";
364
365 // This part is an ugly hack that doesn't belong here//
366 if ($mod[$seq]->mod == "resource") {
367 if ($resource = get_record("resource", "id", $rawmods[$seq]->instance)) {
368 if ($resource->type == 5 and $resource->alltext) {
23b8ce89 369 $mod[$seq]->extra = urlencode("target=\"resource$resource->id\" onClick=\"return ".
86aa7ccf 370 "openpopup('/mod/resource/view.php?id=".
371 $mod[$seq]->cm.
93faa0c4 372 "','resource$resource->id','$resource->alltext');\"");
86aa7ccf 373 }
374 }
375 }
d897cae4 376 }
377 }
378 }
379 }
380 return $mod;
381}
382
383
384
e1360728 385
90845098 386function get_all_mods($courseid, &$mods, &$modnames, &$modnamesplural, &$modnamesused) {
387// Returns a number of useful structures for course displays
7468bf01 388
90845098 389 $mods = NULL; // course modules indexed by id
390 $modnames = NULL; // all course module names
94361e02 391 $modnamesplural= NULL; // all course module names (plural form)
90845098 392 $modnamesused = NULL; // course module names used
7468bf01 393
9fa49e22 394 if ($allmods = get_records("modules")) {
90845098 395 foreach ($allmods as $mod) {
5867bfb5 396 if ($mod->visible) {
397 $modnames[$mod->name] = get_string("modulename", "$mod->name");
398 $modnamesplural[$mod->name] = get_string("modulenameplural", "$mod->name");
399 }
90845098 400 }
401 asort($modnames);
402 } else {
403 error("No modules are installed!");
404 }
405
9fa49e22 406 if ($rawmods = get_course_mods($courseid)) {
7468bf01 407 foreach($rawmods as $mod) { // Index the mods
408 $mods[$mod->id] = $mod;
90845098 409 $mods[$mod->id]->modfullname = $modnames[$mod->modname];
1acfbce5 410 if ($mod->visible or isteacher($courseid)) {
411 $modnamesused[$mod->modname] = $modnames[$mod->modname];
412 }
7468bf01 413 }
c7da6f7a 414 if ($modnamesused) {
415 asort($modnamesused);
416 }
7468bf01 417 }
7468bf01 418}
419
9fa49e22 420
7468bf01 421function get_all_sections($courseid) {
422
d26d7ed0 423 return get_records("course_sections", "course", "$courseid", "section",
7d99d695 424 "section, id, course, summary, sequence, visible");
7468bf01 425}
426
b86fc0e2 427function course_set_display($courseid, $display=0) {
428 global $USER;
429
430 if (empty($USER)) {
431 return false;
432 }
433
434 if ($display == "all" or empty($display)) {
435 $display = 0;
436 }
437
438 if (record_exists("course_display", "userid", $USER->id, "course", $courseid)) {
439 set_field("course_display", "display", $display, "userid", $USER->id, "course", $courseid);
440 } else {
441 $record->userid = $USER->id;
442 $record->course = $courseid;
443 $record->display = $display;
444 if (!insert_record("course_display", $record)) {
445 notify("Could not save your course display!");
446 }
447 }
448
449 return $USER->display[$courseid] = $display; // Note: = not ==
450}
451
7d99d695 452function set_section_visible($courseid, $sectionnumber, $visibility) {
453/// For a given course section, markes it visible or hidden,
454/// and does the same for every activity in that section
455
456 if ($section = get_record("course_sections", "course", $courseid, "section", $sectionnumber)) {
457 set_field("course_sections", "visible", "$visibility", "id", $section->id);
458 if (!empty($section->sequence)) {
459 $modules = explode(",", $section->sequence);
460 foreach ($modules as $moduleid) {
461 set_field("course_modules", "visible", "$visibility", "id", $moduleid);
462 }
463 }
5867bfb5 464 rebuild_course_cache($courseid);
7d99d695 465 }
466}
ba2e5d73 467
5e367a2d 468function print_section_block($heading, $course, $section, $mods, $modnames, $modnamesused,
52dcc2f9 469 $absolute=true, $width="100%") {
5e367a2d 470
471 global $CFG;
52dcc2f9 472 static $isteacher;
473 static $isediting;
474
475 if (!isset($isteacher)) {
476 $isteacher = isteacher($course->id);
477 }
478 if (!isset($isediting)) {
479 $isediting = isediting($course->id);
480 }
5e367a2d 481
482 $modinfo = unserialize($course->modinfo);
483 $moddata = array();
484 $modicon = array();
485 $editbuttons = "";
486
74666583 487 if (!empty($section->sequence)) {
5e367a2d 488
489 $sectionmods = explode(",", $section->sequence);
490
491 foreach ($sectionmods as $modnumber) {
52dcc2f9 492 if (empty($mods[$modnumber])) {
493 continue;
494 }
5e367a2d 495 $mod = $mods[$modnumber];
496 if ($isediting) {
7977cffd 497 $editbuttons = make_editing_buttons($mod->id, $absolute, $mod->visible, false);
1acfbce5 498 }
52dcc2f9 499 if ($mod->visible or $isteacher) {
1acfbce5 500 $instancename = urldecode($modinfo[$modnumber]->name);
c2cb4545 501 $linkcss = $mod->visible ? "" : " class=\"dimmed\" ";
3ee3e01a 502 if (!empty($modinfo[$modnumber]->extra)) {
503 $extra = urldecode($modinfo[$modnumber]->extra);
504 } else {
505 $extra = "";
506 }
7977cffd 507
52dcc2f9 508 $modicon[] = "<img src=\"$CFG->wwwroot/mod/$mod->modname/icon.gif\"".
509 " height=\"16\" width=\"16\" alt=\"$mod->modfullname\">";
c2cb4545 510 $moddata[] = "<a title=\"$mod->modfullname\" $linkcss $extra".
52dcc2f9 511 "href=\"$CFG->wwwroot/mod/$mod->modname/view.php?id=$mod->id\">$instancename</a>".
512 "<br />$editbuttons";
5e367a2d 513 }
5e367a2d 514 }
515 }
a44d18e7 516 if ($isediting) {
15ac9065 517 $editmenu = popup_form("$CFG->wwwroot/course/mod.php?id=$course->id&amp;section=$section->section&add=",
5e367a2d 518 $modnames, "section0", "", get_string("add")."...", "mods", get_string("activities"), true);
dfec7b01 519 $editmenu = "<div align=right>$editmenu</div>";
47f1da80 520 } else {
521 $editmenu = "";
5e367a2d 522 }
523
524 print_side_block($heading, "", $moddata, $modicon, $editmenu, $width);
525}
526
527
d897cae4 528function print_section($course, $section, $mods, $modnamesused, $absolute=false, $width="100%") {
52dcc2f9 529/// Prints a section full of activity modules
7977cffd 530 global $CFG, $USER;
531
52dcc2f9 532 static $isteacher;
533 static $isediting;
7977cffd 534 static $ismoving;
535 static $strmovehere;
536 static $strmovefull;
52dcc2f9 537
538 if (!isset($isteacher)) {
539 $isteacher = isteacher($course->id);
540 }
541 if (!isset($isediting)) {
542 $isediting = isediting($course->id);
543 }
7977cffd 544 if (!isset($ismoving)) {
545 $ismoving = ismoving($course->id);
546 }
547 if ($ismoving) {
548 $strmovehere = get_string("movehere");
549 $strmovefull = get_string("movefull", "", "'$USER->activitycopyname'");
550 }
94361e02 551
c408b0c4 552 $modinfo = unserialize($course->modinfo);
94361e02 553
dfec7b01 554 echo "<table width=\"$width\"><tr><td>\n";
74666583 555 if (!empty($section->sequence)) {
94361e02 556
557 $sectionmods = explode(",", $section->sequence);
558
559 foreach ($sectionmods as $modnumber) {
9ae687af 560 if (empty($mods[$modnumber])) {
561 continue;
562 }
94361e02 563 $mod = $mods[$modnumber];
52dcc2f9 564 if ($mod->visible or $isteacher) {
7977cffd 565 if ($ismoving) {
566 if ($mod->id == $USER->activitycopy) {
567 continue;
568 }
569 echo "<font size=\"2\"> -> <a title=\"$strmovefull\"".
570 " href=\"mod.php?moveto=$mod->id\">$strmovehere</a></font><br />\n";
1acfbce5 571 }
7977cffd 572 $instancename = urldecode($modinfo[$modnumber]->name);
86aa7ccf 573 if (!empty($modinfo[$modnumber]->extra)) {
574 $extra = urldecode($modinfo[$modnumber]->extra);
575 } else {
576 $extra = "";
577 }
c2cb4545 578 $linkcss = $mod->visible ? "" : " class=\"dimmed\" ";
52dcc2f9 579 echo "<img src=\"$CFG->wwwroot/mod/$mod->modname/icon.gif\"".
580 " height=16 width=16 alt=\"$mod->modfullname\">".
c2cb4545 581 " <font size=2><a title=\"$mod->modfullname\" $linkcss $extra".
52dcc2f9 582 " href=\"$CFG->wwwroot/mod/$mod->modname/view.php?id=$mod->id\">$instancename</a></font>";
1acfbce5 583 }
d897cae4 584 if (isediting($course->id)) {
5e367a2d 585 echo "&nbsp;&nbsp;";
1acfbce5 586 echo make_editing_buttons($mod->id, $absolute, $mod->visible);
587 }
52dcc2f9 588 if ($mod->visible or $isteacher) {
dfec7b01 589 echo "<br />\n";
94361e02 590 }
94361e02 591 }
592 }
7977cffd 593 if ($ismoving) {
594 echo "<font size=\"2\"> -> <a title=\"$strmovefull\"".
595 " href=\"mod.php?movetosection=$section->id\">$strmovehere</a></font><br />\n";
596 }
dfec7b01 597 echo "</td></tr></table><br />\n\n";
a7ad3ea6 598}
599
5867bfb5 600
601function rebuild_course_cache($courseid=0) {
602// Rebuilds the cached list of course activities stored in the database
603// If a courseid is not specified, then all are rebuilt
604
605 if ($courseid) {
606 $select = "id = '$courseid'";
607 } else {
608 $select = "";
609 }
610
611 if ($courses = get_records_select("course", $select)) {
612 foreach ($courses as $course) {
613 $modinfo = serialize(get_array_of_activities($course->id));
614 if (!set_field("course", "modinfo", $modinfo, "id", $course->id)) {
615 notify("Could not cache module information for course '$course->fullname'!");
616 }
617 }
618 }
619}
620
621
7541bc3e 622function print_heading_block($heading, $width="100%", $class="headingblock") {
5e367a2d 623 global $THEME;
624
7541bc3e 625 echo "<table width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"5\">";
626 echo "<tr><td bgcolor=\"$THEME->cellheading\" class=\"$class\">";
5e367a2d 627 echo stripslashes($heading);
628 echo "</td></tr></table>";
5e367a2d 629}
630
631function print_side_block($heading="", $content="", $list=NULL, $icons=NULL, $footer="", $width=180) {
632// Prints a nice side block with an optional header. The content can either
633// be a block of HTML or a list of text with optional icons.
a7ad3ea6 634
5e367a2d 635 global $THEME;
636
7541bc3e 637 print_side_block_start($heading, $width);
638
5e367a2d 639 if ($content) {
7541bc3e 640 echo "$content";
5e367a2d 641 } else {
5e367a2d 642 echo "<table width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"2\">";
643 foreach ($list as $key => $string) {
7541bc3e 644 echo "<tr bgcolor=\"$THEME->cellcontent2\">";
5e367a2d 645 if ($icons) {
7541bc3e 646 echo "<td class=\"sideblocklinks\" valign=\"top\" width=\"16\">".$icons[$key]."</td>";
a7ad3ea6 647 }
7541bc3e 648 echo "<td class=\"sideblocklinks\" valign=\"top\" width=\"*\"><font size=\"-1\">$string</font></td>";
5e367a2d 649 echo "</tr>";
a7ad3ea6 650 }
5e367a2d 651 if ($footer) {
7541bc3e 652 echo "<tr bgcolor=\"$THEME->cellcontent2\">";
5e367a2d 653 if ($icons) {
7541bc3e 654 echo "<td class=\"sideblocklinks\" valign=\"top\" width=\"16\">&nbsp;</td>";
5e367a2d 655 }
7541bc3e 656 echo "<td class=\"sideblocklinks\"><font size=\"-1\">$footer</td>";
5e367a2d 657 echo "</tr>";
658 }
659 echo "</table>";
a7ad3ea6 660 }
5e367a2d 661
7541bc3e 662 print_side_block_end();
663}
664
665function print_side_block_start($heading="", $width=180, $class="sideblockmain") {
666// Starts a nice side block with an optional header.
667
668 global $THEME;
669
670 echo "<table class=\"sideblock\" width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"5\">";
671 if ($heading) {
672 echo "<tr>";
673 echo "<td class=\"sideblockheading\" bgcolor=\"$THEME->cellheading\">$heading</td>";
674 echo "</tr>";
675 }
676 echo "<tr>";
677 echo "<td class=\"$class\" bgcolor=\"$THEME->cellcontent2\">";
678}
679
680function print_side_block_end() {
681 echo "</td></tr>";
7d99d695 682 echo "</table><br />";
94361e02 683}
684
5e367a2d 685
670fddf1 686function print_admin_links ($siteid, $width=180) {
dc0dc7d5 687 global $CFG, $THEME;
2b25f2a0 688
dc0dc7d5 689 if (empty($THEME->custompix)) {
690 $icon = "<img src=\"$CFG->wwwroot/pix/i/settings.gif\" height=16 width=16 alt=\"\">";
691 } else {
692 $icon = "<img src=\"$CFG->wwwroot/theme/$CFG->theme/pix/i/settings.gif\" height=16 width=16 alt=\"\">";
693 }
694
1924074c 695 if (isadmin()) {
5867bfb5 696 $moddata[]="<a href=\"$CFG->wwwroot/$CFG->admin/config.php\">".get_string("configvariables")."</a>";
697 $modicon[]=$icon;
698 $moddata[]="<a href=\"$CFG->wwwroot/$CFG->admin/site.php\">".get_string("sitesettings")."</a>";
1924074c 699 $modicon[]=$icon;
5867bfb5 700 $moddata[]="<a href=\"$CFG->wwwroot/course/log.php?id=$siteid\">".get_string("sitelogs")."</a>";
1924074c 701 $modicon[]=$icon;
5867bfb5 702 $moddata[]="<a href=\"$CFG->wwwroot/theme/index.php\">".get_string("choosetheme")."</a>";
1924074c 703 $modicon[]=$icon;
5867bfb5 704 $moddata[]="<a href=\"$CFG->wwwroot/$CFG->admin/lang.php\">".get_string("checklanguage")."</a>";
1924074c 705 $modicon[]=$icon;
5867bfb5 706 $moddata[]="<a href=\"$CFG->wwwroot/$CFG->admin/modules.php\">".get_string("managemodules")."</a>";
1924074c 707 $modicon[]=$icon;
be09831a 708 if (file_exists("$CFG->dirroot/$CFG->admin/$CFG->dbtype")) {
5867bfb5 709 $moddata[]="<a href=\"$CFG->wwwroot/$CFG->admin/$CFG->dbtype/frame.php\">".get_string("managedatabase")."</a>";
1924074c 710 $modicon[]=$icon;
711 }
5867bfb5 712 $moddata[]="<hr>";
1924074c 713 $modicon[]="";
714 }
715 if (iscreator()) {
5867bfb5 716 $moddata[]="<a href=\"$CFG->wwwroot/course/edit.php\">".get_string("addnewcourse")."</a>";
1924074c 717 $modicon[]=$icon;
e808d25f 718 $moddata[]="<a href=\"$CFG->wwwroot/$CFG->admin/teacher.php\">".get_string("assignteachers")."</a>";
1924074c 719 $modicon[]=$icon;
5b337cdf 720 $fulladmin = "";
1924074c 721 }
722 if (isadmin()) {
5867bfb5 723 $moddata[]="<a href=\"$CFG->wwwroot/course/categories.php\">".get_string("categories")."</a>";
1924074c 724 $modicon[]=$icon;
5867bfb5 725 $moddata[]="<a href=\"$CFG->wwwroot/course/delete.php\">".get_string("deletecourse")."</a>";
1924074c 726 $modicon[]=$icon;
5867bfb5 727 $moddata[]="<hr>";
1924074c 728 $modicon[]="";
3399a22c 729 if($CFG->auth == "email" || $CFG->auth == "none" || $CFG->auth == "manual"){
5867bfb5 730 $moddata[]="<a href=\"$CFG->wwwroot/$CFG->admin/user.php?newuser=true\">".get_string("addnewuser")."</a>";
3399a22c 731 $modicon[]=$icon;
732 }
5867bfb5 733 $moddata[]="<a href=\"$CFG->wwwroot/$CFG->admin/user.php\">".get_string("edituser")."</a>";
1924074c 734 $modicon[]=$icon;
5867bfb5 735 $moddata[]="<a href=\"$CFG->wwwroot/$CFG->admin/admin.php\">".get_string("assignadmins")."</a>";
1924074c 736 $modicon[]=$icon;
5867bfb5 737 $moddata[]="<a href=\"$CFG->wwwroot/$CFG->admin/creators.php\">".get_string("assigncreators")."</a>";
1924074c 738 $modicon[]=$icon;
5867bfb5 739 $moddata[]="<a href=\"$CFG->wwwroot/$CFG->admin/auth.php\">".get_string("authentication")."</a>";
1924074c 740 $modicon[]=$icon;
5867bfb5 741 $fulladmin = "<p><a href=\"$CFG->wwwroot/$CFG->admin/\">".get_string("admin")."</a>...";
1924074c 742 }
5e367a2d 743
744 print_side_block(get_string("administration"), "", $moddata, $modicon, $fulladmin, $width);
745
dc0dc7d5 746 echo "<img src=\"$CFG->wwwroot/pix/spacer.gif\" width=\"$width\" height=1><br>";
2b25f2a0 747}
748
b4d7002e 749function print_course_admin_links($course, $width=180) {
dc0dc7d5 750 global $USER, $CFG, $THEME;
44dad735 751
fce1ce13 752 if (isguest()) {
753 return true;
754 }
dc0dc7d5 755 if (empty($THEME->custompix)) {
756 $pixpath = "$CFG->wwwroot/pix";
757 $modpixpath = "$CFG->wwwroot/mod";
758 } else {
759 $pixpath = "$CFG->wwwroot/theme/$CFG->theme/pix";
760 $modpixpath = "$CFG->wwwroot/theme/$CFG->theme/pix/mod";
761 }
13469b82 762 if (isteacher($course->id)) {
dc0dc7d5 763 $adminicon[]="<img src=\"$pixpath/i/edit.gif\" height=16 width=16 alt=\"\">";
13469b82 764 if (isediting($course->id)) {
dc0dc7d5 765 $admindata[]="<a href=\"view.php?id=$course->id&edit=off\">".get_string("turneditingoff")."</a>";
13469b82 766 } else {
dc0dc7d5 767 $admindata[]="<a href=\"view.php?id=$course->id&edit=on\">".get_string("turneditingon")."</a>";
13469b82 768 }
dc0dc7d5 769 $admindata[]="<a href=\"edit.php?id=$course->id\">".get_string("settings")."...</a>";
770 $adminicon[]="<img src=\"$pixpath/i/settings.gif\" height=16 width=16 alt=\"\">";
13469b82 771 if (!$course->teachers) {
772 $course->teachers = get_string("defaultcourseteachers");
773 }
dc0dc7d5 774 $admindata[]="<a href=\"teachers.php?id=$course->id\">$course->teachers...</a>";
775 $adminicon[]="<img src=\"$pixpath/i/settings.gif\" height=16 width=16 alt=\"\">";
13469b82 776
dc0dc7d5 777 $admindata[]="<a href=\"grades.php?id=$course->id\">".get_string("grades")."...</a>";
778 $adminicon[]="<img src=\"$pixpath/i/grades.gif\" height=16 width=16 alt=\"\">";
13469b82 779
dc0dc7d5 780 $admindata[]="<a href=\"log.php?id=$course->id\">".get_string("logs")."...</a>";
781 $adminicon[]="<img src=\"$pixpath/i/log.gif\" height=16 width=16 alt=\"\">";
782
783 $admindata[]="<a href=\"$CFG->wwwroot/files/index.php?id=$course->id\">".get_string("files")."...</a>";
784 $adminicon[]="<img src=\"$pixpath/i/files.gif\" height=16 width=16 alt=\"\">";
13469b82 785
dc0dc7d5 786 $admindata[]="<a href=\"$CFG->wwwroot/doc/view.php?id=$course->id&file=teacher.html\">".get_string("help")."...</a>";
787 $adminicon[]="<img src=\"$modpixpath/resource/icon.gif\" height=16 width=16 alt=\"\">";
b4d7002e 788
13469b82 789 if ($teacherforum = forum_get_course_forum($course->id, "teacher")) {
dc0dc7d5 790 $admindata[]="<a href=\"$CFG->wwwroot/mod/forum/view.php?f=$teacherforum->id\">".get_string("nameteacher", "forum")."</a>";
791 $adminicon[]="<img src=\"$modpixpath/forum/icon.gif\" height=16 width=16 alt=\"\">";
13469b82 792 }
793 } else {
dc0dc7d5 794 $admindata[]="<a href=\"grade.php?id=$course->id\">".get_string("grades")."...</a>";
795 $adminicon[]="<img src=\"$pixpath/i/grades.gif\" height=16 width=16 alt=\"\">";
13469b82 796 }
44dad735 797
5e367a2d 798 print_side_block(get_string("administration"), "", $admindata, $adminicon, "", $width);
44dad735 799}
2b25f2a0 800
c2cb4545 801
802function make_categories_list(&$list, &$parents, $category=NULL, $path="") {
803/// Given an empty array, this function recursively travels the
804/// categories, building up a nice list for display. It also makes
805/// an array that list all the parents for each category.
806
807 if ($category) {
808 if ($path) {
809 $path = "$path / $category->name";
810 } else {
811 $path = "$category->name";
812 }
813 $list[$category->id] = $path;
814 } else {
815 $category->id = 0;
816 }
817
818 if ($categories = get_categories("$category->id")) { // Print all the children recursively
819 foreach ($categories as $cat) {
820 if (!empty($category->id)) {
821 $parents[$cat->id] = $parents[$category->id];
822 $parents[$cat->id][] = $category->id;
823 }
824 make_categories_list($list, &$parents, $cat, $path);
825 }
826 }
827}
828
829
830function fix_category_courses($categoryid) {
831/// Given a category, this function makes sure the courseorder
832/// variable reflects the real world.
833
834 if (!$category = get_record("course_categories", "id", $categoryid)) {
835 return false;
836 }
837
838 $catcourseschanged = false;
839
840 if (trim($category->courseorder)) {
841 $catcourses = explode(',', $category->courseorder);
842 } else {
843 $catcourses = array();
844 }
845 $courses = get_records("course", "category", $category->id);
846
847 if ($catcourses) {
848 foreach ($catcourses as $key => $catcourse) { // Look for missing courses
849 if (!isset($courses[$catcourse])) {
850 $catcourseschanged = true;
851 unset($catcourses[$key]);
852 }
853 }
854 }
855 if ($courses) {
856 foreach ($courses as $course) {
857 if (!in_array($course->id, $catcourses)) {
858 $catcourseschanged = true;
859 $catcourses[] = $course->id;
860 }
861 }
862 }
863 if ($catcourseschanged) {
864 $category->courseorder = implode(',', $catcourses);
865 return set_field("course_categories", "courseorder", $category->courseorder, "id", $category->id);
866 }
867 return true;
868}
869
870
871
872function print_whole_category_list($category=NULL, $displaylist=NULL, $parentslist=NULL, $depth=-1) {
873/// Recursive function to print out all the categories in a nice format
874/// with or without courses included
875
876 if (!$displaylist) {
877 make_categories_list(&$displaylist, &$parentslist);
878 }
879
880 if ($category) {
881 if ($category->visible or isadmin()) {
882 print_category_box($category, $depth);
883 } else {
884 return; // Don't bother printing children of invisible categories
885 }
886
887 } else {
888 print_simple_box_start("center", "100%");
889 $category->id = "0";
890 }
891
892 if ($categories = get_categories($category->id)) { // Print all the children recursively
893 $countcats = count($categories);
894 $count = 0;
895 $first = true;
896 $last = false;
897 foreach ($categories as $cat) {
898 $count++;
899 if ($count == $countcats) {
900 $last = true;
901 }
902 $up = $first ? false : true;
903 $down = $last ? false : true;
904 $first = false;
905
906 print_whole_category_list($cat, $displaylist, $parentslist, $depth + 1);
907 }
908 }
909
910 if ($category->id == "0") {
911 print_simple_box_end();
912 }
913}
914
915
916
917function print_category_box($category, $depth) {
918/// Prints the category box in indented fashion
919
920 global $CFG;
921
85098089 922 $strallowguests = get_string("allowguests");
923 $strrequireskey = get_string("requireskey");
ba2e5d73 924
dc0dc7d5 925 if (empty($THEME->custompix)) {
926 $pixpath = "$CFG->wwwroot/pix";
927 } else {
928 $pixpath = "$CFG->wwwroot/theme/$CFG->theme/pix";
929 }
930
c2cb4545 931 $size = $depth * 40;
932
933 $catlinkcss = $category->visible ? "" : " class=\"dimmed\" ";
934
935 echo "<table class=\"categorybox\">";
936 echo "<tr>";
937 echo "<td width=\"$size\">";
938 echo print_spacer(1, $size);
939 echo "<td width=\"100%\">";
940 echo "<font size=+1><a $catlinkcss href=\"$CFG->wwwroot/course/index.php?category=$category->id\">$category->name</a></font>";
941 //echo "<font size=+1>$category->name</font>";
942 if ($CFG->frontpage == FRONTPAGECOURSELIST) {
943 if ($courses = get_courses($category)) {
944 echo "<ul>";
945 foreach ($courses as $course) {
946 $linkcss = $course->visible ? "" : " class=\"dimmed\" ";
947 echo "<li><a $linkcss href=\"$CFG->wwwroot/course/view.php?id=$course->id\">$course->fullname</a>";
948 echo "&nbsp;&nbsp;";
949 unset($courses[$key]);
950 if ($course->guest ) {
951 echo "<a title=\"$strallowguests\" href=\"$CFG->wwwroot/course/view.php?id=$course->id\">";
952 echo "<img alt=\"\" height=16 width=16 border=0 src=\"$pixpath/i/user.gif\"></a>";
0c656181 953 }
c2cb4545 954 if ($course->password) {
955 echo "<a title=\"$strrequireskey\" href=\"$CFG->wwwroot/course/view.php?id=$course->id\">";
956 echo "<img alt=\"\" height=16 width=16 border=0 src=\"$pixpath/i/key.gif\"></a>";
0c656181 957 }
0c656181 958 }
c2cb4545 959 echo "</ul>";
ba2e5d73 960 }
c2cb4545 961 }
962 echo "</td>";
963 echo "</tr>";
964 echo "</table>";
0c656181 965
c2cb4545 966}
967
968function print_courses_sideblock($category=0, $width="100%") {
969 global $CFG, $THEME;
970
971 if (empty($THEME->custompix)) {
972 $icon = "<img src=\"$CFG->wwwroot/pix/i/course.gif\"".
973 " height=\"16\" width=\"16\" alt=\"".get_string("course")."\">";
974 } else {
975 $icon = "<img src=\"$CFG->wwwroot/theme/$CFG->theme/pix/i/course.gif\"".
976 " height=\"16\" width=\"16\" alt=\"".get_string("course")."\">";
977 }
978
979 $categories = get_categories(0); // Parent = 0 ie top-level categories only
980 if (count($categories) > 1) { // Just print top level category links
981 foreach ($categories as $category) {
982 $linkcss = $category->visible ? "" : " class=\"dimmed\" ";
983 $moddata[]="<a $linkcss href=\"$CFG->wwwroot/course/index.php?category=$category->id\">$category->name</a>";
984 $modicon[]=$icon;
0c656181 985 }
c2cb4545 986 } else { // Just print course names of single category
987 $courses = get_courses($category);
988 if ($courses) {
989 foreach ($courses as $course) {
990 $linkcss = $course->visible ? "" : " class=\"dimmed\" ";
991 $moddata[]="<a $linkcss title=\"$course->shortname\" ".
992 "href=\"$CFG->wwwroot/course/view.php?id=$course->id\">$course->fullname</a>";
993 $modicon[]=$icon;
994 }
995 $fulllist = "<p><a href=\"$CFG->wwwroot/course/\">".get_string("fulllistofcourses")."</a>...";
996 } else {
997 $moddata = array();
998 $modicon = array();
999 $fulllist = get_string("nocoursesyet");
0c656181 1000 }
ba2e5d73 1001 }
c2cb4545 1002
1003 print_side_block(get_string("courses"), "", $moddata, $modicon, $fulllist, $width);
ba2e5d73 1004}
94361e02 1005
c2cb4545 1006
1007function print_courses($category, $width="100%") {
1008/// Category is 0 (for all courses) or an object
1009
1010 global $CFG, $THEME;
1011
1012 if (empty($category)) {
1013 $categories = NULL;
1014 $courses = get_courses(0);
607809b3 1015 } else {
c2cb4545 1016 $categories = get_categories($category->id); // sub categories
1017 $courses = get_courses($category);
1018 }
1019
1020 if ($categories) {
1021 print_simple_box_start("center");
1022 print_heading(get_string("subcategories"));
1023 foreach ($categories as $category) {
1024 $linkcss = $category->visible ? "" : " class=\"dimmed\" ";
1025 echo "<p align=\"center\"><a $linkcss".
1026 " href=\"$CFG->wwwroot/course/index.php?category=$category->id\">$category->name</a></p>";
1027 }
1028 print_simple_box_end();
1029 }
1030
1031 if ($courses) {
1032 foreach ($courses as $course) {
1033 print_course($course, $width);
1034 echo "<br />\n";
1035 }
1036 } else {
1037 print_heading(get_string("nocoursesyet"));
1038 }
1039
1040}
1041
1042
1043function print_course($course, $width="100%") {
1044
1045 global $CFG, $THEME;
1046
1047 if (! $site = get_site()) {
1048 error("Could not find a site!");
1049 }
1050
1051 if (empty($THEME->custompix)) {
1052 $pixpath = "$CFG->wwwroot/pix";
1053 } else {
1054 $pixpath = "$CFG->wwwroot/theme/$CFG->theme/pix";
1055 }
1056
1057 print_simple_box_start("center", "$width");
1058
1059 echo "<table width=\"100%\">";
1060 echo "<tr valign=top>";
1061 echo "<td valign=top width=50%>";
1062 echo "<p><font size=3><b><a title=\"".get_string("entercourse")."\"
1063 href=\"$CFG->wwwroot/course/view.php?id=$course->id\">$course->fullname</a></b></font></p>";
1064 if ($teachers = get_course_teachers($course->id)) {
1065 echo "<p><font size=\"1\">\n";
1066 foreach ($teachers as $teacher) {
1067 if ($teacher->authority > 0) {
1068 if (!$teacher->role) {
1069 $teacher->role = $course->teacher;
1070 }
1071 echo "$teacher->role: <a href=\"$CFG->wwwroot/user/view.php?id=$teacher->id&course=$site->id\">$teacher->firstname $teacher->lastname</a><br />";
1072 }
1073 }
1074 echo "</font></p>";
1075 }
1076 if ($course->guest) {
1077 $strallowguests = get_string("allowguests");
1078 echo "<a title=\"$strallowguests\" href=\"$CFG->wwwroot/course/view.php?id=$course->id\">";
1079 echo "<img vspace=4 alt=\"$strallowguests\" height=16 width=16 border=0 src=\"$pixpath/i/user.gif\"></a>&nbsp;&nbsp;";
1080 }
1081 if ($course->password) {
1082 $strrequireskey = get_string("requireskey");
1083 echo "<a title=\"$strrequireskey\" href=\"$CFG->wwwroot/course/view.php?id=$course->id\">";
1084 echo "<img vspace=4 alt=\"$strrequireskey\" height=16 width=16 border=0 src=\"$pixpath/i/key.gif\"></a>";
1085 }
1086
1087
1088 echo "</td><td valign=top width=50%>";
1089 echo "<p><font size=2>".text_to_html($course->summary)."</font></p>";
1090 echo "</td></tr>";
1091 echo "</table>";
1092
1093 print_simple_box_end();
1094}
1095
1096
1097function print_my_moodle() {
1098/// Prints custom user information on the home page.
1099/// Over time this can include all sorts of information
1100
1101 global $USER, $CFG;
1102
1103 if (!isset($USER->id)) {
1104 error("It shouldn't be possible to see My Moodle without being logged in.");
1105 }
1106
1107 if ($courses = get_my_courses($USER->id)) {
1108 foreach ($courses as $course) {
1109 print_course($course, "100%");
1110 echo "<br />\n";
1111 }
607809b3 1112 }
c2cb4545 1113 echo "<p align=\"right\"><a href=\"$CFG->wwwroot/course/\">".get_string("fulllistofcourses")."</a>...</p>";
2b8cef80 1114}
1115
11b0c469 1116
1117
1118/// MODULE FUNCTIONS /////////////////////////////////////////////////////////////////
1119
1120function add_course_module($mod) {
11b0c469 1121
e5dfd0f3 1122 $mod->added = time();
11b0c469 1123
e5dfd0f3 1124 return insert_record("course_modules", $mod);
11b0c469 1125}
1126
7977cffd 1127function add_mod_to_section($mod, $beforemod=NULL) {
1128/// Given a full mod object with section and course already defined
1129/// If $before is specified, then this is an existing ID which we
1130/// will insert the new module before
1131///
1132/// Returns the course_sections ID where the mod is inserted
11b0c469 1133
9fa49e22 1134 if ($section = get_record("course_sections", "course", "$mod->course", "section", "$mod->section")) {
7977cffd 1135
1136 $section->sequence = trim($section->sequence);
1137
1138 if (empty($section->sequence)) {
11b0c469 1139 $newsequence = "$mod->coursemodule";
7977cffd 1140
1141 } else if ($beforemod) {
1142 $modarray = explode(",", $section->sequence);
1143
1144 if ($key = array_keys ($modarray, $beforemod->id)) {
1145 $insertarray = array($mod->id, $beforemod->id);
1146 array_splice($modarray, $key[0], 1, $insertarray);
1147 $newsequence = implode(",", $modarray);
1148
1149 } else { // Just tack it on the end anyway
1150 $newsequence = "$section->sequence,$mod->coursemodule";
1151 }
1152
1153 } else {
1154 $newsequence = "$section->sequence,$mod->coursemodule";
11b0c469 1155 }
7977cffd 1156
e5dfd0f3 1157 if (set_field("course_sections", "sequence", $newsequence, "id", $section->id)) {
1158 return $section->id; // Return course_sections ID that was used.
11b0c469 1159 } else {
e5dfd0f3 1160 return 0;
11b0c469 1161 }
1162
1163 } else { // Insert a new record
e5dfd0f3 1164 $section->course = $mod->course;
1165 $section->section = $mod->section;
1166 $section->summary = "";
1167 $section->sequence = $mod->coursemodule;
1168 return insert_record("course_sections", $section);
11b0c469 1169 }
1170}
1171
1acfbce5 1172function hide_course_module($mod) {
1173 return set_field("course_modules", "visible", 0, "id", $mod);
1174}
1175
1176function show_course_module($mod) {
1177 return set_field("course_modules", "visible", 1, "id", $mod);
1178}
1179
11b0c469 1180function delete_course_module($mod) {
1181 return set_field("course_modules", "deleted", 1, "id", $mod);
1182}
1183
1184function delete_mod_from_section($mod, $section) {
11b0c469 1185
e5dfd0f3 1186 if ($section = get_record("course_sections", "id", "$section") ) {
11b0c469 1187
e5dfd0f3 1188 $modarray = explode(",", $section->sequence);
11b0c469 1189
1190 if ($key = array_keys ($modarray, $mod)) {
1191 array_splice($modarray, $key[0], 1);
1192 $newsequence = implode(",", $modarray);
e5dfd0f3 1193 return set_field("course_sections", "sequence", $newsequence, "id", $section->id);
11b0c469 1194 } else {
1195 return false;
1196 }
1197
11b0c469 1198 }
7977cffd 1199 return false;
11b0c469 1200}
1201
12905134 1202function move_section($course, $section, $move) {
1203/// Moves a whole course section up and down within the course
1204
1205 if (!$move) {
1206 return true;
1207 }
1208
1209 $sectiondest = $section + $move;
1210
1211 if ($sectiondest > $course->numsections or $sectiondest < 1) {
1212 return false;
1213 }
1214
1215 if (!$sectionrecord = get_record("course_sections", "course", $course->id, "section", $section)) {
1216 return false;
1217 }
1218
1219 if (!$sectiondestrecord = get_record("course_sections", "course", $course->id, "section", $sectiondest)) {
1220 return false;
1221 }
1222
1223 $sectionrecord->section = $sectiondest;
1224 $sectiondestrecord->section = $section;
1225
1226 if (!update_record("course_sections", $sectionrecord)) {
1227 return false;
1228 }
1229 if (!update_record("course_sections", $sectiondestrecord)) {
1230 return false;
1231 }
1232 return true;
1233}
1234
1235
7977cffd 1236function moveto_module($mod, $section, $beforemod=NULL) {
1237/// All parameters are objects
1238/// Move the module object $mod to the specified $section
1239/// If $beforemod exists then that is the module
1240/// before which $modid should be inserted
1241
1242/// Remove original module from original section
1243
1244 if (! delete_mod_from_section($mod->id, $mod->section)) {
1245 notify("Could not delete module from existing section");
1246 }
1247
1248/// Update module itself if necessary
1249
1250 if ($mod->section != $section->id) {
1251 $mod->section = $section->id;
1252
1253 if (!update_record("course_modules", $mod)) {
1254 return false;
1255 }
1256 }
1257
1258/// Add the module into the new section
1259
1260 $mod->course = $section->course;
1261 $mod->section = $section->section; // need relative reference
1262 $mod->coursemodule = $mod->id;
1263
1264 if (! add_mod_to_section($mod, $beforemod)) {
1265 return false;
1266 }
1267
1268 return true;
1269
1270}
1271
1272
11b0c469 1273
7c0f2984 1274function move_module($cm, $move) {
12905134 1275/// Moves an activity module up and down within the course
11b0c469 1276
1277 if (!$move) {
1278 return true;
1279 }
1280
11b0c469 1281 if (! $thissection = get_record("course_sections", "id", $cm->section)) {
1282 error("This course section doesn't exist");
1283 }
1284
1285 $mods = explode(",", $thissection->sequence);
1286
1287 $len = count($mods);
1288 $pos = array_keys($mods, $cm->id);
1289 $thepos = $pos[0];
1290
1291 if ($len == 0 || count($pos) == 0 ) {
1292 error("Very strange. Could not find the required module in this section.");
1293 }
1294
1295 if ($len == 1) {
1296 $first = true;
1297 $last = true;
1298 } else {
1299 $first = ($thepos == 0);
1300 $last = ($thepos == $len - 1);
1301 }
1302
1303 if ($move < 0) { // Moving the module up
1304
1305 if ($first) {
ad41694c 1306 if ($thissection->section == 0) { // First section, do nothing
11b0c469 1307 return true;
ad41694c 1308
11b0c469 1309 } else { // Push onto end of previous section
1310 $prevsectionnumber = $thissection->section - 1;
9fa49e22 1311 if (! $prevsection = get_record("course_sections", "course", "$thissection->course",
1312 "section", "$prevsectionnumber")) {
11b0c469 1313 error("Previous section ($prevsection->id) doesn't exist");
1314 }
1315
74666583 1316 if (!empty($prevsection->sequence)) {
11b0c469 1317 $newsequence = "$prevsection->sequence,$cm->id";
1318 } else {
1319 $newsequence = "$cm->id";
1320 }
1321
1322 if (! set_field("course_sections", "sequence", $newsequence, "id", $prevsection->id)) {
1323 error("Previous section could not be updated");
1324 }
1325
1326 if (! set_field("course_modules", "section", $prevsection->id, "id", $cm->id)) {
1327 error("Module could not be updated");
1328 }
1329
1330 array_splice($mods, 0, 1);
1331 $newsequence = implode(",", $mods);
1332 if (! set_field("course_sections", "sequence", $newsequence, "id", $thissection->id)) {
1333 error("Module could not be updated");
1334 }
1335
1336 return true;
1337
1338 }
1339 } else { // move up within this section
1340 $swap = $mods[$thepos-1];
1341 $mods[$thepos-1] = $mods[$thepos];
1342 $mods[$thepos] = $swap;
1343
1344 $newsequence = implode(",", $mods);
1345 if (! set_field("course_sections", "sequence", $newsequence, "id", $thissection->id)) {
1346 error("This section could not be updated");
1347 }
1348 return true;
1349 }
1350
1351 } else { // Moving the module down
1352
1353 if ($last) {
1354 $nextsectionnumber = $thissection->section + 1;
9fa49e22 1355 if ($nextsection = get_record("course_sections", "course", "$thissection->course",
1356 "section", "$nextsectionnumber")) {
11b0c469 1357
74666583 1358 if (!empty($nextsection->sequence)) {
11b0c469 1359 $newsequence = "$cm->id,$nextsection->sequence";
1360 } else {
1361 $newsequence = "$cm->id";
1362 }
1363
1364 if (! set_field("course_sections", "sequence", $newsequence, "id", $nextsection->id)) {
1365 error("Next section could not be updated");
1366 }
1367
1368 if (! set_field("course_modules", "section", $nextsection->id, "id", $cm->id)) {
1369 error("Module could not be updated");
1370 }
1371
1372 array_splice($mods, $thepos, 1);
1373 $newsequence = implode(",", $mods);
1374 if (! set_field("course_sections", "sequence", $newsequence, "id", $thissection->id)) {
1375 error("This section could not be updated");
1376 }
1377 return true;
1378
1379 } else { // There is no next section, so just return
1380 return true;
1381
1382 }
1383 } else { // move down within this section
1384 $swap = $mods[$thepos+1];
1385 $mods[$thepos+1] = $mods[$thepos];
1386 $mods[$thepos] = $swap;
1387
1388 $newsequence = implode(",", $mods);
1389 if (! set_field("course_sections", "sequence", $newsequence, "id", $thissection->id)) {
1390 error("This section could not be updated");
1391 }
1392 return true;
1393 }
1394 }
1395}
1396
7977cffd 1397function make_editing_buttons($moduleid, $absolute=false, $visible=true, $moveselect=true) {
dc0dc7d5 1398 global $CFG, $THEME;
94361e02 1399
4de4fdfe 1400 static $str = '';
1acfbce5 1401 if (empty($str)) {
1402 $str->delete = get_string("delete");
493486c4 1403 $str->move = get_string("move");
1acfbce5 1404 $str->moveup = get_string("moveup");
1405 $str->movedown = get_string("movedown");
1406 $str->update = get_string("update");
1407 $str->hide = get_string("hide");
1408 $str->show = get_string("show");
1409 }
94361e02 1410
1411 if ($absolute) {
dc0dc7d5 1412 $path = "$CFG->wwwroot/course";
1413 } else {
1414 $path = ".";
1415 }
1416
1417 if (empty($THEME->custompix)) {
1418 $pixpath = "$path/../pix";
94361e02 1419 } else {
dc0dc7d5 1420 $pixpath = "$path/../theme/$CFG->theme/pix";
94361e02 1421 }
1acfbce5 1422
1423 if ($visible) {
7977cffd 1424 $hideshow = "<a title=\"$str->hide\" href=\"$path/mod.php?hide=$moduleid\"><img".
1425 " src=\"$pixpath/t/hide.gif\" hspace=2 height=11 width=11 border=0></a> ";
1acfbce5 1426 } else {
7977cffd 1427 $hideshow = "<a title=\"$str->show\" href=\"$path/mod.php?show=$moduleid\"><img".
1428 " src=\"$pixpath/t/show.gif\" hspace=2 height=11 width=11 border=0></a> ";
1429 }
1430
1431 if ($moveselect) {
1432 $move = "<a title=\"$str->move\" href=\"$path/mod.php?copy=$moduleid\"><img".
1433 " src=\"$pixpath/t/move.gif\" height=\"11\" width=\"11\" border=\"0\"></a> ";
493486c4 1434 } else {
1435 $move = "<a title=\"$str->moveup\" href=\"$path/mod.php?id=$moduleid&move=-1\"><img".
1436 " src=\"$pixpath/t/up.gif\" height=11 width=11 border=0></a> ".
1437 "<a title=\"$str->movedown\" href=\"$path/mod.php?id=$moduleid&move=1\"><img".
1438 " src=\"$pixpath/t/down.gif\" height=11 width=11 border=0></a> ";
7977cffd 1439 }
1440
1441 return "<a title=\"$str->delete\" href=\"$path/mod.php?delete=$moduleid\"><img".
493486c4 1442 " src=\"$pixpath/t/delete.gif\" height=11 width=11 border=0></a> $move".
7977cffd 1443 "<a title=\"$str->update\" href=\"$path/mod.php?update=$moduleid\"><img".
1444 " src=\"$pixpath/t/edit.gif\" height=11 width=11 border=0></a> $hideshow";
90845098 1445}
1446
f9903ed0 1447?>