More checks to solve support requests before they happen.
[moodle.git] / course / lib.php
CommitLineData
f9903ed0 1<? // $Id$
97c270e9 2 // Library of useful functions
f9903ed0 3
f9903ed0 4
97c270e9 5$COURSE_MAX_LOG_DISPLAY = 150; // days
d9d1c35d 6
1c0200e0 7$COURSE_MAX_LOGS_PER_PAGE = 1000; // records
8
97c270e9 9$COURSE_TEACHER_COLOR = "#990000"; // To hilight certain items that are teacher-only
87ffed0d 10
97c270e9 11$COURSE_LIVELOG_REFRESH = 60; // Seconds
ef58b822 12
f9903ed0 13
f9903ed0 14function print_log_selector_form($course, $selecteduser=0, $selecteddate="today") {
15
9481285b 16 global $USER, $CFG;
17
f9903ed0 18 // Get all the possible users
19 $users = array();
720a43ce 20
21 if ($course->category) {
30d6173e 22 if ($courseusers = get_course_users($course->id, "u.lastaccess DESC")) {
23 foreach ($courseusers as $courseuser) {
24 $users[$courseuser->id] = "$courseuser->firstname $courseuser->lastname";
25 }
f9903ed0 26 }
9fa49e22 27 if ($guest = get_guest()) {
30d6173e 28 $users[$guest->id] = "$guest->firstname $guest->lastname";
122cffc9 29 }
f9903ed0 30 }
720a43ce 31
32 if (isadmin()) {
9fa49e22 33 if ($ccc = get_records("course", "", "", "fullname")) {
720a43ce 34 foreach ($ccc as $cc) {
cfa5d3f2 35 if ($cc->category) {
36 $courses["$cc->id"] = "$cc->fullname";
37 } else {
38 $courses["$cc->id"] = " $cc->fullname (Site)";
39 }
720a43ce 40 }
f9903ed0 41 }
cfa5d3f2 42 asort($courses);
f9903ed0 43 }
44
45 asort($users);
46
47 // Get all the possible dates
9481285b 48 // Note that we are keeping track of real (GMT) time and user time
49 // User time is only used in displays - all calcs and passing is GMT
50
51 $timenow = time(); // GMT
52
53 // What day is it now for the user, and when is midnight that day (in GMT).
9604ccb1 54 $timemidnight = $today = usergetmidnight($timenow);
9481285b 55
56 // Put today up the top of the list
62c13a2f 57 $dates = array("$timemidnight" => get_string("today").", ".userdate($timenow, "%d %B %Y") );
9481285b 58
59 if (! $course->startdate) {
60 $course->startdate = $course->timecreated;
61 }
f9903ed0 62
9481285b 63 $numdates = 1;
64 while ($timemidnight > $course->startdate and $numdates < 365) {
f9903ed0 65 $timemidnight = $timemidnight - 86400;
9481285b 66 $timenow = $timenow - 86400;
62c13a2f 67 $dates["$timemidnight"] = userdate($timenow, "%A, %d %B %Y");
9481285b 68 $numdates++;
f9903ed0 69 }
70
71 if ($selecteddate == "today") {
72 $selecteddate = $today;
73 }
74
75 echo "<CENTER>";
76 echo "<FORM ACTION=log.php METHOD=get>";
720a43ce 77 if (isadmin()) {
849bc02a 78 choose_from_menu ($courses, "id", $course->id, "");
720a43ce 79 } else {
80 echo "<INPUT TYPE=hidden NAME=id VALUE=\"$course->id\">";
81 }
82 if ($course->category) {
97c270e9 83 choose_from_menu ($users, "user", $selecteduser, get_string("allparticipants") );
720a43ce 84 }
97c270e9 85 choose_from_menu ($dates, "date", $selecteddate, get_string("alldays"));
86 echo "<INPUT TYPE=submit VALUE=\"".get_string("showtheselogs")."\">";
f9903ed0 87 echo "</FORM>";
88 echo "</CENTER>";
89}
90
600149be 91function make_log_url($module, $url) {
92 switch ($module) {
93 case "course":
94 case "user":
95 case "file":
96 case "login":
97 case "lib":
98 case "admin":
99 return "/$module/$url";
100 break;
101 default:
102 return "/mod/$module/$url";
103 break;
104 }
105}
106
107
f9903ed0 108function print_log($course, $user=0, $date=0, $order="ORDER BY l.time ASC") {
9481285b 109// It is assumed that $date is the GMT time of midnight for that day,
110// and so the next 86400 seconds worth of logs are printed.
f9903ed0 111
1c0200e0 112 global $CFG, $COURSE_MAX_LOGS_PER_PAGE;
47f1da80 113
720a43ce 114 if ($course->category) {
ebc3bd2b 115 $selector = "WHERE l.course='$course->id' AND l.userid = u.id";
a2ab3b05 116
720a43ce 117 } else {
ebc3bd2b 118 $selector = "WHERE l.userid = u.id"; // Show all courses
9fa49e22 119 if ($ccc = get_courses(-1)) {
720a43ce 120 foreach ($ccc as $cc) {
121 $courses[$cc->id] = "$cc->shortname";
122 }
123 }
124 }
f9903ed0 125
126 if ($user) {
ebc3bd2b 127 $selector .= " AND l.userid = '$user'";
f9903ed0 128 }
129
130 if ($date) {
131 $enddate = $date + 86400;
132 $selector .= " AND l.time > '$date' AND l.time < '$enddate'";
133 }
134
6aeec7f8 135 if (!$logs = get_logs($selector, $order)) {
f9903ed0 136 notify("No logs found!");
137 print_footer($course);
138 exit;
139 }
140
141 $count=0;
142 $tt = getdate(time());
143 $today = mktime (0, 0, 0, $tt["mon"], $tt["mday"], $tt["year"]);
1c0200e0 144 if (($totalcountlogs = count($logs)) > $COURSE_MAX_LOGS_PER_PAGE) {
145 $totalcountlogs = "$COURSE_MAX_LOGS_PER_PAGE/$totalcountlogs";
146 }
147
9fa49e22 148 echo "<P ALIGN=CENTER>";
1c0200e0 149 print_string("displayingrecords", "", $totalcountlogs);
9fa49e22 150 echo "</P>";
1c0200e0 151
152 $countlogs = 0;
f9903ed0 153 echo "<TABLE BORDER=0 ALIGN=center CELLPADDING=3 CELLSPACING=3>";
154 foreach ($logs as $log) {
600149be 155
1c0200e0 156 $countlogs++;
157
158 if ($countlogs > $COURSE_MAX_LOGS_PER_PAGE) {
159 break;
160 }
161
9fa49e22 162 if ($ld = get_record("log_display", "module", "$log->module", "action", "$log->action")) {
565f7a95 163 $log->info = get_field($ld->mtable, $ld->field, "id", $log->info);
600149be 164 }
165
f58d18bc 166 echo "<TR NOWRAP>";
720a43ce 167 if (! $course->category) {
f58d18bc 168 echo "<TD NOWRAP><FONT SIZE=2><A HREF=\"view.php?id=$log->course\">".$courses[$log->course]."</A></TD>";
720a43ce 169 }
f58d18bc 170 echo "<TD NOWRAP ALIGN=right><FONT SIZE=2>".userdate($log->time, "%A")."</TD>";
62c13a2f 171 echo "<TD NOWRAP><FONT SIZE=2>".userdate($log->time, "%d %B %Y, %I:%M %p")."</TD>";
ebea4e27 172 echo "<TD NOWRAP><FONT SIZE=2>";
47f1da80 173 link_to_popup_window("/lib/ipatlas/plot.php?address=$log->ip&user=$log->userid", "ipatlas","$log->ip", 400, 700);
ebea4e27 174 echo "</TD>";
ebc3bd2b 175 echo "<TD NOWRAP><FONT SIZE=2><A HREF=\"../user/view.php?id=$log->userid&course=$log->course\"><B>$log->firstname $log->lastname</B></TD>";
f58d18bc 176 echo "<TD NOWRAP><FONT SIZE=2>";
600149be 177 link_to_popup_window( make_log_url($log->module,$log->url), "fromloglive","$log->module $log->action", 400, 600);
f9903ed0 178 echo "</TD>";
f58d18bc 179 echo "<TD NOWRAP><FONT SIZE=2>$log->info</TD>";
f9903ed0 180 echo "</TR>";
181 }
182 echo "</TABLE>";
183}
184
185
776dc270 186function print_all_courses($category="all", $style="full", $maxcount=999, $width=180) {
0a263205 187 global $CFG, $USER;
d887b5a7 188
ba2e5d73 189 if ($category == "all") {
9fa49e22 190 $courses = get_courses();
0a263205 191
192 } else if ($category == "my") {
193 if (isset($USER->id)) {
9fa49e22 194 if ($courses = get_courses()) {
0a263205 195 foreach ($courses as $key => $course) {
196 if (!isteacher($course->id) and !isstudent($course->id)) {
197 unset($courses[$key]);
198 }
199 }
200 }
201 }
202
ba2e5d73 203 } else {
9fa49e22 204 $courses = get_courses($category);
ba2e5d73 205 }
206
393cc508 207 if ($style == "minimal") {
208 $count = 0;
209 $icon = "<IMG SRC=\"pix/i/course.gif\" HEIGHT=16 WIDTH=16 ALT=\"".get_string("course")."\">";
210 if ($courses) {
94361e02 211 foreach ($courses as $course) {
565f7a95 212 $moddata[]="<A TITLE=\"$course->shortname\" HREF=\"$CFG->wwwroot/course/view.php?id=$course->id\">$course->fullname</A>";
94361e02 213 $modicon[]=$icon;
214 if ($count++ >= $maxcount) {
215 break;
216 }
217 }
218 $fulllist = "<P><A HREF=\"$CFG->wwwroot/course/\">".get_string("fulllistofcourses")."</A>...";
94361e02 219 } else {
393cc508 220 $moddata = array();
221 $modicon = array();
222 $fulllist = get_string("nocoursesyet");
223 }
224 print_side_block(get_string("courses"), "", $moddata, $modicon, $fulllist, $width);
225
226 } else if ($courses) {
227 foreach ($courses as $course) {
228 print_course($course);
229 echo "<BR>\n";
d887b5a7 230 }
231
232 } else {
393cc508 233 echo "<P>".get_string("nocoursesyet")."</P>";
d887b5a7 234 }
235}
236
237
f9903ed0 238function print_course($course) {
239
d887b5a7 240 global $CFG;
241
a83fded1 242 if (! $site = get_site()) {
f9903ed0 243 error("Could not find a site!");
244 }
245
d887b5a7 246 print_simple_box_start("CENTER", "100%");
f9903ed0 247
248 echo "<TABLE WIDTH=100%>";
da5c172a 249 echo "<TR VALIGN=top>";
250 echo "<TD VALIGN=top WIDTH=50%>";
7a302afc 251 echo "<P><FONT SIZE=3><B><A TITLE=\"".get_string("entercourse")."\"
9481285b 252 HREF=\"$CFG->wwwroot/course/view.php?id=$course->id\">$course->fullname</A></B></FONT></P>";
0c71c9ae 253 if ($teachers = get_course_teachers($course->id)) {
f9903ed0 254 echo "<P><FONT SIZE=1>\n";
255 foreach ($teachers as $teacher) {
0c71c9ae 256 if ($teacher->authority > 0) {
b4d7002e 257 if (!$teacher->role) {
258 $teacher->role = $course->teacher;
259 }
260 echo "$teacher->role: <A HREF=\"$CFG->wwwroot/user/view.php?id=$teacher->id&course=$site->id\">$teacher->firstname $teacher->lastname</A><BR>";
0c71c9ae 261 }
f9903ed0 262 }
263 echo "</FONT></P>";
da5c172a 264 }
f25f1e1b 265 if ($course->guest) {
5e367a2d 266 $strallowguests = get_string("allowguests");
267 echo "<A TITLE=\"$strallowguests\" HREF=\"$CFG->wwwroot/course/view.php?id=$course->id\">";
268 echo "<IMG VSPACE=4 ALT=\"$strallowguests\" HEIGHT=16 WIDTH=16 BORDER=0 SRC=\"$CFG->wwwroot/user/user.gif\"></A>&nbsp;&nbsp;";
da5c172a 269 }
270 if ($course->password) {
5e367a2d 271 $strrequireskey = get_string("requireskey");
272 echo "<A TITLE=\"$strrequireskey\" HREF=\"$CFG->wwwroot/course/view.php?id=$course->id\">";
273 echo "<IMG VSPACE=4 ALT=\"$strrequireskey\" HEIGHT=16 WIDTH=16 BORDER=0 SRC=\"$CFG->wwwroot/pix/i/key.gif\"></A>";
da5c172a 274 }
275
276
277 echo "</TD><TD VALIGN=top WIDTH=50%>";
278 echo "<P><FONT SIZE=2>".text_to_html($course->summary)."</FONT></P>";
279 echo "</TD></TR>";
280 echo "</TABLE>";
f9903ed0 281
da5c172a 282 print_simple_box_end();
f9903ed0 283}
284
600149be 285function print_headline($text, $size=2) {
286 echo "<B><FONT SIZE=\"$size\">$text</FONT></B><BR>\n";
287}
288
289function print_recent_activity($course) {
290 // $course is an object
291 // This function trawls through the logs looking for
292 // anything new since the user's last login
293
97c270e9 294 global $CFG, $USER, $COURSE_TEACHER_COLOR;
600149be 295
296 if (! $USER->lastlogin ) {
4c654ee3 297 echo "<P ALIGN=CENTER><FONT SIZE=1>";
4b1371a7 298 print_string("welcometocourse", "", $course->shortname);
4c654ee3 299 echo "</FONT></P>";
600149be 300 return;
4c654ee3 301 } else {
302 echo "<P ALIGN=CENTER><FONT SIZE=1>";
303 echo get_string("yourlastlogin").":<BR>";
62c13a2f 304 echo userdate($USER->lastlogin, "%A, %d %b %Y, %H:%M");
4c654ee3 305 echo "</FONT></P>";
600149be 306 }
307
9fa49e22 308 if (! $logs = get_records_select("log", "time > '$USER->lastlogin' AND course = '$course->id'", "time ASC")) {
600149be 309 return;
310 }
311
312
313 // Firstly, have there been any new enrolments?
314
315 $heading = false;
316 $content = false;
317 foreach ($logs as $log) {
318 if ($log->module == "course" and $log->action == "enrol") {
319 if (! $heading) {
4c654ee3 320 print_headline(get_string("newusers").":");
600149be 321 $heading = true;
322 $content = true;
323 }
324 $user = get_record("user", "id", $log->info);
d578afc8 325 if (isstudent($course->id, $user->id)) {
326 echo "<P><FONT SIZE=1><A HREF=\"../user/view.php?id=$user->id&course=$course->id\">$user->firstname $user->lastname</A></FONT></P>";
327 }
600149be 328 }
329 }
330
331 // Next, have there been any changes to the course structure?
332
600149be 333 foreach ($logs as $log) {
334 if ($log->module == "course") {
335 if ($log->action == "add mod" or $log->action == "update mod" or $log->action == "delete mod") {
600149be 336 $info = split(" ", $log->info);
337 $modname = get_field($info[0], "name", "id", $info[1]);
338
600149be 339 switch ($log->action) {
340 case "add mod":
27038d9f 341 $stradded = get_string("added", "moodle", get_string("modulename", $info[0]));
4c654ee3 342 $changelist["$log->info"] = array ("operation" => "add", "text" => "$stradded:<BR><A HREF=\"$CFG->wwwroot/course/$log->url\">$modname</A>");
600149be 343 break;
344 case "update mod":
27038d9f 345 $strupdated = get_string("updated", "moodle", get_string("modulename", $info[0]));
47f1da80 346 if (empty($changelist["$log->info"])) {
4c654ee3 347 $changelist["$log->info"] = array ("operation" => "update", "text" => "$strupdated:<BR><A HREF=\"$CFG->wwwroot/course/$log->url\">$modname</A>");
ef25340c 348 }
600149be 349 break;
350 case "delete mod":
ef25340c 351 if ($changelist["$log->info"]["operation"] == "add") {
352 $changelist["$log->info"] = NULL;
353 } else {
27038d9f 354 $strdeleted = get_string("deletedactivity", "moodle", get_string("modulename", $info[0]));
4c654ee3 355 $changelist["$log->info"] = array ("operation" => "delete", "text" => $strdeleted);
ef25340c 356 }
600149be 357 break;
358 }
ef25340c 359 }
360 }
361 }
362
9c9f7d77 363 if (!empty($changelist)) {
ef25340c 364 foreach ($changelist as $changeinfo => $change) {
365 if ($change) {
366 $changes[$changeinfo] = $change;
367 }
368 }
369 if (count($changes) > 0) {
4c654ee3 370 print_headline(get_string("courseupdates").":");
ef25340c 371 $content = true;
372 foreach ($changes as $changeinfo => $change) {
373 echo "<P><FONT SIZE=1>".$change["text"]."</FONT></P>";
600149be 374 }
375 }
376 }
377
378
3869a2ac 379 // Now display new things from each module
600149be 380
3869a2ac 381 $mods = get_list_of_plugins("mod");
600149be 382
3869a2ac 383 foreach ($mods as $mod) {
384 include_once("$CFG->dirroot/mod/$mod/lib.php");
385 $print_recent_activity = $mod."_print_recent_activity";
386 if (function_exists($print_recent_activity)) {
387 $modcontent = $print_recent_activity($logs, isteacher($course->id));
388 if ($modcontent) {
389 $content = true;
600149be 390 }
600149be 391 }
392 }
393
3869a2ac 394
600149be 395 if (! $content) {
97c270e9 396 echo "<FONT SIZE=2>".get_string("nothingnew")."</FONT>";
600149be 397 }
600149be 398}
399
e1360728 400
d897cae4 401function get_array_of_activities($courseid) {
402// For a given course, returns an array of course activity objects
403// Each item in the array contains he following properties:
404// cm - course module id
405// mod - name of the module (eg forum)
406// section - the number of the section (eg week or topic)
407// name - the name of the instance
408
409 $mod = array();
410
9fa49e22 411 if (!$rawmods = get_course_mods($courseid)) {
d897cae4 412 return NULL;
413 }
414
415 if ($sections = get_records("course_sections", "course", $courseid, "section ASC")) {
416 foreach ($sections as $section) {
417 if ($section->sequence) {
418 $sequence = explode(",", $section->sequence);
419 foreach ($sequence as $seq) {
420 $mod[$seq]->cm = $rawmods[$seq]->id;
421 $mod[$seq]->mod = $rawmods[$seq]->modname;
422 $mod[$seq]->section = $section->section;
423 $mod[$seq]->name = urlencode(get_field($rawmods[$seq]->modname, "name", "id", $rawmods[$seq]->instance));
424 }
425 }
426 }
427 }
428 return $mod;
429}
430
431
432
e1360728 433
90845098 434function get_all_mods($courseid, &$mods, &$modnames, &$modnamesplural, &$modnamesused) {
435// Returns a number of useful structures for course displays
7468bf01 436
90845098 437 $mods = NULL; // course modules indexed by id
438 $modnames = NULL; // all course module names
94361e02 439 $modnamesplural= NULL; // all course module names (plural form)
90845098 440 $modnamesused = NULL; // course module names used
7468bf01 441
9fa49e22 442 if ($allmods = get_records("modules")) {
90845098 443 foreach ($allmods as $mod) {
444 $modnames[$mod->name] = get_string("modulename", "$mod->name");
445 $modnamesplural[$mod->name] = get_string("modulenameplural", "$mod->name");
446 }
447 asort($modnames);
448 } else {
449 error("No modules are installed!");
450 }
451
9fa49e22 452 if ($rawmods = get_course_mods($courseid)) {
7468bf01 453 foreach($rawmods as $mod) { // Index the mods
454 $mods[$mod->id] = $mod;
90845098 455 $mods[$mod->id]->modfullname = $modnames[$mod->modname];
456 $modnamesused[$mod->modname] = $modnames[$mod->modname];
7468bf01 457 }
90845098 458 asort($modnamesused);
7468bf01 459 }
7468bf01 460}
461
9fa49e22 462
7468bf01 463function get_all_sections($courseid) {
464
d26d7ed0 465 return get_records("course_sections", "course", "$courseid", "section",
9fa49e22 466 "section, id, course, summary, sequence");
7468bf01 467}
468
ba2e5d73 469
5e367a2d 470function print_section_block($heading, $course, $section, $mods, $modnames, $modnamesused,
471 $absolute=true, $width="100%", $isediting=false) {
472
473 global $CFG;
474
475 $modinfo = unserialize($course->modinfo);
476 $moddata = array();
477 $modicon = array();
478 $editbuttons = "";
479
480 if ($section->sequence) {
481
482 $sectionmods = explode(",", $section->sequence);
483
484 foreach ($sectionmods as $modnumber) {
485 $mod = $mods[$modnumber];
486 if ($isediting) {
487 $editbuttons = make_editing_buttons($mod->id, $absolute);
488 }
489 $instancename = urldecode($modinfo[$modnumber]->name);
490 $modicon[] = "<img src=\"$CFG->wwwroot/mod/$mod->modname/icon.gif\" height=\"16\" width=\"16\" alt=\"$mod->modfullname\">";
491 $moddata[] = "<a title=\"$mod->modfullname\" href=\"$CFG->wwwroot/mod/$mod->modname/view.php?id=$mod->id\">$instancename</a><BR>$editbuttons";
492 }
493 }
a44d18e7 494 if ($isediting) {
15ac9065 495 $editmenu = popup_form("$CFG->wwwroot/course/mod.php?id=$course->id&amp;section=$section->section&add=",
5e367a2d 496 $modnames, "section0", "", get_string("add")."...", "mods", get_string("activities"), true);
497 $editmenu = "<DIV ALIGN=right>$editmenu</DIV>";
47f1da80 498 } else {
499 $editmenu = "";
5e367a2d 500 }
501
502 print_side_block($heading, "", $moddata, $modicon, $editmenu, $width);
503}
504
505
d897cae4 506function print_section($course, $section, $mods, $modnamesused, $absolute=false, $width="100%") {
94361e02 507 global $CFG;
508
c408b0c4 509 $modinfo = unserialize($course->modinfo);
94361e02 510
19a55d67 511 echo "<TABLE WIDTH=\"$width\"><TR><TD>\n";
94361e02 512 if ($section->sequence) {
513
514 $sectionmods = explode(",", $section->sequence);
515
516 foreach ($sectionmods as $modnumber) {
517 $mod = $mods[$modnumber];
c408b0c4 518 $instancename = urldecode($modinfo[$modnumber]->name);
94361e02 519 echo "<IMG SRC=\"$CFG->wwwroot/mod/$mod->modname/icon.gif\" HEIGHT=16 WIDTH=16 ALT=\"$mod->modfullname\">";
a7ad3ea6 520 echo " <FONT SIZE=2><A TITLE=\"$mod->modfullname\"";
521 echo " HREF=\"$CFG->wwwroot/mod/$mod->modname/view.php?id=$mod->id\">$instancename</A></FONT>";
d897cae4 522 if (isediting($course->id)) {
5e367a2d 523 echo "&nbsp;&nbsp;";
94361e02 524 echo make_editing_buttons($mod->id, $absolute);
525 }
526 echo "<BR>\n";
527 }
528 }
47ef8795 529 echo "</TD></TR></TABLE><BR>\n\n";
a7ad3ea6 530}
531
7541bc3e 532function print_heading_block($heading, $width="100%", $class="headingblock") {
5e367a2d 533 global $THEME;
534
7541bc3e 535 echo "<table width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"5\">";
536 echo "<tr><td bgcolor=\"$THEME->cellheading\" class=\"$class\">";
5e367a2d 537 echo stripslashes($heading);
538 echo "</td></tr></table>";
5e367a2d 539}
540
541function print_side_block($heading="", $content="", $list=NULL, $icons=NULL, $footer="", $width=180) {
542// Prints a nice side block with an optional header. The content can either
543// be a block of HTML or a list of text with optional icons.
a7ad3ea6 544
5e367a2d 545 global $THEME;
546
7541bc3e 547 print_side_block_start($heading, $width);
548
5e367a2d 549 if ($content) {
7541bc3e 550 echo "$content";
5e367a2d 551 } else {
5e367a2d 552 echo "<table width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"2\">";
553 foreach ($list as $key => $string) {
7541bc3e 554 echo "<tr bgcolor=\"$THEME->cellcontent2\">";
5e367a2d 555 if ($icons) {
7541bc3e 556 echo "<td class=\"sideblocklinks\" valign=\"top\" width=\"16\">".$icons[$key]."</td>";
a7ad3ea6 557 }
7541bc3e 558 echo "<td class=\"sideblocklinks\" valign=\"top\" width=\"*\"><font size=\"-1\">$string</font></td>";
5e367a2d 559 echo "</tr>";
a7ad3ea6 560 }
5e367a2d 561 if ($footer) {
7541bc3e 562 echo "<tr bgcolor=\"$THEME->cellcontent2\">";
5e367a2d 563 if ($icons) {
7541bc3e 564 echo "<td class=\"sideblocklinks\" valign=\"top\" width=\"16\">&nbsp;</td>";
5e367a2d 565 }
7541bc3e 566 echo "<td class=\"sideblocklinks\"><font size=\"-1\">$footer</td>";
5e367a2d 567 echo "</tr>";
568 }
569 echo "</table>";
a7ad3ea6 570 }
5e367a2d 571
7541bc3e 572 print_side_block_end();
573}
574
575function print_side_block_start($heading="", $width=180, $class="sideblockmain") {
576// Starts a nice side block with an optional header.
577
578 global $THEME;
579
580 echo "<table class=\"sideblock\" width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"5\">";
581 if ($heading) {
582 echo "<tr>";
583 echo "<td class=\"sideblockheading\" bgcolor=\"$THEME->cellheading\">$heading</td>";
584 echo "</tr>";
585 }
586 echo "<tr>";
587 echo "<td class=\"$class\" bgcolor=\"$THEME->cellcontent2\">";
588}
589
590function print_side_block_end() {
591 echo "</td></tr>";
5e367a2d 592 echo "</table><br \>";
94361e02 593}
594
5e367a2d 595
670fddf1 596function print_admin_links ($siteid, $width=180) {
b0e3a925 597 global $CFG;
2b25f2a0 598
2b25f2a0 599 $icon = "<IMG SRC=\"$CFG->wwwroot/pix/i/settings.gif\" HEIGHT=16 WIDTH=16 ALT=\"\">";
74944b73 600 $moddata[]="<A HREF=\"$CFG->wwwroot/admin/config.php\">".get_string("configvariables")."</A>";
2b25f2a0 601 $modicon[]=$icon;
602 $moddata[]="<A HREF=\"$CFG->wwwroot/admin/site.php\">".get_string("sitesettings")."</A>";
603 $modicon[]=$icon;
74944b73 604 $moddata[]="<A HREF=\"$CFG->wwwroot/course/log.php?id=$siteid\">".get_string("sitelogs")."</A>";
605 $modicon[]=$icon;
1e3e716f 606 $moddata[]="<A HREF=\"$CFG->wwwroot/theme/index.php\">".get_string("choosetheme")."</A>";
607 $modicon[]=$icon;
31410e9a 608 $moddata[]="<A HREF=\"$CFG->wwwroot/admin/lang.php\">".get_string("checklanguage")."</A>";
609 $modicon[]=$icon;
077f9a67 610 if (file_exists("$CFG->dirroot/admin/$CFG->dbtype")) {
8b3235f5 611 $moddata[]="<A HREF=\"$CFG->wwwroot/admin/$CFG->dbtype/frame.php\">".get_string("managedatabase")."</A>";
077f9a67 612 $modicon[]=$icon;
613 }
74944b73 614 $moddata[]="<HR>";
615 $modicon[]="";
2b25f2a0 616 $moddata[]="<A HREF=\"$CFG->wwwroot/course/edit.php\">".get_string("addnewcourse")."</A>";
617 $modicon[]=$icon;
ba2e5d73 618 $moddata[]="<A HREF=\"$CFG->wwwroot/course/categories.php\">".get_string("categories")."</A>";
619 $modicon[]=$icon;
2b25f2a0 620 $moddata[]="<A HREF=\"$CFG->wwwroot/course/teacher.php\">".get_string("assignteachers")."</A>";
621 $modicon[]=$icon;
622 $moddata[]="<A HREF=\"$CFG->wwwroot/course/delete.php\">".get_string("deletecourse")."</A>";
623 $modicon[]=$icon;
74944b73 624 $moddata[]="<HR>";
625 $modicon[]="";
2b25f2a0 626 $moddata[]="<A HREF=\"$CFG->wwwroot/admin/user.php?newuser=true\">".get_string("addnewuser")."</A>";
627 $modicon[]=$icon;
628 $moddata[]="<A HREF=\"$CFG->wwwroot/admin/user.php\">".get_string("edituser")."</A>";
629 $modicon[]=$icon;
d7facad8 630 $moddata[]="<A HREF=\"$CFG->wwwroot/admin/admin.php\">".get_string("assignadmins")."</A>";
631 $modicon[]=$icon;
60459ef7 632 $moddata[]="<A HREF=\"$CFG->wwwroot/admin/auth.php\">".get_string("authentication")."</A>";
633 $modicon[]=$icon;
2b25f2a0 634 $fulladmin = "<P><A HREF=\"$CFG->wwwroot/admin/\">".get_string("admin")."</A>...";
5e367a2d 635
636 print_side_block(get_string("administration"), "", $moddata, $modicon, $fulladmin, $width);
637
19a55d67 638 echo "<IMG SRC=\"$CFG->wwwroot/pix/spacer.gif\" WIDTH=\"$width\" HEIGHT=1><BR>";
2b25f2a0 639}
640
b4d7002e 641function print_course_admin_links($course, $width=180) {
b0e3a925 642 global $USER, $CFG;
44dad735 643
13469b82 644 if (isteacher($course->id)) {
645 $adminicon[]="<IMG SRC=\"$CFG->wwwroot/pix/i/edit.gif\" HEIGHT=16 WIDTH=16 ALT=\"\">";
646 if (isediting($course->id)) {
647 $admindata[]="<A HREF=\"view.php?id=$course->id&edit=off\">".get_string("turneditingoff")."</A>";
648 } else {
649 $admindata[]="<A HREF=\"view.php?id=$course->id&edit=on\">".get_string("turneditingon")."</A>";
650 }
651 $admindata[]="<A HREF=\"edit.php?id=$course->id\">".get_string("settings")."...</A>";
652 $adminicon[]="<IMG SRC=\"$CFG->wwwroot/pix/i/settings.gif\" HEIGHT=16 WIDTH=16 ALT=\"\">";
653 if (!$course->teachers) {
654 $course->teachers = get_string("defaultcourseteachers");
655 }
656 $admindata[]="<A HREF=\"teachers.php?id=$course->id\">$course->teachers...</A>";
657 $adminicon[]="<IMG SRC=\"$CFG->wwwroot/pix/i/settings.gif\" HEIGHT=16 WIDTH=16 ALT=\"\">";
658
659 $admindata[]="<A HREF=\"grades.php?id=$course->id\">".get_string("grades")."...</A>";
660 $adminicon[]="<IMG SRC=\"$CFG->wwwroot/pix/i/grades.gif\" HEIGHT=16 WIDTH=16 ALT=\"\">";
661
662 $admindata[]="<A HREF=\"log.php?id=$course->id\">".get_string("logs")."...</A>";
663 $adminicon[]="<IMG SRC=\"$CFG->wwwroot/pix/i/log.gif\" HEIGHT=16 WIDTH=16 ALT=\"\">";
664 $admindata[]="<A HREF=\"$CFG->wwwroot/files/index.php?id=$course->id\">".get_string("files")."...</A>";
665 $adminicon[]="<IMG SRC=\"$CFG->wwwroot/files/pix/files.gif\" HEIGHT=16 WIDTH=16 ALT=\"\">";
666
667 $admindata[]="<A HREF=\"$CFG->wwwroot/doc/view.php?id=$course->id&file=teacher.html\">".get_string("help")."...</A>";
668 $adminicon[]="<IMG SRC=\"$CFG->wwwroot/mod/resource/icon.gif\" HEIGHT=16 WIDTH=16 ALT=\"\">";
b4d7002e 669
13469b82 670 if ($teacherforum = forum_get_course_forum($course->id, "teacher")) {
671 $admindata[]="<A HREF=\"$CFG->wwwroot/mod/forum/view.php?f=$teacherforum->id\">".get_string("nameteacher", "forum")."</A>";
672 $adminicon[]="<IMG SRC=\"$CFG->wwwroot/mod/forum/icon.gif\" HEIGHT=16 WIDTH=16 ALT=\"\">";
673 }
674 } else {
675 $admindata[]="<A HREF=\"grade.php?id=$course->id\">".get_string("grades")."...</A>";
676 $adminicon[]="<IMG SRC=\"$CFG->wwwroot/pix/i/grades.gif\" HEIGHT=16 WIDTH=16 ALT=\"\">";
677 }
44dad735 678
5e367a2d 679 print_side_block(get_string("administration"), "", $admindata, $adminicon, "", $width);
44dad735 680}
2b25f2a0 681
ba2e5d73 682function print_course_categories($categories, $selected="none", $width=180) {
0a263205 683 global $CFG, $THEME, $USER;
85098089 684
685 $strallowguests = get_string("allowguests");
686 $strrequireskey = get_string("requireskey");
ba2e5d73 687
0c656181 688 if ($selected == "index") { // Print comprehensive index of categories with courses
9fa49e22 689 if ($courses = get_courses()) {
0c656181 690 if (isset($USER->id) and !isadmin()) {
39246b72 691 print_simple_box_start("CENTER", "100%", $THEME->cellheading);
0c656181 692 print_heading("<A HREF=\"course/index.php?category=my\">".get_string("mycourses")."</A>", "LEFT");
693 $some = false;
694 echo "<UL>";
695 foreach ($courses as $key => $course) {
696 if (isteacher($course->id) or isstudent($course->id)) {
85098089 697 echo "<A HREF=\"$CFG->wwwroot/course/view.php?id=$course->id\">$course->fullname</A>";
698 echo "<BR>";
0c656181 699 $some = true;
700 }
701 }
702 if (!$some) {
703 print_string("nocoursesyet");
704 }
705 echo "</UL>";
706 print_simple_box_end();
707 print_spacer(8,1);
708 }
709 foreach ($categories as $category) {
bd4707bf 710 print_simple_box_start("CENTER", "100%");
7ef459eb 711 print_heading("<A HREF=\"course/index.php?category=$category->id\">$category->name</A>", "LEFT");
0c656181 712 $some = false;
713 echo "<UL>";
714 foreach ($courses as $key => $course) {
715 if ($course->category == $category->id) {
85098089 716 echo "<A HREF=\"$CFG->wwwroot/course/view.php?id=$course->id\">$course->fullname</A>";
717 echo "&nbsp;&nbsp;";
0c656181 718 unset($courses[$key]);
85098089 719 if ($course->guest ) {
720 echo "<A TITLE=\"$strallowguests\" HREF=\"$CFG->wwwroot/course/view.php?id=$course->id\">";
721 echo "<IMG ALT=\"\" HEIGHT=16 WIDTH=16 BORDER=0 SRC=\"$CFG->wwwroot/user/user.gif\"></A>";
722 }
723 if ($course->password) {
724 echo "<A TITLE=\"$strrequireskey\" HREF=\"$CFG->wwwroot/course/view.php?id=$course->id\">";
725 echo "<IMG ALT=\"\" HEIGHT=16 WIDTH=16 BORDER=0 SRC=\"$CFG->wwwroot/pix/i/key.gif\"></A>";
726 }
727 echo "<BR>";
0c656181 728 $some = true;
729 }
730 }
731 if (!$some) {
732 print_string("nocoursesyet");
733 }
734 echo "</UL>";
735 print_simple_box_end();
736 print_spacer(8,1);
737 }
ba2e5d73 738 }
0c656181 739
740 } else { // Print short list of categories only
741 foreach ($categories as $cat) {
742 $caticon[]="<IMG SRC=\"$CFG->wwwroot/pix/i/course.gif\" HEIGHT=16 WIDTH=16>";
743 if ($cat->id == $selected) {
744 $catdata[]="$cat->name";
745 } else {
746 $catdata[]="<A HREF=\"$CFG->wwwroot/course/index.php?category=$cat->id\">$cat->name</A>";
747 }
748 }
749 $catdata[] = "<A HREF=\"$CFG->wwwroot/course/index.php?category=all\">".get_string("fulllistofcourses")."</A>";
13770d0c 750 $caticon[] = "";
0c656181 751 if (isset($USER->id)) {
752 $catdata[] = "<A HREF=\"$CFG->wwwroot/course/index.php?category=my\">".get_string("mycourses")."</A>";
13770d0c 753 $caticon[] = "";
0c656181 754 }
13770d0c 755 print_side_block(get_string("categories"), "", $catdata, $caticon, "", $width);
ba2e5d73 756 }
ba2e5d73 757}
94361e02 758
2b8cef80 759function print_log_graph($course, $userid=0, $type="course.png", $date=0) {
760 global $CFG;
607809b3 761 if (empty($CFG->gdversion)) {
762 echo "(".get_string("gdneed").")";
763 } else {
764 echo "<IMG BORDER=0 SRC=\"$CFG->wwwroot/course/loggraph.php?id=$course->id&user=$userid&type=$type&date=$date\">";
765 }
2b8cef80 766}
767
11b0c469 768
769
770/// MODULE FUNCTIONS /////////////////////////////////////////////////////////////////
771
772function add_course_module($mod) {
773 GLOBAL $db;
774
e5dfd0f3 775 $mod->added = time();
11b0c469 776
e5dfd0f3 777 return insert_record("course_modules", $mod);
11b0c469 778}
779
780function add_mod_to_section($mod) {
781// Returns the course_sections ID where the mod is inserted
782 GLOBAL $db;
783
9fa49e22 784 if ($section = get_record("course_sections", "course", "$mod->course", "section", "$mod->section")) {
e5dfd0f3 785 if ($section->sequence) {
786 $newsequence = "$section->sequence,$mod->coursemodule";
11b0c469 787 } else {
788 $newsequence = "$mod->coursemodule";
789 }
e5dfd0f3 790 if (set_field("course_sections", "sequence", $newsequence, "id", $section->id)) {
791 return $section->id; // Return course_sections ID that was used.
11b0c469 792 } else {
e5dfd0f3 793 return 0;
11b0c469 794 }
795
796 } else { // Insert a new record
e5dfd0f3 797 $section->course = $mod->course;
798 $section->section = $mod->section;
799 $section->summary = "";
800 $section->sequence = $mod->coursemodule;
801 return insert_record("course_sections", $section);
11b0c469 802 }
803}
804
805function delete_course_module($mod) {
806 return set_field("course_modules", "deleted", 1, "id", $mod);
807}
808
809function delete_mod_from_section($mod, $section) {
810 GLOBAL $db;
811
e5dfd0f3 812 if ($section = get_record("course_sections", "id", "$section") ) {
11b0c469 813
e5dfd0f3 814 $modarray = explode(",", $section->sequence);
11b0c469 815
816 if ($key = array_keys ($modarray, $mod)) {
817 array_splice($modarray, $key[0], 1);
818 $newsequence = implode(",", $modarray);
e5dfd0f3 819 return set_field("course_sections", "sequence", $newsequence, "id", $section->id);
11b0c469 820 } else {
821 return false;
822 }
823
824 } else {
825 return false;
826 }
827}
828
829
7c0f2984 830function move_module($cm, $move) {
11b0c469 831 GLOBAL $db;
832
833 if (!$move) {
834 return true;
835 }
836
11b0c469 837 if (! $thissection = get_record("course_sections", "id", $cm->section)) {
838 error("This course section doesn't exist");
839 }
840
841 $mods = explode(",", $thissection->sequence);
842
843 $len = count($mods);
844 $pos = array_keys($mods, $cm->id);
845 $thepos = $pos[0];
846
847 if ($len == 0 || count($pos) == 0 ) {
848 error("Very strange. Could not find the required module in this section.");
849 }
850
851 if ($len == 1) {
852 $first = true;
853 $last = true;
854 } else {
855 $first = ($thepos == 0);
856 $last = ($thepos == $len - 1);
857 }
858
859 if ($move < 0) { // Moving the module up
860
861 if ($first) {
862 if ($thissection->section == 1) { // First section, do nothing
863 return true;
864 } else { // Push onto end of previous section
865 $prevsectionnumber = $thissection->section - 1;
9fa49e22 866 if (! $prevsection = get_record("course_sections", "course", "$thissection->course",
867 "section", "$prevsectionnumber")) {
11b0c469 868 error("Previous section ($prevsection->id) doesn't exist");
869 }
870
871 if ($prevsection->sequence) {
872 $newsequence = "$prevsection->sequence,$cm->id";
873 } else {
874 $newsequence = "$cm->id";
875 }
876
877 if (! set_field("course_sections", "sequence", $newsequence, "id", $prevsection->id)) {
878 error("Previous section could not be updated");
879 }
880
881 if (! set_field("course_modules", "section", $prevsection->id, "id", $cm->id)) {
882 error("Module could not be updated");
883 }
884
885 array_splice($mods, 0, 1);
886 $newsequence = implode(",", $mods);
887 if (! set_field("course_sections", "sequence", $newsequence, "id", $thissection->id)) {
888 error("Module could not be updated");
889 }
890
891 return true;
892
893 }
894 } else { // move up within this section
895 $swap = $mods[$thepos-1];
896 $mods[$thepos-1] = $mods[$thepos];
897 $mods[$thepos] = $swap;
898
899 $newsequence = implode(",", $mods);
900 if (! set_field("course_sections", "sequence", $newsequence, "id", $thissection->id)) {
901 error("This section could not be updated");
902 }
903 return true;
904 }
905
906 } else { // Moving the module down
907
908 if ($last) {
909 $nextsectionnumber = $thissection->section + 1;
9fa49e22 910 if ($nextsection = get_record("course_sections", "course", "$thissection->course",
911 "section", "$nextsectionnumber")) {
11b0c469 912
913 if ($nextsection->sequence) {
914 $newsequence = "$cm->id,$nextsection->sequence";
915 } else {
916 $newsequence = "$cm->id";
917 }
918
919 if (! set_field("course_sections", "sequence", $newsequence, "id", $nextsection->id)) {
920 error("Next section could not be updated");
921 }
922
923 if (! set_field("course_modules", "section", $nextsection->id, "id", $cm->id)) {
924 error("Module could not be updated");
925 }
926
927 array_splice($mods, $thepos, 1);
928 $newsequence = implode(",", $mods);
929 if (! set_field("course_sections", "sequence", $newsequence, "id", $thissection->id)) {
930 error("This section could not be updated");
931 }
932 return true;
933
934 } else { // There is no next section, so just return
935 return true;
936
937 }
938 } else { // move down within this section
939 $swap = $mods[$thepos+1];
940 $mods[$thepos+1] = $mods[$thepos];
941 $mods[$thepos] = $swap;
942
943 $newsequence = implode(",", $mods);
944 if (! set_field("course_sections", "sequence", $newsequence, "id", $thissection->id)) {
945 error("This section could not be updated");
946 }
947 return true;
948 }
949 }
950}
951
94361e02 952function make_editing_buttons($moduleid, $absolute=false) {
953 global $CFG;
954
90845098 955 $delete = get_string("delete");
956 $moveup = get_string("moveup");
957 $movedown = get_string("movedown");
958 $update = get_string("update");
94361e02 959
960 if ($absolute) {
961 $path = "$CFG->wwwroot/course/";
962 } else {
963 $path = "";
964 }
5e367a2d 965 return "<A TITLE=\"$delete\" HREF=\"".$path."mod.php?delete=$moduleid\"><IMG
97c270e9 966 SRC=".$path."../pix/t/delete.gif BORDER=0></A>
967 <A TITLE=\"$moveup\" HREF=\"".$path."mod.php?id=$moduleid&move=-1\"><IMG
968 SRC=".$path."../pix/t/up.gif BORDER=0></A>
969 <A TITLE=\"$movedown\" HREF=\"".$path."mod.php?id=$moduleid&move=1\"><IMG
970 SRC=".$path."../pix/t/down.gif BORDER=0></A>
971 <A TITLE=\"$update\" HREF=\"".$path."mod.php?update=$moduleid\"><IMG
972 SRC=".$path."../pix/t/edit.gif BORDER=0></A>";
90845098 973}
974
f9903ed0 975?>