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