Improvements to the help system
[moodle.git] / course / lib.php
CommitLineData
f9903ed0 1<? // $Id$
2
3$MAXNEWSDISPLAY = 4;
4
5$FORMATS = array (
b5fe4c93 6 "weeks" => "Weekly layout",
7 "social" => "Social layout",
8 "topics" => "Topics layout"
f9903ed0 9 );
10
d9d1c35d 11$SECTION = array (
b5fe4c93 12 "weeks" => "week",
13 "social" => "section",
14 "topics" => "topic"
d9d1c35d 15 );
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
279 if ($info[0] == "discuss") {
83b0b773 280 $info[0] = "discussion"; // nasty hack, really.
600149be 281 }
282
600149be 283 switch ($log->action) {
284 case "add mod":
ef25340c 285 $changelist["$log->info"] = array ("operation" => "add", "text" => "Added a ".$info[0].":<BR><A HREF=\"$CFG->wwwroot/course/$log->url\">$modname</A>");
600149be 286 break;
287 case "update mod":
ef25340c 288 if (! $changelist["$log->info"]) {
289 $changelist["$log->info"] = array ("operation" => "update", "text" => "Updated the ".$info[0].":<BR><A HREF=\"$CFG->wwwroot/course/$log->url\">$modname</A>");
290 }
600149be 291 break;
292 case "delete mod":
ef25340c 293 if ($changelist["$log->info"]["operation"] == "add") {
294 $changelist["$log->info"] = NULL;
295 } else {
296 $changelist["$log->info"] = array ("operation" => "delete", "text" => "Deleted a ".$info[0]);
297 }
600149be 298 break;
299 }
ef25340c 300 }
301 }
302 }
303
304 if ($changelist) {
305 foreach ($changelist as $changeinfo => $change) {
306 if ($change) {
307 $changes[$changeinfo] = $change;
308 }
309 }
310 if (count($changes) > 0) {
311 print_headline("Course changes:");
312 $content = true;
313 foreach ($changes as $changeinfo => $change) {
314 echo "<P><FONT SIZE=1>".$change["text"]."</FONT></P>";
600149be 315 }
316 }
317 }
318
319
320 // Now all we need to know are the new posts.
321
ef25340c 322 $heading = false;
600149be 323 foreach ($logs as $log) {
324
325 if ($log->module == "discuss") {
326 $post = NULL;
327
328 if ($log->action == "add post") {
f9730aef 329 $post = get_record_sql("SELECT p.*, d.forum, u.firstname, u.lastname,
600149be 330 u.email, u.picture, u.id as userid
f9730aef 331 FROM discuss d, discuss_posts p, user u
332 WHERE p.id = '$log->info' AND d.id = p.discuss AND p.user = u.id");
600149be 333
334 } else if ($log->action == "add") {
f9730aef 335 $post = get_record_sql("SELECT p.*, d.forum, u.firstname, u.lastname,
600149be 336 u.email, u.picture, u.id as userid
337 FROM discuss d, discuss_posts p, user u
338 WHERE d.id = '$log->info' AND d.firstpost = p.id AND p.user = u.id");
339 }
340
341 if ($post) {
f9730aef 342
343 $teacherpost = "";
344 if ($forum = get_record("forum", "id", $post->forum) ) {
345 if ($forum->type == "teacher") {
346 if (!isteacher($course->id)) {
347 continue;
348 } else {
349 $teacherpost = "COLOR=#666666";
350 }
351 }
352 }
600149be 353 if (! $heading) {
ef25340c 354 print_headline("Discussion Posts:");
600149be 355 $heading = true;
356 $content = true;
357 }
f9730aef 358 echo "<P><FONT SIZE=1 $teacherpost>$post->firstname $post->lastname:<BR>";
ef25340c 359 echo "\"<A HREF=\"$CFG->wwwroot/mod/discuss/$log->url\">";
600149be 360 if ($log->action == "add") {
ef25340c 361 echo "<B>$post->subject</B>";
600149be 362 } else {
ef25340c 363 echo "$post->subject";
600149be 364 }
ef25340c 365 echo "</A>\"</FONT></P>";
600149be 366 }
367
368 }
369 }
370
371 if (! $content) {
372 echo "<FONT SIZE=2>Nothing new since your last login</FONT>";
373 }
374
375}
376
e1360728 377
378function unenrol_student_in_course($user, $course) {
379 global $db;
380
381 return $db->Execute("DELETE FROM user_students WHERE user = '$user' AND course = '$course'");
382}
383
384
385
386function enrol_student_in_course($user, $course) {
387 global $db;
388
389 $timenow = time();
390
391 $rs = $db->Execute("INSERT INTO user_students (user, course, start, end, time)
392 VALUES ($user, $course, 0, 0, $timenow)");
393 if ($rs) {
394 return true;
395 } else {
396 return false;
397 }
398}
399
f9903ed0 400?>