Merge branch 'MDL-32249-21' of github.com:srynot4sale/moodle into MOODLE_21_STABLE
[moodle.git] / course / recent_form.php
blobb79e44372f8c2e3751d8f3f63cc3f65c6325b5a3
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 if (!defined('MOODLE_INTERNAL')) {
27 die('Direct access to this script is forbidden.'); /// It must be included from a Moodle page
30 require_once($CFG->libdir.'/formslib.php');
32 class recent_form extends moodleform {
33 function definition() {
34 global $CFG, $COURSE, $USER;
36 $mform =& $this->_form;
37 $context = get_context_instance(CONTEXT_COURSE, $COURSE->id);
38 $modinfo = get_fast_modinfo($COURSE);
39 $sections = get_all_sections($COURSE->id);
41 $mform->addElement('header', 'filters', get_string('managefilters')); //TODO: add better string
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));
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);
62 foreach($allgroups as $group) {
63 $groupoptions[$group->id] = format_string($group->name, true, array('context'=>$context));
67 if ($COURSE->id == SITEID) {
68 $viewparticipants = has_capability('moodle/site:viewparticipants', get_context_instance(CONTEXT_SYSTEM));
69 } else {
70 $viewparticipants = has_capability('moodle/course:viewparticipants', $context);
73 if ($viewparticipants) {
74 $viewfullnames = has_capability('moodle/site:viewfullnames', get_context_instance(CONTEXT_COURSE, $COURSE->id));
76 $options = array();
77 $options[0] = get_string('allparticipants');
78 $options[$CFG->siteguest] = get_string('guestuser');
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);
87 } else {
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);
100 $mform->addElement('select', 'user', get_string('participants'), $options);
101 $mform->setAdvanced('user');
104 $sectiontitle = get_string('sectionname', 'format_'.$COURSE->format);
106 $options = array(''=>get_string('allactivities'));
107 $modsused = array();
109 foreach($modinfo->cms as $cm) {
110 if (!$cm->uservisible) {
111 continue;
113 $modsused[$cm->modname] = true;
116 foreach ($modsused as $modname=>$unused) {
117 $libfile = "$CFG->dirroot/mod/$modname/lib.php";
118 if (!file_exists($libfile)) {
119 unset($modsused[$modname]);
120 continue;
122 include_once($libfile);
123 $libfunction = $modname."_get_recent_mod_activity";
124 if (!function_exists($libfunction)) {
125 unset($modsused[$modname]);
126 continue;
128 $options["mod/$modname"] = get_string('allmods', '', get_string('modulenameplural', $modname));
131 foreach ($modinfo->sections as $section=>$cmids) {
132 $options["section/$section"] = "-- ".get_section_name($COURSE, $sections[$section])." --";
133 foreach ($cmids as $cmid) {
134 $cm = $modinfo->cms[$cmid];
135 if (empty($modsused[$cm->modname]) or !$cm->uservisible) {
136 continue;
138 $options[$cm->id] = format_string($cm->name);
141 $mform->addElement('select', 'modid', get_string('activities'), $options);
142 $mform->setAdvanced('modid');
145 if ($groupoptions) {
146 $mform->addElement('select', 'group', get_string('groups'), $groupoptions);
147 $mform->setAdvanced('group');
148 } else {
149 // no access to groups in separate mode
150 $mform->addElement('hidden','group');
151 $mform->setType('group', PARAM_INT);
152 $mform->setConstants(array('group'=>-1));
155 $options = array('default' => get_string('bycourseorder'),
156 'dateasc' => get_string('datemostrecentlast'),
157 'datedesc' => get_string('datemostrecentfirst'));
158 $mform->addElement('select', 'sortby', get_string('sortby'), $options);
159 $mform->setAdvanced('sortby');
161 $mform->addElement('date_time_selector', 'date', get_string('since'), array('optional'=>true));
163 $mform->addElement('hidden','id');
164 $mform->setType('id', PARAM_INT);
165 $mform->setType('courseid', PARAM_INT);
167 $this->add_action_buttons(false, get_string('showrecent'));