Merge branch 'install_23_STABLE' of git://git.moodle.cz/moodle-install into MOODLE_23...
[moodle.git] / course / recent.php
blob6a9e00abb958160b3636930be9c543afefbf20ee
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));
33 $PAGE->set_pagelayout('report');
35 if (!$course = $DB->get_record('course', array('id'=>$id))) {
36 print_error("That's an invalid course id");
39 require_login($course);
41 add_to_log($course->id, "course", "recent", "recent.php?id=$course->id", $course->id);
43 $context = get_context_instance(CONTEXT_COURSE, $course->id);
45 $lastlogin = time() - COURSE_MAX_RECENT_PERIOD;
46 if (!isguestuser() and !empty($USER->lastcourseaccess[$COURSE->id])) {
47 if ($USER->lastcourseaccess[$COURSE->id] > $lastlogin) {
48 $lastlogin = $USER->lastcourseaccess[$COURSE->id];
52 $param = new stdClass();
53 $param->user = 0;
54 $param->modid = 'all';
55 $param->group = 0;
56 $param->sortby = 'default';
57 $param->date = $lastlogin;
58 $param->id = $COURSE->id;
60 $mform = new recent_form();
61 $mform->set_data($param);
62 if ($formdata = $mform->get_data()) {
63 $param = $formdata;
66 $userinfo = get_string('allparticipants');
67 $dateinfo = get_string('alldays');
69 if (!empty($param->user)) {
70 if (!$u = $DB->get_record('user', array('id'=>$param->user))) {
71 print_error("That's an invalid user!");
73 $userinfo = fullname($u);
76 $strrecentactivity = get_string('recentactivity');
77 $PAGE->navbar->add($strrecentactivity, new moodle_url('/course/recent.php', array('id'=>$course->id)));
78 $PAGE->navbar->add($userinfo);
79 $PAGE->set_title("$course->shortname: $strrecentactivity");
80 $PAGE->set_heading($course->fullname);
81 echo $OUTPUT->header();
82 echo $OUTPUT->heading(format_string($course->fullname) . ": $userinfo", 2);
84 $mform->display();
86 $modinfo = get_fast_modinfo($course);
87 get_all_mods($course->id, $mods, $modnames, $modnamesplural, $modnamesused);
89 if (has_capability('moodle/course:viewhiddensections', $context)) {
90 $hiddenfilter = "";
91 } else {
92 $hiddenfilter = "AND cs.visible = 1";
94 $sections = array();
95 $rawsections = array_slice(get_all_sections($course->id), 0, $course->numsections+1, true);
96 $canviewhidden = has_capability('moodle/course:viewhiddensections', $context);
97 foreach ($rawsections as $section) {
98 if ($canviewhidden || !empty($section->visible)) {
99 $sections[$section->section] = $section;
103 if ($param->modid === 'all') {
104 // ok
106 } else if (strpos($param->modid, 'mod/') === 0) {
107 $modname = substr($param->modid, strlen('mod/'));
108 if (array_key_exists($modname, $modnames) and file_exists("$CFG->dirroot/mod/$modname/lib.php")) {
109 $filter = $modname;
112 } else if (strpos($param->modid, 'section/') === 0) {
113 $sectionid = substr($param->modid, strlen('section/'));
114 if (isset($sections[$sectionid])) {
115 $sections = array($sectionid=>$sections[$sectionid]);
118 } else if (is_numeric($param->modid)) {
119 $section = $sections[$modinfo->cms[$param->modid]->sectionnum];
120 $section->sequence = $param->modid;
121 $sections = array($section->sequence=>$section);
125 if (is_null($modinfo->groups)) {
126 $modinfo->groups = groups_get_user_groups($course->id); // load all my groups and cache it in modinfo
129 $activities = array();
130 $index = 0;
132 foreach ($sections as $section) {
134 $activity = new stdClass();
135 $activity->type = 'section';
136 if ($section->section > 0) {
137 $activity->name = get_section_name($course, $section);
138 } else {
139 $activity->name = '';
142 $activity->visible = $section->visible;
143 $activities[$index++] = $activity;
145 if (empty($section->sequence)) {
146 continue;
149 $sectionmods = explode(",", $section->sequence);
151 foreach ($sectionmods as $cmid) {
152 if (!isset($mods[$cmid]) or !isset($modinfo->cms[$cmid])) {
153 continue;
156 $cm = $modinfo->cms[$cmid];
158 if (!$cm->uservisible) {
159 continue;
162 if (!empty($filter) and $cm->modname != $filter) {
163 continue;
166 $libfile = "$CFG->dirroot/mod/$cm->modname/lib.php";
168 if (file_exists($libfile)) {
169 require_once($libfile);
170 $get_recent_mod_activity = $cm->modname."_get_recent_mod_activity";
172 if (function_exists($get_recent_mod_activity)) {
173 $activity = new stdClass();
174 $activity->type = 'activity';
175 $activity->cmid = $cmid;
176 $activities[$index++] = $activity;
177 $get_recent_mod_activity($activities, $index, $param->date, $course->id, $cmid, $param->user, $param->group);
183 $detail = true;
185 switch ($param->sortby) {
186 case 'datedesc' : usort($activities, 'compare_activities_by_time_desc'); break;
187 case 'dateasc' : usort($activities, 'compare_activities_by_time_asc'); break;
188 case 'default' :
189 default : $detail = false; $param->sortby = 'default';
193 if (!empty($activities)) {
195 $newsection = true;
196 $lastsection = '';
197 $newinstance = true;
198 $lastinstance = '';
199 $inbox = false;
201 $section = 0;
203 $activity_count = count($activities);
204 $viewfullnames = array();
206 foreach ($activities as $key => $activity) {
208 if ($activity->type == 'section') {
209 if ($param->sortby != 'default') {
210 continue; // no section if ordering by date
212 if ($activity_count == ($key + 1) or $activities[$key+1]->type == 'section') {
213 // peak at next activity. If it's another section, don't print this one!
214 // this means there are no activities in the current section
215 continue;
219 if (($activity->type == 'section') && ($param->sortby == 'default')) {
220 if ($inbox) {
221 echo $OUTPUT->box_end();
222 echo $OUTPUT->spacer(array('height'=>30, 'br'=>true)); // should be done with CSS instead
224 echo $OUTPUT->box_start();
225 if (!empty($activity->name)) {
226 echo html_writer::tag('h2', $activity->name);
228 $inbox = true;
230 } else if ($activity->type == 'activity') {
232 if ($param->sortby == 'default') {
233 $cm = $modinfo->cms[$activity->cmid];
235 if ($cm->visible) {
236 $class = '';
237 } else {
238 $class = 'dimmed';
240 $name = format_string($cm->name);
241 $modfullname = $modnames[$cm->modname];
243 $image = $OUTPUT->pix_icon('icon', $modfullname, $cm->modname, array('class' => 'icon smallicon'));
244 $link = html_writer::link(new moodle_url("/mod/$cm->modname/view.php",
245 array("id" => $cm->id)), $name, array('class' => $class));
246 echo html_writer::tag('h3', "$image $modfullname $link");
249 } else {
251 if (!isset($viewfullnames[$activity->cmid])) {
252 $cm_context = get_context_instance(CONTEXT_MODULE, $activity->cmid);
253 $viewfullnames[$activity->cmid] = has_capability('moodle/site:viewfullnames', $cm_context);
256 if (!$inbox) {
257 echo $OUTPUT->box_start();
258 $inbox = true;
261 $print_recent_mod_activity = $activity->type.'_print_recent_mod_activity';
263 if (function_exists($print_recent_mod_activity)) {
264 $print_recent_mod_activity($activity, $course->id, $detail, $modnames, $viewfullnames[$activity->cmid]);
269 if ($inbox) {
270 echo $OUTPUT->box_end();
274 } else {
276 echo html_writer::tag('h3', get_string('norecentactivity'), array('class' => 'mdl-align'));
280 echo $OUTPUT->footer();
282 function compare_activities_by_time_desc($a, $b) {
283 // make sure the activities actually have a timestamp property
284 if ((!array_key_exists('timestamp', $a)) && (!array_key_exists('timestamp', $b))) {
285 return 0;
287 // We treat instances without timestamp as if they have a timestamp of 0.
288 if ((!array_key_exists('timestamp', $a)) && (array_key_exists('timestamp', $b))) {
289 return 1;
291 if ((array_key_exists('timestamp', $a)) && (!array_key_exists('timestamp', $b))) {
292 return -1;
294 if ($a->timestamp == $b->timestamp) {
295 return 0;
297 return ($a->timestamp > $b->timestamp) ? -1 : 1;
300 function compare_activities_by_time_asc($a, $b) {
301 // make sure the activities actually have a timestamp property
302 if ((!array_key_exists('timestamp', $a)) && (!array_key_exists('timestamp', $b))) {
303 return 0;
305 // We treat instances without timestamp as if they have a timestamp of 0.
306 if ((!array_key_exists('timestamp', $a)) && (array_key_exists('timestamp', $b))) {
307 return -1;
309 if ((array_key_exists('timestamp', $a)) && (!array_key_exists('timestamp', $b))) {
310 return 1;
312 if ($a->timestamp == $b->timestamp) {
313 return 0;
315 return ($a->timestamp < $b->timestamp) ? -1 : 1;