3 // This file is part of Moodle - http://moodle.org/
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.
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/>.
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
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 = context_course
::instance($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();
54 $param->modid
= 'all';
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()) {
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);
86 $modinfo = get_fast_modinfo($course);
87 $modnames = get_module_types_names();
89 if (has_capability('moodle/course:viewhiddensections', $context)) {
92 $hiddenfilter = "AND cs.visible = 1";
95 foreach ($modinfo->get_section_info_all() as $i => $section) {
96 if (!empty($section->uservisible
)) {
97 $sections[$i] = $section;
101 if ($param->modid
=== 'all') {
104 } else if (strpos($param->modid
, 'mod/') === 0) {
105 $modname = substr($param->modid
, strlen('mod/'));
106 if (array_key_exists($modname, $modnames) and file_exists("$CFG->dirroot/mod/$modname/lib.php")) {
110 } else if (strpos($param->modid
, 'section/') === 0) {
111 $sectionid = substr($param->modid
, strlen('section/'));
112 if (isset($sections[$sectionid])) {
113 $sections = array($sectionid=>$sections[$sectionid]);
116 } else if (is_numeric($param->modid
)) {
117 $sectionnum = $modinfo->cms
[$param->modid
]->sectionnum
;
118 $filter_modid = $param->modid
;
119 $sections = array($sectionnum => $sections[$sectionnum]);
122 $activities = array();
125 foreach ($sections as $sectionnum => $section) {
127 $activity = new stdClass();
128 $activity->type
= 'section';
129 if ($section->section
> 0) {
130 $activity->name
= get_section_name($course, $section);
132 $activity->name
= '';
135 $activity->visible
= $section->visible
;
136 $activities[$index++
] = $activity;
138 if (empty($modinfo->sections
[$sectionnum])) {
142 foreach ($modinfo->sections
[$sectionnum] as $cmid) {
143 $cm = $modinfo->cms
[$cmid];
145 if (!$cm->uservisible
) {
149 if (!empty($filter) and $cm->modname
!= $filter) {
153 if (!empty($filter_modid) and $cmid != $filter_modid) {
157 $libfile = "$CFG->dirroot/mod/$cm->modname/lib.php";
159 if (file_exists($libfile)) {
160 require_once($libfile);
161 $get_recent_mod_activity = $cm->modname
."_get_recent_mod_activity";
163 if (function_exists($get_recent_mod_activity)) {
164 $activity = new stdClass();
165 $activity->type
= 'activity';
166 $activity->cmid
= $cmid;
167 $activities[$index++
] = $activity;
168 $get_recent_mod_activity($activities, $index, $param->date
, $course->id
, $cmid, $param->user
, $param->group
);
176 switch ($param->sortby
) {
177 case 'datedesc' : usort($activities, 'compare_activities_by_time_desc'); break;
178 case 'dateasc' : usort($activities, 'compare_activities_by_time_asc'); break;
180 default : $detail = false; $param->sortby
= 'default';
184 if (!empty($activities)) {
194 $activity_count = count($activities);
195 $viewfullnames = array();
197 foreach ($activities as $key => $activity) {
199 if ($activity->type
== 'section') {
200 if ($param->sortby
!= 'default') {
201 continue; // no section if ordering by date
203 if ($activity_count == ($key +
1) or $activities[$key+
1]->type
== 'section') {
204 // peak at next activity. If it's another section, don't print this one!
205 // this means there are no activities in the current section
210 if (($activity->type
== 'section') && ($param->sortby
== 'default')) {
212 echo $OUTPUT->box_end();
213 echo $OUTPUT->spacer(array('height'=>30, 'br'=>true)); // should be done with CSS instead
215 echo $OUTPUT->box_start();
216 if (!empty($activity->name
)) {
217 echo html_writer
::tag('h2', $activity->name
);
221 } else if ($activity->type
== 'activity') {
223 if ($param->sortby
== 'default') {
224 $cm = $modinfo->cms
[$activity->cmid
];
231 $name = format_string($cm->name
);
232 $modfullname = $modnames[$cm->modname
];
234 $image = $OUTPUT->pix_icon('icon', $modfullname, $cm->modname
, array('class' => 'icon smallicon'));
235 $link = html_writer
::link(new moodle_url("/mod/$cm->modname/view.php",
236 array("id" => $cm->id
)), $name, array('class' => $class));
237 echo html_writer
::tag('h3', "$image $modfullname $link");
242 if (!isset($viewfullnames[$activity->cmid
])) {
243 $cm_context = context_module
::instance($activity->cmid
);
244 $viewfullnames[$activity->cmid
] = has_capability('moodle/site:viewfullnames', $cm_context);
248 echo $OUTPUT->box_start();
252 $print_recent_mod_activity = $activity->type
.'_print_recent_mod_activity';
254 if (function_exists($print_recent_mod_activity)) {
255 $print_recent_mod_activity($activity, $course->id
, $detail, $modnames, $viewfullnames[$activity->cmid
]);
261 echo $OUTPUT->box_end();
267 echo html_writer
::tag('h3', get_string('norecentactivity'), array('class' => 'mdl-align'));
271 echo $OUTPUT->footer();