MDL-37337 Assignment: Restructure assignment settings screen to make it "wieldy"
[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');
58         $this->add_intro_editor(true, get_string('description', 'assign'));
60         $ctx = null;
61         if ($this->current && $this->current->coursemodule) {
62             $cm = get_coursemodule_from_instance('assign', $this->current->id, 0, false, MUST_EXIST);
63             $ctx = context_module::instance($cm->id);
64         }
65         $assignment = new assign($ctx, null, null);
66         if ($this->current && $this->current->course) {
67             if (!$ctx) {
68                 $ctx = context_course::instance($this->current->course);
69             }
70             $course = $DB->get_record('course', array('id'=>$this->current->course), '*', MUST_EXIST);
71             $assignment->set_course($course);
72         }
74         $config = get_config('assign');
76         $assignment->add_all_plugin_settings($mform);
78         $mform->addElement('header', 'availability', get_string('availability', 'assign'));
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');
84         $mform->setDefault('allowsubmissionsfromdate', time());
86         $name = get_string('duedate', 'assign');
87         $mform->addElement('date_time_selector', 'duedate', $name, array('optional'=>true));
88         $mform->addHelpButton('duedate', 'duedate', 'assign');
89         $mform->setDefault('duedate', time()+7*24*3600);
91         $name = get_string('cutoffdate', 'assign');
92         $mform->addElement('date_time_selector', 'cutoffdate', $name, array('optional'=>true));
93         $mform->addHelpButton('cutoffdate', 'cutoffdate', 'assign');
94         $mform->setDefault('cutoffdate', time()+7*24*3600);
96         $name = get_string('alwaysshowdescription', 'assign');
97         $mform->addElement('checkbox', 'alwaysshowdescription', $name);
98         $mform->addHelpButton('alwaysshowdescription', 'alwaysshowdescription', 'assign');
99         $mform->setDefault('alwaysshowdescription', 1);
101         $mform->addElement('header', 'submissionsettings', get_string('submissionsettings', 'assign'));
103         $name = get_string('submissiondrafts', 'assign');
104         $mform->addElement('selectyesno', 'submissiondrafts', $name);
105         $mform->addHelpButton('submissiondrafts', 'submissiondrafts', 'assign');
106         $mform->setDefault('submissiondrafts', 0);
108         if (empty($config->submissionstatement)) {
109             $mform->addElement('hidden', 'requiresubmissionstatement', 0);
110         } else if (empty($config->requiresubmissionstatement)) {
111             $name = get_string('requiresubmissionstatement', 'assign');
112             $mform->addElement('selectyesno', 'requiresubmissionstatement', $name);
113             $mform->setDefault('requiresubmissionstatement', 0);
114             $mform->addHelpButton('requiresubmissionstatement',
115                                   'requiresubmissionstatementassignment',
116                                   'assign');
117         } else {
118             $mform->addElement('hidden', 'requiresubmissionstatement', 1);
119         }
121         $mform->addElement('header', 'groupsubmissionsettings', get_string('groupsubmissionsettings', 'assign'));
123         $name = get_string('teamsubmission', 'assign');
124         $mform->addElement('selectyesno', 'teamsubmission', $name);
125         $mform->addHelpButton('teamsubmission', 'teamsubmission', 'assign');
126         $mform->setDefault('teamsubmission', 0);
128         $name = get_string('requireallteammemberssubmit', 'assign');
129         $mform->addElement('selectyesno', 'requireallteammemberssubmit', $name);
130         $mform->addHelpButton('requireallteammemberssubmit', 'requireallteammemberssubmit', 'assign');
131         $mform->setDefault('requireallteammemberssubmit', 0);
132         $mform->disabledIf('requireallteammemberssubmit', 'teamsubmission', 'eq', 0);
133         $mform->disabledIf('requireallteammemberssubmit', 'submissiondrafts', 'eq', 0);
135         $groupings = groups_get_all_groupings($assignment->get_course()->id);
136         $options = array();
137         $options[0] = get_string('none');
138         foreach ($groupings as $grouping) {
139             $options[$grouping->id] = $grouping->name;
140         }
142         $name = get_string('teamsubmissiongroupingid', 'assign');
143         $mform->addElement('select', 'teamsubmissiongroupingid', $name, $options);
144         $mform->addHelpButton('teamsubmissiongroupingid', 'teamsubmissiongroupingid', 'assign');
145         $mform->setDefault('teamsubmissiongroupingid', 0);
146         $mform->disabledIf('teamsubmissiongroupingid', 'teamsubmission', 'eq', 0);
148         $mform->addElement('header', 'notifications', get_string('notifications', 'assign'));
150         $name = get_string('sendnotifications', 'assign');
151         $mform->addElement('selectyesno', 'sendnotifications', $name);
152         $mform->addHelpButton('sendnotifications', 'sendnotifications', 'assign');
153         $mform->setDefault('sendnotifications', 1);
155         $name = get_string('sendlatenotifications', 'assign');
156         $mform->addElement('selectyesno', 'sendlatenotifications', $name);
157         $mform->addHelpButton('sendlatenotifications', 'sendlatenotifications', 'assign');
158         $mform->setDefault('sendlatenotifications', 1);
159         $mform->disabledIf('sendlatenotifications', 'sendnotifications', 'eq', 1);
161         // Plagiarism enabling form.
162         if (!empty($CFG->enableplagiarism)) {
163             require_once($CFG->libdir . '/plagiarismlib.php');
164             plagiarism_get_form_elements_module($mform, $ctx->get_course_context(), 'mod_assign');
165         }
167         $this->standard_grading_coursemodule_elements();
168         $name = get_string('blindmarking', 'assign');
169         $mform->addElement('selectyesno', 'blindmarking', $name);
170         $mform->addHelpButton('blindmarking', 'blindmarking', 'assign');
171         $mform->setDefault('blindmarking', 0);
172         if ($assignment->has_submissions_or_grades() ) {
173             $mform->freeze('blindmarking');
174         }
176         $this->standard_coursemodule_elements();
178         $this->add_action_buttons();
180         // Add warning popup/noscript tag, if grades are changed by user.
181         $hasgrade = false;
182         if (!empty($this->_instance)) {
183             $hasgrade = $DB->record_exists_select('assign_grades',
184                                                   'assignment = ? AND grade <> -1',
185                                                   array($this->_instance));
186         }
188         if ($mform->elementExists('grade') && $hasgrade) {
189             $module = array(
190                 'name' => 'mod_assign',
191                 'fullpath' => '/mod/assign/module.js',
192                 'requires' => array('node', 'event'),
193                 'strings' => array(array('changegradewarning', 'mod_assign'))
194                 );
195             $PAGE->requires->js_init_call('M.mod_assign.init_grade_change', null, false, $module);
197             // Add noscript tag in case.
198             $noscriptwarning = $mform->createElement('static',
199                                                      'warning',
200                                                      null,
201                                                      html_writer::tag('noscript',
202                                                      get_string('changegradewarning', 'mod_assign')));
203             $mform->insertElementBefore($noscriptwarning, 'grade');
204         }
205     }
207     /**
208      * Perform minimal validation on the settings form
209      * @param array $data
210      * @param array $files
211      */
212     public function validation($data, $files) {
213         $errors = parent::validation($data, $files);
215         if ($data['allowsubmissionsfromdate'] && $data['duedate']) {
216             if ($data['allowsubmissionsfromdate'] > $data['duedate']) {
217                 $errors['duedate'] = get_string('duedatevalidation', 'assign');
218             }
219         }
220         if ($data['duedate'] && $data['cutoffdate']) {
221             if ($data['duedate'] > $data['cutoffdate']) {
222                 $errors['cutoffdate'] = get_string('cutoffdatevalidation', 'assign');
223             }
224         }
225         if ($data['allowsubmissionsfromdate'] && $data['cutoffdate']) {
226             if ($data['allowsubmissionsfromdate'] > $data['cutoffdate']) {
227                 $errors['cutoffdate'] = get_string('cutoffdatefromdatevalidation', 'assign');
228             }
229         }
231         return $errors;
232     }
234     /**
235      * Any data processing needed before the form is displayed
236      * (needed to set up draft areas for editor and filemanager elements)
237      * @param array $defaultvalues
238      */
239     public function data_preprocessing(&$defaultvalues) {
240         global $DB;
242         $ctx = null;
243         if ($this->current && $this->current->coursemodule) {
244             $cm = get_coursemodule_from_instance('assign', $this->current->id, 0, false, MUST_EXIST);
245             $ctx = context_module::instance($cm->id);
246         }
247         $assignment = new assign($ctx, null, null);
248         if ($this->current && $this->current->course) {
249             if (!$ctx) {
250                 $ctx = context_course::instance($this->current->course);
251             }
252             $course = $DB->get_record('course', array('id'=>$this->current->course), '*', MUST_EXIST);
253             $assignment->set_course($course);
254         }
255         $assignment->plugin_data_preprocessing($defaultvalues);
256     }
258     public function add_completion_rules() {
259         $mform =& $this->_form;
261         $mform->addElement('checkbox', 'completionsubmit', '', get_string('completionsubmit', 'assign'));
262         return array('completionsubmit');
263     }
265     public function completion_rule_enabled($data) {
266         return !empty($data['completionsubmit']);
267     }