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