Fixes #223 allow student attendance to be disabled at site-level
[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
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 $defopts = array('maxfiles' => EDITOR_UNLIMITED_FILES, 'noclean' => true, 'context' => $modcontext);
53 $sess = file_prepare_standard_editor($sess, 'description', $defopts, $modcontext, 'mod_attendance', 'session', $sess->id);
54
55 $starttime = $sess->sessdate - usergetmidnight($sess->sessdate);
56 $starthour = floor($starttime / HOURSECS);
57 $startminute = floor(($starttime - $starthour * HOURSECS) / MINSECS);
58
59 $enddate = $sess->sessdate + $sess->duration;
60 $endtime = $enddate - usergetmidnight($enddate);
61 $endhour = floor($endtime / HOURSECS);
62 $endminute = floor(($endtime - $endhour * HOURSECS) / MINSECS);
63
64 $sesendtime = $sess->sessdate + $sess->duration;
65 $data = array('sessiondate' => $sess->sessdate,
66 'sestime' => array('starthour' => $starthour, 'startminute' => $startminute,
67 'endhour' => $endhour, 'endminute' => $endminute),
68 'sdescription' => $sess->description_editor);
69
70 $mform->addElement('header', 'general', get_string('changesession', 'attendance'));
71
72 if ($sess->groupid == 0) {
73 $strtype = get_string('commonsession', 'attendance');
74 } else {
75 $groupname = $DB->get_field('groups', 'name', array('id' => $sess->groupid));
76 $strtype = get_string('group') . ': ' . $groupname;
77 }
78 $mform->addElement('static', 'sessiontypedescription', get_string('sessiontype', 'attendance'), $strtype);
79
80 $olddate = construct_session_full_date_time($sess->sessdate, $sess->duration);
81 $mform->addElement('static', 'olddate', get_string('olddate', 'attendance'), $olddate);
82
83 attendance_form_sessiondate_selector($mform);
84
85 // Show which status set is in use.
86 $maxstatusset = attendance_get_max_statusset($this->_customdata['att']->id);
87 if ($maxstatusset > 0) {
88 $mform->addElement('static', 'statusset', get_string('usestatusset', 'mod_attendance'),
89 attendance_get_setname($this->_customdata['att']->id, $sess->statusset));
90 }
91
92 $mform->addElement('editor', 'sdescription', get_string('description', 'attendance'),
93 array('rows' => 1, 'columns' => 80), $defopts);
94 $mform->setType('sdescription', PARAM_RAW);
95
96 $mform->setDefaults($data);
97
98 $this->add_action_buttons(true);
99 }
100
101 /**
102 * Perform minimal validation on the settings form
103 * @param array $data
104 * @param array $files
105 */
106 public function validation($data, $files) {
107 $errors = parent::validation($data, $files);
108
109 $sesstarttime = $data['sestime']['starthour'] * HOURSECS + $data['sestime']['startminute'] * MINSECS;
110 $sesendtime = $data['sestime']['endhour'] * HOURSECS + $data['sestime']['endminute'] * MINSECS;
111 if ($sesendtime < $sesstarttime) {
112 $errors['sestime'] = get_string('invalidsessionendtime', 'attendance');
113 }
114
115 return $errors;
116 }
117 }