Fixes to usergetdate() ... looks OK now.
[moodle.git] / course / lib.php
CommitLineData
f9903ed0 1<? // $Id$
2
3$MAXNEWSDISPLAY = 4;
4
5$FORMATS = array (
4567fb71 6 "1" => "Weekly layout",
7 "2" => "Social layout"
f9903ed0 8 );
9
10
f9903ed0 11function print_log_selector_form($course, $selecteduser=0, $selecteddate="today") {
12
9481285b 13 global $USER, $CFG;
14
f9903ed0 15 // Get all the possible users
16 $users = array();
17 if ($students = get_records_sql("SELECT u.* FROM user u, user_students s
18 WHERE s.course = '$course->id' AND s.user = u.id
19 ORDER BY u.lastaccess DESC")) {
20 foreach ($students as $student) {
21 $users["$student->id"] = "$student->firstname $student->lastname";
22 }
23 }
24 if ($teachers = get_records_sql("SELECT u.* FROM user u, user_teachers t
25 WHERE t.course = '$course->id' AND t.user = u.id
26 ORDER BY u.lastaccess DESC")) {
27 foreach ($teachers as $teacher) {
28 $users["$teacher->id"] = "$teacher->firstname $teacher->lastname";
29 }
30 }
31
32 asort($users);
33
34 // Get all the possible dates
9481285b 35 // Note that we are keeping track of real (GMT) time and user time
36 // User time is only used in displays - all calcs and passing is GMT
37
38 $timenow = time(); // GMT
39
40 // What day is it now for the user, and when is midnight that day (in GMT).
edf7fe8c 41 $timemidnight = usergetmidnight($timenow);
9481285b 42
43 // Put today up the top of the list
44 $dates = array("$timemidnight" => "Today, ".userdate($timenow, "j F Y") );
45
46 if (! $course->startdate) {
47 $course->startdate = $course->timecreated;
48 }
f9903ed0 49
9481285b 50 $numdates = 1;
51 while ($timemidnight > $course->startdate and $numdates < 365) {
f9903ed0 52 $timemidnight = $timemidnight - 86400;
9481285b 53 $timenow = $timenow - 86400;
54 $dates["$timemidnight"] = userdate($timenow, "l, j F Y");
55 $numdates++;
f9903ed0 56 }
57
58 if ($selecteddate == "today") {
59 $selecteddate = $today;
60 }
61
62 echo "<CENTER>";
63 echo "<FORM ACTION=log.php METHOD=get>";
64 echo "<INPUT TYPE=hidden NAME=id VALUE=\"$course->id\">";
65 choose_from_menu ($users, "user", $selecteduser, "All participants");
66 choose_from_menu ($dates, "date", $selecteddate, "Any day");
67 echo "<INPUT TYPE=submit VALUE=\"Show these logs\">";
68 echo "</FORM>";
69 echo "</CENTER>";
70}
71
600149be 72function make_log_url($module, $url) {
73 switch ($module) {
74 case "course":
75 case "user":
76 case "file":
77 case "login":
78 case "lib":
79 case "admin":
80 return "/$module/$url";
81 break;
82 default:
83 return "/mod/$module/$url";
84 break;
85 }
86}
87
88
f9903ed0 89function print_log($course, $user=0, $date=0, $order="ORDER BY l.time ASC") {
9481285b 90// It is assumed that $date is the GMT time of midnight for that day,
91// and so the next 86400 seconds worth of logs are printed.
f9903ed0 92
93 $selector = "WHERE l.course='$course->id' AND l.user = u.id";
94
95 if ($user) {
96 $selector .= " AND l.user = '$user'";
97 }
98
99 if ($date) {
100 $enddate = $date + 86400;
101 $selector .= " AND l.time > '$date' AND l.time < '$enddate'";
102 }
103
104 if (!$logs = get_records_sql("SELECT l.*, u.firstname, u.lastname, u.picture
600149be 105 FROM log l, user u $selector $order")){
f9903ed0 106 notify("No logs found!");
107 print_footer($course);
108 exit;
109 }
110
111 $count=0;
112 $tt = getdate(time());
113 $today = mktime (0, 0, 0, $tt["mon"], $tt["mday"], $tt["year"]);
114 echo "<P ALIGN=CENTER>Displaying ".count($logs)." records</P>";
115 echo "<TABLE BORDER=0 ALIGN=center CELLPADDING=3 CELLSPACING=3>";
116 foreach ($logs as $log) {
600149be 117
118 if ($ld = get_record_sql("SELECT * FROM log_display WHERE module='$log->module' AND action='$log->action'")) {
119 $log->info = get_field($ld->table, $ld->field, "id", $log->info);
120 }
121
f9903ed0 122 echo "<TR>";
873960de 123 echo "<TD ALIGN=right><FONT SIZE=2>".userdate($log->time, "l")."</TD>";
124 echo "<TD><FONT SIZE=2>".userdate($log->time, "j M Y, h:i A")."</TD>";
f3ecd2c8 125 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 126 echo "<TD><FONT SIZE=2>";
600149be 127 link_to_popup_window( make_log_url($log->module,$log->url), "fromloglive","$log->module $log->action", 400, 600);
f9903ed0 128 echo "</TD>";
600149be 129 echo "<TD><FONT SIZE=2>$log->info</TD>";
f9903ed0 130 echo "</TR>";
131 }
132 echo "</TABLE>";
133}
134
135
d887b5a7 136function print_all_courses($cat=1) {
137
138 if ($courses = get_records("course", "category", $cat, "fullname ASC")) {
139 foreach ($courses as $course) {
140 print_course($course);
141 echo "<BR>\n";
142 }
143
144 } else {
145 echo "<H3>No courses have been defined yet</H3>";
146 }
147}
148
149
f9903ed0 150function print_course($course) {
151
d887b5a7 152 global $CFG;
153
f9903ed0 154 if (! $site = get_record("course", "category", "0") ) {
155 error("Could not find a site!");
156 }
157
d887b5a7 158 print_simple_box_start("CENTER", "100%");
f9903ed0 159
160 echo "<TABLE WIDTH=100%>";
da5c172a 161 echo "<TR VALIGN=top>";
162 echo "<TD VALIGN=top WIDTH=50%>";
9481285b 163 echo "<P><FONT SIZE=3><B><A TITLE=\"Click to enter this course\"
164 HREF=\"$CFG->wwwroot/course/view.php?id=$course->id\">$course->fullname</A></B></FONT></P>";
f9903ed0 165 if ($teachers = get_records_sql("SELECT u.* FROM user u, user_teachers t
166 WHERE u.id = t.user AND t.course = '$course->id'
f51ab26b 167 ORDER BY t.authority ASC")) {
f9903ed0 168
169 echo "<P><FONT SIZE=1>\n";
170 foreach ($teachers as $teacher) {
d887b5a7 171 echo "$course->teacher: <A HREF=\"$CFG->wwwroot/user/view.php?id=$teacher->id&course=$site->id\">$teacher->firstname $teacher->lastname</A><BR>";
f9903ed0 172 }
173 echo "</FONT></P>";
da5c172a 174 }
175 if ($course->guest or ($course->password == "")) {
cbc14629 176 echo "<A TITLE=\"This course allows guest users\" HREF=\"$CFG->wwwroot/course/view.php?id=$course->id\">";
d887b5a7 177 echo "<IMG VSPACE=4 ALT=\"\" HEIGHT=16 WIDTH=16 BORDER=0 SRC=\"$CFG->wwwroot/user/user.gif\"></A>&nbsp;&nbsp;";
da5c172a 178 }
179 if ($course->password) {
cbc14629 180 echo "<A TITLE=\"This course requires an enrolment key\" HREF=\"$CFG->wwwroot/course/view.php?id=$course->id\">";
d887b5a7 181 echo "<IMG VSPACE=4 ALT=\"\" HEIGHT=16 WIDTH=16 BORDER=0 SRC=\"$CFG->wwwroot/pix/i/key.gif\"></A>";
da5c172a 182 }
183
184
185 echo "</TD><TD VALIGN=top WIDTH=50%>";
186 echo "<P><FONT SIZE=2>".text_to_html($course->summary)."</FONT></P>";
187 echo "</TD></TR>";
188 echo "</TABLE>";
f9903ed0 189
da5c172a 190 print_simple_box_end();
f9903ed0 191}
192
600149be 193function print_headline($text, $size=2) {
194 echo "<B><FONT SIZE=\"$size\">$text</FONT></B><BR>\n";
195}
196
197function print_recent_activity($course) {
198 // $course is an object
199 // This function trawls through the logs looking for
200 // anything new since the user's last login
201
202 global $CFG, $USER;
203
204 if (! $USER->lastlogin ) {
205 echo "<P>Welcome to the course! Here you will find a list of what's new since your last login.</P>";
206 return;
207 }
208
209 if (! $logs = get_records_sql("SELECT * FROM log WHERE time > '$USER->lastlogin' AND course = '$course->id' ORDER BY time ASC")) {
210 return;
211 }
212
213
214 // Firstly, have there been any new enrolments?
215
216 $heading = false;
217 $content = false;
218 foreach ($logs as $log) {
219 if ($log->module == "course" and $log->action == "enrol") {
220 if (! $heading) {
ef25340c 221 print_headline("New users:");
600149be 222 $heading = true;
223 $content = true;
224 }
225 $user = get_record("user", "id", $log->info);
ef25340c 226 echo "<P><FONT SIZE=1><A HREF=\"../user/view.php?id=$user->id&course=$course->id\">$user->firstname $user->lastname</A></FONT></P>";
600149be 227 }
228 }
229
230 // Next, have there been any changes to the course structure?
231
600149be 232 foreach ($logs as $log) {
233 if ($log->module == "course") {
234 if ($log->action == "add mod" or $log->action == "update mod" or $log->action == "delete mod") {
600149be 235 $info = split(" ", $log->info);
236 $modname = get_field($info[0], "name", "id", $info[1]);
237
238 if ($info[0] == "discuss") {
83b0b773 239 $info[0] = "discussion"; // nasty hack, really.
600149be 240 }
241
600149be 242 switch ($log->action) {
243 case "add mod":
ef25340c 244 $changelist["$log->info"] = array ("operation" => "add", "text" => "Added a ".$info[0].":<BR><A HREF=\"$CFG->wwwroot/course/$log->url\">$modname</A>");
600149be 245 break;
246 case "update mod":
ef25340c 247 if (! $changelist["$log->info"]) {
248 $changelist["$log->info"] = array ("operation" => "update", "text" => "Updated the ".$info[0].":<BR><A HREF=\"$CFG->wwwroot/course/$log->url\">$modname</A>");
249 }
600149be 250 break;
251 case "delete mod":
ef25340c 252 if ($changelist["$log->info"]["operation"] == "add") {
253 $changelist["$log->info"] = NULL;
254 } else {
255 $changelist["$log->info"] = array ("operation" => "delete", "text" => "Deleted a ".$info[0]);
256 }
600149be 257 break;
258 }
ef25340c 259 }
260 }
261 }
262
263 if ($changelist) {
264 foreach ($changelist as $changeinfo => $change) {
265 if ($change) {
266 $changes[$changeinfo] = $change;
267 }
268 }
269 if (count($changes) > 0) {
270 print_headline("Course changes:");
271 $content = true;
272 foreach ($changes as $changeinfo => $change) {
273 echo "<P><FONT SIZE=1>".$change["text"]."</FONT></P>";
600149be 274 }
275 }
276 }
277
278
279 // Now all we need to know are the new posts.
280
ef25340c 281 $heading = false;
600149be 282 foreach ($logs as $log) {
283
284 if ($log->module == "discuss") {
285 $post = NULL;
286
287 if ($log->action == "add post") {
288 $post = get_record_sql("SELECT p.*, u.firstname, u.lastname,
289 u.email, u.picture, u.id as userid
290 FROM discuss_posts p, user u
291 WHERE p.id = '$log->info' AND p.user = u.id");
292
293 } else if ($log->action == "add") {
294 $post = get_record_sql("SELECT p.*, u.firstname, u.lastname,
295 u.email, u.picture, u.id as userid
296 FROM discuss d, discuss_posts p, user u
297 WHERE d.id = '$log->info' AND d.firstpost = p.id AND p.user = u.id");
298 }
299
300 if ($post) {
301 if (! $heading) {
ef25340c 302 print_headline("Discussion Posts:");
600149be 303 $heading = true;
304 $content = true;
305 }
ef25340c 306 echo "<P><FONT SIZE=1>$post->firstname $post->lastname:<BR>";
307 echo "\"<A HREF=\"$CFG->wwwroot/mod/discuss/$log->url\">";
600149be 308 if ($log->action == "add") {
ef25340c 309 echo "<B>$post->subject</B>";
600149be 310 } else {
ef25340c 311 echo "$post->subject";
600149be 312 }
ef25340c 313 echo "</A>\"</FONT></P>";
600149be 314 }
315
316 }
317 }
318
319 if (! $content) {
320 echo "<FONT SIZE=2>Nothing new since your last login</FONT>";
321 }
322
323}
324
e1360728 325
326function unenrol_student_in_course($user, $course) {
327 global $db;
328
329 return $db->Execute("DELETE FROM user_students WHERE user = '$user' AND course = '$course'");
330}
331
332
333
334function enrol_student_in_course($user, $course) {
335 global $db;
336
337 $timenow = time();
338
339 $rs = $db->Execute("INSERT INTO user_students (user, course, start, end, time)
340 VALUES ($user, $course, 0, 0, $timenow)");
341 if ($rs) {
342 return true;
343 } else {
344 return false;
345 }
346}
347
f9903ed0 348?>