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