e4027ac9 |
1 | <?php // $Id$ |
97c270e9 |
2 | // Library of useful functions |
f9903ed0 |
3 | |
f9903ed0 |
4 | |
3d891989 |
5 | if (defined('COURSE_MAX_LOG_DISPLAY')) { // Being included again - should never happen!! |
9ae687af |
6 | return; |
7 | } |
8 | |
3d891989 |
9 | define('COURSE_MAX_LOG_DISPLAY', 150); // days |
d9d1c35d |
10 | |
3d891989 |
11 | define('COURSE_MAX_LOGS_PER_PAGE', 1000); // records |
1c0200e0 |
12 | |
3d891989 |
13 | define('COURSE_LIVELOG_REFRESH', 60); // Seconds |
87ffed0d |
14 | |
9e51847a |
15 | define('COURSE_MAX_RECENT_PERIOD', 172800); // Two days, in seconds |
cb29b020 |
16 | |
17 | define('COURSE_MAX_SUMMARIES_PER_PAGE', 10); // courses |
ef58b822 |
18 | |
950c35a9 |
19 | define('COURSE_MAX_COURSES_PER_DROPDOWN',1000); // max courses in log dropdown before switching to optional |
60fdc714 |
20 | |
950c35a9 |
21 | define('COURSE_MAX_USERS_PER_DROPDOWN',1000); // max users in log dropdown before switching to optional |
60fdc714 |
22 | |
c2cb4545 |
23 | define("FRONTPAGENEWS", 0); |
24 | define("FRONTPAGECOURSELIST", 1); |
25 | define("FRONTPAGECATEGORYNAMES", 2); |
5eafd948 |
26 | define("FRONTPAGETOPICONLY", 3); |
f9903ed0 |
27 | |
587510be |
28 | function print_recent_selector_form($course, $advancedfilter=0, $selecteduser=0, $selecteddate="lastlogin", |
4581271a |
29 | $mod="", $modid="activity/All", $modaction="", $selectedgroup="", $selectedsort="default") { |
cb83c3cb |
30 | |
31 | global $USER, $CFG; |
32 | |
cb83c3cb |
33 | $isteacher = isteacher($course->id); |
587510be |
34 | if ($advancedfilter) { |
35 | |
36 | // Get all the possible users |
37 | $users = array(); |
89adb174 |
38 | |
65ee9c16 |
39 | if ($courseusers = get_course_users($course->id, '', '', 'u.id, u.firstname, u.lastname')) { |
587510be |
40 | foreach ($courseusers as $courseuser) { |
41 | $users[$courseuser->id] = fullname($courseuser, $isteacher); |
42 | } |
cb83c3cb |
43 | } |
587510be |
44 | if ($guest = get_guest()) { |
45 | $users[$guest->id] = fullname($guest); |
46 | } |
89adb174 |
47 | |
587510be |
48 | if (isadmin()) { |
49 | if ($ccc = get_records("course", "", "", "fullname")) { |
50 | foreach ($ccc as $cc) { |
51 | if ($cc->category) { |
52 | $courses["$cc->id"] = "$cc->fullname"; |
53 | } else { |
54 | $courses["$cc->id"] = " $cc->fullname (Site)"; |
89adb174 |
55 | } |
587510be |
56 | } |
cb83c3cb |
57 | } |
587510be |
58 | asort($courses); |
59 | } |
4581271a |
60 | |
587510be |
61 | $activities = array(); |
cb83c3cb |
62 | |
587510be |
63 | $selectedactivity = $modid; |
4581271a |
64 | |
587510be |
65 | if ($modinfo = unserialize($course->modinfo)) { |
66 | $section = 0; |
67 | if ($course->format == 'weeks') { // Body |
68 | $strsection = get_string("week"); |
69 | } else { |
70 | $strsection = get_string("topic"); |
cb83c3cb |
71 | } |
cb83c3cb |
72 | |
587510be |
73 | $activities["activity/All"] = "All activities"; |
74 | $activities["activity/Assignments"] = "All assignments"; |
75 | $activities["activity/Chats"] = "All chats"; |
76 | $activities["activity/Forums"] = "All forums"; |
77 | $activities["activity/Quizzes"] = "All quizzes"; |
78 | $activities["activity/Workshops"] = "All workshops"; |
79 | |
80 | $activities["section/individual"] = "------------- Individual Activities --------------"; |
81 | |
82 | foreach ($modinfo as $mod) { |
83 | if ($mod->mod == "label") { |
84 | continue; |
89adb174 |
85 | } |
9c08ad13 |
86 | if (!$mod->visible and !$isteacher) { |
87 | continue; |
88 | } |
89 | |
587510be |
90 | if ($mod->section > 0 and $section <> $mod->section) { |
91 | $activities["section/$mod->section"] = "-------------- $strsection $mod->section --------------"; |
92 | } |
93 | $section = $mod->section; |
235a4ee8 |
94 | $mod->name = strip_tags(format_string(urldecode($mod->name),true)); |
587510be |
95 | if (strlen($mod->name) > 55) { |
96 | $mod->name = substr($mod->name, 0, 50)."..."; |
97 | } |
98 | if (!$mod->visible) { |
99 | $mod->name = "(".$mod->name.")"; |
100 | } |
101 | $activities["$mod->cm"] = $mod->name; |
102 | |
103 | if ($mod->cm == $modid) { |
104 | $selectedactivity = "$mod->cm"; |
105 | } |
cb83c3cb |
106 | } |
107 | } |
cb83c3cb |
108 | |
587510be |
109 | $strftimedate = get_string("strftimedate"); |
110 | $strftimedaydate = get_string("strftimedaydate"); |
cb83c3cb |
111 | |
587510be |
112 | asort($users); |
cb83c3cb |
113 | |
587510be |
114 | // Get all the possible dates |
115 | // Note that we are keeping track of real (GMT) time and user time |
116 | // User time is only used in displays - all calcs and passing is GMT |
cb83c3cb |
117 | |
587510be |
118 | $timenow = time(); // GMT |
cb83c3cb |
119 | |
587510be |
120 | // What day is it now for the user, and when is midnight that day (in GMT). |
121 | $timemidnight = $today = usergetmidnight($timenow); |
cb83c3cb |
122 | |
587510be |
123 | $dates = array(); |
124 | $dates["$USER->lastlogin"] = get_string("lastlogin").", ".userdate($USER->lastlogin, $strftimedate); |
125 | $dates["$timemidnight"] = get_string("today").", ".userdate($timenow, $strftimedate); |
cb83c3cb |
126 | |
587510be |
127 | if (!$course->startdate or ($course->startdate > $timenow)) { |
128 | $course->startdate = $course->timecreated; |
129 | } |
cb83c3cb |
130 | |
587510be |
131 | $numdates = 1; |
132 | while ($timemidnight > $course->startdate and $numdates < 365) { |
133 | $timemidnight = $timemidnight - 86400; |
134 | $timenow = $timenow - 86400; |
135 | $dates["$timemidnight"] = userdate($timenow, $strftimedaydate); |
136 | $numdates++; |
137 | } |
cb83c3cb |
138 | |
96dcfb56 |
139 | if ($selecteddate === "lastlogin") { |
587510be |
140 | $selecteddate = $USER->lastlogin; |
141 | } |
142 | |
143 | echo '<form action="recent.php" method="get">'; |
1c919752 |
144 | echo '<input type="hidden" name="chooserecent" value="1" />'; |
587510be |
145 | echo "<center>"; |
146 | echo "<table>"; |
147 | |
148 | if (isadmin()) { |
149 | echo "<tr><td><b>" . get_string("courses") . "</b></td><td>"; |
150 | choose_from_menu ($courses, "id", $course->id, ""); |
151 | echo "</td></tr>"; |
152 | } else { |
1c919752 |
153 | echo '<input type="hidden" name="id" value="'.$course->id.'" />'; |
587510be |
154 | } |
cb83c3cb |
155 | |
f5ffb87d |
156 | $sortfields = array("default" => get_string("bycourseorder"),"dateasc" => get_string("datemostrecentlast"), "datedesc" => get_string("datemostrecentfirst")); |
4581271a |
157 | |
587510be |
158 | echo "<tr><td><b>" . get_string("participants") . "</b></td><td>"; |
159 | choose_from_menu ($users, "user", $selecteduser, get_string("allparticipants") ); |
160 | echo "</td>"; |
161 | |
1c919752 |
162 | echo '<td align="right"><b>' . get_string("since") . '</b></td><td>'; |
587510be |
163 | choose_from_menu ($dates, "date", $selecteddate, get_string("alldays")); |
4581271a |
164 | echo "</td></tr>"; |
4581271a |
165 | |
587510be |
166 | echo "<tr><td><b>" . get_string("activities") . "</b></td><td>"; |
167 | choose_from_menu ($activities, "modid", $selectedactivity, ""); |
168 | echo "</td>"; |
4581271a |
169 | |
1c919752 |
170 | echo '<td align="right"><b>' . get_string("sortby") . "</b></td><td>"; |
587510be |
171 | choose_from_menu ($sortfields, "sortby", $selectedsort, ""); |
172 | echo "</td></tr>"; |
4581271a |
173 | |
587510be |
174 | echo '<tr>'; |
4581271a |
175 | |
587510be |
176 | $groupmode = groupmode($course); |
4581271a |
177 | |
587510be |
178 | if ($groupmode == VISIBLEGROUPS or ($groupmode and isteacheredit($course->id))) { |
179 | if ($groups = get_records_menu("groups", "courseid", $course->id, "name ASC", "id,name")) { |
4581271a |
180 | echo '<td><b>'; |
587510be |
181 | if ($groupmode == VISIBLEGROUPS) { |
182 | print_string('groupsvisible'); |
183 | } else { |
184 | print_string('groupsseparate'); |
185 | } |
186 | echo ':</b></td><td>'; |
187 | choose_from_menu($groups, "selectedgroup", $selectedgroup, get_string("allgroups"), "", ""); |
188 | echo '</td>'; |
189 | } |
190 | } |
191 | |
192 | |
1c919752 |
193 | echo '<td colspan="2" align="right">'; |
194 | echo '<input type="submit" value="'.get_string('showrecent').'" />'; |
587510be |
195 | echo "</td></tr>"; |
196 | |
197 | echo "</table>"; |
198 | |
839f2456 |
199 | $advancedlink = "<a href=\"$CFG->wwwroot/course/recent.php?id=$course->id&advancedfilter=0\">" . get_string("normalfilter") . "</a>"; |
587510be |
200 | print_heading($advancedlink); |
201 | echo "</center>"; |
202 | echo "</form>"; |
203 | |
204 | } else { |
205 | |
206 | $day_list = array("1","7","14","21","30"); |
207 | $strsince = get_string("since"); |
208 | $strlastlogin = get_string("lastlogin"); |
209 | $strday = get_string("day"); |
210 | $strdays = get_string("days"); |
211 | |
212 | $heading = ""; |
213 | foreach ($day_list as $count) { |
214 | if ($count == "1") { |
215 | $day = $strday; |
4581271a |
216 | } else { |
587510be |
217 | $day = $strdays; |
4581271a |
218 | } |
3819ed31 |
219 | $tmpdate = time() - ($count * 3600 * 24); |
587510be |
220 | $heading = $heading . |
839f2456 |
221 | "<a href=\"$CFG->wwwroot/course/recent.php?id=$course->id&date=$tmpdate\"> $count $day</a> | "; |
4581271a |
222 | } |
4581271a |
223 | |
587510be |
224 | $heading = $strsince . ": <a href=\"$CFG->wwwroot/course/recent.php?id=$course->id\">$strlastlogin</a>" . " | " . $heading; |
225 | print_heading($heading); |
4581271a |
226 | |
839f2456 |
227 | $advancedlink = "<a href=\"$CFG->wwwroot/course/recent.php?id=$course->id&advancedfilter=1\">" . get_string("advancedfilter") . "</a>"; |
587510be |
228 | print_heading($advancedlink); |
229 | |
230 | } |
4581271a |
231 | |
cb83c3cb |
232 | } |
9ae687af |
233 | |
f9903ed0 |
234 | |
600149be |
235 | function make_log_url($module, $url) { |
236 | switch ($module) { |
bd7be234 |
237 | case 'user': |
238 | case 'course': |
239 | case 'file': |
240 | case 'login': |
241 | case 'lib': |
242 | case 'admin': |
243 | case 'message': |
244 | case 'calendar': |
245 | case 'blog': |
600149be |
246 | return "/$module/$url"; |
247 | break; |
bd7be234 |
248 | case 'upload': |
249 | return $url; |
c80b7585 |
250 | break; |
bd7be234 |
251 | case 'library': |
252 | case '': |
253 | return '/'; |
de2dfe68 |
254 | break; |
600149be |
255 | default: |
256 | return "/mod/$module/$url"; |
257 | break; |
258 | } |
259 | } |
260 | |
8f0cd6ef |
261 | function print_log($course, $user=0, $date=0, $order="l.time ASC", $page=0, $perpage=100, |
69c76405 |
262 | $url="", $modname="", $modid=0, $modaction="", $groupid=0) { |
f24cffb9 |
263 | |
e0161bff |
264 | // It is assumed that $date is the GMT time of midnight for that day, |
265 | // and so the next 86400 seconds worth of logs are printed. |
f9903ed0 |
266 | |
f24cffb9 |
267 | global $CFG, $db; |
47f1da80 |
268 | |
69c76405 |
269 | /// Setup for group handling. |
270 | $isteacher = isteacher($course->id); |
271 | $isteacheredit = isteacheredit($course->id); |
272 | |
273 | /// If the group mode is separate, and this user does not have editing privileges, |
274 | /// then only the user's group can be viewed. |
275 | if ($course->groupmode == SEPARATEGROUPS and !$isteacheredit) { |
276 | $groupid = get_current_group($course->id); |
277 | } |
278 | /// If this course doesn't have groups, no groupid can be specified. |
279 | else if (!$course->groupmode) { |
280 | $groupid = 0; |
281 | } |
282 | |
e0161bff |
283 | $joins = array(); |
a2ab3b05 |
284 | |
e15ef260 |
285 | if ($course->id != SITEID || $modid != 0) { |
8f0cd6ef |
286 | $joins[] = "l.course='$course->id'"; |
e15ef260 |
287 | } |
288 | if ($course->id == SITEID) { |
2eb68e6f |
289 | $courses[0] = ''; |
e15ef260 |
290 | if ($ccc = get_courses('all', 'c.id ASC', 'c.id,c.shortname')) { |
720a43ce |
291 | foreach ($ccc as $cc) { |
e15ef260 |
292 | $courses[$cc->id] = $cc->shortname; |
720a43ce |
293 | } |
294 | } |
295 | } |
f9903ed0 |
296 | |
c469a7ef |
297 | if ($modname) { |
e0161bff |
298 | $joins[] = "l.module = '$modname'"; |
f24cffb9 |
299 | } |
300 | |
e21922f0 |
301 | if ('site_errors' === $modid) { |
bf35eb15 |
302 | $joins[] = "( l.action='error' OR l.action='infected' )"; |
e21922f0 |
303 | } else if ($modid) { |
304 | $joins[] = "l.cmid = '$modid'"; |
69d79bc3 |
305 | } |
306 | |
307 | if ($modaction) { |
ee35e0b8 |
308 | $firstletter = substr($modaction, 0, 1); |
309 | if (ctype_alpha($firstletter)) { |
310 | $joins[] = "lower(l.action) LIKE '%" . strtolower($modaction) . "%'"; |
311 | } else if ($firstletter == '-') { |
312 | $joins[] = "lower(l.action) NOT LIKE '%" . strtolower(substr($modaction, 1)) . "%'"; |
313 | } |
f24cffb9 |
314 | } |
315 | |
69c76405 |
316 | /// Getting all members of a group. |
317 | if ($groupid and !$user) { |
318 | if ($gusers = get_records('groups_members', 'groupid', $groupid)) { |
319 | $first = true; |
320 | foreach($gusers as $guser) { |
321 | if ($first) { |
322 | $gselect = '(l.userid='.$guser->userid; |
323 | $first = false; |
324 | } |
325 | else { |
326 | $gselect .= ' OR l.userid='.$guser->userid; |
327 | } |
328 | } |
329 | if (!$first) $gselect .= ')'; |
330 | $joins[] = $gselect; |
331 | } |
332 | } |
333 | else if ($user) { |
e0161bff |
334 | $joins[] = "l.userid = '$user'"; |
f9903ed0 |
335 | } |
336 | |
337 | if ($date) { |
338 | $enddate = $date + 86400; |
e0161bff |
339 | $joins[] = "l.time > '$date' AND l.time < '$enddate'"; |
f9903ed0 |
340 | } |
341 | |
2828ff51 |
342 | $selector = ''; |
e0161bff |
343 | for ($i = 0; $i < count($joins); $i++) { |
344 | $selector .= $joins[$i] . (($i == count($joins)-1) ? " " : " AND "); |
345 | } |
346 | |
347 | |
d09f3c80 |
348 | $totalcount = 0; // Initialise |
349 | |
519d369f |
350 | if (!$logs = get_logs($selector, $order, $page*$perpage, $perpage, $totalcount)) { |
f9903ed0 |
351 | notify("No logs found!"); |
352 | print_footer($course); |
353 | exit; |
354 | } |
355 | |
356 | $count=0; |
2eb68e6f |
357 | $ldcache = array(); |
f9903ed0 |
358 | $tt = getdate(time()); |
359 | $today = mktime (0, 0, 0, $tt["mon"], $tt["mday"], $tt["year"]); |
1c0200e0 |
360 | |
dcde9f02 |
361 | $strftimedatetime = get_string("strftimedatetime"); |
2ac64806 |
362 | $isteacher = isteacher($course->id); |
dcde9f02 |
363 | |
21283ddc |
364 | echo "<p align=\"center\">\n"; |
519d369f |
365 | print_string("displayingrecords", "", $totalcount); |
21283ddc |
366 | echo "</p>\n"; |
1c0200e0 |
367 | |
1548978d |
368 | |
8f0cd6ef |
369 | print_paging_bar($totalcount, $page, $perpage, "$url&perpage=$perpage&"); |
519d369f |
370 | |
21283ddc |
371 | echo "<table class=\"logtable\" border=\"0\" align=\"center\" cellpadding=\"3\" cellspacing=\"0\">\n"; |
372 | echo "<tr>"; |
1548978d |
373 | if ($course->id == SITEID) { |
21283ddc |
374 | echo "<th class=\"c0 header\">".get_string('course')."</th>\n"; |
1548978d |
375 | } |
21283ddc |
376 | echo "<th class=\"c1 header\">".get_string('time')."</th>\n"; |
377 | echo "<th class=\"c2 header\">".get_string('ip_address')."</th>\n"; |
378 | echo "<th class=\"c3 header\">".get_string('fullname')."</th>\n"; |
379 | echo "<th class=\"c4 header\">".get_string('action')."</th>\n"; |
380 | echo "<th class=\"c5 header\">".get_string('info')."</th>\n"; |
381 | echo "</tr>\n"; |
1548978d |
382 | |
383 | $row = 1; |
f9903ed0 |
384 | foreach ($logs as $log) { |
600149be |
385 | |
1548978d |
386 | $row = ($row + 1) % 2; |
387 | |
2eb68e6f |
388 | if (isset($ldcache[$log->module][$log->action])) { |
389 | $ld = $ldcache[$log->module][$log->action]; |
390 | } else { |
1548978d |
391 | $ld = get_record('log_display', 'module', $log->module, 'action', $log->action); |
2eb68e6f |
392 | $ldcache[$log->module][$log->action] = $ld; |
393 | } |
76feee3f |
394 | if ($ld && !empty($log->info)) { |
181b888e |
395 | // ugly hack to make sure fullname is shown correctly |
396 | if (($ld->mtable == 'user') and ($ld->field == 'CONCAT(firstname," ",lastname)')) { |
397 | $log->info = fullname(get_record($ld->mtable, 'id', $log->info), true); |
398 | } else { |
399 | $log->info = get_field($ld->mtable, $ld->field, 'id', $log->info); |
400 | } |
600149be |
401 | } |
402 | |
c8b0a50b |
403 | //Filter log->info |
404 | $log->info = format_string($log->info); |
405 | |
d7d145b1 |
406 | $log->url = strip_tags(urldecode($log->url)); // Some XSS protection |
407 | $log->info = strip_tags(urldecode($log->info)); // Some XSS protection |
6ac98433 |
408 | $log->url = str_replace('&', '&', $log->url); /// XHTML compatibility |
d7d145b1 |
409 | |
1548978d |
410 | echo '<tr class="r'.$row.'">'; |
411 | if ($course->id == SITEID) { |
21283ddc |
412 | echo "<td class=\"r$row c0\" nowrap=\"nowrap\">\n"; |
413 | echo " <a href=\"view.php?id={$log->course}\">".$courses[$log->course]."</a>\n"; |
414 | echo "</td>\n"; |
720a43ce |
415 | } |
21283ddc |
416 | echo "<td class=\"r$row c1\" nowrap=\"nowrap\" align=\"right\">".userdate($log->time, '%a'). |
417 | ' '.userdate($log->time, $strftimedatetime)."</td>\n"; |
418 | echo "<td class=\"r$row c2\" nowrap=\"nowrap\">\n"; |
7eca967c |
419 | link_to_popup_window("/iplookup/index.php?ip=$log->ip&user=$log->userid", 'iplookup',$log->ip, 400, 700); |
21283ddc |
420 | echo "</td>\n"; |
2ac64806 |
421 | $fullname = fullname($log, $isteacher); |
21283ddc |
422 | echo "<td class=\"r$row c3\" nowrap=\"nowrap\">\n"; |
d3a75287 |
423 | echo " <a href=\"$CFG->wwwroot/user/view.php?id={$log->userid}&course={$log->course}\">$fullname</a>\n"; |
21283ddc |
424 | echo "</td>\n"; |
425 | echo "<td class=\"r$row c4\" nowrap=\"nowrap\">\n"; |
2eb68e6f |
426 | link_to_popup_window( make_log_url($log->module,$log->url), 'fromloglive',"$log->module $log->action", 400, 600); |
21283ddc |
427 | echo "</td>\n";; |
428 | echo "<td class=\"r$row c5\" nowrap=\"nowrap\">{$log->info}</td>\n"; |
429 | echo "</tr>\n"; |
f9903ed0 |
430 | } |
21283ddc |
431 | echo "</table>\n"; |
519d369f |
432 | |
8f0cd6ef |
433 | print_paging_bar($totalcount, $page, $perpage, "$url&perpage=$perpage&"); |
f9903ed0 |
434 | } |
435 | |
436 | |
c2cb4545 |
437 | function print_log_graph($course, $userid=0, $type="course.png", $date=0) { |
438 | global $CFG; |
439 | if (empty($CFG->gdversion)) { |
440 | echo "(".get_string("gdneed").")"; |
d887b5a7 |
441 | } else { |
980a5b3a |
442 | echo '<img src="'.$CFG->wwwroot.'/course/report/log/graph.php?id='.$course->id. |
29b59206 |
443 | '&user='.$userid.'&type='.$type.'&date='.$date.'" alt="" />'; |
d887b5a7 |
444 | } |
445 | } |
446 | |
447 | |
185cfb09 |
448 | function print_overview($courses) { |
0d6b9d4f |
449 | |
450 | global $CFG, $USER; |
451 | |
0d6b9d4f |
452 | |
185cfb09 |
453 | $htmlarray = array(); |
f8716988 |
454 | if ($modules = get_records('modules')) { |
455 | foreach ($modules as $mod) { |
456 | if (file_exists(dirname(dirname(__FILE__)).'/mod/'.$mod->name.'/lib.php')) { |
457 | require_once(dirname(dirname(__FILE__)).'/mod/'.$mod->name.'/lib.php'); |
458 | $fname = $mod->name.'_print_overview'; |
0d6b9d4f |
459 | if (function_exists($fname)) { |
185cfb09 |
460 | $fname($courses,$htmlarray); |
0d6b9d4f |
461 | } |
462 | } |
463 | } |
464 | } |
185cfb09 |
465 | |
466 | foreach ($courses as $course) { |
fe5a1e23 |
467 | print_simple_box_start('center', '100%', '', 5, "coursebox"); |
185cfb09 |
468 | $linkcss = ''; |
469 | if (empty($course->visible)) { |
470 | $linkcss = 'class="dimmed"'; |
471 | } |
472 | print_heading('<a title="'.$course->fullname.'" '.$linkcss.' href="'.$CFG->wwwroot.'/course/view.php?id='.$course->id.'">'.$course->fullname.'</a>'); |
473 | if (array_key_exists($course->id,$htmlarray)) { |
474 | foreach ($htmlarray[$course->id] as $modname => $html) { |
475 | echo $html; |
476 | } |
477 | } |
478 | print_simple_box_end(); |
479 | } |
0d6b9d4f |
480 | } |
481 | |
482 | |
f9903ed0 |
483 | |
600149be |
484 | function print_recent_activity($course) { |
485 | // $course is an object |
89adb174 |
486 | // This function trawls through the logs looking for |
600149be |
487 | // anything new since the user's last login |
488 | |
810393c8 |
489 | global $CFG, $USER, $SESSION; |
600149be |
490 | |
2ac64806 |
491 | $isteacher = isteacher($course->id); |
492 | |
6f80940b |
493 | $timestart = time() - COURSE_MAX_RECENT_PERIOD; |
0f87cb1d |
494 | |
9e51847a |
495 | if (!empty($USER->timeaccess[$course->id])) { |
6f80940b |
496 | if ($USER->timeaccess[$course->id] > $timestart) { |
9e51847a |
497 | $timestart = $USER->timeaccess[$course->id]; |
498 | } |
3d891989 |
499 | } |
0f87cb1d |
500 | |
de785682 |
501 | echo '<div class="activitydate">'; |
27bf9e20 |
502 | echo get_string('activitysince', '', userdate($timestart)); |
de785682 |
503 | echo '</div>'; |
504 | echo '<div class="activityhead">'; |
0f87cb1d |
505 | |
de785682 |
506 | echo '<a href="'.$CFG->wwwroot.'/course/recent.php?id='.$course->id.'">'.get_string('recentactivityreport').'</a>'; |
0f87cb1d |
507 | |
de785682 |
508 | echo '</div>'; |
0f87cb1d |
509 | |
600149be |
510 | |
511 | // Firstly, have there been any new enrolments? |
512 | |
513 | $heading = false; |
514 | $content = false; |
1b5910c4 |
515 | |
6c38b7e0 |
516 | $users = get_recent_enrolments($course->id, $timestart); |
1b5910c4 |
517 | |
6c38b7e0 |
518 | if ($users) { |
27bf9e20 |
519 | echo '<div class="newusers">'; |
6c38b7e0 |
520 | foreach ($users as $user) { |
600149be |
521 | if (! $heading) { |
27bf9e20 |
522 | print_headline(get_string("newusers").':', 3); |
600149be |
523 | $heading = true; |
524 | $content = true; |
525 | } |
2ac64806 |
526 | $fullname = fullname($user, $isteacher); |
27bf9e20 |
527 | echo '<span class="name"><a href="'.$CFG->wwwroot."/user/view.php?id=$user->id&course=$course->id\">$fullname</a></span><br />"; |
600149be |
528 | } |
27bf9e20 |
529 | echo '</div>'; |
600149be |
530 | } |
531 | |
1b5910c4 |
532 | // Next, have there been any modifications to the course structure? |
533 | |
27bf9e20 |
534 | $logs = get_records_select('log', "time > '$timestart' AND course = '$course->id' AND |
1b5910c4 |
535 | module = 'course' AND action LIKE '% mod'", "time ASC"); |
536 | |
537 | if ($logs) { |
538 | foreach ($logs as $key => $log) { |
27bf9e20 |
539 | $info = split(' ', $log->info); |
c9f6251e |
540 | |
27bf9e20 |
541 | if ($info[0] == 'label') { // Labels are special activities |
c9f6251e |
542 | continue; |
543 | } |
544 | |
27bf9e20 |
545 | $modname = get_field($info[0], 'name', 'id', $info[1]); |
1b5910c4 |
546 | //Create a temp valid module structure (course,id) |
547 | $tempmod->course = $log->course; |
548 | $tempmod->id = $info[1]; |
549 | //Obtain the visible property from the instance |
550 | $modvisible = instance_is_visible($info[0],$tempmod); |
89adb174 |
551 | |
1b5910c4 |
552 | //Only if the mod is visible |
553 | if ($modvisible) { |
554 | switch ($log->action) { |
27bf9e20 |
555 | case 'add mod': |
556 | $stradded = get_string('added', 'moodle', get_string('modulename', $info[0])); |
5847b267 |
557 | $changelist[$log->info] = array ('operation' => 'add', 'text' => "$stradded:<br /><a href=\"$CFG->wwwroot/course/$log->url\">".format_string($modname,true)."</a>"); |
1b5910c4 |
558 | break; |
27bf9e20 |
559 | case 'update mod': |
560 | $strupdated = get_string('updated', 'moodle', get_string('modulename', $info[0])); |
561 | if (empty($changelist[$log->info])) { |
5847b267 |
562 | $changelist[$log->info] = array ('operation' => 'update', 'text' => "$strupdated:<br /><a href=\"$CFG->wwwroot/course/$log->url\">".format_string($modname,true)."</a>"); |
1b5910c4 |
563 | } |
564 | break; |
27bf9e20 |
565 | case 'delete mod': |
566 | if (!empty($changelist[$log->info]['operation']) and |
567 | $changelist[$log->info]['operation'] == 'add') { |
568 | $changelist[$log->info] = NULL; |
1b5910c4 |
569 | } else { |
27bf9e20 |
570 | $strdeleted = get_string('deletedactivity', 'moodle', get_string('modulename', $info[0])); |
571 | $changelist[$log->info] = array ('operation' => 'delete', 'text' => $strdeleted); |
1b5910c4 |
572 | } |
573 | break; |
600149be |
574 | } |
ef25340c |
575 | } |
576 | } |
577 | } |
578 | |
9c9f7d77 |
579 | if (!empty($changelist)) { |
ef25340c |
580 | foreach ($changelist as $changeinfo => $change) { |
581 | if ($change) { |
582 | $changes[$changeinfo] = $change; |
583 | } |
584 | } |
8a59942e |
585 | if (isset($changes)){ |
586 | if (count($changes) > 0) { |
27bf9e20 |
587 | print_headline(get_string('courseupdates').':', 3); |
8a59942e |
588 | $content = true; |
589 | foreach ($changes as $changeinfo => $change) { |
27bf9e20 |
590 | echo '<p class="activity">'.$change['text'].'</p>'; |
8a59942e |
591 | } |
600149be |
592 | } |
593 | } |
89adb174 |
594 | } |
bf40f9c1 |
595 | |
3869a2ac |
596 | // Now display new things from each module |
600149be |
597 | |
27bf9e20 |
598 | $mods = get_records('modules', 'visible', '1', 'name', 'id, name'); |
0fd7da81 |
599 | |
1b5910c4 |
600 | foreach ($mods as $mod) { // Each module gets it's own logs and prints them |
27bf9e20 |
601 | include_once($CFG->dirroot.'/mod/'.$mod->name.'/lib.php'); |
602 | $print_recent_activity = $mod->name.'_print_recent_activity'; |
1b5910c4 |
603 | if (function_exists($print_recent_activity)) { |
604 | $modcontent = $print_recent_activity($course, $isteacher, $timestart); |
3869a2ac |
605 | if ($modcontent) { |
606 | $content = true; |
600149be |
607 | } |
600149be |
608 | } |
609 | } |
610 | |
611 | if (! $content) { |
27bf9e20 |
612 | echo '<p class="message">'.get_string('nothingnew').'</p>'; |
600149be |
613 | } |
600149be |
614 | } |
615 | |
e1360728 |
616 | |
d897cae4 |
617 | function get_array_of_activities($courseid) { |
89adb174 |
618 | // For a given course, returns an array of course activity objects |
d897cae4 |
619 | // Each item in the array contains he following properties: |
620 | // cm - course module id |
621 | // mod - name of the module (eg forum) |
622 | // section - the number of the section (eg week or topic) |
623 | // name - the name of the instance |
5867bfb5 |
624 | // visible - is the instance visible or not |
86aa7ccf |
625 | // extra - contains extra string to include in any link |
d897cae4 |
626 | |
8dddba42 |
627 | global $CFG; |
628 | |
d897cae4 |
629 | $mod = array(); |
630 | |
9fa49e22 |
631 | if (!$rawmods = get_course_mods($courseid)) { |
d897cae4 |
632 | return NULL; |
633 | } |
634 | |
635 | if ($sections = get_records("course_sections", "course", $courseid, "section ASC")) { |
636 | foreach ($sections as $section) { |
74666583 |
637 | if (!empty($section->sequence)) { |
d897cae4 |
638 | $sequence = explode(",", $section->sequence); |
639 | foreach ($sequence as $seq) { |
7af6281f |
640 | if (empty($rawmods[$seq])) { |
641 | continue; |
642 | } |
d897cae4 |
643 | $mod[$seq]->cm = $rawmods[$seq]->id; |
644 | $mod[$seq]->mod = $rawmods[$seq]->modname; |
645 | $mod[$seq]->section = $section->section; |
646 | $mod[$seq]->name = urlencode(get_field($rawmods[$seq]->modname, "name", "id", $rawmods[$seq]->instance)); |
fec5a6a6 |
647 | $mod[$seq]->visible = $rawmods[$seq]->visible; |
86aa7ccf |
648 | $mod[$seq]->extra = ""; |
8dddba42 |
649 | |
650 | $modname = $mod[$seq]->mod; |
651 | $functionname = $modname."_get_coursemodule_info"; |
652 | |
653 | include_once("$CFG->dirroot/mod/$modname/lib.php"); |
654 | |
655 | if (function_exists($functionname)) { |
9d361034 |
656 | if ($info = $functionname($rawmods[$seq])) { |
657 | if (!empty($info->extra)) { |
658 | $mod[$seq]->extra = $info->extra; |
659 | } |
660 | if (!empty($info->icon)) { |
661 | $mod[$seq]->icon = $info->icon; |
662 | } |
c9f6251e |
663 | } |
664 | } |
d897cae4 |
665 | } |
666 | } |
667 | } |
668 | } |
669 | return $mod; |
670 | } |
671 | |
672 | |
673 | |
e1360728 |
674 | |
90845098 |
675 | function get_all_mods($courseid, &$mods, &$modnames, &$modnamesplural, &$modnamesused) { |
676 | // Returns a number of useful structures for course displays |
7468bf01 |
677 | |
90845098 |
678 | $mods = NULL; // course modules indexed by id |
e0161bff |
679 | $modnames = NULL; // all course module names (except resource!) |
94361e02 |
680 | $modnamesplural= NULL; // all course module names (plural form) |
90845098 |
681 | $modnamesused = NULL; // course module names used |
7468bf01 |
682 | |
9fa49e22 |
683 | if ($allmods = get_records("modules")) { |
90845098 |
684 | foreach ($allmods as $mod) { |
5867bfb5 |
685 | if ($mod->visible) { |
686 | $modnames[$mod->name] = get_string("modulename", "$mod->name"); |
687 | $modnamesplural[$mod->name] = get_string("modulenameplural", "$mod->name"); |
688 | } |
90845098 |
689 | } |
690 | asort($modnames); |
691 | } else { |
692 | error("No modules are installed!"); |
693 | } |
694 | |
9fa49e22 |
695 | if ($rawmods = get_course_mods($courseid)) { |
7468bf01 |
696 | foreach($rawmods as $mod) { // Index the mods |
959ae824 |
697 | if (empty($modnames[$mod->modname])) { |
698 | continue; |
699 | } |
7468bf01 |
700 | $mods[$mod->id] = $mod; |
959ae824 |
701 | $mods[$mod->id]->modfullname = $modnames[$mod->modname]; |
702 | if ($mod->visible or isteacher($courseid)) { |
703 | $modnamesused[$mod->modname] = $modnames[$mod->modname]; |
1acfbce5 |
704 | } |
7468bf01 |
705 | } |
c7da6f7a |
706 | if ($modnamesused) { |
707 | asort($modnamesused); |
708 | } |
7468bf01 |
709 | } |
e0161bff |
710 | |
711 | unset($modnames['resource']); |
712 | unset($modnames['label']); |
7468bf01 |
713 | } |
714 | |
9fa49e22 |
715 | |
7468bf01 |
716 | function get_all_sections($courseid) { |
89adb174 |
717 | |
718 | return get_records("course_sections", "course", "$courseid", "section", |
7d99d695 |
719 | "section, id, course, summary, sequence, visible"); |
7468bf01 |
720 | } |
721 | |
b86fc0e2 |
722 | function course_set_display($courseid, $display=0) { |
723 | global $USER; |
724 | |
1066e0dc |
725 | if (empty($USER->id)) { |
b86fc0e2 |
726 | return false; |
727 | } |
728 | |
729 | if ($display == "all" or empty($display)) { |
730 | $display = 0; |
731 | } |
732 | |
733 | if (record_exists("course_display", "userid", $USER->id, "course", $courseid)) { |
734 | set_field("course_display", "display", $display, "userid", $USER->id, "course", $courseid); |
735 | } else { |
736 | $record->userid = $USER->id; |
737 | $record->course = $courseid; |
738 | $record->display = $display; |
739 | if (!insert_record("course_display", $record)) { |
740 | notify("Could not save your course display!"); |
741 | } |
742 | } |
743 | |
744 | return $USER->display[$courseid] = $display; // Note: = not == |
745 | } |
746 | |
7d99d695 |
747 | function set_section_visible($courseid, $sectionnumber, $visibility) { |
748 | /// For a given course section, markes it visible or hidden, |
749 | /// and does the same for every activity in that section |
750 | |
751 | if ($section = get_record("course_sections", "course", $courseid, "section", $sectionnumber)) { |
752 | set_field("course_sections", "visible", "$visibility", "id", $section->id); |
753 | if (!empty($section->sequence)) { |
754 | $modules = explode(",", $section->sequence); |
755 | foreach ($modules as $moduleid) { |
02f66c42 |
756 | set_coursemodule_visible($moduleid, $visibility, true); |
7d99d695 |
757 | } |
758 | } |
5867bfb5 |
759 | rebuild_course_cache($courseid); |
7d99d695 |
760 | } |
761 | } |
ba2e5d73 |
762 | |
5e367a2d |
763 | |
d897cae4 |
764 | function print_section($course, $section, $mods, $modnamesused, $absolute=false, $width="100%") { |
52dcc2f9 |
765 | /// Prints a section full of activity modules |
7977cffd |
766 | global $CFG, $USER; |
767 | |
3d575e6f |
768 | static $groupbuttons; |
32d03b7b |
769 | static $groupbuttonslink; |
52dcc2f9 |
770 | static $isteacher; |
771 | static $isediting; |
7977cffd |
772 | static $ismoving; |
773 | static $strmovehere; |
774 | static $strmovefull; |
54669989 |
775 | static $strunreadpostsone; |
52dcc2f9 |
776 | |
4877707e |
777 | static $untracked; |
a2d71d8e |
778 | static $usetracking; |
4877707e |
779 | |
a22f8313 |
780 | $labelformatoptions = New stdClass; |
110a32e2 |
781 | |
52dcc2f9 |
782 | if (!isset($isteacher)) { |
9fd9c29b |
783 | $groupbuttons = ($course->groupmode or (!$course->groupmodeforce)); |
32d03b7b |
784 | $groupbuttonslink = (!$course->groupmodeforce); |
52dcc2f9 |
785 | $isteacher = isteacher($course->id); |
52dcc2f9 |
786 | $isediting = isediting($course->id); |
ff0c7de0 |
787 | $ismoving = $isediting && ismoving($course->id); |
3d575e6f |
788 | if ($ismoving) { |
789 | $strmovehere = get_string("movehere"); |
790 | $strmovefull = strip_tags(get_string("movefull", "", "'$USER->activitycopyname'")); |
791 | } |
94a6a70f |
792 | include_once($CFG->dirroot.'/mod/forum/lib.php'); |
793 | if ($usetracking = forum_tp_can_track_forums()) { |
a2d71d8e |
794 | $strunreadpostsone = get_string('unreadpostsone', 'forum'); |
94a6a70f |
795 | $untracked = forum_tp_get_untracked_forums($USER->id, $course->id); |
a2d71d8e |
796 | } |
7977cffd |
797 | } |
60bd11cf |
798 | $labelformatoptions->noclean = true; |
94361e02 |
799 | |
c408b0c4 |
800 | $modinfo = unserialize($course->modinfo); |
94361e02 |
801 | |
c6a55371 |
802 | //Acccessibility: replace table with list <ul>, but don't output empty list. |
74666583 |
803 | if (!empty($section->sequence)) { |
94361e02 |
804 | |
f2d660dc |
805 | // Fix bug #5027, don't want style=\"width:$width\". |
806 | echo "<ul class=\"section\">\n"; |
94361e02 |
807 | $sectionmods = explode(",", $section->sequence); |
808 | |
809 | foreach ($sectionmods as $modnumber) { |
9ae687af |
810 | if (empty($mods[$modnumber])) { |
811 | continue; |
812 | } |
94361e02 |
813 | $mod = $mods[$modnumber]; |
c9f6251e |
814 | |
52dcc2f9 |
815 | if ($mod->visible or $isteacher) { |
64fdc686 |
816 | echo '<li class="activity '.$mod->modname.'">'; |
7977cffd |
817 | if ($ismoving) { |
818 | if ($mod->id == $USER->activitycopy) { |
819 | continue; |
820 | } |
1c919752 |
821 | echo '<a title="'.$strmovefull.'"'. |
8b92f5bb |
822 | ' href="'.$CFG->wwwroot.'/course/mod.php?moveto='.$mod->id.'&sesskey='.$USER->sesskey.'">'. |
446390fb |
823 | '<img class="movetarget" src="'.$CFG->pixpath.'/movehere.gif" '. |
824 | ' alt="'.$strmovehere.'" /></a><br /> |
1c919752 |
825 | '; |
1acfbce5 |
826 | } |
7977cffd |
827 | $instancename = urldecode($modinfo[$modnumber]->name); |
a92d95b1 |
828 | $instancename = format_string($instancename, true, $course->id); |
c9f6251e |
829 | |
86aa7ccf |
830 | if (!empty($modinfo[$modnumber]->extra)) { |
831 | $extra = urldecode($modinfo[$modnumber]->extra); |
832 | } else { |
833 | $extra = ""; |
834 | } |
c9f6251e |
835 | |
9d361034 |
836 | if (!empty($modinfo[$modnumber]->icon)) { |
837 | $icon = "$CFG->pixpath/".urldecode($modinfo[$modnumber]->icon); |
838 | } else { |
839 | $icon = "$CFG->modpixpath/$mod->modname/icon.gif"; |
840 | } |
841 | |
aac94fd0 |
842 | if ($mod->indent) { |
843 | print_spacer(12, 20 * $mod->indent, false); |
844 | } |
845 | |
c9f6251e |
846 | if ($mod->modname == "label") { |
aac94fd0 |
847 | if (!$mod->visible) { |
848 | echo "<span class=\"dimmed_text\">"; |
849 | } |
179c9a50 |
850 | echo format_text($extra, FORMAT_HTML, $labelformatoptions); |
aac94fd0 |
851 | if (!$mod->visible) { |
852 | echo "</span>"; |
853 | } |
c9f6251e |
854 | |
855 | } else { // Normal activity |
856 | $linkcss = $mod->visible ? "" : " class=\"dimmed\" "; |
1c919752 |
857 | echo '<img src="'.$icon.'"'. |
446390fb |
858 | ' class="activityicon" alt="'.$mod->modfullname.'" />'. |
859 | ' <a title="'.$mod->modfullname.'" '.$linkcss.' '.$extra. |
1c919752 |
860 | ' href="'.$CFG->wwwroot.'/mod/'.$mod->modname.'/view.php?id='.$mod->id.'">'. |
446390fb |
861 | $instancename.'</a>'; |
c9f6251e |
862 | } |
a2d71d8e |
863 | if ($usetracking && $mod->modname == 'forum') { |
f37da850 |
864 | $groupmode = groupmode($course, $mod); |
865 | $groupid = ($groupmode == SEPARATEGROUPS && !isteacheredit($course->id)) ? |
a2d71d8e |
866 | get_current_group($course->id) : false; |
4877707e |
867 | |
94a6a70f |
868 | if (forum_tp_can_track_forums() && !isset($untracked[$mod->instance])) { |
4877707e |
869 | $unread = forum_tp_count_forum_unread_posts($USER->id, $mod->instance, $groupid); |
870 | if ($unread) { |
871 | echo '<span class="unread"> <a href="'.$CFG->wwwroot.'/mod/forum/view.php?id='.$mod->id.'">'; |
872 | if ($unread == 1) { |
873 | echo $strunreadpostsone; |
874 | } else { |
875 | print_string('unreadpostsnumber', 'forum', $unread); |
876 | } |
877 | echo '</a> </span>'; |
d0388ebe |
878 | } |
54669989 |
879 | } |
f37da850 |
880 | } |
881 | |
c9f6251e |
882 | if ($isediting) { |
5f390342 |
883 | if ($groupbuttons and $mod->modname != 'label' and $mod->modname != 'resource') { |
32d03b7b |
884 | if (! $mod->groupmodelink = $groupbuttonslink) { |
885 | $mod->groupmode = $course->groupmode; |
886 | } |
887 | |
888 | } else { |
3d575e6f |
889 | $mod->groupmode = false; |
890 | } |
37a88449 |
891 | echo ' '; |
24e1eae4 |
892 | echo make_editing_buttons($mod, $absolute, true, $mod->indent, $section->section); |
c9f6251e |
893 | } |
c6a55371 |
894 | echo "</li>\n"; |
94361e02 |
895 | } |
94361e02 |
896 | } |
f2d660dc |
897 | } elseif ($ismoving) { |
898 | echo "<ul class=\"section\">\n"; |
899 | } |
7977cffd |
900 | if ($ismoving) { |
64fdc686 |
901 | echo '<li><a title="'.$strmovefull.'"'. |
8b92f5bb |
902 | ' href="'.$CFG->wwwroot.'/course/mod.php?movetosection='.$section->id.'&sesskey='.$USER->sesskey.'">'. |
446390fb |
903 | '<img class="movetarget" src="'.$CFG->pixpath.'/movehere.gif" '. |
c6a55371 |
904 | ' alt="'.$strmovehere.'" /></a></li> |
1c919752 |
905 | '; |
7977cffd |
906 | } |
c6a55371 |
907 | if (!empty($section->sequence) || $ismoving) { |
908 | echo "</ul><!--class='section'-->\n\n"; |
909 | } |
a7ad3ea6 |
910 | } |
911 | |
5867bfb5 |
912 | |
cb57e6f4 |
913 | function print_section_add_menus($course, $section, $modnames, $vertical=false, $return=false) { |
e0161bff |
914 | // Prints the menus to add activities and resources |
915 | |
8b92f5bb |
916 | global $CFG, $USER; |
6da4b261 |
917 | static $straddactivity, $stractivities, $straddresource, $resources; |
e0161bff |
918 | |
919 | if (!isset($straddactivity)) { |
920 | $straddactivity = get_string('addactivity'); |
921 | $straddresource = get_string('addresource'); |
6da4b261 |
922 | |
923 | /// Standard resource types |
924 | require_once("$CFG->dirroot/mod/resource/lib.php"); |
925 | $resourceraw = resource_get_resource_types(); |
926 | |
927 | foreach ($resourceraw as $type => $name) { |
839f2456 |
928 | $resources["resource&type=$type"] = $name; |
e0161bff |
929 | } |
0705ff84 |
930 | if (course_allowed_module($course,'label')) { |
931 | $resources['label'] = get_string('resourcetypelabel', 'resource'); |
932 | } |
e0161bff |
933 | } |
934 | |
d33d0cda |
935 | $output = '<div style="text-align: right">'; |
0705ff84 |
936 | if (course_allowed_module($course,'resource')) { |
81a5d9ca |
937 | $resourceallowed = true; |
0705ff84 |
938 | $output .= popup_form("$CFG->wwwroot/course/mod.php?id=$course->id&section=$section&sesskey=$USER->sesskey&add=", |
939 | $resources, "ressection$section", "", $straddresource, 'resource/types', $straddresource, true); |
940 | } |
cb57e6f4 |
941 | |
942 | if ($vertical) { |
d33d0cda |
943 | $output .= '<div>'; |
cb57e6f4 |
944 | } |
945 | |
0705ff84 |
946 | // we need to loop through the forms and check to see if we can add them. |
947 | foreach ($modnames as $key) { |
948 | if (!course_allowed_module($course,$key)) |
949 | unset($modnames[strtolower($key)]); |
950 | } |
951 | |
952 | // this is stupid but labels get put into resource, so if resource is hidden and label is not, we're in trouble. |
953 | if (course_allowed_module($course,'label') && empty($resourceallowed)) { |
954 | $modnames['label'] = get_string('modulename', 'label'); |
955 | } |
956 | |
1c46eb03 |
957 | $output .= ' '; |
8b92f5bb |
958 | $output .= popup_form("$CFG->wwwroot/course/mod.php?id=$course->id&section=$section&sesskey=$USER->sesskey&add=", |
6da4b261 |
959 | $modnames, "section$section", "", $straddactivity, 'mods', $straddactivity, true); |
d33d0cda |
960 | |
961 | if ($vertical) { |
962 | $output .= '</div>'; |
963 | } |
964 | |
cb57e6f4 |
965 | $output .= '</div>'; |
966 | |
967 | if ($return) { |
968 | return $output; |
969 | } else { |
970 | echo $output; |
971 | } |
e0161bff |
972 | } |
973 | |
5867bfb5 |
974 | function rebuild_course_cache($courseid=0) { |
975 | // Rebuilds the cached list of course activities stored in the database |
976 | // If a courseid is not specified, then all are rebuilt |
977 | |
978 | if ($courseid) { |
979 | $select = "id = '$courseid'"; |
980 | } else { |
981 | $select = ""; |
982 | } |
983 | |
1a31c2b3 |
984 | if ($courses = get_records_select("course", $select,'','id,fullname')) { |
5867bfb5 |
985 | foreach ($courses as $course) { |
986 | $modinfo = serialize(get_array_of_activities($course->id)); |
987 | if (!set_field("course", "modinfo", $modinfo, "id", $course->id)) { |
988 | notify("Could not cache module information for course '$course->fullname'!"); |
989 | } |
990 | } |
991 | } |
992 | } |
993 | |
994 | |
c2cb4545 |
995 | |
996 | function make_categories_list(&$list, &$parents, $category=NULL, $path="") { |
89adb174 |
997 | /// Given an empty array, this function recursively travels the |
c2cb4545 |
998 | /// categories, building up a nice list for display. It also makes |
999 | /// an array that list all the parents for each category. |
1000 | |
9d866ae0 |
1001 | // initialize the arrays if needed |
1002 | if (!is_array($list)) { |
1003 | $list = array(); |
1004 | } |
1005 | if (!is_array($parents)) { |
1006 | $parents = array(); |
1007 | } |
1008 | |
c2cb4545 |
1009 | if ($category) { |
1010 | if ($path) { |
e05bcf2f |
1011 | $path = $path.' / '.$category->name; |
c2cb4545 |
1012 | } else { |
e05bcf2f |
1013 | $path = $category->name; |
c2cb4545 |
1014 | } |
1015 | $list[$category->id] = $path; |
1016 | } else { |
1017 | $category->id = 0; |
1018 | } |
1019 | |
e05bcf2f |
1020 | if ($categories = get_categories($category->id)) { // Print all the children recursively |
c2cb4545 |
1021 | foreach ($categories as $cat) { |
1022 | if (!empty($category->id)) { |
3bd4de22 |
1023 | if (isset($parents[$category->id])) { |
2832badf |
1024 | $parents[$cat->id] = $parents[$category->id]; |
1025 | } |
c2cb4545 |
1026 | $parents[$cat->id][] = $category->id; |
1027 | } |
89adb174 |
1028 | make_categories_list($list, $parents, $cat, $path); |
c2cb4545 |
1029 | } |
1030 | } |
1031 | } |
1032 | |
1033 | |
c2cb4545 |
1034 | function print_whole_category_list($category=NULL, $displaylist=NULL, $parentslist=NULL, $depth=-1) { |
89adb174 |
1035 | /// Recursive function to print out all the categories in a nice format |
c2cb4545 |
1036 | /// with or without courses included |
9ff5310a |
1037 | global $CFG; |
e05bcf2f |
1038 | |
1039 | if (isset($CFG->max_category_depth) && ($depth >= $CFG->max_category_depth)) { |
1040 | return; |
9ff5310a |
1041 | } |
c2cb4545 |
1042 | |
1043 | if (!$displaylist) { |
e92fe848 |
1044 | make_categories_list($displaylist, $parentslist); |
c2cb4545 |
1045 | } |
1046 | |
1047 | if ($category) { |
3af6e1db |
1048 | if ($category->visible or iscreator()) { |
d2b6ba70 |
1049 | print_category_info($category, $depth); |
c2cb4545 |
1050 | } else { |
1051 | return; // Don't bother printing children of invisible categories |
1052 | } |
89adb174 |
1053 | |
c2cb4545 |
1054 | } else { |
c2cb4545 |
1055 | $category->id = "0"; |
1056 | } |
1057 | |
1058 | if ($categories = get_categories($category->id)) { // Print all the children recursively |
1059 | $countcats = count($categories); |
1060 | $count = 0; |
1061 | $first = true; |
1062 | $last = false; |
1063 | foreach ($categories as $cat) { |
1064 | $count++; |
1065 | if ($count == $countcats) { |
1066 | $last = true; |
1067 | } |
1068 | $up = $first ? false : true; |
1069 | $down = $last ? false : true; |
1070 | $first = false; |
1071 | |
814f88c0 |
1072 | print_whole_category_list($cat, $displaylist, $parentslist, $depth + 1); |
c2cb4545 |
1073 | } |
1074 | } |
c2cb4545 |
1075 | } |
1076 | |
0705ff84 |
1077 | // this function will return $options array for choose_from_menu, with whitespace to denote nesting. |
1078 | |
1079 | function make_categories_options() { |
1080 | make_categories_list($cats,$parents); |
1081 | foreach ($cats as $key => $value) { |
1082 | if (array_key_exists($key,$parents)) { |
1083 | if ($indent = count($parents[$key])) { |
1084 | for ($i = 0; $i < $indent; $i++) { |
1085 | $cats[$key] = ' '.$cats[$key]; |
1086 | } |
1087 | } |
1088 | } |
1089 | } |
1090 | return $cats; |
1091 | } |
c2cb4545 |
1092 | |
d2b6ba70 |
1093 | function print_category_info($category, $depth) { |
1094 | /// Prints the category info in indented fashion |
1095 | /// This function is only used by print_whole_category_list() above |
c2cb4545 |
1096 | |
1097 | global $CFG; |
b48f834c |
1098 | static $strallowguests, $strrequireskey, $strsummary; |
c2cb4545 |
1099 | |
b48f834c |
1100 | if (empty($strsummary)) { |
e05bcf2f |
1101 | $strallowguests = get_string('allowguests'); |
1102 | $strrequireskey = get_string('requireskey'); |
1103 | $strsummary = get_string('summary'); |
b48f834c |
1104 | } |
ba2e5d73 |
1105 | |
e05bcf2f |
1106 | $catlinkcss = $category->visible ? '' : ' class="dimmed" '; |
d5f26b07 |
1107 | |
e1638d61 |
1108 | $frontpage = explode(',', $CFG->frontpage); |
1109 | $frontpage = $frontpage?array_flip($frontpage):array(); |
48643b49 |
1110 | $coursecount = count_records('course') <= 200; |
1111 | if (isset($frontpage[FRONTPAGECATEGORYNAMES]) && !isset($frontpage[FRONTPAGECOURSELIST]) && $coursecount) { |
839f2456 |
1112 | $catimage = '<img src="'.$CFG->pixpath.'/i/course.gif" width="16" height="16" border="0" alt="" />'; |
b48f834c |
1113 | } else { |
7b0b5c14 |
1114 | $catimage = " "; |
8ef9cb56 |
1115 | } |
b48f834c |
1116 | |
87d32352 |
1117 | echo "\n\n".'<table border="0" cellpadding="3" cellspacing="0" width="100%">'; |
d2b6ba70 |
1118 | |
48643b49 |
1119 | if (isset($frontpage[FRONTPAGECATEGORYNAMES]) && !isset($frontpage[FRONTPAGECOURSELIST]) && $coursecount) { |
19374e76 |
1120 | $courses = get_courses($category->id, 'c.sortorder ASC', 'c.id,c.sortorder,c.visible,c.fullname,c.shortname,c.password,c.summary,c.guest,c.cost,c.currency'); |
b48f834c |
1121 | |
1122 | echo "<tr>"; |
1123 | |
1124 | if ($depth) { |
1125 | $indent = $depth*30; |
1126 | $rows = count($courses) + 1; |
1c919752 |
1127 | echo '<td rowspan="'.$rows.'" valign="top" width="'.$indent.'">'; |
b48f834c |
1128 | print_spacer(10, $indent); |
e05bcf2f |
1129 | echo '</td>'; |
b48f834c |
1130 | } |
89adb174 |
1131 | |
e05bcf2f |
1132 | echo '<td valign="top">'.$catimage.'</td>'; |
290130b3 |
1133 | echo '<td valign="top" width="100%" class="category name">'; |
e05bcf2f |
1134 | echo '<a '.$catlinkcss.' href="'.$CFG->wwwroot.'/course/category.php?id='.$category->id.'">'.$category->name.'</a>'; |
1135 | echo '</td>'; |
290130b3 |
1136 | echo '<td class="category info"> </td>'; |
e05bcf2f |
1137 | echo '</tr>'; |
b48f834c |
1138 | |
9ff5310a |
1139 | if ($courses && !(isset($CFG->max_category_depth)&&($depth>=$CFG->max_category_depth-1))) { |
c2cb4545 |
1140 | foreach ($courses as $course) { |
e05bcf2f |
1141 | $linkcss = $course->visible ? '' : ' class="dimmed" '; |
1142 | echo '<tr><td valign="top" width="30"> '; |
290130b3 |
1143 | echo '</td><td valign="top" width="100%" class="course name">'; |
e05bcf2f |
1144 | echo '<a '.$linkcss.' href="'.$CFG->wwwroot.'/course/view.php?id='.$course->id.'">'.$course->fullname.'</a>'; |
290130b3 |
1145 | echo '</td><td align="right" valign="top" nowrap="nowrap" class="course info">'; |
c2cb4545 |
1146 | if ($course->guest ) { |
e05bcf2f |
1147 | echo '<a title="'.$strallowguests.'" href="'.$CFG->wwwroot.'/course/view.php?id='.$course->id.'">'; |
1148 | echo '<img hspace="1" alt="'.$strallowguests.'" height="16" width="16" border="0" src="'.$CFG->pixpath.'/i/guest.gif" /></a>'; |
ebe8ddc1 |
1149 | } else { |
e05bcf2f |
1150 | echo '<img alt="" height="16" width="18" border="0" src="'.$CFG->pixpath.'/spacer.gif" />'; |
0c656181 |
1151 | } |
c2cb4545 |
1152 | if ($course->password) { |
e05bcf2f |
1153 | echo '<a title="'.$strrequireskey.'" href="'.$CFG->wwwroot.'/course/view.php?id='.$course->id.'">'; |
1154 | echo '<img hspace="1" alt="'.$strrequireskey.'" height="16" width="16" border="0" src="'.$CFG->pixpath.'/i/key.gif" /></a>'; |
ebe8ddc1 |
1155 | } else { |
e05bcf2f |
1156 | echo '<img alt="" height="16" width="18" border="0" src="'.$CFG->pixpath.'/spacer.gif" />'; |
b48f834c |
1157 | } |
1158 | if ($course->summary) { |
e05bcf2f |
1159 | link_to_popup_window ('/course/info.php?id='.$course->id, 'courseinfo', |
1160 | '<img hspace="1" alt="'.$strsummary.'" height="16" width="16" border="0" src="'.$CFG->pixpath.'/i/info.gif" />', |
b48f834c |
1161 | 400, 500, $strsummary); |
ebe8ddc1 |
1162 | } else { |
e05bcf2f |
1163 | echo '<img alt="" height="16" width="18" border="0" src="'.$CFG->pixpath.'/spacer.gif" />'; |
0c656181 |
1164 | } |
e05bcf2f |
1165 | echo '</td></tr>'; |
0c656181 |
1166 | } |
ba2e5d73 |
1167 | } |
d2b6ba70 |
1168 | } else { |
b48f834c |
1169 | |
e0140f24 |
1170 | echo '<tr>'; |
1171 | |
b48f834c |
1172 | if ($depth) { |
1173 | $indent = $depth*20; |
e05bcf2f |
1174 | echo '<td valign="top" width="'.$indent.'">'; |
b48f834c |
1175 | print_spacer(10, $indent); |
e05bcf2f |
1176 | echo '</td>'; |
d2b6ba70 |
1177 | } |
89adb174 |
1178 | |
290130b3 |
1179 | echo '<td valign="top" width="100%" class="category name">'; |
e05bcf2f |
1180 | echo '<a '.$catlinkcss.' href="'.$CFG->wwwroot.'/course/category.php?id='.$category->id.'">'.$category->name.'</a>'; |
1181 | echo '</td>'; |
290130b3 |
1182 | echo '<td valign="top" class="category number">'; |
e05bcf2f |
1183 | if ($category->coursecount) { |
1184 | echo $category->coursecount; |
1185 | } |
1186 | echo '</td></tr>'; |
c2cb4545 |
1187 | } |
e05bcf2f |
1188 | echo '</table>'; |
c2cb4545 |
1189 | } |
1190 | |
c2cb4545 |
1191 | |
8e227aa7 |
1192 | function print_courses($category, $width="100%", $hidesitecourse = false) { |
c2cb4545 |
1193 | /// Category is 0 (for all courses) or an object |
1194 | |
810393c8 |
1195 | global $CFG; |
c2cb4545 |
1196 | |
1197 | if (empty($category)) { |
90c2ca2e |
1198 | $categories = get_categories(0); // Parent = 0 ie top-level categories only |
1199 | if (count($categories) == 1) { |
1200 | $category = array_shift($categories); |
0cb9ec10 |
1201 | $courses = get_courses($category->id, 'c.sortorder ASC', 'c.id,c.category,c.sortorder,c.visible,c.fullname,c.shortname,c.password,c.summary,c.teacher,c.cost,c.currency,c.enrol'); |
90c2ca2e |
1202 | } else { |
0cb9ec10 |
1203 | $courses = get_courses('all', 'c.sortorder ASC', 'c.id,c.category,c.sortorder,c.visible,c.fullname,c.shortname,c.password,c.summary,c.teacher,c.cost,c.currency,c.enrol'); |
90c2ca2e |
1204 | } |
1205 | unset($categories); |
607809b3 |
1206 | } else { |
c2cb4545 |
1207 | $categories = get_categories($category->id); // sub categories |
0cb9ec10 |
1208 | $courses = get_courses($category->id, 'c.sortorder ASC', 'c.id,c.category,c.sortorder,c.visible,c.fullname,c.shortname,c.password,c.summary,c.teacher,c.cost,c.currency,c.enrol'); |
c2cb4545 |
1209 | } |
1210 | |
c2cb4545 |
1211 | if ($courses) { |
1212 | foreach ($courses as $course) { |
8e227aa7 |
1213 | if ($hidesitecourse && !$course->category) { |
1214 | continue; |
1215 | } |
c2cb4545 |
1216 | print_course($course, $width); |
c2cb4545 |
1217 | } |
1218 | } else { |
f9667a5a |
1219 | print_heading(get_string("nocoursesyet")); |
255d1033 |
1220 | if (iscreator()) { // Make it obvious for newbies on new sites how to add a course |
1221 | $options = array(); |
1222 | $options['category'] = $category->id; |
1223 | echo '<div class="addcoursebutton" align="center">'; |
1224 | print_single_button($CFG->wwwroot.'/course/edit.php', $options, get_string("addnewcourse")); |
1225 | echo '</div>'; |
1226 | } |
c2cb4545 |
1227 | } |
1228 | |
1229 | } |
1230 | |
1231 | |
1232 | function print_course($course, $width="100%") { |
1233 | |
861efb19 |
1234 | global $CFG, $USER; |
c2cb4545 |
1235 | |
f9667a5a |
1236 | require_once("$CFG->dirroot/enrol/enrol.class.php"); |
146bbb8f |
1237 | |
f9667a5a |
1238 | $enrol = enrolment_factory::factory($course->enrol); |
146bbb8f |
1239 | |
810393c8 |
1240 | print_simple_box_start("center", "$width", '', 5, "coursebox"); |
c2cb4545 |
1241 | |
22288704 |
1242 | $linkcss = $course->visible ? "" : " class=\"dimmed\" "; |
1243 | |
c2cb4545 |
1244 | echo "<table width=\"100%\">"; |
e5e81e78 |
1245 | echo '<tr valign="top">'; |
290130b3 |
1246 | echo '<td valign="top" width="50%" class="info">'; |
34b5847a |
1247 | echo '<b><a title="'.get_string('entercourse').'"'. |
e5e81e78 |
1248 | $linkcss.' href="'.$CFG->wwwroot.'/course/view.php?id='.$course->id.'">'. |
34b5847a |
1249 | $course->fullname.'</a></b><br />'; |
c2cb4545 |
1250 | if ($teachers = get_course_teachers($course->id)) { |
290130b3 |
1251 | echo "<span class=\"teachers\">\n"; |
c2cb4545 |
1252 | foreach ($teachers as $teacher) { |
1253 | if ($teacher->authority > 0) { |
1254 | if (!$teacher->role) { |
1255 | $teacher->role = $course->teacher; |
1256 | } |
2ac64806 |
1257 | $fullname = fullname($teacher, isteacher($course->id)); // is the USER a teacher of that course |
8bd84b4d |
1258 | echo $teacher->role.': <a href="'.$CFG->wwwroot.'/user/view.php?id='.$teacher->id. |
1259 | '&course='.SITEID.'">'.$fullname.'</a><br />'; |
c2cb4545 |
1260 | } |
1261 | } |
34b5847a |
1262 | echo "</span>\n"; |
c2cb4545 |
1263 | } |
c2cb4545 |
1264 | |
146bbb8f |
1265 | echo $enrol->get_access_icons($course); |
c2cb4545 |
1266 | |
290130b3 |
1267 | echo '</td><td valign="top" width="50%" class="summary">'; |
9f39c190 |
1268 | $options = NULL; |
1269 | $options->noclean = true; |
34b5847a |
1270 | $options->para = false; |
9f39c190 |
1271 | echo format_text($course->summary, FORMAT_MOODLE, $options, $course->id); |
c2cb4545 |
1272 | echo "</td></tr>"; |
1273 | echo "</table>"; |
1274 | |
1275 | print_simple_box_end(); |
1276 | } |
1277 | |
1278 | |
1279 | function print_my_moodle() { |
1280 | /// Prints custom user information on the home page. |
1281 | /// Over time this can include all sorts of information |
1282 | |
1283 | global $USER, $CFG; |
1284 | |
1285 | if (!isset($USER->id)) { |
1286 | error("It shouldn't be possible to see My Moodle without being logged in."); |
1287 | } |
1288 | |
1289 | if ($courses = get_my_courses($USER->id)) { |
1290 | foreach ($courses as $course) { |
c81696e5 |
1291 | if (!$course->category) { |
1292 | continue; |
1293 | } |
c2cb4545 |
1294 | print_course($course, "100%"); |
c2cb4545 |
1295 | } |
38a10939 |
1296 | |
7f989948 |
1297 | if (count_records("course") > (count($courses) + 1) ) { // Some courses not being displayed |
1298 | echo "<table width=\"100%\"><tr><td align=\"center\">"; |
1299 | print_course_search("", false, "short"); |
1300 | echo "</td><td align=\"center\">"; |
1301 | print_single_button("$CFG->wwwroot/course/index.php", NULL, get_string("fulllistofcourses"), "get"); |
1302 | echo "</td></tr></table>\n"; |
1303 | } |
26330001 |
1304 | } else { |
1305 | if (count_records("course_categories") > 1) { |
cb29b020 |
1306 | print_simple_box_start("center", "100%", "#FFFFFF", 5, "categorybox"); |
26330001 |
1307 | print_whole_category_list(); |
1308 | print_simple_box_end(); |
1309 | } else { |
1310 | print_courses(0, "100%"); |
1311 | } |
607809b3 |
1312 | } |
2b8cef80 |
1313 | } |
1314 | |
11b0c469 |
1315 | |
a8b56716 |
1316 | function print_course_search($value="", $return=false, $format="plain") { |
38a10939 |
1317 | |
1318 | global $CFG; |
1319 | |
1320 | $strsearchcourses= get_string("searchcourses"); |
1321 | |
1c919752 |
1322 | if ($format == 'plain') { |
87d32352 |
1323 | $output = '<form name="coursesearch" action="'.$CFG->wwwroot.'/course/search.php" method="get">'; |
1324 | $output .= '<center><p align="center" class="coursesearchbox">'; |
f8a5159a |
1325 | $output .= '<input type="text" size="30" name="search" alt="'.s($strsearchcourses).'" value="'.s($value, true).'" />'; |
9cc78ee1 |
1326 | $output .= '<input type="submit" value="'.s($strsearchcourses).'" />'; |
87d32352 |
1327 | $output .= '</p></center></form>'; |
1c919752 |
1328 | } else if ($format == 'short') { |
87d32352 |
1329 | $output = '<form name="coursesearch" action="'.$CFG->wwwroot.'/course/search.php" method="get">'; |
1330 | $output .= '<center><p align="center" class="coursesearchbox">'; |
f8a5159a |
1331 | $output .= '<input type="text" size="12" name="search" alt="'.s($strsearchcourses).'" value="'.s($value, true).'" />'; |
9cc78ee1 |
1332 | $output .= '<input type="submit" value="'.s($strsearchcourses).'" />'; |
87d32352 |
1333 | $output .= '</p></center></form>'; |
1c919752 |
1334 | } else if ($format == 'navbar') { |
87d32352 |
1335 | $output = '<form name="coursesearch" action="'.$CFG->wwwroot.'/course/search.php" method="get">'; |
1336 | $output .= '<table border="0" cellpadding="0" cellspacing="0"><tr><td nowrap="nowrap">'; |
f8a5159a |
1337 | $output .= '<input type="text" size="20" name="search" alt="'.s($strsearchcourses).'" value="'.s($value, true).'" />'; |
9cc78ee1 |
1338 | $output .= '<input type="submit" value="'.s($strsearchcourses).'" />'; |
1c919752 |
1339 | $output .= '</td></tr></table>'; |
87d32352 |
1340 | $output .= '</form>'; |
a8b56716 |
1341 | } |
1342 | |
1343 | if ($return) { |
1344 | return $output; |
1345 | } |
1346 | echo $output; |
38a10939 |
1347 | } |
11b0c469 |
1348 | |
1349 | /// MODULE FUNCTIONS ///////////////////////////////////////////////////////////////// |
1350 | |
1351 | function add_course_module($mod) { |
11b0c469 |
1352 | |
e5dfd0f3 |
1353 | $mod->added = time(); |
53f4ad2c |
1354 | unset($mod->id); |
11b0c469 |
1355 | |
e5dfd0f3 |
1356 | return insert_record("course_modules", $mod); |
11b0c469 |
1357 | } |
1358 | |
7977cffd |
1359 | function add_mod_to_section($mod, $beforemod=NULL) { |
1360 | /// Given a full mod object with section and course already defined |
1361 | /// If $before is specified, then this is an existing ID which we |
1362 | /// will insert the new module before |
1363 | /// |
1364 | /// Returns the course_sections ID where the mod is inserted |
11b0c469 |
1365 | |
9fa49e22 |
1366 | if ($section = get_record("course_sections", "course", "$mod->course", "section", "$mod->section")) { |
7977cffd |
1367 | |
1368 | $section->sequence = trim($section->sequence); |
1369 | |
1370 | if (empty($section->sequence)) { |
11b0c469 |
1371 | $newsequence = "$mod->coursemodule"; |
7977cffd |
1372 | |
1373 | } else if ($beforemod) { |
1374 | $modarray = explode(",", $section->sequence); |
1375 | |
1376 | if ($key = array_keys ($modarray, $beforemod->id)) { |
1377 | $insertarray = array($mod->id, $beforemod->id); |
1378 | array_splice($modarray, $key[0], 1, $insertarray); |
1379 | $newsequence = implode(",", $modarray); |
1380 | |
1381 | } else { // Just tack it on the end anyway |
1382 | $newsequence = "$section->sequence,$mod->coursemodule"; |
1383 | } |
1384 | |
1385 | } else { |
1386 | $newsequence = "$section->sequence,$mod->coursemodule"; |
11b0c469 |
1387 | } |
89adb174 |
1388 | |
e5dfd0f3 |
1389 | if (set_field("course_sections", "sequence", $newsequence, "id", $section->id)) { |
1390 | return $section->id; // Return course_sections ID that was used. |
11b0c469 |
1391 | } else { |
e5dfd0f3 |
1392 | return 0; |
11b0c469 |
1393 | } |
89adb174 |
1394 | |
11b0c469 |
1395 | } else { // Insert a new record |
e5dfd0f3 |
1396 | $section->course = $mod->course; |
1397 | $section->section = $mod->section; |
1398 | $section->summary = ""; |
1399 | $section->sequence = $mod->coursemodule; |
1400 | return insert_record("course_sections", $section); |
11b0c469 |
1401 | } |
1402 | } |
1403 | |
48e535bc |
1404 | function set_coursemodule_groupmode($id, $groupmode) { |
3d575e6f |
1405 | return set_field("course_modules", "groupmode", $groupmode, "id", $id); |
1406 | } |
1407 | |
02f66c42 |
1408 | /** |
1409 | * $prevstateoverrides = true will set the visibility of the course module |
1410 | * to what is defined in visibleold. This enables us to remember the current |
1411 | * visibility when making a whole section hidden, so that when we toggle |
1412 | * that section back to visible, we are able to return the visibility of |
1413 | * the course module back to what it was originally. |
1414 | */ |
1415 | function set_coursemodule_visible($id, $visible, $prevstateoverrides=false) { |
48e535bc |
1416 | $cm = get_record('course_modules', 'id', $id); |
dcd338ff |
1417 | $modulename = get_field('modules', 'name', 'id', $cm->module); |
1418 | if ($events = get_records_select('event', "instance = '$cm->instance' AND modulename = '$modulename'")) { |
1419 | foreach($events as $event) { |
48e535bc |
1420 | if ($visible) { |
1421 | show_event($event); |
1422 | } else { |
1423 | hide_event($event); |
1424 | } |
dcd338ff |
1425 | } |
1426 | } |
02f66c42 |
1427 | if ($prevstateoverrides) { |
1428 | if ($visible == '0') { |
1429 | // Remember the current visible state so we can toggle this back. |
1430 | set_field('course_modules', 'visibleold', $cm->visible, 'id', $id); |
1431 | } else { |
1432 | // Get the previous saved visible states. |
1433 | return set_field('course_modules', 'visible', $cm->visibleold, 'id', $id); |
1434 | } |
1435 | } |
48e535bc |
1436 | return set_field("course_modules", "visible", $visible, "id", $id); |
1acfbce5 |
1437 | } |
1438 | |
290130b3 |
1439 | /* |
1440 | * Delete a course module and any associated data at the course level (events) |
1441 | * Until 1.5 this function simply marked a deleted flag ... now it |
1442 | * deletes it completely. |
1443 | * |
1444 | */ |
48e535bc |
1445 | function delete_course_module($id) { |
290130b3 |
1446 | if (!$cm = get_record('course_modules', 'id', $id)) { |
1447 | return true; |
1448 | } |
dcd338ff |
1449 | $modulename = get_field('modules', 'name', 'id', $cm->module); |
1450 | if ($events = get_records_select('event', "instance = '$cm->instance' AND modulename = '$modulename'")) { |
1451 | foreach($events as $event) { |
48e535bc |
1452 | delete_event($event); |
dcd338ff |
1453 | } |
1454 | } |
290130b3 |
1455 | return delete_records('course_modules', 'id', $cm->id); |
11b0c469 |
1456 | } |
1457 | |
1458 | function delete_mod_from_section($mod, $section) { |
11b0c469 |
1459 | |
e5dfd0f3 |
1460 | if ($section = get_record("course_sections", "id", "$section") ) { |
11b0c469 |
1461 | |
e5dfd0f3 |
1462 | $modarray = explode(",", $section->sequence); |
11b0c469 |
1463 | |
1464 | if ($key = array_keys ($modarray, $mod)) { |
1465 | array_splice($modarray, $key[0], 1); |
1466 | $newsequence = implode(",", $modarray); |
e5dfd0f3 |
1467 | return set_field("course_sections", "sequence", $newsequence, "id", $section->id); |
11b0c469 |
1468 | } else { |
1469 | return false; |
1470 | } |
89adb174 |
1471 | |
11b0c469 |
1472 | } |
7977cffd |
1473 | return false; |
11b0c469 |
1474 | } |
1475 | |
12905134 |
1476 | function move_section($course, $section, $move) { |
1477 | /// Moves a whole course section up and down within the course |
798b70a1 |
1478 | global $USER; |
12905134 |
1479 | |
1480 | if (!$move) { |
1481 | return true; |
1482 | } |
1483 | |
1484 | $sectiondest = $section + $move; |
1485 | |
1486 | if ($sectiondest > $course->numsections or $sectiondest < 1) { |
1487 | return false; |
1488 | } |
1489 | |
1490 | if (!$sectionrecord = get_record("course_sections", "course", $course->id, "section", $section)) { |
1491 | return false; |
1492 | } |
1493 | |
1494 | if (!$sectiondestrecord = get_record("course_sections", "course", $course->id, "section", $sectiondest)) { |
1495 | return false; |
1496 | } |
1497 | |
56e34ee4 |
1498 | if (!set_field("course_sections", "section", $sectiondest, "id", $sectionrecord->id)) { |
12905134 |
1499 | return false; |
1500 | } |
56e34ee4 |
1501 | if (!set_field("course_sections", "section", $section, "id", $sectiondestrecord->id)) { |
12905134 |
1502 | return false; |
1503 | } |
798b70a1 |
1504 | // if the focus is on the section that is being moved, then move the focus along |
1505 | if (isset($USER->display[$course->id]) and ($USER->display[$course->id] == $section)) { |
1506 | course_set_display($course->id, $sectiondest); |
1507 | } |
5390cbb7 |
1508 | |
1509 | // Check for duplicates. |
1510 | // There is a very rare case that some sections in the same course have the same section id. |
1511 | if (($count_section = count_records('course_sections', 'course', $course->id) - 1) != $course->numsections) { |
1512 | $sections = get_records_select('course_sections', "course = $course->id AND section > 0", 'section ASC'); |
1513 | $n = 1; |
1514 | foreach ($sections as $section) { |
1515 | if (!set_field('course_sections', 'section', $n, 'id', $section->id)) { |
1516 | return false; |
1517 | } |
1518 | $n++; |
1519 | } |
1520 | if (!set_field('course', 'numsections', $count_section, 'id', $course->id)) { |
1521 | return false; |
1522 | } |
1523 | } |
12905134 |
1524 | return true; |
1525 | } |
1526 | |
1527 | |
7977cffd |
1528 | function moveto_module($mod, $section, $beforemod=NULL) { |
1529 | /// All parameters are objects |
1530 | /// Move the module object $mod to the specified $section |
1531 | /// If $beforemod exists then that is the module |
1532 | /// before which $modid should be inserted |
1533 | |
1534 | /// Remove original module from original section |
1535 | |
1536 | if (! delete_mod_from_section($mod->id, $mod->section)) { |
1537 | notify("Could not delete module from existing section"); |
1538 | } |
1539 | |
1540 | /// Update module itself if necessary |
1541 | |
1542 | if ($mod->section != $section->id) { |
89adb174 |
1543 | $mod->section = $section->id; |
7977cffd |
1544 | if (!update_record("course_modules", $mod)) { |
1545 | return false; |
1546 | } |
48e535bc |
1547 | // if moving to a hidden section then hide module |
1548 | if (!$section->visible) { |
1549 | set_coursemodule_visible($mod->id, 0); |
1550 | } |
7977cffd |
1551 | } |
1552 | |
1553 | /// Add the module into the new section |
1554 | |
1555 | $mod->course = $section->course; |
1556 | $mod->section = $section->section; // need relative reference |
1557 | $mod->coursemodule = $mod->id; |
1558 | |
1559 | if (! add_mod_to_section($mod, $beforemod)) { |
1560 | return false; |
1561 | } |
1562 | |
1563 | return true; |
1564 | |
1565 | } |
1566 | |
24e1eae4 |
1567 | function make_editing_buttons($mod, $absolute=false, $moveselect=true, $indent=-1, $section=-1) { |
810393c8 |
1568 | global $CFG, $USER; |
94361e02 |
1569 | |
3d575e6f |
1570 | static $str; |
37a88449 |
1571 | static $sesskey; |
3d575e6f |
1572 | |
1573 | if (!isset($str)) { |
1574 | $str->delete = get_string("delete"); |
1575 | $str->move = get_string("move"); |
1576 | $str->moveup = get_string("moveup"); |
1577 | $str->movedown = get_string("movedown"); |
1578 | $str->moveright = get_string("moveright"); |
1579 | $str->moveleft = get_string("moveleft"); |
1580 | $str->update = get_string("update"); |
d33a2a6f |
1581 | $str->duplicate = get_string("duplicate"); |
3d575e6f |
1582 | $str->hide = get_string("hide"); |
1583 | $str->show = get_string("show"); |
1584 | $str->clicktochange = get_string("clicktochange"); |
32d03b7b |
1585 | $str->forcedmode = get_string("forcedmode"); |
3d575e6f |
1586 | $str->groupsnone = get_string("groupsnone"); |
1587 | $str->groupsseparate = get_string("groupsseparate"); |
1588 | $str->groupsvisible = get_string("groupsvisible"); |
37a88449 |
1589 | $sesskey = sesskey(); |
1acfbce5 |
1590 | } |
94361e02 |
1591 | |
24e1eae4 |
1592 | if ($section >= 0) { |
75f087b6 |
1593 | $section = '&sr='.$section; // Section return |
24e1eae4 |
1594 | } else { |
1595 | $section = ''; |
1596 | } |
1597 | |
94361e02 |
1598 | if ($absolute) { |
37a88449 |
1599 | $path = $CFG->wwwroot.'/course'; |
dc0dc7d5 |
1600 | } else { |
37a88449 |
1601 | $path = '.'; |
dc0dc7d5 |
1602 | } |
1603 | |
3d575e6f |
1604 | if ($mod->visible) { |
37a88449 |
1605 | $hideshow = '<a title="'.$str->hide.'" href="'.$path.'/mod.php?hide='.$mod->id. |
1606 | '&sesskey='.$sesskey.$section.'"><img'. |
1607 | ' src="'.$CFG->pixpath.'/t/hide.gif" hspace="2" height="11" width="11" '. |
1608 | ' border="0" alt="'.$str->hide.'" /></a> '; |
1acfbce5 |
1609 | } else { |
37a88449 |
1610 | $hideshow = '<a title="'.$str->show.'" href="'.$path.'/mod.php?show='.$mod->id. |
1611 | '&sesskey='.$sesskey.$section.'"><img'. |
1612 | ' src="'.$CFG->pixpath.'/t/show.gif" hspace="2" height="11" width="11" '. |
1613 | ' border="0" alt="'.$str->show.'" /></a> '; |
7977cffd |
1614 | } |
3d575e6f |
1615 | if ($mod->groupmode !== false) { |
1616 | if ($mod->groupmode == SEPARATEGROUPS) { |
32d03b7b |
1617 | $grouptitle = $str->groupsseparate; |
37a88449 |
1618 | $groupimage = $CFG->pixpath.'/t/groups.gif'; |
1619 | $grouplink = $path.'/mod.php?id='.$mod->id.'&groupmode=0&sesskey='.$sesskey; |
3d575e6f |
1620 | } else if ($mod->groupmode == VISIBLEGROUPS) { |
32d03b7b |
1621 | $grouptitle = $str->groupsvisible; |
37a88449 |
1622 | $groupimage = $CFG->pixpath.'/t/groupv.gif'; |
1623 | $grouplink = $path.'/mod.php?id='.$mod->id.'&groupmode=1&sesskey='.$sesskey; |
32d03b7b |
1624 | } else { |
1625 | $grouptitle = $str->groupsnone; |
37a88449 |
1626 | $groupimage = $CFG->pixpath.'/t/groupn.gif'; |
1627 | $grouplink = $path.'/mod.php?id='.$mod->id.'&groupmode=2&sesskey='.$sesskey; |
32d03b7b |
1628 | } |
1629 | if ($mod->groupmodelink) { |
37a88449 |
1630 | $groupmode = '<a title="'.$grouptitle.' ('.$str->clicktochange.')" href="'.$grouplink.'">'. |
1631 | '<img src="'.$groupimage.'" hspace="2" height="11" width="11" '. |
1632 | 'border="0" alt="'.$grouptitle.'" /></a>'; |
3d575e6f |
1633 | } else { |
37a88449 |
1634 | $groupmode = '<img title="'.$grouptitle.' ('.$str->forcedmode.')" '. |
1635 | ' src="'.$groupimage.'" hspace="2" height="11" width="11" '. |
1636 | 'border="0" alt="'.$grouptitle.'" />'; |
3d575e6f |
1637 | } |
1638 | } else { |
1639 | $groupmode = ""; |
1640 | } |
7977cffd |
1641 | |
1642 | if ($moveselect) { |
37a88449 |
1643 | $move = '<a title="'.$str->move.'" href="'.$path.'/mod.php?copy='.$mod->id. |
1644 | '&sesskey='.$sesskey.$section.'"><img'. |
1645 | ' src="'.$CFG->pixpath.'/t/move.gif" hspace="2" height="11" width="11" '. |
1646 | ' border="0" alt="'.$str->move.'" /></a>'; |
493486c4 |
1647 | } else { |
37a88449 |
1648 | $move = '<a title="'.$str->moveup.'" href="'.$path.'/mod.php?id='.$mod->id. |
1649 | '&move=-1&sesskey='.$sesskey.$section.'"><img'. |
1650 | ' src="'.$CFG->pixpath.'/t/up.gif" hspace="2" height="11" width="11" '. |
1651 | ' border="0" alt="'.$str->moveup.'" /></a>'. |
1652 | '<a title="'.$str->movedown.'" href="'.$path.'/mod.php?id='.$mod->id. |
1653 | '&move=1&sesskey='.$sesskey.$section.'"><img'. |
1654 | ' src="'.$CFG->pixpath.'/t/down.gif" hspace="2" height="11" width="11" '. |
1655 | ' border="0" alt="'.$str->movedown.'" /></a>'; |
7977cffd |
1656 | } |
1657 | |
aac94fd0 |
1658 | $leftright = ""; |
1659 | if ($indent > 0) { |
37a88449 |
1660 | $leftright .= '<a title="'.$str->moveleft.'" href="'.$path.'/mod.php?id='.$mod->id. |
1661 | '&indent=-1&sesskey='.$sesskey.$section.'"><img'. |
1662 | ' src="'.$CFG->pixpath.'/t/left.gif" hspace="2" height="11" width="11" '. |
1663 | ' border="0" alt="'.$str->moveleft.'" /></a>'; |
aac94fd0 |
1664 | } |
1665 | if ($indent >= 0) { |
37a88449 |
1666 | $leftright .= '<a title="'.$str->moveright.'" href="'.$path.'/mod.php?id='.$mod->id. |
1667 | '&indent=1&sesskey='.$sesskey.$section.'"><img'. |
1668 | ' src="'.$CFG->pixpath.'/t/right.gif" hspace="2" height="11" width="11" '. |
1669 | ' border="0" alt="'.$str->moveright.'" /></a>'; |
1670 | } |
1671 | |
1672 | return '<span class="commands">'.$leftright.$move. |
1673 | '<a title="'.$str->update.'" href="'.$path.'/mod.php?update='.$mod->id. |
1674 | '&sesskey='.$sesskey.$section.'"><img'. |
1675 | ' src="'.$CFG->pixpath.'/t/edit.gif" hspace="2" height="11" width="11" border="0" '. |
1676 | ' alt="'.$str->update.'" /></a>'. |
1677 | '<a title="'.$str->delete.'" href="'.$path.'/mod.php?delete='.$mod->id. |
1678 | '&sesskey='.$sesskey.$section.'"><img'. |
1679 | ' src="'.$CFG->pixpath.'/t/delete.gif" hspace="2" height="11" width="11" border="0" '. |
1680 | ' alt="'.$str->delete.'" /></a>'.$hideshow.$groupmode.'</span>'; |
90845098 |
1681 | } |
1682 | |
b61efafb |
1683 | /** |
1684 | * given a course object with shortname & fullname, this function will |
1685 | * truncate the the number of chars allowed and add ... if it was too long |
1686 | */ |
1687 | function course_format_name ($course,$max=100) { |
1688 | |
1689 | $str = $course->shortname.': '.$course->fullname; |
1690 | if (strlen($str) <= $max) { |
1691 | return $str; |
1692 | } |
1693 | else { |
1694 | return substr($str,0,$max-3).'...'; |
1695 | } |
1696 | } |
1697 | |
1698 | /** |
1699 | * This function will return true if the given course is a child course at all |
1700 | */ |
1701 | function course_in_meta ($course) { |
5f37b628 |
1702 | return record_exists("course_meta","child_course",$course->id); |
b61efafb |
1703 | } |
1704 | |
48e535bc |
1705 | |
1706 | /** |
1707 | * Print standard form elements on module setup forms in mod/.../mod.html |
1708 | */ |
1709 | function print_standard_coursemodule_settings($form) { |
da2224f8 |
1710 | if (! $course = get_record('course', 'id', $form->course)) { |
1711 | error("This course doesn't exist"); |
1712 | } |
1713 | print_groupmode_setting($form, $course); |
1714 | print_visible_setting($form, $course); |
48e535bc |
1715 | } |
1716 | |
1717 | /** |
1718 | * Print groupmode form element on module setup forms in mod/.../mod.html |
1719 | */ |
5ebb746b |
1720 | function print_groupmode_setting($form, $course=NULL) { |
48e535bc |
1721 | |
5ebb746b |
1722 | if (empty($course)) { |
1723 | if (! $course = get_record('course', 'id', $form->course)) { |
1724 | error("This course doesn't exist"); |
1725 | } |
1726 | } |
48e535bc |
1727 | if ($form->coursemodule) { |
1728 | if (! $cm = get_record('course_modules', 'id', $form->coursemodule)) { |
1729 | error("This course module doesn't exist"); |
1730 | } |
1731 | } else { |
1732 | $cm = null; |
1733 | } |
1734 | $groupmode = groupmode($course, $cm); |
1735 | if ($course->groupmode or (!$course->groupmodeforce)) { |
1736 | echo '<tr valign="top">'; |
1737 | echo '<td align="right"><b>'.get_string('groupmode').':</b></td>'; |
7bbe08a2 |
1738 | echo '<td align="left">'; |
48e535bc |
1739 | unset($choices); |
1740 | $choices[NOGROUPS] = get_string('groupsnone'); |
1741 | $choices[SEPARATEGROUPS] = get_string('groupsseparate'); |
1742 | $choices[VISIBLEGROUPS] = get_string('groupsvisible'); |
1743 | choose_from_menu($choices, 'groupmode', $groupmode, '', '', 0, false, $course->groupmodeforce); |
1744 | helpbutton('groupmode', get_string('groupmode')); |
1745 | echo '</td></tr>'; |
1746 | } |
1747 | } |
1748 | |
1749 | /** |
1750 | * Print visibility setting form element on module setup forms in mod/.../mod.html |
1751 | */ |
5ebb746b |
1752 | function print_visible_setting($form, $course=NULL) { |
1ee55c41 |
1753 | if (empty($course)) { |
1754 | if (! $course = get_record('course', 'id', $form->course)) { |
1755 | error("This course doesn't exist"); |
1756 | } |
1757 | } |
48e535bc |
1758 | if ($form->coursemodule) { |
1759 | $visible = get_field('course_modules', 'visible', 'id', $form->coursemodule); |
1760 | } else { |
1761 | $visible = true; |
1762 | } |
1763 | |
1764 | if ($form->mode == 'add') { // in this case $form->section is the section number, not the id |
1765 | $hiddensection = !get_field('course_sections', 'visible', 'section', $form->section, 'course', $form->course); |
1766 | } else { |
1767 | $hiddensection = !get_field('course_sections', 'visible', 'id', $form->section); |
1768 | } |
1769 | if ($hiddensection) { |
1770 | $visible = false; |
1771 | } |
1772 | |
1773 | echo '<tr valign="top">'; |
1f15db9d |
1774 | echo '<td align="right"><b>'.get_string('visibletostudents','',moodle_strtolower($course->students)).':</b></td>'; |
7bbe08a2 |
1775 | echo '<td align="left">'; |
48e535bc |
1776 | unset($choices); |
1f15db9d |
1777 | $choices[1] = get_string('show'); |
1778 | $choices[0] = get_string('hide'); |
48e535bc |
1779 | choose_from_menu($choices, 'visible', $visible, '', '', 0, false, $hiddensection); |
1780 | echo '</td></tr>'; |
1781 | } |
1782 | |
0705ff84 |
1783 | function update_restricted_mods($course,$mods) { |
1784 | delete_records("course_allowed_modules","course",$course->id); |
1785 | if (empty($course->restrictmodules)) { |
1786 | return; |
1787 | } |
1788 | else { |
1789 | foreach ($mods as $mod) { |
1790 | if ($mod == 0) |
1791 | continue; // this is the 'allow none' option |
1792 | $am->course = $course->id; |
1793 | $am->module = $mod; |
1794 | insert_record("course_allowed_modules",$am); |
1795 | } |
1796 | } |
1797 | } |
1798 | |
1799 | /** |
1800 | * This function will take an int (module id) or a string (module name) |
1801 | * and return true or false, whether it's allowed in the given course (object) |
1802 | * $mod is not allowed to be an object, as the field for the module id is inconsistent |
1803 | * depending on where in the code it's called from (sometimes $mod->id, sometimes $mod->module) |
1804 | */ |
1805 | |
1806 | function course_allowed_module($course,$mod) { |
1807 | if (empty($course->restrictmodules)) { |
1808 | return true; |
1809 | } |
1810 | if (isadmin()) { |
1811 | return true; |
1812 | } |
1813 | if (is_numeric($mod)) { |
1814 | $modid = $mod; |
1815 | } else if (is_string($mod)) { |
1816 | if ($mod = get_field("modules","id","name",strtolower($mod))) |
1817 | $modid = $mod; |
1818 | } |
1819 | if (empty($modid)) { |
1820 | return false; |
1821 | } |
1822 | return (record_exists("course_allowed_modules","course",$course->id,"module",$modid)); |
1823 | } |
1824 | |
861efb19 |
1825 | /*** |
1826 | *** Efficiently moves many courses around while maintaining |
1827 | *** sortorder in order. |
1828 | *** |
1829 | *** $courseids is an array of course ids |
1830 | *** |
1831 | **/ |
1832 | |
1833 | function move_courses ($courseids, $categoryid) { |
1834 | |
1835 | global $CFG; |
1836 | |
1837 | if (!empty($courseids)) { |
1838 | |
1839 | $courseids = array_reverse($courseids); |
1840 | |
1841 | foreach ($courseids as $courseid) { |
1842 | |
1843 | if (! $course = get_record("course", "id", $courseid)) { |
1844 | notify("Error finding course $courseid"); |
1845 | } else { |
1846 | // figure out a sortorder that we can use in the destination category |
1847 | $sortorder = get_field_sql('SELECT MIN(sortorder)-1 AS min |
1848 | FROM ' . $CFG->prefix . 'course WHERE category=' . $categoryid); |
1849 | if ($sortorder === false) { |
1850 | // the category is empty |
1851 | // rather than let the db default to 0 |
1852 | // set it to > 100 and avoid extra work in fix_coursesortorder() |
1853 | $sortorder = 200; |
1854 | } else if ($sortorder < 10) { |
1855 | fix_course_sortorder($categoryid); |
1856 | } |
1857 | |
1858 | $course->category = $categoryid; |
1859 | $course->sortorder = $sortorder; |
1860 | $course->fullname = addslashes($course->fullname); |
1861 | $course->shortname = addslashes($course->shortname); |
1862 | $course->summary = addslashes($course->summary); |
1863 | $course->password = addslashes($course->password); |
1864 | $course->teacher = addslashes($course->teacher); |
1865 | $course->teachers = addslashes($course->teachers); |
1866 | $course->student = addslashes($course->student); |
1867 | $course->students = addslashes($course->students); |
1868 | |
1869 | if (!update_record('course', $course)) { |
1870 | notify("An error occurred - course not moved!"); |
1871 | } |
1872 | } |
1873 | } |
1874 | fix_course_sortorder(); |
1875 | } |
1876 | return true; |
1877 | } |
1878 | |
87d32352 |
1879 | ?> |