Make display of user 'extrafields' optional in report. (#294)
[moodle-mod_attendance.git] / update_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 * Update form
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 update 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_update_form extends moodleform {
35
36 /**
37 * Called to define this moodle form
38 *
39 * @return void
40 */
41 public function definition() {
42
43 global $DB;
44 $mform =& $this->_form;
45
46 $modcontext = $this->_customdata['modcontext'];
47 $sessionid = $this->_customdata['sessionid'];
48
49 if (!$sess = $DB->get_record('attendance_sessions', array('id' => $sessionid) )) {
50 error('No such session in this course');
51 }
52 $attendancesubnet = $DB->get_field('attendance', 'subnet', array('id' => $sess->attendanceid));
53 $defopts = array('maxfiles' => EDITOR_UNLIMITED_FILES, 'noclean' => true, 'context' => $modcontext);
54 $sess = file_prepare_standard_editor($sess, 'description', $defopts, $modcontext, 'mod_attendance', 'session', $sess->id);
55
56 $starttime = $sess->sessdate - usergetmidnight($sess->sessdate);
57 $starthour = floor($starttime / HOURSECS);
58 $startminute = floor(($starttime - $starthour * HOURSECS) / MINSECS);
59
60 $enddate = $sess->sessdate + $sess->duration;
61 $endtime = $enddate - usergetmidnight($enddate);
62 $endhour = floor($endtime / HOURSECS);
63 $endminute = floor(($endtime - $endhour * HOURSECS) / MINSECS);
64
65 $data = array('sessiondate' => $sess->sessdate,
66 'sestime' => array('starthour' => $starthour, 'startminute' => $startminute,
67 'endhour' => $endhour, 'endminute' => $endminute),
68 'sdescription' => $sess->description_editor,
69 'studentscanmark' => $sess->studentscanmark,
70 'studentpassword' => $sess->studentpassword,
71 'autoassignstatus' => $sess->autoassignstatus,
72 'subnet' => $sess->subnet,
73 'automark' => $sess->automark,
74 'absenteereport' => $sess->absenteereport,
75 'automarkcompleted' => 0);
76 if ($sess->subnet == $attendancesubnet) {
77 $data['usedefaultsubnet'] = 1;
78 } else {
79 $data['usedefaultsubnet'] = 0;
80 }
81
82 $mform->addElement('header', 'general', get_string('changesession', 'attendance'));
83
84 if ($sess->groupid == 0) {
85 $strtype = get_string('commonsession', 'attendance');
86 } else {
87 $groupname = $DB->get_field('groups', 'name', array('id' => $sess->groupid));
88 $strtype = get_string('group') . ': ' . $groupname;
89 }
90 $mform->addElement('static', 'sessiontypedescription', get_string('sessiontype', 'attendance'), $strtype);
91
92 $olddate = construct_session_full_date_time($sess->sessdate, $sess->duration);
93 $mform->addElement('static', 'olddate', get_string('olddate', 'attendance'), $olddate);
94
95 attendance_form_sessiondate_selector($mform);
96
97 // Show which status set is in use.
98 $maxstatusset = attendance_get_max_statusset($this->_customdata['att']->id);
99 if ($maxstatusset > 0) {
100 $mform->addElement('static', 'statusset', get_string('usestatusset', 'mod_attendance'),
101 attendance_get_setname($this->_customdata['att']->id, $sess->statusset));
102 }
103
104 $mform->addElement('editor', 'sdescription', get_string('description', 'attendance'),
105 array('rows' => 1, 'columns' => 80), $defopts);
106 $mform->setType('sdescription', PARAM_RAW);
107
108 // If warnings allow selector for reporting.
109 if (!empty(get_config('attendance', 'enablewarnings'))) {
110 $mform->addElement('checkbox', 'absenteereport', '', get_string('includeabsentee', 'attendance'));
111 $mform->addHelpButton('absenteereport', 'includeabsentee', 'attendance');
112 }
113
114 // Students can mark own attendance.
115 if (!empty(get_config('attendance', 'studentscanmark'))) {
116 $mform->addElement('header', 'headerstudentmarking', get_string('studentmarking', 'attendance'), true);
117 $mform->setExpanded('headerstudentmarking');
118
119 $mform->addElement('checkbox', 'studentscanmark', '', get_string('studentscanmark', 'attendance'));
120 $mform->addHelpButton('studentscanmark', 'studentscanmark', 'attendance');
121
122 $options2 = attendance_get_automarkoptions();
123
124 $mform->addElement('select', 'automark', get_string('automark', 'attendance'), $options2);
125 $mform->setType('automark', PARAM_INT);
126 $mform->addHelpButton('automark', 'automark', 'attendance');
127 $mform->hideif('automark', 'studentscanmark', 'notchecked');
128
129 $mform->addElement('text', 'studentpassword', get_string('studentpassword', 'attendance'));
130 $mform->setType('studentpassword', PARAM_TEXT);
131 $mform->addHelpButton('studentpassword', 'passwordgrp', 'attendance');
132 $mform->hideif('studentpassword', 'studentscanmark', 'notchecked');
133 $mform->hideif('studentpassword', 'automark', 'eq', ATTENDANCE_AUTOMARK_ALL);
134 $mform->hideif('randompassword', 'automark', 'eq', ATTENDANCE_AUTOMARK_ALL);
135 $mform->addElement('checkbox', 'autoassignstatus', '', get_string('autoassignstatus', 'attendance'));
136 $mform->addHelpButton('autoassignstatus', 'autoassignstatus', 'attendance');
137 $mform->hideif('autoassignstatus', 'studentscanmark', 'notchecked');
138
139 $mgroup = array();
140 $mgroup[] = & $mform->createElement('text', 'subnet', get_string('requiresubnet', 'attendance'));
141 $mform->setDefault('subnet', $this->_customdata['att']->subnet);
142 $mgroup[] = & $mform->createElement('checkbox', 'usedefaultsubnet', get_string('usedefaultsubnet', 'attendance'));
143 $mform->setDefault('usedefaultsubnet', 1);
144 $mform->setType('subnet', PARAM_TEXT);
145
146 $mform->addGroup($mgroup, 'subnetgrp', get_string('requiresubnet', 'attendance'), array(' '), false);
147 $mform->setAdvanced('subnetgrp');
148 $mform->addHelpButton('subnetgrp', 'requiresubnet', 'attendance');
149
150 $mform->hideif('subnetgrp', 'studentscanmark', 'notchecked');
151 $mform->hideif('subnet', 'usedefaultsubnet', 'checked');
152
153 $mform->addElement('hidden', 'automarkcompleted', '0');
154 $mform->settype('automarkcompleted', PARAM_INT);
155
156 } else {
157 $mform->addElement('hidden', 'studentscanmark', '0');
158 $mform->settype('studentscanmark', PARAM_INT);
159 $mform->addElement('hidden', 'subnet', '0');
160 $mform->settype('subnet', PARAM_TEXT);
161 $mform->addElement('hidden', 'automark', '0');
162 $mform->settype('automark', PARAM_INT);
163 $mform->addElement('hidden', 'automarkcompleted', '0');
164 $mform->settype('automarkcompleted', PARAM_INT);
165 $mform->addElement('hidden', 'autoassignstatus', '0');
166 $mform->setType('autoassignstatus', PARAM_INT);
167 }
168
169 $mform->setDefaults($data);
170
171 $this->add_action_buttons(true);
172 }
173
174 /**
175 * Perform minimal validation on the settings form
176 * @param array $data
177 * @param array $files
178 */
179 public function validation($data, $files) {
180 $errors = parent::validation($data, $files);
181
182 $sesstarttime = $data['sestime']['starthour'] * HOURSECS + $data['sestime']['startminute'] * MINSECS;
183 $sesendtime = $data['sestime']['endhour'] * HOURSECS + $data['sestime']['endminute'] * MINSECS;
184 if ($sesendtime < $sesstarttime) {
185 $errors['sestime'] = get_string('invalidsessionendtime', 'attendance');
186 }
187
188 return $errors;
189 }
190 }