Implement data privacy provider.
[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 defined('MOODLE_INTERNAL') || die();
26 require_once($CFG->libdir.'/formslib.php');
27
28 /**
29 * class for displaying export form.
30 *
31 * @copyright 2011 Artem Andreev <andreev.artem@gmail.com>
32 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
33 */
34 class mod_attendance_export_form extends moodleform {
35
36 /**
37 * Called to define this moodle form
38 *
39 * @return void
40 */
41 public function definition() {
42 global $USER, $DB, $PAGE;
43 $mform =& $this->_form;
44 $course = $this->_customdata['course'];
45 $cm = $this->_customdata['cm'];
46 $modcontext = $this->_customdata['modcontext'];
47
48 $mform->addElement('header', 'general', get_string('export', 'attendance'));
49
50 $groupmode = groups_get_activity_groupmode($cm, $course);
51 $groups = groups_get_activity_allowed_groups($cm, $USER->id);
52 if ($groupmode == VISIBLEGROUPS or has_capability('moodle/site:accessallgroups', $modcontext)) {
53 $grouplist[0] = get_string('allparticipants');
54 }
55 if ($groups) {
56 foreach ($groups as $group) {
57 $grouplist[$group->id] = $group->name;
58 }
59 }
60
61 // Restrict the export to the selected users.
62 $namefields = get_all_user_name_fields(true, 'u');
63 $allusers = get_enrolled_users($modcontext, 'mod/attendance:canbelisted', 0, 'u.id,'.$namefields);
64 $userlist = array();
65 foreach ($allusers as $user) {
66 $userlist[$user->id] = fullname($user);
67 }
68 unset($allusers);
69 $tempusers = $DB->get_records('attendance_tempusers', array('courseid' => $course->id), 'studentid, fullname');
70 foreach ($tempusers as $user) {
71 $userlist[$user->studentid] = $user->fullname;
72 }
73 if (empty($userlist)) {
74 $mform->addElement('static', 'nousers', '', get_string('noattendanceusers', 'attendance'));
75 return;
76 }
77
78 list($gsql, $gparams) = $DB->get_in_or_equal(array_keys($grouplist), SQL_PARAMS_NAMED);
79 list($usql, $uparams) = $DB->get_in_or_equal(array_keys($userlist), SQL_PARAMS_NAMED);
80 $params = array_merge($gparams, $uparams);
81 $groupmembers = $DB->get_recordset_select('groups_members', "groupid {$gsql} AND userid {$usql}", $params,
82 '', 'groupid, userid');
83 $groupmappings = array();
84 foreach ($groupmembers as $groupmember) {
85 if (!isset($groupmappings[$groupmember->groupid])) {
86 $groupmappings[$groupmember->groupid] = array();
87 }
88 $groupmappings[$groupmember->groupid][$groupmember->userid] = $userlist[$groupmember->userid];
89 }
90 if (isset($grouplist[0])) {
91 $groupmappings[0] = $userlist;
92 }
93
94 $mform->addElement('select', 'group', get_string('group'), $grouplist);
95
96 $mform->addElement('selectyesno', 'selectedusers', get_string('onlyselectedusers', 'mod_attendance'));
97 $sel = $mform->addElement('select', 'users', get_string('users', 'mod_attendance'), $userlist, array('size' => 12));
98 $sel->setMultiple(true);
99 $mform->disabledIf('users', 'selectedusers', 'eq', 0);
100
101 $opts = array('groupmappings' => $groupmappings);
102 $PAGE->requires->yui_module('moodle-mod_attendance-groupfilter', 'M.mod_attendance.groupfilter.init', array($opts));
103
104 $ident = array();
105 $ident[] =& $mform->createElement('checkbox', 'id', '', get_string('studentid', 'attendance'));
106 $ident[] =& $mform->createElement('checkbox', 'uname', '', get_string('username'));
107
108 $optional = array('idnumber', 'institution', 'department');
109 foreach ($optional as $opt) {
110 $ident[] =& $mform->createElement('checkbox', $opt, '', get_string($opt));
111 $mform->setType($opt, PARAM_NOTAGS);
112 }
113
114 $mform->addGroup($ident, 'ident', get_string('identifyby', 'attendance'), array('<br />'), true);
115 $mform->setDefaults(array('ident[id]' => true, 'ident[uname]' => true));
116 $mform->setType('id', PARAM_INT);
117 $mform->setType('uname', PARAM_INT);
118
119 $mform->addElement('checkbox', 'includeallsessions', get_string('includeall', 'attendance'), get_string('yes'));
120 $mform->setDefault('includeallsessions', true);
121 $mform->addElement('checkbox', 'includenottaken', get_string('includenottaken', 'attendance'), get_string('yes'));
122 $mform->addElement('checkbox', 'includeremarks', get_string('includeremarks', 'attendance'), get_string('yes'));
123 $mform->addElement('date_selector', 'sessionstartdate', get_string('startofperiod', 'attendance'));
124 $mform->setDefault('sessionstartdate', $course->startdate);
125 $mform->disabledIf('sessionstartdate', 'includeallsessions', 'checked');
126 $mform->addElement('date_selector', 'sessionenddate', get_string('endofperiod', 'attendance'));
127 $mform->disabledIf('sessionenddate', 'includeallsessions', 'checked');
128
129 $formatoptions = array('excel' => get_string('downloadexcel', 'attendance'),
130 'ooo' => get_string('downloadooo', 'attendance'),
131 'text' => get_string('downloadtext', 'attendance'));
132 $mform->addElement('select', 'format', get_string('format'), $formatoptions);
133
134 $submitstring = get_string('ok');
135 $this->add_action_buttons(false, $submitstring);
136
137 $mform->addElement('hidden', 'id', $cm->id);
138 }
139
140 /**
141 * Validate form.
142 * @param array $data
143 * @param array $files
144 * @return array
145 */
146 public function validation($data, $files) {
147 $errors = parent::validation($data, $files);
148
149 // Validate the 'users' field.
150 if ($data['selectedusers'] && empty($data['users'])) {
151 $errors['users'] = get_string('mustselectusers', 'mod_attendance');
152 }
153
154 return $errors;
155 }
156 }
157