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