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