MDL-37602 Add new fields into the workshop table to hold the feature settings
[moodle.git] / mod / workshop / mod_form.php
CommitLineData
4eab2e7f 1<?php
53fad4b9
DM
2
3// This file is part of Moodle - http://moodle.org/
4//
4eab2e7f
DM
5// Moodle is free software: you can redistribute it and/or modify
6// it under the terms of the GNU General Public License as published by
7// the Free Software Foundation, either version 3 of the License, or
8// (at your option) any later version.
9//
10// Moodle is distributed in the hope that it will be useful,
11// but WITHOUT ANY WARRANTY; without even the implied warranty of
12// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13// GNU General Public License for more details.
53fad4b9 14//
4eab2e7f
DM
15// You should have received a copy of the GNU General Public License
16// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
53fad4b9 17
4eab2e7f
DM
18/**
19 * The main workshop configuration form
20 *
21 * The UI mockup has been proposed in MDL-18688
53fad4b9 22 * It uses the standard core Moodle formslib. For more info about them, please
728ebac7 23 * visit: http://docs.moodle.org/dev/lib/formslib.php
4eab2e7f 24 *
65601f04
DM
25 * @package mod
26 * @subpackage workshop
27 * @copyright 2009 David Mudrak <david.mudrak@gmail.com>
28 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
4eab2e7f
DM
29 */
30
6867e05d
DM
31defined('MOODLE_INTERNAL') || die();
32
6516b9e9
DM
33require_once($CFG->dirroot . '/course/moodleform_mod.php');
34require_once(dirname(__FILE__) . '/locallib.php');
35require_once($CFG->libdir . '/filelib.php');
4eab2e7f 36
a74cadfa
DM
37/**
38 * Module settings form for Workshop instances
39 */
4eab2e7f
DM
40class mod_workshop_mod_form extends moodleform_mod {
41
a74cadfa
DM
42 /** @var object the course this instance is part of */
43 protected $course = null;
44
45 /**
46 * Constructor
47 */
48 public function __construct($current, $section, $cm, $course) {
49 $this->course = $course;
50 parent::__construct($current, $section, $cm, $course);
51 }
52
6516b9e9
DM
53 /**
54 * Defines the workshop instance configuration form
55 *
56 * @return void
57 */
a74cadfa
DM
58 public function definition() {
59 global $CFG;
4eab2e7f 60
4eab2e7f 61 $workshopconfig = get_config('workshop');
74e28b26 62 $mform = $this->_form;
4eab2e7f 63
6516b9e9 64 // General --------------------------------------------------------------------
4eab2e7f
DM
65 $mform->addElement('header', 'general', get_string('general', 'form'));
66
6516b9e9 67 // Workshop name
4eab2e7f
DM
68 $label = get_string('workshopname', 'workshop');
69 $mform->addElement('text', 'name', $label, array('size'=>'64'));
70 if (!empty($CFG->formatstringstriptags)) {
71 $mform->setType('name', PARAM_TEXT);
72 } else {
b8ea3041 73 $mform->setType('name', PARAM_CLEANHTML);
4eab2e7f
DM
74 }
75 $mform->addRule('name', null, 'required', null, 'client');
76 $mform->addRule('name', get_string('maximumchars', '', 255), 'maxlength', 255, 'client');
4eab2e7f 77
6516b9e9 78 // Introduction
4eab2e7f
DM
79 $this->add_intro_editor(false, get_string('introduction', 'workshop'));
80
6516b9e9 81 // Workshop features ----------------------------------------------------------
4eab2e7f
DM
82 $mform->addElement('header', 'workshopfeatures', get_string('workshopfeatures', 'workshop'));
83
84 $label = get_string('useexamples', 'workshop');
e2a81c66 85 $text = get_string('useexamples_desc', 'workshop');
0d0b7d99 86 $mform->addElement('checkbox', 'useexamples', $label, $text);
e2a81c66 87 $mform->addHelpButton('useexamples', 'useexamples', 'workshop');
4eab2e7f
DM
88
89 $label = get_string('usepeerassessment', 'workshop');
e2a81c66 90 $text = get_string('usepeerassessment_desc', 'workshop');
0d0b7d99 91 $mform->addElement('checkbox', 'usepeerassessment', $label, $text);
e2a81c66 92 $mform->addHelpButton('usepeerassessment', 'usepeerassessment', 'workshop');
4eab2e7f
DM
93
94 $label = get_string('useselfassessment', 'workshop');
e2a81c66 95 $text = get_string('useselfassessment_desc', 'workshop');
0d0b7d99 96 $mform->addElement('checkbox', 'useselfassessment', $label, $text);
e2a81c66 97 $mform->addHelpButton('useselfassessment', 'useselfassessment', 'workshop');
4eab2e7f 98
6516b9e9 99 // Grading settings -----------------------------------------------------------
4eab2e7f
DM
100 $mform->addElement('header', 'gradingsettings', get_string('gradingsettings', 'workshop'));
101
f6e8b318 102 $grades = workshop::available_maxgrades_list();
a74cadfa 103 $gradecategories = grade_get_categories_menu($this->course->id);
4eab2e7f 104
b4857acb 105 $label = get_string('submissiongrade', 'workshop');
a74cadfa
DM
106 $mform->addGroup(array(
107 $mform->createElement('select', 'grade', '', $grades),
108 $mform->createElement('select', 'gradecategory', '', $gradecategories),
109 ), 'submissiongradegroup', $label, ' ', false);
4eab2e7f 110 $mform->setDefault('grade', $workshopconfig->grade);
a74cadfa 111 $mform->addHelpButton('submissiongradegroup', 'submissiongrade', 'workshop');
4eab2e7f 112
b4857acb 113 $label = get_string('gradinggrade', 'workshop');
a74cadfa
DM
114 $mform->addGroup(array(
115 $mform->createElement('select', 'gradinggrade', '', $grades),
116 $mform->createElement('select', 'gradinggradecategory', '', $gradecategories),
117 ), 'gradinggradegroup', $label, ' ', false);
4eab2e7f 118 $mform->setDefault('gradinggrade', $workshopconfig->gradinggrade);
a74cadfa 119 $mform->addHelpButton('gradinggradegroup', 'gradinggrade', 'workshop');
4eab2e7f
DM
120
121 $label = get_string('strategy', 'workshop');
f6e8b318 122 $mform->addElement('select', 'strategy', $label, workshop::available_strategies_list());
4eab2e7f 123 $mform->setDefault('strategy', $workshopconfig->strategy);
e2a81c66 124 $mform->addHelpButton('strategy', 'strategy', 'workshop');
4eab2e7f 125
b4857acb
DM
126 $options = array();
127 for ($i=5; $i>=0; $i--) {
128 $options[$i] = $i;
129 }
130 $label = get_string('gradedecimals', 'workshop');
131 $mform->addElement('select', 'gradedecimals', $label, $options);
b4857acb
DM
132 $mform->setAdvanced('gradedecimals');
133 $mform->setDefault('gradedecimals', $workshopconfig->gradedecimals);
134
6516b9e9 135 // Submission settings --------------------------------------------------------
4eab2e7f
DM
136 $mform->addElement('header', 'submissionsettings', get_string('submissionsettings', 'workshop'));
137
6516b9e9
DM
138 $label = get_string('instructauthors', 'workshop');
139 $mform->addElement('editor', 'instructauthorseditor', $label, null,
140 workshop::instruction_editors_options($this->context));
141
4eab2e7f
DM
142 $options = array();
143 for ($i=7; $i>=0; $i--) {
144 $options[$i] = $i;
145 }
146 $label = get_string('nattachments', 'workshop');
147 $mform->addElement('select', 'nattachments', $label, $options);
148 $mform->setDefault('nattachments', 1);
6405b254 149
a74cadfa 150 $options = get_max_upload_sizes($CFG->maxbytes, $this->course->maxbytes);
e2a81c66 151 $mform->addElement('select', 'maxbytes', get_string('maxbytes', 'workshop'), $options);
4eab2e7f 152 $mform->setDefault('maxbytes', $workshopconfig->maxbytes);
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
DM
158 $mform->setAdvanced('latesubmissions');
159
6516b9e9 160 // Assessment settings --------------------------------------------------------
4eab2e7f
DM
161 $mform->addElement('header', 'assessmentsettings', get_string('assessmentsettings', 'workshop'));
162
15d12b54
DM
163 $label = get_string('instructreviewers', 'workshop');
164 $mform->addElement('editor', 'instructreviewerseditor', $label, null,
165 workshop::instruction_editors_options($this->context));
6516b9e9 166
4eab2e7f 167 $label = get_string('examplesmode', 'workshop');
f6e8b318 168 $options = workshop::available_example_modes_list();
4eab2e7f
DM
169 $mform->addElement('select', 'examplesmode', $label, $options);
170 $mform->setDefault('examplesmode', $workshopconfig->examplesmode);
f6e8b318 171 $mform->disabledIf('examplesmode', 'useexamples');
4eab2e7f
DM
172 $mform->setAdvanced('examplesmode');
173
5a49f825
DM
174 // Miscellaneous settings
175 $mform->addElement('header', 'miscellaneoussettings', get_string('miscellaneoussettings', 'workshop'));
176
177 $label = get_string('conclusion', 'workshop');
178 $mform->addElement('editor', 'conclusioneditor', $label, null,
179 workshop::instruction_editors_options($this->context));
180 $mform->addHelpButton('conclusioneditor', 'conclusion', 'workshop');
181
6516b9e9 182 // Access control -------------------------------------------------------------
4eab2e7f
DM
183 $mform->addElement('header', 'accesscontrol', get_string('accesscontrol', 'workshop'));
184
185 $label = get_string('submissionstart', 'workshop');
de2f199b 186 $mform->addElement('date_time_selector', 'submissionstart', $label, array('optional' => true));
4eab2e7f
DM
187 $mform->setAdvanced('submissionstart');
188
189 $label = get_string('submissionend', 'workshop');
de2f199b 190 $mform->addElement('date_time_selector', 'submissionend', $label, array('optional' => true));
4eab2e7f
DM
191 $mform->setAdvanced('submissionend');
192
3ff08057
DM
193 $label = get_string('submissionendswitch', 'mod_workshop');
194 $mform->addElement('checkbox', 'phaseswitchassessment', $label);
195 $mform->setAdvanced('phaseswitchassessment');
196 $mform->disabledIf('phaseswitchassessment', 'submissionend[enabled]');
197 $mform->addHelpButton('phaseswitchassessment', 'submissionendswitch', 'mod_workshop');
198
4eab2e7f 199 $label = get_string('assessmentstart', 'workshop');
de2f199b 200 $mform->addElement('date_time_selector', 'assessmentstart', $label, array('optional' => true));
4eab2e7f
DM
201 $mform->setAdvanced('assessmentstart');
202
203 $label = get_string('assessmentend', 'workshop');
de2f199b 204 $mform->addElement('date_time_selector', 'assessmentend', $label, array('optional' => true));
4eab2e7f
DM
205 $mform->setAdvanced('assessmentend');
206
67ba9007 207 $coursecontext = context_course::instance($this->course->id);
67fbfe8b 208 plagiarism_get_form_elements_module($mform, $coursecontext, 'mod_workshop');
67ba9007 209
5924db72 210 // Common module settings, Restrict availability, Activity completion etc. ----
53fad4b9
DM
211 $features = array('groups'=>true, 'groupings'=>true, 'groupmembersonly'=>true,
212 'outcomes'=>true, 'gradecat'=>false, 'idnumber'=>false);
213
4eab2e7f
DM
214 $this->standard_coursemodule_elements();
215
6516b9e9 216 // Standard buttons, common to all modules ------------------------------------
4eab2e7f 217 $this->add_action_buttons();
6516b9e9 218 }
4eab2e7f 219
6516b9e9
DM
220 /**
221 * Prepares the form before data are set
222 *
a74cadfa
DM
223 * Additional wysiwyg editor are prepared here, the introeditor is prepared automatically by core.
224 * Grade items are set here because the core modedit supports single grade item only.
6516b9e9
DM
225 *
226 * @param array $data to be set
227 * @return void
228 */
a74cadfa 229 public function data_preprocessing(&$data) {
6516b9e9
DM
230 if ($this->current->instance) {
231 // editing an existing workshop - let us prepare the added editor elements (intro done automatically)
232 $draftitemid = file_get_submitted_draft_itemid('instructauthors');
233 $data['instructauthorseditor']['text'] = file_prepare_draft_area($draftitemid, $this->context->id,
6a0d85aa 234 'mod_workshop', 'instructauthors', 0,
6516b9e9
DM
235 workshop::instruction_editors_options($this->context),
236 $data['instructauthors']);
237 $data['instructauthorseditor']['format'] = $data['instructauthorsformat'];
238 $data['instructauthorseditor']['itemid'] = $draftitemid;
15d12b54
DM
239
240 $draftitemid = file_get_submitted_draft_itemid('instructreviewers');
241 $data['instructreviewerseditor']['text'] = file_prepare_draft_area($draftitemid, $this->context->id,
6a0d85aa 242 'mod_workshop', 'instructreviewers', 0,
15d12b54
DM
243 workshop::instruction_editors_options($this->context),
244 $data['instructreviewers']);
245 $data['instructreviewerseditor']['format'] = $data['instructreviewersformat'];
246 $data['instructreviewerseditor']['itemid'] = $draftitemid;
5a49f825
DM
247
248 $draftitemid = file_get_submitted_draft_itemid('conclusion');
249 $data['conclusioneditor']['text'] = file_prepare_draft_area($draftitemid, $this->context->id,
250 'mod_workshop', 'conclusion', 0,
251 workshop::instruction_editors_options($this->context),
252 $data['conclusion']);
253 $data['conclusioneditor']['format'] = $data['conclusionformat'];
254 $data['conclusioneditor']['itemid'] = $draftitemid;
6516b9e9
DM
255 } else {
256 // adding a new workshop instance
257 $draftitemid = file_get_submitted_draft_itemid('instructauthors');
6a0d85aa 258 file_prepare_draft_area($draftitemid, null, 'mod_workshop', 'instructauthors', 0); // no context yet, itemid not used
884482fb 259 $data['instructauthorseditor'] = array('text' => '', 'format' => editors_get_preferred_format(), 'itemid' => $draftitemid);
15d12b54
DM
260
261 $draftitemid = file_get_submitted_draft_itemid('instructreviewers');
6a0d85aa 262 file_prepare_draft_area($draftitemid, null, 'mod_workshop', 'instructreviewers', 0); // no context yet, itemid not used
884482fb 263 $data['instructreviewerseditor'] = array('text' => '', 'format' => editors_get_preferred_format(), 'itemid' => $draftitemid);
5a49f825
DM
264
265 $draftitemid = file_get_submitted_draft_itemid('conclusion');
266 file_prepare_draft_area($draftitemid, null, 'mod_workshop', 'conclusion', 0); // no context yet, itemid not used
267 $data['conclusioneditor'] = array('text' => '', 'format' => editors_get_preferred_format(), 'itemid' => $draftitemid);
6516b9e9 268 }
4eab2e7f 269 }
a74cadfa
DM
270
271 /**
272 * Set the grade item categories when editing an instance
273 */
274 public function definition_after_data() {
275
276 $mform =& $this->_form;
277
278 if ($id = $mform->getElementValue('update')) {
279 $instance = $mform->getElementValue('instance');
280
281 $gradeitems = grade_item::fetch_all(array(
282 'itemtype' => 'mod',
283 'itemmodule' => 'workshop',
284 'iteminstance' => $instance,
285 'courseid' => $this->course->id));
286
287 if (!empty($gradeitems)) {
288 foreach ($gradeitems as $gradeitem) {
289 // here comes really crappy way how to set the value of the fields
290 // gradecategory and gradinggradecategory - grrr QuickForms
291 if ($gradeitem->itemnumber == 0) {
292 $group = $mform->getElement('submissiongradegroup');
293 $elements = $group->getElements();
294 foreach ($elements as $element) {
295 if ($element->getName() == 'gradecategory') {
296 $element->setValue($gradeitem->categoryid);
297 }
298 }
299 } else if ($gradeitem->itemnumber == 1) {
300 $group = $mform->getElement('gradinggradegroup');
301 $elements = $group->getElements();
302 foreach ($elements as $element) {
303 if ($element->getName() == 'gradinggradecategory') {
304 $element->setValue($gradeitem->categoryid);
305 }
306 }
307 }
308 }
309 }
310 }
311
312 parent::definition_after_data();
313 }
df558765
DM
314
315 /**
316 * Validates the form input
317 *
318 * @param array $data submitted data
319 * @param array $files submitted files
320 * @return array eventual errors indexed by the field name
321 */
2fc04a4b 322 public function validation($data, $files) {
df558765
DM
323 $errors = array();
324
325 // check the phases borders are valid
326 if ($data['submissionstart'] > 0 and $data['submissionend'] > 0 and $data['submissionstart'] >= $data['submissionend']) {
327 $errors['submissionend'] = get_string('submissionendbeforestart', 'mod_workshop');
328 }
329 if ($data['assessmentstart'] > 0 and $data['assessmentend'] > 0 and $data['assessmentstart'] >= $data['assessmentend']) {
330 $errors['assessmentend'] = get_string('assessmentendbeforestart', 'mod_workshop');
331 }
332
333 // check the phases do not overlap
334 if (max($data['submissionstart'], $data['submissionend']) > 0 and max($data['assessmentstart'], $data['assessmentend']) > 0) {
335 $phasesubmissionend = max($data['submissionstart'], $data['submissionend']);
336 $phaseassessmentstart = min($data['assessmentstart'], $data['assessmentend']);
337 if ($phaseassessmentstart == 0) {
338 $phaseassessmentstart = max($data['assessmentstart'], $data['assessmentend']);
339 }
340 if ($phasesubmissionend > 0 and $phaseassessmentstart > 0 and $phaseassessmentstart < $phasesubmissionend) {
341 foreach (array('submissionend', 'submissionstart', 'assessmentstart', 'assessmentend') as $f) {
342 if ($data[$f] > 0) {
343 $errors[$f] = get_string('phasesoverlap', 'mod_workshop');
344 break;
345 }
346 }
347 }
348 }
349
350 return $errors;
351 }
4eab2e7f 352}