Some upper/lower case consistency
[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()) {
d2b6ba70 716 $moddata[]="<a href=\"$CFG->wwwroot/course/index.php\">".get_string("coursemanagement")."</a>";
1924074c 717 $modicon[]=$icon;
d2b6ba70 718 $moddata[]="<a href=\"$CFG->wwwroot/course/edit.php\">".get_string("addnewcourse")."</a>";
1924074c 719 $modicon[]=$icon;
5b337cdf 720 $fulladmin = "";
1924074c 721 }
722 if (isadmin()) {
434d0139 723 $moddata[]="<a href=\"$CFG->wwwroot/files/index.php?id=$siteid\">".get_string("courserestore")."</a>";
724 $modicon[]=$icon;
5867bfb5 725 $moddata[]="<hr>";
1924074c 726 $modicon[]="";
3399a22c 727 if($CFG->auth == "email" || $CFG->auth == "none" || $CFG->auth == "manual"){
5867bfb5 728 $moddata[]="<a href=\"$CFG->wwwroot/$CFG->admin/user.php?newuser=true\">".get_string("addnewuser")."</a>";
3399a22c 729 $modicon[]=$icon;
730 }
5867bfb5 731 $moddata[]="<a href=\"$CFG->wwwroot/$CFG->admin/user.php\">".get_string("edituser")."</a>";
1924074c 732 $modicon[]=$icon;
5867bfb5 733 $moddata[]="<a href=\"$CFG->wwwroot/$CFG->admin/admin.php\">".get_string("assignadmins")."</a>";
1924074c 734 $modicon[]=$icon;
5867bfb5 735 $moddata[]="<a href=\"$CFG->wwwroot/$CFG->admin/creators.php\">".get_string("assigncreators")."</a>";
1924074c 736 $modicon[]=$icon;
5867bfb5 737 $moddata[]="<a href=\"$CFG->wwwroot/$CFG->admin/auth.php\">".get_string("authentication")."</a>";
1924074c 738 $modicon[]=$icon;
5867bfb5 739 $fulladmin = "<p><a href=\"$CFG->wwwroot/$CFG->admin/\">".get_string("admin")."</a>...";
1924074c 740 }
5e367a2d 741
742 print_side_block(get_string("administration"), "", $moddata, $modicon, $fulladmin, $width);
743
dc0dc7d5 744 echo "<img src=\"$CFG->wwwroot/pix/spacer.gif\" width=\"$width\" height=1><br>";
2b25f2a0 745}
746
b4d7002e 747function print_course_admin_links($course, $width=180) {
dc0dc7d5 748 global $USER, $CFG, $THEME;
44dad735 749
fce1ce13 750 if (isguest()) {
751 return true;
752 }
dc0dc7d5 753 if (empty($THEME->custompix)) {
754 $pixpath = "$CFG->wwwroot/pix";
755 $modpixpath = "$CFG->wwwroot/mod";
756 } else {
757 $pixpath = "$CFG->wwwroot/theme/$CFG->theme/pix";
758 $modpixpath = "$CFG->wwwroot/theme/$CFG->theme/pix/mod";
759 }
13469b82 760 if (isteacher($course->id)) {
dc0dc7d5 761 $adminicon[]="<img src=\"$pixpath/i/edit.gif\" height=16 width=16 alt=\"\">";
13469b82 762 if (isediting($course->id)) {
dc0dc7d5 763 $admindata[]="<a href=\"view.php?id=$course->id&edit=off\">".get_string("turneditingoff")."</a>";
13469b82 764 } else {
dc0dc7d5 765 $admindata[]="<a href=\"view.php?id=$course->id&edit=on\">".get_string("turneditingon")."</a>";
13469b82 766 }
dc0dc7d5 767 $admindata[]="<a href=\"edit.php?id=$course->id\">".get_string("settings")."...</a>";
768 $adminicon[]="<img src=\"$pixpath/i/settings.gif\" height=16 width=16 alt=\"\">";
13469b82 769 if (!$course->teachers) {
770 $course->teachers = get_string("defaultcourseteachers");
771 }
dc0dc7d5 772 $admindata[]="<a href=\"teachers.php?id=$course->id\">$course->teachers...</a>";
773 $adminicon[]="<img src=\"$pixpath/i/settings.gif\" height=16 width=16 alt=\"\">";
13469b82 774
dc0dc7d5 775 $admindata[]="<a href=\"grades.php?id=$course->id\">".get_string("grades")."...</a>";
776 $adminicon[]="<img src=\"$pixpath/i/grades.gif\" height=16 width=16 alt=\"\">";
13469b82 777
dc0dc7d5 778 $admindata[]="<a href=\"log.php?id=$course->id\">".get_string("logs")."...</a>";
779 $adminicon[]="<img src=\"$pixpath/i/log.gif\" height=16 width=16 alt=\"\">";
780
781 $admindata[]="<a href=\"$CFG->wwwroot/files/index.php?id=$course->id\">".get_string("files")."...</a>";
782 $adminicon[]="<img src=\"$pixpath/i/files.gif\" height=16 width=16 alt=\"\">";
f354106d 783
784 $admindata[]="<a href=\"$CFG->wwwroot/backup/backup.php?id=$course->id\">".get_string("backup")."...</a>";
785 $adminicon[]="<img src=\"$pixpath/i/backup.gif\" height=16 width=16 alt=\"\">";
03162427 786
787 //Only showed if "backupdata" dir exists
788 if (is_dir($CFG->dataroot."/".$course->id."/".backupdata)) {
789 $admindata[]="<a href=\"$CFG->wwwroot/files/index.php?id=$course->id&wdir=/backupdata\">".get_string("restore")."...</a>";
790 $adminicon[]="<img src=\"$pixpath/i/restore.gif\" height=16 width=16 alt=\"\">";
791 }
13469b82 792
dc0dc7d5 793 $admindata[]="<a href=\"$CFG->wwwroot/doc/view.php?id=$course->id&file=teacher.html\">".get_string("help")."...</a>";
794 $adminicon[]="<img src=\"$modpixpath/resource/icon.gif\" height=16 width=16 alt=\"\">";
b4d7002e 795
13469b82 796 if ($teacherforum = forum_get_course_forum($course->id, "teacher")) {
dc0dc7d5 797 $admindata[]="<a href=\"$CFG->wwwroot/mod/forum/view.php?f=$teacherforum->id\">".get_string("nameteacher", "forum")."</a>";
798 $adminicon[]="<img src=\"$modpixpath/forum/icon.gif\" height=16 width=16 alt=\"\">";
13469b82 799 }
f354106d 800
13469b82 801 } else {
dc0dc7d5 802 $admindata[]="<a href=\"grade.php?id=$course->id\">".get_string("grades")."...</a>";
803 $adminicon[]="<img src=\"$pixpath/i/grades.gif\" height=16 width=16 alt=\"\">";
13469b82 804 }
44dad735 805
f354106d 806 if (!empty($admindata)) {
807 print_side_block(get_string("administration"), "", $admindata, $adminicon, "", $width);
808 }
44dad735 809}
2b25f2a0 810
c2cb4545 811
812function make_categories_list(&$list, &$parents, $category=NULL, $path="") {
813/// Given an empty array, this function recursively travels the
814/// categories, building up a nice list for display. It also makes
815/// an array that list all the parents for each category.
816
817 if ($category) {
818 if ($path) {
819 $path = "$path / $category->name";
820 } else {
821 $path = "$category->name";
822 }
823 $list[$category->id] = $path;
824 } else {
825 $category->id = 0;
826 }
827
828 if ($categories = get_categories("$category->id")) { // Print all the children recursively
829 foreach ($categories as $cat) {
830 if (!empty($category->id)) {
831 $parents[$cat->id] = $parents[$category->id];
832 $parents[$cat->id][] = $category->id;
833 }
e92fe848 834 make_categories_list($list, $parents, $cat, $path);
c2cb4545 835 }
836 }
837}
838
839
c2cb4545 840function print_whole_category_list($category=NULL, $displaylist=NULL, $parentslist=NULL, $depth=-1) {
841/// Recursive function to print out all the categories in a nice format
842/// with or without courses included
843
844 if (!$displaylist) {
e92fe848 845 make_categories_list($displaylist, $parentslist);
c2cb4545 846 }
847
848 if ($category) {
849 if ($category->visible or isadmin()) {
d2b6ba70 850 print_category_info($category, $depth);
c2cb4545 851 } else {
852 return; // Don't bother printing children of invisible categories
853 }
854
855 } else {
c2cb4545 856 $category->id = "0";
d2b6ba70 857 echo "<table width=\"100%\" class=\"categorybox\">";
858 $toplevel = true;
c2cb4545 859 }
860
861 if ($categories = get_categories($category->id)) { // Print all the children recursively
862 $countcats = count($categories);
863 $count = 0;
864 $first = true;
865 $last = false;
866 foreach ($categories as $cat) {
867 $count++;
868 if ($count == $countcats) {
869 $last = true;
870 }
871 $up = $first ? false : true;
872 $down = $last ? false : true;
873 $first = false;
874
875 print_whole_category_list($cat, $displaylist, $parentslist, $depth + 1);
876 }
877 }
878
d2b6ba70 879 if (isset($toplevel)) {
880 echo "</table>";
c2cb4545 881 }
882}
883
884
d2b6ba70 885function print_category_info($category, $depth) {
886/// Prints the category info in indented fashion
887/// This function is only used by print_whole_category_list() above
c2cb4545 888
889 global $CFG;
890
85098089 891 $strallowguests = get_string("allowguests");
892 $strrequireskey = get_string("requireskey");
ba2e5d73 893
dc0dc7d5 894 if (empty($THEME->custompix)) {
895 $pixpath = "$CFG->wwwroot/pix";
896 } else {
897 $pixpath = "$CFG->wwwroot/theme/$CFG->theme/pix";
898 }
899
c2cb4545 900 $catlinkcss = $category->visible ? "" : " class=\"dimmed\" ";
901
c2cb4545 902 echo "<tr>";
d2b6ba70 903 echo "<td valign=\"top\">";
904 for ($i=0; $i<$depth;$i++) {
6f065ce3 905 echo "<ul style=\"margin-bottom:0;margin-top:0\">";
d2b6ba70 906 }
907 echo "<font size=+1><a $catlinkcss ".
908 "href=\"$CFG->wwwroot/course/category.php?id=$category->id\">$category->name</a></font>";
909
c2cb4545 910 if ($CFG->frontpage == FRONTPAGECOURSELIST) {
d2b6ba70 911 if ($courses = get_courses($category->id)) {
c2cb4545 912 foreach ($courses as $course) {
6f065ce3 913 echo "<ul style=\"margin-bottom:0;margin-top:0\">";
c2cb4545 914 $linkcss = $course->visible ? "" : " class=\"dimmed\" ";
a6870f0c 915 echo "<a $linkcss href=\"$CFG->wwwroot/course/view.php?id=$course->id\">$course->fullname</a>";
c2cb4545 916 echo "&nbsp;&nbsp;";
917 unset($courses[$key]);
918 if ($course->guest ) {
919 echo "<a title=\"$strallowguests\" href=\"$CFG->wwwroot/course/view.php?id=$course->id\">";
920 echo "<img alt=\"\" height=16 width=16 border=0 src=\"$pixpath/i/user.gif\"></a>";
0c656181 921 }
c2cb4545 922 if ($course->password) {
923 echo "<a title=\"$strrequireskey\" href=\"$CFG->wwwroot/course/view.php?id=$course->id\">";
924 echo "<img alt=\"\" height=16 width=16 border=0 src=\"$pixpath/i/key.gif\"></a>";
0c656181 925 }
d2b6ba70 926 echo "</ul>";
0c656181 927 }
ba2e5d73 928 }
d2b6ba70 929 for ($i=0; $i<$depth;$i++) {
930 echo "</ul>";
931 }
932 echo "</td>";
933 echo "</tr>";
934 } else {
935 for ($i=0; $i<$depth;$i++) {
936 echo "</ul>";
937 }
938 echo "</td>";
939 echo "<td valign=\"top\">";
940 if ($count = count_records("course", "category", $category->id)) {
941 echo $count;
942 }
943 echo "</td>";
944 echo "</tr>";
c2cb4545 945 }
c2cb4545 946}
947
948function print_courses_sideblock($category=0, $width="100%") {
949 global $CFG, $THEME;
950
951 if (empty($THEME->custompix)) {
952 $icon = "<img src=\"$CFG->wwwroot/pix/i/course.gif\"".
953 " height=\"16\" width=\"16\" alt=\"".get_string("course")."\">";
954 } else {
955 $icon = "<img src=\"$CFG->wwwroot/theme/$CFG->theme/pix/i/course.gif\"".
956 " height=\"16\" width=\"16\" alt=\"".get_string("course")."\">";
957 }
958
959 $categories = get_categories(0); // Parent = 0 ie top-level categories only
960 if (count($categories) > 1) { // Just print top level category links
961 foreach ($categories as $category) {
962 $linkcss = $category->visible ? "" : " class=\"dimmed\" ";
d2b6ba70 963 $moddata[]="<a $linkcss href=\"$CFG->wwwroot/course/category.php?id=$category->id\">$category->name</a>";
c2cb4545 964 $modicon[]=$icon;
0c656181 965 }
c2cb4545 966 } else { // Just print course names of single category
90c2ca2e 967 $category = array_shift($categories);
d2b6ba70 968 $courses = get_courses($category->id);
969
c2cb4545 970 if ($courses) {
971 foreach ($courses as $course) {
972 $linkcss = $course->visible ? "" : " class=\"dimmed\" ";
973 $moddata[]="<a $linkcss title=\"$course->shortname\" ".
974 "href=\"$CFG->wwwroot/course/view.php?id=$course->id\">$course->fullname</a>";
975 $modicon[]=$icon;
976 }
977 $fulllist = "<p><a href=\"$CFG->wwwroot/course/\">".get_string("fulllistofcourses")."</a>...";
978 } else {
979 $moddata = array();
980 $modicon = array();
981 $fulllist = get_string("nocoursesyet");
0c656181 982 }
ba2e5d73 983 }
c2cb4545 984
985 print_side_block(get_string("courses"), "", $moddata, $modicon, $fulllist, $width);
ba2e5d73 986}
94361e02 987
c2cb4545 988
989function print_courses($category, $width="100%") {
990/// Category is 0 (for all courses) or an object
991
992 global $CFG, $THEME;
993
994 if (empty($category)) {
90c2ca2e 995 $categories = get_categories(0); // Parent = 0 ie top-level categories only
996 if (count($categories) == 1) {
997 $category = array_shift($categories);
d2b6ba70 998 $courses = get_courses($category->id);
90c2ca2e 999 } else {
d2b6ba70 1000 $courses = get_courses("all");
90c2ca2e 1001 }
1002 unset($categories);
607809b3 1003 } else {
c2cb4545 1004 $categories = get_categories($category->id); // sub categories
d2b6ba70 1005 $courses = get_courses($category->id);
c2cb4545 1006 }
1007
1008 if ($categories) {
1009 print_simple_box_start("center");
1010 print_heading(get_string("subcategories"));
1011 foreach ($categories as $category) {
1012 $linkcss = $category->visible ? "" : " class=\"dimmed\" ";
1013 echo "<p align=\"center\"><a $linkcss".
1014 " href=\"$CFG->wwwroot/course/index.php?category=$category->id\">$category->name</a></p>";
1015 }
1016 print_simple_box_end();
1017 }
1018
1019 if ($courses) {
1020 foreach ($courses as $course) {
1021 print_course($course, $width);
1022 echo "<br />\n";
1023 }
1024 } else {
1025 print_heading(get_string("nocoursesyet"));
1026 }
1027
1028}
1029
1030
1031function print_course($course, $width="100%") {
1032
1033 global $CFG, $THEME;
1034
1035 if (! $site = get_site()) {
1036 error("Could not find a site!");
1037 }
1038
1039 if (empty($THEME->custompix)) {
1040 $pixpath = "$CFG->wwwroot/pix";
1041 } else {
1042 $pixpath = "$CFG->wwwroot/theme/$CFG->theme/pix";
1043 }
1044
1045 print_simple_box_start("center", "$width");
1046
1047 echo "<table width=\"100%\">";
1048 echo "<tr valign=top>";
1049 echo "<td valign=top width=50%>";
1050 echo "<p><font size=3><b><a title=\"".get_string("entercourse")."\"
1051 href=\"$CFG->wwwroot/course/view.php?id=$course->id\">$course->fullname</a></b></font></p>";
1052 if ($teachers = get_course_teachers($course->id)) {
1053 echo "<p><font size=\"1\">\n";
1054 foreach ($teachers as $teacher) {
1055 if ($teacher->authority > 0) {
1056 if (!$teacher->role) {
1057 $teacher->role = $course->teacher;
1058 }
1059 echo "$teacher->role: <a href=\"$CFG->wwwroot/user/view.php?id=$teacher->id&course=$site->id\">$teacher->firstname $teacher->lastname</a><br />";
1060 }
1061 }
1062 echo "</font></p>";
1063 }
1064 if ($course->guest) {
1065 $strallowguests = get_string("allowguests");
1066 echo "<a title=\"$strallowguests\" href=\"$CFG->wwwroot/course/view.php?id=$course->id\">";
1067 echo "<img vspace=4 alt=\"$strallowguests\" height=16 width=16 border=0 src=\"$pixpath/i/user.gif\"></a>&nbsp;&nbsp;";
1068 }
1069 if ($course->password) {
1070 $strrequireskey = get_string("requireskey");
1071 echo "<a title=\"$strrequireskey\" href=\"$CFG->wwwroot/course/view.php?id=$course->id\">";
1072 echo "<img vspace=4 alt=\"$strrequireskey\" height=16 width=16 border=0 src=\"$pixpath/i/key.gif\"></a>";
1073 }
1074
1075
1076 echo "</td><td valign=top width=50%>";
1077 echo "<p><font size=2>".text_to_html($course->summary)."</font></p>";
1078 echo "</td></tr>";
1079 echo "</table>";
1080
1081 print_simple_box_end();
1082}
1083
1084
1085function print_my_moodle() {
1086/// Prints custom user information on the home page.
1087/// Over time this can include all sorts of information
1088
1089 global $USER, $CFG;
1090
1091 if (!isset($USER->id)) {
1092 error("It shouldn't be possible to see My Moodle without being logged in.");
1093 }
1094
1095 if ($courses = get_my_courses($USER->id)) {
1096 foreach ($courses as $course) {
1097 print_course($course, "100%");
1098 echo "<br />\n";
1099 }
607809b3 1100 }
c2cb4545 1101 echo "<p align=\"right\"><a href=\"$CFG->wwwroot/course/\">".get_string("fulllistofcourses")."</a>...</p>";
2b8cef80 1102}
1103
11b0c469 1104
1105
1106/// MODULE FUNCTIONS /////////////////////////////////////////////////////////////////
1107
1108function add_course_module($mod) {
11b0c469 1109
e5dfd0f3 1110 $mod->added = time();
11b0c469 1111
e5dfd0f3 1112 return insert_record("course_modules", $mod);
11b0c469 1113}
1114
7977cffd 1115function add_mod_to_section($mod, $beforemod=NULL) {
1116/// Given a full mod object with section and course already defined
1117/// If $before is specified, then this is an existing ID which we
1118/// will insert the new module before
1119///
1120/// Returns the course_sections ID where the mod is inserted
11b0c469 1121
9fa49e22 1122 if ($section = get_record("course_sections", "course", "$mod->course", "section", "$mod->section")) {
7977cffd 1123
1124 $section->sequence = trim($section->sequence);
1125
1126 if (empty($section->sequence)) {
11b0c469 1127 $newsequence = "$mod->coursemodule";
7977cffd 1128
1129 } else if ($beforemod) {
1130 $modarray = explode(",", $section->sequence);
1131
1132 if ($key = array_keys ($modarray, $beforemod->id)) {
1133 $insertarray = array($mod->id, $beforemod->id);
1134 array_splice($modarray, $key[0], 1, $insertarray);
1135 $newsequence = implode(",", $modarray);
1136
1137 } else { // Just tack it on the end anyway
1138 $newsequence = "$section->sequence,$mod->coursemodule";
1139 }
1140
1141 } else {
1142 $newsequence = "$section->sequence,$mod->coursemodule";
11b0c469 1143 }
7977cffd 1144
e5dfd0f3 1145 if (set_field("course_sections", "sequence", $newsequence, "id", $section->id)) {
1146 return $section->id; // Return course_sections ID that was used.
11b0c469 1147 } else {
e5dfd0f3 1148 return 0;
11b0c469 1149 }
1150
1151 } else { // Insert a new record
e5dfd0f3 1152 $section->course = $mod->course;
1153 $section->section = $mod->section;
1154 $section->summary = "";
1155 $section->sequence = $mod->coursemodule;
1156 return insert_record("course_sections", $section);
11b0c469 1157 }
1158}
1159
1acfbce5 1160function hide_course_module($mod) {
1161 return set_field("course_modules", "visible", 0, "id", $mod);
1162}
1163
1164function show_course_module($mod) {
1165 return set_field("course_modules", "visible", 1, "id", $mod);
1166}
1167
11b0c469 1168function delete_course_module($mod) {
1169 return set_field("course_modules", "deleted", 1, "id", $mod);
1170}
1171
1172function delete_mod_from_section($mod, $section) {
11b0c469 1173
e5dfd0f3 1174 if ($section = get_record("course_sections", "id", "$section") ) {
11b0c469 1175
e5dfd0f3 1176 $modarray = explode(",", $section->sequence);
11b0c469 1177
1178 if ($key = array_keys ($modarray, $mod)) {
1179 array_splice($modarray, $key[0], 1);
1180 $newsequence = implode(",", $modarray);
e5dfd0f3 1181 return set_field("course_sections", "sequence", $newsequence, "id", $section->id);
11b0c469 1182 } else {
1183 return false;
1184 }
1185
11b0c469 1186 }
7977cffd 1187 return false;
11b0c469 1188}
1189
12905134 1190function move_section($course, $section, $move) {
1191/// Moves a whole course section up and down within the course
1192
1193 if (!$move) {
1194 return true;
1195 }
1196
1197 $sectiondest = $section + $move;
1198
1199 if ($sectiondest > $course->numsections or $sectiondest < 1) {
1200 return false;
1201 }
1202
1203 if (!$sectionrecord = get_record("course_sections", "course", $course->id, "section", $section)) {
1204 return false;
1205 }
1206
1207 if (!$sectiondestrecord = get_record("course_sections", "course", $course->id, "section", $sectiondest)) {
1208 return false;
1209 }
1210
1211 $sectionrecord->section = $sectiondest;
1212 $sectiondestrecord->section = $section;
1213
1214 if (!update_record("course_sections", $sectionrecord)) {
1215 return false;
1216 }
1217 if (!update_record("course_sections", $sectiondestrecord)) {
1218 return false;
1219 }
1220 return true;
1221}
1222
1223
7977cffd 1224function moveto_module($mod, $section, $beforemod=NULL) {
1225/// All parameters are objects
1226/// Move the module object $mod to the specified $section
1227/// If $beforemod exists then that is the module
1228/// before which $modid should be inserted
1229
1230/// Remove original module from original section
1231
1232 if (! delete_mod_from_section($mod->id, $mod->section)) {
1233 notify("Could not delete module from existing section");
1234 }
1235
1236/// Update module itself if necessary
1237
1238 if ($mod->section != $section->id) {
1239 $mod->section = $section->id;
1240
1241 if (!update_record("course_modules", $mod)) {
1242 return false;
1243 }
1244 }
1245
1246/// Add the module into the new section
1247
1248 $mod->course = $section->course;
1249 $mod->section = $section->section; // need relative reference
1250 $mod->coursemodule = $mod->id;
1251
1252 if (! add_mod_to_section($mod, $beforemod)) {
1253 return false;
1254 }
1255
1256 return true;
1257
1258}
1259
1260
11b0c469 1261
7c0f2984 1262function move_module($cm, $move) {
12905134 1263/// Moves an activity module up and down within the course
11b0c469 1264
1265 if (!$move) {
1266 return true;
1267 }
1268
11b0c469 1269 if (! $thissection = get_record("course_sections", "id", $cm->section)) {
1270 error("This course section doesn't exist");
1271 }
1272
1273 $mods = explode(",", $thissection->sequence);
1274
1275 $len = count($mods);
1276 $pos = array_keys($mods, $cm->id);
1277 $thepos = $pos[0];
1278
1279 if ($len == 0 || count($pos) == 0 ) {
1280 error("Very strange. Could not find the required module in this section.");
1281 }
1282
1283 if ($len == 1) {
1284 $first = true;
1285 $last = true;
1286 } else {
1287 $first = ($thepos == 0);
1288 $last = ($thepos == $len - 1);
1289 }
1290
1291 if ($move < 0) { // Moving the module up
1292
1293 if ($first) {
ad41694c 1294 if ($thissection->section == 0) { // First section, do nothing
11b0c469 1295 return true;
ad41694c 1296
11b0c469 1297 } else { // Push onto end of previous section
1298 $prevsectionnumber = $thissection->section - 1;
9fa49e22 1299 if (! $prevsection = get_record("course_sections", "course", "$thissection->course",
1300 "section", "$prevsectionnumber")) {
11b0c469 1301 error("Previous section ($prevsection->id) doesn't exist");
1302 }
1303
74666583 1304 if (!empty($prevsection->sequence)) {
11b0c469 1305 $newsequence = "$prevsection->sequence,$cm->id";
1306 } else {
1307 $newsequence = "$cm->id";
1308 }
1309
1310 if (! set_field("course_sections", "sequence", $newsequence, "id", $prevsection->id)) {
1311 error("Previous section could not be updated");
1312 }
1313
1314 if (! set_field("course_modules", "section", $prevsection->id, "id", $cm->id)) {
1315 error("Module could not be updated");
1316 }
1317
1318 array_splice($mods, 0, 1);
1319 $newsequence = implode(",", $mods);
1320 if (! set_field("course_sections", "sequence", $newsequence, "id", $thissection->id)) {
1321 error("Module could not be updated");
1322 }
1323
1324 return true;
1325
1326 }
1327 } else { // move up within this section
1328 $swap = $mods[$thepos-1];
1329 $mods[$thepos-1] = $mods[$thepos];
1330 $mods[$thepos] = $swap;
1331
1332 $newsequence = implode(",", $mods);
1333 if (! set_field("course_sections", "sequence", $newsequence, "id", $thissection->id)) {
1334 error("This section could not be updated");
1335 }
1336 return true;
1337 }
1338
1339 } else { // Moving the module down
1340
1341 if ($last) {
1342 $nextsectionnumber = $thissection->section + 1;
9fa49e22 1343 if ($nextsection = get_record("course_sections", "course", "$thissection->course",
1344 "section", "$nextsectionnumber")) {
11b0c469 1345
74666583 1346 if (!empty($nextsection->sequence)) {
11b0c469 1347 $newsequence = "$cm->id,$nextsection->sequence";
1348 } else {
1349 $newsequence = "$cm->id";
1350 }
1351
1352 if (! set_field("course_sections", "sequence", $newsequence, "id", $nextsection->id)) {
1353 error("Next section could not be updated");
1354 }
1355
1356 if (! set_field("course_modules", "section", $nextsection->id, "id", $cm->id)) {
1357 error("Module could not be updated");
1358 }
1359
1360 array_splice($mods, $thepos, 1);
1361 $newsequence = implode(",", $mods);
1362 if (! set_field("course_sections", "sequence", $newsequence, "id", $thissection->id)) {
1363 error("This section could not be updated");
1364 }
1365 return true;
1366
1367 } else { // There is no next section, so just return
1368 return true;
1369
1370 }
1371 } else { // move down within this section
1372 $swap = $mods[$thepos+1];
1373 $mods[$thepos+1] = $mods[$thepos];
1374 $mods[$thepos] = $swap;
1375
1376 $newsequence = implode(",", $mods);
1377 if (! set_field("course_sections", "sequence", $newsequence, "id", $thissection->id)) {
1378 error("This section could not be updated");
1379 }
1380 return true;
1381 }
1382 }
1383}
1384
7977cffd 1385function make_editing_buttons($moduleid, $absolute=false, $visible=true, $moveselect=true) {
dc0dc7d5 1386 global $CFG, $THEME;
94361e02 1387
4de4fdfe 1388 static $str = '';
1acfbce5 1389 if (empty($str)) {
1390 $str->delete = get_string("delete");
493486c4 1391 $str->move = get_string("move");
1acfbce5 1392 $str->moveup = get_string("moveup");
1393 $str->movedown = get_string("movedown");
1394 $str->update = get_string("update");
1395 $str->hide = get_string("hide");
1396 $str->show = get_string("show");
1397 }
94361e02 1398
1399 if ($absolute) {
dc0dc7d5 1400 $path = "$CFG->wwwroot/course";
1401 } else {
1402 $path = ".";
1403 }
1404
1405 if (empty($THEME->custompix)) {
1406 $pixpath = "$path/../pix";
94361e02 1407 } else {
dc0dc7d5 1408 $pixpath = "$path/../theme/$CFG->theme/pix";
94361e02 1409 }
1acfbce5 1410
1411 if ($visible) {
7977cffd 1412 $hideshow = "<a title=\"$str->hide\" href=\"$path/mod.php?hide=$moduleid\"><img".
1413 " src=\"$pixpath/t/hide.gif\" hspace=2 height=11 width=11 border=0></a> ";
1acfbce5 1414 } else {
7977cffd 1415 $hideshow = "<a title=\"$str->show\" href=\"$path/mod.php?show=$moduleid\"><img".
1416 " src=\"$pixpath/t/show.gif\" hspace=2 height=11 width=11 border=0></a> ";
1417 }
1418
1419 if ($moveselect) {
1420 $move = "<a title=\"$str->move\" href=\"$path/mod.php?copy=$moduleid\"><img".
1421 " src=\"$pixpath/t/move.gif\" height=\"11\" width=\"11\" border=\"0\"></a> ";
493486c4 1422 } else {
1423 $move = "<a title=\"$str->moveup\" href=\"$path/mod.php?id=$moduleid&move=-1\"><img".
1424 " src=\"$pixpath/t/up.gif\" height=11 width=11 border=0></a> ".
1425 "<a title=\"$str->movedown\" href=\"$path/mod.php?id=$moduleid&move=1\"><img".
1426 " src=\"$pixpath/t/down.gif\" height=11 width=11 border=0></a> ";
7977cffd 1427 }
1428
1429 return "<a title=\"$str->delete\" href=\"$path/mod.php?delete=$moduleid\"><img".
493486c4 1430 " src=\"$pixpath/t/delete.gif\" height=11 width=11 border=0></a> $move".
7977cffd 1431 "<a title=\"$str->update\" href=\"$path/mod.php?update=$moduleid\"><img".
1432 " src=\"$pixpath/t/edit.gif\" height=11 width=11 border=0></a> $hideshow";
90845098 1433}
1434
f9903ed0 1435?>