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