Fixed missing "search results" string
[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>";
519d369f 89 echo "<INPUT TYPE=hidden NAME=chooselog VALUE=\"1\">";
720a43ce 90 if (isadmin()) {
849bc02a 91 choose_from_menu ($courses, "id", $course->id, "");
720a43ce 92 } else {
93 echo "<INPUT TYPE=hidden NAME=id VALUE=\"$course->id\">";
94 }
95 if ($course->category) {
97c270e9 96 choose_from_menu ($users, "user", $selecteduser, get_string("allparticipants") );
720a43ce 97 }
97c270e9 98 choose_from_menu ($dates, "date", $selecteddate, get_string("alldays"));
99 echo "<INPUT TYPE=submit VALUE=\"".get_string("showtheselogs")."\">";
f9903ed0 100 echo "</FORM>";
101 echo "</CENTER>";
102}
103
600149be 104function make_log_url($module, $url) {
105 switch ($module) {
106 case "course":
107 case "user":
108 case "file":
109 case "login":
110 case "lib":
111 case "admin":
112 return "/$module/$url";
113 break;
114 default:
115 return "/mod/$module/$url";
116 break;
117 }
118}
119
120
519d369f 121function print_log($course, $user=0, $date=0, $order="l.time ASC", $page=0, $perpage=100, $url="") {
9481285b 122// It is assumed that $date is the GMT time of midnight for that day,
123// and so the next 86400 seconds worth of logs are printed.
f9903ed0 124
3d891989 125 global $CFG;
47f1da80 126
720a43ce 127 if ($course->category) {
519d369f 128 $selector = "l.course='$course->id' AND l.userid = u.id";
a2ab3b05 129
720a43ce 130 } else {
519d369f 131 $selector = "l.userid = u.id"; // Show all courses
9fa49e22 132 if ($ccc = get_courses(-1)) {
720a43ce 133 foreach ($ccc as $cc) {
134 $courses[$cc->id] = "$cc->shortname";
135 }
136 }
137 }
f9903ed0 138
139 if ($user) {
ebc3bd2b 140 $selector .= " AND l.userid = '$user'";
f9903ed0 141 }
142
143 if ($date) {
144 $enddate = $date + 86400;
145 $selector .= " AND l.time > '$date' AND l.time < '$enddate'";
146 }
147
519d369f 148 if (!$logs = get_logs($selector, $order, $page*$perpage, $perpage, $totalcount)) {
f9903ed0 149 notify("No logs found!");
150 print_footer($course);
151 exit;
152 }
153
154 $count=0;
155 $tt = getdate(time());
156 $today = mktime (0, 0, 0, $tt["mon"], $tt["mday"], $tt["year"]);
1c0200e0 157
dcde9f02 158 $strftimedatetime = get_string("strftimedatetime");
159
f9d5fd3b 160 echo "<p align=center>";
519d369f 161 print_string("displayingrecords", "", $totalcount);
f9d5fd3b 162 echo "</p>";
1c0200e0 163
519d369f 164 print_paging_bar($totalcount, $page, $perpage, "$url&perpage=$perpage&");
165
f9d5fd3b 166 echo "<table border=0 align=center cellpadding=3 cellspacing=3>";
f9903ed0 167 foreach ($logs as $log) {
600149be 168
9fa49e22 169 if ($ld = get_record("log_display", "module", "$log->module", "action", "$log->action")) {
565f7a95 170 $log->info = get_field($ld->mtable, $ld->field, "id", $log->info);
600149be 171 }
172
f9d5fd3b 173 echo "<tr nowrap>";
720a43ce 174 if (! $course->category) {
f9d5fd3b 175 echo "<td nowrap><font size=2><a href=\"view.php?id=$log->course\">".$courses[$log->course]."</a></td>";
720a43ce 176 }
f9d5fd3b 177 echo "<td nowrap align=right><font size=2>".userdate($log->time, "%a")."</td>";
178 echo "<td nowrap><font size=2>".userdate($log->time, $strftimedatetime)."</td>";
dc0dc7d5 179 echo "<td nowrap><font size=2>";
47f1da80 180 link_to_popup_window("/lib/ipatlas/plot.php?address=$log->ip&user=$log->userid", "ipatlas","$log->ip", 400, 700);
f9d5fd3b 181 echo "</td>";
182 echo "<td nowrap><font size=2><a href=\"../user/view.php?id=$log->userid&course=$log->course\"><b>$log->firstname $log->lastname</b></td>";
183 echo "<td nowrap><font size=2>";
600149be 184 link_to_popup_window( make_log_url($log->module,$log->url), "fromloglive","$log->module $log->action", 400, 600);
f9d5fd3b 185 echo "</td>";
186 echo "<td nowrap><font size=2>$log->info</td>";
187 echo "</tr>";
f9903ed0 188 }
f9d5fd3b 189 echo "</table>";
519d369f 190
191 print_paging_bar($totalcount, $page, $perpage, "$url&perpage=$perpage&");
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
2c8cb30c 467 global $CFG, $USER, $THEME;
52dcc2f9 468 static $isteacher;
469 static $isediting;
2c8cb30c 470 static $ismoving;
471 static $strmovehere;
472 static $strmovefull;
52dcc2f9 473
474 if (!isset($isteacher)) {
475 $isteacher = isteacher($course->id);
476 }
477 if (!isset($isediting)) {
478 $isediting = isediting($course->id);
479 }
2c8cb30c 480 if (!isset($ismoving)) {
481 $ismoving = ismoving($course->id);
482 }
5e367a2d 483
484 $modinfo = unserialize($course->modinfo);
485 $moddata = array();
486 $modicon = array();
487 $editbuttons = "";
488
74666583 489 if (!empty($section->sequence)) {
5e367a2d 490
491 $sectionmods = explode(",", $section->sequence);
492
2c8cb30c 493 if ($ismoving) {
494 $strmovehere = get_string("movehere");
495 $strmovefull = get_string("movefull", "", "'$USER->activitycopyname'");
496 $stractivityclipboard = $USER->activitycopyname;
497 $strcancel= get_string("cancel");
498 if (empty($THEME->custompix)) {
499 $pixpath = "$CFG->wwwroot/pix";
500 } else {
501 $pixpath = "$CFG->wwwroot/theme/$CFG->theme/pix";
502 }
503 $modicon[] = "&nbsp;<img align=bottom src=\"$pixpath/t/move.gif\" height=\"11\" width=\"11\">";
504 $moddata[] = "$USER->activitycopyname&nbsp;(<a href=\"$CFG->wwwroot/course/mod.php?cancelcopy=true\">$strcancel</a>)";
505 }
506
5e367a2d 507 foreach ($sectionmods as $modnumber) {
52dcc2f9 508 if (empty($mods[$modnumber])) {
509 continue;
510 }
5e367a2d 511 $mod = $mods[$modnumber];
2c8cb30c 512 if ($isediting and !$ismoving) {
513 $editbuttons = "<br />".make_editing_buttons($mod->id, $absolute, $mod->visible, true);
514 } else {
515 $editbuttons = "";
1acfbce5 516 }
52dcc2f9 517 if ($mod->visible or $isteacher) {
2c8cb30c 518 if ($ismoving) {
519 if ($mod->id == $USER->activitycopy) {
520 continue;
521 }
522 $modicon[] = "";
523 $moddata[] = "<font size=\"2\"> -> <a title=\"$strmovefull\"".
524 " href=\"$CFG->wwwroot/course/mod.php?moveto=$mod->id\"><b>$strmovehere</b></a></font>";
525 }
1acfbce5 526 $instancename = urldecode($modinfo[$modnumber]->name);
c2cb4545 527 $linkcss = $mod->visible ? "" : " class=\"dimmed\" ";
3ee3e01a 528 if (!empty($modinfo[$modnumber]->extra)) {
529 $extra = urldecode($modinfo[$modnumber]->extra);
530 } else {
531 $extra = "";
532 }
7977cffd 533
52dcc2f9 534 $modicon[] = "<img src=\"$CFG->wwwroot/mod/$mod->modname/icon.gif\"".
535 " height=\"16\" width=\"16\" alt=\"$mod->modfullname\">";
c2cb4545 536 $moddata[] = "<a title=\"$mod->modfullname\" $linkcss $extra".
52dcc2f9 537 "href=\"$CFG->wwwroot/mod/$mod->modname/view.php?id=$mod->id\">$instancename</a>".
2c8cb30c 538 "$editbuttons";
5e367a2d 539 }
5e367a2d 540 }
541 }
2c8cb30c 542 if ($ismoving) {
543 $modicon[] = "";
544 $moddata[] = "<font size=\"2\"> -> <a title=\"$strmovefull\"".
545 " href=\"$CFG->wwwroot/course/mod.php?movetosection=$section->id\"><b>$strmovehere</b></a></font>";
546 }
a44d18e7 547 if ($isediting) {
15ac9065 548 $editmenu = popup_form("$CFG->wwwroot/course/mod.php?id=$course->id&amp;section=$section->section&add=",
5e367a2d 549 $modnames, "section0", "", get_string("add")."...", "mods", get_string("activities"), true);
dfec7b01 550 $editmenu = "<div align=right>$editmenu</div>";
47f1da80 551 } else {
552 $editmenu = "";
5e367a2d 553 }
554
555 print_side_block($heading, "", $moddata, $modicon, $editmenu, $width);
556}
557
558
d897cae4 559function print_section($course, $section, $mods, $modnamesused, $absolute=false, $width="100%") {
52dcc2f9 560/// Prints a section full of activity modules
7977cffd 561 global $CFG, $USER;
562
52dcc2f9 563 static $isteacher;
564 static $isediting;
7977cffd 565 static $ismoving;
566 static $strmovehere;
567 static $strmovefull;
52dcc2f9 568
569 if (!isset($isteacher)) {
570 $isteacher = isteacher($course->id);
571 }
572 if (!isset($isediting)) {
573 $isediting = isediting($course->id);
574 }
7977cffd 575 if (!isset($ismoving)) {
576 $ismoving = ismoving($course->id);
577 }
578 if ($ismoving) {
579 $strmovehere = get_string("movehere");
580 $strmovefull = get_string("movefull", "", "'$USER->activitycopyname'");
581 }
94361e02 582
c408b0c4 583 $modinfo = unserialize($course->modinfo);
94361e02 584
dfec7b01 585 echo "<table width=\"$width\"><tr><td>\n";
74666583 586 if (!empty($section->sequence)) {
94361e02 587
588 $sectionmods = explode(",", $section->sequence);
589
590 foreach ($sectionmods as $modnumber) {
9ae687af 591 if (empty($mods[$modnumber])) {
592 continue;
593 }
94361e02 594 $mod = $mods[$modnumber];
52dcc2f9 595 if ($mod->visible or $isteacher) {
7977cffd 596 if ($ismoving) {
597 if ($mod->id == $USER->activitycopy) {
598 continue;
599 }
600 echo "<font size=\"2\"> -> <a title=\"$strmovefull\"".
601 " href=\"mod.php?moveto=$mod->id\">$strmovehere</a></font><br />\n";
1acfbce5 602 }
7977cffd 603 $instancename = urldecode($modinfo[$modnumber]->name);
86aa7ccf 604 if (!empty($modinfo[$modnumber]->extra)) {
605 $extra = urldecode($modinfo[$modnumber]->extra);
606 } else {
607 $extra = "";
608 }
c2cb4545 609 $linkcss = $mod->visible ? "" : " class=\"dimmed\" ";
52dcc2f9 610 echo "<img src=\"$CFG->wwwroot/mod/$mod->modname/icon.gif\"".
611 " height=16 width=16 alt=\"$mod->modfullname\">".
c2cb4545 612 " <font size=2><a title=\"$mod->modfullname\" $linkcss $extra".
52dcc2f9 613 " href=\"$CFG->wwwroot/mod/$mod->modname/view.php?id=$mod->id\">$instancename</a></font>";
1acfbce5 614 }
d897cae4 615 if (isediting($course->id)) {
5e367a2d 616 echo "&nbsp;&nbsp;";
1acfbce5 617 echo make_editing_buttons($mod->id, $absolute, $mod->visible);
618 }
52dcc2f9 619 if ($mod->visible or $isteacher) {
dfec7b01 620 echo "<br />\n";
94361e02 621 }
94361e02 622 }
623 }
7977cffd 624 if ($ismoving) {
625 echo "<font size=\"2\"> -> <a title=\"$strmovefull\"".
626 " href=\"mod.php?movetosection=$section->id\">$strmovehere</a></font><br />\n";
627 }
dfec7b01 628 echo "</td></tr></table><br />\n\n";
a7ad3ea6 629}
630
5867bfb5 631
632function rebuild_course_cache($courseid=0) {
633// Rebuilds the cached list of course activities stored in the database
634// If a courseid is not specified, then all are rebuilt
635
636 if ($courseid) {
637 $select = "id = '$courseid'";
638 } else {
639 $select = "";
640 }
641
642 if ($courses = get_records_select("course", $select)) {
643 foreach ($courses as $course) {
644 $modinfo = serialize(get_array_of_activities($course->id));
645 if (!set_field("course", "modinfo", $modinfo, "id", $course->id)) {
646 notify("Could not cache module information for course '$course->fullname'!");
647 }
648 }
649 }
650}
651
652
7541bc3e 653function print_heading_block($heading, $width="100%", $class="headingblock") {
5e367a2d 654 global $THEME;
655
7541bc3e 656 echo "<table width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"5\">";
657 echo "<tr><td bgcolor=\"$THEME->cellheading\" class=\"$class\">";
5e367a2d 658 echo stripslashes($heading);
659 echo "</td></tr></table>";
5e367a2d 660}
661
662function print_side_block($heading="", $content="", $list=NULL, $icons=NULL, $footer="", $width=180) {
663// Prints a nice side block with an optional header. The content can either
664// be a block of HTML or a list of text with optional icons.
a7ad3ea6 665
5e367a2d 666 global $THEME;
667
7541bc3e 668 print_side_block_start($heading, $width);
669
5e367a2d 670 if ($content) {
7541bc3e 671 echo "$content";
5e367a2d 672 } else {
5e367a2d 673 echo "<table width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"2\">";
674 foreach ($list as $key => $string) {
7541bc3e 675 echo "<tr bgcolor=\"$THEME->cellcontent2\">";
5e367a2d 676 if ($icons) {
7541bc3e 677 echo "<td class=\"sideblocklinks\" valign=\"top\" width=\"16\">".$icons[$key]."</td>";
a7ad3ea6 678 }
7541bc3e 679 echo "<td class=\"sideblocklinks\" valign=\"top\" width=\"*\"><font size=\"-1\">$string</font></td>";
5e367a2d 680 echo "</tr>";
a7ad3ea6 681 }
5e367a2d 682 if ($footer) {
7541bc3e 683 echo "<tr bgcolor=\"$THEME->cellcontent2\">";
5e367a2d 684 if ($icons) {
7541bc3e 685 echo "<td class=\"sideblocklinks\" valign=\"top\" width=\"16\">&nbsp;</td>";
5e367a2d 686 }
7541bc3e 687 echo "<td class=\"sideblocklinks\"><font size=\"-1\">$footer</td>";
5e367a2d 688 echo "</tr>";
689 }
690 echo "</table>";
a7ad3ea6 691 }
5e367a2d 692
7541bc3e 693 print_side_block_end();
694}
695
696function print_side_block_start($heading="", $width=180, $class="sideblockmain") {
697// Starts a nice side block with an optional header.
698
699 global $THEME;
700
701 echo "<table class=\"sideblock\" width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"5\">";
702 if ($heading) {
703 echo "<tr>";
704 echo "<td class=\"sideblockheading\" bgcolor=\"$THEME->cellheading\">$heading</td>";
705 echo "</tr>";
706 }
707 echo "<tr>";
708 echo "<td class=\"$class\" bgcolor=\"$THEME->cellcontent2\">";
709}
710
711function print_side_block_end() {
712 echo "</td></tr>";
7d99d695 713 echo "</table><br />";
94361e02 714}
715
5e367a2d 716
670fddf1 717function print_admin_links ($siteid, $width=180) {
dc0dc7d5 718 global $CFG, $THEME;
2b25f2a0 719
dc0dc7d5 720 if (empty($THEME->custompix)) {
2f4d324b 721 $pixpath = "$CFG->wwwroot/pix";
dc0dc7d5 722 } else {
2f4d324b 723 $pixpath = "$CFG->wwwroot/theme/$CFG->theme/pix";
dc0dc7d5 724 }
725
440eb0d7 726 if (isadmin()) {
2f4d324b 727 $moddata[]="<a href=\"$CFG->wwwroot/$CFG->admin/configure.php\">".get_string("configuration")."</a>...";
728 $modicon[]="<img src=\"$pixpath/i/admin.gif\" height=16 width=16 alt=\"\" />";
729
730 $moddata[]="<a href=\"$CFG->wwwroot/$CFG->admin/users.php\">".get_string("users")."</a>...";
731 $modicon[]="<img src=\"$pixpath/i/users.gif\" height=16 width=16 alt=\"\" />";
440eb0d7 732 }
2f4d324b 733
55e4b5f9 734 if (iscreator()) {
735 $moddata[]="<a href=\"$CFG->wwwroot/course/index.php?edit=on\">".get_string("courses")."</a>";
2f4d324b 736 $modicon[]="<img src=\"$pixpath/i/course.gif\" height=16 width=16 alt=\"\" />";
55e4b5f9 737 $fulladmin = "";
738 }
2f4d324b 739
1924074c 740 if (isadmin()) {
55e4b5f9 741 $moddata[]="<a href=\"$CFG->wwwroot/course/log.php?id=$siteid\">".get_string("logs")."</a>";
2f4d324b 742 $modicon[]="<img src=\"$pixpath/i/log.gif\" height=16 width=16 alt=\"\" />";
743
440eb0d7 744 $moddata[]="<a href=\"$CFG->wwwroot/files/index.php?id=$siteid\">".get_string("sitefiles")."</a>";
2f4d324b 745 $modicon[]="<img src=\"$pixpath/i/files.gif\" height=16 width=16 alt=\"\" />";
746
be09831a 747 if (file_exists("$CFG->dirroot/$CFG->admin/$CFG->dbtype")) {
2f4d324b 748 $moddata[]="<a href=\"$CFG->wwwroot/$CFG->admin/$CFG->dbtype/frame.php\">".
749 get_string("managedatabase")."</a>";
750 $modicon[]="<img src=\"$pixpath/i/settings.gif\" height=16 width=16 alt=\"\" />";
1924074c 751 }
5867bfb5 752 $fulladmin = "<p><a href=\"$CFG->wwwroot/$CFG->admin/\">".get_string("admin")."</a>...";
1924074c 753 }
5e367a2d 754
755 print_side_block(get_string("administration"), "", $moddata, $modicon, $fulladmin, $width);
756
dc0dc7d5 757 echo "<img src=\"$CFG->wwwroot/pix/spacer.gif\" width=\"$width\" height=1><br>";
2b25f2a0 758}
759
b4d7002e 760function print_course_admin_links($course, $width=180) {
dc0dc7d5 761 global $USER, $CFG, $THEME;
44dad735 762
fce1ce13 763 if (isguest()) {
764 return true;
765 }
dc0dc7d5 766 if (empty($THEME->custompix)) {
767 $pixpath = "$CFG->wwwroot/pix";
768 $modpixpath = "$CFG->wwwroot/mod";
769 } else {
770 $pixpath = "$CFG->wwwroot/theme/$CFG->theme/pix";
771 $modpixpath = "$CFG->wwwroot/theme/$CFG->theme/pix/mod";
772 }
13469b82 773 if (isteacher($course->id)) {
f3c6d219 774 if (isteacheredit($course->id)) {
02ebf404 775 $adminicon[]="<img src=\"$pixpath/i/edit.gif\" height=16 width=16 alt=\"\">";
776 if (isediting($course->id)) {
777 $admindata[]="<a href=\"view.php?id=$course->id&edit=off\">".get_string("turneditingoff")."</a>";
778 } else {
779 $admindata[]="<a href=\"view.php?id=$course->id&edit=on\">".get_string("turneditingon")."</a>";
780 }
781 $admindata[]="<a href=\"edit.php?id=$course->id\">".get_string("settings")."...</a>";
782 $adminicon[]="<img src=\"$pixpath/i/settings.gif\" height=16 width=16 alt=\"\">";
3b1bb969 783
784 if (iscreator() or !empty($CFG->teacherassignteachers)) {
785 if (!$course->teachers) {
786 $course->teachers = get_string("defaultcourseteachers");
787 }
788 $admindata[]="<a href=\"teacher.php?id=$course->id\">$course->teachers...</a>";
789 $adminicon[]="<img src=\"$pixpath/i/users.gif\" height=16 width=16 alt=\"\">";
02ebf404 790 }
4d9d90c0 791 }
02ebf404 792
a33c4ae8 793 if (!$course->students) {
794 $course->students = get_string("defaultcoursestudents");
3b1bb969 795 }
4d9d90c0 796 $admindata[]="<a href=\"student.php?id=$course->id\">$course->students...</a>";
797 $adminicon[]="<img src=\"$pixpath/i/users.gif\" height=16 width=16 alt=\"\">";
7d569d28 798
4d9d90c0 799 if (isteacheredit($course->id)) {
02ebf404 800 $admindata[]="<a href=\"$CFG->wwwroot/backup/backup.php?id=$course->id\">".get_string("backup")."...</a>";
801 $adminicon[]="<img src=\"$pixpath/i/backup.gif\" height=16 width=16 alt=\"\">";
802
803 //Only showed if "backupdata" dir exists
804 if (is_dir("$CFG->dataroot/$course->id/backupdata")) {
805 $admindata[]="<a href=\"$CFG->wwwroot/files/index.php?id=$course->id&wdir=/backupdata\">".get_string("restore")."...</a>";
806 $adminicon[]="<img src=\"$pixpath/i/restore.gif\" height=16 width=16 alt=\"\">";
807 }
808 $admindata[]="<a href=\"scales.php?id=$course->id\">".get_string("scales")."...</a>";
809 $adminicon[]="<img src=\"$pixpath/i/scales.gif\" height=16 width=16 alt=\"\">";
13469b82 810 }
13469b82 811
dc0dc7d5 812 $admindata[]="<a href=\"grades.php?id=$course->id\">".get_string("grades")."...</a>";
813 $adminicon[]="<img src=\"$pixpath/i/grades.gif\" height=16 width=16 alt=\"\">";
13469b82 814
dc0dc7d5 815 $admindata[]="<a href=\"log.php?id=$course->id\">".get_string("logs")."...</a>";
816 $adminicon[]="<img src=\"$pixpath/i/log.gif\" height=16 width=16 alt=\"\">";
817
818 $admindata[]="<a href=\"$CFG->wwwroot/files/index.php?id=$course->id\">".get_string("files")."...</a>";
819 $adminicon[]="<img src=\"$pixpath/i/files.gif\" height=16 width=16 alt=\"\">";
f354106d 820
dc0dc7d5 821 $admindata[]="<a href=\"$CFG->wwwroot/doc/view.php?id=$course->id&file=teacher.html\">".get_string("help")."...</a>";
822 $adminicon[]="<img src=\"$modpixpath/resource/icon.gif\" height=16 width=16 alt=\"\">";
b4d7002e 823
13469b82 824 if ($teacherforum = forum_get_course_forum($course->id, "teacher")) {
eafc0b44 825 $admindata[]="<a href=\"$CFG->wwwroot/mod/forum/view.php?f=$teacherforum->id\">$teacherforum->name</a>";
dc0dc7d5 826 $adminicon[]="<img src=\"$modpixpath/forum/icon.gif\" height=16 width=16 alt=\"\">";
13469b82 827 }
f354106d 828
fff79722 829 } else { // Students menu
5b8540e0 830 if ($course->showgrades) {
fff79722 831 $admindata[]="<a href=\"grade.php?id=$course->id\">".get_string("grades")."...</a>";
832 $adminicon[]="<img src=\"$pixpath/i/grades.gif\" height=16 width=16 alt=\"\">";
833 }
13469b82 834 }
44dad735 835
f354106d 836 if (!empty($admindata)) {
837 print_side_block(get_string("administration"), "", $admindata, $adminicon, "", $width);
838 }
44dad735 839}
2b25f2a0 840
c2cb4545 841
842function make_categories_list(&$list, &$parents, $category=NULL, $path="") {
843/// Given an empty array, this function recursively travels the
844/// categories, building up a nice list for display. It also makes
845/// an array that list all the parents for each category.
846
847 if ($category) {
848 if ($path) {
849 $path = "$path / $category->name";
850 } else {
851 $path = "$category->name";
852 }
853 $list[$category->id] = $path;
854 } else {
855 $category->id = 0;
856 }
857
858 if ($categories = get_categories("$category->id")) { // Print all the children recursively
859 foreach ($categories as $cat) {
860 if (!empty($category->id)) {
861 $parents[$cat->id] = $parents[$category->id];
862 $parents[$cat->id][] = $category->id;
863 }
e92fe848 864 make_categories_list($list, $parents, $cat, $path);
c2cb4545 865 }
866 }
867}
868
869
c2cb4545 870function print_whole_category_list($category=NULL, $displaylist=NULL, $parentslist=NULL, $depth=-1) {
871/// Recursive function to print out all the categories in a nice format
872/// with or without courses included
873
874 if (!$displaylist) {
e92fe848 875 make_categories_list($displaylist, $parentslist);
c2cb4545 876 }
877
878 if ($category) {
879 if ($category->visible or isadmin()) {
d2b6ba70 880 print_category_info($category, $depth);
c2cb4545 881 } else {
882 return; // Don't bother printing children of invisible categories
883 }
884
885 } else {
c2cb4545 886 $category->id = "0";
d2b6ba70 887 echo "<table width=\"100%\" class=\"categorybox\">";
888 $toplevel = true;
c2cb4545 889 }
890
891 if ($categories = get_categories($category->id)) { // Print all the children recursively
892 $countcats = count($categories);
893 $count = 0;
894 $first = true;
895 $last = false;
896 foreach ($categories as $cat) {
897 $count++;
898 if ($count == $countcats) {
899 $last = true;
900 }
901 $up = $first ? false : true;
902 $down = $last ? false : true;
903 $first = false;
904
905 print_whole_category_list($cat, $displaylist, $parentslist, $depth + 1);
906 }
907 }
908
d2b6ba70 909 if (isset($toplevel)) {
910 echo "</table>";
c2cb4545 911 }
912}
913
914
d2b6ba70 915function print_category_info($category, $depth) {
916/// Prints the category info in indented fashion
917/// This function is only used by print_whole_category_list() above
c2cb4545 918
919 global $CFG;
920
85098089 921 $strallowguests = get_string("allowguests");
922 $strrequireskey = get_string("requireskey");
ba2e5d73 923
dc0dc7d5 924 if (empty($THEME->custompix)) {
925 $pixpath = "$CFG->wwwroot/pix";
926 } else {
927 $pixpath = "$CFG->wwwroot/theme/$CFG->theme/pix";
928 }
929
c2cb4545 930 $catlinkcss = $category->visible ? "" : " class=\"dimmed\" ";
931
c2cb4545 932 echo "<tr>";
d2b6ba70 933 echo "<td valign=\"top\">";
934 for ($i=0; $i<$depth;$i++) {
6f065ce3 935 echo "<ul style=\"margin-bottom:0;margin-top:0\">";
d2b6ba70 936 }
8ef9cb56 937
938 $catimage = "";
939 if ($CFG->frontpage == FRONTPAGECOURSELIST) {
ee44a11f 940 $catimage = "<img src=\"$pixpath/i/course.gif\" width=16 height=16 border=0>";
8ef9cb56 941 }
942 echo "<font size=+1>$catimage <a $catlinkcss ".
d2b6ba70 943 "href=\"$CFG->wwwroot/course/category.php?id=$category->id\">$category->name</a></font>";
944
c2cb4545 945 if ($CFG->frontpage == FRONTPAGECOURSELIST) {
d2b6ba70 946 if ($courses = get_courses($category->id)) {
c2cb4545 947 foreach ($courses as $course) {
6f065ce3 948 echo "<ul style=\"margin-bottom:0;margin-top:0\">";
c2cb4545 949 $linkcss = $course->visible ? "" : " class=\"dimmed\" ";
a6870f0c 950 echo "<a $linkcss href=\"$CFG->wwwroot/course/view.php?id=$course->id\">$course->fullname</a>";
c2cb4545 951 echo "&nbsp;&nbsp;";
952 unset($courses[$key]);
953 if ($course->guest ) {
954 echo "<a title=\"$strallowguests\" href=\"$CFG->wwwroot/course/view.php?id=$course->id\">";
955 echo "<img alt=\"\" height=16 width=16 border=0 src=\"$pixpath/i/user.gif\"></a>";
0c656181 956 }
c2cb4545 957 if ($course->password) {
958 echo "<a title=\"$strrequireskey\" href=\"$CFG->wwwroot/course/view.php?id=$course->id\">";
959 echo "<img alt=\"\" height=16 width=16 border=0 src=\"$pixpath/i/key.gif\"></a>";
0c656181 960 }
d2b6ba70 961 echo "</ul>";
0c656181 962 }
ba2e5d73 963 }
d2b6ba70 964 for ($i=0; $i<$depth;$i++) {
965 echo "</ul>";
966 }
967 echo "</td>";
968 echo "</tr>";
969 } else {
970 for ($i=0; $i<$depth;$i++) {
971 echo "</ul>";
972 }
973 echo "</td>";
974 echo "<td valign=\"top\">";
9936fe81 975 if ($category->coursecount) {
976 echo $category->coursecount;
d2b6ba70 977 }
978 echo "</td>";
979 echo "</tr>";
c2cb4545 980 }
c2cb4545 981}
982
983function print_courses_sideblock($category=0, $width="100%") {
f3120024 984 global $CFG, $THEME, $USER;
c2cb4545 985
986 if (empty($THEME->custompix)) {
987 $icon = "<img src=\"$CFG->wwwroot/pix/i/course.gif\"".
988 " height=\"16\" width=\"16\" alt=\"".get_string("course")."\">";
989 } else {
990 $icon = "<img src=\"$CFG->wwwroot/theme/$CFG->theme/pix/i/course.gif\"".
991 " height=\"16\" width=\"16\" alt=\"".get_string("course")."\">";
992 }
993
f3120024 994 if (isset($USER->id) and !isadmin()) { // Just print My Courses
995 if ($courses = get_my_courses($USER->id)) {
996 foreach ($courses as $course) {
d1d74247 997 $linkcss = $course->visible ? "" : " class=\"dimmed\" ";
f3120024 998 $moddata[]="<a $linkcss title=\"$course->shortname\" ".
999 "href=\"$CFG->wwwroot/course/view.php?id=$course->id\">$course->fullname</a>";
1000 $modicon[]=$icon;
1001 }
5dc3d80e 1002 $fulllist = "<p><a href=\"$CFG->wwwroot/course/index.php\">".get_string("fulllistofcourses")."</a>...";
f3120024 1003 print_side_block( get_string("mycourses"), "", $moddata, $modicon, $fulllist, $width);
1004 return;
1005 }
1006 }
1007
c571f3fc 1008 $categories = get_categories("0"); // Parent = 0 ie top-level categories only
c2cb4545 1009 if (count($categories) > 1) { // Just print top level category links
1010 foreach ($categories as $category) {
1011 $linkcss = $category->visible ? "" : " class=\"dimmed\" ";
d2b6ba70 1012 $moddata[]="<a $linkcss href=\"$CFG->wwwroot/course/category.php?id=$category->id\">$category->name</a>";
c2cb4545 1013 $modicon[]=$icon;
0c656181 1014 }
c571f3fc 1015 $fulllist = "<p><a href=\"$CFG->wwwroot/course/search.php\">".get_string("searchcourses")."</a>...";
c2cb4545 1016 } else { // Just print course names of single category
90c2ca2e 1017 $category = array_shift($categories);
d2b6ba70 1018 $courses = get_courses($category->id);
1019
c2cb4545 1020 if ($courses) {
1021 foreach ($courses as $course) {
1022 $linkcss = $course->visible ? "" : " class=\"dimmed\" ";
1023 $moddata[]="<a $linkcss title=\"$course->shortname\" ".
1024 "href=\"$CFG->wwwroot/course/view.php?id=$course->id\">$course->fullname</a>";
1025 $modicon[]=$icon;
1026 }
5dc3d80e 1027 $fulllist = "<p><a href=\"$CFG->wwwroot/course/index.php\">".get_string("fulllistofcourses")."</a>...";
c2cb4545 1028 } else {
1029 $moddata = array();
1030 $modicon = array();
1031 $fulllist = get_string("nocoursesyet");
0c656181 1032 }
ba2e5d73 1033 }
c2cb4545 1034
f3120024 1035 print_side_block( get_string("courses"), "", $moddata, $modicon, $fulllist, $width);
ba2e5d73 1036}
94361e02 1037
c2cb4545 1038
1039function print_courses($category, $width="100%") {
1040/// Category is 0 (for all courses) or an object
1041
1042 global $CFG, $THEME;
1043
1044 if (empty($category)) {
90c2ca2e 1045 $categories = get_categories(0); // Parent = 0 ie top-level categories only
1046 if (count($categories) == 1) {
1047 $category = array_shift($categories);
d2b6ba70 1048 $courses = get_courses($category->id);
90c2ca2e 1049 } else {
d2b6ba70 1050 $courses = get_courses("all");
90c2ca2e 1051 }
1052 unset($categories);
607809b3 1053 } else {
c2cb4545 1054 $categories = get_categories($category->id); // sub categories
d2b6ba70 1055 $courses = get_courses($category->id);
c2cb4545 1056 }
1057
1058 if ($categories) {
1059 print_simple_box_start("center");
1060 print_heading(get_string("subcategories"));
1061 foreach ($categories as $category) {
1062 $linkcss = $category->visible ? "" : " class=\"dimmed\" ";
1063 echo "<p align=\"center\"><a $linkcss".
1064 " href=\"$CFG->wwwroot/course/index.php?category=$category->id\">$category->name</a></p>";
1065 }
1066 print_simple_box_end();
1067 }
1068
1069 if ($courses) {
1070 foreach ($courses as $course) {
1071 print_course($course, $width);
1072 echo "<br />\n";
1073 }
1074 } else {
1075 print_heading(get_string("nocoursesyet"));
1076 }
1077
1078}
1079
1080
1081function print_course($course, $width="100%") {
1082
1083 global $CFG, $THEME;
1084
1085 if (! $site = get_site()) {
1086 error("Could not find a site!");
1087 }
1088
1089 if (empty($THEME->custompix)) {
1090 $pixpath = "$CFG->wwwroot/pix";
1091 } else {
1092 $pixpath = "$CFG->wwwroot/theme/$CFG->theme/pix";
1093 }
1094
1095 print_simple_box_start("center", "$width");
1096
22288704 1097 $linkcss = $course->visible ? "" : " class=\"dimmed\" ";
1098
c2cb4545 1099 echo "<table width=\"100%\">";
1100 echo "<tr valign=top>";
1101 echo "<td valign=top width=50%>";
1102 echo "<p><font size=3><b><a title=\"".get_string("entercourse")."\"
22288704 1103 $linkcss href=\"$CFG->wwwroot/course/view.php?id=$course->id\">$course->fullname</a></b></font></p>";
c2cb4545 1104 if ($teachers = get_course_teachers($course->id)) {
1105 echo "<p><font size=\"1\">\n";
1106 foreach ($teachers as $teacher) {
1107 if ($teacher->authority > 0) {
1108 if (!$teacher->role) {
1109 $teacher->role = $course->teacher;
1110 }
1111 echo "$teacher->role: <a href=\"$CFG->wwwroot/user/view.php?id=$teacher->id&course=$site->id\">$teacher->firstname $teacher->lastname</a><br />";
1112 }
1113 }
1114 echo "</font></p>";
1115 }
1116 if ($course->guest) {
1117 $strallowguests = get_string("allowguests");
1118 echo "<a title=\"$strallowguests\" href=\"$CFG->wwwroot/course/view.php?id=$course->id\">";
1119 echo "<img vspace=4 alt=\"$strallowguests\" height=16 width=16 border=0 src=\"$pixpath/i/user.gif\"></a>&nbsp;&nbsp;";
1120 }
1121 if ($course->password) {
1122 $strrequireskey = get_string("requireskey");
1123 echo "<a title=\"$strrequireskey\" href=\"$CFG->wwwroot/course/view.php?id=$course->id\">";
1124 echo "<img vspace=4 alt=\"$strrequireskey\" height=16 width=16 border=0 src=\"$pixpath/i/key.gif\"></a>";
1125 }
1126
1127
1128 echo "</td><td valign=top width=50%>";
1129 echo "<p><font size=2>".text_to_html($course->summary)."</font></p>";
1130 echo "</td></tr>";
1131 echo "</table>";
1132
1133 print_simple_box_end();
1134}
1135
1136
1137function print_my_moodle() {
1138/// Prints custom user information on the home page.
1139/// Over time this can include all sorts of information
1140
1141 global $USER, $CFG;
1142
1143 if (!isset($USER->id)) {
1144 error("It shouldn't be possible to see My Moodle without being logged in.");
1145 }
1146
1147 if ($courses = get_my_courses($USER->id)) {
1148 foreach ($courses as $course) {
1149 print_course($course, "100%");
1150 echo "<br />\n";
1151 }
38a10939 1152
1153 echo "<table width=\"100%\"><tr><td align=\"center\">";
1154 print_course_search();
1155 echo "</td><td align=\"center\">";
5dc3d80e 1156 print_single_button("$CFG->wwwroot/course/index.php", NULL, get_string("fulllistofcourses"), "get");
38a10939 1157 echo "</td></tr></table>\n";
26330001 1158 } else {
1159 if (count_records("course_categories") > 1) {
1160 print_simple_box_start("center", "100%");
1161 print_whole_category_list();
1162 print_simple_box_end();
1163 } else {
1164 print_courses(0, "100%");
1165 }
607809b3 1166 }
2b8cef80 1167}
1168
11b0c469 1169
a8b56716 1170function print_course_search($value="", $return=false, $format="plain") {
38a10939 1171
1172 global $CFG;
1173
1174 $strsearchcourses= get_string("searchcourses");
1175
a8b56716 1176 if ($format == "plain") {
1177 $output = "<center><p align=\"center\" class=\"coursesearchbox\">";
1178 $output .= "<form name=\"coursesearch\" action=\"$CFG->wwwroot/course/search.php\" method=\"get\">";
1179 $output .= "<input type=\"text\" size=30 name=\"search\" value=\"$value\">";
1180 $output .= "<input type=\"submit\" value=\"$strsearchcourses\">";
1181 $output .= "</form></p></center>";
1182 } else if ($format == "navbar") {
1183 $output = "<table border=0 cellpadding=0 cellspacing=0><tr><td nowrap>";
1184 $output .= "<form name=\"coursesearch\" action=\"$CFG->wwwroot/course/search.php\" method=\"get\">";
1185 $output .= "<input type=\"text\" size=20 name=\"search\" value=\"$value\">";
1186 $output .= "<input type=\"submit\" value=\"$strsearchcourses\">";
a8b56716 1187 $output .= "</form>";
1188 $output .= "</td></tr></table>";
1189 }
1190
1191 if ($return) {
1192 return $output;
1193 }
1194 echo $output;
38a10939 1195}
11b0c469 1196
1197/// MODULE FUNCTIONS /////////////////////////////////////////////////////////////////
1198
1199function add_course_module($mod) {
11b0c469 1200
e5dfd0f3 1201 $mod->added = time();
11b0c469 1202
e5dfd0f3 1203 return insert_record("course_modules", $mod);
11b0c469 1204}
1205
7977cffd 1206function add_mod_to_section($mod, $beforemod=NULL) {
1207/// Given a full mod object with section and course already defined
1208/// If $before is specified, then this is an existing ID which we
1209/// will insert the new module before
1210///
1211/// Returns the course_sections ID where the mod is inserted
11b0c469 1212
9fa49e22 1213 if ($section = get_record("course_sections", "course", "$mod->course", "section", "$mod->section")) {
7977cffd 1214
1215 $section->sequence = trim($section->sequence);
1216
1217 if (empty($section->sequence)) {
11b0c469 1218 $newsequence = "$mod->coursemodule";
7977cffd 1219
1220 } else if ($beforemod) {
1221 $modarray = explode(",", $section->sequence);
1222
1223 if ($key = array_keys ($modarray, $beforemod->id)) {
1224 $insertarray = array($mod->id, $beforemod->id);
1225 array_splice($modarray, $key[0], 1, $insertarray);
1226 $newsequence = implode(",", $modarray);
1227
1228 } else { // Just tack it on the end anyway
1229 $newsequence = "$section->sequence,$mod->coursemodule";
1230 }
1231
1232 } else {
1233 $newsequence = "$section->sequence,$mod->coursemodule";
11b0c469 1234 }
7977cffd 1235
e5dfd0f3 1236 if (set_field("course_sections", "sequence", $newsequence, "id", $section->id)) {
1237 return $section->id; // Return course_sections ID that was used.
11b0c469 1238 } else {
e5dfd0f3 1239 return 0;
11b0c469 1240 }
1241
1242 } else { // Insert a new record
e5dfd0f3 1243 $section->course = $mod->course;
1244 $section->section = $mod->section;
1245 $section->summary = "";
1246 $section->sequence = $mod->coursemodule;
1247 return insert_record("course_sections", $section);
11b0c469 1248 }
1249}
1250
1acfbce5 1251function hide_course_module($mod) {
1252 return set_field("course_modules", "visible", 0, "id", $mod);
1253}
1254
1255function show_course_module($mod) {
1256 return set_field("course_modules", "visible", 1, "id", $mod);
1257}
1258
11b0c469 1259function delete_course_module($mod) {
1260 return set_field("course_modules", "deleted", 1, "id", $mod);
1261}
1262
1263function delete_mod_from_section($mod, $section) {
11b0c469 1264
e5dfd0f3 1265 if ($section = get_record("course_sections", "id", "$section") ) {
11b0c469 1266
e5dfd0f3 1267 $modarray = explode(",", $section->sequence);
11b0c469 1268
1269 if ($key = array_keys ($modarray, $mod)) {
1270 array_splice($modarray, $key[0], 1);
1271 $newsequence = implode(",", $modarray);
e5dfd0f3 1272 return set_field("course_sections", "sequence", $newsequence, "id", $section->id);
11b0c469 1273 } else {
1274 return false;
1275 }
1276
11b0c469 1277 }
7977cffd 1278 return false;
11b0c469 1279}
1280
12905134 1281function move_section($course, $section, $move) {
1282/// Moves a whole course section up and down within the course
1283
1284 if (!$move) {
1285 return true;
1286 }
1287
1288 $sectiondest = $section + $move;
1289
1290 if ($sectiondest > $course->numsections or $sectiondest < 1) {
1291 return false;
1292 }
1293
1294 if (!$sectionrecord = get_record("course_sections", "course", $course->id, "section", $section)) {
1295 return false;
1296 }
1297
1298 if (!$sectiondestrecord = get_record("course_sections", "course", $course->id, "section", $sectiondest)) {
1299 return false;
1300 }
1301
56e34ee4 1302 if (!set_field("course_sections", "section", $sectiondest, "id", $sectionrecord->id)) {
12905134 1303 return false;
1304 }
56e34ee4 1305 if (!set_field("course_sections", "section", $section, "id", $sectiondestrecord->id)) {
12905134 1306 return false;
1307 }
1308 return true;
1309}
1310
1311
7977cffd 1312function moveto_module($mod, $section, $beforemod=NULL) {
1313/// All parameters are objects
1314/// Move the module object $mod to the specified $section
1315/// If $beforemod exists then that is the module
1316/// before which $modid should be inserted
1317
1318/// Remove original module from original section
1319
1320 if (! delete_mod_from_section($mod->id, $mod->section)) {
1321 notify("Could not delete module from existing section");
1322 }
1323
1324/// Update module itself if necessary
1325
1326 if ($mod->section != $section->id) {
1327 $mod->section = $section->id;
1328
1329 if (!update_record("course_modules", $mod)) {
1330 return false;
1331 }
1332 }
1333
1334/// Add the module into the new section
1335
1336 $mod->course = $section->course;
1337 $mod->section = $section->section; // need relative reference
1338 $mod->coursemodule = $mod->id;
1339
1340 if (! add_mod_to_section($mod, $beforemod)) {
1341 return false;
1342 }
1343
1344 return true;
1345
1346}
1347
1348
11b0c469 1349
7c0f2984 1350function move_module($cm, $move) {
12905134 1351/// Moves an activity module up and down within the course
11b0c469 1352
1353 if (!$move) {
1354 return true;
1355 }
1356
11b0c469 1357 if (! $thissection = get_record("course_sections", "id", $cm->section)) {
1358 error("This course section doesn't exist");
1359 }
1360
1361 $mods = explode(",", $thissection->sequence);
1362
1363 $len = count($mods);
1364 $pos = array_keys($mods, $cm->id);
1365 $thepos = $pos[0];
1366
1367 if ($len == 0 || count($pos) == 0 ) {
1368 error("Very strange. Could not find the required module in this section.");
1369 }
1370
1371 if ($len == 1) {
1372 $first = true;
1373 $last = true;
1374 } else {
1375 $first = ($thepos == 0);
1376 $last = ($thepos == $len - 1);
1377 }
1378
1379 if ($move < 0) { // Moving the module up
1380
1381 if ($first) {
ad41694c 1382 if ($thissection->section == 0) { // First section, do nothing
11b0c469 1383 return true;
ad41694c 1384
11b0c469 1385 } else { // Push onto end of previous section
1386 $prevsectionnumber = $thissection->section - 1;
9fa49e22 1387 if (! $prevsection = get_record("course_sections", "course", "$thissection->course",
1388 "section", "$prevsectionnumber")) {
11b0c469 1389 error("Previous section ($prevsection->id) doesn't exist");
1390 }
1391
74666583 1392 if (!empty($prevsection->sequence)) {
11b0c469 1393 $newsequence = "$prevsection->sequence,$cm->id";
1394 } else {
1395 $newsequence = "$cm->id";
1396 }
1397
1398 if (! set_field("course_sections", "sequence", $newsequence, "id", $prevsection->id)) {
1399 error("Previous section could not be updated");
1400 }
1401
1402 if (! set_field("course_modules", "section", $prevsection->id, "id", $cm->id)) {
1403 error("Module could not be updated");
1404 }
1405
1406 array_splice($mods, 0, 1);
1407 $newsequence = implode(",", $mods);
1408 if (! set_field("course_sections", "sequence", $newsequence, "id", $thissection->id)) {
1409 error("Module could not be updated");
1410 }
1411
1412 return true;
1413
1414 }
1415 } else { // move up within this section
1416 $swap = $mods[$thepos-1];
1417 $mods[$thepos-1] = $mods[$thepos];
1418 $mods[$thepos] = $swap;
1419
1420 $newsequence = implode(",", $mods);
1421 if (! set_field("course_sections", "sequence", $newsequence, "id", $thissection->id)) {
1422 error("This section could not be updated");
1423 }
1424 return true;
1425 }
1426
1427 } else { // Moving the module down
1428
1429 if ($last) {
1430 $nextsectionnumber = $thissection->section + 1;
9fa49e22 1431 if ($nextsection = get_record("course_sections", "course", "$thissection->course",
1432 "section", "$nextsectionnumber")) {
11b0c469 1433
74666583 1434 if (!empty($nextsection->sequence)) {
11b0c469 1435 $newsequence = "$cm->id,$nextsection->sequence";
1436 } else {
1437 $newsequence = "$cm->id";
1438 }
1439
1440 if (! set_field("course_sections", "sequence", $newsequence, "id", $nextsection->id)) {
1441 error("Next section could not be updated");
1442 }
1443
1444 if (! set_field("course_modules", "section", $nextsection->id, "id", $cm->id)) {
1445 error("Module could not be updated");
1446 }
1447
1448 array_splice($mods, $thepos, 1);
1449 $newsequence = implode(",", $mods);
1450 if (! set_field("course_sections", "sequence", $newsequence, "id", $thissection->id)) {
1451 error("This section could not be updated");
1452 }
1453 return true;
1454
1455 } else { // There is no next section, so just return
1456 return true;
1457
1458 }
1459 } else { // move down within this section
1460 $swap = $mods[$thepos+1];
1461 $mods[$thepos+1] = $mods[$thepos];
1462 $mods[$thepos] = $swap;
1463
1464 $newsequence = implode(",", $mods);
1465 if (! set_field("course_sections", "sequence", $newsequence, "id", $thissection->id)) {
1466 error("This section could not be updated");
1467 }
1468 return true;
1469 }
1470 }
1471}
1472
7977cffd 1473function make_editing_buttons($moduleid, $absolute=false, $visible=true, $moveselect=true) {
dc0dc7d5 1474 global $CFG, $THEME;
94361e02 1475
4de4fdfe 1476 static $str = '';
1acfbce5 1477 if (empty($str)) {
1478 $str->delete = get_string("delete");
493486c4 1479 $str->move = get_string("move");
1acfbce5 1480 $str->moveup = get_string("moveup");
1481 $str->movedown = get_string("movedown");
1482 $str->update = get_string("update");
1483 $str->hide = get_string("hide");
1484 $str->show = get_string("show");
1485 }
94361e02 1486
1487 if ($absolute) {
dc0dc7d5 1488 $path = "$CFG->wwwroot/course";
1489 } else {
1490 $path = ".";
1491 }
1492
1493 if (empty($THEME->custompix)) {
1494 $pixpath = "$path/../pix";
94361e02 1495 } else {
dc0dc7d5 1496 $pixpath = "$path/../theme/$CFG->theme/pix";
94361e02 1497 }
1acfbce5 1498
1499 if ($visible) {
7977cffd 1500 $hideshow = "<a title=\"$str->hide\" href=\"$path/mod.php?hide=$moduleid\"><img".
1501 " src=\"$pixpath/t/hide.gif\" hspace=2 height=11 width=11 border=0></a> ";
1acfbce5 1502 } else {
7977cffd 1503 $hideshow = "<a title=\"$str->show\" href=\"$path/mod.php?show=$moduleid\"><img".
1504 " src=\"$pixpath/t/show.gif\" hspace=2 height=11 width=11 border=0></a> ";
1505 }
1506
1507 if ($moveselect) {
1508 $move = "<a title=\"$str->move\" href=\"$path/mod.php?copy=$moduleid\"><img".
1509 " src=\"$pixpath/t/move.gif\" height=\"11\" width=\"11\" border=\"0\"></a> ";
493486c4 1510 } else {
1511 $move = "<a title=\"$str->moveup\" href=\"$path/mod.php?id=$moduleid&move=-1\"><img".
1512 " src=\"$pixpath/t/up.gif\" height=11 width=11 border=0></a> ".
1513 "<a title=\"$str->movedown\" href=\"$path/mod.php?id=$moduleid&move=1\"><img".
1514 " src=\"$pixpath/t/down.gif\" height=11 width=11 border=0></a> ";
7977cffd 1515 }
1516
1517 return "<a title=\"$str->delete\" href=\"$path/mod.php?delete=$moduleid\"><img".
493486c4 1518 " src=\"$pixpath/t/delete.gif\" height=11 width=11 border=0></a> $move".
7977cffd 1519 "<a title=\"$str->update\" href=\"$path/mod.php?update=$moduleid\"><img".
1520 " src=\"$pixpath/t/edit.gif\" height=11 width=11 border=0></a> $hideshow";
90845098 1521}
1522
f9903ed0 1523?>