a13766c1d06fb6bdd2d50fc97d212a41bac85422
[moodle.git] / mod / workshop / 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  * The main workshop configuration form
19  *
20  * The UI mockup has been proposed in MDL-18688
21  * It uses the standard core Moodle formslib. For more info about them, please
22  * visit: http://docs.moodle.org/dev/lib/formslib.php
23  *
24  * @package    mod_workshop
25  * @copyright  2009 David Mudrak <david.mudrak@gmail.com>
26  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
27  */
29 defined('MOODLE_INTERNAL') || die();
31 require_once($CFG->dirroot . '/course/moodleform_mod.php');
32 require_once(__DIR__ . '/locallib.php');
33 require_once($CFG->libdir . '/filelib.php');
35 /**
36  * Module settings form for Workshop instances
37  */
38 class mod_workshop_mod_form extends moodleform_mod {
40     /** @var object the course this instance is part of */
41     protected $course = null;
43     /**
44      * Constructor
45      */
46     public function __construct($current, $section, $cm, $course) {
47         $this->course = $course;
48         parent::__construct($current, $section, $cm, $course);
49     }
51     /**
52      * Defines the workshop instance configuration form
53      *
54      * @return void
55      */
56     public function definition() {
57         global $CFG, $PAGE;
59         $workshopconfig = get_config('workshop');
60         $mform = $this->_form;
62         // General --------------------------------------------------------------------
63         $mform->addElement('header', 'general', get_string('general', 'form'));
65         // Workshop name
66         $label = get_string('workshopname', 'workshop');
67         $mform->addElement('text', 'name', $label, array('size' => '64'));
68         if (!empty($CFG->formatstringstriptags)) {
69             $mform->setType('name', PARAM_TEXT);
70         } else {
71             $mform->setType('name', PARAM_CLEANHTML);
72         }
73         $mform->addRule('name', null, 'required', null, 'client');
74         $mform->addRule('name', get_string('maximumchars', '', 255), 'maxlength', 255, 'client');
76         // Introduction
77         $this->standard_intro_elements(get_string('introduction', 'workshop'));
79         // Grading settings -----------------------------------------------------------
80         $mform->addElement('header', 'gradingsettings', get_string('gradingsettings', 'workshop'));
81         $mform->setExpanded('gradingsettings');
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');
88         $grades = workshop::available_maxgrades_list();
89         $gradecategories = grade_get_categories_menu($this->course->id);
91         $label = get_string('submissiongrade', 'workshop');
92         $mform->addGroup(array(
93             $mform->createElement('select', 'grade', '', $grades),
94             $mform->createElement('select', 'gradecategory', '', $gradecategories),
95             ), 'submissiongradegroup', $label, ' ', false);
96         $mform->setDefault('grade', $workshopconfig->grade);
97         $mform->addHelpButton('submissiongradegroup', 'submissiongrade', 'workshop');
99         $mform->addElement('text', 'submissiongradepass', get_string('gradetopasssubmission', 'workshop'));
100         $mform->addHelpButton('submissiongradepass', 'gradepass', 'grades');
101         $mform->setDefault('submissiongradepass', '');
102         $mform->setType('submissiongradepass', PARAM_RAW);
104         $label = get_string('gradinggrade', 'workshop');
105         $mform->addGroup(array(
106             $mform->createElement('select', 'gradinggrade', '', $grades),
107             $mform->createElement('select', 'gradinggradecategory', '', $gradecategories),
108             ), 'gradinggradegroup', $label, ' ', false);
109         $mform->setDefault('gradinggrade', $workshopconfig->gradinggrade);
110         $mform->addHelpButton('gradinggradegroup', 'gradinggrade', 'workshop');
112         $mform->addElement('text', 'gradinggradepass', get_string('gradetopassgrading', 'workshop'));
113         $mform->addHelpButton('gradinggradepass', 'gradepass', 'grades');
114         $mform->setDefault('gradinggradepass', '');
115         $mform->setType('gradinggradepass', PARAM_RAW);
117         $options = array();
118         for ($i = 5; $i >= 0; $i--) {
119             $options[$i] = $i;
120         }
121         $label = get_string('gradedecimals', 'workshop');
122         $mform->addElement('select', 'gradedecimals', $label, $options);
123         $mform->setDefault('gradedecimals', $workshopconfig->gradedecimals);
125         // Submission settings --------------------------------------------------------
126         $mform->addElement('header', 'submissionsettings', get_string('submissionsettings', 'workshop'));
128         $label = get_string('instructauthors', 'workshop');
129         $mform->addElement('editor', 'instructauthorseditor', $label, null,
130                             workshop::instruction_editors_options($this->context));
132         $typeelements = [];
133         foreach (['submissiontypetext', 'submissiontypefile'] as $type) {
134             $available = $type . 'available';
135             $required = $type . 'required';
136             $availablelabel = get_string($available, 'workshop');
137             $requiredlabel = get_string($required, 'workshop');
138             $typeelements[] = $mform->createElement('advcheckbox', $available, '', $availablelabel);
139             $typeelements[] = $mform->createElement('advcheckbox', $required, '', $requiredlabel);
140             $mform->setDefault($available, 1);
141         }
142         // We can't use <br> as the separator as it does not work well in this case with the Boost theme.
143         // Instead, separate both tuples with a full-width empty div.
144         $mform->addGroup($typeelements, 'submissiontypes', get_string('submissiontypes', 'workshop'),
145             array(' ', '<div style="width:100%"></div>'), false);
147         $options = array();
148         for ($i = 7; $i >= 1; $i--) {
149             $options[$i] = $i;
150         }
151         $label = get_string('nattachments', 'workshop');
152         $mform->addElement('select', 'nattachments', $label, $options);
153         $mform->setDefault('nattachments', 1);
154         $mform->hideIf('nattachments', 'submissiontypefileavailable');
156         $label = get_string('allowedfiletypesforsubmission', 'workshop');
157         $mform->addElement('filetypes', 'submissionfiletypes', $label);
158         $mform->addHelpButton('submissionfiletypes', 'allowedfiletypesforsubmission', 'workshop');
159         $mform->hideIf('submissionfiletypes', 'submissiontypefileavailable');
161         $options = get_max_upload_sizes($CFG->maxbytes, $this->course->maxbytes, 0, $workshopconfig->maxbytes);
162         $mform->addElement('select', 'maxbytes', get_string('maxbytes', 'workshop'), $options);
163         $mform->setDefault('maxbytes', $workshopconfig->maxbytes);
164         $mform->hideIf('maxbytes', 'submissiontypefileavailable');
166         $label = get_string('latesubmissions', 'workshop');
167         $text = get_string('latesubmissions_desc', 'workshop');
168         $mform->addElement('checkbox', 'latesubmissions', $label, $text);
169         $mform->addHelpButton('latesubmissions', 'latesubmissions', 'workshop');
171         // Assessment settings --------------------------------------------------------
172         $mform->addElement('header', 'assessmentsettings', get_string('assessmentsettings', 'workshop'));
174         $label = get_string('instructreviewers', 'workshop');
175         $mform->addElement('editor', 'instructreviewerseditor', $label, null,
176                             workshop::instruction_editors_options($this->context));
178         $label = get_string('useselfassessment', 'workshop');
179         $text = get_string('useselfassessment_desc', 'workshop');
180         $mform->addElement('checkbox', 'useselfassessment', $label, $text);
181         $mform->addHelpButton('useselfassessment', 'useselfassessment', 'workshop');
183         // Feedback -------------------------------------------------------------------
184         $mform->addElement('header', 'feedbacksettings', get_string('feedbacksettings', 'workshop'));
186         $mform->addElement('select', 'overallfeedbackmode', get_string('overallfeedbackmode', 'mod_workshop'), array(
187             0 => get_string('overallfeedbackmode_0', 'mod_workshop'),
188             1 => get_string('overallfeedbackmode_1', 'mod_workshop'),
189             2 => get_string('overallfeedbackmode_2', 'mod_workshop')));
190         $mform->addHelpButton('overallfeedbackmode', 'overallfeedbackmode', 'mod_workshop');
191         $mform->setDefault('overallfeedbackmode', 1);
193         $options = array();
194         for ($i = 7; $i >= 0; $i--) {
195             $options[$i] = $i;
196         }
197         $mform->addElement('select', 'overallfeedbackfiles', get_string('overallfeedbackfiles', 'workshop'), $options);
198         $mform->setDefault('overallfeedbackfiles', 0);
199         $mform->hideIf('overallfeedbackfiles', 'overallfeedbackmode', 'eq', 0);
201         $label = get_string('allowedfiletypesforoverallfeedback', 'workshop');
202         $mform->addElement('filetypes', 'overallfeedbackfiletypes', $label);
203         $mform->addHelpButton('overallfeedbackfiletypes', 'allowedfiletypesforoverallfeedback', 'workshop');
204         $mform->hideIf('overallfeedbackfiletypes', 'overallfeedbackfiles', 'eq', 0);
206         $options = get_max_upload_sizes($CFG->maxbytes, $this->course->maxbytes);
207         $mform->addElement('select', 'overallfeedbackmaxbytes', get_string('overallfeedbackmaxbytes', 'workshop'), $options);
208         $mform->setDefault('overallfeedbackmaxbytes', $workshopconfig->maxbytes);
209         $mform->hideIf('overallfeedbackmaxbytes', 'overallfeedbackmode', 'eq', 0);
210         $mform->hideIf('overallfeedbackmaxbytes', 'overallfeedbackfiles', 'eq', 0);
212         $label = get_string('conclusion', 'workshop');
213         $mform->addElement('editor', 'conclusioneditor', $label, null,
214                             workshop::instruction_editors_options($this->context));
215         $mform->addHelpButton('conclusioneditor', 'conclusion', 'workshop');
217         // Example submissions --------------------------------------------------------
218         $mform->addElement('header', 'examplesubmissionssettings', get_string('examplesubmissions', 'workshop'));
220         $label = get_string('useexamples', 'workshop');
221         $text = get_string('useexamples_desc', 'workshop');
222         $mform->addElement('checkbox', 'useexamples', $label, $text);
223         $mform->addHelpButton('useexamples', 'useexamples', 'workshop');
225         $label = get_string('examplesmode', 'workshop');
226         $options = workshop::available_example_modes_list();
227         $mform->addElement('select', 'examplesmode', $label, $options);
228         $mform->setDefault('examplesmode', $workshopconfig->examplesmode);
229         $mform->hideIf('examplesmode', 'useexamples');
231         // Availability ---------------------------------------------------------------
232         $mform->addElement('header', 'accesscontrol', get_string('availability', 'core'));
234         $label = get_string('submissionstart', 'workshop');
235         $mform->addElement('date_time_selector', 'submissionstart', $label, array('optional' => true));
237         $label = get_string('submissionend', 'workshop');
238         $mform->addElement('date_time_selector', 'submissionend', $label, array('optional' => true));
240         $label = get_string('submissionendswitch', 'mod_workshop');
241         $mform->addElement('checkbox', 'phaseswitchassessment', $label);
242         $mform->hideIf('phaseswitchassessment', 'submissionend[enabled]');
243         $mform->addHelpButton('phaseswitchassessment', 'submissionendswitch', 'mod_workshop');
245         $label = get_string('assessmentstart', 'workshop');
246         $mform->addElement('date_time_selector', 'assessmentstart', $label, array('optional' => true));
248         $label = get_string('assessmentend', 'workshop');
249         $mform->addElement('date_time_selector', 'assessmentend', $label, array('optional' => true));
251         $coursecontext = context_course::instance($this->course->id);
252         plagiarism_get_form_elements_module($mform, $coursecontext, 'mod_workshop');
254         // Common module settings, Restrict availability, Activity completion etc. ----
255         $features = array('groups' => true, 'groupings' => true,
256                 'outcomes' => true, 'gradecat' => false, 'idnumber' => false);
258         $this->standard_coursemodule_elements();
260         // Standard buttons, common to all modules ------------------------------------
261         $this->add_action_buttons();
263         $PAGE->requires->js_call_amd('mod_workshop/modform', 'init');
264     }
266     /**
267      * Prepares the form before data are set
268      *
269      * Additional wysiwyg editor are prepared here, the introeditor is prepared automatically by core.
270      * Grade items are set here because the core modedit supports single grade item only.
271      *
272      * @param array $data to be set
273      * @return void
274      */
275     public function data_preprocessing(&$data) {
276         if ($this->current->instance) {
277             // editing an existing workshop - let us prepare the added editor elements (intro done automatically)
278             $draftitemid = file_get_submitted_draft_itemid('instructauthors');
279             $data['instructauthorseditor']['text'] = file_prepare_draft_area($draftitemid, $this->context->id,
280                                 'mod_workshop', 'instructauthors', 0,
281                                 workshop::instruction_editors_options($this->context),
282                                 $data['instructauthors']);
283             $data['instructauthorseditor']['format'] = $data['instructauthorsformat'];
284             $data['instructauthorseditor']['itemid'] = $draftitemid;
286             $draftitemid = file_get_submitted_draft_itemid('instructreviewers');
287             $data['instructreviewerseditor']['text'] = file_prepare_draft_area($draftitemid, $this->context->id,
288                                 'mod_workshop', 'instructreviewers', 0,
289                                 workshop::instruction_editors_options($this->context),
290                                 $data['instructreviewers']);
291             $data['instructreviewerseditor']['format'] = $data['instructreviewersformat'];
292             $data['instructreviewerseditor']['itemid'] = $draftitemid;
294             $draftitemid = file_get_submitted_draft_itemid('conclusion');
295             $data['conclusioneditor']['text'] = file_prepare_draft_area($draftitemid, $this->context->id,
296                                 'mod_workshop', 'conclusion', 0,
297                                 workshop::instruction_editors_options($this->context),
298                                 $data['conclusion']);
299             $data['conclusioneditor']['format'] = $data['conclusionformat'];
300             $data['conclusioneditor']['itemid'] = $draftitemid;
301             // Set submission type checkboxes.
302             foreach (['submissiontypetext', 'submissiontypefile'] as $type) {
303                 $data[$type . 'available'] = 1;
304                 $data[$type . 'required'] = 0;
305                 if ($data[$type] == WORKSHOP_SUBMISSION_TYPE_DISABLED) {
306                     $data[$type . 'available'] = 0;
307                 } else if ($data[$type] == WORKSHOP_SUBMISSION_TYPE_REQUIRED) {
308                     $data[$type . 'required'] = 1;
309                 }
310             }
311         } else {
312             // adding a new workshop instance
313             $draftitemid = file_get_submitted_draft_itemid('instructauthors');
314             file_prepare_draft_area($draftitemid, null, 'mod_workshop', 'instructauthors', 0);    // no context yet, itemid not used
315             $data['instructauthorseditor'] = array('text' => '', 'format' => editors_get_preferred_format(), 'itemid' => $draftitemid);
317             $draftitemid = file_get_submitted_draft_itemid('instructreviewers');
318             file_prepare_draft_area($draftitemid, null, 'mod_workshop', 'instructreviewers', 0);    // no context yet, itemid not used
319             $data['instructreviewerseditor'] = array('text' => '', 'format' => editors_get_preferred_format(), 'itemid' => $draftitemid);
321             $draftitemid = file_get_submitted_draft_itemid('conclusion');
322             file_prepare_draft_area($draftitemid, null, 'mod_workshop', 'conclusion', 0);    // no context yet, itemid not used
323             $data['conclusioneditor'] = array('text' => '', 'format' => editors_get_preferred_format(), 'itemid' => $draftitemid);
324         }
325     }
327     /**
328      * Combine submission type checkboxes into integer values for the database.
329      *
330      * @param stdClass $data The submitted form data.
331      */
332     public function data_postprocessing($data) {
333         parent::data_postprocessing($data);
335         foreach (['text', 'file'] as $type) {
336             $field = 'submissiontype' . $type;
337             $available = $field . 'available';
338             $required = $field . 'required';
339             if ($data->$required) {
340                 $data->$field = WORKSHOP_SUBMISSION_TYPE_REQUIRED;
341             } else if ($data->$available) {
342                 $data->$field = WORKSHOP_SUBMISSION_TYPE_AVAILABLE;
343             } else {
344                 $data->$field = WORKSHOP_SUBMISSION_TYPE_DISABLED;
345             }
346             unset($data->$available);
347             unset($data->$required);
348         }
349     }
351     /**
352      * Set the grade item categories when editing an instance
353      */
354     public function definition_after_data() {
356         $mform =& $this->_form;
358         if ($id = $mform->getElementValue('update')) {
359             $instance   = $mform->getElementValue('instance');
361             $gradeitems = grade_item::fetch_all(array(
362                 'itemtype'      => 'mod',
363                 'itemmodule'    => 'workshop',
364                 'iteminstance'  => $instance,
365                 'courseid'      => $this->course->id));
367             if (!empty($gradeitems)) {
368                 foreach ($gradeitems as $gradeitem) {
369                     // here comes really crappy way how to set the value of the fields
370                     // gradecategory and gradinggradecategory - grrr QuickForms
371                     $decimalpoints = $gradeitem->get_decimals();
372                     if ($gradeitem->itemnumber == 0) {
373                         $mform->setDefault('submissiongradepass', format_float($gradeitem->gradepass, $decimalpoints));
374                         $group = $mform->getElement('submissiongradegroup');
375                         $elements = $group->getElements();
376                         foreach ($elements as $element) {
377                             if ($element->getName() == 'gradecategory') {
378                                 $element->setValue($gradeitem->categoryid);
379                             }
380                         }
381                     } else if ($gradeitem->itemnumber == 1) {
382                         $mform->setDefault('gradinggradepass', format_float($gradeitem->gradepass, $decimalpoints));
383                         $group = $mform->getElement('gradinggradegroup');
384                         $elements = $group->getElements();
385                         foreach ($elements as $element) {
386                             if ($element->getName() == 'gradinggradecategory') {
387                                 $element->setValue($gradeitem->categoryid);
388                             }
389                         }
390                     }
391                 }
392             }
393         }
394         $typevalues = $mform->getElementValue('submissiontypes');
395         foreach (['submissiontypetext', 'submissiontypefile'] as $type) {
396             // Don't leave a disabled "required" checkbox checked.
397             if (!$typevalues[$type . 'available']) {
398                 $mform->setDefault($type . 'required', 0);
399             }
400         }
402         parent::definition_after_data();
403     }
405     /**
406      * Validates the form input
407      *
408      * @param array $data submitted data
409      * @param array $files submitted files
410      * @return array eventual errors indexed by the field name
411      */
412     public function validation($data, $files) {
413         $errors = parent::validation($data, $files);
415         // check the phases borders are valid
416         if ($data['submissionstart'] > 0 and $data['submissionend'] > 0 and $data['submissionstart'] >= $data['submissionend']) {
417             $errors['submissionend'] = get_string('submissionendbeforestart', 'mod_workshop');
418         }
419         if ($data['assessmentstart'] > 0 and $data['assessmentend'] > 0 and $data['assessmentstart'] >= $data['assessmentend']) {
420             $errors['assessmentend'] = get_string('assessmentendbeforestart', 'mod_workshop');
421         }
423         // check the phases do not overlap
424         if (max($data['submissionstart'], $data['submissionend']) > 0 and max($data['assessmentstart'], $data['assessmentend']) > 0) {
425             $phasesubmissionend = max($data['submissionstart'], $data['submissionend']);
426             $phaseassessmentstart = min($data['assessmentstart'], $data['assessmentend']);
427             if ($phaseassessmentstart == 0) {
428                 $phaseassessmentstart = max($data['assessmentstart'], $data['assessmentend']);
429             }
430             if ($phasesubmissionend > 0 and $phaseassessmentstart > 0 and $phaseassessmentstart < $phasesubmissionend) {
431                 foreach (array('submissionend', 'submissionstart', 'assessmentstart', 'assessmentend') as $f) {
432                     if ($data[$f] > 0) {
433                         $errors[$f] = get_string('phasesoverlap', 'mod_workshop');
434                         break;
435                     }
436                 }
437             }
438         }
440         // Check that the submission grade pass is a valid number.
441         if (!empty($data['submissiongradepass'])) {
442             $submissiongradefloat = unformat_float($data['submissiongradepass'], true);
443             if ($submissiongradefloat === false) {
444                 $errors['submissiongradepass'] = get_string('err_numeric', 'form');
445             } else {
446                 if ($submissiongradefloat > $data['grade']) {
447                     $errors['submissiongradepass'] = get_string('gradepassgreaterthangrade', 'grades', $data['grade']);
448                 }
449             }
450         }
452         // Check that the grade pass is a valid number.
453         if (!empty($data['gradinggradepass'])) {
454             $gradepassfloat = unformat_float($data['gradinggradepass'], true);
455             if ($gradepassfloat === false) {
456                 $errors['gradinggradepass'] = get_string('err_numeric', 'form');
457             } else {
458                 if ($gradepassfloat > $data['gradinggrade']) {
459                     $errors['gradinggradepass'] = get_string('gradepassgreaterthangrade', 'grades', $data['gradinggrade']);
460                 }
461             }
462         }
464         if (!$data['submissiontypetextavailable'] && !$data['submissiontypefileavailable']) {
465             // One submission type must be available.
466             $errors['submissiontypes'] = get_string('nosubmissiontype', 'workshop');
467         }
469         return $errors;
470     }