MDL-37621 assign: Fix for DB error on update.
[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, $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->add_intro_editor(true, get_string('description', 'assign'));
61         $ctx = null;
62         if ($this->current && $this->current->coursemodule) {
63             $cm = get_coursemodule_from_instance('assign', $this->current->id, 0, false, MUST_EXIST);
64             $ctx = context_module::instance($cm->id);
65         }
66         $assignment = new assign($ctx, null, null);
67         if ($this->current && $this->current->course) {
68             if (!$ctx) {
69                 $ctx = context_course::instance($this->current->course);
70             }
71             $course = $DB->get_record('course', array('id'=>$this->current->course), '*', MUST_EXIST);
72             $assignment->set_course($course);
73         }
75         $config = get_config('assign');
77         $mform->addElement('header', 'availability', get_string('availability', 'assign'));
78         $mform->setExpanded('availability', true);
80         $name = get_string('allowsubmissionsfromdate', 'assign');
81         $options = array('optional'=>true);
82         $mform->addElement('date_time_selector', 'allowsubmissionsfromdate', $name, $options);
83         $mform->addHelpButton('allowsubmissionsfromdate', 'allowsubmissionsfromdate', 'assign');
85         $name = get_string('duedate', 'assign');
86         $mform->addElement('date_time_selector', 'duedate', $name, array('optional'=>true));
87         $mform->addHelpButton('duedate', 'duedate', 'assign');
89         $name = get_string('cutoffdate', 'assign');
90         $mform->addElement('date_time_selector', 'cutoffdate', $name, array('optional'=>true));
91         $mform->addHelpButton('cutoffdate', 'cutoffdate', 'assign');
93         $name = get_string('alwaysshowdescription', 'assign');
94         $mform->addElement('checkbox', 'alwaysshowdescription', $name);
95         $mform->addHelpButton('alwaysshowdescription', 'alwaysshowdescription', 'assign');
96         $mform->disabledIf('alwaysshowdescription', 'allowsubmissionsfromdate[enabled]', 'notchecked');
98         $assignment->add_all_plugin_settings($mform);
100         $mform->addElement('header', 'submissionsettings', get_string('submissionsettings', 'assign'));
102         $name = get_string('submissiondrafts', 'assign');
103         $mform->addElement('selectyesno', 'submissiondrafts', $name);
104         $mform->addHelpButton('submissiondrafts', 'submissiondrafts', 'assign');
106         $name = get_string('requiresubmissionstatement', 'assign');
107         $mform->addElement('selectyesno', 'requiresubmissionstatement', $name);
108         $mform->addHelpButton('requiresubmissionstatement',
109                               'requiresubmissionstatement',
110                               'assign');
111         $mform->setType('requiresubmissionstatement', PARAM_BOOL);
113         $options = array(
114             ASSIGN_ATTEMPT_REOPEN_METHOD_NONE => get_string('attemptreopenmethod_none', 'mod_assign'),
115             ASSIGN_ATTEMPT_REOPEN_METHOD_MANUAL => get_string('attemptreopenmethod_manual', 'mod_assign'),
116             ASSIGN_ATTEMPT_REOPEN_METHOD_UNTILPASS => get_string('attemptreopenmethod_untilpass', 'mod_assign')
117         );
118         $mform->addElement('select', 'attemptreopenmethod', get_string('attemptreopenmethod', 'mod_assign'), $options);
119         $mform->addHelpButton('attemptreopenmethod', 'attemptreopenmethod', 'mod_assign');
121         $options = array(ASSIGN_UNLIMITED_ATTEMPTS => get_string('unlimitedattempts', 'mod_assign'));
122         $options += array_combine(range(1, 30), range(1, 30));
123         $mform->addElement('select', 'maxattempts', get_string('maxattempts', 'mod_assign'), $options);
124         $mform->addHelpButton('maxattempts', 'maxattempts', 'assign');
125         $mform->disabledIf('maxattempts', 'attemptreopenmethod', 'eq', ASSIGN_ATTEMPT_REOPEN_METHOD_NONE);
127         $mform->addElement('header', 'groupsubmissionsettings', get_string('groupsubmissionsettings', 'assign'));
129         $name = get_string('teamsubmission', 'assign');
130         $mform->addElement('selectyesno', 'teamsubmission', $name);
131         $mform->addHelpButton('teamsubmission', 'teamsubmission', 'assign');
133         $name = get_string('requireallteammemberssubmit', 'assign');
134         $mform->addElement('selectyesno', 'requireallteammemberssubmit', $name);
135         $mform->addHelpButton('requireallteammemberssubmit', 'requireallteammemberssubmit', 'assign');
136         $mform->disabledIf('requireallteammemberssubmit', 'teamsubmission', 'eq', 0);
137         $mform->disabledIf('requireallteammemberssubmit', 'submissiondrafts', 'eq', 0);
139         $groupings = groups_get_all_groupings($assignment->get_course()->id);
140         $options = array();
141         $options[0] = get_string('none');
142         foreach ($groupings as $grouping) {
143             $options[$grouping->id] = $grouping->name;
144         }
146         $name = get_string('teamsubmissiongroupingid', 'assign');
147         $mform->addElement('select', 'teamsubmissiongroupingid', $name, $options);
148         $mform->addHelpButton('teamsubmissiongroupingid', 'teamsubmissiongroupingid', 'assign');
149         $mform->disabledIf('teamsubmissiongroupingid', 'teamsubmission', 'eq', 0);
151         $mform->addElement('header', 'notifications', get_string('notifications', 'assign'));
153         $name = get_string('sendnotifications', 'assign');
154         $mform->addElement('selectyesno', 'sendnotifications', $name);
155         $mform->addHelpButton('sendnotifications', 'sendnotifications', 'assign');
157         $name = get_string('sendlatenotifications', 'assign');
158         $mform->addElement('selectyesno', 'sendlatenotifications', $name);
159         $mform->addHelpButton('sendlatenotifications', 'sendlatenotifications', 'assign');
160         $mform->disabledIf('sendlatenotifications', 'sendnotifications', 'eq', 1);
162         // Plagiarism enabling form.
163         if (!empty($CFG->enableplagiarism)) {
164             require_once($CFG->libdir . '/plagiarismlib.php');
165             plagiarism_get_form_elements_module($mform, $ctx->get_course_context(), 'mod_assign');
166         }
168         $this->standard_grading_coursemodule_elements();
169         $name = get_string('blindmarking', 'assign');
170         $mform->addElement('selectyesno', 'blindmarking', $name);
171         $mform->addHelpButton('blindmarking', 'blindmarking', 'assign');
172         if ($assignment->has_submissions_or_grades() ) {
173             $mform->freeze('blindmarking');
174         }
176         $name = get_string('markingworkflow', 'assign');
177         $mform->addElement('selectyesno', 'markingworkflow', $name);
178         $mform->addHelpButton('markingworkflow', 'markingworkflow', 'assign');
180         $name = get_string('markingallocation', 'assign');
181         $mform->addElement('selectyesno', 'markingallocation', $name);
182         $mform->addHelpButton('markingallocation', 'markingallocation', 'assign');
183         $mform->disabledIf('markingallocation', 'markingworkflow', 'eq', 0);
185         $this->standard_coursemodule_elements();
186         $this->apply_admin_defaults();
188         $this->add_action_buttons();
190         // Add warning popup/noscript tag, if grades are changed by user.
191         $hasgrade = false;
192         if (!empty($this->_instance)) {
193             $hasgrade = $DB->record_exists_select('assign_grades',
194                                                   'assignment = ? AND grade <> -1',
195                                                   array($this->_instance));
196         }
198         if ($mform->elementExists('grade') && $hasgrade) {
199             $module = array(
200                 'name' => 'mod_assign',
201                 'fullpath' => '/mod/assign/module.js',
202                 'requires' => array('node', 'event'),
203                 'strings' => array(array('changegradewarning', 'mod_assign'))
204                 );
205             $PAGE->requires->js_init_call('M.mod_assign.init_grade_change', null, false, $module);
207             // Add noscript tag in case.
208             $noscriptwarning = $mform->createElement('static',
209                                                      'warning',
210                                                      null,
211                                                      html_writer::tag('noscript',
212                                                      get_string('changegradewarning', 'mod_assign')));
213             $mform->insertElementBefore($noscriptwarning, 'grade');
214         }
215     }
217     /**
218      * Perform minimal validation on the settings form
219      * @param array $data
220      * @param array $files
221      */
222     public function validation($data, $files) {
223         $errors = parent::validation($data, $files);
225         if ($data['allowsubmissionsfromdate'] && $data['duedate']) {
226             if ($data['allowsubmissionsfromdate'] > $data['duedate']) {
227                 $errors['duedate'] = get_string('duedatevalidation', 'assign');
228             }
229         }
230         if ($data['duedate'] && $data['cutoffdate']) {
231             if ($data['duedate'] > $data['cutoffdate']) {
232                 $errors['cutoffdate'] = get_string('cutoffdatevalidation', 'assign');
233             }
234         }
235         if ($data['allowsubmissionsfromdate'] && $data['cutoffdate']) {
236             if ($data['allowsubmissionsfromdate'] > $data['cutoffdate']) {
237                 $errors['cutoffdate'] = get_string('cutoffdatefromdatevalidation', 'assign');
238             }
239         }
241         return $errors;
242     }
244     /**
245      * Any data processing needed before the form is displayed
246      * (needed to set up draft areas for editor and filemanager elements)
247      * @param array $defaultvalues
248      */
249     public function data_preprocessing(&$defaultvalues) {
250         global $DB;
252         $ctx = null;
253         if ($this->current && $this->current->coursemodule) {
254             $cm = get_coursemodule_from_instance('assign', $this->current->id, 0, false, MUST_EXIST);
255             $ctx = context_module::instance($cm->id);
256         }
257         $assignment = new assign($ctx, null, null);
258         if ($this->current && $this->current->course) {
259             if (!$ctx) {
260                 $ctx = context_course::instance($this->current->course);
261             }
262             $course = $DB->get_record('course', array('id'=>$this->current->course), '*', MUST_EXIST);
263             $assignment->set_course($course);
264         }
265         $assignment->plugin_data_preprocessing($defaultvalues);
266     }
268     public function add_completion_rules() {
269         $mform =& $this->_form;
271         $mform->addElement('checkbox', 'completionsubmit', '', get_string('completionsubmit', 'assign'));
272         return array('completionsubmit');
273     }
275     public function completion_rule_enabled($data) {
276         return !empty($data['completionsubmit']);
277     }