MDL-11517 reserved word MOD used in table alias in questions backup code
[moodle-pu.git] / course / recent.php
blobb84172cc314aa65e791c1d4109dda045ac9c9d4a
1 <?php // $Id$
3 // Display all recent activity in a flexible way
5 require_once("../config.php");
6 require_once("lib.php");
8 $id = required_param('id', PARAM_INT);
9 $user = optional_param('user', '0', PARAM_INT);
10 $sortby = optional_param('sortby', 'default', PARAM_ALPHA);
11 $selectedgroup = optional_param('selectedgroup', 0, PARAM_INT);
12 $date = optional_param('date', '', PARAM_INT);
13 $advancedfilter = optional_param('advancedfilter', 0, PARAM_INT);
14 $modname = optional_param('modname', '', PARAM_ALPHA); // not used??
15 $modid = optional_param('modid', 'activity/All', PARAM_FILE); // not a file, but looks like it anyway
16 $modaction = optional_param('modaction', '', PARAM_ALPHA); // not used??
17 $chooserecent = optional_param('chooserecent', 0, PARAM_INT);
19 if (! $course = get_record("course", "id", $id) ) {
20 error("That's an invalid course id");
23 require_login($course->id);
25 add_to_log($course->id, "course", "recent", "recent.php?id=$course->id", "$course->id");
27 $strrecentactivity = get_string("recentactivity");
28 $meta = '<meta name="robots" content="none" />'; // prevent duplicate content in search engines MDL-7299
30 $loggedinas = user_login_string($course, $USER);
31 $navlinks = array();
33 if (!empty($chooserecent)) {
34 $userinfo = get_string("allparticipants");
35 $dateinfo = get_string("alldays");
37 if ($user) {
38 if (!$u = get_record("user", "id", $user) ) {
39 error("That's an invalid user!");
41 $userinfo = fullname($u);
43 if ($date)
44 $dateinfo = userdate($date, get_string("strftimedaydate"));
46 $navlinks[] = array('name' => $strrecentactivity, 'link' => "recent.php?id=$course->id", 'type' => 'misc');
47 $navlinks[] = array('name' => "$userinfo, $dateinfo", 'link' => null, 'type' => 'misc');
48 $navigation = build_navigation($navlinks);
49 print_header("$course->shortname: $strrecentactivity", $course->fullname, $navigation, "", $meta);
51 print_heading(format_string($course->fullname) . ": $userinfo, $dateinfo (".usertimezone().")", '', 3);
52 $advancedfilter = 1;
53 print_recent_selector_form($course, $advancedfilter, $user, $date, $modname, $modid, $modaction, $selectedgroup, $sortby);
55 } else {
57 if (empty($date)) { // no date picked, default to last login time
58 $date = time() - COURSE_MAX_RECENT_PERIOD;
60 if (!empty($USER->lastcourseaccess[$course->id])) {
61 if ($USER->lastcourseaccess[$course->id] > $date) {
62 $date = $USER->lastcourseaccess[$course->id];
66 $navlinks[] = array('name' => $strrecentactivity, 'link' => null, 'type' => 'misc');
67 $navigation = build_navigation($navlinks);
68 print_header("$course->shortname: $strrecentactivity", $course->fullname, $navigation, "", $meta);
70 print_heading(get_string("activitysince", "", userdate($date)));
72 print_recent_selector_form($course, $advancedfilter);
76 $tmpmodid = $modid;
78 switch ($tmpmodid) {
79 case "activity/Assignments" : $filter = "assignment"; break;
80 case "activity/Chats" : $filter = "chat"; break;
81 case "activity/Forums" : $filter = "forum"; break;
82 case "activity/Quizzes" : $filter = "quiz"; break;
83 case "activity/Workshops" : $filter = "workshop"; break;
84 default : $filter = "";
87 $activities = array();
88 $sections = array();
90 switch ($course->format) {
91 case "weeks": $sectiontitle = get_string("week"); break;
92 case "topics": $sectiontitle = get_string("topic"); break;
93 default: $sectiontitle = get_string("section"); break;
96 $index = 0;
98 if (is_numeric($modid)) { // you chose a single activity
100 $sections[0]->sequence = "$modid";
102 } else { // you chose a group of activities
104 if (has_capability('moodle/course:viewhiddensections', get_context_instance(CONTEXT_COURSE, $course->id))) {
105 $hiddenfilter = "";
106 } else {
107 $hiddenfilter = " AND cs.visible = '1' ";
110 $sections = get_records_sql("SELECT cs.id, cs.section, cs.sequence, cs.summary, cs.visible
111 FROM {$CFG->prefix}course_sections cs
112 WHERE course = '$course->id' $hiddenfilter
113 ORDER by section");
116 get_all_mods($course->id, $mods, $modnames, $modnamesplural, $modnamesused);
118 if (!empty($sections)) {
120 echo "<hr />";
121 $i = 0;
123 if (!empty($filter)) {
124 $activityfilter = "AND m.name = '$filter'";
125 } else {
126 $activityfilter = "";
129 if (has_capability('moodle/course:viewhiddensections', get_context_instance(CONTEXT_COURSE, $course->id))) {
130 $hiddenfilter = "";
131 } else {
132 $hiddenfilter = " AND cm.visible = '1' ";
135 foreach ($sections as $section) {
137 if ($i <= $course->numsections) {
138 $activity = new Object;
139 $activity->type = "section";
140 if ($i) {
141 $activity->name = $sectiontitle . " $i";
142 } else {
143 $activity->name = '';
145 $activity->visible = $section->visible;
146 $activities[$index] = $activity;
148 $index++;
149 $i++;
151 $sectionmods = explode(",", $section->sequence);
153 foreach ($sectionmods as $sectionmod) {
155 if (empty($mods[$sectionmod])) {
156 continue;
158 $mod = $mods[$sectionmod];
159 $instance = get_record("$mod->modname", "id", "$mod->instance");
161 $coursemod = get_record_sql("SELECT m.id, m.name, cm.groupmode, cm.visible, cm.course, cm.groupingid
162 FROM {$CFG->prefix}course_modules cm,
163 {$CFG->prefix}modules m
164 WHERE course = '$course->id' $hiddenfilter
165 AND m.id = cm.module $activityfilter
166 AND cm.id = '$sectionmod'");
167 $groupmode = groups_get_activity_groupmode($coursemod);
168 switch ($groupmode) {
169 case SEPARATEGROUPS :
170 $groupid = 0;
171 if ($mygroups = groups_get_all_groups($course->id, $USER->id, $coursemode->groupingid)) {
172 if (array_key_exists($selectedgroup, $mygroups)) {
173 $groupid = $selectedgroup;
176 break;
178 case VISIBLEGROUPS : $groupid = $selectedgroup; break;
179 case NOGROUPS :
180 default : $groupid = 0;
183 $libfile = "$CFG->dirroot/mod/$coursemod->name/lib.php";
185 if (file_exists($libfile)) {
186 require_once($libfile);
187 $get_recent_mod_activity = $coursemod->name."_get_recent_mod_activity";
189 if (function_exists($get_recent_mod_activity)) {
190 $activity = new Object;
191 $activity->type = "activity";
192 $activity->name = $instance->name;
193 $activity->visible = $coursemod->visible;
194 $activity->content->modfullname = $mod->modfullname;
195 $activity->content->modname = $mod->modname;
196 $activity->content->modid =$mod->id;
197 $activities[$index] = $activity;
198 $index++;
199 $get_recent_mod_activity($activities, $index, $date, $course->id, $sectionmod, $user, $groupid);
206 $detail = true;
208 switch ($sortby) {
209 case "datedesc" : usort($activities, "compare_activities_by_time_desc"); break;
210 case "dateasc" : usort($activities, "compare_activities_by_time_asc"); break;
211 case "default" :
212 default : $detail = false; $sortby = "default";
216 if (!empty($activities)) {
218 $newsection = true;
219 $lastsection = "";
220 $newinstance = true;
221 $lastinstance = "";
222 $inbox = false;
224 $section = 0;
226 if (has_capability('moodle/course:viewhiddensections', get_context_instance(CONTEXT_COURSE, $course->id))) {
227 $teacher = true;
228 } else {
229 $teacher = false;
231 $activity_count = count($activities);
233 foreach ($activities as $key => $activity) {
235 // peak at next activity. If it's another section, don't print this one!
236 // this means there are no activities in the current section
237 if (($activity->type == "section") &&
238 (($activity_count == ($key + 1)) ||
239 ($activities[$key+1]->type == "section"))) {
241 continue;
245 if (($activity->type == "section") && ($sortby == "default")) {
246 if ($inbox) {
247 print_simple_box_end();
248 print_spacer(30);
250 print_simple_box_start("center", "90%");
251 echo "<h2>$activity->name</h2>";
252 $inbox = true;
254 } else if ($activity->type == "activity") {
256 if ($sortby == "default") {
257 if ($teacher && $activity->visible == 0) {
258 $linkformat = 'class="dimmed"';
259 } else {
260 $linkformat = '';
262 $image = "<img src=\"$CFG->modpixpath/" . $activity->content->modname . "/icon.gif\"" .
263 " class=\"icon\" alt=\"" . $activity->content->modfullname . "\" />";
264 echo "<ul><h4>$image " . $activity->content->modfullname .
265 " <a href=\"$CFG->wwwroot/mod/" . $activity->content->modname . "/view.php?" .
266 "id=" . $activity->content->modid . "\" $linkformat>" .
267 format_string($activity->name,true) . "</a></h4></ul>";
270 } else {
272 if (!$inbox) {
273 print_simple_box_start("center", "90%");
274 $inbox = true;
277 $print_recent_mod_activity = $activity->type."_print_recent_mod_activity";
279 if (function_exists($print_recent_mod_activity)) {
280 echo '<ul><ul>';
281 $print_recent_mod_activity($activity, $course->id, $detail);
282 echo '</ul></ul>';
287 if ($inbox) {
288 print_simple_box_end();
292 } else {
294 echo "<h4><center>" . get_string("norecentactivity") . "</center></h2>";
297 // fix modid for selection form
298 $modid =$tmpmodid;
300 print_footer($course);
302 function compare_activities_by_time_desc($a, $b) {
303 // make sure the activities actually have a timestamp property
304 if ((!object_property_exists($a,'timestamp')) or (!object_property_exists($b,'timestamp'))) {
305 return 0;
307 if ($a->timestamp == $b->timestamp)
308 return 0;
309 return ($a->timestamp > $b->timestamp) ? -1 : 1;
312 function compare_activities_by_time_asc($a, $b) {
313 // make sure the activities actually have a timestamp property
314 if ((!object_property_exists($a,'timestamp')) or (!object_property_exists($b,'timestamp'))) {
315 return 0;
317 if ($a->timestamp == $b->timestamp)
318 return 0;
319 return ($a->timestamp < $b->timestamp) ? -1 : 1;