MDL-67264 core_course: Activity chooser new feature
[moodle.git] / course / recent.php
CommitLineData
d9cb06dc 1<?php
2
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.
14//
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/>.
17
18/**
19 * Display all recent activity in a flexible way
20 *
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
24 */
25
26require_once('../config.php');
27require_once('lib.php');
28require_once('recent_form.php');
29
30$id = required_param('id', PARAM_INT);
31
a6855934 32$PAGE->set_url('/course/recent.php', array('id'=>$id));
909b8671 33$PAGE->set_pagelayout('report');
d9cb06dc 34
35if (!$course = $DB->get_record('course', array('id'=>$id))) {
36 print_error("That's an invalid course id");
37}
46054fa8 38
d9cb06dc 39require_login($course);
9a5e297b 40$context = context_course::instance($course->id);
dd97c328 41
dce7ec41
PS
42\core\event\recent_activity_viewed::create(array('context' => $context))->trigger();
43
d9cb06dc 44$lastlogin = time() - COURSE_MAX_RECENT_PERIOD;
45if (!isguestuser() and !empty($USER->lastcourseaccess[$COURSE->id])) {
46 if ($USER->lastcourseaccess[$COURSE->id] > $lastlogin) {
47 $lastlogin = $USER->lastcourseaccess[$COURSE->id];
46054fa8 48 }
d9cb06dc 49}
46054fa8 50
fbaea88f 51$param = new stdClass();
d9cb06dc 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;
58
59$mform = new recent_form();
60$mform->set_data($param);
61if ($formdata = $mform->get_data()) {
62 $param = $formdata;
63}
46054fa8 64
d9cb06dc 65$userinfo = get_string('allparticipants');
66$dateinfo = get_string('alldays');
46054fa8 67
d9cb06dc 68if (!empty($param->user)) {
69 if (!$u = $DB->get_record('user', array('id'=>$param->user))) {
70 print_error("That's an invalid user!");
dd97c328 71 }
d9cb06dc 72 $userinfo = fullname($u);
73}
e7d9c966 74
d9cb06dc 75$strrecentactivity = get_string('recentactivity');
a6855934 76$PAGE->navbar->add($strrecentactivity, new moodle_url('/course/recent.php', array('id'=>$course->id)));
d9cb06dc 77$PAGE->navbar->add($userinfo);
78$PAGE->set_title("$course->shortname: $strrecentactivity");
79$PAGE->set_heading($course->fullname);
80echo $OUTPUT->header();
551028df 81echo $OUTPUT->heading(format_string($course->fullname) . ": $userinfo", 2);
6f80940b 82
d9cb06dc 83$mform->display();
e7d9c966 84
f20edd52 85$modinfo = get_fast_modinfo($course);
d57aa283 86$modnames = get_module_types_names();
e7d9c966 87
d9cb06dc 88if (has_capability('moodle/course:viewhiddensections', $context)) {
89 $hiddenfilter = "";
90} else {
91 $hiddenfilter = "AND cs.visible = 1";
92}
93$sections = array();
71a56e08
MG
94foreach ($modinfo->get_section_info_all() as $i => $section) {
95 if (!empty($section->uservisible)) {
96 $sections[$i] = $section;
d9cb06dc 97 }
98}
46054fa8 99
d9cb06dc 100if ($param->modid === 'all') {
101 // ok
46054fa8 102
d9cb06dc 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;
4581271a 107 }
d9cb06dc 108
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]);
08e2111b 113 }
114
d9cb06dc 115} else if (is_numeric($param->modid)) {
71a56e08
MG
116 $sectionnum = $modinfo->cms[$param->modid]->sectionnum;
117 $filter_modid = $param->modid;
118 $sections = array($sectionnum => $sections[$sectionnum]);
d9cb06dc 119}
46054fa8 120
d9cb06dc 121$activities = array();
122$index = 0;
4581271a 123
71a56e08 124foreach ($sections as $sectionnum => $section) {
dd97c328 125
fbaea88f 126 $activity = new stdClass();
d9cb06dc 127 $activity->type = 'section';
128 if ($section->section > 0) {
7487c856 129 $activity->name = get_section_name($course, $section);
d9cb06dc 130 } else {
131 $activity->name = '';
dd97c328 132 }
08e2111b 133
d9cb06dc 134 $activity->visible = $section->visible;
135 $activities[$index++] = $activity;
4581271a 136
71a56e08 137 if (empty($modinfo->sections[$sectionnum])) {
d9cb06dc 138 continue;
139 }
b934e7a3 140
44aa854e 141 foreach ($modinfo->sections[$sectionnum] as $cmid) {
d9cb06dc 142 $cm = $modinfo->cms[$cmid];
dd97c328 143
d9cb06dc 144 if (!$cm->uservisible) {
145 continue;
146 }
4581271a 147
d9cb06dc 148 if (!empty($filter) and $cm->modname != $filter) {
149 continue;
150 }
08e2111b 151
71a56e08
MG
152 if (!empty($filter_modid) and $cmid != $filter_modid) {
153 continue;
154 }
155
d9cb06dc 156 $libfile = "$CFG->dirroot/mod/$cm->modname/lib.php";
5a810603 157
d9cb06dc 158 if (file_exists($libfile)) {
159 require_once($libfile);
160 $get_recent_mod_activity = $cm->modname."_get_recent_mod_activity";
4581271a 161
d9cb06dc 162 if (function_exists($get_recent_mod_activity)) {
fbaea88f 163 $activity = new stdClass();
d9cb06dc 164 $activity->type = 'activity';
165 $activity->cmid = $cmid;
166 $activities[$index++] = $activity;
167 $get_recent_mod_activity($activities, $index, $param->date, $course->id, $cmid, $param->user, $param->group);
4581271a 168 }
169 }
170 }
d9cb06dc 171}
4581271a 172
d9cb06dc 173$detail = true;
4581271a 174
d9cb06dc 175switch ($param->sortby) {
176 case 'datedesc' : usort($activities, 'compare_activities_by_time_desc'); break;
177 case 'dateasc' : usort($activities, 'compare_activities_by_time_asc'); break;
178 case 'default' :
179 default : $detail = false; $param->sortby = 'default';
a8e44ce4 180
d9cb06dc 181}
4581271a 182
d9cb06dc 183if (!empty($activities)) {
4581271a 184
d9cb06dc 185 $newsection = true;
186 $lastsection = '';
187 $newinstance = true;
188 $lastinstance = '';
189 $inbox = false;
4581271a 190
d9cb06dc 191 $section = 0;
b934e7a3 192
d9cb06dc 193 $activity_count = count($activities);
194 $viewfullnames = array();
b934e7a3 195
d9cb06dc 196 foreach ($activities as $key => $activity) {
b934e7a3 197
d9cb06dc 198 if ($activity->type == 'section') {
199 if ($param->sortby != 'default') {
200 continue; // no section if ordering by date
201 }
202 if ($activity_count == ($key + 1) or $activities[$key+1]->type == 'section') {
203 // peak at next activity. If it's another section, don't print this one!
204 // this means there are no activities in the current section
205 continue;
b934e7a3 206 }
d9cb06dc 207 }
0f87cb1d 208
d9cb06dc 209 if (($activity->type == 'section') && ($param->sortby == 'default')) {
210 if ($inbox) {
211 echo $OUTPUT->box_end();
1ba862ec 212 echo $OUTPUT->spacer(array('height'=>30, 'br'=>true)); // should be done with CSS instead
d9cb06dc 213 }
214 echo $OUTPUT->box_start();
278b8d18 215 if (strval($activity->name) !== '') {
7eea889b
FM
216 echo html_writer::tag('h2', $activity->name);
217 }
d9cb06dc 218 $inbox = true;
0f87cb1d 219
d9cb06dc 220 } else if ($activity->type == 'activity') {
dd97c328 221
d9cb06dc 222 if ($param->sortby == 'default') {
223 $cm = $modinfo->cms[$activity->cmid];
dd97c328 224
d9cb06dc 225 if ($cm->visible) {
7eea889b 226 $class = '';
d9cb06dc 227 } else {
7eea889b 228 $class = 'dimmed';
d9cb06dc 229 }
230 $name = format_string($cm->name);
231 $modfullname = $modnames[$cm->modname];
dd97c328 232
7eea889b
FM
233 $image = $OUTPUT->pix_icon('icon', $modfullname, $cm->modname, array('class' => 'icon smallicon'));
234 $link = html_writer::link(new moodle_url("/mod/$cm->modname/view.php",
235 array("id" => $cm->id)), $name, array('class' => $class));
236 echo html_writer::tag('h3', "$image $modfullname $link");
d9cb06dc 237 }
08e2111b 238
d9cb06dc 239 } else {
a8e44ce4 240
d9cb06dc 241 if (!isset($viewfullnames[$activity->cmid])) {
9a5e297b 242 $cm_context = context_module::instance($activity->cmid);
d9cb06dc 243 $viewfullnames[$activity->cmid] = has_capability('moodle/site:viewfullnames', $cm_context);
244 }
76cbde41 245
d9cb06dc 246 if (!$inbox) {
247 echo $OUTPUT->box_start();
248 $inbox = true;
249 }
a8e44ce4 250
d9cb06dc 251 $print_recent_mod_activity = $activity->type.'_print_recent_mod_activity';
a8e44ce4 252
d9cb06dc 253 if (function_exists($print_recent_mod_activity)) {
254 $print_recent_mod_activity($activity, $course->id, $detail, $modnames, $viewfullnames[$activity->cmid]);
46054fa8 255 }
256 }
d9cb06dc 257 }
08e2111b 258
d9cb06dc 259 if ($inbox) {
260 echo $OUTPUT->box_end();
261 }
0f87cb1d 262
08e2111b 263
d9cb06dc 264} else {
08e2111b 265
7eea889b 266 echo html_writer::tag('h3', get_string('norecentactivity'), array('class' => 'mdl-align'));
46054fa8 267
d9cb06dc 268}
4581271a 269
d9cb06dc 270echo $OUTPUT->footer();
46054fa8 271