MDL-11515:
[moodle-linuxchix.git] / course / recent.php
blobcdf1de224488bfd2746c9c305b5c3094e21b48ce
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'");
168 if (!$coursemod) {
169 continue;
172 $groupmode = groups_get_activity_groupmode($coursemod);
173 switch ($groupmode) {
174 case SEPARATEGROUPS :
175 $groupid = 0;
176 if ($mygroups = groups_get_all_groups($course->id, $USER->id, $coursemod->groupingid)) {
177 if (array_key_exists($selectedgroup, $mygroups)) {
178 $groupid = $selectedgroup;
181 break;
183 case VISIBLEGROUPS : $groupid = $selectedgroup; break;
184 case NOGROUPS :
185 default : $groupid = 0;
188 $libfile = "$CFG->dirroot/mod/$coursemod->name/lib.php";
190 if (file_exists($libfile)) {
191 require_once($libfile);
192 $get_recent_mod_activity = $coursemod->name."_get_recent_mod_activity";
194 if (function_exists($get_recent_mod_activity)) {
195 $activity = new Object;
196 $activity->type = "activity";
197 $activity->name = $instance->name;
198 $activity->visible = $coursemod->visible;
199 $activity->content->modfullname = $mod->modfullname;
200 $activity->content->modname = $mod->modname;
201 $activity->content->modid =$mod->id;
202 $activities[$index] = $activity;
203 $index++;
204 $get_recent_mod_activity($activities, $index, $date, $course->id, $sectionmod, $user, $groupid);
211 $detail = true;
213 switch ($sortby) {
214 case "datedesc" : usort($activities, "compare_activities_by_time_desc"); break;
215 case "dateasc" : usort($activities, "compare_activities_by_time_asc"); break;
216 case "default" :
217 default : $detail = false; $sortby = "default";
221 if (!empty($activities)) {
223 $newsection = true;
224 $lastsection = "";
225 $newinstance = true;
226 $lastinstance = "";
227 $inbox = false;
229 $section = 0;
231 if (has_capability('moodle/course:viewhiddensections', get_context_instance(CONTEXT_COURSE, $course->id))) {
232 $teacher = true;
233 } else {
234 $teacher = false;
236 $activity_count = count($activities);
238 foreach ($activities as $key => $activity) {
240 // peak at next activity. If it's another section, don't print this one!
241 // this means there are no activities in the current section
242 if (($activity->type == "section") &&
243 (($activity_count == ($key + 1)) ||
244 ($activities[$key+1]->type == "section"))) {
246 continue;
250 if (($activity->type == "section") && ($sortby == "default")) {
251 if ($inbox) {
252 print_simple_box_end();
253 print_spacer(30);
255 print_simple_box_start("center", "90%");
256 echo "<h2>$activity->name</h2>";
257 $inbox = true;
259 } else if ($activity->type == "activity") {
261 if ($sortby == "default") {
262 if ($teacher && $activity->visible == 0) {
263 $linkformat = 'class="dimmed"';
264 } else {
265 $linkformat = '';
267 $image = "<img src=\"$CFG->modpixpath/" . $activity->content->modname . "/icon.gif\"" .
268 " class=\"icon\" alt=\"" . $activity->content->modfullname . "\" />";
269 echo "<ul><h4>$image " . $activity->content->modfullname .
270 " <a href=\"$CFG->wwwroot/mod/" . $activity->content->modname . "/view.php?" .
271 "id=" . $activity->content->modid . "\" $linkformat>" .
272 format_string($activity->name,true) . "</a></h4></ul>";
275 } else {
277 if (!$inbox) {
278 print_simple_box_start("center", "90%");
279 $inbox = true;
282 $print_recent_mod_activity = $activity->type."_print_recent_mod_activity";
284 if (function_exists($print_recent_mod_activity)) {
285 echo '<ul><ul>';
286 $print_recent_mod_activity($activity, $course->id, $detail);
287 echo '</ul></ul>';
292 if ($inbox) {
293 print_simple_box_end();
297 } else {
299 echo "<h4><center>" . get_string("norecentactivity") . "</center></h2>";
302 // fix modid for selection form
303 $modid =$tmpmodid;
305 print_footer($course);
307 function compare_activities_by_time_desc($a, $b) {
308 // make sure the activities actually have a timestamp property
309 if ((!object_property_exists($a,'timestamp')) or (!object_property_exists($b,'timestamp'))) {
310 return 0;
312 if ($a->timestamp == $b->timestamp)
313 return 0;
314 return ($a->timestamp > $b->timestamp) ? -1 : 1;
317 function compare_activities_by_time_asc($a, $b) {
318 // make sure the activities actually have a timestamp property
319 if ((!object_property_exists($a,'timestamp')) or (!object_property_exists($b,'timestamp'))) {
320 return 0;
322 if ($a->timestamp == $b->timestamp)
323 return 0;
324 return ($a->timestamp < $b->timestamp) ? -1 : 1;