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