MDL-22388 Added some checks to kill these scripts dead with an unequivocal notice...
[moodle.git] / course / recent_form.php
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.
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/>.
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  */
26 if (!defined('MOODLE_INTERNAL')) {
27     die('Direct access to this script is forbidden.');    ///  It must be included from a Moodle page
28 }
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);
40         $mform->addElement('header', 'filters', get_string('managefilters')); //TODO: add better string
42         if ($COURSE->id == SITEID) {
43             $viewparticipants = has_capability('moodle/site:viewparticipants', get_context_instance(CONTEXT_SYSTEM));
44         } else {
45             $viewparticipants = has_capability('moodle/course:viewparticipants', $context);
46         }
48         $viewfullnames = has_capability('moodle/site:viewfullnames', get_context_instance(CONTEXT_COURSE, $COURSE->id));
50         if ($viewparticipants) {
51             $options = array();
52             $options[0] = get_string('allparticipants');
53             if ($guest = get_complete_user_data('username', 'guest')) {
54                 $options[$guest->id] = fullname($guest);
55             }
57             if (groups_get_course_groupmode($COURSE) == SEPARATEGROUPS) {
58                 $groups = groups_get_user_groups($COURSE->id);
59                 $group = $groups[0];
60             } else {
61                 $group = '';
62             }
64             if ($enrolled = get_enrolled_users($context, null, $group, user_picture::fields('u'))) {
65                 foreach ($enrolled as $euser) {
66                     $options[$euser->id] = fullname($euser, $viewfullnames);
67                 }
68             }
69             $mform->addElement('select', 'user', get_string('participants'), $options);
70             $mform->setAdvanced('user');
71         }
73         switch ($COURSE->format) {
74             case 'weeks':  $sectiontitle = get_string('week'); break;
75             case 'topics': $sectiontitle = get_string('topic'); break;
76             default: $sectiontitle = get_string('section'); break;
77         }
79         $options = array(''=>get_string('allactivities'));
80         $modsused = array();
82         foreach($modinfo->cms as $cm) {
83             if (!$cm->uservisible) {
84                 continue;
85             }
86             $modsused[$cm->modname] = true;
87         }
89         foreach ($modsused as $modname=>$unused) {
90             $libfile = "$CFG->dirroot/mod/$modname/lib.php";
91             if (!file_exists($libfile)) {
92                 unset($modsused[$modname]);
93                 continue;
94             }
95             include_once($libfile);
96             $libfunction = $modname."_get_recent_mod_activity";
97             if (!function_exists($libfunction)) {
98                 unset($modsused[$modname]);
99                 continue;
100             }
101             $options["mod/$modname"] = get_string('allmods', '', get_string('modulenameplural', $modname));
102         }
104         foreach ($modinfo->sections as $section=>$cmids) {
105             $options["section/$section"] = "-- $sectiontitle $section --";
106             foreach ($cmids as $cmid) {
107                 $cm = $modinfo->cms[$cmid];
108                 if (empty($modsused[$cm->modname]) or !$cm->uservisible) {
109                     continue;
110                 }
111                 $options[$cm->id] = format_string($cm->name);
112             }
113         }
114         $mform->addElement('select', 'modid', get_string('activities'), $options);
115         $mform->setAdvanced('modid');
118         if (has_capability('moodle/site:accessallgroups', $context)) {
119             if ($groups = groups_get_all_groups($COURSE->id)) {
120                 $options = array('0'=>get_string('allgroups'));
121                 foreach($groups as $group) {
122                     $options[$group->id] = format_string($group->name);
123                 }
124                 $mform->addElement('select', 'group', get_string('groups'), $options);
125                 $mform->setAdvanced('group');
126             }
127         } else {
128             $mform->addElement('hidden','group');
129             $mform->setType('group', PARAM_INT);
130             $mform->setConstants(array('group'=>0));
131         }
133         $options = array('default'  => get_string('bycourseorder'),
134                          'dateasc'  => get_string('datemostrecentlast'),
135                          'datedesc' => get_string('datemostrecentfirst'));
136         $mform->addElement('select', 'sortby', get_string('sortby'), $options);
137         $mform->setAdvanced('sortby');
139         $mform->addElement('date_time_selector', 'date', get_string('since'), array('optional'=>true));
141         $mform->addElement('hidden','id');
142         $mform->setType('id', PARAM_INT);
143         $mform->setType('courseid', PARAM_INT);
145         $this->add_action_buttons(false, get_string('showrecent'));
146     }