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