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