MDL-36983 fix incorrect test method name
[moodle.git] / course / recent.php
blobdf0f0d515d05f1bd946663a9d44b3b9b4a458b04
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 = context_course::instance($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", 2);
83 $mform->display();
85 $modinfo = get_fast_modinfo($course);
86 $modnames = get_module_types_names();
88 if (has_capability('moodle/course:viewhiddensections', $context)) {
89 $hiddenfilter = "";
90 } else {
91 $hiddenfilter = "AND cs.visible = 1";
93 $sections = array();
94 foreach ($modinfo->get_section_info_all() as $i => $section) {
95 if (!empty($section->uservisible)) {
96 $sections[$i] = $section;
100 if ($param->modid === 'all') {
101 // ok
103 } else if (strpos($param->modid, 'mod/') === 0) {
104 $modname = substr($param->modid, strlen('mod/'));
105 if (array_key_exists($modname, $modnames) and file_exists("$CFG->dirroot/mod/$modname/lib.php")) {
106 $filter = $modname;
109 } else if (strpos($param->modid, 'section/') === 0) {
110 $sectionid = substr($param->modid, strlen('section/'));
111 if (isset($sections[$sectionid])) {
112 $sections = array($sectionid=>$sections[$sectionid]);
115 } else if (is_numeric($param->modid)) {
116 $sectionnum = $modinfo->cms[$param->modid]->sectionnum;
117 $filter_modid = $param->modid;
118 $sections = array($sectionnum => $sections[$sectionnum]);
122 $modinfo->get_groups(); // load all my groups and cache it in modinfo
124 $activities = array();
125 $index = 0;
127 foreach ($sections as $sectionnum => $section) {
129 $activity = new stdClass();
130 $activity->type = 'section';
131 if ($section->section > 0) {
132 $activity->name = get_section_name($course, $section);
133 } else {
134 $activity->name = '';
137 $activity->visible = $section->visible;
138 $activities[$index++] = $activity;
140 if (empty($modinfo->sections[$sectionnum])) {
141 continue;
144 foreach ($modinfo->sections[$sectionnum] as $cmid) {
145 $cm = $modinfo->cms[$cmid];
147 if (!$cm->uservisible) {
148 continue;
151 if (!empty($filter) and $cm->modname != $filter) {
152 continue;
155 if (!empty($filter_modid) and $cmid != $filter_modid) {
156 continue;
159 $libfile = "$CFG->dirroot/mod/$cm->modname/lib.php";
161 if (file_exists($libfile)) {
162 require_once($libfile);
163 $get_recent_mod_activity = $cm->modname."_get_recent_mod_activity";
165 if (function_exists($get_recent_mod_activity)) {
166 $activity = new stdClass();
167 $activity->type = 'activity';
168 $activity->cmid = $cmid;
169 $activities[$index++] = $activity;
170 $get_recent_mod_activity($activities, $index, $param->date, $course->id, $cmid, $param->user, $param->group);
176 $detail = true;
178 switch ($param->sortby) {
179 case 'datedesc' : usort($activities, 'compare_activities_by_time_desc'); break;
180 case 'dateasc' : usort($activities, 'compare_activities_by_time_asc'); break;
181 case 'default' :
182 default : $detail = false; $param->sortby = 'default';
186 if (!empty($activities)) {
188 $newsection = true;
189 $lastsection = '';
190 $newinstance = true;
191 $lastinstance = '';
192 $inbox = false;
194 $section = 0;
196 $activity_count = count($activities);
197 $viewfullnames = array();
199 foreach ($activities as $key => $activity) {
201 if ($activity->type == 'section') {
202 if ($param->sortby != 'default') {
203 continue; // no section if ordering by date
205 if ($activity_count == ($key + 1) or $activities[$key+1]->type == 'section') {
206 // peak at next activity. If it's another section, don't print this one!
207 // this means there are no activities in the current section
208 continue;
212 if (($activity->type == 'section') && ($param->sortby == 'default')) {
213 if ($inbox) {
214 echo $OUTPUT->box_end();
215 echo $OUTPUT->spacer(array('height'=>30, 'br'=>true)); // should be done with CSS instead
217 echo $OUTPUT->box_start();
218 if (!empty($activity->name)) {
219 echo html_writer::tag('h2', $activity->name);
221 $inbox = true;
223 } else if ($activity->type == 'activity') {
225 if ($param->sortby == 'default') {
226 $cm = $modinfo->cms[$activity->cmid];
228 if ($cm->visible) {
229 $class = '';
230 } else {
231 $class = 'dimmed';
233 $name = format_string($cm->name);
234 $modfullname = $modnames[$cm->modname];
236 $image = $OUTPUT->pix_icon('icon', $modfullname, $cm->modname, array('class' => 'icon smallicon'));
237 $link = html_writer::link(new moodle_url("/mod/$cm->modname/view.php",
238 array("id" => $cm->id)), $name, array('class' => $class));
239 echo html_writer::tag('h3', "$image $modfullname $link");
242 } else {
244 if (!isset($viewfullnames[$activity->cmid])) {
245 $cm_context = context_module::instance($activity->cmid);
246 $viewfullnames[$activity->cmid] = has_capability('moodle/site:viewfullnames', $cm_context);
249 if (!$inbox) {
250 echo $OUTPUT->box_start();
251 $inbox = true;
254 $print_recent_mod_activity = $activity->type.'_print_recent_mod_activity';
256 if (function_exists($print_recent_mod_activity)) {
257 $print_recent_mod_activity($activity, $course->id, $detail, $modnames, $viewfullnames[$activity->cmid]);
262 if ($inbox) {
263 echo $OUTPUT->box_end();
267 } else {
269 echo html_writer::tag('h3', get_string('norecentactivity'), array('class' => 'mdl-align'));
273 echo $OUTPUT->footer();
275 function compare_activities_by_time_desc($a, $b) {
276 // make sure the activities actually have a timestamp property
277 if ((!array_key_exists('timestamp', $a)) or (!array_key_exists('timestamp', $b))) {
278 return 0;
280 if ($a->timestamp == $b->timestamp)
281 return 0;
282 return ($a->timestamp > $b->timestamp) ? -1 : 1;
285 function compare_activities_by_time_asc($a, $b) {
286 // make sure the activities actually have a timestamp property
287 if ((!array_key_exists('timestamp', $a)) or (!array_key_exists('timestamp', $b))) {
288 return 0;
290 if ($a->timestamp == $b->timestamp)
291 return 0;
292 return ($a->timestamp < $b->timestamp) ? -1 : 1;