Changes to allow choice of format on home page of site
[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
d887b5a7 118function print_all_courses($cat=1) {
119
120 if ($courses = get_records("course", "category", $cat, "fullname ASC")) {
121 foreach ($courses as $course) {
122 print_course($course);
123 echo "<BR>\n";
124 }
125
126 } else {
127 echo "<H3>No courses have been defined yet</H3>";
128 }
129}
130
131
f9903ed0 132function print_course($course) {
133
d887b5a7 134 global $CFG;
135
f9903ed0 136 if (! $site = get_record("course", "category", "0") ) {
137 error("Could not find a site!");
138 }
139
d887b5a7 140 print_simple_box_start("CENTER", "100%");
f9903ed0 141
142 echo "<TABLE WIDTH=100%>";
da5c172a 143 echo "<TR VALIGN=top>";
144 echo "<TD VALIGN=top WIDTH=50%>";
d887b5a7 145 echo "<P><FONT SIZE=3><B><A HREF=\"$CFG->wwwroot/course/view.php?id=$course->id\">$course->fullname</A></B></FONT></P>";
f9903ed0 146 if ($teachers = get_records_sql("SELECT u.* FROM user u, user_teachers t
147 WHERE u.id = t.user AND t.course = '$course->id'
f51ab26b 148 ORDER BY t.authority ASC")) {
f9903ed0 149
150 echo "<P><FONT SIZE=1>\n";
151 foreach ($teachers as $teacher) {
d887b5a7 152 echo "$course->teacher: <A HREF=\"$CFG->wwwroot/user/view.php?id=$teacher->id&course=$site->id\">$teacher->firstname $teacher->lastname</A><BR>";
f9903ed0 153 }
154 echo "</FONT></P>";
da5c172a 155 }
156 if ($course->guest or ($course->password == "")) {
d887b5a7 157 echo "<A TITLE=\"Guest user allowed\" HREF=\"$CFG->wwwroot/course/view.php?id=$course->id\">";
158 echo "<IMG VSPACE=4 ALT=\"\" HEIGHT=16 WIDTH=16 BORDER=0 SRC=\"$CFG->wwwroot/user/user.gif\"></A>&nbsp;&nbsp;";
da5c172a 159 }
160 if ($course->password) {
d887b5a7 161 echo "<A TITLE=\"Requires a Course entry key\" HREF=\"$CFG->wwwroot/course/view.php?id=$course->id\">";
162 echo "<IMG VSPACE=4 ALT=\"\" HEIGHT=16 WIDTH=16 BORDER=0 SRC=\"$CFG->wwwroot/pix/i/key.gif\"></A>";
da5c172a 163 }
164
165
166 echo "</TD><TD VALIGN=top WIDTH=50%>";
167 echo "<P><FONT SIZE=2>".text_to_html($course->summary)."</FONT></P>";
168 echo "</TD></TR>";
169 echo "</TABLE>";
f9903ed0 170
da5c172a 171 print_simple_box_end();
f9903ed0 172}
173
600149be 174function print_headline($text, $size=2) {
175 echo "<B><FONT SIZE=\"$size\">$text</FONT></B><BR>\n";
176}
177
178function print_recent_activity($course) {
179 // $course is an object
180 // This function trawls through the logs looking for
181 // anything new since the user's last login
182
183 global $CFG, $USER;
184
185 if (! $USER->lastlogin ) {
186 echo "<P>Welcome to the course! Here you will find a list of what's new since your last login.</P>";
187 return;
188 }
189
190 if (! $logs = get_records_sql("SELECT * FROM log WHERE time > '$USER->lastlogin' AND course = '$course->id' ORDER BY time ASC")) {
191 return;
192 }
193
194
195 // Firstly, have there been any new enrolments?
196
197 $heading = false;
198 $content = false;
199 foreach ($logs as $log) {
200 if ($log->module == "course" and $log->action == "enrol") {
201 if (! $heading) {
ef25340c 202 print_headline("New users:");
600149be 203 $heading = true;
204 $content = true;
205 }
206 $user = get_record("user", "id", $log->info);
ef25340c 207 echo "<P><FONT SIZE=1><A HREF=\"../user/view.php?id=$user->id&course=$course->id\">$user->firstname $user->lastname</A></FONT></P>";
600149be 208 }
209 }
210
211 // Next, have there been any changes to the course structure?
212
600149be 213 foreach ($logs as $log) {
214 if ($log->module == "course") {
215 if ($log->action == "add mod" or $log->action == "update mod" or $log->action == "delete mod") {
600149be 216 $info = split(" ", $log->info);
217 $modname = get_field($info[0], "name", "id", $info[1]);
218
219 if ($info[0] == "discuss") {
83b0b773 220 $info[0] = "discussion"; // nasty hack, really.
600149be 221 }
222
600149be 223 switch ($log->action) {
224 case "add mod":
ef25340c 225 $changelist["$log->info"] = array ("operation" => "add", "text" => "Added a ".$info[0].":<BR><A HREF=\"$CFG->wwwroot/course/$log->url\">$modname</A>");
600149be 226 break;
227 case "update mod":
ef25340c 228 if (! $changelist["$log->info"]) {
229 $changelist["$log->info"] = array ("operation" => "update", "text" => "Updated the ".$info[0].":<BR><A HREF=\"$CFG->wwwroot/course/$log->url\">$modname</A>");
230 }
600149be 231 break;
232 case "delete mod":
ef25340c 233 if ($changelist["$log->info"]["operation"] == "add") {
234 $changelist["$log->info"] = NULL;
235 } else {
236 $changelist["$log->info"] = array ("operation" => "delete", "text" => "Deleted a ".$info[0]);
237 }
600149be 238 break;
239 }
ef25340c 240 }
241 }
242 }
243
244 if ($changelist) {
245 foreach ($changelist as $changeinfo => $change) {
246 if ($change) {
247 $changes[$changeinfo] = $change;
248 }
249 }
250 if (count($changes) > 0) {
251 print_headline("Course changes:");
252 $content = true;
253 foreach ($changes as $changeinfo => $change) {
254 echo "<P><FONT SIZE=1>".$change["text"]."</FONT></P>";
600149be 255 }
256 }
257 }
258
259
260 // Now all we need to know are the new posts.
261
ef25340c 262 $heading = false;
600149be 263 foreach ($logs as $log) {
264
265 if ($log->module == "discuss") {
266 $post = NULL;
267
268 if ($log->action == "add post") {
269 $post = get_record_sql("SELECT p.*, u.firstname, u.lastname,
270 u.email, u.picture, u.id as userid
271 FROM discuss_posts p, user u
272 WHERE p.id = '$log->info' AND p.user = u.id");
273
274 } else if ($log->action == "add") {
275 $post = get_record_sql("SELECT p.*, u.firstname, u.lastname,
276 u.email, u.picture, u.id as userid
277 FROM discuss d, discuss_posts p, user u
278 WHERE d.id = '$log->info' AND d.firstpost = p.id AND p.user = u.id");
279 }
280
281 if ($post) {
282 if (! $heading) {
ef25340c 283 print_headline("Discussion Posts:");
600149be 284 $heading = true;
285 $content = true;
286 }
ef25340c 287 echo "<P><FONT SIZE=1>$post->firstname $post->lastname:<BR>";
288 echo "\"<A HREF=\"$CFG->wwwroot/mod/discuss/$log->url\">";
600149be 289 if ($log->action == "add") {
ef25340c 290 echo "<B>$post->subject</B>";
600149be 291 } else {
ef25340c 292 echo "$post->subject";
600149be 293 }
ef25340c 294 echo "</A>\"</FONT></P>";
600149be 295 }
296
297 }
298 }
299
300 if (! $content) {
301 echo "<FONT SIZE=2>Nothing new since your last login</FONT>";
302 }
303
304}
305
e1360728 306
307function unenrol_student_in_course($user, $course) {
308 global $db;
309
310 return $db->Execute("DELETE FROM user_students WHERE user = '$user' AND course = '$course'");
311}
312
313
314
315function enrol_student_in_course($user, $course) {
316 global $db;
317
318 $timenow = time();
319
320 $rs = $db->Execute("INSERT INTO user_students (user, course, start, end, time)
321 VALUES ($user, $course, 0, 0, $timenow)");
322 if ($rs) {
323 return true;
324 } else {
325 return false;
326 }
327}
328
f9903ed0 329?>