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