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