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