Merge branch 'MDL-56486-master-workshopfiletypes' of git://github.com/mudrd8mz/moodle
[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;
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         $options = array();
133         for ($i = 7; $i >= 0; $i--) {
134             $options[$i] = $i;
135         }
136         $label = get_string('nattachments', 'workshop');
137         $mform->addElement('select', 'nattachments', $label, $options);
138         $mform->setDefault('nattachments', 1);
140         $label = get_string('allowedfiletypesforsubmission', 'workshop');
141         $mform->addElement('filetypes', 'submissionfiletypes', $label);
142         $mform->addHelpButton('submissionfiletypes', 'allowedfiletypesforsubmission', 'workshop');
143         $mform->disabledIf('submissionfiletypes', 'nattachments', 'eq', 0);
145         $options = get_max_upload_sizes($CFG->maxbytes, $this->course->maxbytes, 0, $workshopconfig->maxbytes);
146         $mform->addElement('select', 'maxbytes', get_string('maxbytes', 'workshop'), $options);
147         $mform->setDefault('maxbytes', $workshopconfig->maxbytes);
148         $mform->disabledIf('maxbytes', 'nattachments', 'eq', 0);
150         $label = get_string('latesubmissions', 'workshop');
151         $text = get_string('latesubmissions_desc', 'workshop');
152         $mform->addElement('checkbox', 'latesubmissions', $label, $text);
153         $mform->addHelpButton('latesubmissions', 'latesubmissions', 'workshop');
155         // Assessment settings --------------------------------------------------------
156         $mform->addElement('header', 'assessmentsettings', get_string('assessmentsettings', 'workshop'));
158         $label = get_string('instructreviewers', 'workshop');
159         $mform->addElement('editor', 'instructreviewerseditor', $label, null,
160                             workshop::instruction_editors_options($this->context));
162         $label = get_string('useselfassessment', 'workshop');
163         $text = get_string('useselfassessment_desc', 'workshop');
164         $mform->addElement('checkbox', 'useselfassessment', $label, $text);
165         $mform->addHelpButton('useselfassessment', 'useselfassessment', 'workshop');
167         // Feedback -------------------------------------------------------------------
168         $mform->addElement('header', 'feedbacksettings', get_string('feedbacksettings', 'workshop'));
170         $mform->addElement('select', 'overallfeedbackmode', get_string('overallfeedbackmode', 'mod_workshop'), array(
171             0 => get_string('overallfeedbackmode_0', 'mod_workshop'),
172             1 => get_string('overallfeedbackmode_1', 'mod_workshop'),
173             2 => get_string('overallfeedbackmode_2', 'mod_workshop')));
174         $mform->addHelpButton('overallfeedbackmode', 'overallfeedbackmode', 'mod_workshop');
175         $mform->setDefault('overallfeedbackmode', 1);
177         $options = array();
178         for ($i = 7; $i >= 0; $i--) {
179             $options[$i] = $i;
180         }
181         $mform->addElement('select', 'overallfeedbackfiles', get_string('overallfeedbackfiles', 'workshop'), $options);
182         $mform->setDefault('overallfeedbackfiles', 0);
183         $mform->disabledIf('overallfeedbackfiles', 'overallfeedbackmode', 'eq', 0);
185         $label = get_string('allowedfiletypesforoverallfeedback', 'workshop');
186         $mform->addElement('filetypes', 'overallfeedbackfiletypes', $label);
187         $mform->addHelpButton('overallfeedbackfiletypes', 'allowedfiletypesforoverallfeedback', 'workshop');
188         $mform->disabledIf('overallfeedbackfiletypes', 'overallfeedbackfiles', 'eq', 0);
190         $options = get_max_upload_sizes($CFG->maxbytes, $this->course->maxbytes);
191         $mform->addElement('select', 'overallfeedbackmaxbytes', get_string('overallfeedbackmaxbytes', 'workshop'), $options);
192         $mform->setDefault('overallfeedbackmaxbytes', $workshopconfig->maxbytes);
193         $mform->disabledIf('overallfeedbackmaxbytes', 'overallfeedbackmode', 'eq', 0);
194         $mform->disabledIf('overallfeedbackmaxbytes', 'overallfeedbackfiles', 'eq', 0);
196         $label = get_string('conclusion', 'workshop');
197         $mform->addElement('editor', 'conclusioneditor', $label, null,
198                             workshop::instruction_editors_options($this->context));
199         $mform->addHelpButton('conclusioneditor', 'conclusion', 'workshop');
201         // Example submissions --------------------------------------------------------
202         $mform->addElement('header', 'examplesubmissionssettings', get_string('examplesubmissions', 'workshop'));
204         $label = get_string('useexamples', 'workshop');
205         $text = get_string('useexamples_desc', 'workshop');
206         $mform->addElement('checkbox', 'useexamples', $label, $text);
207         $mform->addHelpButton('useexamples', 'useexamples', 'workshop');
209         $label = get_string('examplesmode', 'workshop');
210         $options = workshop::available_example_modes_list();
211         $mform->addElement('select', 'examplesmode', $label, $options);
212         $mform->setDefault('examplesmode', $workshopconfig->examplesmode);
213         $mform->disabledIf('examplesmode', 'useexamples');
215         // Availability ---------------------------------------------------------------
216         $mform->addElement('header', 'accesscontrol', get_string('availability', 'core'));
218         $label = get_string('submissionstart', 'workshop');
219         $mform->addElement('date_time_selector', 'submissionstart', $label, array('optional' => true));
221         $label = get_string('submissionend', 'workshop');
222         $mform->addElement('date_time_selector', 'submissionend', $label, array('optional' => true));
224         $label = get_string('submissionendswitch', 'mod_workshop');
225         $mform->addElement('checkbox', 'phaseswitchassessment', $label);
226         $mform->disabledIf('phaseswitchassessment', 'submissionend[enabled]');
227         $mform->addHelpButton('phaseswitchassessment', 'submissionendswitch', 'mod_workshop');
229         $label = get_string('assessmentstart', 'workshop');
230         $mform->addElement('date_time_selector', 'assessmentstart', $label, array('optional' => true));
232         $label = get_string('assessmentend', 'workshop');
233         $mform->addElement('date_time_selector', 'assessmentend', $label, array('optional' => true));
235         $coursecontext = context_course::instance($this->course->id);
236         plagiarism_get_form_elements_module($mform, $coursecontext, 'mod_workshop');
238         // Common module settings, Restrict availability, Activity completion etc. ----
239         $features = array('groups' => true, 'groupings' => true,
240                 'outcomes' => true, 'gradecat' => false, 'idnumber' => false);
242         $this->standard_coursemodule_elements();
244         // Standard buttons, common to all modules ------------------------------------
245         $this->add_action_buttons();
246     }
248     /**
249      * Prepares the form before data are set
250      *
251      * Additional wysiwyg editor are prepared here, the introeditor is prepared automatically by core.
252      * Grade items are set here because the core modedit supports single grade item only.
253      *
254      * @param array $data to be set
255      * @return void
256      */
257     public function data_preprocessing(&$data) {
258         if ($this->current->instance) {
259             // editing an existing workshop - let us prepare the added editor elements (intro done automatically)
260             $draftitemid = file_get_submitted_draft_itemid('instructauthors');
261             $data['instructauthorseditor']['text'] = file_prepare_draft_area($draftitemid, $this->context->id,
262                                 'mod_workshop', 'instructauthors', 0,
263                                 workshop::instruction_editors_options($this->context),
264                                 $data['instructauthors']);
265             $data['instructauthorseditor']['format'] = $data['instructauthorsformat'];
266             $data['instructauthorseditor']['itemid'] = $draftitemid;
268             $draftitemid = file_get_submitted_draft_itemid('instructreviewers');
269             $data['instructreviewerseditor']['text'] = file_prepare_draft_area($draftitemid, $this->context->id,
270                                 'mod_workshop', 'instructreviewers', 0,
271                                 workshop::instruction_editors_options($this->context),
272                                 $data['instructreviewers']);
273             $data['instructreviewerseditor']['format'] = $data['instructreviewersformat'];
274             $data['instructreviewerseditor']['itemid'] = $draftitemid;
276             $draftitemid = file_get_submitted_draft_itemid('conclusion');
277             $data['conclusioneditor']['text'] = file_prepare_draft_area($draftitemid, $this->context->id,
278                                 'mod_workshop', 'conclusion', 0,
279                                 workshop::instruction_editors_options($this->context),
280                                 $data['conclusion']);
281             $data['conclusioneditor']['format'] = $data['conclusionformat'];
282             $data['conclusioneditor']['itemid'] = $draftitemid;
283         } else {
284             // adding a new workshop instance
285             $draftitemid = file_get_submitted_draft_itemid('instructauthors');
286             file_prepare_draft_area($draftitemid, null, 'mod_workshop', 'instructauthors', 0);    // no context yet, itemid not used
287             $data['instructauthorseditor'] = array('text' => '', 'format' => editors_get_preferred_format(), 'itemid' => $draftitemid);
289             $draftitemid = file_get_submitted_draft_itemid('instructreviewers');
290             file_prepare_draft_area($draftitemid, null, 'mod_workshop', 'instructreviewers', 0);    // no context yet, itemid not used
291             $data['instructreviewerseditor'] = array('text' => '', 'format' => editors_get_preferred_format(), 'itemid' => $draftitemid);
293             $draftitemid = file_get_submitted_draft_itemid('conclusion');
294             file_prepare_draft_area($draftitemid, null, 'mod_workshop', 'conclusion', 0);    // no context yet, itemid not used
295             $data['conclusioneditor'] = array('text' => '', 'format' => editors_get_preferred_format(), 'itemid' => $draftitemid);
296         }
297     }
299     /**
300      * Set the grade item categories when editing an instance
301      */
302     public function definition_after_data() {
304         $mform =& $this->_form;
306         if ($id = $mform->getElementValue('update')) {
307             $instance   = $mform->getElementValue('instance');
309             $gradeitems = grade_item::fetch_all(array(
310                 'itemtype'      => 'mod',
311                 'itemmodule'    => 'workshop',
312                 'iteminstance'  => $instance,
313                 'courseid'      => $this->course->id));
315             if (!empty($gradeitems)) {
316                 foreach ($gradeitems as $gradeitem) {
317                     // here comes really crappy way how to set the value of the fields
318                     // gradecategory and gradinggradecategory - grrr QuickForms
319                     $decimalpoints = $gradeitem->get_decimals();
320                     if ($gradeitem->itemnumber == 0) {
321                         $submissiongradepass = $mform->getElement('submissiongradepass');
322                         $submissiongradepass->setValue(format_float($gradeitem->gradepass, $decimalpoints));
323                         $group = $mform->getElement('submissiongradegroup');
324                         $elements = $group->getElements();
325                         foreach ($elements as $element) {
326                             if ($element->getName() == 'gradecategory') {
327                                 $element->setValue($gradeitem->categoryid);
328                             }
329                         }
330                     } else if ($gradeitem->itemnumber == 1) {
331                         $gradinggradepass = $mform->getElement('gradinggradepass');
332                         $gradinggradepass->setValue(format_float($gradeitem->gradepass, $decimalpoints));
333                         $group = $mform->getElement('gradinggradegroup');
334                         $elements = $group->getElements();
335                         foreach ($elements as $element) {
336                             if ($element->getName() == 'gradinggradecategory') {
337                                 $element->setValue($gradeitem->categoryid);
338                             }
339                         }
340                     }
341                 }
342             }
343         }
345         parent::definition_after_data();
346     }
348     /**
349      * Validates the form input
350      *
351      * @param array $data submitted data
352      * @param array $files submitted files
353      * @return array eventual errors indexed by the field name
354      */
355     public function validation($data, $files) {
356         $errors = parent::validation($data, $files);
358         // check the phases borders are valid
359         if ($data['submissionstart'] > 0 and $data['submissionend'] > 0 and $data['submissionstart'] >= $data['submissionend']) {
360             $errors['submissionend'] = get_string('submissionendbeforestart', 'mod_workshop');
361         }
362         if ($data['assessmentstart'] > 0 and $data['assessmentend'] > 0 and $data['assessmentstart'] >= $data['assessmentend']) {
363             $errors['assessmentend'] = get_string('assessmentendbeforestart', 'mod_workshop');
364         }
366         // check the phases do not overlap
367         if (max($data['submissionstart'], $data['submissionend']) > 0 and max($data['assessmentstart'], $data['assessmentend']) > 0) {
368             $phasesubmissionend = max($data['submissionstart'], $data['submissionend']);
369             $phaseassessmentstart = min($data['assessmentstart'], $data['assessmentend']);
370             if ($phaseassessmentstart == 0) {
371                 $phaseassessmentstart = max($data['assessmentstart'], $data['assessmentend']);
372             }
373             if ($phasesubmissionend > 0 and $phaseassessmentstart > 0 and $phaseassessmentstart < $phasesubmissionend) {
374                 foreach (array('submissionend', 'submissionstart', 'assessmentstart', 'assessmentend') as $f) {
375                     if ($data[$f] > 0) {
376                         $errors[$f] = get_string('phasesoverlap', 'mod_workshop');
377                         break;
378                     }
379                 }
380             }
381         }
383         // Check that the submission grade pass is a valid number.
384         if (!empty($data['submissiongradepass'])) {
385             $submissiongradefloat = unformat_float($data['submissiongradepass'], true);
386             if ($submissiongradefloat === false) {
387                 $errors['submissiongradepass'] = get_string('err_numeric', 'form');
388             } else {
389                 if ($submissiongradefloat > $data['grade']) {
390                     $errors['submissiongradepass'] = get_string('gradepassgreaterthangrade', 'grades', $data['grade']);
391                 }
392             }
393         }
395         // Check that the grade pass is a valid number.
396         if (!empty($data['gradinggradepass'])) {
397             $gradepassfloat = unformat_float($data['gradinggradepass'], true);
398             if ($gradepassfloat === false) {
399                 $errors['gradinggradepass'] = get_string('err_numeric', 'form');
400             } else {
401                 if ($gradepassfloat > $data['gradinggrade']) {
402                     $errors['gradinggradepass'] = get_string('gradepassgreaterthangrade', 'grades', $data['gradinggrade']);
403                 }
404             }
405         }
407         return $errors;
408     }