Don't show overall graph when no students have completed survey
[moodle.git] / course / lib.php
CommitLineData
f9903ed0 1<? // $Id$
2
3$MAXNEWSDISPLAY = 4;
4
5$FORMATS = array (
4567fb71 6 "1" => "Weekly layout",
0b35af18 7 "2" => "Social layout",
8 "3" => "Topics layout"
f9903ed0 9 );
10
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
61 $dates = array("$timemidnight" => "Today, ".userdate($timenow, "j F Y") );
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;
71 $dates["$timemidnight"] = userdate($timenow, "l, j F Y");
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) {
87 choose_from_menu ($users, "user", $selecteduser, "All participants");
88 }
f9903ed0 89 choose_from_menu ($dates, "date", $selecteddate, "Any day");
90 echo "<INPUT TYPE=submit VALUE=\"Show these logs\">";
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";
118 } else {
119 $selector = "WHERE l.user = u.id"; // Show all courses
120 if ($ccc = get_records_sql("SELECT * FROM course ORDER BY fullname")) {
121 foreach ($ccc as $cc) {
122 $courses[$cc->id] = "$cc->shortname";
123 }
124 }
125 }
f9903ed0 126
127 if ($user) {
128 $selector .= " AND l.user = '$user'";
129 }
130
131 if ($date) {
132 $enddate = $date + 86400;
133 $selector .= " AND l.time > '$date' AND l.time < '$enddate'";
134 }
135
136 if (!$logs = get_records_sql("SELECT l.*, u.firstname, u.lastname, u.picture
600149be 137 FROM log l, user u $selector $order")){
f9903ed0 138 notify("No logs found!");
139 print_footer($course);
140 exit;
141 }
142
143 $count=0;
144 $tt = getdate(time());
145 $today = mktime (0, 0, 0, $tt["mon"], $tt["mday"], $tt["year"]);
146 echo "<P ALIGN=CENTER>Displaying ".count($logs)." records</P>";
147 echo "<TABLE BORDER=0 ALIGN=center CELLPADDING=3 CELLSPACING=3>";
148 foreach ($logs as $log) {
600149be 149
150 if ($ld = get_record_sql("SELECT * FROM log_display WHERE module='$log->module' AND action='$log->action'")) {
151 $log->info = get_field($ld->table, $ld->field, "id", $log->info);
152 }
153
f9903ed0 154 echo "<TR>";
720a43ce 155 if (! $course->category) {
156 echo "<TD><FONT SIZE=2><A HREF=\"view.php?id=$log->course\">".$courses[$log->course]."</A></TD>";
157 }
873960de 158 echo "<TD ALIGN=right><FONT SIZE=2>".userdate($log->time, "l")."</TD>";
159 echo "<TD><FONT SIZE=2>".userdate($log->time, "j M Y, h:i A")."</TD>";
f3ecd2c8 160 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 161 echo "<TD><FONT SIZE=2>";
600149be 162 link_to_popup_window( make_log_url($log->module,$log->url), "fromloglive","$log->module $log->action", 400, 600);
f9903ed0 163 echo "</TD>";
600149be 164 echo "<TD><FONT SIZE=2>$log->info</TD>";
f9903ed0 165 echo "</TR>";
166 }
167 echo "</TABLE>";
168}
169
170
d887b5a7 171function print_all_courses($cat=1) {
172
173 if ($courses = get_records("course", "category", $cat, "fullname ASC")) {
174 foreach ($courses as $course) {
175 print_course($course);
176 echo "<BR>\n";
177 }
178
179 } else {
180 echo "<H3>No courses have been defined yet</H3>";
181 }
182}
183
184
f9903ed0 185function print_course($course) {
186
d887b5a7 187 global $CFG;
188
f9903ed0 189 if (! $site = get_record("course", "category", "0") ) {
190 error("Could not find a site!");
191 }
192
d887b5a7 193 print_simple_box_start("CENTER", "100%");
f9903ed0 194
195 echo "<TABLE WIDTH=100%>";
da5c172a 196 echo "<TR VALIGN=top>";
197 echo "<TD VALIGN=top WIDTH=50%>";
9481285b 198 echo "<P><FONT SIZE=3><B><A TITLE=\"Click to enter this course\"
199 HREF=\"$CFG->wwwroot/course/view.php?id=$course->id\">$course->fullname</A></B></FONT></P>";
f9903ed0 200 if ($teachers = get_records_sql("SELECT u.* FROM user u, user_teachers t
201 WHERE u.id = t.user AND t.course = '$course->id'
f51ab26b 202 ORDER BY t.authority ASC")) {
f9903ed0 203
204 echo "<P><FONT SIZE=1>\n";
205 foreach ($teachers as $teacher) {
d887b5a7 206 echo "$course->teacher: <A HREF=\"$CFG->wwwroot/user/view.php?id=$teacher->id&course=$site->id\">$teacher->firstname $teacher->lastname</A><BR>";
f9903ed0 207 }
208 echo "</FONT></P>";
da5c172a 209 }
210 if ($course->guest or ($course->password == "")) {
cbc14629 211 echo "<A TITLE=\"This course allows guest users\" HREF=\"$CFG->wwwroot/course/view.php?id=$course->id\">";
d887b5a7 212 echo "<IMG VSPACE=4 ALT=\"\" HEIGHT=16 WIDTH=16 BORDER=0 SRC=\"$CFG->wwwroot/user/user.gif\"></A>&nbsp;&nbsp;";
da5c172a 213 }
214 if ($course->password) {
cbc14629 215 echo "<A TITLE=\"This course requires an enrolment key\" HREF=\"$CFG->wwwroot/course/view.php?id=$course->id\">";
d887b5a7 216 echo "<IMG VSPACE=4 ALT=\"\" HEIGHT=16 WIDTH=16 BORDER=0 SRC=\"$CFG->wwwroot/pix/i/key.gif\"></A>";
da5c172a 217 }
218
219
220 echo "</TD><TD VALIGN=top WIDTH=50%>";
221 echo "<P><FONT SIZE=2>".text_to_html($course->summary)."</FONT></P>";
222 echo "</TD></TR>";
223 echo "</TABLE>";
f9903ed0 224
da5c172a 225 print_simple_box_end();
f9903ed0 226}
227
600149be 228function print_headline($text, $size=2) {
229 echo "<B><FONT SIZE=\"$size\">$text</FONT></B><BR>\n";
230}
231
232function print_recent_activity($course) {
233 // $course is an object
234 // This function trawls through the logs looking for
235 // anything new since the user's last login
236
237 global $CFG, $USER;
238
239 if (! $USER->lastlogin ) {
240 echo "<P>Welcome to the course! Here you will find a list of what's new since your last login.</P>";
241 return;
242 }
243
244 if (! $logs = get_records_sql("SELECT * FROM log WHERE time > '$USER->lastlogin' AND course = '$course->id' ORDER BY time ASC")) {
245 return;
246 }
247
248
249 // Firstly, have there been any new enrolments?
250
251 $heading = false;
252 $content = false;
253 foreach ($logs as $log) {
254 if ($log->module == "course" and $log->action == "enrol") {
255 if (! $heading) {
ef25340c 256 print_headline("New users:");
600149be 257 $heading = true;
258 $content = true;
259 }
260 $user = get_record("user", "id", $log->info);
ef25340c 261 echo "<P><FONT SIZE=1><A HREF=\"../user/view.php?id=$user->id&course=$course->id\">$user->firstname $user->lastname</A></FONT></P>";
600149be 262 }
263 }
264
265 // Next, have there been any changes to the course structure?
266
600149be 267 foreach ($logs as $log) {
268 if ($log->module == "course") {
269 if ($log->action == "add mod" or $log->action == "update mod" or $log->action == "delete mod") {
600149be 270 $info = split(" ", $log->info);
271 $modname = get_field($info[0], "name", "id", $info[1]);
272
273 if ($info[0] == "discuss") {
83b0b773 274 $info[0] = "discussion"; // nasty hack, really.
600149be 275 }
276
600149be 277 switch ($log->action) {
278 case "add mod":
ef25340c 279 $changelist["$log->info"] = array ("operation" => "add", "text" => "Added a ".$info[0].":<BR><A HREF=\"$CFG->wwwroot/course/$log->url\">$modname</A>");
600149be 280 break;
281 case "update mod":
ef25340c 282 if (! $changelist["$log->info"]) {
283 $changelist["$log->info"] = array ("operation" => "update", "text" => "Updated the ".$info[0].":<BR><A HREF=\"$CFG->wwwroot/course/$log->url\">$modname</A>");
284 }
600149be 285 break;
286 case "delete mod":
ef25340c 287 if ($changelist["$log->info"]["operation"] == "add") {
288 $changelist["$log->info"] = NULL;
289 } else {
290 $changelist["$log->info"] = array ("operation" => "delete", "text" => "Deleted a ".$info[0]);
291 }
600149be 292 break;
293 }
ef25340c 294 }
295 }
296 }
297
298 if ($changelist) {
299 foreach ($changelist as $changeinfo => $change) {
300 if ($change) {
301 $changes[$changeinfo] = $change;
302 }
303 }
304 if (count($changes) > 0) {
305 print_headline("Course changes:");
306 $content = true;
307 foreach ($changes as $changeinfo => $change) {
308 echo "<P><FONT SIZE=1>".$change["text"]."</FONT></P>";
600149be 309 }
310 }
311 }
312
313
314 // Now all we need to know are the new posts.
315
ef25340c 316 $heading = false;
600149be 317 foreach ($logs as $log) {
318
319 if ($log->module == "discuss") {
320 $post = NULL;
321
322 if ($log->action == "add post") {
323 $post = get_record_sql("SELECT p.*, u.firstname, u.lastname,
324 u.email, u.picture, u.id as userid
325 FROM discuss_posts p, user u
326 WHERE p.id = '$log->info' AND p.user = u.id");
327
328 } else if ($log->action == "add") {
329 $post = get_record_sql("SELECT p.*, u.firstname, u.lastname,
330 u.email, u.picture, u.id as userid
331 FROM discuss d, discuss_posts p, user u
332 WHERE d.id = '$log->info' AND d.firstpost = p.id AND p.user = u.id");
333 }
334
335 if ($post) {
336 if (! $heading) {
ef25340c 337 print_headline("Discussion Posts:");
600149be 338 $heading = true;
339 $content = true;
340 }
ef25340c 341 echo "<P><FONT SIZE=1>$post->firstname $post->lastname:<BR>";
342 echo "\"<A HREF=\"$CFG->wwwroot/mod/discuss/$log->url\">";
600149be 343 if ($log->action == "add") {
ef25340c 344 echo "<B>$post->subject</B>";
600149be 345 } else {
ef25340c 346 echo "$post->subject";
600149be 347 }
ef25340c 348 echo "</A>\"</FONT></P>";
600149be 349 }
350
351 }
352 }
353
354 if (! $content) {
355 echo "<FONT SIZE=2>Nothing new since your last login</FONT>";
356 }
357
358}
359
e1360728 360
361function unenrol_student_in_course($user, $course) {
362 global $db;
363
364 return $db->Execute("DELETE FROM user_students WHERE user = '$user' AND course = '$course'");
365}
366
367
368
369function enrol_student_in_course($user, $course) {
370 global $db;
371
372 $timenow = time();
373
374 $rs = $db->Execute("INSERT INTO user_students (user, course, start, end, time)
375 VALUES ($user, $course, 0, 0, $timenow)");
376 if ($rs) {
377 return true;
378 } else {
379 return false;
380 }
381}
382
f9903ed0 383?>