MDL-67264 core_course: Begin set up for Activity chooser
[moodle.git] / course / recent_form.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 */
dd97c328 25
bfebaf64
MD
26if (!defined('MOODLE_INTERNAL')) {
27 die('Direct access to this script is forbidden.'); /// It must be included from a Moodle page
28}
29
93b47710 30require_once($CFG->dirroot . '/course/lib.php');
dd97c328 31require_once($CFG->libdir.'/formslib.php');
32
33class recent_form extends moodleform {
34 function definition() {
35 global $CFG, $COURSE, $USER;
36
37 $mform =& $this->_form;
9a5e297b 38 $context = context_course::instance($COURSE->id);
dd97c328 39 $modinfo = get_fast_modinfo($COURSE);
40
41 $mform->addElement('header', 'filters', get_string('managefilters')); //TODO: add better string
42
e7dd4ff5
PS
43 $groupoptions = array();
44 if (groups_get_course_groupmode($COURSE) == SEPARATEGROUPS and !has_capability('moodle/site:accessallgroups', $context)) {
45 // limited group access
46 $groups = groups_get_user_groups($COURSE->id);
47 $allgroups = groups_get_all_groups($COURSE->id);
48 if (!empty($groups[$COURSE->defaultgroupingid])) {
49 foreach ($groups[$COURSE->defaultgroupingid] AS $groupid) {
50 $groupoptions[$groupid] = format_string($allgroups[$groupid]->name, true, array('context'=>$context));
51 }
52 }
53 } else {
54 $groupoptions = array('0'=>get_string('allgroups'));
55 if (has_capability('moodle/site:accessallgroups', $context)) {
56 // user can see all groups
57 $allgroups = groups_get_all_groups($COURSE->id);
58 } else {
59 // user can see course level groups
60 $allgroups = groups_get_all_groups($COURSE->id, 0, $COURSE->defaultgroupingid);
61 }
62 foreach($allgroups as $group) {
63 $groupoptions[$group->id] = format_string($group->name, true, array('context'=>$context));
64 }
65 }
66
dd97c328 67 if ($COURSE->id == SITEID) {
93b47710 68 $viewparticipants = course_can_view_participants(context_system::instance());
dd97c328 69 } else {
93b47710 70 $viewparticipants = course_can_view_participants($context);
dd97c328 71 }
72
dd97c328 73 if ($viewparticipants) {
9a5e297b 74 $viewfullnames = has_capability('moodle/site:viewfullnames', context_course::instance($COURSE->id));
e7dd4ff5 75
dd97c328 76 $options = array();
77 $options[0] = get_string('allparticipants');
629e12fd 78 $options[$CFG->siteguest] = get_string('guestuser');
dd97c328 79
e7dd4ff5
PS
80 if (isset($groupoptions[0])) {
81 // can see all enrolled users
82 if ($enrolled = get_enrolled_users($context, null, 0, user_picture::fields('u'))) {
83 foreach ($enrolled as $euser) {
84 $options[$euser->id] = fullname($euser, $viewfullnames);
85 }
86 }
4692da47 87 } else {
e7dd4ff5
PS
88 // can see users from some groups only
89 foreach ($groupoptions as $groupid=>$unused) {
90 if ($enrolled = get_enrolled_users($context, null, $groupid, user_picture::fields('u'))) {
91 foreach ($enrolled as $euser) {
92 if (!array_key_exists($euser->id, $options)) {
93 $options[$euser->id] = fullname($euser, $viewfullnames);
94 }
95 }
96 }
dd97c328 97 }
98 }
e7dd4ff5 99
dd97c328 100 $mform->addElement('select', 'user', get_string('participants'), $options);
101 $mform->setAdvanced('user');
7b48aceb
DP
102 } else {
103 // Default to no user.
104 $mform->addElement('hidden', 'user', 0);
2b34a377
AA
105 $mform->setType('user', PARAM_INT);
106 $mform->setConstant('user', 0);
dd97c328 107 }
108
dd97c328 109 $options = array(''=>get_string('allactivities'));
110 $modsused = array();
111
112 foreach($modinfo->cms as $cm) {
113 if (!$cm->uservisible) {
114 continue;
115 }
116 $modsused[$cm->modname] = true;
117 }
118
119 foreach ($modsused as $modname=>$unused) {
120 $libfile = "$CFG->dirroot/mod/$modname/lib.php";
121 if (!file_exists($libfile)) {
122 unset($modsused[$modname]);
123 continue;
124 }
125 include_once($libfile);
126 $libfunction = $modname."_get_recent_mod_activity";
127 if (!function_exists($libfunction)) {
128 unset($modsused[$modname]);
129 continue;
130 }
131 $options["mod/$modname"] = get_string('allmods', '', get_string('modulenameplural', $modname));
132 }
133
134 foreach ($modinfo->sections as $section=>$cmids) {
71a56e08 135 $options["section/$section"] = "-- ".get_section_name($COURSE, $section)." --";
dd97c328 136 foreach ($cmids as $cmid) {
137 $cm = $modinfo->cms[$cmid];
138 if (empty($modsused[$cm->modname]) or !$cm->uservisible) {
139 continue;
140 }
141 $options[$cm->id] = format_string($cm->name);
142 }
143 }
144 $mform->addElement('select', 'modid', get_string('activities'), $options);
145 $mform->setAdvanced('modid');
146
147
e7dd4ff5
PS
148 if ($groupoptions) {
149 $mform->addElement('select', 'group', get_string('groups'), $groupoptions);
150 $mform->setAdvanced('group');
dd97c328 151 } else {
e7dd4ff5 152 // no access to groups in separate mode
dd97c328 153 $mform->addElement('hidden','group');
d18e0fe6 154 $mform->setType('group', PARAM_INT);
e7dd4ff5 155 $mform->setConstants(array('group'=>-1));
dd97c328 156 }
157
158 $options = array('default' => get_string('bycourseorder'),
159 'dateasc' => get_string('datemostrecentlast'),
160 'datedesc' => get_string('datemostrecentfirst'));
161 $mform->addElement('select', 'sortby', get_string('sortby'), $options);
162 $mform->setAdvanced('sortby');
163
164 $mform->addElement('date_time_selector', 'date', get_string('since'), array('optional'=>true));
165
166 $mform->addElement('hidden','id');
d18e0fe6 167 $mform->setType('id', PARAM_INT);
dd97c328 168 $mform->setType('courseid', PARAM_INT);
169
170 $this->add_action_buttons(false, get_string('showrecent'));
171 }
172}