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