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