SCORM MDL-25891 typo causing general exception for some SCORM objects - thanks to...
[moodle.git] / course / recent.php
blob143ff26c591bcd9f73c1ac07277c58c4156d3221
1 <?php // $Id$
3 // Display all recent activity in a flexible way
5 require_once('../config.php');
6 require_once('lib.php');
7 require_once('recent_form.php');
9 $id = required_param('id', PARAM_INT);
11 if (!$course = get_record('course', 'id', $id) ) {
12 error("That's an invalid course id");
15 require_login($course);
17 add_to_log($course->id, "course", "recent", "recent.php?id=$course->id", $course->id);
19 $context = get_context_instance(CONTEXT_COURSE, $course->id);
21 $meta = '<meta name="robots" content="none" />'; // prevent duplicate content in search engines MDL-7299
23 $lastlogin = time() - COURSE_MAX_RECENT_PERIOD;
24 if (!isguestuser() and !empty($USER->lastcourseaccess[$COURSE->id])) {
25 if ($USER->lastcourseaccess[$COURSE->id] > $lastlogin) {
26 $lastlogin = $USER->lastcourseaccess[$COURSE->id];
30 $param = new object();
31 $param->user = 0;
32 $param->modid = 'all';
33 $param->group = 0;
34 $param->sortby = 'default';
35 $param->date = $lastlogin;
36 $param->id = $COURSE->id;
38 $mform = new recent_form();
39 $mform->set_data($param);
40 if ($formdata = $mform->get_data(false)) {
41 $param = $formdata;
44 $userinfo = get_string('allparticipants');
45 $dateinfo = get_string('alldays');
47 if (!empty($param->user)) {
48 if (!$u = get_record('user', 'id', $param->user) ) {
49 error("That's an invalid user!");
51 $userinfo = fullname($u);
54 $strrecentactivity = get_string('recentactivity');
55 $navlinks = array();
56 $navlinks[] = array('name' => $strrecentactivity, 'link' => "recent.php?id=$course->id", 'type' => 'misc');
57 $navlinks[] = array('name' => $userinfo, 'link' => null, 'type' => 'misc');
58 $navigation = build_navigation($navlinks);
59 print_header("$course->shortname: $strrecentactivity", $course->fullname, $navigation, '', $meta);
60 print_heading(format_string($course->fullname) . ": $userinfo", '', 3);
62 $mform->display();
64 $modinfo =& get_fast_modinfo($course);
65 get_all_mods($course->id, $mods, $modnames, $modnamesplural, $modnamesused);
67 if (has_capability('moodle/course:viewhiddensections', $context)) {
68 $hiddenfilter = "";
69 } else {
70 $hiddenfilter = "AND cs.visible = 1";
72 $sections = array();
73 if ($ss = get_records_sql("SELECT cs.id, cs.section, cs.sequence, cs.summary, cs.visible
74 FROM {$CFG->prefix}course_sections cs
75 WHERE cs.course = $course->id AND cs.section <= $course->numsections
76 $hiddenfilter
77 ORDER BY section")) {
78 foreach ($ss as $section) {
79 $sections[$section->section] = $section;
83 if ($param->modid === 'all') {
84 // ok
86 } else if (strpos($param->modid, 'mod/') === 0) {
87 $modname = substr($param->modid, strlen('mod/'));
88 if (array_key_exists($modname, $modnames) and file_exists("$CFG->dirroot/mod/$modname/lib.php")) {
89 $filter = $modname;
92 } else if (strpos($param->modid, 'section/') === 0) {
93 $sectionid = substr($param->modid, strlen('section/'));
94 if (isset($sections[$sectionid])) {
95 $sections = array($sectionid=>$sections[$sectionid]);
98 } else if (is_numeric($param->modid)) {
99 $section = $sections[$modinfo->cms[$param->modid]->sectionnum];
100 $section->sequence = $param->modid;
101 $sections = array($section->sequence=>$section);
104 switch ($course->format) {
105 case 'weeks': $sectiontitle = get_string('week'); break;
106 case 'topics': $sectiontitle = get_string('topic'); break;
107 default: $sectiontitle = get_string('section'); break;
110 if (is_null($modinfo->groups)) {
111 $modinfo->groups = groups_get_user_groups($course->id); // load all my groups and cache it in modinfo
114 $activities = array();
115 $index = 0;
117 foreach ($sections as $section) {
119 $activity = new object();
120 $activity->type = 'section';
121 if ($section->section > 0) {
122 $activity->name = $sectiontitle.' '.$section->section;
123 } else {
124 $activity->name = '';
127 $activity->visible = $section->visible;
128 $activities[$index++] = $activity;
130 if (empty($section->sequence)) {
131 continue;
134 $sectionmods = explode(",", $section->sequence);
136 foreach ($sectionmods as $cmid) {
137 if (!isset($mods[$cmid]) or !isset($modinfo->cms[$cmid])) {
138 continue;
141 $cm = $modinfo->cms[$cmid];
143 if (!$cm->uservisible) {
144 continue;
147 if (!empty($filter) and $cm->modname != $filter) {
148 continue;
151 $libfile = "$CFG->dirroot/mod/$cm->modname/lib.php";
153 if (file_exists($libfile)) {
154 require_once($libfile);
155 $get_recent_mod_activity = $cm->modname."_get_recent_mod_activity";
157 if (function_exists($get_recent_mod_activity)) {
158 $activity = new object();
159 $activity->type = 'activity';
160 $activity->cmid = $cmid;
161 $activities[$index++] = $activity;
162 $get_recent_mod_activity($activities, $index, $param->date, $course->id, $cmid, $param->user, $param->group);
168 $detail = true;
170 switch ($param->sortby) {
171 case 'datedesc' : usort($activities, 'compare_activities_by_time_desc'); break;
172 case 'dateasc' : usort($activities, 'compare_activities_by_time_asc'); break;
173 case 'default' :
174 default : $detail = false; $param->sortby = 'default';
178 if (!empty($activities)) {
180 $newsection = true;
181 $lastsection = '';
182 $newinstance = true;
183 $lastinstance = '';
184 $inbox = false;
186 $section = 0;
188 $activity_count = count($activities);
189 $viewfullnames = array();
191 foreach ($activities as $key => $activity) {
193 if ($activity->type == 'section') {
194 if ($param->sortby != 'default') {
195 continue; // no section if ordering by date
197 if ($activity_count == ($key + 1) or $activities[$key+1]->type == 'section') {
198 // peak at next activity. If it's another section, don't print this one!
199 // this means there are no activities in the current section
200 continue;
204 if (($activity->type == 'section') && ($param->sortby == 'default')) {
205 if ($inbox) {
206 print_simple_box_end();
207 print_spacer(30);
209 print_simple_box_start('center', '90%');
210 echo "<h2>$activity->name</h2>";
211 $inbox = true;
213 } else if ($activity->type == 'activity') {
215 if ($param->sortby == 'default') {
216 $cm = $modinfo->cms[$activity->cmid];
218 if ($cm->visible) {
219 $linkformat = '';
220 } else {
221 $linkformat = 'class="dimmed"';
223 $name = format_string($cm->name);
224 $modfullname = $modnames[$cm->modname];
226 $image = "<img src=\"$CFG->modpixpath/$cm->modname/icon.gif\" class=\"icon\" alt=\"$modfullname\" />";
227 echo "<h4>$image $modfullname".
228 " <a href=\"$CFG->wwwroot/mod/$cm->modname/view.php?id=$cm->id\" $linkformat>$name</a></h4>";
231 } else {
233 if (!isset($viewfullnames[$activity->cmid])) {
234 $cm_context = get_context_instance(CONTEXT_MODULE, $activity->cmid);
235 $viewfullnames[$activity->cmid] = has_capability('moodle/site:viewfullnames', $cm_context);
238 if (!$inbox) {
239 print_simple_box_start('center', '90%');
240 $inbox = true;
243 $print_recent_mod_activity = $activity->type.'_print_recent_mod_activity';
245 if (function_exists($print_recent_mod_activity)) {
246 $print_recent_mod_activity($activity, $course->id, $detail, $modnames, $viewfullnames[$activity->cmid]);
251 if ($inbox) {
252 print_simple_box_end();
256 } else {
258 echo '<h4><center>' . get_string('norecentactivity') . '</center></h2>';
262 print_footer($course);
264 function compare_activities_by_time_desc($a, $b) {
265 // make sure the activities actually have a timestamp property
266 if ((!array_key_exists('timestamp', $a)) or (!array_key_exists('timestamp', $b))) {
267 return 0;
269 if ($a->timestamp == $b->timestamp)
270 return 0;
271 return ($a->timestamp > $b->timestamp) ? -1 : 1;
274 function compare_activities_by_time_asc($a, $b) {
275 // make sure the activities actually have a timestamp property
276 if ((!array_key_exists('timestamp', $a)) or (!array_key_exists('timestamp', $b))) {
277 return 0;
279 if ($a->timestamp == $b->timestamp)
280 return 0;
281 return ($a->timestamp < $b->timestamp) ? -1 : 1;