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