2 // Library of useful functions
5 $COURSE_MAX_LOG_DISPLAY = 150; // days
7 $COURSE_TEACHER_COLOR = "#990000"; // To hilight certain items that are teacher-only
9 $COURSE_LIVELOG_REFRESH = 60; // Seconds
12 function print_log_selector_form($course, $selecteduser=0, $selecteddate="today") {
16 // Get all the possible users
19 if ($course->category
) {
20 if ($students = get_records_sql("SELECT u.* FROM user u, user_students s
21 WHERE s.course = '$course->id' AND s.user = u.id
22 ORDER BY u.lastaccess DESC")) {
23 foreach ($students as $student) {
24 $users["$student->id"] = "$student->firstname $student->lastname";
27 if ($teachers = get_records_sql("SELECT u.* FROM user u, user_teachers t
28 WHERE t.course = '$course->id' AND t.user = u.id
29 ORDER BY u.lastaccess DESC")) {
30 foreach ($teachers as $teacher) {
31 $users["$teacher->id"] = "$teacher->firstname $teacher->lastname";
37 if ($ccc = get_records_sql("SELECT * FROM course ORDER BY fullname")) {
38 foreach ($ccc as $cc) {
40 $courses["$cc->id"] = "$cc->fullname";
42 $courses["$cc->id"] = " $cc->fullname (Site)";
51 // Get all the possible dates
52 // Note that we are keeping track of real (GMT) time and user time
53 // User time is only used in displays - all calcs and passing is GMT
55 $timenow = time(); // GMT
57 // What day is it now for the user, and when is midnight that day (in GMT).
58 $timemidnight = $today = usergetmidnight($timenow);
60 // Put today up the top of the list
61 $dates = array("$timemidnight" => get_string("today").", ".userdate($timenow, "%e %B %Y") );
63 if (! $course->startdate
) {
64 $course->startdate
= $course->timecreated
;
68 while ($timemidnight > $course->startdate
and $numdates < 365) {
69 $timemidnight = $timemidnight - 86400;
70 $timenow = $timenow - 86400;
71 $dates["$timemidnight"] = userdate($timenow, "%A, %e %B %Y");
75 if ($selecteddate == "today") {
76 $selecteddate = $today;
80 echo "<FORM ACTION=log.php METHOD=get>";
82 choose_from_menu ($courses, "id", $course->id
, "");
84 echo "<INPUT TYPE=hidden NAME=id VALUE=\"$course->id\">";
86 if ($course->category
) {
87 choose_from_menu ($users, "user", $selecteduser, get_string("allparticipants") );
89 choose_from_menu ($dates, "date", $selecteddate, get_string("alldays"));
90 echo "<INPUT TYPE=submit VALUE=\"".get_string("showtheselogs")."\">";
95 function make_log_url($module, $url) {
103 return "/$module/$url";
106 return "/mod/$module/$url";
112 function print_log($course, $user=0, $date=0, $order="ORDER BY l.time ASC") {
113 // It is assumed that $date is the GMT time of midnight for that day,
114 // and so the next 86400 seconds worth of logs are printed.
116 if ($course->category
) {
117 $selector = "WHERE l.course='$course->id' AND l.user = u.id";
120 $selector = "WHERE l.user = u.id"; // Show all courses
121 if ($ccc = get_records_sql("SELECT * FROM course ORDER BY fullname")) {
122 foreach ($ccc as $cc) {
123 $courses[$cc->id
] = "$cc->shortname";
129 $selector .= " AND l.user = '$user'";
133 $enddate = $date +
86400;
134 $selector .= " AND l.time > '$date' AND l.time < '$enddate'";
137 if (!$logs = get_records_sql("SELECT l.*, u.firstname, u.lastname, u.picture
138 FROM log l, user u $selector $order")){
139 notify("No logs found!");
140 print_footer($course);
145 $tt = getdate(time());
146 $today = mktime (0, 0, 0, $tt["mon"], $tt["mday"], $tt["year"]);
147 echo "<P ALIGN=CENTER>Displaying ".count($logs)." records</P>";
148 echo "<TABLE BORDER=0 ALIGN=center CELLPADDING=3 CELLSPACING=3>";
149 foreach ($logs as $log) {
151 if ($ld = get_record_sql("SELECT * FROM log_display WHERE module='$log->module' AND action='$log->action'")) {
152 $log->info
= get_field($ld->mtable
, $ld->field
, "id", $log->info
);
156 if (! $course->category
) {
157 echo "<TD NOWRAP><FONT SIZE=2><A HREF=\"view.php?id=$log->course\">".$courses[$log->course
]."</A></TD>";
159 echo "<TD NOWRAP ALIGN=right><FONT SIZE=2>".userdate($log->time
, "%A")."</TD>";
160 echo "<TD NOWRAP><FONT SIZE=2>".userdate($log->time
, "%e %B %Y, %I:%M %p")."</TD>";
161 echo "<TD NOWRAP><FONT SIZE=2>$log->ip</TD>";
162 echo "<TD NOWRAP><FONT SIZE=2><A HREF=\"../user/view.php?id=$log->user&course=$log->course\"><B>$log->firstname $log->lastname</B></TD>";
163 echo "<TD NOWRAP><FONT SIZE=2>";
164 link_to_popup_window( make_log_url($log->module
,$log->url
), "fromloglive","$log->module $log->action", 400, 600);
166 echo "<TD NOWRAP><FONT SIZE=2>$log->info</TD>";
173 function print_all_courses($cat=1, $style="full", $maxcount=999) {
176 if ($courses = get_records("course", "category", $cat, "fullname ASC")) {
177 if ($style == "minimal") {
179 $icon = "<IMG SRC=\"pix/i/course.gif\" HEIGHT=16 WIDTH=16 ALT=\"".get_string("course")."\">";
180 foreach ($courses as $course) {
181 $moddata[]="<A TITLE=\"$course->shortname\" HREF=\"$CFG->wwwroot/course/view.php?id=$course->id\">$course->fullname</A>";
183 if ($count++
>= $maxcount) {
187 $fulllist = "<P><A HREF=\"$CFG->wwwroot/course/\">".get_string("fulllistofcourses")."</A>...";
188 print_side_block("", $moddata, "$fulllist", $modicon);
191 foreach ($courses as $course) {
192 print_course($course);
198 echo "<H3>No courses have been defined yet</H3>";
203 function print_course($course) {
207 if (! $site = get_site()) {
208 error("Could not find a site!");
211 print_simple_box_start("CENTER", "100%");
213 echo "<TABLE WIDTH=100%>";
214 echo "<TR VALIGN=top>";
215 echo "<TD VALIGN=top WIDTH=50%>";
216 echo "<P><FONT SIZE=3><B><A TITLE=\"".get_string("entercourse")."\"
217 HREF=\"$CFG->wwwroot/course/view.php?id=$course->id\">$course->fullname</A></B></FONT></P>";
218 if ($teachers = get_records_sql("SELECT u.* FROM user u, user_teachers t
219 WHERE u.id = t.user AND t.course = '$course->id'
220 ORDER BY t.authority ASC")) {
222 echo "<P><FONT SIZE=1>\n";
223 foreach ($teachers as $teacher) {
224 echo "$course->teacher: <A HREF=\"$CFG->wwwroot/user/view.php?id=$teacher->id&course=$site->id\">$teacher->firstname $teacher->lastname</A><BR>";
228 if ($course->guest
or ($course->password
== "")) {
229 echo "<A TITLE=\"".get_string("allowguests")."\" HREF=\"$CFG->wwwroot/course/view.php?id=$course->id\">";
230 echo "<IMG VSPACE=4 ALT=\"\" HEIGHT=16 WIDTH=16 BORDER=0 SRC=\"$CFG->wwwroot/user/user.gif\"></A> ";
232 if ($course->password
) {
233 echo "<A TITLE=\"".get_string("requireskey")."\" HREF=\"$CFG->wwwroot/course/view.php?id=$course->id\">";
234 echo "<IMG VSPACE=4 ALT=\"\" HEIGHT=16 WIDTH=16 BORDER=0 SRC=\"$CFG->wwwroot/pix/i/key.gif\"></A>";
238 echo "</TD><TD VALIGN=top WIDTH=50%>";
239 echo "<P><FONT SIZE=2>".text_to_html($course->summary
)."</FONT></P>";
243 print_simple_box_end();
246 function print_headline($text, $size=2) {
247 echo "<B><FONT SIZE=\"$size\">$text</FONT></B><BR>\n";
250 function print_recent_activity($course) {
251 // $course is an object
252 // This function trawls through the logs looking for
253 // anything new since the user's last login
255 global $CFG, $USER, $COURSE_TEACHER_COLOR;
257 if (! $USER->lastlogin
) {
258 echo "<P ALIGN=CENTER><FONT SIZE=1>";
259 print_string("welcometocourse", "", $course->shortname
);
263 echo "<P ALIGN=CENTER><FONT SIZE=1>";
264 echo get_string("yourlastlogin").":<BR>";
265 echo userdate($USER->lastlogin
, "%A, %e %b %Y, %H:%M");
269 if (! $logs = get_records_sql("SELECT * FROM log WHERE time > '$USER->lastlogin' AND course = '$course->id' ORDER BY time ASC")) {
274 // Firstly, have there been any new enrolments?
278 foreach ($logs as $log) {
279 if ($log->module
== "course" and $log->action
== "enrol") {
281 print_headline(get_string("newusers").":");
285 $user = get_record("user", "id", $log->info
);
286 echo "<P><FONT SIZE=1><A HREF=\"../user/view.php?id=$user->id&course=$course->id\">$user->firstname $user->lastname</A></FONT></P>";
290 // Next, have there been any changes to the course structure?
292 foreach ($logs as $log) {
293 if ($log->module
== "course") {
294 if ($log->action
== "add mod" or $log->action
== "update mod" or $log->action
== "delete mod") {
295 $info = split(" ", $log->info
);
296 $modname = get_field($info[0], "name", "id", $info[1]);
298 switch ($log->action
) {
300 $stradded = get_string("added", "moodle", get_string("modulename", $info[0]));
301 $changelist["$log->info"] = array ("operation" => "add", "text" => "$stradded:<BR><A HREF=\"$CFG->wwwroot/course/$log->url\">$modname</A>");
304 $strupdated = get_string("updated", "moodle", get_string("modulename", $info[0]));
305 if (! $changelist["$log->info"]) {
306 $changelist["$log->info"] = array ("operation" => "update", "text" => "$strupdated:<BR><A HREF=\"$CFG->wwwroot/course/$log->url\">$modname</A>");
310 if ($changelist["$log->info"]["operation"] == "add") {
311 $changelist["$log->info"] = NULL;
313 $strdeleted = get_string("deletedactivity", "moodle", get_string("modulename", $info[0]));
314 $changelist["$log->info"] = array ("operation" => "delete", "text" => $strdeleted);
323 foreach ($changelist as $changeinfo => $change) {
325 $changes[$changeinfo] = $change;
328 if (count($changes) > 0) {
329 print_headline(get_string("courseupdates").":");
331 foreach ($changes as $changeinfo => $change) {
332 echo "<P><FONT SIZE=1>".$change["text"]."</FONT></P>";
338 // Now all we need to know are the new posts.
341 foreach ($logs as $log) {
343 if ($log->module
== "forum") {
346 if ($log->action
== "add post") {
347 $post = get_record_sql("SELECT p.*, d.forum, u.firstname, u.lastname,
348 u.email, u.picture, u.id as userid
349 FROM forum_discussions d, forum_posts p, user u
350 WHERE p.id = '$log->info' AND d.id = p.discussion AND p.user = u.id");
352 } else if ($log->action
== "add discussion") {
353 $post = get_record_sql("SELECT p.*, d.forum, u.firstname, u.lastname,
354 u.email, u.picture, u.id as userid
355 FROM forum_discussions d, forum_posts p, user u
356 WHERE d.id = '$log->info' AND d.firstpost = p.id AND p.user = u.id");
362 if ($forum = get_record("forum", "id", $post->forum
) ) {
363 if ($forum->type
== "teacher") {
364 if (!isteacher($course->id
)) {
367 $teacherpost = "COLOR=$COURSE_TEACHER_COLOR";
372 print_headline(get_string("newforumposts").":");
376 $date = userdate($post->modified
, "%e %b, %H:%M");
377 echo "<P><FONT SIZE=1 $teacherpost>$date - $post->firstname $post->lastname<BR>";
378 echo "\"<A HREF=\"$CFG->wwwroot/mod/forum/$log->url\">";
379 if ($log->action
== "add") {
380 echo "<B>$post->subject</B>";
382 echo "$post->subject";
384 echo "</A>\"</FONT></P>";
391 echo "<FONT SIZE=2>".get_string("nothingnew")."</FONT>";
396 function unenrol_student_in_course($user, $course) {
399 return $db->Execute("DELETE FROM user_students WHERE user = '$user' AND course = '$course'");
404 function enrol_student_in_course($user, $course) {
409 $rs = $db->Execute("INSERT INTO user_students (user, course, start, end, time)
410 VALUES ($user, $course, 0, 0, $timenow)");
418 function get_all_mods($courseid, &$mods, &$modnames, &$modnamesplural, &$modnamesused) {
419 // Returns a number of useful structures for course displays
421 $mods = NULL; // course modules indexed by id
422 $modnames = NULL; // all course module names
423 $modnamesplural= NULL; // all course module names (plural form)
424 $modnamesused = NULL; // course module names used
426 if ($allmods = get_records_sql("SELECT * FROM modules") ) {
427 foreach ($allmods as $mod) {
428 $modnames[$mod->name
] = get_string("modulename", "$mod->name");
429 $modnamesplural[$mod->name
] = get_string("modulenameplural", "$mod->name");
433 error("No modules are installed!");
436 if ($rawmods = get_records_sql("SELECT cm.*, m.name as modname
437 FROM modules m, course_modules cm
438 WHERE cm.course = '$courseid'
440 AND cm.module = m.id ") ) {
441 foreach($rawmods as $mod) { // Index the mods
442 $mods[$mod->id
] = $mod;
443 $mods[$mod->id
]->modfullname
= $modnames[$mod->modname
];
444 $modnamesused[$mod->modname
] = $modnames[$mod->modname
];
446 asort($modnamesused);
450 function get_all_sections($courseid) {
452 return get_records_sql("SELECT section, id, course, summary, sequence
454 WHERE course = '$courseid'
458 function print_section($courseid, $section, $mods, $modnamesused, $absolute=false) {
462 echo "<TABLE WIDTH=100%><TR><TD>\n";
463 if ($section->sequence
) {
465 $sectionmods = explode(",", $section->sequence
);
467 foreach ($sectionmods as $modnumber) {
468 $mod = $mods[$modnumber];
469 $instancename = get_field("$mod->modname", "name", "id", "$mod->instance");
470 echo "<IMG SRC=\"$CFG->wwwroot/mod/$mod->modname/icon.gif\" HEIGHT=16 WIDTH=16 ALT=\"$mod->modfullname\">";
471 echo " <FONT SIZE=2><A TITLE=\"$mod->modfullname\"";
472 echo " HREF=\"$CFG->wwwroot/mod/$mod->modname/view.php?id=$mod->id\">$instancename</A></FONT>";
473 if (isediting($courseid)) {
474 echo make_editing_buttons($mod->id
, $absolute);
479 echo "</TD></TR></TABLE><BR>\n\n";
482 function print_side_block($heading="", $list=NULL, $footer="", $icons=NULL) {
484 echo "<TABLE WIDTH=100%>\n";
485 echo "<TR><TD COLSPAN=2><P><B><FONT SIZE=2>$heading</TD></TR>\n";
487 foreach($list as $key => $string) {
488 echo "<TR><TD VALIGN=top WIDTH=12>";
494 echo "</TD>\n<TD WIDTH=100% VALIGN=top>";
495 echo "<P><FONT SIZE=2>$string</FONT></P>";
500 echo "<TR><TD></TD><TD ALIGN=left><P><FONT SIZE=2>$footer</TD></TR>\n";
502 echo "</TABLE><BR>\n\n";
505 function print_admin_links ($siteid) {
508 print_simple_box(get_string("administration"), $align="CENTER", $width="100%", $color="$THEME->cellheading");
509 $icon = "<IMG SRC=\"$CFG->wwwroot/pix/i/settings.gif\" HEIGHT=16 WIDTH=16 ALT=\"\">";
510 $moddata[]="<A HREF=\"$CFG->wwwroot/course/log.php?id=$siteid\">".get_string("sitelogs")."</A>";
512 $moddata[]="<A HREF=\"$CFG->wwwroot/admin/site.php\">".get_string("sitesettings")."</A>";
514 $moddata[]="<A HREF=\"$CFG->wwwroot/course/edit.php\">".get_string("addnewcourse")."</A>";
516 $moddata[]="<A HREF=\"$CFG->wwwroot/course/teacher.php\">".get_string("assignteachers")."</A>";
518 $moddata[]="<A HREF=\"$CFG->wwwroot/course/delete.php\">".get_string("deletecourse")."</A>";
520 $moddata[]="<A HREF=\"$CFG->wwwroot/admin/user.php?newuser=true\">".get_string("addnewuser")."</A>";
522 $moddata[]="<A HREF=\"$CFG->wwwroot/admin/user.php\">".get_string("edituser")."</A>";
524 $fulladmin = "<P><A HREF=\"$CFG->wwwroot/admin/\">".get_string("admin")."</A>...";
525 print_side_block("", $moddata, "$fulladmin", $modicon);
526 echo "<IMG SRC=\"$CFG->wwwroot/pix/spacer.gif\" WIDTH=200 HEIGHT=0><BR>";
531 function print_log_graph($course, $userid=0, $type="course.png", $date=0) {
533 echo "<IMG BORDER=0 SRC=\"$CFG->wwwroot/course/loggraph.php?id=$course->id&user=$userid&type=$type&date=$date\">";
538 /// MODULE FUNCTIONS /////////////////////////////////////////////////////////////////
540 function add_course_module($mod) {
545 if (!$rs = $db->Execute("INSERT into course_modules
546 SET course = '$mod->course',
547 module = '$mod->module',
548 instance = '$mod->instance',
549 section = '$mod->section',
550 added = '$timenow' ")) {
554 // Get it out again - this is the most compatible way to determine the ID
555 if ($rs = $db->Execute("SELECT id FROM course_modules
556 WHERE module = $mod->module AND added = $timenow")) {
557 return $rs->fields
[0];
564 function add_mod_to_section($mod) {
565 // Returns the course_sections ID where the mod is inserted
568 if ($cw = get_record_sql("SELECT * FROM course_sections
569 WHERE course = '$mod->course' AND section = '$mod->section'") ) {
572 $newsequence = "$cw->sequence,$mod->coursemodule";
574 $newsequence = "$mod->coursemodule";
576 if (!$rs = $db->Execute("UPDATE course_sections SET sequence = '$newsequence' WHERE id = '$cw->id'")) {
579 return $cw->id
; // Return course_sections ID that was used.
582 } else { // Insert a new record
583 if (!$rs = $db->Execute("INSERT into course_sections
584 SET course = '$mod->course',
585 section = '$mod->section',
587 sequence = '$mod->coursemodule' ")) {
590 // Get it out again - this is the most compatible way to determine the ID
591 if ($rs = $db->Execute("SELECT id FROM course_sections
592 WHERE course = '$mod->course' AND section = '$mod->section'")) {
593 return $rs->fields
[0];
600 function delete_course_module($mod) {
601 return set_field("course_modules", "deleted", 1, "id", $mod);
604 function delete_mod_from_section($mod, $section) {
607 if ($cw = get_record("course_sections", "id", "$section") ) {
609 $modarray = explode(",", $cw->sequence
);
611 if ($key = array_keys ($modarray, $mod)) {
612 array_splice($modarray, $key[0], 1);
613 $newsequence = implode(",", $modarray);
614 return set_field("course_sections", "sequence", $newsequence, "id", $cw->id
);
625 function move_module($id, $move) {
632 if (! $cm = get_record("course_modules", "id", $id)) {
633 error("This course module doesn't exist");
636 if (! $thissection = get_record("course_sections", "id", $cm->section
)) {
637 error("This course section doesn't exist");
640 $mods = explode(",", $thissection->sequence
);
643 $pos = array_keys($mods, $cm->id
);
646 if ($len == 0 ||
count($pos) == 0 ) {
647 error("Very strange. Could not find the required module in this section.");
654 $first = ($thepos == 0);
655 $last = ($thepos == $len - 1);
658 if ($move < 0) { // Moving the module up
661 if ($thissection->section
== 1) { // First section, do nothing
663 } else { // Push onto end of previous section
664 $prevsectionnumber = $thissection->section
- 1;
665 if (! $prevsection = get_record_sql("SELECT * FROM course_sections
666 WHERE course='$thissection->course'
667 AND section='$prevsectionnumber' ")) {
668 error("Previous section ($prevsection->id) doesn't exist");
671 if ($prevsection->sequence
) {
672 $newsequence = "$prevsection->sequence,$cm->id";
674 $newsequence = "$cm->id";
677 if (! set_field("course_sections", "sequence", $newsequence, "id", $prevsection->id
)) {
678 error("Previous section could not be updated");
681 if (! set_field("course_modules", "section", $prevsection->id
, "id", $cm->id
)) {
682 error("Module could not be updated");
685 array_splice($mods, 0, 1);
686 $newsequence = implode(",", $mods);
687 if (! set_field("course_sections", "sequence", $newsequence, "id", $thissection->id
)) {
688 error("Module could not be updated");
694 } else { // move up within this section
695 $swap = $mods[$thepos-1];
696 $mods[$thepos-1] = $mods[$thepos];
697 $mods[$thepos] = $swap;
699 $newsequence = implode(",", $mods);
700 if (! set_field("course_sections", "sequence", $newsequence, "id", $thissection->id
)) {
701 error("This section could not be updated");
706 } else { // Moving the module down
709 $nextsectionnumber = $thissection->section +
1;
710 if ($nextsection = get_record_sql("SELECT * FROM course_sections
711 WHERE course='$thissection->course'
712 AND section='$nextsectionnumber' ")) {
714 if ($nextsection->sequence
) {
715 $newsequence = "$cm->id,$nextsection->sequence";
717 $newsequence = "$cm->id";
720 if (! set_field("course_sections", "sequence", $newsequence, "id", $nextsection->id
)) {
721 error("Next section could not be updated");
724 if (! set_field("course_modules", "section", $nextsection->id
, "id", $cm->id
)) {
725 error("Module could not be updated");
728 array_splice($mods, $thepos, 1);
729 $newsequence = implode(",", $mods);
730 if (! set_field("course_sections", "sequence", $newsequence, "id", $thissection->id
)) {
731 error("This section could not be updated");
735 } else { // There is no next section, so just return
739 } else { // move down within this section
740 $swap = $mods[$thepos+
1];
741 $mods[$thepos+
1] = $mods[$thepos];
742 $mods[$thepos] = $swap;
744 $newsequence = implode(",", $mods);
745 if (! set_field("course_sections", "sequence", $newsequence, "id", $thissection->id
)) {
746 error("This section could not be updated");
753 function make_editing_buttons($moduleid, $absolute=false) {
756 $delete = get_string("delete");
757 $moveup = get_string("moveup");
758 $movedown = get_string("movedown");
759 $update = get_string("update");
762 $path = "$CFG->wwwroot/course/";
766 return "
767 <A TITLE=\"$delete\" HREF=\"".$path."mod.php?delete=$moduleid\"><IMG
768 SRC=".$path."../pix/t/delete.gif BORDER=0></A>
769 <A TITLE=\"$moveup\" HREF=\"".$path."mod.php?id=$moduleid&move=-1\"><IMG
770 SRC=".$path."../pix/t/up.gif BORDER=0></A>
771 <A TITLE=\"$movedown\" HREF=\"".$path."mod.php?id=$moduleid&move=1\"><IMG
772 SRC=".$path."../pix/t/down.gif BORDER=0></A>
773 <A TITLE=\"$update\" HREF=\"".$path."mod.php?update=$moduleid\"><IMG
774 SRC=".$path."../pix/t/edit.gif BORDER=0></A>";