Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 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/>.
16
 
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: https://moodledev.io/docs/apis/subsystems/form
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
 */
28
 
29
defined('MOODLE_INTERNAL') || die();
30
 
31
require_once($CFG->dirroot . '/course/moodleform_mod.php');
32
require_once(__DIR__ . '/locallib.php');
33
require_once($CFG->libdir . '/filelib.php');
34
 
35
use core_grades\component_gradeitems;
36
/**
37
 * Module settings form for Workshop instances
38
 */
39
class mod_workshop_mod_form extends moodleform_mod {
40
 
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
 
52
    /**
53
     * Defines the workshop instance configuration form
54
     *
55
     * @return void
56
     */
57
    public function definition() {
58
        global $CFG, $PAGE;
59
 
60
        $workshopconfig = get_config('workshop');
61
        $mform = $this->_form;
62
 
63
        // General --------------------------------------------------------------------
64
        $mform->addElement('header', 'general', get_string('general', 'form'));
65
 
66
        // Workshop name
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 {
72
            $mform->setType('name', PARAM_CLEANHTML);
73
        }
74
        $mform->addRule('name', null, 'required', null, 'client');
75
        $mform->addRule('name', get_string('maximumchars', '', 255), 'maxlength', 255, 'client');
76
 
77
        // Introduction
78
        $this->standard_intro_elements(get_string('introduction', 'workshop'));
79
 
80
        // Grading settings -----------------------------------------------------------
81
        $mform->addElement('header', 'gradingsettings', get_string('gradingsettings', 'workshop'));
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');
88
 
89
        $grades = workshop::available_maxgrades_list();
90
        $gradecategories = grade_get_categories_menu($this->course->id);
91
 
92
        $label = get_string('submissiongrade', 'workshop');
93
        $mform->addGroup(array(
94
            $mform->createElement('select', 'grade', '', $grades),
95
            $mform->createElement('select', 'gradecategory', '', $gradecategories),
96
            ), 'submissiongradegroup', $label, ' ', false);
97
        $mform->setDefault('grade', $workshopconfig->grade);
98
        $mform->addHelpButton('submissiongradegroup', 'submissiongrade', 'workshop');
99
 
100
        $mform->addElement('float', 'submissiongradepass', get_string('gradetopasssubmission', 'workshop'));
101
        $mform->addHelpButton('submissiongradepass', 'gradepass', 'grades');
102
        $mform->setDefault('submissiongradepass', '');
103
 
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');
111
 
112
        $mform->addElement('float', 'gradinggradepass', get_string('gradetopassgrading', 'workshop'));
113
        $mform->addHelpButton('gradinggradepass', 'gradepass', 'grades');
114
        $mform->setDefault('gradinggradepass', '');
115
 
116
        $options = array();
117
        for ($i = 5; $i >= 0; $i--) {
118
            $options[$i] = $i;
119
        }
120
        $label = get_string('gradedecimals', 'workshop');
121
        $mform->addElement('select', 'gradedecimals', $label, $options);
122
        $mform->setDefault('gradedecimals', $workshopconfig->gradedecimals);
123
 
124
        // Submission settings --------------------------------------------------------
125
        $mform->addElement('header', 'submissionsettings', get_string('submissionsettings', 'workshop'));
126
 
127
        $label = get_string('instructauthors', 'workshop');
128
        $mform->addElement('editor', 'instructauthorseditor', $label, null,
129
                            workshop::instruction_editors_options($this->context));
130
 
131
        $typeelements = [];
132
        foreach (['submissiontypetext', 'submissiontypefile'] as $type) {
133
            $available = $type . 'available';
134
            $required = $type . 'required';
135
            $availablelabel = get_string($available, 'workshop');
136
            $requiredlabel = get_string($required, 'workshop');
137
            $typeelements[] = $mform->createElement('advcheckbox', $available, '', $availablelabel);
138
            $typeelements[] = $mform->createElement('advcheckbox', $required, '', $requiredlabel);
139
            $mform->setDefault($available, 1);
140
        }
141
        // We can't use <br> as the separator as it does not work well in this case with the Boost theme.
142
        // Instead, separate both tuples with a full-width empty div.
143
        $mform->addGroup($typeelements, 'submissiontypes', get_string('submissiontypes', 'workshop'),
144
            array(' ', '<div style="width:100%"></div>'), false);
145
 
146
        $options = array();
147
        for ($i = 7; $i >= 1; $i--) {
148
            $options[$i] = $i;
149
        }
150
        $label = get_string('nattachments', 'workshop');
151
        $mform->addElement('select', 'nattachments', $label, $options);
152
        $mform->setDefault('nattachments', 1);
153
        $mform->hideIf('nattachments', 'submissiontypefileavailable');
154
 
155
        $label = get_string('allowedfiletypesforsubmission', 'workshop');
156
        $mform->addElement('filetypes', 'submissionfiletypes', $label);
157
        $mform->addHelpButton('submissionfiletypes', 'allowedfiletypesforsubmission', 'workshop');
158
        $mform->hideIf('submissionfiletypes', 'submissiontypefileavailable');
159
 
160
        $options = get_max_upload_sizes($CFG->maxbytes, $this->course->maxbytes, 0, $workshopconfig->maxbytes);
161
        $mform->addElement('select', 'maxbytes', get_string('maxbytes', 'workshop'), $options);
162
        $mform->setDefault('maxbytes', $workshopconfig->maxbytes);
163
        $mform->hideIf('maxbytes', 'submissiontypefileavailable');
164
 
165
        $label = get_string('latesubmissions', 'workshop');
166
        $text = get_string('latesubmissions_desc', 'workshop');
167
        $mform->addElement('checkbox', 'latesubmissions', $label, $text);
168
        $mform->addHelpButton('latesubmissions', 'latesubmissions', 'workshop');
169
 
170
        // Assessment settings --------------------------------------------------------
171
        $mform->addElement('header', 'assessmentsettings', get_string('assessmentsettings', 'workshop'));
172
 
173
        $label = get_string('instructreviewers', 'workshop');
174
        $mform->addElement('editor', 'instructreviewerseditor', $label, null,
175
                            workshop::instruction_editors_options($this->context));
176
 
177
        $label = get_string('useselfassessment', 'workshop');
178
        $text = get_string('useselfassessment_desc', 'workshop');
179
        $mform->addElement('checkbox', 'useselfassessment', $label, $text);
180
        $mform->addHelpButton('useselfassessment', 'useselfassessment', 'workshop');
181
 
182
        // Feedback -------------------------------------------------------------------
183
        $mform->addElement('header', 'feedbacksettings', get_string('feedbacksettings', 'workshop'));
184
 
185
        $mform->addElement('select', 'overallfeedbackmode', get_string('overallfeedbackmode', 'mod_workshop'), array(
186
 
187
            1 => get_string('overallfeedbackmode_1', 'mod_workshop'),
188
            2 => get_string('overallfeedbackmode_2', 'mod_workshop')));
189
        $mform->addHelpButton('overallfeedbackmode', 'overallfeedbackmode', 'mod_workshop');
190
        $mform->setDefault('overallfeedbackmode', 1);
191
 
192
        $options = array();
193
        for ($i = 7; $i >= 0; $i--) {
194
            $options[$i] = $i;
195
        }
196
        $mform->addElement('select', 'overallfeedbackfiles', get_string('overallfeedbackfiles', 'workshop'), $options);
197
        $mform->setDefault('overallfeedbackfiles', 0);
198
        $mform->hideIf('overallfeedbackfiles', 'overallfeedbackmode', 'eq', 0);
199
 
200
        $label = get_string('allowedfiletypesforoverallfeedback', 'workshop');
201
        $mform->addElement('filetypes', 'overallfeedbackfiletypes', $label);
202
        $mform->addHelpButton('overallfeedbackfiletypes', 'allowedfiletypesforoverallfeedback', 'workshop');
203
        $mform->hideIf('overallfeedbackfiletypes', 'overallfeedbackfiles', 'eq', 0);
204
 
205
        $options = get_max_upload_sizes($CFG->maxbytes, $this->course->maxbytes);
206
        $mform->addElement('select', 'overallfeedbackmaxbytes', get_string('overallfeedbackmaxbytes', 'workshop'), $options);
207
        $mform->setDefault('overallfeedbackmaxbytes', $workshopconfig->maxbytes);
208
        $mform->hideIf('overallfeedbackmaxbytes', 'overallfeedbackmode', 'eq', 0);
209
        $mform->hideIf('overallfeedbackmaxbytes', 'overallfeedbackfiles', 'eq', 0);
210
 
211
        $label = get_string('conclusion', 'workshop');
212
        $mform->addElement('editor', 'conclusioneditor', $label, null,
213
                            workshop::instruction_editors_options($this->context));
214
        $mform->addHelpButton('conclusioneditor', 'conclusion', 'workshop');
215
 
216
        // Example submissions --------------------------------------------------------
217
        $mform->addElement('header', 'examplesubmissionssettings', get_string('examplesubmissions', 'workshop'));
218
 
219
        $label = get_string('useexamples', 'workshop');
220
        $text = get_string('useexamples_desc', 'workshop');
221
        $mform->addElement('checkbox', 'useexamples', $label, $text);
222
        $mform->addHelpButton('useexamples', 'useexamples', 'workshop');
223
 
224
        $label = get_string('examplesmode', 'workshop');
225
        $options = workshop::available_example_modes_list();
226
        $mform->addElement('select', 'examplesmode', $label, $options);
227
        $mform->setDefault('examplesmode', $workshopconfig->examplesmode);
228
        $mform->hideIf('examplesmode', 'useexamples');
229
 
230
        // Availability ---------------------------------------------------------------
231
        $mform->addElement('header', 'accesscontrol', get_string('availability', 'core'));
232
 
233
        $label = get_string('submissionstart', 'workshop');
234
        $mform->addElement('date_time_selector', 'submissionstart', $label, array('optional' => true));
235
 
236
        $label = get_string('submissionend', 'workshop');
237
        $mform->addElement('date_time_selector', 'submissionend', $label, array('optional' => true));
238
 
239
        $label = get_string('submissionendswitch', 'mod_workshop');
240
        $mform->addElement('checkbox', 'phaseswitchassessment', $label);
241
        $mform->hideIf('phaseswitchassessment', 'submissionend[enabled]');
242
        $mform->addHelpButton('phaseswitchassessment', 'submissionendswitch', 'mod_workshop');
243
 
244
        $label = get_string('assessmentstart', 'workshop');
245
        $mform->addElement('date_time_selector', 'assessmentstart', $label, array('optional' => true));
246
 
247
        $label = get_string('assessmentend', 'workshop');
248
        $mform->addElement('date_time_selector', 'assessmentend', $label, array('optional' => true));
249
 
250
        // Common module settings, Restrict availability, Activity completion etc. ----
251
        $features = array('groups' => true, 'groupings' => true,
252
                'outcomes' => true, 'gradecat' => false, 'idnumber' => false);
253
 
254
        $this->standard_coursemodule_elements();
255
 
256
        // Standard buttons, common to all modules ------------------------------------
257
        $this->add_action_buttons();
258
 
259
        $PAGE->requires->js_call_amd('mod_workshop/modform', 'init');
260
    }
261
 
262
    /**
263
     * Prepares the form before data are set
264
     *
265
     * Additional wysiwyg editor are prepared here, the introeditor is prepared automatically by core.
266
     * Grade items are set here because the core modedit supports single grade item only.
267
     *
268
     * @param array $data to be set
269
     * @return void
270
     */
271
    public function data_preprocessing(&$data) {
272
        if ($this->current->instance) {
273
            // editing an existing workshop - let us prepare the added editor elements (intro done automatically)
274
            $draftitemid = file_get_submitted_draft_itemid('instructauthors');
275
            $data['instructauthorseditor']['text'] = file_prepare_draft_area($draftitemid, $this->context->id,
276
                                'mod_workshop', 'instructauthors', 0,
277
                                workshop::instruction_editors_options($this->context),
278
                                $data['instructauthors']);
279
            $data['instructauthorseditor']['format'] = $data['instructauthorsformat'];
280
            $data['instructauthorseditor']['itemid'] = $draftitemid;
281
 
282
            $draftitemid = file_get_submitted_draft_itemid('instructreviewers');
283
            $data['instructreviewerseditor']['text'] = file_prepare_draft_area($draftitemid, $this->context->id,
284
                                'mod_workshop', 'instructreviewers', 0,
285
                                workshop::instruction_editors_options($this->context),
286
                                $data['instructreviewers']);
287
            $data['instructreviewerseditor']['format'] = $data['instructreviewersformat'];
288
            $data['instructreviewerseditor']['itemid'] = $draftitemid;
289
 
290
            $draftitemid = file_get_submitted_draft_itemid('conclusion');
291
            $data['conclusioneditor']['text'] = file_prepare_draft_area($draftitemid, $this->context->id,
292
                                'mod_workshop', 'conclusion', 0,
293
                                workshop::instruction_editors_options($this->context),
294
                                $data['conclusion']);
295
            $data['conclusioneditor']['format'] = $data['conclusionformat'];
296
            $data['conclusioneditor']['itemid'] = $draftitemid;
297
            // Set submission type checkboxes.
298
            foreach (['submissiontypetext', 'submissiontypefile'] as $type) {
299
                $data[$type . 'available'] = 1;
300
                $data[$type . 'required'] = 0;
301
                if ($data[$type] == WORKSHOP_SUBMISSION_TYPE_DISABLED) {
302
                    $data[$type . 'available'] = 0;
303
                } else if ($data[$type] == WORKSHOP_SUBMISSION_TYPE_REQUIRED) {
304
                    $data[$type . 'required'] = 1;
305
                }
306
            }
307
        } else {
308
            // adding a new workshop instance
309
            $draftitemid = file_get_submitted_draft_itemid('instructauthors');
310
            file_prepare_draft_area($draftitemid, null, 'mod_workshop', 'instructauthors', 0);    // no context yet, itemid not used
311
            $data['instructauthorseditor'] = array('text' => '', 'format' => editors_get_preferred_format(), 'itemid' => $draftitemid);
312
 
313
            $draftitemid = file_get_submitted_draft_itemid('instructreviewers');
314
            file_prepare_draft_area($draftitemid, null, 'mod_workshop', 'instructreviewers', 0);    // no context yet, itemid not used
315
            $data['instructreviewerseditor'] = array('text' => '', 'format' => editors_get_preferred_format(), 'itemid' => $draftitemid);
316
 
317
            $draftitemid = file_get_submitted_draft_itemid('conclusion');
318
            file_prepare_draft_area($draftitemid, null, 'mod_workshop', 'conclusion', 0);    // no context yet, itemid not used
319
            $data['conclusioneditor'] = array('text' => '', 'format' => editors_get_preferred_format(), 'itemid' => $draftitemid);
320
        }
321
    }
322
 
323
    /**
324
     * Combine submission type checkboxes into integer values for the database.
325
     *
326
     * @param stdClass $data The submitted form data.
327
     */
328
    public function data_postprocessing($data) {
329
        parent::data_postprocessing($data);
330
 
331
        foreach (['text', 'file'] as $type) {
332
            $field = 'submissiontype' . $type;
333
            $available = $field . 'available';
334
            $required = $field . 'required';
335
            if ($data->$required) {
336
                $data->$field = WORKSHOP_SUBMISSION_TYPE_REQUIRED;
337
            } else if ($data->$available) {
338
                $data->$field = WORKSHOP_SUBMISSION_TYPE_AVAILABLE;
339
            } else {
340
                $data->$field = WORKSHOP_SUBMISSION_TYPE_DISABLED;
341
            }
342
            unset($data->$available);
343
            unset($data->$required);
344
        }
345
    }
346
 
347
    /**
348
     * Set the grade item categories when editing an instance
349
     */
350
    public function definition_after_data() {
351
 
352
        $mform =& $this->_form;
353
 
354
        if ($id = $mform->getElementValue('update')) {
355
            $instance   = $mform->getElementValue('instance');
356
 
357
            $gradeitems = grade_item::fetch_all(array(
358
                'itemtype'      => 'mod',
359
                'itemmodule'    => 'workshop',
360
                'iteminstance'  => $instance,
361
                'courseid'      => $this->course->id));
362
 
363
            if (!empty($gradeitems)) {
364
                foreach ($gradeitems as $gradeitem) {
365
                    // here comes really crappy way how to set the value of the fields
366
                    // gradecategory and gradinggradecategory - grrr QuickForms
367
                    $decimalpoints = $gradeitem->get_decimals();
368
                    if ($gradeitem->itemnumber == 0) {
369
                        $mform->setDefault('submissiongradepass', format_float($gradeitem->gradepass, $decimalpoints));
370
                        $group = $mform->getElement('submissiongradegroup');
371
                        $elements = $group->getElements();
372
                        foreach ($elements as $element) {
373
                            if ($element->getName() == 'gradecategory') {
374
                                $element->setValue($gradeitem->categoryid);
375
                            }
376
                        }
377
                    } else if ($gradeitem->itemnumber == 1) {
378
                        $mform->setDefault('gradinggradepass', format_float($gradeitem->gradepass, $decimalpoints));
379
                        $group = $mform->getElement('gradinggradegroup');
380
                        $elements = $group->getElements();
381
                        foreach ($elements as $element) {
382
                            if ($element->getName() == 'gradinggradecategory') {
383
                                $element->setValue($gradeitem->categoryid);
384
                            }
385
                        }
386
                    }
387
                }
388
            }
389
        }
390
        $typevalues = $mform->getElementValue('submissiontypes');
391
        foreach (['submissiontypetext', 'submissiontypefile'] as $type) {
392
            // Don't leave a disabled "required" checkbox checked.
393
            if (!$typevalues[$type . 'available']) {
394
                $mform->setDefault($type . 'required', 0);
395
            }
396
        }
397
 
398
        parent::definition_after_data();
399
    }
400
 
401
    /**
402
     * Validates the form input
403
     *
404
     * @param array $data submitted data
405
     * @param array $files submitted files
406
     * @return array eventual errors indexed by the field name
407
     */
408
    public function validation($data, $files) {
409
        $errors = parent::validation($data, $files);
410
 
411
        // check the phases borders are valid
412
        if ($data['submissionstart'] > 0 and $data['submissionend'] > 0 and $data['submissionstart'] >= $data['submissionend']) {
413
            $errors['submissionend'] = get_string('submissionendbeforestart', 'mod_workshop');
414
        }
415
        if ($data['assessmentstart'] > 0 and $data['assessmentend'] > 0 and $data['assessmentstart'] >= $data['assessmentend']) {
416
            $errors['assessmentend'] = get_string('assessmentendbeforestart', 'mod_workshop');
417
        }
418
 
419
        // check the phases do not overlap
420
        if (max($data['submissionstart'], $data['submissionend']) > 0 and max($data['assessmentstart'], $data['assessmentend']) > 0) {
421
            $phasesubmissionend = max($data['submissionstart'], $data['submissionend']);
422
            $phaseassessmentstart = min($data['assessmentstart'], $data['assessmentend']);
423
            if ($phaseassessmentstart == 0) {
424
                $phaseassessmentstart = max($data['assessmentstart'], $data['assessmentend']);
425
            }
426
            if ($phasesubmissionend > 0 and $phaseassessmentstart > 0 and $phaseassessmentstart < $phasesubmissionend) {
427
                foreach (array('submissionend', 'submissionstart', 'assessmentstart', 'assessmentend') as $f) {
428
                    if ($data[$f] > 0) {
429
                        $errors[$f] = get_string('phasesoverlap', 'mod_workshop');
430
                        break;
431
                    }
432
                }
433
            }
434
        }
435
 
436
        // Check that the submission grade pass is a valid number.
437
        if (!empty($data['submissiongradepass'])) {
438
            $submissiongradefloat = unformat_float($data['submissiongradepass'], true);
439
            if ($submissiongradefloat === false) {
440
                $errors['submissiongradepass'] = get_string('err_numeric', 'form');
441
            } else {
442
                if ($submissiongradefloat > $data['grade']) {
443
                    $errors['submissiongradepass'] = get_string('gradepassgreaterthangrade', 'grades', $data['grade']);
444
                }
445
            }
446
        }
447
 
448
        // Check that the grade pass is a valid number.
449
        if (!empty($data['gradinggradepass'])) {
450
            $gradepassfloat = unformat_float($data['gradinggradepass'], true);
451
            if ($gradepassfloat === false) {
452
                $errors['gradinggradepass'] = get_string('err_numeric', 'form');
453
            } else {
454
                if ($gradepassfloat > $data['gradinggrade']) {
455
                    $errors['gradinggradepass'] = get_string('gradepassgreaterthangrade', 'grades', $data['gradinggrade']);
456
                }
457
            }
458
        }
459
 
460
        // We need to do a custom completion validation because workshop grade items identifiers divert from standard.
461
        // Refer to validation defined in moodleform_mod.php.
462
        if (isset($data['completionpassgrade']) && $data['completionpassgrade'] &&
463
            isset($data['completiongradeitemnumber'])) {
464
            $itemnames = component_gradeitems::get_itemname_mapping_for_component('mod_workshop');
465
            $gradepassfield = $itemnames[(int) $data['completiongradeitemnumber']] . 'gradepass';
466
            // We need to make all the validations related with $gradepassfield
467
            // with them being correct floats, keeping the originals unmodified for
468
            // later validations / showing the form back...
469
            // TODO: Note that once MDL-73994 is fixed we'll have to re-visit this and
470
            // adapt the code below to the new values arriving here, without forgetting
471
            // the special case of empties and nulls.
472
            $gradepass = isset($data[$gradepassfield]) ? unformat_float($data[$gradepassfield]) : null;
473
            if (is_null($gradepass) || $gradepass == 0) {
474
                $errors['completionpassgrade'] = get_string(
475
                    'activitygradetopassnotset',
476
                    'completion'
477
                );
478
            } else {
479
                // We have validated grade pass. Unset any errors.
480
                unset($errors['completionpassgrade']);
481
            }
482
        }
483
 
484
        if (!$data['submissiontypetextavailable'] && !$data['submissiontypefileavailable']) {
485
            // One submission type must be available.
486
            $errors['submissiontypes'] = get_string('nosubmissiontype', 'workshop');
487
        }
488
 
489
        return $errors;
490
    }
491
}