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