Addition of modulename and modulenameplural strings as a standard to
[moodle.git] / course / lib.php
CommitLineData
f9903ed0 1<? // $Id$
2
ef58b822 3$COURSE_FORMATS = array (
b5fe4c93 4 "weeks" => "Weekly layout",
5 "social" => "Social layout",
6 "topics" => "Topics layout"
f9903ed0 7 );
8
ef58b822 9$COURSE_SECTION = array (
b5fe4c93 10 "weeks" => "week",
11 "social" => "section",
12 "topics" => "topic"
d9d1c35d 13 );
14
ef58b822 15$COURSE_MAX_LOG_DISPLAY = 150; // days
16
f9903ed0 17
f9903ed0 18function print_log_selector_form($course, $selecteduser=0, $selecteddate="today") {
19
9481285b 20 global $USER, $CFG;
21
f9903ed0 22 // Get all the possible users
23 $users = array();
720a43ce 24
25 if ($course->category) {
26 if ($students = get_records_sql("SELECT u.* FROM user u, user_students s
27 WHERE s.course = '$course->id' AND s.user = u.id
28 ORDER BY u.lastaccess DESC")) {
29 foreach ($students as $student) {
30 $users["$student->id"] = "$student->firstname $student->lastname";
31 }
32 }
33 if ($teachers = get_records_sql("SELECT u.* FROM user u, user_teachers t
34 WHERE t.course = '$course->id' AND t.user = u.id
35 ORDER BY u.lastaccess DESC")) {
36 foreach ($teachers as $teacher) {
37 $users["$teacher->id"] = "$teacher->firstname $teacher->lastname";
38 }
f9903ed0 39 }
40 }
720a43ce 41
42 if (isadmin()) {
43 if ($ccc = get_records_sql("SELECT * FROM course ORDER BY fullname")) {
44 foreach ($ccc as $cc) {
cfa5d3f2 45 if ($cc->category) {
46 $courses["$cc->id"] = "$cc->fullname";
47 } else {
48 $courses["$cc->id"] = " $cc->fullname (Site)";
49 }
720a43ce 50 }
f9903ed0 51 }
cfa5d3f2 52 asort($courses);
f9903ed0 53 }
54
55 asort($users);
56
57 // Get all the possible dates
9481285b 58 // Note that we are keeping track of real (GMT) time and user time
59 // User time is only used in displays - all calcs and passing is GMT
60
61 $timenow = time(); // GMT
62
63 // What day is it now for the user, and when is midnight that day (in GMT).
9604ccb1 64 $timemidnight = $today = usergetmidnight($timenow);
9481285b 65
66 // Put today up the top of the list
7a302afc 67 $dates = array("$timemidnight" => "Today, ".userdate($timenow, "%e %B %Y") );
9481285b 68
69 if (! $course->startdate) {
70 $course->startdate = $course->timecreated;
71 }
f9903ed0 72
9481285b 73 $numdates = 1;
74 while ($timemidnight > $course->startdate and $numdates < 365) {
f9903ed0 75 $timemidnight = $timemidnight - 86400;
9481285b 76 $timenow = $timenow - 86400;
7a302afc 77 $dates["$timemidnight"] = userdate($timenow, "%A, %e %B %Y");
9481285b 78 $numdates++;
f9903ed0 79 }
80
81 if ($selecteddate == "today") {
82 $selecteddate = $today;
83 }
84
85 echo "<CENTER>";
86 echo "<FORM ACTION=log.php METHOD=get>";
720a43ce 87 if (isadmin()) {
849bc02a 88 choose_from_menu ($courses, "id", $course->id, "");
720a43ce 89 } else {
90 echo "<INPUT TYPE=hidden NAME=id VALUE=\"$course->id\">";
91 }
92 if ($course->category) {
93 choose_from_menu ($users, "user", $selecteduser, "All participants");
94 }
f9903ed0 95 choose_from_menu ($dates, "date", $selecteddate, "Any day");
96 echo "<INPUT TYPE=submit VALUE=\"Show these logs\">";
97 echo "</FORM>";
98 echo "</CENTER>";
99}
100
600149be 101function make_log_url($module, $url) {
102 switch ($module) {
103 case "course":
104 case "user":
105 case "file":
106 case "login":
107 case "lib":
108 case "admin":
109 return "/$module/$url";
110 break;
111 default:
112 return "/mod/$module/$url";
113 break;
114 }
115}
116
117
f9903ed0 118function print_log($course, $user=0, $date=0, $order="ORDER BY l.time ASC") {
9481285b 119// It is assumed that $date is the GMT time of midnight for that day,
120// and so the next 86400 seconds worth of logs are printed.
f9903ed0 121
720a43ce 122 if ($course->category) {
123 $selector = "WHERE l.course='$course->id' AND l.user = u.id";
124 } else {
125 $selector = "WHERE l.user = u.id"; // Show all courses
126 if ($ccc = get_records_sql("SELECT * FROM course ORDER BY fullname")) {
127 foreach ($ccc as $cc) {
128 $courses[$cc->id] = "$cc->shortname";
129 }
130 }
131 }
f9903ed0 132
133 if ($user) {
134 $selector .= " AND l.user = '$user'";
135 }
136
137 if ($date) {
138 $enddate = $date + 86400;
139 $selector .= " AND l.time > '$date' AND l.time < '$enddate'";
140 }
141
142 if (!$logs = get_records_sql("SELECT l.*, u.firstname, u.lastname, u.picture
600149be 143 FROM log l, user u $selector $order")){
f9903ed0 144 notify("No logs found!");
145 print_footer($course);
146 exit;
147 }
148
149 $count=0;
150 $tt = getdate(time());
151 $today = mktime (0, 0, 0, $tt["mon"], $tt["mday"], $tt["year"]);
152 echo "<P ALIGN=CENTER>Displaying ".count($logs)." records</P>";
153 echo "<TABLE BORDER=0 ALIGN=center CELLPADDING=3 CELLSPACING=3>";
154 foreach ($logs as $log) {
600149be 155
156 if ($ld = get_record_sql("SELECT * FROM log_display WHERE module='$log->module' AND action='$log->action'")) {
157 $log->info = get_field($ld->table, $ld->field, "id", $log->info);
158 }
159
f9903ed0 160 echo "<TR>";
720a43ce 161 if (! $course->category) {
162 echo "<TD><FONT SIZE=2><A HREF=\"view.php?id=$log->course\">".$courses[$log->course]."</A></TD>";
163 }
7a302afc 164 echo "<TD ALIGN=right><FONT SIZE=2>".userdate($log->time, "%A")."</TD>";
165 echo "<TD><FONT SIZE=2>".userdate($log->time, "%e %B %Y, %I:%M %p")."</TD>";
f3ecd2c8 166 echo "<TD><FONT SIZE=2><A TITLE=\"$log->ip\" HREF=\"../user/view.php?id=$log->user&course=$log->course\"><B>$log->firstname $log->lastname</B></TD>";
f9903ed0 167 echo "<TD><FONT SIZE=2>";
600149be 168 link_to_popup_window( make_log_url($log->module,$log->url), "fromloglive","$log->module $log->action", 400, 600);
f9903ed0 169 echo "</TD>";
600149be 170 echo "<TD><FONT SIZE=2>$log->info</TD>";
f9903ed0 171 echo "</TR>";
172 }
173 echo "</TABLE>";
174}
175
176
d887b5a7 177function print_all_courses($cat=1) {
178
179 if ($courses = get_records("course", "category", $cat, "fullname ASC")) {
180 foreach ($courses as $course) {
181 print_course($course);
182 echo "<BR>\n";
183 }
184
185 } else {
186 echo "<H3>No courses have been defined yet</H3>";
187 }
188}
189
190
f9903ed0 191function print_course($course) {
192
d887b5a7 193 global $CFG;
194
a83fded1 195 if (! $site = get_site()) {
f9903ed0 196 error("Could not find a site!");
197 }
198
d887b5a7 199 print_simple_box_start("CENTER", "100%");
f9903ed0 200
201 echo "<TABLE WIDTH=100%>";
da5c172a 202 echo "<TR VALIGN=top>";
203 echo "<TD VALIGN=top WIDTH=50%>";
7a302afc 204 echo "<P><FONT SIZE=3><B><A TITLE=\"".get_string("entercourse")."\"
9481285b 205 HREF=\"$CFG->wwwroot/course/view.php?id=$course->id\">$course->fullname</A></B></FONT></P>";
f9903ed0 206 if ($teachers = get_records_sql("SELECT u.* FROM user u, user_teachers t
207 WHERE u.id = t.user AND t.course = '$course->id'
f51ab26b 208 ORDER BY t.authority ASC")) {
f9903ed0 209
210 echo "<P><FONT SIZE=1>\n";
211 foreach ($teachers as $teacher) {
d887b5a7 212 echo "$course->teacher: <A HREF=\"$CFG->wwwroot/user/view.php?id=$teacher->id&course=$site->id\">$teacher->firstname $teacher->lastname</A><BR>";
f9903ed0 213 }
214 echo "</FONT></P>";
da5c172a 215 }
216 if ($course->guest or ($course->password == "")) {
cbc14629 217 echo "<A TITLE=\"This course allows guest users\" HREF=\"$CFG->wwwroot/course/view.php?id=$course->id\">";
d887b5a7 218 echo "<IMG VSPACE=4 ALT=\"\" HEIGHT=16 WIDTH=16 BORDER=0 SRC=\"$CFG->wwwroot/user/user.gif\"></A>&nbsp;&nbsp;";
da5c172a 219 }
220 if ($course->password) {
cbc14629 221 echo "<A TITLE=\"This course requires an enrolment key\" HREF=\"$CFG->wwwroot/course/view.php?id=$course->id\">";
d887b5a7 222 echo "<IMG VSPACE=4 ALT=\"\" HEIGHT=16 WIDTH=16 BORDER=0 SRC=\"$CFG->wwwroot/pix/i/key.gif\"></A>";
da5c172a 223 }
224
225
226 echo "</TD><TD VALIGN=top WIDTH=50%>";
227 echo "<P><FONT SIZE=2>".text_to_html($course->summary)."</FONT></P>";
228 echo "</TD></TR>";
229 echo "</TABLE>";
f9903ed0 230
da5c172a 231 print_simple_box_end();
f9903ed0 232}
233
600149be 234function print_headline($text, $size=2) {
235 echo "<B><FONT SIZE=\"$size\">$text</FONT></B><BR>\n";
236}
237
238function print_recent_activity($course) {
239 // $course is an object
240 // This function trawls through the logs looking for
241 // anything new since the user's last login
242
243 global $CFG, $USER;
244
245 if (! $USER->lastlogin ) {
246 echo "<P>Welcome to the course! Here you will find a list of what's new since your last login.</P>";
247 return;
248 }
249
250 if (! $logs = get_records_sql("SELECT * FROM log WHERE time > '$USER->lastlogin' AND course = '$course->id' ORDER BY time ASC")) {
251 return;
252 }
253
254
255 // Firstly, have there been any new enrolments?
256
257 $heading = false;
258 $content = false;
259 foreach ($logs as $log) {
260 if ($log->module == "course" and $log->action == "enrol") {
261 if (! $heading) {
ef25340c 262 print_headline("New users:");
600149be 263 $heading = true;
264 $content = true;
265 }
266 $user = get_record("user", "id", $log->info);
ef25340c 267 echo "<P><FONT SIZE=1><A HREF=\"../user/view.php?id=$user->id&course=$course->id\">$user->firstname $user->lastname</A></FONT></P>";
600149be 268 }
269 }
270
271 // Next, have there been any changes to the course structure?
272
600149be 273 foreach ($logs as $log) {
274 if ($log->module == "course") {
275 if ($log->action == "add mod" or $log->action == "update mod" or $log->action == "delete mod") {
600149be 276 $info = split(" ", $log->info);
277 $modname = get_field($info[0], "name", "id", $info[1]);
278
600149be 279 switch ($log->action) {
280 case "add mod":
ef25340c 281 $changelist["$log->info"] = array ("operation" => "add", "text" => "Added a ".$info[0].":<BR><A HREF=\"$CFG->wwwroot/course/$log->url\">$modname</A>");
600149be 282 break;
283 case "update mod":
ef25340c 284 if (! $changelist["$log->info"]) {
285 $changelist["$log->info"] = array ("operation" => "update", "text" => "Updated the ".$info[0].":<BR><A HREF=\"$CFG->wwwroot/course/$log->url\">$modname</A>");
286 }
600149be 287 break;
288 case "delete mod":
ef25340c 289 if ($changelist["$log->info"]["operation"] == "add") {
290 $changelist["$log->info"] = NULL;
291 } else {
292 $changelist["$log->info"] = array ("operation" => "delete", "text" => "Deleted a ".$info[0]);
293 }
600149be 294 break;
295 }
ef25340c 296 }
297 }
298 }
299
300 if ($changelist) {
301 foreach ($changelist as $changeinfo => $change) {
302 if ($change) {
303 $changes[$changeinfo] = $change;
304 }
305 }
306 if (count($changes) > 0) {
307 print_headline("Course changes:");
308 $content = true;
309 foreach ($changes as $changeinfo => $change) {
310 echo "<P><FONT SIZE=1>".$change["text"]."</FONT></P>";
600149be 311 }
312 }
313 }
314
315
316 // Now all we need to know are the new posts.
317
ef25340c 318 $heading = false;
600149be 319 foreach ($logs as $log) {
320
501cdbd8 321 if ($log->module == "forum") {
600149be 322 $post = NULL;
323
36d4a9a1 324 if ($log->action == "add post") {
f9730aef 325 $post = get_record_sql("SELECT p.*, d.forum, u.firstname, u.lastname,
600149be 326 u.email, u.picture, u.id as userid
501cdbd8 327 FROM forum_discussions d, forum_posts p, user u
328 WHERE p.id = '$log->info' AND d.id = p.discussion AND p.user = u.id");
600149be 329
36d4a9a1 330 } else if ($log->action == "add discussion") {
f9730aef 331 $post = get_record_sql("SELECT p.*, d.forum, u.firstname, u.lastname,
600149be 332 u.email, u.picture, u.id as userid
501cdbd8 333 FROM forum_discussions d, forum_posts p, user u
600149be 334 WHERE d.id = '$log->info' AND d.firstpost = p.id AND p.user = u.id");
335 }
336
337 if ($post) {
f9730aef 338
339 $teacherpost = "";
340 if ($forum = get_record("forum", "id", $post->forum) ) {
341 if ($forum->type == "teacher") {
342 if (!isteacher($course->id)) {
343 continue;
344 } else {
345 $teacherpost = "COLOR=#666666";
346 }
347 }
348 }
600149be 349 if (! $heading) {
ef25340c 350 print_headline("Discussion Posts:");
600149be 351 $heading = true;
352 $content = true;
353 }
f9730aef 354 echo "<P><FONT SIZE=1 $teacherpost>$post->firstname $post->lastname:<BR>";
501cdbd8 355 echo "\"<A HREF=\"$CFG->wwwroot/mod/forum/$log->url\">";
600149be 356 if ($log->action == "add") {
ef25340c 357 echo "<B>$post->subject</B>";
600149be 358 } else {
ef25340c 359 echo "$post->subject";
600149be 360 }
ef25340c 361 echo "</A>\"</FONT></P>";
600149be 362 }
363
364 }
365 }
366
367 if (! $content) {
368 echo "<FONT SIZE=2>Nothing new since your last login</FONT>";
369 }
370
371}
372
e1360728 373
374function unenrol_student_in_course($user, $course) {
375 global $db;
376
377 return $db->Execute("DELETE FROM user_students WHERE user = '$user' AND course = '$course'");
378}
379
380
381
382function enrol_student_in_course($user, $course) {
383 global $db;
384
385 $timenow = time();
386
387 $rs = $db->Execute("INSERT INTO user_students (user, course, start, end, time)
388 VALUES ($user, $course, 0, 0, $timenow)");
389 if ($rs) {
390 return true;
391 } else {
392 return false;
393 }
394}
395
7468bf01 396function get_all_mods($courseid, &$mods, &$modtype) {
397
398 $mods = NULL;
399 $modtype = NULL;
400
401 if ( $rawmods = get_records_sql("SELECT cm.*, m.name as modname, m.fullname as modfullname
402 FROM modules m, course_modules cm
403 WHERE cm.course = '$courseid'
404 AND cm.deleted = '0'
405 AND cm.module = m.id") ) {
406 foreach($rawmods as $mod) { // Index the mods
407 $mods[$mod->id] = $mod;
408 $modtype[$mod->modname] = $mod->modfullname;
409 }
410 ksort($modtype);
411 }
412
413}
414
415function get_all_sections($courseid) {
416
417 return get_records_sql("SELECT section, id, course, summary, sequence
418 FROM course_sections
419 WHERE course = '$courseid'
420 ORDER BY section");
421}
422
2b8cef80 423function print_log_graph($course, $userid=0, $type="course.png", $date=0) {
424 global $CFG;
425 echo "<IMG BORDER=0 SRC=\"$CFG->wwwroot/course/loggraph.php?id=$course->id&user=$userid&type=$type&date=$date\">";
426}
427
11b0c469 428
429
430/// MODULE FUNCTIONS /////////////////////////////////////////////////////////////////
431
432function add_course_module($mod) {
433 GLOBAL $db;
434
435 $timenow = time();
436
437 if (!$rs = $db->Execute("INSERT into course_modules
438 SET course = '$mod->course',
439 module = '$mod->module',
440 instance = '$mod->instance',
441 section = '$mod->section',
442 added = '$timenow' ")) {
443 return 0;
444 }
445
446 // Get it out again - this is the most compatible way to determine the ID
447 if ($rs = $db->Execute("SELECT id FROM course_modules
448 WHERE module = $mod->module AND added = $timenow")) {
449 return $rs->fields[0];
450 } else {
451 return 0;
452 }
453
454}
455
456function add_mod_to_section($mod) {
457// Returns the course_sections ID where the mod is inserted
458 GLOBAL $db;
459
460 if ($cw = get_record_sql("SELECT * FROM course_sections
461 WHERE course = '$mod->course' AND section = '$mod->section'") ) {
462
463 if ($cw->sequence) {
464 $newsequence = "$cw->sequence,$mod->coursemodule";
465 } else {
466 $newsequence = "$mod->coursemodule";
467 }
468 if (!$rs = $db->Execute("UPDATE course_sections SET sequence = '$newsequence' WHERE id = '$cw->id'")) {
469 return 0;
470 } else {
471 return $cw->id; // Return course_sections ID that was used.
472 }
473
474 } else { // Insert a new record
475 if (!$rs = $db->Execute("INSERT into course_sections
476 SET course = '$mod->course',
477 section = '$mod->section',
478 summary = '',
479 sequence = '$mod->coursemodule' ")) {
480 return 0;
481 }
482 // Get it out again - this is the most compatible way to determine the ID
483 if ($rs = $db->Execute("SELECT id FROM course_sections
484 WHERE course = '$mod->course' AND section = '$mod->section'")) {
485 return $rs->fields[0];
486 } else {
487 return 0;
488 }
489 }
490}
491
492function delete_course_module($mod) {
493 return set_field("course_modules", "deleted", 1, "id", $mod);
494}
495
496function delete_mod_from_section($mod, $section) {
497 GLOBAL $db;
498
499 if ($cw = get_record("course_sections", "id", "$section") ) {
500
501 $modarray = explode(",", $cw->sequence);
502
503 if ($key = array_keys ($modarray, $mod)) {
504 array_splice($modarray, $key[0], 1);
505 $newsequence = implode(",", $modarray);
506 return set_field("course_sections", "sequence", $newsequence, "id", $cw->id);
507 } else {
508 return false;
509 }
510
511 } else {
512 return false;
513 }
514}
515
516
517function move_module($id, $move) {
518 GLOBAL $db;
519
520 if (!$move) {
521 return true;
522 }
523
524 if (! $cm = get_record("course_modules", "id", $id)) {
525 error("This course module doesn't exist");
526 }
527
528 if (! $thissection = get_record("course_sections", "id", $cm->section)) {
529 error("This course section doesn't exist");
530 }
531
532 $mods = explode(",", $thissection->sequence);
533
534 $len = count($mods);
535 $pos = array_keys($mods, $cm->id);
536 $thepos = $pos[0];
537
538 if ($len == 0 || count($pos) == 0 ) {
539 error("Very strange. Could not find the required module in this section.");
540 }
541
542 if ($len == 1) {
543 $first = true;
544 $last = true;
545 } else {
546 $first = ($thepos == 0);
547 $last = ($thepos == $len - 1);
548 }
549
550 if ($move < 0) { // Moving the module up
551
552 if ($first) {
553 if ($thissection->section == 1) { // First section, do nothing
554 return true;
555 } else { // Push onto end of previous section
556 $prevsectionnumber = $thissection->section - 1;
557 if (! $prevsection = get_record_sql("SELECT * FROM course_sections
558 WHERE course='$thissection->course'
559 AND section='$prevsectionnumber' ")) {
560 error("Previous section ($prevsection->id) doesn't exist");
561 }
562
563 if ($prevsection->sequence) {
564 $newsequence = "$prevsection->sequence,$cm->id";
565 } else {
566 $newsequence = "$cm->id";
567 }
568
569 if (! set_field("course_sections", "sequence", $newsequence, "id", $prevsection->id)) {
570 error("Previous section could not be updated");
571 }
572
573 if (! set_field("course_modules", "section", $prevsection->id, "id", $cm->id)) {
574 error("Module could not be updated");
575 }
576
577 array_splice($mods, 0, 1);
578 $newsequence = implode(",", $mods);
579 if (! set_field("course_sections", "sequence", $newsequence, "id", $thissection->id)) {
580 error("Module could not be updated");
581 }
582
583 return true;
584
585 }
586 } else { // move up within this section
587 $swap = $mods[$thepos-1];
588 $mods[$thepos-1] = $mods[$thepos];
589 $mods[$thepos] = $swap;
590
591 $newsequence = implode(",", $mods);
592 if (! set_field("course_sections", "sequence", $newsequence, "id", $thissection->id)) {
593 error("This section could not be updated");
594 }
595 return true;
596 }
597
598 } else { // Moving the module down
599
600 if ($last) {
601 $nextsectionnumber = $thissection->section + 1;
602 if ($nextsection = get_record_sql("SELECT * FROM course_sections
603 WHERE course='$thissection->course'
604 AND section='$nextsectionnumber' ")) {
605
606 if ($nextsection->sequence) {
607 $newsequence = "$cm->id,$nextsection->sequence";
608 } else {
609 $newsequence = "$cm->id";
610 }
611
612 if (! set_field("course_sections", "sequence", $newsequence, "id", $nextsection->id)) {
613 error("Next section could not be updated");
614 }
615
616 if (! set_field("course_modules", "section", $nextsection->id, "id", $cm->id)) {
617 error("Module could not be updated");
618 }
619
620 array_splice($mods, $thepos, 1);
621 $newsequence = implode(",", $mods);
622 if (! set_field("course_sections", "sequence", $newsequence, "id", $thissection->id)) {
623 error("This section could not be updated");
624 }
625 return true;
626
627 } else { // There is no next section, so just return
628 return true;
629
630 }
631 } else { // move down within this section
632 $swap = $mods[$thepos+1];
633 $mods[$thepos+1] = $mods[$thepos];
634 $mods[$thepos] = $swap;
635
636 $newsequence = implode(",", $mods);
637 if (! set_field("course_sections", "sequence", $newsequence, "id", $thissection->id)) {
638 error("This section could not be updated");
639 }
640 return true;
641 }
642 }
643}
644
645
f9903ed0 646?>