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