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