70bc77b441298979470b2f310b4d08c96bd7b0bc
[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         $mform->addElement('header', 'general', get_string('settings', 'assign'));
78         $name = get_string('allowsubmissionsfromdate', 'assign');
79         $options = array('optional'=>true);
80         $mform->addElement('date_time_selector', 'allowsubmissionsfromdate', $name, $options);
81         $mform->addHelpButton('allowsubmissionsfromdate', 'allowsubmissionsfromdate', 'assign');
82         $mform->setDefault('allowsubmissionsfromdate', time());
84         $name = get_string('duedate', 'assign');
85         $mform->addElement('date_time_selector', 'duedate', $name, array('optional'=>true));
86         $mform->addHelpButton('duedate', 'duedate', 'assign');
87         $mform->setDefault('duedate', time()+7*24*3600);
89         $name = get_string('cutoffdate', 'assign');
90         $mform->addElement('date_time_selector', 'cutoffdate', $name, array('optional'=>true));
91         $mform->addHelpButton('cutoffdate', 'cutoffdate', 'assign');
92         $mform->setDefault('cutoffdate', time()+7*24*3600);
94         $name = get_string('alwaysshowdescription', 'assign');
95         $mform->addElement('selectyesno', 'alwaysshowdescription', $name);
96         $mform->addHelpButton('alwaysshowdescription', 'alwaysshowdescription', 'assign');
97         $mform->setDefault('alwaysshowdescription', 1);
99         $name = get_string('submissiondrafts', 'assign');
100         $mform->addElement('selectyesno', 'submissiondrafts', $name);
101         $mform->addHelpButton('submissiondrafts', 'submissiondrafts', 'assign');
102         $mform->setDefault('submissiondrafts', 0);
104         if (empty($config->submissionstatement)) {
105             $mform->addElement('hidden', 'requiresubmissionstatement', 0);
106         } else if (empty($config->requiresubmissionstatement)) {
107             $name = get_string('requiresubmissionstatement', 'assign');
108             $mform->addElement('selectyesno', 'requiresubmissionstatement', $name);
109             $mform->setDefault('requiresubmissionstatement', 0);
110             $mform->addHelpButton('requiresubmissionstatement',
111                                   'requiresubmissionstatementassignment',
112                                   'assign');
113         } else {
114             $mform->addElement('hidden', 'requiresubmissionstatement', 1);
115         }
117         $name = get_string('sendnotifications', 'assign');
118         $mform->addElement('selectyesno', 'sendnotifications', $name);
119         $mform->addHelpButton('sendnotifications', 'sendnotifications', 'assign');
120         $mform->setDefault('sendnotifications', 1);
122         $name = get_string('sendlatenotifications', 'assign');
123         $mform->addElement('selectyesno', 'sendlatenotifications', $name);
124         $mform->addHelpButton('sendlatenotifications', 'sendlatenotifications', 'assign');
125         $mform->setDefault('sendlatenotifications', 1);
126         $mform->disabledIf('sendlatenotifications', 'sendnotifications', 'eq', 1);
128         $name = get_string('teamsubmission', 'assign');
129         $mform->addElement('selectyesno', 'teamsubmission', $name);
130         $mform->addHelpButton('teamsubmission', 'teamsubmission', 'assign');
131         $mform->setDefault('teamsubmission', 0);
133         $name = get_string('requireallteammemberssubmit', 'assign');
134         $mform->addElement('selectyesno', 'requireallteammemberssubmit', $name);
135         $mform->addHelpButton('requireallteammemberssubmit', 'requireallteammemberssubmit', 'assign');
136         $mform->setDefault('requireallteammemberssubmit', 0);
137         $mform->disabledIf('requireallteammemberssubmit', 'teamsubmission', 'eq', 0);
138         $mform->disabledIf('requireallteammemberssubmit', 'submissiondrafts', 'eq', 0);
140         $groupings = groups_get_all_groupings($assignment->get_course()->id);
141         $options = array();
142         $options[0] = get_string('none');
143         foreach ($groupings as $grouping) {
144             $options[$grouping->id] = $grouping->name;
145         }
147         $name = get_string('teamsubmissiongroupingid', 'assign');
148         $mform->addElement('select', 'teamsubmissiongroupingid', $name, $options);
149         $mform->addHelpButton('teamsubmissiongroupingid', 'teamsubmissiongroupingid', 'assign');
150         $mform->setDefault('teamsubmissiongroupingid', 0);
151         $mform->disabledIf('teamsubmissiongroupingid', 'teamsubmission', 'eq', 0);
153         $name = get_string('blindmarking', 'assign');
154         $mform->addElement('selectyesno', 'blindmarking', $name);
155         $mform->addHelpButton('blindmarking', 'blindmarking', 'assign');
156         $mform->setDefault('blindmarking', 0);
157         if ($assignment->has_submissions_or_grades() ) {
158             $mform->freeze('blindmarking');
159         }
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         $assignment->add_all_plugin_settings($mform);
168         $this->standard_grading_coursemodule_elements();
169         $this->standard_coursemodule_elements();
171         $this->add_action_buttons();
173         // Add warning popup/noscript tag, if grades are changed by user.
174         $hasgrade = false;
175         if (!empty($this->_instance)) {
176             $hasgrade = $DB->record_exists_select('assign_grades',
177                                                   'assignment = ? AND grade <> -1',
178                                                   array($this->_instance));
179         }
180         if ($mform->elementExists('grade') && $hasgrade) {
181             $module = array(
182                 'name' => 'mod_assign',
183                 'fullpath' => '/mod/assign/module.js',
184                 'requires' => array('node', 'event'),
185                 'strings' => array(array('changegradewarning', 'mod_assign'))
186                 );
187             $PAGE->requires->js_init_call('M.mod_assign.init_grade_change', null, false, $module);
189             // Add noscript tag in case.
190             $noscriptwarning = $mform->createElement('static',
191                                                      'warning',
192                                                      null,
193                                                      html_writer::tag('noscript',
194                                                      get_string('changegradewarning', 'mod_assign')));
195             $mform->insertElementBefore($noscriptwarning, 'grade');
196         }
197     }
199     /**
200      * Perform minimal validation on the settings form
201      * @param array $data
202      * @param array $files
203      */
204     public function validation($data, $files) {
205         $errors = parent::validation($data, $files);
207         if ($data['allowsubmissionsfromdate'] && $data['duedate']) {
208             if ($data['allowsubmissionsfromdate'] > $data['duedate']) {
209                 $errors['duedate'] = get_string('duedatevalidation', 'assign');
210             }
211         }
212         if ($data['duedate'] && $data['cutoffdate']) {
213             if ($data['duedate'] > $data['cutoffdate']) {
214                 $errors['cutoffdate'] = get_string('cutoffdatevalidation', 'assign');
215             }
216         }
217         if ($data['allowsubmissionsfromdate'] && $data['cutoffdate']) {
218             if ($data['allowsubmissionsfromdate'] > $data['cutoffdate']) {
219                 $errors['cutoffdate'] = get_string('cutoffdatefromdatevalidation', 'assign');
220             }
221         }
223         return $errors;
224     }
226     /**
227      * Any data processing needed before the form is displayed
228      * (needed to set up draft areas for editor and filemanager elements)
229      * @param array $defaultvalues
230      */
231     public function data_preprocessing(&$defaultvalues) {
232         global $DB;
234         $ctx = null;
235         if ($this->current && $this->current->coursemodule) {
236             $cm = get_coursemodule_from_instance('assign', $this->current->id, 0, false, MUST_EXIST);
237             $ctx = context_module::instance($cm->id);
238         }
239         $assignment = new assign($ctx, null, null);
240         if ($this->current && $this->current->course) {
241             if (!$ctx) {
242                 $ctx = context_course::instance($this->current->course);
243             }
244             $course = $DB->get_record('course', array('id'=>$this->current->course), '*', MUST_EXIST);
245             $assignment->set_course($course);
246         }
247         $assignment->plugin_data_preprocessing($defaultvalues);
248     }
250     public function add_completion_rules() {
251         $mform =& $this->_form;
253         $mform->addElement('checkbox', 'completionsubmit', '', get_string('completionsubmit', 'assign'));
254         return array('completionsubmit');
255     }
257     public function completion_rule_enabled($data) {
258         return !empty($data['completionsubmit']);
259     }