d622435467f13c6a1330a3278ac4359475aba6d5
[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);
150         $mform->setDefault('teamsubmissiongroupingid', 0);
152         $mform->addElement('header', 'notifications', get_string('notifications', 'assign'));
154         $name = get_string('sendnotifications', 'assign');
155         $mform->addElement('selectyesno', 'sendnotifications', $name);
156         $mform->addHelpButton('sendnotifications', 'sendnotifications', 'assign');
158         $name = get_string('sendlatenotifications', 'assign');
159         $mform->addElement('selectyesno', 'sendlatenotifications', $name);
160         $mform->addHelpButton('sendlatenotifications', 'sendlatenotifications', 'assign');
161         $mform->disabledIf('sendlatenotifications', 'sendnotifications', 'eq', 1);
163         // Plagiarism enabling form.
164         if (!empty($CFG->enableplagiarism)) {
165             require_once($CFG->libdir . '/plagiarismlib.php');
166             plagiarism_get_form_elements_module($mform, $ctx->get_course_context(), 'mod_assign');
167         }
169         $this->standard_grading_coursemodule_elements();
170         $name = get_string('blindmarking', 'assign');
171         $mform->addElement('selectyesno', 'blindmarking', $name);
172         $mform->addHelpButton('blindmarking', 'blindmarking', 'assign');
173         if ($assignment->has_submissions_or_grades() ) {
174             $mform->freeze('blindmarking');
175         }
177         $name = get_string('markingworkflow', 'assign');
178         $mform->addElement('selectyesno', 'markingworkflow', $name);
179         $mform->addHelpButton('markingworkflow', 'markingworkflow', 'assign');
181         $name = get_string('markingallocation', 'assign');
182         $mform->addElement('selectyesno', 'markingallocation', $name);
183         $mform->addHelpButton('markingallocation', 'markingallocation', 'assign');
184         $mform->disabledIf('markingallocation', 'markingworkflow', 'eq', 0);
186         $this->standard_coursemodule_elements();
187         $this->apply_admin_defaults();
189         $this->add_action_buttons();
191         // Add warning popup/noscript tag, if grades are changed by user.
192         $hasgrade = false;
193         if (!empty($this->_instance)) {
194             $hasgrade = $DB->record_exists_select('assign_grades',
195                                                   'assignment = ? AND grade <> -1',
196                                                   array($this->_instance));
197         }
199         if ($mform->elementExists('grade') && $hasgrade) {
200             $module = array(
201                 'name' => 'mod_assign',
202                 'fullpath' => '/mod/assign/module.js',
203                 'requires' => array('node', 'event'),
204                 'strings' => array(array('changegradewarning', 'mod_assign'))
205                 );
206             $PAGE->requires->js_init_call('M.mod_assign.init_grade_change', null, false, $module);
208             // Add noscript tag in case.
209             $noscriptwarning = $mform->createElement('static',
210                                                      'warning',
211                                                      null,
212                                                      html_writer::tag('noscript',
213                                                      get_string('changegradewarning', 'mod_assign')));
214             $mform->insertElementBefore($noscriptwarning, 'grade');
215         }
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         }
242         return $errors;
243     }
245     /**
246      * Any data processing needed before the form is displayed
247      * (needed to set up draft areas for editor and filemanager elements)
248      * @param array $defaultvalues
249      */
250     public function data_preprocessing(&$defaultvalues) {
251         global $DB;
253         $ctx = null;
254         if ($this->current && $this->current->coursemodule) {
255             $cm = get_coursemodule_from_instance('assign', $this->current->id, 0, false, MUST_EXIST);
256             $ctx = context_module::instance($cm->id);
257         }
258         $assignment = new assign($ctx, null, null);
259         if ($this->current && $this->current->course) {
260             if (!$ctx) {
261                 $ctx = context_course::instance($this->current->course);
262             }
263             $course = $DB->get_record('course', array('id'=>$this->current->course), '*', MUST_EXIST);
264             $assignment->set_course($course);
265         }
266         $assignment->plugin_data_preprocessing($defaultvalues);
267     }
269     public function add_completion_rules() {
270         $mform =& $this->_form;
272         $mform->addElement('checkbox', 'completionsubmit', '', get_string('completionsubmit', 'assign'));
273         return array('completionsubmit');
274     }
276     public function completion_rule_enabled($data) {
277         return !empty($data['completionsubmit']);
278     }