3a2327266d4eabd2641485cd32117a4a9423a8fc
[moodle.git] / mod / assign / mod_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/>.
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  */
25 defined('MOODLE_INTERNAL') || die('Direct access to this script is forbidden.');
27 require_once($CFG->dirroot.'/course/moodleform_mod.php');
28 require_once($CFG->dirroot . '/mod/assign/locallib.php');
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  */
37 class mod_assign_mod_form extends moodleform_mod {
39     /**
40      * Called to define this moodle form
41      *
42      * @return void
43      */
44     public function definition() {
45         global $CFG, $COURSE, $DB, $PAGE;
46         $mform = $this->_form;
48         $mform->addElement('header', 'general', get_string('general', 'form'));
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');
57         $mform->addRule('name', get_string('maximumchars', '', 255), 'maxlength', 255, 'client');
59         $this->standard_intro_elements(get_string('description', 'assign'));
61         $mform->addElement('filemanager', 'introattachments',
62                             get_string('introattachments', 'assign'),
63                             null, array('subdirs' => 0, 'maxbytes' => $COURSE->maxbytes) );
64         $mform->addHelpButton('introattachments', 'introattachments', 'assign');
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             }
76             $course = $DB->get_record('course', array('id'=>$this->current->course), '*', MUST_EXIST);
77             $assignment->set_course($course);
78         }
80         $config = get_config('assign');
82         $mform->addElement('header', 'availability', get_string('availability', 'assign'));
83         $mform->setExpanded('availability', true);
85         $name = get_string('allowsubmissionsfromdate', 'assign');
86         $options = array('optional'=>true);
87         $mform->addElement('date_time_selector', 'allowsubmissionsfromdate', $name, $options);
88         $mform->addHelpButton('allowsubmissionsfromdate', 'allowsubmissionsfromdate', 'assign');
90         $name = get_string('duedate', 'assign');
91         $mform->addElement('date_time_selector', 'duedate', $name, array('optional'=>true));
92         $mform->addHelpButton('duedate', 'duedate', 'assign');
94         $name = get_string('cutoffdate', 'assign');
95         $mform->addElement('date_time_selector', 'cutoffdate', $name, array('optional'=>true));
96         $mform->addHelpButton('cutoffdate', 'cutoffdate', 'assign');
98         $name = get_string('gradingduedate', 'assign');
99         $mform->addElement('date_time_selector', 'gradingduedate', $name, array('optional' => true));
100         $mform->addHelpButton('gradingduedate', 'gradingduedate', 'assign');
102         $name = get_string('alwaysshowdescription', 'assign');
103         $mform->addElement('checkbox', 'alwaysshowdescription', $name);
104         $mform->addHelpButton('alwaysshowdescription', 'alwaysshowdescription', 'assign');
105         $mform->disabledIf('alwaysshowdescription', 'allowsubmissionsfromdate[enabled]', 'notchecked');
107         $assignment->add_all_plugin_settings($mform);
109         $mform->addElement('header', 'submissionsettings', get_string('submissionsettings', 'assign'));
111         $name = get_string('submissiondrafts', 'assign');
112         $mform->addElement('selectyesno', 'submissiondrafts', $name);
113         $mform->addHelpButton('submissiondrafts', 'submissiondrafts', 'assign');
115         $name = get_string('requiresubmissionstatement', 'assign');
116         $mform->addElement('selectyesno', 'requiresubmissionstatement', $name);
117         $mform->addHelpButton('requiresubmissionstatement',
118                               'requiresubmissionstatement',
119                               'assign');
120         $mform->setType('requiresubmissionstatement', PARAM_BOOL);
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);
128         $mform->addHelpButton('attemptreopenmethod', 'attemptreopenmethod', 'mod_assign');
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');
134         $mform->disabledIf('maxattempts', 'attemptreopenmethod', 'eq', ASSIGN_ATTEMPT_REOPEN_METHOD_NONE);
136         $mform->addElement('header', 'groupsubmissionsettings', get_string('groupsubmissionsettings', 'assign'));
138         $name = get_string('teamsubmission', 'assign');
139         $mform->addElement('selectyesno', 'teamsubmission', $name);
140         $mform->addHelpButton('teamsubmission', 'teamsubmission', 'assign');
141         if ($assignment->has_submissions_or_grades()) {
142             $mform->freeze('teamsubmission');
143         }
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);
151         $mform->disabledIf('preventsubmissionnotingroup', 'teamsubmission', 'eq', 0);
153         $name = get_string('requireallteammemberssubmit', 'assign');
154         $mform->addElement('selectyesno', 'requireallteammemberssubmit', $name);
155         $mform->addHelpButton('requireallteammemberssubmit', 'requireallteammemberssubmit', 'assign');
156         $mform->disabledIf('requireallteammemberssubmit', 'teamsubmission', 'eq', 0);
157         $mform->disabledIf('requireallteammemberssubmit', 'submissiondrafts', 'eq', 0);
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         }
166         $name = get_string('teamsubmissiongroupingid', 'assign');
167         $mform->addElement('select', 'teamsubmissiongroupingid', $name, $options);
168         $mform->addHelpButton('teamsubmissiongroupingid', 'teamsubmissiongroupingid', 'assign');
169         $mform->disabledIf('teamsubmissiongroupingid', 'teamsubmission', 'eq', 0);
170         if ($assignment->has_submissions_or_grades()) {
171             $mform->freeze('teamsubmissiongroupingid');
172         }
174         $mform->addElement('header', 'notifications', get_string('notifications', 'assign'));
176         $name = get_string('sendnotifications', 'assign');
177         $mform->addElement('selectyesno', 'sendnotifications', $name);
178         $mform->addHelpButton('sendnotifications', 'sendnotifications', 'assign');
180         $name = get_string('sendlatenotifications', 'assign');
181         $mform->addElement('selectyesno', 'sendlatenotifications', $name);
182         $mform->addHelpButton('sendlatenotifications', 'sendlatenotifications', 'assign');
183         $mform->disabledIf('sendlatenotifications', 'sendnotifications', 'eq', 1);
185         $name = get_string('sendstudentnotificationsdefault', 'assign');
186         $mform->addElement('selectyesno', 'sendstudentnotifications', $name);
187         $mform->addHelpButton('sendstudentnotifications', 'sendstudentnotificationsdefault', 'assign');
189         // Plagiarism enabling form.
190         if (!empty($CFG->enableplagiarism)) {
191             require_once($CFG->libdir . '/plagiarismlib.php');
192             plagiarism_get_form_elements_module($mform, $ctx->get_course_context(), 'mod_assign');
193         }
195         $this->standard_grading_coursemodule_elements();
196         $name = get_string('blindmarking', 'assign');
197         $mform->addElement('selectyesno', 'blindmarking', $name);
198         $mform->addHelpButton('blindmarking', 'blindmarking', 'assign');
199         if ($assignment->has_submissions_or_grades() ) {
200             $mform->freeze('blindmarking');
201         }
203         $name = get_string('markingworkflow', 'assign');
204         $mform->addElement('selectyesno', 'markingworkflow', $name);
205         $mform->addHelpButton('markingworkflow', 'markingworkflow', 'assign');
207         $name = get_string('markingallocation', 'assign');
208         $mform->addElement('selectyesno', 'markingallocation', $name);
209         $mform->addHelpButton('markingallocation', 'markingallocation', 'assign');
210         $mform->disabledIf('markingallocation', 'markingworkflow', 'eq', 0);
212         $this->standard_coursemodule_elements();
213         $this->apply_admin_defaults();
215         $this->add_action_buttons();
216     }
218     /**
219      * Perform minimal validation on the settings form
220      * @param array $data
221      * @param array $files
222      */
223     public function validation($data, $files) {
224         $errors = parent::validation($data, $files);
226         if ($data['allowsubmissionsfromdate'] && $data['duedate']) {
227             if ($data['allowsubmissionsfromdate'] > $data['duedate']) {
228                 $errors['duedate'] = get_string('duedatevalidation', 'assign');
229             }
230         }
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         }
241         if ($data['blindmarking'] && $data['attemptreopenmethod'] == ASSIGN_ATTEMPT_REOPEN_METHOD_UNTILPASS) {
242             $errors['attemptreopenmethod'] = get_string('reopenuntilpassincompatiblewithblindmarking', 'assign');
243         }
245         return $errors;
246     }
248     /**
249      * Any data processing needed before the form is displayed
250      * (needed to set up draft areas for editor and filemanager elements)
251      * @param array $defaultvalues
252      */
253     public function data_preprocessing(&$defaultvalues) {
254         global $DB;
256         $ctx = null;
257         if ($this->current && $this->current->coursemodule) {
258             $cm = get_coursemodule_from_instance('assign', $this->current->id, 0, false, MUST_EXIST);
259             $ctx = context_module::instance($cm->id);
260         }
261         $assignment = new assign($ctx, null, null);
262         if ($this->current && $this->current->course) {
263             if (!$ctx) {
264                 $ctx = context_course::instance($this->current->course);
265             }
266             $course = $DB->get_record('course', array('id'=>$this->current->course), '*', MUST_EXIST);
267             $assignment->set_course($course);
268         }
270         $draftitemid = file_get_submitted_draft_itemid('introattachments');
271         file_prepare_draft_area($draftitemid, $ctx->id, 'mod_assign', ASSIGN_INTROATTACHMENT_FILEAREA,
272                                 0, array('subdirs' => 0));
273         $defaultvalues['introattachments'] = $draftitemid;
275         $assignment->plugin_data_preprocessing($defaultvalues);
276     }
278     /**
279      * Add any custom completion rules to the form.
280      *
281      * @return array Contains the names of the added form elements
282      */
283     public function add_completion_rules() {
284         $mform =& $this->_form;
286         $mform->addElement('advcheckbox', 'completionsubmit', '', get_string('completionsubmit', 'assign'));
287         // Enable this completion rule by default.
288         $mform->setDefault('completionsubmit', 1);
289         return array('completionsubmit');
290     }
292     /**
293      * Determines if completion is enabled for this module.
294      *
295      * @param array $data
296      * @return bool
297      */
298     public function completion_rule_enabled($data) {
299         return !empty($data['completionsubmit']);
300     }