2 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
18 * Export attendance sessions forms
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
25 require_once($CFG->libdir
.'/formslib.php');
28 * class for displaying export form.
30 * @copyright 2011 Artem Andreev <andreev.artem@gmail.com>
31 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
33 class mod_attendance_export_form
extends moodleform
{
36 * Called to define this moodle form
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'];
47 $mform->addElement('header', 'general', get_string('export', 'attendance'));
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');
55 foreach ($groups as $group) {
56 $grouplist[$group->id
] = $group->name
;
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);
64 foreach ($allusers as $user) {
65 $userlist[$user->id
] = fullname($user);
68 $tempusers = $DB->get_records('attendance_tempusers', array('courseid' => $course->id
), 'studentid, fullname');
69 foreach ($tempusers as $user) {
70 $userlist[$user->studentid
] = $user->fullname
;
72 if (empty($userlist)) {
73 $mform->addElement('static', 'nousers', '', get_string('noattendanceusers', 'attendance'));
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();
87 $groupmappings[$groupmember->groupid
][$groupmember->userid
] = $userlist[$groupmember->userid
];
89 if (isset($grouplist[0])) {
90 $groupmappings[0] = $userlist;
93 $mform->addElement('select', 'group', get_string('group'), $grouplist);
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);
100 $opts = array('groupmappings' => $groupmappings);
101 $PAGE->requires
->yui_module('moodle-mod_attendance-groupfilter', 'M.mod_attendance.groupfilter.init', array($opts));
104 $ident[] =& $mform->createElement('checkbox', 'id', '', get_string('studentid', 'attendance'));
105 $ident[] =& $mform->createElement('checkbox', 'uname', '', get_string('username'));
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
);
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
);
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');
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);
133 $submitstring = get_string('ok');
134 $this->add_action_buttons(false
, $submitstring);
136 $mform->addElement('hidden', 'id', $cm->id
);
139 public function validation($data, $files) {
140 $errors = parent
::validation($data, $files);
142 // Validate the 'users' field.
143 if ($data['selectedusers'] && empty($data['users'])) {
144 $errors['users'] = get_string('mustselectusers', 'mod_attendance');