MDL-38603 Deprecate usepeerassessment setting in Workshop
[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 88
4eab2e7f 89 $label = get_string('useselfassessment', 'workshop');
e2a81c66 90 $text = get_string('useselfassessment_desc', 'workshop');
0d0b7d99 91 $mform->addElement('checkbox', 'useselfassessment', $label, $text);
e2a81c66 92 $mform->addHelpButton('useselfassessment', 'useselfassessment', 'workshop');
4eab2e7f 93
6516b9e9 94 // Grading settings -----------------------------------------------------------
4eab2e7f
DM
95 $mform->addElement('header', 'gradingsettings', get_string('gradingsettings', 'workshop'));
96
f6e8b318 97 $grades = workshop::available_maxgrades_list();
a74cadfa 98 $gradecategories = grade_get_categories_menu($this->course->id);
4eab2e7f 99
b4857acb 100 $label = get_string('submissiongrade', 'workshop');
a74cadfa
DM
101 $mform->addGroup(array(
102 $mform->createElement('select', 'grade', '', $grades),
103 $mform->createElement('select', 'gradecategory', '', $gradecategories),
104 ), 'submissiongradegroup', $label, ' ', false);
4eab2e7f 105 $mform->setDefault('grade', $workshopconfig->grade);
a74cadfa 106 $mform->addHelpButton('submissiongradegroup', 'submissiongrade', 'workshop');
4eab2e7f 107
b4857acb 108 $label = get_string('gradinggrade', 'workshop');
a74cadfa
DM
109 $mform->addGroup(array(
110 $mform->createElement('select', 'gradinggrade', '', $grades),
111 $mform->createElement('select', 'gradinggradecategory', '', $gradecategories),
112 ), 'gradinggradegroup', $label, ' ', false);
4eab2e7f 113 $mform->setDefault('gradinggrade', $workshopconfig->gradinggrade);
a74cadfa 114 $mform->addHelpButton('gradinggradegroup', 'gradinggrade', 'workshop');
4eab2e7f
DM
115
116 $label = get_string('strategy', 'workshop');
f6e8b318 117 $mform->addElement('select', 'strategy', $label, workshop::available_strategies_list());
4eab2e7f 118 $mform->setDefault('strategy', $workshopconfig->strategy);
e2a81c66 119 $mform->addHelpButton('strategy', 'strategy', 'workshop');
4eab2e7f 120
b4857acb
DM
121 $options = array();
122 for ($i=5; $i>=0; $i--) {
123 $options[$i] = $i;
124 }
125 $label = get_string('gradedecimals', 'workshop');
126 $mform->addElement('select', 'gradedecimals', $label, $options);
b4857acb
DM
127 $mform->setAdvanced('gradedecimals');
128 $mform->setDefault('gradedecimals', $workshopconfig->gradedecimals);
129
6516b9e9 130 // Submission settings --------------------------------------------------------
4eab2e7f
DM
131 $mform->addElement('header', 'submissionsettings', get_string('submissionsettings', 'workshop'));
132
6516b9e9
DM
133 $label = get_string('instructauthors', 'workshop');
134 $mform->addElement('editor', 'instructauthorseditor', $label, null,
135 workshop::instruction_editors_options($this->context));
136
4eab2e7f
DM
137 $options = array();
138 for ($i=7; $i>=0; $i--) {
139 $options[$i] = $i;
140 }
141 $label = get_string('nattachments', 'workshop');
142 $mform->addElement('select', 'nattachments', $label, $options);
143 $mform->setDefault('nattachments', 1);
6405b254 144
a74cadfa 145 $options = get_max_upload_sizes($CFG->maxbytes, $this->course->maxbytes);
e2a81c66 146 $mform->addElement('select', 'maxbytes', get_string('maxbytes', 'workshop'), $options);
4eab2e7f 147 $mform->setDefault('maxbytes', $workshopconfig->maxbytes);
4eab2e7f 148
f6e8b318 149 $label = get_string('latesubmissions', 'workshop');
e2a81c66 150 $text = get_string('latesubmissions_desc', 'workshop');
0d0b7d99 151 $mform->addElement('checkbox', 'latesubmissions', $label, $text);
e2a81c66 152 $mform->addHelpButton('latesubmissions', 'latesubmissions', 'workshop');
f6e8b318
DM
153 $mform->setAdvanced('latesubmissions');
154
6516b9e9 155 // Assessment settings --------------------------------------------------------
4eab2e7f
DM
156 $mform->addElement('header', 'assessmentsettings', get_string('assessmentsettings', 'workshop'));
157
15d12b54
DM
158 $label = get_string('instructreviewers', 'workshop');
159 $mform->addElement('editor', 'instructreviewerseditor', $label, null,
160 workshop::instruction_editors_options($this->context));
6516b9e9 161
1c6b484d
DM
162 $mform->addElement('select', 'overallfeedbackmode', get_string('overallfeedbackmode', 'mod_workshop'), array(
163 0 => get_string('overallfeedbackmode_0', 'mod_workshop'),
164 1 => get_string('overallfeedbackmode_1', 'mod_workshop'),
165 2 => get_string('overallfeedbackmode_2', 'mod_workshop')));
166 $mform->addHelpButton('overallfeedbackmode', 'overallfeedbackmode', 'mod_workshop');
167 $mform->setDefault('overallfeedbackmode', 1);
168 $mform->setAdvanced('overallfeedbackmode');
169
170 $options = array();
171 for ($i = 7; $i >= 0; $i--) {
172 $options[$i] = $i;
173 }
174 $mform->addElement('select', 'overallfeedbackfiles', get_string('overallfeedbackfiles', 'workshop'), $options);
175 $mform->setDefault('overallfeedbackfiles', 0);
176 $mform->setAdvanced('overallfeedbackfiles');
177 $mform->disabledIf('overallfeedbackfiles', 'overallfeedbackmode', 'eq', 0);
178
179 $options = get_max_upload_sizes($CFG->maxbytes, $this->course->maxbytes);
180 $mform->addElement('select', 'overallfeedbackmaxbytes', get_string('overallfeedbackmaxbytes', 'workshop'), $options);
181 $mform->setAdvanced('overallfeedbackmaxbytes');
182 $mform->setDefault('overallfeedbackmaxbytes', $workshopconfig->maxbytes);
183 $mform->disabledIf('overallfeedbackmaxbytes', 'overallfeedbackmode', 'eq', 0);
184 $mform->disabledIf('overallfeedbackmaxbytes', 'overallfeedbackfiles', 'eq', 0);
185
4eab2e7f 186 $label = get_string('examplesmode', 'workshop');
f6e8b318 187 $options = workshop::available_example_modes_list();
4eab2e7f
DM
188 $mform->addElement('select', 'examplesmode', $label, $options);
189 $mform->setDefault('examplesmode', $workshopconfig->examplesmode);
f6e8b318 190 $mform->disabledIf('examplesmode', 'useexamples');
4eab2e7f
DM
191 $mform->setAdvanced('examplesmode');
192
5a49f825
DM
193 // Miscellaneous settings
194 $mform->addElement('header', 'miscellaneoussettings', get_string('miscellaneoussettings', 'workshop'));
195
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');
200
6516b9e9 201 // Access control -------------------------------------------------------------
4eab2e7f
DM
202 $mform->addElement('header', 'accesscontrol', get_string('accesscontrol', 'workshop'));
203
204 $label = get_string('submissionstart', 'workshop');
de2f199b 205 $mform->addElement('date_time_selector', 'submissionstart', $label, array('optional' => true));
4eab2e7f
DM
206 $mform->setAdvanced('submissionstart');
207
208 $label = get_string('submissionend', 'workshop');
de2f199b 209 $mform->addElement('date_time_selector', 'submissionend', $label, array('optional' => true));
4eab2e7f
DM
210 $mform->setAdvanced('submissionend');
211
3ff08057
DM
212 $label = get_string('submissionendswitch', 'mod_workshop');
213 $mform->addElement('checkbox', 'phaseswitchassessment', $label);
214 $mform->setAdvanced('phaseswitchassessment');
215 $mform->disabledIf('phaseswitchassessment', 'submissionend[enabled]');
216 $mform->addHelpButton('phaseswitchassessment', 'submissionendswitch', 'mod_workshop');
217
4eab2e7f 218 $label = get_string('assessmentstart', 'workshop');
de2f199b 219 $mform->addElement('date_time_selector', 'assessmentstart', $label, array('optional' => true));
4eab2e7f
DM
220 $mform->setAdvanced('assessmentstart');
221
222 $label = get_string('assessmentend', 'workshop');
de2f199b 223 $mform->addElement('date_time_selector', 'assessmentend', $label, array('optional' => true));
4eab2e7f
DM
224 $mform->setAdvanced('assessmentend');
225
67ba9007 226 $coursecontext = context_course::instance($this->course->id);
67fbfe8b 227 plagiarism_get_form_elements_module($mform, $coursecontext, 'mod_workshop');
67ba9007 228
5924db72 229 // Common module settings, Restrict availability, Activity completion etc. ----
53fad4b9
DM
230 $features = array('groups'=>true, 'groupings'=>true, 'groupmembersonly'=>true,
231 'outcomes'=>true, 'gradecat'=>false, 'idnumber'=>false);
232
4eab2e7f
DM
233 $this->standard_coursemodule_elements();
234
6516b9e9 235 // Standard buttons, common to all modules ------------------------------------
4eab2e7f 236 $this->add_action_buttons();
6516b9e9 237 }
4eab2e7f 238
6516b9e9
DM
239 /**
240 * Prepares the form before data are set
241 *
a74cadfa
DM
242 * Additional wysiwyg editor are prepared here, the introeditor is prepared automatically by core.
243 * Grade items are set here because the core modedit supports single grade item only.
6516b9e9
DM
244 *
245 * @param array $data to be set
246 * @return void
247 */
a74cadfa 248 public function data_preprocessing(&$data) {
6516b9e9
DM
249 if ($this->current->instance) {
250 // editing an existing workshop - let us prepare the added editor elements (intro done automatically)
251 $draftitemid = file_get_submitted_draft_itemid('instructauthors');
252 $data['instructauthorseditor']['text'] = file_prepare_draft_area($draftitemid, $this->context->id,
6a0d85aa 253 'mod_workshop', 'instructauthors', 0,
6516b9e9
DM
254 workshop::instruction_editors_options($this->context),
255 $data['instructauthors']);
256 $data['instructauthorseditor']['format'] = $data['instructauthorsformat'];
257 $data['instructauthorseditor']['itemid'] = $draftitemid;
15d12b54
DM
258
259 $draftitemid = file_get_submitted_draft_itemid('instructreviewers');
260 $data['instructreviewerseditor']['text'] = file_prepare_draft_area($draftitemid, $this->context->id,
6a0d85aa 261 'mod_workshop', 'instructreviewers', 0,
15d12b54
DM
262 workshop::instruction_editors_options($this->context),
263 $data['instructreviewers']);
264 $data['instructreviewerseditor']['format'] = $data['instructreviewersformat'];
265 $data['instructreviewerseditor']['itemid'] = $draftitemid;
5a49f825
DM
266
267 $draftitemid = file_get_submitted_draft_itemid('conclusion');
268 $data['conclusioneditor']['text'] = file_prepare_draft_area($draftitemid, $this->context->id,
269 'mod_workshop', 'conclusion', 0,
270 workshop::instruction_editors_options($this->context),
271 $data['conclusion']);
272 $data['conclusioneditor']['format'] = $data['conclusionformat'];
273 $data['conclusioneditor']['itemid'] = $draftitemid;
6516b9e9
DM
274 } else {
275 // adding a new workshop instance
276 $draftitemid = file_get_submitted_draft_itemid('instructauthors');
6a0d85aa 277 file_prepare_draft_area($draftitemid, null, 'mod_workshop', 'instructauthors', 0); // no context yet, itemid not used
884482fb 278 $data['instructauthorseditor'] = array('text' => '', 'format' => editors_get_preferred_format(), 'itemid' => $draftitemid);
15d12b54
DM
279
280 $draftitemid = file_get_submitted_draft_itemid('instructreviewers');
6a0d85aa 281 file_prepare_draft_area($draftitemid, null, 'mod_workshop', 'instructreviewers', 0); // no context yet, itemid not used
884482fb 282 $data['instructreviewerseditor'] = array('text' => '', 'format' => editors_get_preferred_format(), 'itemid' => $draftitemid);
5a49f825
DM
283
284 $draftitemid = file_get_submitted_draft_itemid('conclusion');
285 file_prepare_draft_area($draftitemid, null, 'mod_workshop', 'conclusion', 0); // no context yet, itemid not used
286 $data['conclusioneditor'] = array('text' => '', 'format' => editors_get_preferred_format(), 'itemid' => $draftitemid);
6516b9e9 287 }
4eab2e7f 288 }
a74cadfa
DM
289
290 /**
291 * Set the grade item categories when editing an instance
292 */
293 public function definition_after_data() {
294
295 $mform =& $this->_form;
296
297 if ($id = $mform->getElementValue('update')) {
298 $instance = $mform->getElementValue('instance');
299
300 $gradeitems = grade_item::fetch_all(array(
301 'itemtype' => 'mod',
302 'itemmodule' => 'workshop',
303 'iteminstance' => $instance,
304 'courseid' => $this->course->id));
305
306 if (!empty($gradeitems)) {
307 foreach ($gradeitems as $gradeitem) {
308 // here comes really crappy way how to set the value of the fields
309 // gradecategory and gradinggradecategory - grrr QuickForms
310 if ($gradeitem->itemnumber == 0) {
311 $group = $mform->getElement('submissiongradegroup');
312 $elements = $group->getElements();
313 foreach ($elements as $element) {
314 if ($element->getName() == 'gradecategory') {
315 $element->setValue($gradeitem->categoryid);
316 }
317 }
318 } else if ($gradeitem->itemnumber == 1) {
319 $group = $mform->getElement('gradinggradegroup');
320 $elements = $group->getElements();
321 foreach ($elements as $element) {
322 if ($element->getName() == 'gradinggradecategory') {
323 $element->setValue($gradeitem->categoryid);
324 }
325 }
326 }
327 }
328 }
329 }
330
331 parent::definition_after_data();
332 }
df558765
DM
333
334 /**
335 * Validates the form input
336 *
337 * @param array $data submitted data
338 * @param array $files submitted files
339 * @return array eventual errors indexed by the field name
340 */
2fc04a4b 341 public function validation($data, $files) {
df558765
DM
342 $errors = array();
343
344 // check the phases borders are valid
345 if ($data['submissionstart'] > 0 and $data['submissionend'] > 0 and $data['submissionstart'] >= $data['submissionend']) {
346 $errors['submissionend'] = get_string('submissionendbeforestart', 'mod_workshop');
347 }
348 if ($data['assessmentstart'] > 0 and $data['assessmentend'] > 0 and $data['assessmentstart'] >= $data['assessmentend']) {
349 $errors['assessmentend'] = get_string('assessmentendbeforestart', 'mod_workshop');
350 }
351
352 // check the phases do not overlap
353 if (max($data['submissionstart'], $data['submissionend']) > 0 and max($data['assessmentstart'], $data['assessmentend']) > 0) {
354 $phasesubmissionend = max($data['submissionstart'], $data['submissionend']);
355 $phaseassessmentstart = min($data['assessmentstart'], $data['assessmentend']);
356 if ($phaseassessmentstart == 0) {
357 $phaseassessmentstart = max($data['assessmentstart'], $data['assessmentend']);
358 }
359 if ($phasesubmissionend > 0 and $phaseassessmentstart > 0 and $phaseassessmentstart < $phasesubmissionend) {
360 foreach (array('submissionend', 'submissionstart', 'assessmentstart', 'assessmentend') as $f) {
361 if ($data[$f] > 0) {
362 $errors[$f] = get_string('phasesoverlap', 'mod_workshop');
363 break;
364 }
365 }
366 }
367 }
368
369 return $errors;
370 }
4eab2e7f 371}