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