Merge branch 'MDL-29269' of git://github.com/stronk7/moodle
[moodle.git] / course / recent.php
blob134cce0e07be6ac4468dffe179efd698c36b506e
1 <?php
3 // This file is part of Moodle - http://moodle.org/
4 //
5 // Moodle is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
9 //
10 // Moodle is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
18 /**
19 * Display all recent activity in a flexible way
21 * @copyright 1999 Martin Dougiamas http://dougiamas.com
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23 * @package course
26 require_once('../config.php');
27 require_once('lib.php');
28 require_once('recent_form.php');
30 $id = required_param('id', PARAM_INT);
32 $PAGE->set_url('/course/recent.php', array('id'=>$id));
34 if (!$course = $DB->get_record('course', array('id'=>$id))) {
35 print_error("That's an invalid course id");
38 require_login($course);
40 add_to_log($course->id, "course", "recent", "recent.php?id=$course->id", $course->id);
42 $context = get_context_instance(CONTEXT_COURSE, $course->id);
44 $lastlogin = time() - COURSE_MAX_RECENT_PERIOD;
45 if (!isguestuser() and !empty($USER->lastcourseaccess[$COURSE->id])) {
46 if ($USER->lastcourseaccess[$COURSE->id] > $lastlogin) {
47 $lastlogin = $USER->lastcourseaccess[$COURSE->id];
51 $param = new stdClass();
52 $param->user = 0;
53 $param->modid = 'all';
54 $param->group = 0;
55 $param->sortby = 'default';
56 $param->date = $lastlogin;
57 $param->id = $COURSE->id;
59 $mform = new recent_form();
60 $mform->set_data($param);
61 if ($formdata = $mform->get_data()) {
62 $param = $formdata;
65 $userinfo = get_string('allparticipants');
66 $dateinfo = get_string('alldays');
68 if (!empty($param->user)) {
69 if (!$u = $DB->get_record('user', array('id'=>$param->user))) {
70 print_error("That's an invalid user!");
72 $userinfo = fullname($u);
75 $strrecentactivity = get_string('recentactivity');
76 $PAGE->navbar->add($strrecentactivity, new moodle_url('/course/recent.php', array('id'=>$course->id)));
77 $PAGE->navbar->add($userinfo);
78 $PAGE->set_title("$course->shortname: $strrecentactivity");
79 $PAGE->set_heading($course->fullname);
80 echo $OUTPUT->header();
81 echo $OUTPUT->heading(format_string($course->fullname) . ": $userinfo", 3);
83 $mform->display();
85 $modinfo =& get_fast_modinfo($course);
86 get_all_mods($course->id, $mods, $modnames, $modnamesplural, $modnamesused);
88 if (has_capability('moodle/course:viewhiddensections', $context)) {
89 $hiddenfilter = "";
90 } else {
91 $hiddenfilter = "AND cs.visible = 1";
93 $sections = array();
94 $rawsections = array_slice(get_all_sections($course->id), 0, $course->numsections+1, true);
95 $canviewhidden = has_capability('moodle/course:viewhiddensections', $context);
96 foreach ($rawsections as $section) {
97 if ($canviewhidden || !empty($section->visible)) {
98 $sections[$section->section] = $section;
102 if ($param->modid === 'all') {
103 // ok
105 } else if (strpos($param->modid, 'mod/') === 0) {
106 $modname = substr($param->modid, strlen('mod/'));
107 if (array_key_exists($modname, $modnames) and file_exists("$CFG->dirroot/mod/$modname/lib.php")) {
108 $filter = $modname;
111 } else if (strpos($param->modid, 'section/') === 0) {
112 $sectionid = substr($param->modid, strlen('section/'));
113 if (isset($sections[$sectionid])) {
114 $sections = array($sectionid=>$sections[$sectionid]);
117 } else if (is_numeric($param->modid)) {
118 $section = $sections[$modinfo->cms[$param->modid]->sectionnum];
119 $section->sequence = $param->modid;
120 $sections = array($section->sequence=>$section);
124 if (is_null($modinfo->groups)) {
125 $modinfo->groups = groups_get_user_groups($course->id); // load all my groups and cache it in modinfo
128 $activities = array();
129 $index = 0;
131 foreach ($sections as $section) {
133 $activity = new stdClass();
134 $activity->type = 'section';
135 if ($section->section > 0) {
136 $activity->name = get_section_name($course, $section);
137 } else {
138 $activity->name = '';
141 $activity->visible = $section->visible;
142 $activities[$index++] = $activity;
144 if (empty($section->sequence)) {
145 continue;
148 $sectionmods = explode(",", $section->sequence);
150 foreach ($sectionmods as $cmid) {
151 if (!isset($mods[$cmid]) or !isset($modinfo->cms[$cmid])) {
152 continue;
155 $cm = $modinfo->cms[$cmid];
157 if (!$cm->uservisible) {
158 continue;
161 if (!empty($filter) and $cm->modname != $filter) {
162 continue;
165 $libfile = "$CFG->dirroot/mod/$cm->modname/lib.php";
167 if (file_exists($libfile)) {
168 require_once($libfile);
169 $get_recent_mod_activity = $cm->modname."_get_recent_mod_activity";
171 if (function_exists($get_recent_mod_activity)) {
172 $activity = new stdClass();
173 $activity->type = 'activity';
174 $activity->cmid = $cmid;
175 $activities[$index++] = $activity;
176 $get_recent_mod_activity($activities, $index, $param->date, $course->id, $cmid, $param->user, $param->group);
182 $detail = true;
184 switch ($param->sortby) {
185 case 'datedesc' : usort($activities, 'compare_activities_by_time_desc'); break;
186 case 'dateasc' : usort($activities, 'compare_activities_by_time_asc'); break;
187 case 'default' :
188 default : $detail = false; $param->sortby = 'default';
192 if (!empty($activities)) {
194 $newsection = true;
195 $lastsection = '';
196 $newinstance = true;
197 $lastinstance = '';
198 $inbox = false;
200 $section = 0;
202 $activity_count = count($activities);
203 $viewfullnames = array();
205 foreach ($activities as $key => $activity) {
207 if ($activity->type == 'section') {
208 if ($param->sortby != 'default') {
209 continue; // no section if ordering by date
211 if ($activity_count == ($key + 1) or $activities[$key+1]->type == 'section') {
212 // peak at next activity. If it's another section, don't print this one!
213 // this means there are no activities in the current section
214 continue;
218 if (($activity->type == 'section') && ($param->sortby == 'default')) {
219 if ($inbox) {
220 echo $OUTPUT->box_end();
221 echo $OUTPUT->spacer(array('height'=>30, 'br'=>true)); // should be done with CSS instead
223 echo $OUTPUT->box_start();
224 echo "<h2>$activity->name</h2>";
225 $inbox = true;
227 } else if ($activity->type == 'activity') {
229 if ($param->sortby == 'default') {
230 $cm = $modinfo->cms[$activity->cmid];
232 if ($cm->visible) {
233 $linkformat = '';
234 } else {
235 $linkformat = 'class="dimmed"';
237 $name = format_string($cm->name);
238 $modfullname = $modnames[$cm->modname];
240 $image = "<img src=\"" . $OUTPUT->pix_url('icon', $cm->modname) . "\" class=\"icon\" alt=\"$modfullname\" />";
241 echo "<h4>$image $modfullname".
242 " <a href=\"$CFG->wwwroot/mod/$cm->modname/view.php?id=$cm->id\" $linkformat>$name</a></h4>";
245 } else {
247 if (!isset($viewfullnames[$activity->cmid])) {
248 $cm_context = get_context_instance(CONTEXT_MODULE, $activity->cmid);
249 $viewfullnames[$activity->cmid] = has_capability('moodle/site:viewfullnames', $cm_context);
252 if (!$inbox) {
253 echo $OUTPUT->box_start();
254 $inbox = true;
257 $print_recent_mod_activity = $activity->type.'_print_recent_mod_activity';
259 if (function_exists($print_recent_mod_activity)) {
260 $print_recent_mod_activity($activity, $course->id, $detail, $modnames, $viewfullnames[$activity->cmid]);
265 if ($inbox) {
266 echo $OUTPUT->box_end();
270 } else {
272 echo '<h4><center>' . get_string('norecentactivity') . '</center></h2>';
276 echo $OUTPUT->footer();
278 function compare_activities_by_time_desc($a, $b) {
279 // make sure the activities actually have a timestamp property
280 if ((!array_key_exists('timestamp', $a)) or (!array_key_exists('timestamp', $b))) {
281 return 0;
283 if ($a->timestamp == $b->timestamp)
284 return 0;
285 return ($a->timestamp > $b->timestamp) ? -1 : 1;
288 function compare_activities_by_time_asc($a, $b) {
289 // make sure the activities actually have a timestamp property
290 if ((!array_key_exists('timestamp', $a)) or (!array_key_exists('timestamp', $b))) {
291 return 0;
293 if ($a->timestamp == $b->timestamp)
294 return 0;
295 return ($a->timestamp < $b->timestamp) ? -1 : 1;