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