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