Fixes #223 allow student attendance to be disabled at site-level
[moodle-mod_attendance.git] / export_form.php
1 <?php
2 // This file is part of Moodle - http://moodle.org/
3 //
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
16
17 /**
18 * Export attendance sessions forms
19 *
20 * @package mod_attendance
21 * @copyright 2011 Artem Andreev <andreev.artem@gmail.com>
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23 */
24
25 require_once($CFG->libdir.'/formslib.php');
26
27 /**
28 * class for displaying export form.
29 *
30 * @copyright 2011 Artem Andreev <andreev.artem@gmail.com>
31 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
32 */
33 class mod_attendance_export_form extends moodleform {
34
35 /**
36 * Called to define this moodle form
37 *
38 * @return void
39 */
40 public function definition() {
41 global $USER, $DB, $PAGE;
42 $mform =& $this->_form;
43 $course = $this->_customdata['course'];
44 $cm = $this->_customdata['cm'];
45 $modcontext = $this->_customdata['modcontext'];
46
47 $mform->addElement('header', 'general', get_string('export', 'attendance'));
48
49 $groupmode = groups_get_activity_groupmode($cm, $course);
50 $groups = groups_get_activity_allowed_groups($cm, $USER->id);
51 if ($groupmode == VISIBLEGROUPS or has_capability('moodle/site:accessallgroups', $modcontext)) {
52 $grouplist[0] = get_string('allparticipants');
53 }
54 if ($groups) {
55 foreach ($groups as $group) {
56 $grouplist[$group->id] = $group->name;
57 }
58 }
59
60 // Restrict the export to the selected users.
61 $namefields = get_all_user_name_fields(true, 'u');
62 $allusers = get_enrolled_users($modcontext, 'mod/attendance:canbelisted', 0, 'u.id,'.$namefields);
63 $userlist = array();
64 foreach ($allusers as $user) {
65 $userlist[$user->id] = fullname($user);
66 }
67 unset($allusers);
68 $tempusers = $DB->get_records('attendance_tempusers', array('courseid' => $course->id), 'studentid, fullname');
69 foreach ($tempusers as $user) {
70 $userlist[$user->studentid] = $user->fullname;
71 }
72 if (empty($userlist)) {
73 $mform->addElement('static', 'nousers', '', get_string('noattendanceusers', 'attendance'));
74 return;
75 }
76
77 list($gsql, $gparams) = $DB->get_in_or_equal(array_keys($grouplist), SQL_PARAMS_NAMED);
78 list($usql, $uparams) = $DB->get_in_or_equal(array_keys($userlist), SQL_PARAMS_NAMED);
79 $params = array_merge($gparams, $uparams);
80 $groupmembers = $DB->get_recordset_select('groups_members', "groupid {$gsql} AND userid {$usql}", $params,
81 '', 'groupid, userid');
82 $groupmappings = array();
83 foreach ($groupmembers as $groupmember) {
84 if (!isset($groupmappings[$groupmember->groupid])) {
85 $groupmappings[$groupmember->groupid] = array();
86 }
87 $groupmappings[$groupmember->groupid][$groupmember->userid] = $userlist[$groupmember->userid];
88 }
89 if (isset($grouplist[0])) {
90 $groupmappings[0] = $userlist;
91 }
92
93 $mform->addElement('select', 'group', get_string('group'), $grouplist);
94
95 $mform->addElement('selectyesno', 'selectedusers', get_string('onlyselectedusers', 'mod_attendance'));
96 $sel = $mform->addElement('select', 'users', get_string('users', 'mod_attendance'), $userlist, array('size' => 12));
97 $sel->setMultiple(true);
98 $mform->disabledIf('users', 'selectedusers', 'eq', 0);
99
100 $opts = array('groupmappings' => $groupmappings);
101 $PAGE->requires->yui_module('moodle-mod_attendance-groupfilter', 'M.mod_attendance.groupfilter.init', array($opts));
102
103 $ident = array();
104 $ident[] =& $mform->createElement('checkbox', 'id', '', get_string('studentid', 'attendance'));
105 $ident[] =& $mform->createElement('checkbox', 'uname', '', get_string('username'));
106
107 $optional = array('idnumber', 'institution', 'department');
108 foreach ($optional as $opt) {
109 $ident[] =& $mform->createElement('checkbox', $opt, '', get_string($opt));
110 $mform->setType($opt, PARAM_NOTAGS);
111 }
112
113 $mform->addGroup($ident, 'ident', get_string('identifyby', 'attendance'), array('<br />'), true);
114 $mform->setDefaults(array('ident[id]' => true, 'ident[uname]' => true));
115 $mform->setType('id', PARAM_INT);
116 $mform->setType('uname', PARAM_INT);
117
118 $mform->addElement('checkbox', 'includeallsessions', get_string('includeall', 'attendance'), get_string('yes'));
119 $mform->setDefault('includeallsessions', true);
120 $mform->addElement('checkbox', 'includenottaken', get_string('includenottaken', 'attendance'), get_string('yes'));
121 $mform->addElement('checkbox', 'includeremarks', get_string('includeremarks', 'attendance'), get_string('yes'));
122 $mform->addElement('date_selector', 'sessionstartdate', get_string('startofperiod', 'attendance'));
123 $mform->setDefault('sessionstartdate', $course->startdate);
124 $mform->disabledIf('sessionstartdate', 'includeallsessions', 'checked');
125 $mform->addElement('date_selector', 'sessionenddate', get_string('endofperiod', 'attendance'));
126 $mform->disabledIf('sessionenddate', 'includeallsessions', 'checked');
127
128 $formatoptions = array('excel' => get_string('downloadexcel', 'attendance'),
129 'ooo' => get_string('downloadooo', 'attendance'),
130 'text' => get_string('downloadtext', 'attendance'));
131 $mform->addElement('select', 'format', get_string('format'), $formatoptions);
132
133 $submitstring = get_string('ok');
134 $this->add_action_buttons(false, $submitstring);
135
136 $mform->addElement('hidden', 'id', $cm->id);
137 }
138
139 public function validation($data, $files) {
140 $errors = parent::validation($data, $files);
141
142 // Validate the 'users' field.
143 if ($data['selectedusers'] && empty($data['users'])) {
144 $errors['users'] = get_string('mustselectusers', 'mod_attendance');
145 }
146
147 return $errors;
148 }
149 }
150