MDL-59666 mod_assign: Add validation for grading due date
[moodle.git] / mod / assign / mod_form.php
CommitLineData
bbd0e548
DW
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 * This file contains the forms to create and edit an instance of this module
19 *
20 * @package mod_assign
21 * @copyright 2012 NetSpot {@link http://www.netspot.com.au}
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23 */
24
25defined('MOODLE_INTERNAL') || die('Direct access to this script is forbidden.');
26
e5403f8c 27require_once($CFG->dirroot.'/course/moodleform_mod.php');
bbd0e548
DW
28require_once($CFG->dirroot . '/mod/assign/locallib.php');
29
30/**
31 * Assignment settings form.
32 *
33 * @package mod_assign
34 * @copyright 2012 NetSpot {@link http://www.netspot.com.au}
35 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
36 */
37class mod_assign_mod_form extends moodleform_mod {
38
39 /**
40 * Called to define this moodle form
41 *
42 * @return void
43 */
e5403f8c 44 public function definition() {
7faf78cb 45 global $CFG, $COURSE, $DB, $PAGE;
bbd0e548
DW
46 $mform = $this->_form;
47
48 $mform->addElement('header', 'general', get_string('general', 'form'));
49
50 $mform->addElement('text', 'name', get_string('assignmentname', 'assign'), array('size'=>'64'));
51 if (!empty($CFG->formatstringstriptags)) {
52 $mform->setType('name', PARAM_TEXT);
53 } else {
54 $mform->setType('name', PARAM_CLEANHTML);
55 }
56 $mform->addRule('name', null, 'required', null, 'client');
a74cd331 57 $mform->addRule('name', get_string('maximumchars', '', 255), 'maxlength', 255, 'client');
bbd0e548 58
6398ff53 59 $this->standard_intro_elements(get_string('description', 'assign'));
bbd0e548 60
7faf78cb
HB
61 $mform->addElement('filemanager', 'introattachments',
62 get_string('introattachments', 'assign'),
63 null, array('subdirs' => 0, 'maxbytes' => $COURSE->maxbytes) );
64 $mform->addHelpButton('introattachments', 'introattachments', 'assign');
65
bbd0e548
DW
66 $ctx = null;
67 if ($this->current && $this->current->coursemodule) {
68 $cm = get_coursemodule_from_instance('assign', $this->current->id, 0, false, MUST_EXIST);
69 $ctx = context_module::instance($cm->id);
70 }
71 $assignment = new assign($ctx, null, null);
72 if ($this->current && $this->current->course) {
73 if (!$ctx) {
74 $ctx = context_course::instance($this->current->course);
75 }
74df2951 76 $course = $DB->get_record('course', array('id'=>$this->current->course), '*', MUST_EXIST);
e5403f8c 77 $assignment->set_course($course);
bbd0e548
DW
78 }
79
94f26900
DW
80 $config = get_config('assign');
81
b0da618b 82 $mform->addElement('header', 'availability', get_string('availability', 'assign'));
613abe1b 83 $mform->setExpanded('availability', true);
e5403f8c
DW
84
85 $name = get_string('allowsubmissionsfromdate', 'assign');
86 $options = array('optional'=>true);
87 $mform->addElement('date_time_selector', 'allowsubmissionsfromdate', $name, $options);
bbd0e548 88 $mform->addHelpButton('allowsubmissionsfromdate', 'allowsubmissionsfromdate', 'assign');
e5403f8c 89
b618f2d9
SH
90 $name = get_string('duedate', 'assign');
91 $mform->addElement('date_time_selector', 'duedate', $name, array('optional'=>true));
92 $mform->addHelpButton('duedate', 'duedate', 'assign');
e5403f8c
DW
93
94 $name = get_string('cutoffdate', 'assign');
95 $mform->addElement('date_time_selector', 'cutoffdate', $name, array('optional'=>true));
9e795179 96 $mform->addHelpButton('cutoffdate', 'cutoffdate', 'assign');
e5403f8c 97
3e1c0275
MN
98 $name = get_string('gradingduedate', 'assign');
99 $mform->addElement('date_time_selector', 'gradingduedate', $name, array('optional' => true));
100 $mform->addHelpButton('gradingduedate', 'gradingduedate', 'assign');
101
e5403f8c 102 $name = get_string('alwaysshowdescription', 'assign');
b0da618b 103 $mform->addElement('checkbox', 'alwaysshowdescription', $name);
bbd0e548 104 $mform->addHelpButton('alwaysshowdescription', 'alwaysshowdescription', 'assign');
613abe1b
FM
105 $mform->disabledIf('alwaysshowdescription', 'allowsubmissionsfromdate[enabled]', 'notchecked');
106
107 $assignment->add_all_plugin_settings($mform);
e5403f8c 108
b0da618b
DW
109 $mform->addElement('header', 'submissionsettings', get_string('submissionsettings', 'assign'));
110
e5403f8c
DW
111 $name = get_string('submissiondrafts', 'assign');
112 $mform->addElement('selectyesno', 'submissiondrafts', $name);
bbd0e548 113 $mform->addHelpButton('submissiondrafts', 'submissiondrafts', 'assign');
e5403f8c 114
de6a0a39
DW
115 $name = get_string('requiresubmissionstatement', 'assign');
116 $mform->addElement('selectyesno', 'requiresubmissionstatement', $name);
117 $mform->addHelpButton('requiresubmissionstatement',
118 'requiresubmissionstatement',
119 'assign');
68d646f3 120 $mform->setType('requiresubmissionstatement', PARAM_BOOL);
94f26900 121
df211804
DW
122 $options = array(
123 ASSIGN_ATTEMPT_REOPEN_METHOD_NONE => get_string('attemptreopenmethod_none', 'mod_assign'),
124 ASSIGN_ATTEMPT_REOPEN_METHOD_MANUAL => get_string('attemptreopenmethod_manual', 'mod_assign'),
125 ASSIGN_ATTEMPT_REOPEN_METHOD_UNTILPASS => get_string('attemptreopenmethod_untilpass', 'mod_assign')
126 );
127 $mform->addElement('select', 'attemptreopenmethod', get_string('attemptreopenmethod', 'mod_assign'), $options);
df211804
DW
128 $mform->addHelpButton('attemptreopenmethod', 'attemptreopenmethod', 'mod_assign');
129
130 $options = array(ASSIGN_UNLIMITED_ATTEMPTS => get_string('unlimitedattempts', 'mod_assign'));
131 $options += array_combine(range(1, 30), range(1, 30));
132 $mform->addElement('select', 'maxattempts', get_string('maxattempts', 'mod_assign'), $options);
133 $mform->addHelpButton('maxattempts', 'maxattempts', 'assign');
df211804
DW
134 $mform->disabledIf('maxattempts', 'attemptreopenmethod', 'eq', ASSIGN_ATTEMPT_REOPEN_METHOD_NONE);
135
b0da618b 136 $mform->addElement('header', 'groupsubmissionsettings', get_string('groupsubmissionsettings', 'assign'));
e5403f8c
DW
137
138 $name = get_string('teamsubmission', 'assign');
139 $mform->addElement('selectyesno', 'teamsubmission', $name);
12a1a0da 140 $mform->addHelpButton('teamsubmission', 'teamsubmission', 'assign');
5ae56c6e
PS
141 if ($assignment->has_submissions_or_grades()) {
142 $mform->freeze('teamsubmission');
143 }
e5403f8c 144
e528997a
AH
145 $name = get_string('preventsubmissionnotingroup', 'assign');
146 $mform->addElement('selectyesno', 'preventsubmissionnotingroup', $name);
147 $mform->addHelpButton('preventsubmissionnotingroup',
148 'preventsubmissionnotingroup',
149 'assign');
150 $mform->setType('preventsubmissionnotingroup', PARAM_BOOL);
c3f38fa6 151 $mform->disabledIf('preventsubmissionnotingroup', 'teamsubmission', 'eq', 0);
e528997a 152
e5403f8c
DW
153 $name = get_string('requireallteammemberssubmit', 'assign');
154 $mform->addElement('selectyesno', 'requireallteammemberssubmit', $name);
12a1a0da 155 $mform->addHelpButton('requireallteammemberssubmit', 'requireallteammemberssubmit', 'assign');
12a1a0da
DW
156 $mform->disabledIf('requireallteammemberssubmit', 'teamsubmission', 'eq', 0);
157 $mform->disabledIf('requireallteammemberssubmit', 'submissiondrafts', 'eq', 0);
158
159 $groupings = groups_get_all_groupings($assignment->get_course()->id);
160 $options = array();
161 $options[0] = get_string('none');
162 foreach ($groupings as $grouping) {
163 $options[$grouping->id] = $grouping->name;
164 }
e5403f8c
DW
165
166 $name = get_string('teamsubmissiongroupingid', 'assign');
167 $mform->addElement('select', 'teamsubmissiongroupingid', $name, $options);
12a1a0da 168 $mform->addHelpButton('teamsubmissiongroupingid', 'teamsubmissiongroupingid', 'assign');
12a1a0da 169 $mform->disabledIf('teamsubmissiongroupingid', 'teamsubmission', 'eq', 0);
6849d15d
FM
170 if ($assignment->has_submissions_or_grades()) {
171 $mform->freeze('teamsubmissiongroupingid');
172 }
12a1a0da 173
b0da618b
DW
174 $mform->addElement('header', 'notifications', get_string('notifications', 'assign'));
175
176 $name = get_string('sendnotifications', 'assign');
177 $mform->addElement('selectyesno', 'sendnotifications', $name);
178 $mform->addHelpButton('sendnotifications', 'sendnotifications', 'assign');
b0da618b
DW
179
180 $name = get_string('sendlatenotifications', 'assign');
181 $mform->addElement('selectyesno', 'sendlatenotifications', $name);
182 $mform->addHelpButton('sendlatenotifications', 'sendlatenotifications', 'assign');
b0da618b 183 $mform->disabledIf('sendlatenotifications', 'sendnotifications', 'eq', 1);
b473171a 184
8e1266bf
DW
185 $name = get_string('sendstudentnotificationsdefault', 'assign');
186 $mform->addElement('selectyesno', 'sendstudentnotifications', $name);
187 $mform->addHelpButton('sendstudentnotifications', 'sendstudentnotificationsdefault', 'assign');
188
e5403f8c 189 // Plagiarism enabling form.
bbd0e548 190 if (!empty($CFG->enableplagiarism)) {
bbd0e548 191 require_once($CFG->libdir . '/plagiarismlib.php');
67fbfe8b 192 plagiarism_get_form_elements_module($mform, $ctx->get_course_context(), 'mod_assign');
bbd0e548
DW
193 }
194
bbd0e548 195 $this->standard_grading_coursemodule_elements();
b0da618b
DW
196 $name = get_string('blindmarking', 'assign');
197 $mform->addElement('selectyesno', 'blindmarking', $name);
198 $mform->addHelpButton('blindmarking', 'blindmarking', 'assign');
b0da618b
DW
199 if ($assignment->has_submissions_or_grades() ) {
200 $mform->freeze('blindmarking');
201 }
202
bb6471aa
DW
203 $name = get_string('markingworkflow', 'assign');
204 $mform->addElement('selectyesno', 'markingworkflow', $name);
f8d107b3 205 $mform->addHelpButton('markingworkflow', 'markingworkflow', 'assign');
f8d107b3 206
bb6471aa
DW
207 $name = get_string('markingallocation', 'assign');
208 $mform->addElement('selectyesno', 'markingallocation', $name);
f8d107b3 209 $mform->addHelpButton('markingallocation', 'markingallocation', 'assign');
bb6471aa 210 $mform->disabledIf('markingallocation', 'markingworkflow', 'eq', 0);
f8d107b3 211
bbd0e548 212 $this->standard_coursemodule_elements();
a8fe74e9 213 $this->apply_admin_defaults();
bbd0e548
DW
214
215 $this->add_action_buttons();
216 }
217
218 /**
219 * Perform minimal validation on the settings form
220 * @param array $data
221 * @param array $files
222 */
e5403f8c 223 public function validation($data, $files) {
bbd0e548
DW
224 $errors = parent::validation($data, $files);
225
226 if ($data['allowsubmissionsfromdate'] && $data['duedate']) {
227 if ($data['allowsubmissionsfromdate'] > $data['duedate']) {
228 $errors['duedate'] = get_string('duedatevalidation', 'assign');
229 }
230 }
9e795179
DW
231 if ($data['duedate'] && $data['cutoffdate']) {
232 if ($data['duedate'] > $data['cutoffdate']) {
233 $errors['cutoffdate'] = get_string('cutoffdatevalidation', 'assign');
234 }
235 }
236 if ($data['allowsubmissionsfromdate'] && $data['cutoffdate']) {
237 if ($data['allowsubmissionsfromdate'] > $data['cutoffdate']) {
238 $errors['cutoffdate'] = get_string('cutoffdatefromdatevalidation', 'assign');
239 }
240 }
b5ede848
JP
241 if ($data['gradingduedate']) {
242 if ($data['allowsubmissionsfromdate'] && $data['allowsubmissionsfromdate'] > $data['gradingduedate']) {
243 $errors['gradingduedate'] = get_string('gradingduefromdatevalidation', 'assign');
244 }
245 if ($data['duedate'] && $data['duedate'] > $data['gradingduedate']) {
246 $errors['gradingduedate'] = get_string('gradingdueduedatevalidation', 'assign');
247 }
248 }
3b775a0b
DW
249 if ($data['blindmarking'] && $data['attemptreopenmethod'] == ASSIGN_ATTEMPT_REOPEN_METHOD_UNTILPASS) {
250 $errors['attemptreopenmethod'] = get_string('reopenuntilpassincompatiblewithblindmarking', 'assign');
251 }
9e795179 252
bbd0e548
DW
253 return $errors;
254 }
255
256 /**
257 * Any data processing needed before the form is displayed
258 * (needed to set up draft areas for editor and filemanager elements)
259 * @param array $defaultvalues
260 */
e5403f8c 261 public function data_preprocessing(&$defaultvalues) {
bbd0e548
DW
262 global $DB;
263
264 $ctx = null;
265 if ($this->current && $this->current->coursemodule) {
266 $cm = get_coursemodule_from_instance('assign', $this->current->id, 0, false, MUST_EXIST);
267 $ctx = context_module::instance($cm->id);
268 }
269 $assignment = new assign($ctx, null, null);
270 if ($this->current && $this->current->course) {
271 if (!$ctx) {
272 $ctx = context_course::instance($this->current->course);
273 }
74df2951 274 $course = $DB->get_record('course', array('id'=>$this->current->course), '*', MUST_EXIST);
e5403f8c 275 $assignment->set_course($course);
bbd0e548 276 }
7faf78cb
HB
277
278 $draftitemid = file_get_submitted_draft_itemid('introattachments');
279 file_prepare_draft_area($draftitemid, $ctx->id, 'mod_assign', ASSIGN_INTROATTACHMENT_FILEAREA,
280 0, array('subdirs' => 0));
281 $defaultvalues['introattachments'] = $draftitemid;
282
bbd0e548
DW
283 $assignment->plugin_data_preprocessing($defaultvalues);
284 }
285
1561a37c
DW
286 /**
287 * Add any custom completion rules to the form.
288 *
289 * @return array Contains the names of the added form elements
290 */
e5403f8c 291 public function add_completion_rules() {
79ed4d84
DW
292 $mform =& $this->_form;
293
32b93ea7 294 $mform->addElement('advcheckbox', 'completionsubmit', '', get_string('completionsubmit', 'assign'));
e44d71a6
JP
295 // Enable this completion rule by default.
296 $mform->setDefault('completionsubmit', 1);
79ed4d84
DW
297 return array('completionsubmit');
298 }
299
1561a37c
DW
300 /**
301 * Determines if completion is enabled for this module.
302 *
303 * @param array $data
304 * @return bool
305 */
e5403f8c 306 public function completion_rule_enabled($data) {
79ed4d84
DW
307 return !empty($data['completionsubmit']);
308 }
bbd0e548
DW
309
310}