weekly back-to-dev release 3.2dev
[moodle.git] / mod / workshop / mod_form.php
CommitLineData
4eab2e7f 1<?php
53fad4b9
DM
2// This file is part of Moodle - http://moodle.org/
3//
4eab2e7f
DM
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.
53fad4b9 13//
4eab2e7f
DM
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/>.
53fad4b9 16
4eab2e7f
DM
17/**
18 * The main workshop configuration form
19 *
20 * The UI mockup has been proposed in MDL-18688
53fad4b9 21 * It uses the standard core Moodle formslib. For more info about them, please
728ebac7 22 * visit: http://docs.moodle.org/dev/lib/formslib.php
4eab2e7f 23 *
5cdcfcb9 24 * @package mod_workshop
65601f04
DM
25 * @copyright 2009 David Mudrak <david.mudrak@gmail.com>
26 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
4eab2e7f
DM
27 */
28
6867e05d
DM
29defined('MOODLE_INTERNAL') || die();
30
6516b9e9
DM
31require_once($CFG->dirroot . '/course/moodleform_mod.php');
32require_once(dirname(__FILE__) . '/locallib.php');
33require_once($CFG->libdir . '/filelib.php');
4eab2e7f 34
a74cadfa
DM
35/**
36 * Module settings form for Workshop instances
37 */
4eab2e7f
DM
38class mod_workshop_mod_form extends moodleform_mod {
39
a74cadfa
DM
40 /** @var object the course this instance is part of */
41 protected $course = null;
42
43 /**
44 * Constructor
45 */
46 public function __construct($current, $section, $cm, $course) {
47 $this->course = $course;
48 parent::__construct($current, $section, $cm, $course);
49 }
50
6516b9e9
DM
51 /**
52 * Defines the workshop instance configuration form
53 *
54 * @return void
55 */
a74cadfa
DM
56 public function definition() {
57 global $CFG;
4eab2e7f 58
4eab2e7f 59 $workshopconfig = get_config('workshop');
74e28b26 60 $mform = $this->_form;
4eab2e7f 61
6516b9e9 62 // General --------------------------------------------------------------------
4eab2e7f
DM
63 $mform->addElement('header', 'general', get_string('general', 'form'));
64
6516b9e9 65 // Workshop name
4eab2e7f 66 $label = get_string('workshopname', 'workshop');
1a282212 67 $mform->addElement('text', 'name', $label, array('size' => '64'));
4eab2e7f
DM
68 if (!empty($CFG->formatstringstriptags)) {
69 $mform->setType('name', PARAM_TEXT);
70 } else {
b8ea3041 71 $mform->setType('name', PARAM_CLEANHTML);
4eab2e7f
DM
72 }
73 $mform->addRule('name', null, 'required', null, 'client');
74 $mform->addRule('name', get_string('maximumchars', '', 255), 'maxlength', 255, 'client');
4eab2e7f 75
6516b9e9 76 // Introduction
6398ff53 77 $this->standard_intro_elements(get_string('introduction', 'workshop'));
4eab2e7f 78
6516b9e9 79 // Grading settings -----------------------------------------------------------
4eab2e7f 80 $mform->addElement('header', 'gradingsettings', get_string('gradingsettings', 'workshop'));
9e31e23d
DM
81 $mform->setExpanded('gradingsettings');
82
83 $label = get_string('strategy', 'workshop');
84 $mform->addElement('select', 'strategy', $label, workshop::available_strategies_list());
85 $mform->setDefault('strategy', $workshopconfig->strategy);
86 $mform->addHelpButton('strategy', 'strategy', 'workshop');
4eab2e7f 87
f6e8b318 88 $grades = workshop::available_maxgrades_list();
a74cadfa 89 $gradecategories = grade_get_categories_menu($this->course->id);
4eab2e7f 90
b4857acb 91 $label = get_string('submissiongrade', 'workshop');
a74cadfa
DM
92 $mform->addGroup(array(
93 $mform->createElement('select', 'grade', '', $grades),
94 $mform->createElement('select', 'gradecategory', '', $gradecategories),
95 ), 'submissiongradegroup', $label, ' ', false);
4eab2e7f 96 $mform->setDefault('grade', $workshopconfig->grade);
a74cadfa 97 $mform->addHelpButton('submissiongradegroup', 'submissiongrade', 'workshop');
4eab2e7f 98
8164fad4
GF
99 $mform->addElement('text', 'submissiongradepass', get_string('gradetopasssubmission', 'workshop'));
100 $mform->addHelpButton('submissiongradepass', 'gradepass', 'grades');
101 $mform->setDefault('submissiongradepass', '');
102 $mform->setType('submissiongradepass', PARAM_FLOAT);
103 $mform->addRule('submissiongradepass', null, 'numeric', null, 'client');
104
b4857acb 105 $label = get_string('gradinggrade', 'workshop');
a74cadfa
DM
106 $mform->addGroup(array(
107 $mform->createElement('select', 'gradinggrade', '', $grades),
108 $mform->createElement('select', 'gradinggradecategory', '', $gradecategories),
109 ), 'gradinggradegroup', $label, ' ', false);
4eab2e7f 110 $mform->setDefault('gradinggrade', $workshopconfig->gradinggrade);
a74cadfa 111 $mform->addHelpButton('gradinggradegroup', 'gradinggrade', 'workshop');
4eab2e7f 112
8164fad4
GF
113 $mform->addElement('text', 'gradinggradepass', get_string('gradetopassgrading', 'workshop'));
114 $mform->addHelpButton('gradinggradepass', 'gradepass', 'grades');
115 $mform->setDefault('gradinggradepass', '');
116 $mform->setType('gradinggradepass', PARAM_FLOAT);
117 $mform->addRule('gradinggradepass', null, 'numeric', null, 'client');
118
b4857acb 119 $options = array();
1a282212 120 for ($i = 5; $i >= 0; $i--) {
b4857acb
DM
121 $options[$i] = $i;
122 }
123 $label = get_string('gradedecimals', 'workshop');
124 $mform->addElement('select', 'gradedecimals', $label, $options);
b4857acb
DM
125 $mform->setDefault('gradedecimals', $workshopconfig->gradedecimals);
126
6516b9e9 127 // Submission settings --------------------------------------------------------
4eab2e7f
DM
128 $mform->addElement('header', 'submissionsettings', get_string('submissionsettings', 'workshop'));
129
6516b9e9
DM
130 $label = get_string('instructauthors', 'workshop');
131 $mform->addElement('editor', 'instructauthorseditor', $label, null,
132 workshop::instruction_editors_options($this->context));
133
4eab2e7f 134 $options = array();
1a282212 135 for ($i = 7; $i >= 0; $i--) {
4eab2e7f
DM
136 $options[$i] = $i;
137 }
138 $label = get_string('nattachments', 'workshop');
139 $mform->addElement('select', 'nattachments', $label, $options);
140 $mform->setDefault('nattachments', 1);
6405b254 141
1a282212
K
142 $label = get_string('allowedfiletypesforsubmission', 'workshop');
143 $mform->addElement('text', 'submissionfiletypes', $label, array('maxlength' => 255, 'size' => 64));
144 $mform->addHelpButton('submissionfiletypes', 'allowedfiletypesforsubmission', 'workshop');
145 $mform->setType('submissionfiletypes', PARAM_TEXT);
146 $mform->addRule('submissionfiletypes', get_string('maximumchars', '', 255), 'maxlength', 255, 'client');
147 $mform->disabledIf('submissionfiletypes', 'nattachments', 'eq', 0);
148
367b977d 149 $options = get_max_upload_sizes($CFG->maxbytes, $this->course->maxbytes, 0, $workshopconfig->maxbytes);
e2a81c66 150 $mform->addElement('select', 'maxbytes', get_string('maxbytes', 'workshop'), $options);
4eab2e7f 151 $mform->setDefault('maxbytes', $workshopconfig->maxbytes);
1a282212 152 $mform->disabledIf('maxbytes', 'nattachments', 'eq', 0);
4eab2e7f 153
f6e8b318 154 $label = get_string('latesubmissions', 'workshop');
e2a81c66 155 $text = get_string('latesubmissions_desc', 'workshop');
0d0b7d99 156 $mform->addElement('checkbox', 'latesubmissions', $label, $text);
e2a81c66 157 $mform->addHelpButton('latesubmissions', 'latesubmissions', 'workshop');
f6e8b318 158
6516b9e9 159 // Assessment settings --------------------------------------------------------
4eab2e7f
DM
160 $mform->addElement('header', 'assessmentsettings', get_string('assessmentsettings', 'workshop'));
161
15d12b54
DM
162 $label = get_string('instructreviewers', 'workshop');
163 $mform->addElement('editor', 'instructreviewerseditor', $label, null,
164 workshop::instruction_editors_options($this->context));
6516b9e9 165
9e31e23d
DM
166 $label = get_string('useselfassessment', 'workshop');
167 $text = get_string('useselfassessment_desc', 'workshop');
168 $mform->addElement('checkbox', 'useselfassessment', $label, $text);
169 $mform->addHelpButton('useselfassessment', 'useselfassessment', 'workshop');
170
171 // Feedback -------------------------------------------------------------------
172 $mform->addElement('header', 'feedbacksettings', get_string('feedbacksettings', 'workshop'));
173
1c6b484d
DM
174 $mform->addElement('select', 'overallfeedbackmode', get_string('overallfeedbackmode', 'mod_workshop'), array(
175 0 => get_string('overallfeedbackmode_0', 'mod_workshop'),
176 1 => get_string('overallfeedbackmode_1', 'mod_workshop'),
177 2 => get_string('overallfeedbackmode_2', 'mod_workshop')));
178 $mform->addHelpButton('overallfeedbackmode', 'overallfeedbackmode', 'mod_workshop');
179 $mform->setDefault('overallfeedbackmode', 1);
1c6b484d
DM
180
181 $options = array();
182 for ($i = 7; $i >= 0; $i--) {
183 $options[$i] = $i;
184 }
185 $mform->addElement('select', 'overallfeedbackfiles', get_string('overallfeedbackfiles', 'workshop'), $options);
186 $mform->setDefault('overallfeedbackfiles', 0);
1c6b484d
DM
187 $mform->disabledIf('overallfeedbackfiles', 'overallfeedbackmode', 'eq', 0);
188
1a282212
K
189 $label = get_string('allowedfiletypesforoverallfeedback', 'workshop');
190 $mform->addElement('text', 'overallfeedbackfiletypes', $label, array('maxlength' => 255, 'size' => 64));
191 $mform->addHelpButton('overallfeedbackfiletypes', 'allowedfiletypesforoverallfeedback', 'workshop');
192 $mform->setType('overallfeedbackfiletypes', PARAM_TEXT);
193 $mform->addRule('overallfeedbackfiletypes', get_string('maximumchars', '', 255), 'maxlength', 255, 'client');
194 $mform->disabledIf('overallfeedbackfiletypes', 'overallfeedbackfiles', 'eq', 0);
195
1c6b484d
DM
196 $options = get_max_upload_sizes($CFG->maxbytes, $this->course->maxbytes);
197 $mform->addElement('select', 'overallfeedbackmaxbytes', get_string('overallfeedbackmaxbytes', 'workshop'), $options);
1c6b484d
DM
198 $mform->setDefault('overallfeedbackmaxbytes', $workshopconfig->maxbytes);
199 $mform->disabledIf('overallfeedbackmaxbytes', 'overallfeedbackmode', 'eq', 0);
200 $mform->disabledIf('overallfeedbackmaxbytes', 'overallfeedbackfiles', 'eq', 0);
201
9e31e23d
DM
202 $label = get_string('conclusion', 'workshop');
203 $mform->addElement('editor', 'conclusioneditor', $label, null,
204 workshop::instruction_editors_options($this->context));
205 $mform->addHelpButton('conclusioneditor', 'conclusion', 'workshop');
206
207 // Example submissions --------------------------------------------------------
208 $mform->addElement('header', 'examplesubmissionssettings', get_string('examplesubmissions', 'workshop'));
209
210 $label = get_string('useexamples', 'workshop');
211 $text = get_string('useexamples_desc', 'workshop');
212 $mform->addElement('checkbox', 'useexamples', $label, $text);
213 $mform->addHelpButton('useexamples', 'useexamples', 'workshop');
214
4eab2e7f 215 $label = get_string('examplesmode', 'workshop');
f6e8b318 216 $options = workshop::available_example_modes_list();
4eab2e7f
DM
217 $mform->addElement('select', 'examplesmode', $label, $options);
218 $mform->setDefault('examplesmode', $workshopconfig->examplesmode);
f6e8b318 219 $mform->disabledIf('examplesmode', 'useexamples');
5a49f825 220
9e31e23d
DM
221 // Availability ---------------------------------------------------------------
222 $mform->addElement('header', 'accesscontrol', get_string('availability', 'core'));
4eab2e7f
DM
223
224 $label = get_string('submissionstart', 'workshop');
de2f199b 225 $mform->addElement('date_time_selector', 'submissionstart', $label, array('optional' => true));
4eab2e7f
DM
226
227 $label = get_string('submissionend', 'workshop');
de2f199b 228 $mform->addElement('date_time_selector', 'submissionend', $label, array('optional' => true));
4eab2e7f 229
3ff08057
DM
230 $label = get_string('submissionendswitch', 'mod_workshop');
231 $mform->addElement('checkbox', 'phaseswitchassessment', $label);
3ff08057
DM
232 $mform->disabledIf('phaseswitchassessment', 'submissionend[enabled]');
233 $mform->addHelpButton('phaseswitchassessment', 'submissionendswitch', 'mod_workshop');
234
4eab2e7f 235 $label = get_string('assessmentstart', 'workshop');
de2f199b 236 $mform->addElement('date_time_selector', 'assessmentstart', $label, array('optional' => true));
4eab2e7f
DM
237
238 $label = get_string('assessmentend', 'workshop');
de2f199b 239 $mform->addElement('date_time_selector', 'assessmentend', $label, array('optional' => true));
4eab2e7f 240
67ba9007 241 $coursecontext = context_course::instance($this->course->id);
67fbfe8b 242 plagiarism_get_form_elements_module($mform, $coursecontext, 'mod_workshop');
67ba9007 243
5924db72 244 // Common module settings, Restrict availability, Activity completion etc. ----
45ab2d9a 245 $features = array('groups' => true, 'groupings' => true,
1a282212 246 'outcomes' => true, 'gradecat' => false, 'idnumber' => false);
53fad4b9 247
4eab2e7f
DM
248 $this->standard_coursemodule_elements();
249
6516b9e9 250 // Standard buttons, common to all modules ------------------------------------
4eab2e7f 251 $this->add_action_buttons();
6516b9e9 252 }
4eab2e7f 253
6516b9e9
DM
254 /**
255 * Prepares the form before data are set
256 *
a74cadfa
DM
257 * Additional wysiwyg editor are prepared here, the introeditor is prepared automatically by core.
258 * Grade items are set here because the core modedit supports single grade item only.
6516b9e9
DM
259 *
260 * @param array $data to be set
261 * @return void
262 */
a74cadfa 263 public function data_preprocessing(&$data) {
6516b9e9
DM
264 if ($this->current->instance) {
265 // editing an existing workshop - let us prepare the added editor elements (intro done automatically)
266 $draftitemid = file_get_submitted_draft_itemid('instructauthors');
267 $data['instructauthorseditor']['text'] = file_prepare_draft_area($draftitemid, $this->context->id,
6a0d85aa 268 'mod_workshop', 'instructauthors', 0,
6516b9e9
DM
269 workshop::instruction_editors_options($this->context),
270 $data['instructauthors']);
271 $data['instructauthorseditor']['format'] = $data['instructauthorsformat'];
272 $data['instructauthorseditor']['itemid'] = $draftitemid;
15d12b54
DM
273
274 $draftitemid = file_get_submitted_draft_itemid('instructreviewers');
275 $data['instructreviewerseditor']['text'] = file_prepare_draft_area($draftitemid, $this->context->id,
6a0d85aa 276 'mod_workshop', 'instructreviewers', 0,
15d12b54
DM
277 workshop::instruction_editors_options($this->context),
278 $data['instructreviewers']);
279 $data['instructreviewerseditor']['format'] = $data['instructreviewersformat'];
280 $data['instructreviewerseditor']['itemid'] = $draftitemid;
5a49f825
DM
281
282 $draftitemid = file_get_submitted_draft_itemid('conclusion');
283 $data['conclusioneditor']['text'] = file_prepare_draft_area($draftitemid, $this->context->id,
284 'mod_workshop', 'conclusion', 0,
285 workshop::instruction_editors_options($this->context),
286 $data['conclusion']);
287 $data['conclusioneditor']['format'] = $data['conclusionformat'];
288 $data['conclusioneditor']['itemid'] = $draftitemid;
6516b9e9
DM
289 } else {
290 // adding a new workshop instance
291 $draftitemid = file_get_submitted_draft_itemid('instructauthors');
6a0d85aa 292 file_prepare_draft_area($draftitemid, null, 'mod_workshop', 'instructauthors', 0); // no context yet, itemid not used
884482fb 293 $data['instructauthorseditor'] = array('text' => '', 'format' => editors_get_preferred_format(), 'itemid' => $draftitemid);
15d12b54
DM
294
295 $draftitemid = file_get_submitted_draft_itemid('instructreviewers');
6a0d85aa 296 file_prepare_draft_area($draftitemid, null, 'mod_workshop', 'instructreviewers', 0); // no context yet, itemid not used
884482fb 297 $data['instructreviewerseditor'] = array('text' => '', 'format' => editors_get_preferred_format(), 'itemid' => $draftitemid);
5a49f825
DM
298
299 $draftitemid = file_get_submitted_draft_itemid('conclusion');
300 file_prepare_draft_area($draftitemid, null, 'mod_workshop', 'conclusion', 0); // no context yet, itemid not used
301 $data['conclusioneditor'] = array('text' => '', 'format' => editors_get_preferred_format(), 'itemid' => $draftitemid);
6516b9e9 302 }
4eab2e7f 303 }
a74cadfa
DM
304
305 /**
306 * Set the grade item categories when editing an instance
307 */
308 public function definition_after_data() {
309
310 $mform =& $this->_form;
311
312 if ($id = $mform->getElementValue('update')) {
313 $instance = $mform->getElementValue('instance');
314
315 $gradeitems = grade_item::fetch_all(array(
316 'itemtype' => 'mod',
317 'itemmodule' => 'workshop',
318 'iteminstance' => $instance,
319 'courseid' => $this->course->id));
320
321 if (!empty($gradeitems)) {
322 foreach ($gradeitems as $gradeitem) {
323 // here comes really crappy way how to set the value of the fields
324 // gradecategory and gradinggradecategory - grrr QuickForms
8164fad4 325 $decimalpoints = $gradeitem->get_decimals();
a74cadfa 326 if ($gradeitem->itemnumber == 0) {
8164fad4
GF
327 $submissiongradepass = $mform->getElement('submissiongradepass');
328 $submissiongradepass->setValue(format_float($gradeitem->gradepass, $decimalpoints));
a74cadfa
DM
329 $group = $mform->getElement('submissiongradegroup');
330 $elements = $group->getElements();
331 foreach ($elements as $element) {
332 if ($element->getName() == 'gradecategory') {
333 $element->setValue($gradeitem->categoryid);
334 }
335 }
336 } else if ($gradeitem->itemnumber == 1) {
8164fad4
GF
337 $gradinggradepass = $mform->getElement('gradinggradepass');
338 $gradinggradepass->setValue(format_float($gradeitem->gradepass, $decimalpoints));
a74cadfa
DM
339 $group = $mform->getElement('gradinggradegroup');
340 $elements = $group->getElements();
341 foreach ($elements as $element) {
342 if ($element->getName() == 'gradinggradecategory') {
343 $element->setValue($gradeitem->categoryid);
344 }
345 }
346 }
347 }
348 }
349 }
350
351 parent::definition_after_data();
352 }
df558765
DM
353
354 /**
355 * Validates the form input
356 *
357 * @param array $data submitted data
358 * @param array $files submitted files
359 * @return array eventual errors indexed by the field name
360 */
2fc04a4b 361 public function validation($data, $files) {
90faa6ae 362 $errors = parent::validation($data, $files);
df558765 363
996f7e82
DM
364 // Validate lists of allowed extensions.
365 foreach (array('submissionfiletypes', 'overallfeedbackfiletypes') as $fieldname) {
366 if (isset($data[$fieldname])) {
367 $invalidextensions = workshop::invalid_file_extensions($data[$fieldname], array_keys(core_filetypes::get_types()));
368 if ($invalidextensions) {
369 $errors[$fieldname] = get_string('err_unknownfileextension', 'mod_workshop',
370 workshop::clean_file_extensions($invalidextensions));
371 }
1a282212
K
372 }
373 }
374
df558765
DM
375 // check the phases borders are valid
376 if ($data['submissionstart'] > 0 and $data['submissionend'] > 0 and $data['submissionstart'] >= $data['submissionend']) {
377 $errors['submissionend'] = get_string('submissionendbeforestart', 'mod_workshop');
378 }
379 if ($data['assessmentstart'] > 0 and $data['assessmentend'] > 0 and $data['assessmentstart'] >= $data['assessmentend']) {
380 $errors['assessmentend'] = get_string('assessmentendbeforestart', 'mod_workshop');
381 }
382
383 // check the phases do not overlap
384 if (max($data['submissionstart'], $data['submissionend']) > 0 and max($data['assessmentstart'], $data['assessmentend']) > 0) {
385 $phasesubmissionend = max($data['submissionstart'], $data['submissionend']);
386 $phaseassessmentstart = min($data['assessmentstart'], $data['assessmentend']);
387 if ($phaseassessmentstart == 0) {
388 $phaseassessmentstart = max($data['assessmentstart'], $data['assessmentend']);
389 }
390 if ($phasesubmissionend > 0 and $phaseassessmentstart > 0 and $phaseassessmentstart < $phasesubmissionend) {
391 foreach (array('submissionend', 'submissionstart', 'assessmentstart', 'assessmentend') as $f) {
392 if ($data[$f] > 0) {
393 $errors[$f] = get_string('phasesoverlap', 'mod_workshop');
394 break;
395 }
396 }
397 }
398 }
399
8164fad4 400 if ($data['submissiongradepass'] > $data['grade']) {
c977b350 401 $errors['submissiongradepass'] = get_string('gradepassgreaterthangrade', 'grades', $data['grade']);
8164fad4
GF
402 }
403 if ($data['gradinggradepass'] > $data['gradinggrade']) {
c977b350 404 $errors['gradinggradepass'] = get_string('gradepassgreaterthangrade', 'grades', $data['gradinggrade']);
8164fad4
GF
405 }
406
df558765
DM
407 return $errors;
408 }
4eab2e7f 409}