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