Removed some debugging stuff
[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) {
0a263205 176 global $CFG, $USER;
d887b5a7 177
ba2e5d73 178 if ($category == "all") {
179 $courses = get_records_sql("SELECT * FROM course WHERE category > 0 ORDER BY fullname ASC");
0a263205 180
181 } else if ($category == "my") {
182 if (isset($USER->id)) {
183 if ($courses = get_records_sql("SELECT * FROM course WHERE category > 0 ORDER BY fullname ASC")) {
184 foreach ($courses as $key => $course) {
185 if (!isteacher($course->id) and !isstudent($course->id)) {
186 unset($courses[$key]);
187 }
188 }
189 }
190 }
191
ba2e5d73 192 } else {
193 $courses = get_records("course", "category", $category, "fullname ASC");
194 }
195
196 if ($courses) {
94361e02 197 if ($style == "minimal") {
198 $count = 0;
97c270e9 199 $icon = "<IMG SRC=\"pix/i/course.gif\" HEIGHT=16 WIDTH=16 ALT=\"".get_string("course")."\">";
94361e02 200 foreach ($courses as $course) {
565f7a95 201 $moddata[]="<A TITLE=\"$course->shortname\" HREF=\"$CFG->wwwroot/course/view.php?id=$course->id\">$course->fullname</A>";
94361e02 202 $modicon[]=$icon;
203 if ($count++ >= $maxcount) {
204 break;
205 }
206 }
207 $fulllist = "<P><A HREF=\"$CFG->wwwroot/course/\">".get_string("fulllistofcourses")."</A>...";
208 print_side_block("", $moddata, "$fulllist", $modicon);
209
210 } else {
211 foreach ($courses as $course) {
212 print_course($course);
213 echo "<BR>\n";
214 }
d887b5a7 215 }
216
217 } else {
cf38360f 218 echo "<H3>".get_string("nocoursesyet")."</H3>";
d887b5a7 219 }
220}
221
222
f9903ed0 223function print_course($course) {
224
d887b5a7 225 global $CFG;
226
a83fded1 227 if (! $site = get_site()) {
f9903ed0 228 error("Could not find a site!");
229 }
230
d887b5a7 231 print_simple_box_start("CENTER", "100%");
f9903ed0 232
233 echo "<TABLE WIDTH=100%>";
da5c172a 234 echo "<TR VALIGN=top>";
235 echo "<TD VALIGN=top WIDTH=50%>";
7a302afc 236 echo "<P><FONT SIZE=3><B><A TITLE=\"".get_string("entercourse")."\"
9481285b 237 HREF=\"$CFG->wwwroot/course/view.php?id=$course->id\">$course->fullname</A></B></FONT></P>";
0c71c9ae 238 if ($teachers = get_course_teachers($course->id)) {
f9903ed0 239 echo "<P><FONT SIZE=1>\n";
240 foreach ($teachers as $teacher) {
0c71c9ae 241 if ($teacher->authority > 0) {
b4d7002e 242 if (!$teacher->role) {
243 $teacher->role = $course->teacher;
244 }
245 echo "$teacher->role: <A HREF=\"$CFG->wwwroot/user/view.php?id=$teacher->id&course=$site->id\">$teacher->firstname $teacher->lastname</A><BR>";
0c71c9ae 246 }
f9903ed0 247 }
248 echo "</FONT></P>";
da5c172a 249 }
250 if ($course->guest or ($course->password == "")) {
97c270e9 251 echo "<A TITLE=\"".get_string("allowguests")."\" HREF=\"$CFG->wwwroot/course/view.php?id=$course->id\">";
d887b5a7 252 echo "<IMG VSPACE=4 ALT=\"\" HEIGHT=16 WIDTH=16 BORDER=0 SRC=\"$CFG->wwwroot/user/user.gif\"></A>&nbsp;&nbsp;";
da5c172a 253 }
254 if ($course->password) {
97c270e9 255 echo "<A TITLE=\"".get_string("requireskey")."\" HREF=\"$CFG->wwwroot/course/view.php?id=$course->id\">";
d887b5a7 256 echo "<IMG VSPACE=4 ALT=\"\" HEIGHT=16 WIDTH=16 BORDER=0 SRC=\"$CFG->wwwroot/pix/i/key.gif\"></A>";
da5c172a 257 }
258
259
260 echo "</TD><TD VALIGN=top WIDTH=50%>";
261 echo "<P><FONT SIZE=2>".text_to_html($course->summary)."</FONT></P>";
262 echo "</TD></TR>";
263 echo "</TABLE>";
f9903ed0 264
da5c172a 265 print_simple_box_end();
f9903ed0 266}
267
600149be 268function print_headline($text, $size=2) {
269 echo "<B><FONT SIZE=\"$size\">$text</FONT></B><BR>\n";
270}
271
272function print_recent_activity($course) {
273 // $course is an object
274 // This function trawls through the logs looking for
275 // anything new since the user's last login
276
97c270e9 277 global $CFG, $USER, $COURSE_TEACHER_COLOR;
600149be 278
279 if (! $USER->lastlogin ) {
4c654ee3 280 echo "<P ALIGN=CENTER><FONT SIZE=1>";
4b1371a7 281 print_string("welcometocourse", "", $course->shortname);
4c654ee3 282 echo "</FONT></P>";
600149be 283 return;
4c654ee3 284 } else {
285 echo "<P ALIGN=CENTER><FONT SIZE=1>";
286 echo get_string("yourlastlogin").":<BR>";
287 echo userdate($USER->lastlogin, "%A, %e %b %Y, %H:%M");
288 echo "</FONT></P>";
600149be 289 }
290
291 if (! $logs = get_records_sql("SELECT * FROM log WHERE time > '$USER->lastlogin' AND course = '$course->id' ORDER BY time ASC")) {
292 return;
293 }
294
295
296 // Firstly, have there been any new enrolments?
297
298 $heading = false;
299 $content = false;
300 foreach ($logs as $log) {
301 if ($log->module == "course" and $log->action == "enrol") {
302 if (! $heading) {
4c654ee3 303 print_headline(get_string("newusers").":");
600149be 304 $heading = true;
305 $content = true;
306 }
307 $user = get_record("user", "id", $log->info);
ef25340c 308 echo "<P><FONT SIZE=1><A HREF=\"../user/view.php?id=$user->id&course=$course->id\">$user->firstname $user->lastname</A></FONT></P>";
600149be 309 }
310 }
311
312 // Next, have there been any changes to the course structure?
313
600149be 314 foreach ($logs as $log) {
315 if ($log->module == "course") {
316 if ($log->action == "add mod" or $log->action == "update mod" or $log->action == "delete mod") {
600149be 317 $info = split(" ", $log->info);
318 $modname = get_field($info[0], "name", "id", $info[1]);
319
600149be 320 switch ($log->action) {
321 case "add mod":
27038d9f 322 $stradded = get_string("added", "moodle", get_string("modulename", $info[0]));
4c654ee3 323 $changelist["$log->info"] = array ("operation" => "add", "text" => "$stradded:<BR><A HREF=\"$CFG->wwwroot/course/$log->url\">$modname</A>");
600149be 324 break;
325 case "update mod":
27038d9f 326 $strupdated = get_string("updated", "moodle", get_string("modulename", $info[0]));
ef25340c 327 if (! $changelist["$log->info"]) {
4c654ee3 328 $changelist["$log->info"] = array ("operation" => "update", "text" => "$strupdated:<BR><A HREF=\"$CFG->wwwroot/course/$log->url\">$modname</A>");
ef25340c 329 }
600149be 330 break;
331 case "delete mod":
ef25340c 332 if ($changelist["$log->info"]["operation"] == "add") {
333 $changelist["$log->info"] = NULL;
334 } else {
27038d9f 335 $strdeleted = get_string("deletedactivity", "moodle", get_string("modulename", $info[0]));
4c654ee3 336 $changelist["$log->info"] = array ("operation" => "delete", "text" => $strdeleted);
ef25340c 337 }
600149be 338 break;
339 }
ef25340c 340 }
341 }
342 }
343
344 if ($changelist) {
345 foreach ($changelist as $changeinfo => $change) {
346 if ($change) {
347 $changes[$changeinfo] = $change;
348 }
349 }
350 if (count($changes) > 0) {
4c654ee3 351 print_headline(get_string("courseupdates").":");
ef25340c 352 $content = true;
353 foreach ($changes as $changeinfo => $change) {
354 echo "<P><FONT SIZE=1>".$change["text"]."</FONT></P>";
600149be 355 }
356 }
357 }
358
359
360 // Now all we need to know are the new posts.
361
ef25340c 362 $heading = false;
600149be 363 foreach ($logs as $log) {
364
501cdbd8 365 if ($log->module == "forum") {
600149be 366 $post = NULL;
367
36d4a9a1 368 if ($log->action == "add post") {
f9730aef 369 $post = get_record_sql("SELECT p.*, d.forum, u.firstname, u.lastname,
600149be 370 u.email, u.picture, u.id as userid
501cdbd8 371 FROM forum_discussions d, forum_posts p, user u
372 WHERE p.id = '$log->info' AND d.id = p.discussion AND p.user = u.id");
600149be 373
36d4a9a1 374 } else if ($log->action == "add discussion") {
f9730aef 375 $post = get_record_sql("SELECT p.*, d.forum, u.firstname, u.lastname,
600149be 376 u.email, u.picture, u.id as userid
501cdbd8 377 FROM forum_discussions d, forum_posts p, user u
600149be 378 WHERE d.id = '$log->info' AND d.firstpost = p.id AND p.user = u.id");
379 }
380
381 if ($post) {
f9730aef 382
383 $teacherpost = "";
384 if ($forum = get_record("forum", "id", $post->forum) ) {
385 if ($forum->type == "teacher") {
386 if (!isteacher($course->id)) {
387 continue;
388 } else {
97c270e9 389 $teacherpost = "COLOR=$COURSE_TEACHER_COLOR";
f9730aef 390 }
391 }
392 }
600149be 393 if (! $heading) {
4c654ee3 394 print_headline(get_string("newforumposts").":");
600149be 395 $heading = true;
396 $content = true;
397 }
2c0411e2 398 $date = userdate($post->modified, "%e %b, %H:%M");
399 echo "<P><FONT SIZE=1 $teacherpost>$date - $post->firstname $post->lastname<BR>";
501cdbd8 400 echo "\"<A HREF=\"$CFG->wwwroot/mod/forum/$log->url\">";
600149be 401 if ($log->action == "add") {
ef25340c 402 echo "<B>$post->subject</B>";
600149be 403 } else {
ef25340c 404 echo "$post->subject";
600149be 405 }
ef25340c 406 echo "</A>\"</FONT></P>";
600149be 407 }
408
409 }
410 }
411
412 if (! $content) {
97c270e9 413 echo "<FONT SIZE=2>".get_string("nothingnew")."</FONT>";
600149be 414 }
600149be 415}
416
e1360728 417
418function unenrol_student_in_course($user, $course) {
419 global $db;
420
421 return $db->Execute("DELETE FROM user_students WHERE user = '$user' AND course = '$course'");
422}
423
424
425
426function enrol_student_in_course($user, $course) {
427 global $db;
428
429 $timenow = time();
430
431 $rs = $db->Execute("INSERT INTO user_students (user, course, start, end, time)
432 VALUES ($user, $course, 0, 0, $timenow)");
433 if ($rs) {
434 return true;
435 } else {
436 return false;
437 }
438}
439
90845098 440function get_all_mods($courseid, &$mods, &$modnames, &$modnamesplural, &$modnamesused) {
441// Returns a number of useful structures for course displays
7468bf01 442
90845098 443 $mods = NULL; // course modules indexed by id
444 $modnames = NULL; // all course module names
94361e02 445 $modnamesplural= NULL; // all course module names (plural form)
90845098 446 $modnamesused = NULL; // course module names used
7468bf01 447
90845098 448 if ($allmods = get_records_sql("SELECT * FROM modules") ) {
449 foreach ($allmods as $mod) {
450 $modnames[$mod->name] = get_string("modulename", "$mod->name");
451 $modnamesplural[$mod->name] = get_string("modulenameplural", "$mod->name");
452 }
453 asort($modnames);
454 } else {
455 error("No modules are installed!");
456 }
457
458 if ($rawmods = get_records_sql("SELECT cm.*, m.name as modname
459 FROM modules m, course_modules cm
460 WHERE cm.course = '$courseid'
461 AND cm.deleted = '0'
94361e02 462 AND cm.module = m.id ") ) {
7468bf01 463 foreach($rawmods as $mod) { // Index the mods
464 $mods[$mod->id] = $mod;
90845098 465 $mods[$mod->id]->modfullname = $modnames[$mod->modname];
466 $modnamesused[$mod->modname] = $modnames[$mod->modname];
7468bf01 467 }
90845098 468 asort($modnamesused);
7468bf01 469 }
7468bf01 470}
471
472function get_all_sections($courseid) {
473
474 return get_records_sql("SELECT section, id, course, summary, sequence
475 FROM course_sections
476 WHERE course = '$courseid'
477 ORDER BY section");
478}
479
ba2e5d73 480function get_all_categories() {
481 return get_records_sql("SELECT * FROM course_categories ORDER by name");
482}
483
19a55d67 484function print_section($courseid, $section, $mods, $modnamesused, $absolute=false, $width="100%") {
94361e02 485 global $CFG;
486
487
19a55d67 488 echo "<TABLE WIDTH=\"$width\"><TR><TD>\n";
94361e02 489 if ($section->sequence) {
490
491 $sectionmods = explode(",", $section->sequence);
492
493 foreach ($sectionmods as $modnumber) {
494 $mod = $mods[$modnumber];
495 $instancename = get_field("$mod->modname", "name", "id", "$mod->instance");
496 echo "<IMG SRC=\"$CFG->wwwroot/mod/$mod->modname/icon.gif\" HEIGHT=16 WIDTH=16 ALT=\"$mod->modfullname\">";
a7ad3ea6 497 echo " <FONT SIZE=2><A TITLE=\"$mod->modfullname\"";
498 echo " HREF=\"$CFG->wwwroot/mod/$mod->modname/view.php?id=$mod->id\">$instancename</A></FONT>";
94361e02 499 if (isediting($courseid)) {
500 echo make_editing_buttons($mod->id, $absolute);
501 }
502 echo "<BR>\n";
503 }
504 }
47ef8795 505 echo "</TD></TR></TABLE><BR>\n\n";
a7ad3ea6 506}
507
ee1ddef6 508function print_side_block($heading="", $list=NULL, $footer="", $icons=NULL, $width=180) {
a7ad3ea6 509
19a55d67 510 echo "<TABLE WIDTH=\"$width\">\n";
a7ad3ea6 511 echo "<TR><TD COLSPAN=2><P><B><FONT SIZE=2>$heading</TD></TR>\n";
512 if ($list) {
513 foreach($list as $key => $string) {
514 echo "<TR><TD VALIGN=top WIDTH=12>";
515 if ($icons[$key]) {
516 echo $icons[$key];
517 } else {
518 echo "";
519 }
520 echo "</TD>\n<TD WIDTH=100% VALIGN=top>";
521 echo "<P><FONT SIZE=2>$string</FONT></P>";
522 echo "</TD></TR>\n";
523 }
524 }
525 if ($footer) {
526 echo "<TR><TD></TD><TD ALIGN=left><P><FONT SIZE=2>$footer</TD></TR>\n";
527 }
528 echo "</TABLE><BR>\n\n";
94361e02 529}
530
670fddf1 531function print_admin_links ($siteid, $width=180) {
2b25f2a0 532 global $THEME, $CFG;
533
19a55d67 534 print_simple_box(get_string("administration"), $align="CENTER", $width, $color="$THEME->cellheading");
2b25f2a0 535 $icon = "<IMG SRC=\"$CFG->wwwroot/pix/i/settings.gif\" HEIGHT=16 WIDTH=16 ALT=\"\">";
adecd142 536 $moddata[]="<A HREF=\"$CFG->wwwroot/course/log.php?id=$siteid\">".get_string("sitelogs")."</A>";
2b25f2a0 537 $modicon[]=$icon;
538 $moddata[]="<A HREF=\"$CFG->wwwroot/admin/site.php\">".get_string("sitesettings")."</A>";
539 $modicon[]=$icon;
1e3e716f 540 $moddata[]="<A HREF=\"$CFG->wwwroot/theme/index.php\">".get_string("choosetheme")."</A>";
541 $modicon[]=$icon;
31410e9a 542 $moddata[]="<A HREF=\"$CFG->wwwroot/admin/lang.php\">".get_string("checklanguage")."</A>";
543 $modicon[]=$icon;
2b25f2a0 544 $moddata[]="<A HREF=\"$CFG->wwwroot/course/edit.php\">".get_string("addnewcourse")."</A>";
545 $modicon[]=$icon;
ba2e5d73 546 $moddata[]="<A HREF=\"$CFG->wwwroot/course/categories.php\">".get_string("categories")."</A>";
547 $modicon[]=$icon;
2b25f2a0 548 $moddata[]="<A HREF=\"$CFG->wwwroot/course/teacher.php\">".get_string("assignteachers")."</A>";
549 $modicon[]=$icon;
550 $moddata[]="<A HREF=\"$CFG->wwwroot/course/delete.php\">".get_string("deletecourse")."</A>";
551 $modicon[]=$icon;
552 $moddata[]="<A HREF=\"$CFG->wwwroot/admin/user.php?newuser=true\">".get_string("addnewuser")."</A>";
553 $modicon[]=$icon;
554 $moddata[]="<A HREF=\"$CFG->wwwroot/admin/user.php\">".get_string("edituser")."</A>";
555 $modicon[]=$icon;
556 $fulladmin = "<P><A HREF=\"$CFG->wwwroot/admin/\">".get_string("admin")."</A>...";
19a55d67 557 print_side_block("", $moddata, "$fulladmin", $modicon, $width);
558 echo "<IMG SRC=\"$CFG->wwwroot/pix/spacer.gif\" WIDTH=\"$width\" HEIGHT=1><BR>";
2b25f2a0 559}
560
b4d7002e 561function print_course_admin_links($course, $width=180) {
44dad735 562 global $THEME, $CFG;
563
564 echo "<BR>";
565 $adminicon[]="<IMG SRC=\"$CFG->wwwroot/pix/i/edit.gif\" HEIGHT=16 WIDTH=16 ALT=\"\">";
b4d7002e 566 if (isediting($course->id)) {
567 $admindata[]="<A HREF=\"view.php?id=$course->id&edit=off\">".get_string("turneditingoff")."</A>";
44dad735 568 } else {
b4d7002e 569 $admindata[]="<A HREF=\"view.php?id=$course->id&edit=on\">".get_string("turneditingon")."</A>";
570 }
571 $admindata[]="<A HREF=\"edit.php?id=$course->id\">".get_string("settings")."...</A>";
572 $adminicon[]="<IMG SRC=\"$CFG->wwwroot/pix/i/settings.gif\" HEIGHT=16 WIDTH=16 ALT=\"\">";
573 if (!$course->teachers) {
574 $course->teachers = get_string("defaultcourseteachers");
44dad735 575 }
b4d7002e 576 $admindata[]="<A HREF=\"teachers.php?id=$course->id\">$course->teachers...</A>";
44dad735 577 $adminicon[]="<IMG SRC=\"$CFG->wwwroot/pix/i/settings.gif\" HEIGHT=16 WIDTH=16 ALT=\"\">";
b4d7002e 578
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>";
44dad735 585 $adminicon[]="<IMG SRC=\"$CFG->wwwroot/mod/reading/icon.gif\" HEIGHT=16 WIDTH=16 ALT=\"\">";
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;
ba2e5d73 599
600 foreach ($categories as $cat) {
601 $caticon[]="<IMG SRC=\"$CFG->wwwroot/pix/i/course.gif\" HEIGHT=16 WIDTH=16>";
602 if ($cat->id == $selected) {
603 $catdata[]="$cat->name";
604 } else {
605 $catdata[]="<A HREF=\"$CFG->wwwroot/course/index.php?category=$cat->id\">$cat->name</A>";
606 }
607 }
0a263205 608 $catdata[] = "<A HREF=\"$CFG->wwwroot/course/index.php?category=all\">".get_string("fulllistofcourses")."</A>";
609 if (isset($USER->id)) {
610 $catdata[] = "<A HREF=\"$CFG->wwwroot/course/index.php?category=my\">".get_string("mycourses")."</A>";
611 }
612 print_side_block("", $catdata, $showall.$mine, $caticon, $width);
ba2e5d73 613}
94361e02 614
2b8cef80 615function print_log_graph($course, $userid=0, $type="course.png", $date=0) {
616 global $CFG;
617 echo "<IMG BORDER=0 SRC=\"$CFG->wwwroot/course/loggraph.php?id=$course->id&user=$userid&type=$type&date=$date\">";
618}
619
11b0c469 620
621
622/// MODULE FUNCTIONS /////////////////////////////////////////////////////////////////
623
624function add_course_module($mod) {
625 GLOBAL $db;
626
627 $timenow = time();
628
629 if (!$rs = $db->Execute("INSERT into course_modules
630 SET course = '$mod->course',
631 module = '$mod->module',
632 instance = '$mod->instance',
633 section = '$mod->section',
634 added = '$timenow' ")) {
635 return 0;
636 }
637
638 // Get it out again - this is the most compatible way to determine the ID
639 if ($rs = $db->Execute("SELECT id FROM course_modules
640 WHERE module = $mod->module AND added = $timenow")) {
641 return $rs->fields[0];
642 } else {
643 return 0;
644 }
645
646}
647
648function add_mod_to_section($mod) {
649// Returns the course_sections ID where the mod is inserted
650 GLOBAL $db;
651
652 if ($cw = get_record_sql("SELECT * FROM course_sections
653 WHERE course = '$mod->course' AND section = '$mod->section'") ) {
654
655 if ($cw->sequence) {
656 $newsequence = "$cw->sequence,$mod->coursemodule";
657 } else {
658 $newsequence = "$mod->coursemodule";
659 }
660 if (!$rs = $db->Execute("UPDATE course_sections SET sequence = '$newsequence' WHERE id = '$cw->id'")) {
661 return 0;
662 } else {
663 return $cw->id; // Return course_sections ID that was used.
664 }
665
666 } else { // Insert a new record
667 if (!$rs = $db->Execute("INSERT into course_sections
668 SET course = '$mod->course',
669 section = '$mod->section',
670 summary = '',
671 sequence = '$mod->coursemodule' ")) {
672 return 0;
673 }
674 // Get it out again - this is the most compatible way to determine the ID
675 if ($rs = $db->Execute("SELECT id FROM course_sections
676 WHERE course = '$mod->course' AND section = '$mod->section'")) {
677 return $rs->fields[0];
678 } else {
679 return 0;
680 }
681 }
682}
683
684function delete_course_module($mod) {
685 return set_field("course_modules", "deleted", 1, "id", $mod);
686}
687
688function delete_mod_from_section($mod, $section) {
689 GLOBAL $db;
690
691 if ($cw = get_record("course_sections", "id", "$section") ) {
692
693 $modarray = explode(",", $cw->sequence);
694
695 if ($key = array_keys ($modarray, $mod)) {
696 array_splice($modarray, $key[0], 1);
697 $newsequence = implode(",", $modarray);
698 return set_field("course_sections", "sequence", $newsequence, "id", $cw->id);
699 } else {
700 return false;
701 }
702
703 } else {
704 return false;
705 }
706}
707
708
709function move_module($id, $move) {
710 GLOBAL $db;
711
712 if (!$move) {
713 return true;
714 }
715
716 if (! $cm = get_record("course_modules", "id", $id)) {
717 error("This course module doesn't exist");
718 }
719
720 if (! $thissection = get_record("course_sections", "id", $cm->section)) {
721 error("This course section doesn't exist");
722 }
723
724 $mods = explode(",", $thissection->sequence);
725
726 $len = count($mods);
727 $pos = array_keys($mods, $cm->id);
728 $thepos = $pos[0];
729
730 if ($len == 0 || count($pos) == 0 ) {
731 error("Very strange. Could not find the required module in this section.");
732 }
733
734 if ($len == 1) {
735 $first = true;
736 $last = true;
737 } else {
738 $first = ($thepos == 0);
739 $last = ($thepos == $len - 1);
740 }
741
742 if ($move < 0) { // Moving the module up
743
744 if ($first) {
745 if ($thissection->section == 1) { // First section, do nothing
746 return true;
747 } else { // Push onto end of previous section
748 $prevsectionnumber = $thissection->section - 1;
749 if (! $prevsection = get_record_sql("SELECT * FROM course_sections
750 WHERE course='$thissection->course'
751 AND section='$prevsectionnumber' ")) {
752 error("Previous section ($prevsection->id) doesn't exist");
753 }
754
755 if ($prevsection->sequence) {
756 $newsequence = "$prevsection->sequence,$cm->id";
757 } else {
758 $newsequence = "$cm->id";
759 }
760
761 if (! set_field("course_sections", "sequence", $newsequence, "id", $prevsection->id)) {
762 error("Previous section could not be updated");
763 }
764
765 if (! set_field("course_modules", "section", $prevsection->id, "id", $cm->id)) {
766 error("Module could not be updated");
767 }
768
769 array_splice($mods, 0, 1);
770 $newsequence = implode(",", $mods);
771 if (! set_field("course_sections", "sequence", $newsequence, "id", $thissection->id)) {
772 error("Module could not be updated");
773 }
774
775 return true;
776
777 }
778 } else { // move up within this section
779 $swap = $mods[$thepos-1];
780 $mods[$thepos-1] = $mods[$thepos];
781 $mods[$thepos] = $swap;
782
783 $newsequence = implode(",", $mods);
784 if (! set_field("course_sections", "sequence", $newsequence, "id", $thissection->id)) {
785 error("This section could not be updated");
786 }
787 return true;
788 }
789
790 } else { // Moving the module down
791
792 if ($last) {
793 $nextsectionnumber = $thissection->section + 1;
794 if ($nextsection = get_record_sql("SELECT * FROM course_sections
795 WHERE course='$thissection->course'
796 AND section='$nextsectionnumber' ")) {
797
798 if ($nextsection->sequence) {
799 $newsequence = "$cm->id,$nextsection->sequence";
800 } else {
801 $newsequence = "$cm->id";
802 }
803
804 if (! set_field("course_sections", "sequence", $newsequence, "id", $nextsection->id)) {
805 error("Next section could not be updated");
806 }
807
808 if (! set_field("course_modules", "section", $nextsection->id, "id", $cm->id)) {
809 error("Module could not be updated");
810 }
811
812 array_splice($mods, $thepos, 1);
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 } else { // There is no next section, so just return
820 return true;
821
822 }
823 } else { // move down within this section
824 $swap = $mods[$thepos+1];
825 $mods[$thepos+1] = $mods[$thepos];
826 $mods[$thepos] = $swap;
827
828 $newsequence = implode(",", $mods);
829 if (! set_field("course_sections", "sequence", $newsequence, "id", $thissection->id)) {
830 error("This section could not be updated");
831 }
832 return true;
833 }
834 }
835}
836
94361e02 837function make_editing_buttons($moduleid, $absolute=false) {
838 global $CFG;
839
90845098 840 $delete = get_string("delete");
841 $moveup = get_string("moveup");
842 $movedown = get_string("movedown");
843 $update = get_string("update");
94361e02 844
845 if ($absolute) {
846 $path = "$CFG->wwwroot/course/";
847 } else {
848 $path = "";
849 }
90845098 850 return "&nbsp; &nbsp;
97c270e9 851 <A TITLE=\"$delete\" HREF=\"".$path."mod.php?delete=$moduleid\"><IMG
852 SRC=".$path."../pix/t/delete.gif BORDER=0></A>
853 <A TITLE=\"$moveup\" HREF=\"".$path."mod.php?id=$moduleid&move=-1\"><IMG
854 SRC=".$path."../pix/t/up.gif BORDER=0></A>
855 <A TITLE=\"$movedown\" HREF=\"".$path."mod.php?id=$moduleid&move=1\"><IMG
856 SRC=".$path."../pix/t/down.gif BORDER=0></A>
857 <A TITLE=\"$update\" HREF=\"".$path."mod.php?update=$moduleid\"><IMG
858 SRC=".$path."../pix/t/edit.gif BORDER=0></A>";
90845098 859}
860
f9903ed0 861?>