Hide/show individual activities in any section
[moodle.git] / course / lib.php
1 <? // $Id$
2    // Library of useful functions
5 if (isset($COURSE_MAX_LOG_DISPLAY)) {  // Being included again - should never happen!!
6     return;
7 }
9 $COURSE_MAX_LOG_DISPLAY = 150;       // days
11 $COURSE_MAX_LOGS_PER_PAGE = 1000;    // records
13 $COURSE_TEACHER_COLOR = "#990000";   // To hilight certain items that are teacher-only
15 $COURSE_LIVELOG_REFRESH = 60;        // 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";
30             }
31         }
32         if ($guest = get_guest()) {
33             $users[$guest->id] = "$guest->firstname $guest->lastname";
34         }
35     }
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)";
44                 }
45             }
46         }
47         asort($courses);
48     }
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;
70     }
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++;
78     }
80     if ($selecteddate == "today") {
81         $selecteddate = $today;
82     }
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\">";
90     }
91     if ($course->category) {
92         choose_from_menu ($users, "user", $selecteduser, get_string("allparticipants") );
93     }
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>";
98 }
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;
113     }
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, $COURSE_MAX_LOGS_PER_PAGE;
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";
131             }
132         }
133     }
135     if ($user) {
136         $selector .= " AND l.userid = '$user'";
137     }
139     if ($date) {
140         $enddate = $date + 86400;
141         $selector .= " AND l.time > '$date' AND l.time < '$enddate'";
142     }
144     if (!$logs = get_logs($selector, $order)) {
145         notify("No logs found!");
146         print_footer($course);
147         exit;
148     }
150     $count=0;
151     $tt = getdate(time());
152     $today = mktime (0, 0, 0, $tt["mon"], $tt["mday"], $tt["year"]);
153     if (($totalcountlogs = count($logs)) > $COURSE_MAX_LOGS_PER_PAGE) {
154         $totalcountlogs = "$COURSE_MAX_LOGS_PER_PAGE/$totalcountlogs";
155     }
157     $strftimedatetime = get_string("strftimedatetime");
159     echo "<P ALIGN=CENTER>";
160     print_string("displayingrecords", "", $totalcountlogs);
161     echo "</P>";
163     $countlogs = 0;
164     echo "<TABLE BORDER=0 ALIGN=center CELLPADDING=3 CELLSPACING=3>";
165     foreach ($logs as $log) {
167         $countlogs++;
169         if ($countlogs > $COURSE_MAX_LOGS_PER_PAGE) {
170             break;
171         }
173         if ($ld = get_record("log_display", "module", "$log->module", "action", "$log->action")) {
174             $log->info = get_field($ld->mtable, $ld->field, "id", $log->info);
175         }
177         echo "<TR NOWRAP>";
178         if (! $course->category) {
179             echo "<TD NOWRAP><FONT SIZE=2><A HREF=\"view.php?id=$log->course\">".$courses[$log->course]."</A></TD>";
180         }
181         echo "<TD NOWRAP ALIGN=right><FONT SIZE=2>".userdate($log->time, "%A")."</TD>";
182         echo "<TD NOWRAP><FONT SIZE=2>".userdate($log->time, $strftimedatetime)."</TD>";
183         echo "<TD NOWRAP><FONT SIZE=2>";
184         link_to_popup_window("/lib/ipatlas/plot.php?address=$log->ip&user=$log->userid", "ipatlas","$log->ip", 400, 700);
185         echo "</TD>";
186         echo "<TD NOWRAP><FONT SIZE=2><A HREF=\"../user/view.php?id=$log->userid&course=$log->course\"><B>$log->firstname $log->lastname</B></TD>";
187         echo "<TD NOWRAP><FONT SIZE=2>";
188         link_to_popup_window( make_log_url($log->module,$log->url), "fromloglive","$log->module $log->action", 400, 600);
189         echo "</TD>";
190         echo "<TD NOWRAP><FONT SIZE=2>$log->info</TD>";
191         echo "</TR>";
192     }
193     echo "</TABLE>";
197 function print_all_courses($category="all", $style="full", $maxcount=999, $width=180) {
198     global $CFG, $USER;
200     if ($category == "all") {
201         $courses = get_courses();
203     } else if ($category == "my") {
204         if (isset($USER->id)) {
205             if ($courses = get_courses()) {
206                 foreach ($courses as $key => $course) {
207                     if (!isteacher($course->id) and !isstudent($course->id)) {
208                         unset($courses[$key]);
209                     }
210                 }
211             }
212         }
214     } else {
215         $courses = get_courses($category);
216     }
218     if ($style == "minimal") {
219         $count = 0;
220         $icon  = "<IMG SRC=\"pix/i/course.gif\" HEIGHT=16 WIDTH=16 ALT=\"".get_string("course")."\">";
221         if ($courses) {
222             foreach ($courses as $course) {
223                 $moddata[]="<A TITLE=\"$course->shortname\" HREF=\"$CFG->wwwroot/course/view.php?id=$course->id\">$course->fullname</A>";
224                 $modicon[]=$icon;
225                 if ($count++ >= $maxcount) {
226                     break;
227                 }
228             }   
229             $fulllist = "<P><A HREF=\"$CFG->wwwroot/course/\">".get_string("fulllistofcourses")."</A>...";
230         } else {
231             $moddata = array();
232             $modicon = array();
233             $fulllist = get_string("nocoursesyet");
234         }
235         print_side_block(get_string("courses"), "", $moddata, $modicon, $fulllist, $width);
237     } else if ($courses) {
238         foreach ($courses as $course) {
239             print_course($course);
240             echo "<BR>\n";
241         }
243     } else {
244         echo "<P>".get_string("nocoursesyet")."</P>";
245     }
249 function print_course($course) {
251     global $CFG;
253     if (! $site = get_site()) {
254         error("Could not find a site!");
255     }
257     print_simple_box_start("CENTER", "100%");
259     echo "<TABLE WIDTH=100%>";
260     echo "<TR VALIGN=top>";
261     echo "<TD VALIGN=top WIDTH=50%>";
262     echo "<P><FONT SIZE=3><B><A TITLE=\"".get_string("entercourse")."\" 
263               HREF=\"$CFG->wwwroot/course/view.php?id=$course->id\">$course->fullname</A></B></FONT></P>";
264     if ($teachers = get_course_teachers($course->id)) {
265         echo "<P><FONT SIZE=1>\n";
266         foreach ($teachers as $teacher) {
267             if ($teacher->authority > 0) {
268                 if (!$teacher->role) {
269                     $teacher->role = $course->teacher;
270                 }
271                 echo "$teacher->role: <A HREF=\"$CFG->wwwroot/user/view.php?id=$teacher->id&course=$site->id\">$teacher->firstname $teacher->lastname</A><BR>";
272             }
273         }
274         echo "</FONT></P>";
275     }
276     if ($course->guest) {
277         $strallowguests = get_string("allowguests");
278         echo "<A TITLE=\"$strallowguests\" HREF=\"$CFG->wwwroot/course/view.php?id=$course->id\">";
279         echo "<IMG VSPACE=4 ALT=\"$strallowguests\" HEIGHT=16 WIDTH=16 BORDER=0 SRC=\"$CFG->wwwroot/user/user.gif\"></A>&nbsp;&nbsp;";
280     }
281     if ($course->password) {
282         $strrequireskey = get_string("requireskey");
283         echo "<A TITLE=\"$strrequireskey\" HREF=\"$CFG->wwwroot/course/view.php?id=$course->id\">";
284         echo "<IMG VSPACE=4 ALT=\"$strrequireskey\" HEIGHT=16 WIDTH=16 BORDER=0 SRC=\"$CFG->wwwroot/pix/i/key.gif\"></A>";
285     }
288     echo "</TD><TD VALIGN=top WIDTH=50%>";
289     echo "<P><FONT SIZE=2>".text_to_html($course->summary)."</FONT></P>";
290     echo "</TD></TR>";
291     echo "</TABLE>";
293     print_simple_box_end();
296 function print_headline($text, $size=2) {
297     echo "<B><FONT SIZE=\"$size\">$text</FONT></B><BR>\n";
300 function print_recent_activity($course) {
301     // $course is an object
302     // This function trawls through the logs looking for 
303     // anything new since the user's last login
305     global $CFG, $USER, $COURSE_TEACHER_COLOR;
307     if (! $USER->lastlogin ) {
308         echo "<P ALIGN=CENTER><FONT SIZE=1>";
309         print_string("welcometocourse", "", $course->shortname);
310         echo "</FONT></P>";
311         return;
312     } else {
313         echo "<P ALIGN=CENTER><FONT SIZE=1>";
314         echo get_string("yourlastlogin").":<BR>"; 
315         echo userdate($USER->lastlogin, get_string("strftimerecentfull"));
316         echo "</FONT></P>";
317     }
319     if (! $logs = get_records_select("log", "time > '$USER->lastlogin' AND course = '$course->id'", "time ASC")) {
320         return;
321     }
324     // Firstly, have there been any new enrolments?
326     $heading = false;
327     $content = false;
328     foreach ($logs as $log) {
329         if ($log->module == "course" and $log->action == "enrol") {
330             if (! $heading) {
331                 print_headline(get_string("newusers").":");
332                 $heading = true;
333                 $content = true;
334             }
335             $user = get_record("user", "id", $log->info);
336             if (isstudent($course->id, $user->id)) {
337                 echo "<P><FONT SIZE=1><A HREF=\"../user/view.php?id=$user->id&course=$course->id\">$user->firstname $user->lastname</A></FONT></P>";
338             }
339         }
340     }
342     // Next, have there been any changes to the course structure?
344     foreach ($logs as $log) {
345         if ($log->module == "course") {
346             if ($log->action == "add mod" or $log->action == "update mod" or $log->action == "delete mod") {
347                 $info = split(" ", $log->info);
348                 $modname = get_field($info[0], "name", "id", $info[1]);
349             
350                 switch ($log->action) {
351                     case "add mod":
352                        $stradded = get_string("added", "moodle", get_string("modulename", $info[0]));
353                        $changelist["$log->info"] = array ("operation" => "add", "text" => "$stradded:<BR><A HREF=\"$CFG->wwwroot/course/$log->url\">$modname</A>");
354                     break;
355                     case "update mod":
356                        $strupdated = get_string("updated", "moodle", get_string("modulename", $info[0]));
357                        if (empty($changelist["$log->info"])) {
358                            $changelist["$log->info"] = array ("operation" => "update", "text" => "$strupdated:<BR><A HREF=\"$CFG->wwwroot/course/$log->url\">$modname</A>");
359                        }
360                     break;
361                     case "delete mod":
362                        if ($changelist["$log->info"]["operation"] == "add") {
363                            $changelist["$log->info"] = NULL;
364                        } else {
365                            $strdeleted = get_string("deletedactivity", "moodle", get_string("modulename", $info[0]));
366                            $changelist["$log->info"] = array ("operation" => "delete", "text" => $strdeleted);
367                        }
368                     break;
369                 }
370             }
371         }
372     }
374     if (!empty($changelist)) {
375         foreach ($changelist as $changeinfo => $change) {
376             if ($change) {
377                 $changes[$changeinfo] = $change;
378             }
379         }
380         if (count($changes) > 0) {
381             print_headline(get_string("courseupdates").":");
382             $content = true;
383             foreach ($changes as $changeinfo => $change) {
384                 echo "<P><FONT SIZE=1>".$change["text"]."</FONT></P>";
385             }
386         }
387     }
390     // Now display new things from each module
392     $mods = get_list_of_plugins("mod");
394     foreach ($mods as $mod) {
395         include_once("$CFG->dirroot/mod/$mod/lib.php");
396         $print_recent_activity = $mod."_print_recent_activity";
397         if (function_exists($print_recent_activity)) {
398             $modcontent = $print_recent_activity($logs, isteacher($course->id));
399             if ($modcontent) {
400                 $content = true;
401             }
402         }
403     }
406     if (! $content) {
407         echo "<FONT SIZE=2>".get_string("nothingnew")."</FONT>";
408     }
412 function get_array_of_activities($courseid) {
413 // For a given course, returns an array of course activity objects 
414 // Each item in the array contains he following properties:
415 //  cm - course module id
416 //  mod - name of the module (eg forum)
417 //  section - the number of the section (eg week or topic)
418 //  name - the name of the instance
420     $mod = array();
422     if (!$rawmods = get_course_mods($courseid)) {
423         return NULL;
424     }
426     if ($sections = get_records("course_sections", "course", $courseid, "section ASC")) {
427        foreach ($sections as $section) {
428            if (!empty($section->sequence)) {
429                $sequence = explode(",", $section->sequence);
430                foreach ($sequence as $seq) {
431                    if (empty($rawmods[$seq])) {
432                        continue;
433                    }
434                    $mod[$seq]->cm = $rawmods[$seq]->id;
435                    $mod[$seq]->mod = $rawmods[$seq]->modname;
436                    $mod[$seq]->section = $section->section;
437                    $mod[$seq]->name = urlencode(get_field($rawmods[$seq]->modname, "name", "id", $rawmods[$seq]->instance));
438                }
439             }
440         }
441     }
442     return $mod;
448 function get_all_mods($courseid, &$mods, &$modnames, &$modnamesplural, &$modnamesused) {
449 // Returns a number of useful structures for course displays
451     $mods          = NULL;    // course modules indexed by id
452     $modnames      = NULL;    // all course module names
453     $modnamesplural= NULL;    // all course module names (plural form)
454     $modnamesused  = NULL;    // course module names used
456     if ($allmods = get_records("modules")) {
457         foreach ($allmods as $mod) {
458             $modnames[$mod->name] = get_string("modulename", "$mod->name");
459             $modnamesplural[$mod->name] = get_string("modulenameplural", "$mod->name");
460         }
461         asort($modnames);
462     } else {
463         error("No modules are installed!");
464     }
466     if ($rawmods = get_course_mods($courseid)) {
467         foreach($rawmods as $mod) {    // Index the mods
468             $mods[$mod->id] = $mod;
469             $mods[$mod->id]->modfullname = $modnames[$mod->modname];
470             if ($mod->visible or isteacher($courseid)) {
471                 $modnamesused[$mod->modname] = $modnames[$mod->modname];
472             }
473         }
474         asort($modnamesused);
475     }
479 function get_all_sections($courseid) {
480     
481     return get_records("course_sections", "course", "$courseid", "section", 
482                        "section, id, course, summary, sequence");
486 function print_section_block($heading, $course, $section, $mods, $modnames, $modnamesused, 
487                              $absolute=true, $width="100%", $isediting=false) {
489     global $CFG;
491     $modinfo = unserialize($course->modinfo);
492     $moddata = array();
493     $modicon = array();
494     $editbuttons = "";
496     if (!empty($section->sequence)) {
498         $sectionmods = explode(",", $section->sequence);
500         foreach ($sectionmods as $modnumber) {
501             $mod = $mods[$modnumber];
502             if ($isediting) {
503                 $editbuttons = make_editing_buttons($mod->id, $absolute, $mod->visible);
504             }
505             if ($mod->visible or isteacher($course->id)) {
506                 $instancename = urldecode($modinfo[$modnumber]->name);
507                 if ($mod->visible) {
508                     $link_css = "";
509                 } else {
510                     $link_css = " class=\"dimmed\" ";
511                 }
512                 $modicon[] = "<img src=\"$CFG->wwwroot/mod/$mod->modname/icon.gif\" height=\"16\" width=\"16\" alt=\"$mod->modfullname\">";
513                 $moddata[] = "<a title=\"$mod->modfullname\" $link_css href=\"$CFG->wwwroot/mod/$mod->modname/view.php?id=$mod->id\">$instancename</a><BR>$editbuttons";
514             }
515         }
516     }
517     if ($isediting) {
518         $editmenu = popup_form("$CFG->wwwroot/course/mod.php?id=$course->id&amp;section=$section->section&add=", 
519                    $modnames, "section0", "", get_string("add")."...", "mods", get_string("activities"), true);
520         $editmenu = "<DIV ALIGN=right>$editmenu</DIV>";
521     } else {
522         $editmenu = "";
523     }
525     print_side_block($heading, "", $moddata, $modicon, $editmenu, $width);
529 function print_section($course, $section, $mods, $modnamesused, $absolute=false, $width="100%") {
530     global $CFG;
532     $modinfo = unserialize($course->modinfo);
534     echo "<TABLE WIDTH=\"$width\"><TR><TD>\n";
535     if (!empty($section->sequence)) {
537         $sectionmods = explode(",", $section->sequence);
539         foreach ($sectionmods as $modnumber) {
540             if (empty($mods[$modnumber])) {
541                 continue;
542             }
543             $mod = $mods[$modnumber];
544             if ($mod->visible or isteacher($course->id)) {
545                 $instancename = urldecode($modinfo[$modnumber]->name);
546                 if ($mod->visible) {
547                     $link_css = "";
548                 } else {
549                     $link_css = " class=\"dimmed\" ";
550                 }
551                 echo "<IMG SRC=\"$CFG->wwwroot/mod/$mod->modname/icon.gif\" HEIGHT=16 WIDTH=16 ALT=\"$mod->modfullname\">";
552                 echo " <FONT SIZE=2><A TITLE=\"$mod->modfullname\" $link_css";
553                 echo "   HREF=\"$CFG->wwwroot/mod/$mod->modname/view.php?id=$mod->id\">$instancename</A></FONT>";
554             }
555             if (isediting($course->id)) {
556                 echo "&nbsp;&nbsp;";
557                 echo make_editing_buttons($mod->id, $absolute, $mod->visible);
558             }
559             if ($mod->visible or isteacher($course->id)) {
560                 echo "<BR>\n";
561             }
562         }
563     }
564     echo "</TD></TR></TABLE><BR>\n\n";
567 function print_heading_block($heading, $width="100%", $class="headingblock") {
568     global $THEME;
570     echo "<table width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"5\">";
571     echo "<tr><td bgcolor=\"$THEME->cellheading\" class=\"$class\">";
572     echo stripslashes($heading);
573     echo "</td></tr></table>";
576 function print_side_block($heading="", $content="", $list=NULL, $icons=NULL, $footer="", $width=180) {
577 // Prints a nice side block with an optional header.  The content can either 
578 // be a block of HTML or a list of text with optional icons.
579     
580     global $THEME;
582     print_side_block_start($heading, $width);
584     if ($content) {
585         echo "$content";
586     } else {
587         echo "<table width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"2\">";
588         foreach ($list as $key => $string) {
589             echo "<tr bgcolor=\"$THEME->cellcontent2\">";
590             if ($icons) {
591                 echo "<td class=\"sideblocklinks\" valign=\"top\" width=\"16\">".$icons[$key]."</td>";
592             }
593             echo "<td class=\"sideblocklinks\" valign=\"top\" width=\"*\"><font size=\"-1\">$string</font></td>";
594             echo "</tr>";
595         }
596         if ($footer) {
597             echo "<tr bgcolor=\"$THEME->cellcontent2\">";
598             if ($icons) {
599                 echo "<td class=\"sideblocklinks\" valign=\"top\" width=\"16\">&nbsp;</td>";
600             }
601             echo "<td class=\"sideblocklinks\"><font size=\"-1\">$footer</td>";
602             echo "</tr>";
603         }
604         echo "</table>";
605     }
607     print_side_block_end();
610 function print_side_block_start($heading="", $width=180, $class="sideblockmain") {
611 // Starts a nice side block with an optional header.
612     
613     global $THEME;
615     echo "<table class=\"sideblock\" width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"5\">";
616     if ($heading) {
617         echo "<tr>";
618         echo "<td class=\"sideblockheading\" bgcolor=\"$THEME->cellheading\">$heading</td>";
619         echo "</tr>";
620     }
621     echo "<tr>";
622     echo "<td class=\"$class\" bgcolor=\"$THEME->cellcontent2\">";
625 function print_side_block_end() {
626     echo "</td></tr>";
627     echo "</table><br \>";
631 function print_admin_links ($siteid, $width=180) {
632     global $CFG;
633     
634     $icon = "<IMG SRC=\"$CFG->wwwroot/pix/i/settings.gif\" HEIGHT=16 WIDTH=16 ALT=\"\">";
635     if (isadmin()) {
636             $moddata[]="<A HREF=\"$CFG->wwwroot/$CFG->admin/config.php\">".get_string("configvariables")."</A>";
637                 $modicon[]=$icon;
638                 $moddata[]="<A HREF=\"$CFG->wwwroot/$CFG->admin/site.php\">".get_string("sitesettings")."</A>";
639                 $modicon[]=$icon;
640                 $moddata[]="<A HREF=\"$CFG->wwwroot/course/log.php?id=$siteid\">".get_string("sitelogs")."</A>";
641                 $modicon[]=$icon;
642                 $moddata[]="<A HREF=\"$CFG->wwwroot/theme/index.php\">".get_string("choosetheme")."</A>";
643                 $modicon[]=$icon;
644                 $moddata[]="<A HREF=\"$CFG->wwwroot/$CFG->admin/lang.php\">".get_string("checklanguage")."</A>";
645                 $modicon[]=$icon;
646                 if (file_exists("$CFG->dirroot/$CFG->admin/$CFG->dbtype")) {
647             $moddata[]="<A HREF=\"$CFG->wwwroot/$CFG->admin/$CFG->dbtype/frame.php\">".get_string("managedatabase")."</A>";
648                         $modicon[]=$icon;
649                 }
650                 $moddata[]="<HR>";
651                 $modicon[]="";
652     }
653     if (iscreator()) {
654             $moddata[]="<A HREF=\"$CFG->wwwroot/course/edit.php\">".get_string("addnewcourse")."</A>";
655                 $modicon[]=$icon;
656                 $moddata[]="<A HREF=\"$CFG->wwwroot/course/teacher.php\">".get_string("assignteachers")."</A>";
657                 $modicon[]=$icon;
658         $fulladmin = "";
659     }
660     if (isadmin()) {
661             $moddata[]="<A HREF=\"$CFG->wwwroot/course/categories.php\">".get_string("categories")."</A>";
662                 $modicon[]=$icon;
663                 $moddata[]="<A HREF=\"$CFG->wwwroot/course/delete.php\">".get_string("deletecourse")."</A>";
664                 $modicon[]=$icon;
665                 $moddata[]="<HR>";
666                 $modicon[]="";
667                 $moddata[]="<A HREF=\"$CFG->wwwroot/$CFG->admin/user.php?newuser=true\">".get_string("addnewuser")."</A>";
668                 $modicon[]=$icon;
669                 $moddata[]="<A HREF=\"$CFG->wwwroot/$CFG->admin/user.php\">".get_string("edituser")."</A>";
670                 $modicon[]=$icon;
671                 $moddata[]="<A HREF=\"$CFG->wwwroot/$CFG->admin/admin.php\">".get_string("assignadmins")."</A>";
672                 $modicon[]=$icon;
673         $moddata[]="<A HREF=\"$CFG->wwwroot/$CFG->admin/creators.php\">".get_string("assigncreators")."</A>";
674                 $modicon[]=$icon;
675                 $moddata[]="<A HREF=\"$CFG->wwwroot/$CFG->admin/auth.php\">".get_string("authentication")."</A>";
676                 $modicon[]=$icon;
677         $fulladmin = "<P><A HREF=\"$CFG->wwwroot/$CFG->admin/\">".get_string("admin")."</A>...";
678     }
680     print_side_block(get_string("administration"), "", $moddata, $modicon, $fulladmin, $width);
682     echo "<IMG SRC=\"$CFG->wwwroot/pix/spacer.gif\" WIDTH=\"$width\" HEIGHT=1><BR>";
685 function print_course_admin_links($course, $width=180) {
686     global $USER, $CFG;
688     if (isteacher($course->id)) {
689         $adminicon[]="<IMG SRC=\"$CFG->wwwroot/pix/i/edit.gif\" HEIGHT=16 WIDTH=16 ALT=\"\">";
690         if (isediting($course->id)) {
691             $admindata[]="<A HREF=\"view.php?id=$course->id&edit=off\">".get_string("turneditingoff")."</A>";
692         } else {
693             $admindata[]="<A HREF=\"view.php?id=$course->id&edit=on\">".get_string("turneditingon")."</A>";
694         }
695         $admindata[]="<A HREF=\"edit.php?id=$course->id\">".get_string("settings")."...</A>";
696         $adminicon[]="<IMG SRC=\"$CFG->wwwroot/pix/i/settings.gif\" HEIGHT=16 WIDTH=16 ALT=\"\">";
697         if (!$course->teachers) {
698             $course->teachers = get_string("defaultcourseteachers");
699         }
700         $admindata[]="<A HREF=\"teachers.php?id=$course->id\">$course->teachers...</A>";
701         $adminicon[]="<IMG SRC=\"$CFG->wwwroot/pix/i/settings.gif\" HEIGHT=16 WIDTH=16 ALT=\"\">";
702     
703         $admindata[]="<A HREF=\"grades.php?id=$course->id\">".get_string("grades")."...</A>";
704         $adminicon[]="<IMG SRC=\"$CFG->wwwroot/pix/i/grades.gif\" HEIGHT=16 WIDTH=16 ALT=\"\">";
705     
706         $admindata[]="<A HREF=\"log.php?id=$course->id\">".get_string("logs")."...</A>";
707         $adminicon[]="<IMG SRC=\"$CFG->wwwroot/pix/i/log.gif\" HEIGHT=16 WIDTH=16 ALT=\"\">";
708         $admindata[]="<A HREF=\"$CFG->wwwroot/files/index.php?id=$course->id\">".get_string("files")."...</A>";
709         $adminicon[]="<IMG SRC=\"$CFG->wwwroot/files/pix/files.gif\" HEIGHT=16 WIDTH=16 ALT=\"\">";
710     
711         $admindata[]="<A HREF=\"$CFG->wwwroot/doc/view.php?id=$course->id&file=teacher.html\">".get_string("help")."...</A>";
712         $adminicon[]="<IMG SRC=\"$CFG->wwwroot/mod/resource/icon.gif\" HEIGHT=16 WIDTH=16 ALT=\"\">";
714         if ($teacherforum = forum_get_course_forum($course->id, "teacher")) {
715             $admindata[]="<A HREF=\"$CFG->wwwroot/mod/forum/view.php?f=$teacherforum->id\">".get_string("nameteacher", "forum")."</A>";
716             $adminicon[]="<IMG SRC=\"$CFG->wwwroot/mod/forum/icon.gif\" HEIGHT=16 WIDTH=16 ALT=\"\">";
717         }
718     } else {
719         $admindata[]="<A HREF=\"grade.php?id=$course->id\">".get_string("grades")."...</A>";
720         $adminicon[]="<IMG SRC=\"$CFG->wwwroot/pix/i/grades.gif\" HEIGHT=16 WIDTH=16 ALT=\"\">";
721     } 
723     print_side_block(get_string("administration"), "", $admindata, $adminicon, "", $width);
726 function print_course_categories($categories, $selected="none", $width=180) {
727     global $CFG, $THEME, $USER;
728     
729     $strallowguests = get_string("allowguests");
730     $strrequireskey = get_string("requireskey");
732     if ($selected == "index") {  // Print comprehensive index of categories with courses
733         if ($courses = get_courses()) {
734             if (isset($USER->id) and !isadmin()) {
735                 print_simple_box_start("CENTER", "100%", $THEME->cellheading);
736                 print_heading("<A HREF=\"course/index.php?category=my\">".get_string("mycourses")."</A>", "LEFT");
737                 $some = false;
738                 echo "<UL>";
739                 foreach ($courses as $key => $course) {
740                     if (isteacher($course->id) or isstudent($course->id)) {
741                         echo "<A HREF=\"$CFG->wwwroot/course/view.php?id=$course->id\">$course->fullname</A>";
742                         echo "<BR>";
743                         $some = true;
744                     }
745                 }
746                 if (!$some) {
747                     print_string("nocoursesyet");
748                 }
749                 echo "</UL>";
750                 print_simple_box_end();
751                 print_spacer(8,1);
752             }
753             foreach ($categories as $category) {
754                 print_simple_box_start("CENTER", "100%");
755                 print_heading("<A HREF=\"course/index.php?category=$category->id\">$category->name</A>", "LEFT");
756                 $some = false;
757                 echo "<UL>";
758                 foreach ($courses as $key => $course) {
759                     if ($course->category == $category->id) {
760                         echo "<A HREF=\"$CFG->wwwroot/course/view.php?id=$course->id\">$course->fullname</A>";
761                         echo "&nbsp;&nbsp;";
762                         unset($courses[$key]);
763                         if ($course->guest ) {
764                             echo "<A TITLE=\"$strallowguests\" HREF=\"$CFG->wwwroot/course/view.php?id=$course->id\">";
765                             echo "<IMG ALT=\"\" HEIGHT=16 WIDTH=16 BORDER=0 SRC=\"$CFG->wwwroot/user/user.gif\"></A>";
766                         }
767                         if ($course->password) {
768                             echo "<A TITLE=\"$strrequireskey\" HREF=\"$CFG->wwwroot/course/view.php?id=$course->id\">";
769                             echo "<IMG ALT=\"\" HEIGHT=16 WIDTH=16 BORDER=0 SRC=\"$CFG->wwwroot/pix/i/key.gif\"></A>";
770                         }
771                         echo "<BR>";
772                         $some = true;
773                     }
774                 }
775                 if (!$some) {
776                     print_string("nocoursesyet");
777                 }
778                 echo "</UL>";
779                 print_simple_box_end();
780                 print_spacer(8,1);
781             }
782         }
784     } else {                    // Print short list of categories only 
785         foreach ($categories as $cat) {
786             $caticon[]="<IMG SRC=\"$CFG->wwwroot/pix/i/course.gif\" HEIGHT=16 WIDTH=16>";
787             if ($cat->id == $selected) {
788                 $catdata[]="$cat->name";
789             } else {
790                 $catdata[]="<A HREF=\"$CFG->wwwroot/course/index.php?category=$cat->id\">$cat->name</A>";
791             }
792         }
793         $catdata[] = "<A HREF=\"$CFG->wwwroot/course/index.php?category=all\">".get_string("fulllistofcourses")."</A>";
794         $caticon[] = "";
795         if (isset($USER->id)) {
796             $catdata[] = "<A HREF=\"$CFG->wwwroot/course/index.php?category=my\">".get_string("mycourses")."</A>";
797             $caticon[] = "";
798         }
799         print_side_block(get_string("categories"), "", $catdata, $caticon, "", $width);
800     }
803 function print_log_graph($course, $userid=0, $type="course.png", $date=0) {
804     global $CFG;
805     if (empty($CFG->gdversion)) {
806         echo "(".get_string("gdneed").")";
807     } else {
808         echo "<IMG BORDER=0 SRC=\"$CFG->wwwroot/course/loggraph.php?id=$course->id&user=$userid&type=$type&date=$date\">";
809     }
814 /// MODULE FUNCTIONS /////////////////////////////////////////////////////////////////
816 function add_course_module($mod) {
817     GLOBAL $db;
819     $mod->added = time();
821     return insert_record("course_modules", $mod);
824 function add_mod_to_section($mod) {
825 // Returns the course_sections ID where the mod is inserted
826     GLOBAL $db;
828     if ($section = get_record("course_sections", "course", "$mod->course", "section", "$mod->section")) {
829         if (!empty($section->sequence)) {
830             $newsequence = "$section->sequence,$mod->coursemodule";
831         } else {
832             $newsequence = "$mod->coursemodule";
833         }
834         if (set_field("course_sections", "sequence", $newsequence, "id", $section->id)) {
835             return $section->id;     // Return course_sections ID that was used.
836         } else {
837             return 0;
838         }
839        
840     } else {  // Insert a new record
841         $section->course = $mod->course;
842         $section->section = $mod->section;
843         $section->summary = "";
844         $section->sequence = $mod->coursemodule;
845         return insert_record("course_sections", $section);
846     }
849 function hide_course_module($mod) {
850     return set_field("course_modules", "visible", 0, "id", $mod);
853 function show_course_module($mod) {
854     return set_field("course_modules", "visible", 1, "id", $mod);
857 function delete_course_module($mod) {
858     return set_field("course_modules", "deleted", 1, "id", $mod);
861 function delete_mod_from_section($mod, $section) {
862     GLOBAL $db;
864     if ($section = get_record("course_sections", "id", "$section") ) {
866         $modarray = explode(",", $section->sequence);
868         if ($key = array_keys ($modarray, $mod)) {
869             array_splice($modarray, $key[0], 1);
870             $newsequence = implode(",", $modarray);
871             return set_field("course_sections", "sequence", $newsequence, "id", $section->id);
872         } else {
873             return false;
874         }
875        
876     } else {  
877         return false;
878     }
882 function move_module($cm, $move) {
883     GLOBAL $db;
885     if (!$move) {
886         return true;
887     }
889     if (! $thissection = get_record("course_sections", "id", $cm->section)) {
890         error("This course section doesn't exist");
891     }
893     $mods = explode(",", $thissection->sequence);
895     $len = count($mods);
896     $pos = array_keys($mods, $cm->id);
897     $thepos = $pos[0];
899     if ($len == 0 || count($pos) == 0 ) {
900         error("Very strange. Could not find the required module in this section.");
901     }
903     if ($len == 1) {
904         $first = true;
905         $last = true;
906     } else {
907         $first = ($thepos == 0);
908         $last  = ($thepos == $len - 1);
909     }
911     if ($move < 0) {    // Moving the module up
913         if ($first) {
914             if ($thissection->section == 1) {  // First section, do nothing
915                 return true;
916             } else {               // Push onto end of previous section
917                 $prevsectionnumber = $thissection->section - 1;
918                 if (! $prevsection = get_record("course_sections", "course", "$thissection->course", 
919                                                                    "section", "$prevsectionnumber")) {
920                     error("Previous section ($prevsection->id) doesn't exist");
921                 }
923                 if (!empty($prevsection->sequence)) {
924                     $newsequence = "$prevsection->sequence,$cm->id";
925                 } else {
926                     $newsequence = "$cm->id";
927                 }
929                 if (! set_field("course_sections", "sequence", $newsequence, "id", $prevsection->id)) {
930                     error("Previous section could not be updated");
931                 }
933                 if (! set_field("course_modules", "section", $prevsection->id, "id", $cm->id)) {
934                     error("Module could not be updated");
935                 }
937                 array_splice($mods, 0, 1);
938                 $newsequence = implode(",", $mods);
939                 if (! set_field("course_sections", "sequence", $newsequence, "id", $thissection->id)) {
940                     error("Module could not be updated");
941                 }
943                 return true;
945             }
946         } else {        // move up within this section
947             $swap = $mods[$thepos-1];
948             $mods[$thepos-1] = $mods[$thepos];
949             $mods[$thepos] = $swap;
950             
951             $newsequence = implode(",", $mods);
952             if (! set_field("course_sections", "sequence", $newsequence, "id", $thissection->id)) {
953                 error("This section could not be updated");
954             }
955             return true;
956         }
958     } else {            // Moving the module down
960         if ($last) {
961             $nextsectionnumber = $thissection->section + 1;
962             if ($nextsection = get_record("course_sections", "course", "$thissection->course", 
963                                                                "section", "$nextsectionnumber")) {
965                 if (!empty($nextsection->sequence)) {
966                     $newsequence = "$cm->id,$nextsection->sequence";
967                 } else {
968                     $newsequence = "$cm->id";
969                 }
971                 if (! set_field("course_sections", "sequence", $newsequence, "id", $nextsection->id)) {
972                     error("Next section could not be updated");
973                 }
975                 if (! set_field("course_modules", "section", $nextsection->id, "id", $cm->id)) {
976                     error("Module could not be updated");
977                 }
979                 array_splice($mods, $thepos, 1);
980                 $newsequence = implode(",", $mods);
981                 if (! set_field("course_sections", "sequence", $newsequence, "id", $thissection->id)) {
982                     error("This section could not be updated");
983                 }
984                 return true;
986             } else {        // There is no next section, so just return
987                 return true;
989             }
990         } else {      // move down within this section
991             $swap = $mods[$thepos+1];
992             $mods[$thepos+1] = $mods[$thepos];
993             $mods[$thepos] = $swap;
994             
995             $newsequence = implode(",", $mods);
996             if (! set_field("course_sections", "sequence", $newsequence, "id", $thissection->id)) {
997                 error("This section could not be updated");
998             }
999             return true;
1000         }
1001     }
1004 function make_editing_buttons($moduleid, $absolute=false, $visible=true, $str=NULL) {
1005     global $CFG;
1007     if (empty($str)) {
1008         $str->delete   = get_string("delete");
1009         $str->moveup   = get_string("moveup");
1010         $str->movedown = get_string("movedown");
1011         $str->update   = get_string("update");
1012         $str->hide     = get_string("hide");
1013         $str->show     = get_string("show");
1014     }
1016     if ($absolute) {
1017         $path = "$CFG->wwwroot/course/";
1018     } else {
1019         $path = "";
1020     }
1022     if ($visible) {
1023         $hideshow = " <A TITLE=\"$str->hide\" HREF=\"".$path."mod.php?hide=$moduleid\"><IMG 
1024                         SRC=".$path."../pix/t/hide.gif BORDER=0></A>";
1025     } else {
1026         $hideshow = " <A TITLE=\"$str->show\" HREF=\"".$path."mod.php?show=$moduleid\"><IMG 
1027                         SRC=".$path."../pix/t/show.gif BORDER=0></A>";
1028     }
1030     return "<A TITLE=\"$str->delete\" HREF=\"".$path."mod.php?delete=$moduleid\"><IMG 
1031              SRC=".$path."../pix/t/delete.gif BORDER=0></A>
1032           <A TITLE=\"$str->moveup\" HREF=\"".$path."mod.php?id=$moduleid&move=-1\"><IMG 
1033              SRC=".$path."../pix/t/up.gif BORDER=0></A>
1034           <A TITLE=\"$str->movedown\" HREF=\"".$path."mod.php?id=$moduleid&move=1\"><IMG 
1035              SRC=".$path."../pix/t/down.gif BORDER=0></A>
1036           <A TITLE=\"$str->update\" HREF=\"".$path."mod.php?update=$moduleid\"><IMG 
1037              SRC=".$path."../pix/t/edit.gif BORDER=0></A> $hideshow";
1040 ?>