Polished.
[moodle.git] / course / lib.php
blobef78bb43ab8a889bf675cf4d4af2cf224b07aef8
1 <? // $Id$
2 // Library of useful functions
5 if (defined('COURSE_MAX_LOG_DISPLAY')) { // Being included again - should never happen!!
6 return;
9 define('COURSE_MAX_LOG_DISPLAY', 150); // days
11 define('COURSE_MAX_LOGS_PER_PAGE', 1000); // records
13 define('COURSE_LIVELOG_REFRESH', 60); // Seconds
15 define('COURSE_MAX_RECENT_PERIOD', 604800); // A week, in seconds
19 function print_log_selector_form($course, $selecteduser=0, $selecteddate="today") {
21 global $USER, $CFG;
23 // Get all the possible users
24 $users = array();
26 if ($course->category) {
27 if ($courseusers = get_course_users($course->id, "u.lastaccess DESC")) {
28 foreach ($courseusers as $courseuser) {
29 $users[$courseuser->id] = "$courseuser->firstname $courseuser->lastname";
32 if ($guest = get_guest()) {
33 $users[$guest->id] = "$guest->firstname $guest->lastname";
37 if (isadmin()) {
38 if ($ccc = get_records("course", "", "", "fullname")) {
39 foreach ($ccc as $cc) {
40 if ($cc->category) {
41 $courses["$cc->id"] = "$cc->fullname";
42 } else {
43 $courses["$cc->id"] = " $cc->fullname (Site)";
47 asort($courses);
51 $strftimedate = get_string("strftimedate");
52 $strftimedaydate = get_string("strftimedaydate");
54 asort($users);
56 // Get all the possible dates
57 // Note that we are keeping track of real (GMT) time and user time
58 // User time is only used in displays - all calcs and passing is GMT
60 $timenow = time(); // GMT
62 // What day is it now for the user, and when is midnight that day (in GMT).
63 $timemidnight = $today = usergetmidnight($timenow);
65 // Put today up the top of the list
66 $dates = array("$timemidnight" => get_string("today").", ".userdate($timenow, $strftimedate) );
68 if (! $course->startdate) {
69 $course->startdate = $course->timecreated;
72 $numdates = 1;
73 while ($timemidnight > $course->startdate and $numdates < 365) {
74 $timemidnight = $timemidnight - 86400;
75 $timenow = $timenow - 86400;
76 $dates["$timemidnight"] = userdate($timenow, $strftimedaydate);
77 $numdates++;
80 if ($selecteddate == "today") {
81 $selecteddate = $today;
84 echo "<CENTER>";
85 echo "<FORM ACTION=log.php METHOD=get>";
86 if (isadmin()) {
87 choose_from_menu ($courses, "id", $course->id, "");
88 } else {
89 echo "<INPUT TYPE=hidden NAME=id VALUE=\"$course->id\">";
91 if ($course->category) {
92 choose_from_menu ($users, "user", $selecteduser, get_string("allparticipants") );
94 choose_from_menu ($dates, "date", $selecteddate, get_string("alldays"));
95 echo "<INPUT TYPE=submit VALUE=\"".get_string("showtheselogs")."\">";
96 echo "</FORM>";
97 echo "</CENTER>";
100 function make_log_url($module, $url) {
101 switch ($module) {
102 case "course":
103 case "user":
104 case "file":
105 case "login":
106 case "lib":
107 case "admin":
108 return "/$module/$url";
109 break;
110 default:
111 return "/mod/$module/$url";
112 break;
117 function print_log($course, $user=0, $date=0, $order="ORDER BY l.time ASC") {
118 // It is assumed that $date is the GMT time of midnight for that day,
119 // and so the next 86400 seconds worth of logs are printed.
121 global $CFG;
123 if ($course->category) {
124 $selector = "WHERE l.course='$course->id' AND l.userid = u.id";
126 } else {
127 $selector = "WHERE l.userid = u.id"; // Show all courses
128 if ($ccc = get_courses(-1)) {
129 foreach ($ccc as $cc) {
130 $courses[$cc->id] = "$cc->shortname";
135 if ($user) {
136 $selector .= " AND l.userid = '$user'";
139 if ($date) {
140 $enddate = $date + 86400;
141 $selector .= " AND l.time > '$date' AND l.time < '$enddate'";
144 $order = $order." LIMIT ".COURSE_MAX_LOGS_PER_PAGE; // To keep it manageable
146 if (!$logs = get_logs($selector, $order)) {
147 notify("No logs found!");
148 print_footer($course);
149 exit;
152 $count=0;
153 $tt = getdate(time());
154 $today = mktime (0, 0, 0, $tt["mon"], $tt["mday"], $tt["year"]);
155 if (($totalcountlogs = count($logs)) == COURSE_MAX_LOGS_PER_PAGE) {
156 $totalcountlogs = "$totalcountlogs (+)";
159 $strftimedatetime = get_string("strftimedatetime");
161 echo "<p align=center>";
162 print_string("displayingrecords", "", $totalcountlogs);
163 echo "</p>";
165 echo "<table border=0 align=center cellpadding=3 cellspacing=3>";
166 foreach ($logs as $log) {
168 if ($ld = get_record("log_display", "module", "$log->module", "action", "$log->action")) {
169 $log->info = get_field($ld->mtable, $ld->field, "id", $log->info);
172 echo "<tr nowrap>";
173 if (! $course->category) {
174 echo "<td nowrap><font size=2><a href=\"view.php?id=$log->course\">".$courses[$log->course]."</a></td>";
176 echo "<td nowrap align=right><font size=2>".userdate($log->time, "%a")."</td>";
177 echo "<td nowrap><font size=2>".userdate($log->time, $strftimedatetime)."</td>";
178 echo "<td nowrap><font size=2>";
179 link_to_popup_window("/lib/ipatlas/plot.php?address=$log->ip&user=$log->userid", "ipatlas","$log->ip", 400, 700);
180 echo "</td>";
181 echo "<td nowrap><font size=2><a href=\"../user/view.php?id=$log->userid&course=$log->course\"><b>$log->firstname $log->lastname</b></td>";
182 echo "<td nowrap><font size=2>";
183 link_to_popup_window( make_log_url($log->module,$log->url), "fromloglive","$log->module $log->action", 400, 600);
184 echo "</td>";
185 echo "<td nowrap><font size=2>$log->info</td>";
186 echo "</tr>";
188 echo "</table>";
192 function print_all_courses($category="all", $style="full", $maxcount=999, $width=180) {
193 global $CFG, $THEME, $USER;
195 if ($category == "all") {
196 $courses = get_courses();
198 } else if ($category == "my") {
199 if (isset($USER->id)) {
200 if ($courses = get_courses()) {
201 foreach ($courses as $key => $course) {
202 if (!isteacher($course->id) and !isstudent($course->id)) {
203 unset($courses[$key]);
209 } else {
210 $courses = get_courses($category);
213 if ($style == "minimal") {
214 $count = 0;
215 if (empty($THEME->custompix)) {
216 $icon = "<img src=\"$CFG->wwwroot/pix/i/course.gif\" height=16 width=16 alt=\"".get_string("course")."\">";
217 } else {
218 $icon = "<img src=\"$CFG->wwwroot/theme/$CFG->theme/pix/i/course.gif\" height=16 width=16 alt=\"".get_string("course")."\">";
220 if ($courses) {
221 foreach ($courses as $course) {
222 $moddata[]="<a title=\"$course->shortname\" href=\"$CFG->wwwroot/course/view.php?id=$course->id\">$course->fullname</a>";
223 $modicon[]=$icon;
224 if ($count++ >= $maxcount) {
225 break;
228 $fulllist = "<p><a href=\"$CFG->wwwroot/course/\">".get_string("fulllistofcourses")."</a>...";
229 } else {
230 $moddata = array();
231 $modicon = array();
232 $fulllist = get_string("nocoursesyet");
234 print_side_block(get_string("courses"), "", $moddata, $modicon, $fulllist, $width);
236 } else if ($courses) {
237 foreach ($courses as $course) {
238 print_course($course);
239 echo "<br />\n";
242 } else {
243 echo "<p>".get_string("nocoursesyet")."</p>";
248 function print_course($course) {
250 global $CFG, $THEME;
252 if (! $site = get_site()) {
253 error("Could not find a site!");
256 if (empty($THEME->custompix)) {
257 $pixpath = "$CFG->wwwroot/pix";
258 } else {
259 $pixpath = "$CFG->wwwroot/theme/$CFG->theme/pix";
262 print_simple_box_start("CENTER", "100%");
264 echo "<TABLE WIDTH=100%>";
265 echo "<TR VALIGN=top>";
266 echo "<TD VALIGN=top WIDTH=50%>";
267 echo "<P><FONT SIZE=3><B><A TITLE=\"".get_string("entercourse")."\"
268 HREF=\"$CFG->wwwroot/course/view.php?id=$course->id\">$course->fullname</A></B></FONT></P>";
269 if ($teachers = get_course_teachers($course->id)) {
270 echo "<P><FONT SIZE=1>\n";
271 foreach ($teachers as $teacher) {
272 if ($teacher->authority > 0) {
273 if (!$teacher->role) {
274 $teacher->role = $course->teacher;
276 echo "$teacher->role: <A HREF=\"$CFG->wwwroot/user/view.php?id=$teacher->id&course=$site->id\">$teacher->firstname $teacher->lastname</A><BR>";
279 echo "</FONT></P>";
281 if ($course->guest) {
282 $strallowguests = get_string("allowguests");
283 echo "<A TITLE=\"$strallowguests\" HREF=\"$CFG->wwwroot/course/view.php?id=$course->id\">";
284 echo "<IMG VSPACE=4 ALT=\"$strallowguests\" HEIGHT=16 WIDTH=16 BORDER=0 SRC=\"$pixpath/i/user.gif\"></A>&nbsp;&nbsp;";
286 if ($course->password) {
287 $strrequireskey = get_string("requireskey");
288 echo "<A TITLE=\"$strrequireskey\" HREF=\"$CFG->wwwroot/course/view.php?id=$course->id\">";
289 echo "<IMG VSPACE=4 ALT=\"$strrequireskey\" HEIGHT=16 WIDTH=16 BORDER=0 SRC=\"$pixpath/i/key.gif\"></A>";
293 echo "</TD><TD VALIGN=top WIDTH=50%>";
294 echo "<P><FONT SIZE=2>".text_to_html($course->summary)."</FONT></P>";
295 echo "</TD></TR>";
296 echo "</TABLE>";
298 print_simple_box_end();
301 function print_headline($text, $size=2) {
302 echo "<B><FONT SIZE=\"$size\">$text</FONT></B><BR>\n";
305 function print_recent_activity($course) {
306 // $course is an object
307 // This function trawls through the logs looking for
308 // anything new since the user's last login
310 global $CFG, $USER;
312 if (! $USER->lastlogin ) {
313 echo "<p align=center><font size=1>";
314 print_string("welcometocourse", "", $course->shortname);
315 echo "</font></p>";
316 return;
317 } else {
318 echo "<p align=center><font size=1>";
319 echo get_string("yourlastlogin").":<BR>";
320 echo userdate($USER->lastlogin, get_string("strftimerecentfull"));
321 echo "</font></p>";
324 $timestart = $USER->lastlogin;
325 $timemaxrecent = time() - COURSE_MAX_RECENT_PERIOD;
326 if ($timestart < $timemaxrecent) {
327 $timestart = $timemaxrecent;
331 // Firstly, have there been any new enrolments?
333 $heading = false;
334 $content = false;
336 $logs = get_records_select("log", "time > '$timestart' AND course = '$course->id' AND
337 module = 'course' AND action = 'enrol'", "time ASC");
339 if ($logs) {
340 foreach ($logs as $key => $log) {
341 if (! $heading) {
342 print_headline(get_string("newusers").":");
343 $heading = true;
344 $content = true;
346 $user = get_record("user", "id", $log->info);
347 if (isstudent($course->id, $user->id)) {
348 echo "<p><font size=1><a href=\"../user/view.php?id=$user->id&course=$course->id\">$user->firstname $user->lastname</a></font></p>";
353 // Next, have there been any modifications to the course structure?
355 $logs = get_records_select("log", "time > '$timestart' AND course = '$course->id' AND
356 module = 'course' AND action LIKE '% mod'", "time ASC");
358 if ($logs) {
359 foreach ($logs as $key => $log) {
360 $info = split(" ", $log->info);
361 $modname = get_field($info[0], "name", "id", $info[1]);
362 //Create a temp valid module structure (course,id)
363 $tempmod->course = $log->course;
364 $tempmod->id = $info[1];
365 //Obtain the visible property from the instance
366 $modvisible = instance_is_visible($info[0],$tempmod);
368 //Only if the mod is visible
369 if ($modvisible) {
370 switch ($log->action) {
371 case "add mod":
372 $stradded = get_string("added", "moodle", get_string("modulename", $info[0]));
373 $changelist["$log->info"] = array ("operation" => "add", "text" => "$stradded:<BR><A HREF=\"$CFG->wwwroot/course/$log->url\">$modname</A>");
374 break;
375 case "update mod":
376 $strupdated = get_string("updated", "moodle", get_string("modulename", $info[0]));
377 if (empty($changelist["$log->info"])) {
378 $changelist["$log->info"] = array ("operation" => "update", "text" => "$strupdated:<BR><A HREF=\"$CFG->wwwroot/course/$log->url\">$modname</A>");
380 break;
381 case "delete mod":
382 if (!empty($changelist["$log->info"]["operation"]) and
383 $changelist["$log->info"]["operation"] == "add") {
384 $changelist["$log->info"] = NULL;
385 } else {
386 $strdeleted = get_string("deletedactivity", "moodle", get_string("modulename", $info[0]));
387 $changelist["$log->info"] = array ("operation" => "delete", "text" => $strdeleted);
389 break;
395 if (!empty($changelist)) {
396 foreach ($changelist as $changeinfo => $change) {
397 if ($change) {
398 $changes[$changeinfo] = $change;
401 if (count($changes) > 0) {
402 print_headline(get_string("courseupdates").":");
403 $content = true;
404 foreach ($changes as $changeinfo => $change) {
405 echo "<p><font size=1>".$change["text"]."</font></p>";
410 // Now display new things from each module
412 $mods = get_list_of_plugins("mod");
414 $isteacher = isteacher($course->id);
416 foreach ($mods as $mod) { // Each module gets it's own logs and prints them
417 include_once("$CFG->dirroot/mod/$mod/lib.php");
418 $print_recent_activity = $mod."_print_recent_activity";
419 if (function_exists($print_recent_activity)) {
420 $modcontent = $print_recent_activity($course, $isteacher, $timestart);
421 if ($modcontent) {
422 $content = true;
427 if (! $content) {
428 echo "<font size=2>".get_string("nothingnew")."</font>";
433 function get_array_of_activities($courseid) {
434 // For a given course, returns an array of course activity objects
435 // Each item in the array contains he following properties:
436 // cm - course module id
437 // mod - name of the module (eg forum)
438 // section - the number of the section (eg week or topic)
439 // name - the name of the instance
440 // visible - is the instance visible or not
442 $mod = array();
444 if (!$rawmods = get_course_mods($courseid)) {
445 return NULL;
448 if ($sections = get_records("course_sections", "course", $courseid, "section ASC")) {
449 foreach ($sections as $section) {
450 if (!empty($section->sequence)) {
451 $sequence = explode(",", $section->sequence);
452 foreach ($sequence as $seq) {
453 if (empty($rawmods[$seq])) {
454 continue;
456 $mod[$seq]->cm = $rawmods[$seq]->id;
457 $mod[$seq]->mod = $rawmods[$seq]->modname;
458 $mod[$seq]->section = $section->section;
459 $mod[$seq]->name = urlencode(get_field($rawmods[$seq]->modname, "name", "id", $rawmods[$seq]->instance));
460 $mod[$seq]->visible = $rawmods[$seq]->visible;
465 return $mod;
471 function get_all_mods($courseid, &$mods, &$modnames, &$modnamesplural, &$modnamesused) {
472 // Returns a number of useful structures for course displays
474 $mods = NULL; // course modules indexed by id
475 $modnames = NULL; // all course module names
476 $modnamesplural= NULL; // all course module names (plural form)
477 $modnamesused = NULL; // course module names used
479 if ($allmods = get_records("modules")) {
480 foreach ($allmods as $mod) {
481 if ($mod->visible) {
482 $modnames[$mod->name] = get_string("modulename", "$mod->name");
483 $modnamesplural[$mod->name] = get_string("modulenameplural", "$mod->name");
486 asort($modnames);
487 } else {
488 error("No modules are installed!");
491 if ($rawmods = get_course_mods($courseid)) {
492 foreach($rawmods as $mod) { // Index the mods
493 $mods[$mod->id] = $mod;
494 $mods[$mod->id]->modfullname = $modnames[$mod->modname];
495 if ($mod->visible or isteacher($courseid)) {
496 $modnamesused[$mod->modname] = $modnames[$mod->modname];
499 if ($modnamesused) {
500 asort($modnamesused);
506 function get_all_sections($courseid) {
508 return get_records("course_sections", "course", "$courseid", "section",
509 "section, id, course, summary, sequence, visible");
512 function course_set_display($courseid, $display=0) {
513 global $USER;
515 if (empty($USER)) {
516 return false;
519 if ($display == "all" or empty($display)) {
520 $display = 0;
523 if (record_exists("course_display", "userid", $USER->id, "course", $courseid)) {
524 set_field("course_display", "display", $display, "userid", $USER->id, "course", $courseid);
525 } else {
526 $record->userid = $USER->id;
527 $record->course = $courseid;
528 $record->display = $display;
529 if (!insert_record("course_display", $record)) {
530 notify("Could not save your course display!");
534 return $USER->display[$courseid] = $display; // Note: = not ==
537 function set_section_visible($courseid, $sectionnumber, $visibility) {
538 /// For a given course section, markes it visible or hidden,
539 /// and does the same for every activity in that section
541 if ($section = get_record("course_sections", "course", $courseid, "section", $sectionnumber)) {
542 set_field("course_sections", "visible", "$visibility", "id", $section->id);
543 if (!empty($section->sequence)) {
544 $modules = explode(",", $section->sequence);
545 foreach ($modules as $moduleid) {
546 set_field("course_modules", "visible", "$visibility", "id", $moduleid);
549 rebuild_course_cache($courseid);
553 function print_section_block($heading, $course, $section, $mods, $modnames, $modnamesused,
554 $absolute=true, $width="100%", $isediting=false) {
556 global $CFG;
558 $modinfo = unserialize($course->modinfo);
559 $moddata = array();
560 $modicon = array();
561 $editbuttons = "";
563 if (!empty($section->sequence)) {
565 $sectionmods = explode(",", $section->sequence);
567 foreach ($sectionmods as $modnumber) {
568 $mod = $mods[$modnumber];
569 if ($isediting) {
570 $editbuttons = make_editing_buttons($mod->id, $absolute, $mod->visible);
572 if ($mod->visible or isteacher($course->id)) {
573 $instancename = urldecode($modinfo[$modnumber]->name);
574 if ($mod->visible) {
575 $link_css = "";
576 } else {
577 $link_css = " class=\"dimmed\" ";
579 $modicon[] = "<img src=\"$CFG->wwwroot/mod/$mod->modname/icon.gif\" height=\"16\" width=\"16\" alt=\"$mod->modfullname\">";
580 $moddata[] = "<a title=\"$mod->modfullname\" $link_css href=\"$CFG->wwwroot/mod/$mod->modname/view.php?id=$mod->id\">$instancename</a><BR>$editbuttons";
584 if ($isediting) {
585 $editmenu = popup_form("$CFG->wwwroot/course/mod.php?id=$course->id&amp;section=$section->section&add=",
586 $modnames, "section0", "", get_string("add")."...", "mods", get_string("activities"), true);
587 $editmenu = "<div align=right>$editmenu</div>";
588 } else {
589 $editmenu = "";
592 print_side_block($heading, "", $moddata, $modicon, $editmenu, $width);
596 function print_section($course, $section, $mods, $modnamesused, $absolute=false, $width="100%") {
597 global $CFG;
599 $modinfo = unserialize($course->modinfo);
601 echo "<table width=\"$width\"><tr><td>\n";
602 if (!empty($section->sequence)) {
604 $sectionmods = explode(",", $section->sequence);
606 foreach ($sectionmods as $modnumber) {
607 if (empty($mods[$modnumber])) {
608 continue;
610 $mod = $mods[$modnumber];
611 if ($mod->visible or isteacher($course->id)) {
612 $instancename = urldecode($modinfo[$modnumber]->name);
613 if ($mod->visible) {
614 $link_css = "";
615 } else {
616 $link_css = " class=\"dimmed\" ";
618 echo "<img src=\"$CFG->wwwroot/mod/$mod->modname/icon.gif\" height=16 width=16 alt=\"$mod->modfullname\">";
619 echo " <font size=2><a title=\"$mod->modfullname\" $link_css";
620 echo " href=\"$CFG->wwwroot/mod/$mod->modname/view.php?id=$mod->id\">$instancename</a></font>";
622 if (isediting($course->id)) {
623 echo "&nbsp;&nbsp;";
624 echo make_editing_buttons($mod->id, $absolute, $mod->visible);
626 if ($mod->visible or isteacher($course->id)) {
627 echo "<br />\n";
631 echo "</td></tr></table><br />\n\n";
635 function rebuild_course_cache($courseid=0) {
636 // Rebuilds the cached list of course activities stored in the database
637 // If a courseid is not specified, then all are rebuilt
639 if ($courseid) {
640 $select = "id = '$courseid'";
641 } else {
642 $select = "";
645 if ($courses = get_records_select("course", $select)) {
646 foreach ($courses as $course) {
647 $modinfo = serialize(get_array_of_activities($course->id));
648 if (!set_field("course", "modinfo", $modinfo, "id", $course->id)) {
649 notify("Could not cache module information for course '$course->fullname'!");
656 function print_heading_block($heading, $width="100%", $class="headingblock") {
657 global $THEME;
659 echo "<table width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"5\">";
660 echo "<tr><td bgcolor=\"$THEME->cellheading\" class=\"$class\">";
661 echo stripslashes($heading);
662 echo "</td></tr></table>";
665 function print_side_block($heading="", $content="", $list=NULL, $icons=NULL, $footer="", $width=180) {
666 // Prints a nice side block with an optional header. The content can either
667 // be a block of HTML or a list of text with optional icons.
669 global $THEME;
671 print_side_block_start($heading, $width);
673 if ($content) {
674 echo "$content";
675 } else {
676 echo "<table width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"2\">";
677 foreach ($list as $key => $string) {
678 echo "<tr bgcolor=\"$THEME->cellcontent2\">";
679 if ($icons) {
680 echo "<td class=\"sideblocklinks\" valign=\"top\" width=\"16\">".$icons[$key]."</td>";
682 echo "<td class=\"sideblocklinks\" valign=\"top\" width=\"*\"><font size=\"-1\">$string</font></td>";
683 echo "</tr>";
685 if ($footer) {
686 echo "<tr bgcolor=\"$THEME->cellcontent2\">";
687 if ($icons) {
688 echo "<td class=\"sideblocklinks\" valign=\"top\" width=\"16\">&nbsp;</td>";
690 echo "<td class=\"sideblocklinks\"><font size=\"-1\">$footer</td>";
691 echo "</tr>";
693 echo "</table>";
696 print_side_block_end();
699 function print_side_block_start($heading="", $width=180, $class="sideblockmain") {
700 // Starts a nice side block with an optional header.
702 global $THEME;
704 echo "<table class=\"sideblock\" width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"5\">";
705 if ($heading) {
706 echo "<tr>";
707 echo "<td class=\"sideblockheading\" bgcolor=\"$THEME->cellheading\">$heading</td>";
708 echo "</tr>";
710 echo "<tr>";
711 echo "<td class=\"$class\" bgcolor=\"$THEME->cellcontent2\">";
714 function print_side_block_end() {
715 echo "</td></tr>";
716 echo "</table><br />";
720 function print_admin_links ($siteid, $width=180) {
721 global $CFG, $THEME;
723 if (empty($THEME->custompix)) {
724 $icon = "<img src=\"$CFG->wwwroot/pix/i/settings.gif\" height=16 width=16 alt=\"\">";
725 } else {
726 $icon = "<img src=\"$CFG->wwwroot/theme/$CFG->theme/pix/i/settings.gif\" height=16 width=16 alt=\"\">";
729 if (isadmin()) {
730 $moddata[]="<a href=\"$CFG->wwwroot/$CFG->admin/config.php\">".get_string("configvariables")."</a>";
731 $modicon[]=$icon;
732 $moddata[]="<a href=\"$CFG->wwwroot/$CFG->admin/site.php\">".get_string("sitesettings")."</a>";
733 $modicon[]=$icon;
734 $moddata[]="<a href=\"$CFG->wwwroot/course/log.php?id=$siteid\">".get_string("sitelogs")."</a>";
735 $modicon[]=$icon;
736 $moddata[]="<a href=\"$CFG->wwwroot/theme/index.php\">".get_string("choosetheme")."</a>";
737 $modicon[]=$icon;
738 $moddata[]="<a href=\"$CFG->wwwroot/$CFG->admin/lang.php\">".get_string("checklanguage")."</a>";
739 $modicon[]=$icon;
740 $moddata[]="<a href=\"$CFG->wwwroot/$CFG->admin/modules.php\">".get_string("managemodules")."</a>";
741 $modicon[]=$icon;
742 if (file_exists("$CFG->dirroot/$CFG->admin/$CFG->dbtype")) {
743 $moddata[]="<a href=\"$CFG->wwwroot/$CFG->admin/$CFG->dbtype/frame.php\">".get_string("managedatabase")."</a>";
744 $modicon[]=$icon;
746 $moddata[]="<hr>";
747 $modicon[]="";
749 if (iscreator()) {
750 $moddata[]="<a href=\"$CFG->wwwroot/course/edit.php\">".get_string("addnewcourse")."</a>";
751 $modicon[]=$icon;
752 $moddata[]="<a href=\"$CFG->wwwroot/$CFG->admin/teacher.php\">".get_string("assignteachers")."</a>";
753 $modicon[]=$icon;
754 $fulladmin = "";
756 if (isadmin()) {
757 $moddata[]="<a href=\"$CFG->wwwroot/course/categories.php\">".get_string("categories")."</a>";
758 $modicon[]=$icon;
759 $moddata[]="<a href=\"$CFG->wwwroot/course/delete.php\">".get_string("deletecourse")."</a>";
760 $modicon[]=$icon;
761 $moddata[]="<hr>";
762 $modicon[]="";
763 if($CFG->auth == "email" || $CFG->auth == "none" || $CFG->auth == "manual"){
764 $moddata[]="<a href=\"$CFG->wwwroot/$CFG->admin/user.php?newuser=true\">".get_string("addnewuser")."</a>";
765 $modicon[]=$icon;
767 $moddata[]="<a href=\"$CFG->wwwroot/$CFG->admin/user.php\">".get_string("edituser")."</a>";
768 $modicon[]=$icon;
769 $moddata[]="<a href=\"$CFG->wwwroot/$CFG->admin/admin.php\">".get_string("assignadmins")."</a>";
770 $modicon[]=$icon;
771 $moddata[]="<a href=\"$CFG->wwwroot/$CFG->admin/creators.php\">".get_string("assigncreators")."</a>";
772 $modicon[]=$icon;
773 $moddata[]="<a href=\"$CFG->wwwroot/$CFG->admin/auth.php\">".get_string("authentication")."</a>";
774 $modicon[]=$icon;
775 $fulladmin = "<p><a href=\"$CFG->wwwroot/$CFG->admin/\">".get_string("admin")."</a>...";
778 print_side_block(get_string("administration"), "", $moddata, $modicon, $fulladmin, $width);
780 echo "<img src=\"$CFG->wwwroot/pix/spacer.gif\" width=\"$width\" height=1><br>";
783 function print_course_admin_links($course, $width=180) {
784 global $USER, $CFG, $THEME;
786 if (isguest()) {
787 return true;
789 if (empty($THEME->custompix)) {
790 $pixpath = "$CFG->wwwroot/pix";
791 $modpixpath = "$CFG->wwwroot/mod";
792 } else {
793 $pixpath = "$CFG->wwwroot/theme/$CFG->theme/pix";
794 $modpixpath = "$CFG->wwwroot/theme/$CFG->theme/pix/mod";
796 if (isteacher($course->id)) {
797 $adminicon[]="<img src=\"$pixpath/i/edit.gif\" height=16 width=16 alt=\"\">";
798 if (isediting($course->id)) {
799 $admindata[]="<a href=\"view.php?id=$course->id&edit=off\">".get_string("turneditingoff")."</a>";
800 } else {
801 $admindata[]="<a href=\"view.php?id=$course->id&edit=on\">".get_string("turneditingon")."</a>";
803 $admindata[]="<a href=\"edit.php?id=$course->id\">".get_string("settings")."...</a>";
804 $adminicon[]="<img src=\"$pixpath/i/settings.gif\" height=16 width=16 alt=\"\">";
805 if (!$course->teachers) {
806 $course->teachers = get_string("defaultcourseteachers");
808 $admindata[]="<a href=\"teachers.php?id=$course->id\">$course->teachers...</a>";
809 $adminicon[]="<img src=\"$pixpath/i/settings.gif\" height=16 width=16 alt=\"\">";
811 $admindata[]="<a href=\"grades.php?id=$course->id\">".get_string("grades")."...</a>";
812 $adminicon[]="<img src=\"$pixpath/i/grades.gif\" height=16 width=16 alt=\"\">";
814 $admindata[]="<a href=\"log.php?id=$course->id\">".get_string("logs")."...</a>";
815 $adminicon[]="<img src=\"$pixpath/i/log.gif\" height=16 width=16 alt=\"\">";
817 $admindata[]="<a href=\"$CFG->wwwroot/files/index.php?id=$course->id\">".get_string("files")."...</a>";
818 $adminicon[]="<img src=\"$pixpath/i/files.gif\" height=16 width=16 alt=\"\">";
820 $admindata[]="<a href=\"$CFG->wwwroot/doc/view.php?id=$course->id&file=teacher.html\">".get_string("help")."...</a>";
821 $adminicon[]="<img src=\"$modpixpath/resource/icon.gif\" height=16 width=16 alt=\"\">";
823 if ($teacherforum = forum_get_course_forum($course->id, "teacher")) {
824 $admindata[]="<a href=\"$CFG->wwwroot/mod/forum/view.php?f=$teacherforum->id\">".get_string("nameteacher", "forum")."</a>";
825 $adminicon[]="<img src=\"$modpixpath/forum/icon.gif\" height=16 width=16 alt=\"\">";
827 } else {
828 $admindata[]="<a href=\"grade.php?id=$course->id\">".get_string("grades")."...</a>";
829 $adminicon[]="<img src=\"$pixpath/i/grades.gif\" height=16 width=16 alt=\"\">";
832 print_side_block(get_string("administration"), "", $admindata, $adminicon, "", $width);
835 function print_course_categories($categories, $selected="none", $width=180) {
836 global $CFG, $THEME, $USER;
838 $strallowguests = get_string("allowguests");
839 $strrequireskey = get_string("requireskey");
841 if (empty($THEME->custompix)) {
842 $pixpath = "$CFG->wwwroot/pix";
843 } else {
844 $pixpath = "$CFG->wwwroot/theme/$CFG->theme/pix";
847 if ($selected == "index") { // Print comprehensive index of categories with courses
848 if ($courses = get_courses()) {
849 if (isset($USER->id) and !isadmin()) {
850 print_simple_box_start("CENTER", "100%", $THEME->cellheading);
851 print_heading("<a href=\"course/index.php?category=my\">".get_string("mycourses")."</a>", "left");
852 $some = false;
853 echo "<ul>";
854 foreach ($courses as $key => $course) {
855 if (isteacher($course->id) or isstudent($course->id)) {
856 echo "<a href=\"$CFG->wwwroot/course/view.php?id=$course->id\">$course->fullname</a>";
857 echo "<br />";
858 $some = true;
861 if (!$some) {
862 print_string("nocoursesyet");
864 echo "</ul>";
865 print_simple_box_end();
866 print_spacer(8,1);
868 foreach ($categories as $category) {
869 print_simple_box_start("CENTER", "100%");
870 print_heading("<a href=\"course/index.php?category=$category->id\">$category->name</a>", "left");
871 $some = false;
872 echo "<ul>";
873 foreach ($courses as $key => $course) {
874 if ($course->category == $category->id) {
875 echo "<a href=\"$CFG->wwwroot/course/view.php?id=$course->id\">$course->fullname</a>";
876 echo "&nbsp;&nbsp;";
877 unset($courses[$key]);
878 if ($course->guest ) {
879 echo "<a title=\"$strallowguests\" href=\"$CFG->wwwroot/course/view.php?id=$course->id\">";
880 echo "<img alt=\"\" height=16 width=16 border=0 src=\"$pixpath/i/user.gif\"></a>";
882 if ($course->password) {
883 echo "<a title=\"$strrequireskey\" href=\"$CFG->wwwroot/course/view.php?id=$course->id\">";
884 echo "<img alt=\"\" height=16 width=16 border=0 src=\"$pixpath/i/key.gif\"></a>";
886 echo "<br />";
887 $some = true;
890 if (!$some) {
891 print_string("nocoursesyet");
893 echo "</ul>";
894 print_simple_box_end();
895 print_spacer(8,1);
899 } else { // Print short list of categories only
900 foreach ($categories as $cat) {
901 $caticon[]="<img src=\"$pixpath/i/course.gif\" height=16 width=16>";
902 if ($cat->id == $selected) {
903 $catdata[]="$cat->name";
904 } else {
905 $catdata[]="<a href=\"$CFG->wwwroot/course/index.php?category=$cat->id\">$cat->name</a>";
908 $catdata[] = "<a href=\"$CFG->wwwroot/course/index.php?category=all\">".get_string("fulllistofcourses")."</a>";
909 $caticon[] = "";
910 if (isset($USER->id)) {
911 $catdata[] = "<a href=\"$CFG->wwwroot/course/index.php?category=my\">".get_string("mycourses")."</a>";
912 $caticon[] = "";
914 print_side_block(get_string("categories"), "", $catdata, $caticon, "", $width);
918 function print_log_graph($course, $userid=0, $type="course.png", $date=0) {
919 global $CFG;
920 if (empty($CFG->gdversion)) {
921 echo "(".get_string("gdneed").")";
922 } else {
923 echo "<IMG BORDER=0 SRC=\"$CFG->wwwroot/course/loggraph.php?id=$course->id&user=$userid&type=$type&date=$date\">";
929 /// MODULE FUNCTIONS /////////////////////////////////////////////////////////////////
931 function add_course_module($mod) {
932 GLOBAL $db;
934 $mod->added = time();
936 return insert_record("course_modules", $mod);
939 function add_mod_to_section($mod) {
940 // Returns the course_sections ID where the mod is inserted
941 GLOBAL $db;
943 if ($section = get_record("course_sections", "course", "$mod->course", "section", "$mod->section")) {
944 if (!empty($section->sequence)) {
945 $newsequence = "$section->sequence,$mod->coursemodule";
946 } else {
947 $newsequence = "$mod->coursemodule";
949 if (set_field("course_sections", "sequence", $newsequence, "id", $section->id)) {
950 return $section->id; // Return course_sections ID that was used.
951 } else {
952 return 0;
955 } else { // Insert a new record
956 $section->course = $mod->course;
957 $section->section = $mod->section;
958 $section->summary = "";
959 $section->sequence = $mod->coursemodule;
960 return insert_record("course_sections", $section);
964 function hide_course_module($mod) {
965 return set_field("course_modules", "visible", 0, "id", $mod);
968 function show_course_module($mod) {
969 return set_field("course_modules", "visible", 1, "id", $mod);
972 function delete_course_module($mod) {
973 return set_field("course_modules", "deleted", 1, "id", $mod);
976 function delete_mod_from_section($mod, $section) {
977 GLOBAL $db;
979 if ($section = get_record("course_sections", "id", "$section") ) {
981 $modarray = explode(",", $section->sequence);
983 if ($key = array_keys ($modarray, $mod)) {
984 array_splice($modarray, $key[0], 1);
985 $newsequence = implode(",", $modarray);
986 return set_field("course_sections", "sequence", $newsequence, "id", $section->id);
987 } else {
988 return false;
991 } else {
992 return false;
997 function move_module($cm, $move) {
998 GLOBAL $db;
1000 if (!$move) {
1001 return true;
1004 if (! $thissection = get_record("course_sections", "id", $cm->section)) {
1005 error("This course section doesn't exist");
1008 $mods = explode(",", $thissection->sequence);
1010 $len = count($mods);
1011 $pos = array_keys($mods, $cm->id);
1012 $thepos = $pos[0];
1014 if ($len == 0 || count($pos) == 0 ) {
1015 error("Very strange. Could not find the required module in this section.");
1018 if ($len == 1) {
1019 $first = true;
1020 $last = true;
1021 } else {
1022 $first = ($thepos == 0);
1023 $last = ($thepos == $len - 1);
1026 if ($move < 0) { // Moving the module up
1028 if ($first) {
1029 if ($thissection->section == 1) { // First section, do nothing
1030 return true;
1031 } else { // Push onto end of previous section
1032 $prevsectionnumber = $thissection->section - 1;
1033 if (! $prevsection = get_record("course_sections", "course", "$thissection->course",
1034 "section", "$prevsectionnumber")) {
1035 error("Previous section ($prevsection->id) doesn't exist");
1038 if (!empty($prevsection->sequence)) {
1039 $newsequence = "$prevsection->sequence,$cm->id";
1040 } else {
1041 $newsequence = "$cm->id";
1044 if (! set_field("course_sections", "sequence", $newsequence, "id", $prevsection->id)) {
1045 error("Previous section could not be updated");
1048 if (! set_field("course_modules", "section", $prevsection->id, "id", $cm->id)) {
1049 error("Module could not be updated");
1052 array_splice($mods, 0, 1);
1053 $newsequence = implode(",", $mods);
1054 if (! set_field("course_sections", "sequence", $newsequence, "id", $thissection->id)) {
1055 error("Module could not be updated");
1058 return true;
1061 } else { // move up within this section
1062 $swap = $mods[$thepos-1];
1063 $mods[$thepos-1] = $mods[$thepos];
1064 $mods[$thepos] = $swap;
1066 $newsequence = implode(",", $mods);
1067 if (! set_field("course_sections", "sequence", $newsequence, "id", $thissection->id)) {
1068 error("This section could not be updated");
1070 return true;
1073 } else { // Moving the module down
1075 if ($last) {
1076 $nextsectionnumber = $thissection->section + 1;
1077 if ($nextsection = get_record("course_sections", "course", "$thissection->course",
1078 "section", "$nextsectionnumber")) {
1080 if (!empty($nextsection->sequence)) {
1081 $newsequence = "$cm->id,$nextsection->sequence";
1082 } else {
1083 $newsequence = "$cm->id";
1086 if (! set_field("course_sections", "sequence", $newsequence, "id", $nextsection->id)) {
1087 error("Next section could not be updated");
1090 if (! set_field("course_modules", "section", $nextsection->id, "id", $cm->id)) {
1091 error("Module could not be updated");
1094 array_splice($mods, $thepos, 1);
1095 $newsequence = implode(",", $mods);
1096 if (! set_field("course_sections", "sequence", $newsequence, "id", $thissection->id)) {
1097 error("This section could not be updated");
1099 return true;
1101 } else { // There is no next section, so just return
1102 return true;
1105 } else { // move down within this section
1106 $swap = $mods[$thepos+1];
1107 $mods[$thepos+1] = $mods[$thepos];
1108 $mods[$thepos] = $swap;
1110 $newsequence = implode(",", $mods);
1111 if (! set_field("course_sections", "sequence", $newsequence, "id", $thissection->id)) {
1112 error("This section could not be updated");
1114 return true;
1119 function make_editing_buttons($moduleid, $absolute=false, $visible=true, $str=NULL) {
1120 global $CFG, $THEME;
1122 if (empty($str)) {
1123 $str->delete = get_string("delete");
1124 $str->moveup = get_string("moveup");
1125 $str->movedown = get_string("movedown");
1126 $str->update = get_string("update");
1127 $str->hide = get_string("hide");
1128 $str->show = get_string("show");
1131 if ($absolute) {
1132 $path = "$CFG->wwwroot/course";
1133 } else {
1134 $path = ".";
1137 if (empty($THEME->custompix)) {
1138 $pixpath = "$path/../pix";
1139 } else {
1140 $pixpath = "$path/../theme/$CFG->theme/pix";
1143 if ($visible) {
1144 $hideshow = " <a title=\"$str->hide\" href=\"$path/mod.php?hide=$moduleid\"><img
1145 src=\"$pixpath/t/hide.gif\" hspace=2 height=11 width=11 border=0></a>";
1146 } else {
1147 $hideshow = " <a title=\"$str->show\" href=\"$path/mod.php?show=$moduleid\"><img
1148 src=\"$pixpath/t/show.gif\" hspace=2 height=11 width=11 border=0></a>";
1151 return "<a title=\"$str->delete\" href=\"$path/mod.php?delete=$moduleid\"><img
1152 src=\"$pixpath/t/delete.gif\" height=11 width=11 border=0></a>
1153 <a title=\"$str->moveup\" href=\"$path/mod.php?id=$moduleid&move=-1\"><img
1154 src=\"$pixpath/t/up.gif\" height=11 width=11 border=0></a>
1155 <a title=\"$str->movedown\" href=\"$path/mod.php?id=$moduleid&move=1\"><img
1156 src=\"$pixpath/t/down.gif\" height=11 width=11 border=0></a>
1157 <a title=\"$str->update\" href=\"$path/mod.php?update=$moduleid\"><img
1158 src=\"$pixpath/t/edit.gif\" height=11 width=11 border=0></a> $hideshow";