Proyectos de Subversion Moodle

Rev

Rev 1 | | Comparar con el anterior | 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
 * Defines the quiz module ettings form.
19
 *
20
 * @package    mod_quiz
21
 * @copyright  2006 Jamie Pratt
22
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23
 */
24
 
25
 
26
defined('MOODLE_INTERNAL') || die();
27
 
28
require_once($CFG->dirroot . '/course/moodleform_mod.php');
29
require_once($CFG->dirroot . '/mod/quiz/locallib.php');
30
 
31
use mod_quiz\access_manager;
32
use mod_quiz\question\display_options;
33
 
34
/**
35
 * Settings form for the quiz module.
36
 *
37
 * @copyright  2006 Jamie Pratt
38
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
39
 */
40
class mod_quiz_mod_form extends moodleform_mod {
41
    /** @var array options to be used with date_time_selector fields in the quiz. */
42
    public static $datefieldoptions = ['optional' => true];
43
 
44
    /** @var array caches the quiz overall feedback, for convenience. */
45
    protected $_feedbacks;
46
 
47
    /** @var array for convenience stores the list of types of review option. Initialised in the constructor. */
48
    protected static $reviewfields = [];
49
 
50
    /** @var int the max number of attempts allowed in any user or group override on this quiz. */
51
    protected $maxattemptsanyoverride = null;
52
 
53
    public function __construct($current, $section, $cm, $course) {
54
        self::$reviewfields = [
55
            'attempt'          => ['theattempt', 'quiz'],
56
            'correctness'      => ['whethercorrect', 'question'],
57
            'maxmarks'         => ['maxmarks', 'quiz'],
58
            'marks'            => ['marks', 'quiz'],
59
            'specificfeedback' => ['specificfeedback', 'question'],
60
            'generalfeedback'  => ['generalfeedback', 'question'],
61
            'rightanswer'      => ['rightanswer', 'question'],
62
            'overallfeedback'  => ['reviewoverallfeedback', 'quiz'],
63
        ];
64
        parent::__construct($current, $section, $cm, $course);
65
    }
66
 
67
    protected function definition() {
68
        global $CFG, $DB, $PAGE;
69
        $quizconfig = get_config('quiz');
70
        $mform = $this->_form;
71
 
72
        // -------------------------------------------------------------------------------
73
        $mform->addElement('header', 'general', get_string('general', 'form'));
74
 
75
        // Name.
76
        $mform->addElement('text', 'name', get_string('name'), ['size' => '64']);
77
        if (!empty($CFG->formatstringstriptags)) {
78
            $mform->setType('name', PARAM_TEXT);
79
        } else {
80
            $mform->setType('name', PARAM_CLEANHTML);
81
        }
82
        $mform->addRule('name', null, 'required', null, 'client');
83
        $mform->addRule('name', get_string('maximumchars', '', 255), 'maxlength', 255, 'client');
84
 
85
        // Introduction.
86
        $this->standard_intro_elements(get_string('introduction', 'quiz'));
87
 
88
        // -------------------------------------------------------------------------------
89
        $mform->addElement('header', 'timing', get_string('timing', 'quiz'));
90
 
91
        // Open and close dates.
92
        $mform->addElement('date_time_selector', 'timeopen', get_string('quizopen', 'quiz'),
93
                self::$datefieldoptions);
94
        $mform->addHelpButton('timeopen', 'quizopenclose', 'quiz');
95
 
96
        $mform->addElement('date_time_selector', 'timeclose', get_string('quizclose', 'quiz'),
97
                self::$datefieldoptions);
98
 
99
        // Time limit.
100
        $mform->addElement('duration', 'timelimit', get_string('timelimit', 'quiz'),
101
                ['optional' => true]);
102
        $mform->addHelpButton('timelimit', 'timelimit', 'quiz');
103
 
104
        // What to do with overdue attempts.
105
        $mform->addElement('select', 'overduehandling', get_string('overduehandling', 'quiz'),
106
                quiz_get_overdue_handling_options());
107
        $mform->addHelpButton('overduehandling', 'overduehandling', 'quiz');
108
        // TODO Formslib does OR logic on disableif, and we need AND logic here.
109
        // $mform->disabledIf('overduehandling', 'timelimit', 'eq', 0);
110
        // $mform->disabledIf('overduehandling', 'timeclose', 'eq', 0);
111
 
112
        // Grace period time.
113
        $mform->addElement('duration', 'graceperiod', get_string('graceperiod', 'quiz'),
114
                ['optional' => true]);
115
        $mform->addHelpButton('graceperiod', 'graceperiod', 'quiz');
116
        $mform->hideIf('graceperiod', 'overduehandling', 'neq', 'graceperiod');
117
 
1441 ariadna 118
        // Pre-create attempts.
119
        // This is only shown if "Pre-create period" as been set at site level, and the quiz open time is enabled.
120
        $precreateperiod = get_config('quiz', 'precreateperiod');
121
        if (!empty($precreateperiod)) {
122
            $yesoption = get_string('precreateyes', 'quiz', $precreateperiod / HOURSECS);
123
            $precreateoptions = [
124
                1 => $yesoption,
125
 
126
            ];
127
            $mform->addElement(
128
                'select',
129
                'precreateattempts',
130
                get_string('precreateattempts', 'quiz'),
131
                $precreateoptions
132
            );
133
            $mform->hideIf('precreateattempts', 'timeopen[enabled]');
134
            $mform->addHelpButton('precreateattempts', 'precreateattempts', 'quiz');
135
        }
136
 
1 efrain 137
        // -------------------------------------------------------------------------------
138
        // Grade settings.
139
        $this->standard_grading_coursemodule_elements();
140
 
141
        $mform->removeElement('grade');
142
        if (property_exists($this->current, 'grade')) {
143
            $currentgrade = $this->current->grade;
144
        } else {
145
            $currentgrade = $quizconfig->maximumgrade;
146
        }
147
        $mform->addElement('hidden', 'grade', $currentgrade);
148
        $mform->setType('grade', PARAM_FLOAT);
149
 
150
        // Number of attempts.
151
        $attemptoptions = ['0' => get_string('unlimited')];
152
        for ($i = 1; $i <= QUIZ_MAX_ATTEMPT_OPTION; $i++) {
153
            $attemptoptions[$i] = $i;
154
        }
155
        $mform->addElement('select', 'attempts', get_string('attemptsallowed', 'quiz'),
156
                $attemptoptions);
157
 
158
        // Grading method.
159
        $mform->addElement('select', 'grademethod', get_string('grademethod', 'quiz'),
160
                quiz_get_grading_options());
161
        $mform->addHelpButton('grademethod', 'grademethod', 'quiz');
162
        if ($this->get_max_attempts_for_any_override() < 2) {
163
            $mform->hideIf('grademethod', 'attempts', 'eq', 1);
164
        }
165
 
166
        // -------------------------------------------------------------------------------
167
        $mform->addElement('header', 'layouthdr', get_string('layout', 'quiz'));
168
 
169
        $pagegroup = [];
170
        $pagegroup[] = $mform->createElement('select', 'questionsperpage',
171
                get_string('newpage', 'quiz'), quiz_questions_per_page_options(), ['id' => 'id_questionsperpage']);
172
        $mform->setDefault('questionsperpage', $quizconfig->questionsperpage);
173
 
174
        if (!empty($this->_cm) && !quiz_has_attempts($this->_cm->instance)) {
175
            $pagegroup[] = $mform->createElement('checkbox', 'repaginatenow', '',
176
                    get_string('repaginatenow', 'quiz'), ['id' => 'id_repaginatenow']);
177
        }
178
 
179
        $mform->addGroup($pagegroup, 'questionsperpagegrp',
180
                get_string('newpage', 'quiz'), null, false);
181
        $mform->addHelpButton('questionsperpagegrp', 'newpage', 'quiz');
182
        $mform->setAdvanced('questionsperpagegrp', $quizconfig->questionsperpage_adv);
183
 
184
        // Navigation method.
185
        $mform->addElement('select', 'navmethod', get_string('navmethod', 'quiz'),
186
                quiz_get_navigation_options());
187
        $mform->addHelpButton('navmethod', 'navmethod', 'quiz');
188
 
189
        // -------------------------------------------------------------------------------
190
        $mform->addElement('header', 'interactionhdr', get_string('questionbehaviour', 'quiz'));
191
 
192
        // Shuffle within questions.
193
        $mform->addElement('selectyesno', 'shuffleanswers', get_string('shufflewithin', 'quiz'));
194
        $mform->addHelpButton('shuffleanswers', 'shufflewithin', 'quiz');
195
 
196
        // How questions behave (question behaviour).
197
        if (!empty($this->current->preferredbehaviour)) {
198
            $currentbehaviour = $this->current->preferredbehaviour;
199
        } else {
200
            $currentbehaviour = '';
201
        }
202
        $behaviours = question_engine::get_behaviour_options($currentbehaviour);
203
        $mform->addElement('select', 'preferredbehaviour',
204
                get_string('howquestionsbehave', 'question'), $behaviours);
205
        $mform->addHelpButton('preferredbehaviour', 'howquestionsbehave', 'question');
206
 
207
        // Can redo completed questions.
208
        $redochoices = [0 => get_string('no'), 1 => get_string('canredoquestionsyes', 'quiz')];
209
        $mform->addElement('select', 'canredoquestions', get_string('canredoquestions', 'quiz'), $redochoices);
210
        $mform->addHelpButton('canredoquestions', 'canredoquestions', 'quiz');
211
        foreach ($behaviours as $behaviour => $notused) {
212
            if (!question_engine::can_questions_finish_during_the_attempt($behaviour)) {
213
                $mform->hideIf('canredoquestions', 'preferredbehaviour', 'eq', $behaviour);
214
            }
215
        }
216
 
217
        // Each attempt builds on last.
218
        $mform->addElement('selectyesno', 'attemptonlast',
219
                get_string('eachattemptbuildsonthelast', 'quiz'));
220
        $mform->addHelpButton('attemptonlast', 'eachattemptbuildsonthelast', 'quiz');
221
        if ($this->get_max_attempts_for_any_override() < 2) {
222
            $mform->hideIf('attemptonlast', 'attempts', 'eq', 1);
223
        }
224
 
225
        // -------------------------------------------------------------------------------
226
        $mform->addElement('header', 'reviewoptionshdr',
227
                get_string('reviewoptionsheading', 'quiz'));
228
        $mform->addHelpButton('reviewoptionshdr', 'reviewoptionsheading', 'quiz');
229
 
230
        // Review options.
231
        $this->add_review_options_group($mform, $quizconfig, 'during',
232
                display_options::DURING, true);
233
        $this->add_review_options_group($mform, $quizconfig, 'immediately',
234
                display_options::IMMEDIATELY_AFTER);
235
        $this->add_review_options_group($mform, $quizconfig, 'open',
236
                display_options::LATER_WHILE_OPEN);
237
        $this->add_review_options_group($mform, $quizconfig, 'closed',
238
                display_options::AFTER_CLOSE);
239
 
240
        foreach ($behaviours as $behaviour => $notused) {
241
            $unusedoptions = question_engine::get_behaviour_unused_display_options($behaviour);
242
            foreach ($unusedoptions as $unusedoption) {
243
                $mform->disabledIf($unusedoption . 'during', 'preferredbehaviour',
244
                        'eq', $behaviour);
245
            }
246
        }
247
        $mform->disabledIf('attemptduring', 'preferredbehaviour',
248
                'neq', 'wontmatch');
249
        $mform->disabledIf('overallfeedbackduring', 'preferredbehaviour',
250
                'neq', 'wontmatch');
251
        foreach (self::$reviewfields as $field => $notused) {
252
            $mform->disabledIf($field . 'closed', 'timeclose[enabled]');
253
        }
254
 
255
        // -------------------------------------------------------------------------------
256
        $mform->addElement('header', 'display', get_string('appearance'));
257
 
258
        // Show user picture.
259
        $mform->addElement('select', 'showuserpicture', get_string('showuserpicture', 'quiz'),
260
                quiz_get_user_image_options());
261
        $mform->addHelpButton('showuserpicture', 'showuserpicture', 'quiz');
262
 
263
        // Overall decimal points.
264
        $options = [];
265
        for ($i = 0; $i <= QUIZ_MAX_DECIMAL_OPTION; $i++) {
266
            $options[$i] = $i;
267
        }
268
        $mform->addElement('select', 'decimalpoints', get_string('decimalplaces', 'quiz'),
269
                $options);
270
        $mform->addHelpButton('decimalpoints', 'decimalplaces', 'quiz');
271
 
272
        // Question decimal points.
273
        $options = [-1 => get_string('sameasoverall', 'quiz')];
274
        for ($i = 0; $i <= QUIZ_MAX_Q_DECIMAL_OPTION; $i++) {
275
            $options[$i] = $i;
276
        }
277
        $mform->addElement('select', 'questiondecimalpoints',
278
                get_string('decimalplacesquestion', 'quiz'), $options);
279
        $mform->addHelpButton('questiondecimalpoints', 'decimalplacesquestion', 'quiz');
280
 
281
        // Show blocks during quiz attempt.
282
        $mform->addElement('selectyesno', 'showblocks', get_string('showblocks', 'quiz'));
283
        $mform->addHelpButton('showblocks', 'showblocks', 'quiz');
284
 
285
        // -------------------------------------------------------------------------------
286
        $mform->addElement('header', 'security', get_string('extraattemptrestrictions', 'quiz'));
287
 
288
        // Require password to begin quiz attempt.
289
        $mform->addElement('passwordunmask', 'quizpassword', get_string('requirepassword', 'quiz'));
290
        $mform->setType('quizpassword', PARAM_TEXT);
291
        $mform->addHelpButton('quizpassword', 'requirepassword', 'quiz');
292
 
293
        // IP address.
294
        $mform->addElement('text', 'subnet', get_string('requiresubnet', 'quiz'));
295
        $mform->setType('subnet', PARAM_TEXT);
296
        $mform->addHelpButton('subnet', 'requiresubnet', 'quiz');
297
 
298
        // Enforced time delay between quiz attempts.
299
        $mform->addElement('duration', 'delay1', get_string('delay1st2nd', 'quiz'),
300
                ['optional' => true]);
301
        $mform->addHelpButton('delay1', 'delay1st2nd', 'quiz');
302
        if ($this->get_max_attempts_for_any_override() < 2) {
303
            $mform->hideIf('delay1', 'attempts', 'eq', 1);
304
        }
305
 
306
        $mform->addElement('duration', 'delay2', get_string('delaylater', 'quiz'),
307
                ['optional' => true]);
308
        $mform->addHelpButton('delay2', 'delaylater', 'quiz');
309
        if ($this->get_max_attempts_for_any_override() < 3) {
310
            $mform->hideIf('delay2', 'attempts', 'eq', 1);
311
            $mform->hideIf('delay2', 'attempts', 'eq', 2);
312
        }
313
 
314
        // Browser security choices.
315
        $mform->addElement('select', 'browsersecurity', get_string('browsersecurity', 'quiz'),
316
                access_manager::get_browser_security_choices());
317
        $mform->addHelpButton('browsersecurity', 'browsersecurity', 'quiz');
318
 
319
        // Any other rule plugins.
320
        access_manager::add_settings_form_fields($this, $mform);
321
 
322
        // -------------------------------------------------------------------------------
323
        $mform->addElement('header', 'overallfeedbackhdr', get_string('overallfeedback', 'quiz'));
324
        $mform->addHelpButton('overallfeedbackhdr', 'overallfeedback', 'quiz');
325
 
326
        if (isset($this->current->grade)) {
327
            $needwarning = $this->current->grade === 0;
328
        } else {
329
            $needwarning = $quizconfig->maximumgrade == 0;
330
        }
331
        if ($needwarning) {
332
            $mform->addElement('static', 'nogradewarning', '',
333
                    get_string('nogradewarning', 'quiz'));
334
        }
335
 
336
        $mform->addElement('static', 'gradeboundarystatic1',
337
                get_string('gradeboundary', 'quiz'), '100%');
338
 
339
        $repeatarray = [];
340
        $repeatedoptions = [];
341
        $repeatarray[] = $mform->createElement('editor', 'feedbacktext',
342
                get_string('feedback', 'quiz'), ['rows' => 3], ['maxfiles' => EDITOR_UNLIMITED_FILES,
343
                        'noclean' => true, 'context' => $this->context]);
344
        $repeatarray[] = $mform->createElement('text', 'feedbackboundaries',
345
                get_string('gradeboundary', 'quiz'), ['size' => 10]);
346
        $repeatedoptions['feedbacktext']['type'] = PARAM_RAW;
347
        $repeatedoptions['feedbackboundaries']['type'] = PARAM_RAW;
348
 
349
        if (!empty($this->_instance)) {
350
            $this->_feedbacks = $DB->get_records('quiz_feedback',
351
                    ['quizid' => $this->_instance], 'mingrade DESC');
352
            $numfeedbacks = count($this->_feedbacks);
353
        } else {
354
            $this->_feedbacks = [];
355
            $numfeedbacks = $quizconfig->initialnumfeedbacks;
356
        }
357
        $numfeedbacks = max($numfeedbacks, 1);
358
 
359
        $nextel = $this->repeat_elements($repeatarray, $numfeedbacks - 1,
360
                $repeatedoptions, 'boundary_repeats', 'boundary_add_fields', 3,
361
                get_string('addmoreoverallfeedbacks', 'quiz'), true);
362
 
363
        // Put some extra elements in before the button.
364
        $mform->insertElementBefore($mform->createElement('editor',
365
                "feedbacktext[$nextel]", get_string('feedback', 'quiz'), ['rows' => 3],
366
                ['maxfiles' => EDITOR_UNLIMITED_FILES, 'noclean' => true,
367
                      'context' => $this->context]),
368
                'boundary_add_fields');
369
        $mform->insertElementBefore($mform->createElement('static',
370
                'gradeboundarystatic2', get_string('gradeboundary', 'quiz'), '0%'),
371
                'boundary_add_fields');
372
 
373
        // Add the disabledif rules. We cannot do this using the $repeatoptions parameter to
374
        // repeat_elements because we don't want to dissable the first feedbacktext.
375
        for ($i = 0; $i < $nextel; $i++) {
376
            $mform->disabledIf('feedbackboundaries[' . $i . ']', 'grade', 'eq', 0);
377
            $mform->disabledIf('feedbacktext[' . ($i + 1) . ']', 'grade', 'eq', 0);
378
        }
379
 
380
        // -------------------------------------------------------------------------------
381
        $this->standard_coursemodule_elements();
382
 
383
        // Check and act on whether setting outcomes is considered an advanced setting.
384
        $mform->setAdvanced('modoutcomes', !empty($quizconfig->outcomes_adv));
385
 
386
        // The standard_coursemodule_elements method sets this to 100, but the
387
        // quiz has its own setting, so use that.
388
        $mform->setDefault('grade', $quizconfig->maximumgrade);
389
 
390
        // -------------------------------------------------------------------------------
391
        $this->apply_admin_defaults();
392
        $this->add_action_buttons();
393
 
394
        $PAGE->requires->yui_module('moodle-mod_quiz-modform', 'M.mod_quiz.modform.init');
395
    }
396
 
397
    protected function add_review_options_group($mform, $quizconfig, $whenname,
398
            $when, $withhelp = false) {
399
        global $OUTPUT;
400
 
401
        $group = [];
402
        foreach (self::$reviewfields as $field => $string) {
403
            list($identifier, $component) = $string;
404
 
405
            $label = get_string($identifier, $component);
406
            $group[] = $mform->createElement('html', html_writer::start_div('review_option_item'));
407
            $el = $mform->createElement('checkbox', $field . $whenname, '', $label);
408
            if ($withhelp) {
409
                $el->_helpbutton = $OUTPUT->render(new help_icon($identifier, $component));
410
            }
411
            $group[] = $el;
412
            $group[] = $mform->createElement('html', html_writer::end_div());
413
        }
414
        $mform->addGroup($group, $whenname . 'optionsgrp',
415
                get_string('review' . $whenname, 'quiz'), null, false);
416
 
417
        foreach (self::$reviewfields as $field => $notused) {
418
            $cfgfield = 'review' . $field;
419
            if ($quizconfig->$cfgfield & $when) {
420
                $mform->setDefault($field . $whenname, 1);
421
            } else {
422
                $mform->setDefault($field . $whenname, 0);
423
            }
424
        }
425
 
426
        $mform->disabledIf('marks' . $whenname, 'maxmarks' . $whenname);
427
        if ($whenname != 'during') {
428
            $mform->disabledIf('correctness' . $whenname, 'attempt' . $whenname);
429
            $mform->disabledIf('specificfeedback' . $whenname, 'attempt' . $whenname);
430
            $mform->disabledIf('generalfeedback' . $whenname, 'attempt' . $whenname);
431
            $mform->disabledIf('rightanswer' . $whenname, 'attempt' . $whenname);
432
        }
433
    }
434
 
435
    protected function preprocessing_review_settings(&$toform, $whenname, $when) {
436
        foreach (self::$reviewfields as $field => $notused) {
437
            $fieldname = 'review' . $field;
438
            if (array_key_exists($fieldname, $toform)) {
439
                $toform[$field . $whenname] = $toform[$fieldname] & $when;
440
            }
441
        }
442
    }
443
 
444
    public function data_preprocessing(&$toform) {
445
        if (isset($toform['grade'])) {
446
            // Convert to a real number, so we don't get 0.0000.
447
            $toform['grade'] = $toform['grade'] + 0;
448
        }
449
 
450
        if (count($this->_feedbacks)) {
451
            $key = 0;
452
            foreach ($this->_feedbacks as $feedback) {
453
                $draftid = file_get_submitted_draft_itemid('feedbacktext['.$key.']');
454
                $toform['feedbacktext['.$key.']']['text'] = file_prepare_draft_area(
455
                    $draftid,               // Draftid.
456
                    $this->context->id,     // Context.
457
                    'mod_quiz',             // Component.
458
                    'feedback',             // Filarea.
459
                    !empty($feedback->id) ? (int) $feedback->id : null, // Itemid.
460
                    null,
461
                    $feedback->feedbacktext // Text.
462
                );
463
                $toform['feedbacktext['.$key.']']['format'] = $feedback->feedbacktextformat;
464
                $toform['feedbacktext['.$key.']']['itemid'] = $draftid;
465
 
466
                if ($toform['grade'] == 0) {
467
                    // When a quiz is un-graded, there can only be one lot of
468
                    // feedback. If the quiz previously had a maximum grade and
469
                    // several lots of feedback, we must now avoid putting text
470
                    // into input boxes that are disabled, but which the
471
                    // validation will insist are blank.
472
                    break;
473
                }
474
 
475
                if ($feedback->mingrade > 0) {
476
                    $toform['feedbackboundaries['.$key.']'] =
477
                            round(100.0 * $feedback->mingrade / $toform['grade'], 6) . '%';
478
                }
479
                $key++;
480
            }
481
        }
482
 
483
        if (isset($toform['timelimit'])) {
484
            $toform['timelimitenable'] = $toform['timelimit'] > 0;
485
        }
486
 
487
        $this->preprocessing_review_settings($toform, 'during',
488
                display_options::DURING);
489
        $this->preprocessing_review_settings($toform, 'immediately',
490
                display_options::IMMEDIATELY_AFTER);
491
        $this->preprocessing_review_settings($toform, 'open',
492
                display_options::LATER_WHILE_OPEN);
493
        $this->preprocessing_review_settings($toform, 'closed',
494
                display_options::AFTER_CLOSE);
495
        $toform['attemptduring'] = true;
496
        $toform['overallfeedbackduring'] = false;
497
 
498
        // Password field - different in form to stop browsers that remember
499
        // passwords from getting confused.
500
        if (isset($toform['password'])) {
501
            $toform['quizpassword'] = $toform['password'];
502
            unset($toform['password']);
503
        }
504
 
505
        // Load any settings belonging to the access rules.
506
        if (!empty($toform['instance'])) {
507
            $accesssettings = access_manager::load_settings($toform['instance']);
508
            foreach ($accesssettings as $name => $value) {
509
                $toform[$name] = $value;
510
            }
511
        }
512
 
513
        $suffix = $this->get_suffix();
514
        $completionminattemptsel = 'completionminattempts' . $suffix;
515
        if (empty($toform[$completionminattemptsel])) {
516
            $toform[$completionminattemptsel] = 1;
517
        } else {
518
            $completionminattemptsenabledel = 'completionminattemptsenabled' . $suffix;
519
            $toform[$completionminattemptsenabledel] = $toform[$completionminattemptsel] > 0;
520
        }
521
    }
522
 
523
    /**
524
     * Allows module to modify the data returned by form get_data().
525
     * This method is also called in the bulk activity completion form.
526
     *
527
     * Only available on moodleform_mod.
528
     *
529
     * @param stdClass $data the form data to be modified.
530
     */
531
    public function data_postprocessing($data) {
532
        parent::data_postprocessing($data);
533
        if (!empty($data->completionunlocked)) {
534
            // Turn off completion settings if the checkboxes aren't ticked.
535
            $suffix = $this->get_suffix();
536
            $completion = $data->{'completion' . $suffix};
537
            $autocompletion = !empty($completion) && $completion == COMPLETION_TRACKING_AUTOMATIC;
538
            if (empty($data->{'completionminattemptsenabled' . $suffix}) || !$autocompletion) {
539
                $data->{'completionminattempts' . $suffix} = 0;
540
            }
541
        }
542
    }
543
 
544
    public function validation($data, $files) {
545
        $errors = parent::validation($data, $files);
546
 
547
        // Check open and close times are consistent.
548
        if ($data['timeopen'] != 0 && $data['timeclose'] != 0 &&
549
                $data['timeclose'] < $data['timeopen']) {
550
            $errors['timeclose'] = get_string('closebeforeopen', 'quiz');
551
        }
552
 
553
        // Check that the grace period is not too short.
554
        if ($data['overduehandling'] == 'graceperiod') {
555
            $graceperiodmin = get_config('quiz', 'graceperiodmin');
556
            if ($data['graceperiod'] <= $graceperiodmin) {
557
                $errors['graceperiod'] = get_string('graceperiodtoosmall', 'quiz', format_time($graceperiodmin));
558
            }
559
        }
560
 
561
        $suffix = $this->get_suffix();
562
        $completionminattemptsel = 'completionminattempts' . $suffix;
563
        if (!empty($data[$completionminattemptsel])) {
564
            if ($data['attempts'] > 0 && $data[$completionminattemptsel] > $data['attempts']) {
565
                $completionminattemptsgroupel = 'completionminattemptsgroup' . $suffix;
566
                $errors[$completionminattemptsgroupel] = get_string('completionminattemptserror', 'quiz');
567
            }
568
        }
569
 
570
        // Check the boundary value is a number or a percentage, and in range.
571
        $i = 0;
572
        while (!empty($data['feedbackboundaries'][$i] )) {
573
            $boundary = trim($data['feedbackboundaries'][$i]);
574
            if (strlen($boundary) > 0) {
575
                if ($boundary[strlen($boundary) - 1] == '%') {
576
                    $boundary = trim(substr($boundary, 0, -1));
577
                    if (is_numeric($boundary)) {
578
                        $boundary = $boundary * $data['grade'] / 100.0;
579
                    } else {
580
                        $errors["feedbackboundaries[$i]"] =
581
                                get_string('feedbackerrorboundaryformat', 'quiz', $i + 1);
582
                    }
583
                } else if (!is_numeric($boundary)) {
584
                    $errors["feedbackboundaries[$i]"] =
585
                            get_string('feedbackerrorboundaryformat', 'quiz', $i + 1);
586
                }
587
            }
588
            if (is_numeric($boundary) && $boundary <= 0 || $boundary >= $data['grade'] ) {
589
                $errors["feedbackboundaries[$i]"] =
590
                        get_string('feedbackerrorboundaryoutofrange', 'quiz', $i + 1);
591
            }
592
            if (is_numeric($boundary) && $i > 0 &&
593
                    $boundary >= $data['feedbackboundaries'][$i - 1]) {
594
                $errors["feedbackboundaries[$i]"] =
595
                        get_string('feedbackerrororder', 'quiz', $i + 1);
596
            }
597
            $data['feedbackboundaries'][$i] = $boundary;
598
            $i += 1;
599
        }
600
        $numboundaries = $i;
601
 
602
        // Check there is nothing in the remaining unused fields.
603
        if (!empty($data['feedbackboundaries'])) {
604
            for ($i = $numboundaries; $i < count($data['feedbackboundaries']); $i += 1) {
605
                if (!empty($data['feedbackboundaries'][$i] ) &&
606
                        trim($data['feedbackboundaries'][$i] ) != '') {
607
                    $errors["feedbackboundaries[$i]"] =
608
                            get_string('feedbackerrorjunkinboundary', 'quiz', $i + 1);
609
                }
610
            }
611
        }
612
        for ($i = $numboundaries + 1; $i < count($data['feedbacktext']); $i += 1) {
613
            if (!empty($data['feedbacktext'][$i]['text']) &&
614
                    trim($data['feedbacktext'][$i]['text'] ) != '') {
615
                $errors["feedbacktext[$i]"] =
616
                        get_string('feedbackerrorjunkinfeedback', 'quiz', $i + 1);
617
            }
618
        }
619
 
620
        // If CBM is involved, don't show the warning for grade to pass being larger than the maximum grade.
621
        if (($data['preferredbehaviour'] == 'deferredcbm') OR ($data['preferredbehaviour'] == 'immediatecbm')) {
622
            unset($errors['gradepass']);
623
        }
624
        // Any other rule plugins.
625
        $errors = access_manager::validate_settings_form_fields($errors, $data, $files, $this);
626
 
627
        return $errors;
628
    }
629
 
630
    /**
631
     * Display module-specific activity completion rules.
632
     * Part of the API defined by moodleform_mod
633
     * @return array Array of string IDs of added items, empty array if none
634
     */
635
    public function add_completion_rules() {
636
        $mform = $this->_form;
637
        $suffix = $this->get_suffix();
638
 
639
        $group = [];
640
        $completionminattemptsenabledel = 'completionminattemptsenabled' . $suffix;
641
        $group[] = $mform->createElement(
642
            'checkbox',
643
            $completionminattemptsenabledel,
644
            '',
645
            get_string('completionminattempts', 'quiz')
646
        );
647
        $completionminattemptsel = 'completionminattempts' . $suffix;
648
        $group[] = $mform->createElement('text', $completionminattemptsel, '', ['size' => 3]);
649
        $mform->setType($completionminattemptsel, PARAM_INT);
650
        $completionminattemptsgroupel = 'completionminattemptsgroup' . $suffix;
651
        $mform->addGroup($group, $completionminattemptsgroupel, '', ' ', false);
652
        $mform->hideIf($completionminattemptsel, $completionminattemptsenabledel, 'notchecked');
653
 
654
        return [$completionminattemptsgroupel];
655
    }
656
 
657
    /**
658
     * Add completion grading elements to the form and return the list of element ids.
659
     *
660
     * @return array Array of string IDs of added items, empty array if none
661
     */
662
    public function add_completiongrade_rules(): array {
663
        $mform = $this->_form;
664
        $suffix = $this->get_suffix();
665
 
666
        $completionattemptsexhaustedel = 'completionattemptsexhausted' . $suffix;
667
        $mform->addElement(
668
            'advcheckbox',
669
            $completionattemptsexhaustedel,
670
            null,
671
            get_string('completionattemptsexhausted', 'quiz'),
1441 ariadna 672
            ['group' => 'cattempts', 'parentclass' => 'ms-4']
1 efrain 673
        );
674
        $completionpassgradeel = 'completionpassgrade' . $suffix;
675
        $mform->hideIf($completionattemptsexhaustedel, $completionpassgradeel, 'notchecked');
676
        $mform->hideIf($completionattemptsexhaustedel, $completionpassgradeel, 'notchecked');
677
 
678
        return [$completionattemptsexhaustedel];
679
    }
680
 
681
    /**
682
     * Called during validation. Indicates whether a module-specific completion rule is selected.
683
     *
684
     * @param array $data Input data (not yet validated)
685
     * @return bool True if one or more rules is enabled, false if none are.
686
     */
687
    public function completion_rule_enabled($data) {
688
        $suffix = $this->get_suffix();
689
        return  !empty($data['completionattemptsexhausted' . $suffix]) ||
690
                !empty($data['completionminattemptsenabled' . $suffix]);
691
    }
692
 
693
    /**
694
     * Get the maximum number of attempts that anyone might have due to a user
695
     * or group override. Used to decide whether disabledIf rules should be applied.
696
     * @return int the number of attempts allowed. For the purpose of this method,
697
     * unlimited is returned as 1000, not 0.
698
     */
699
    public function get_max_attempts_for_any_override() {
700
        global $DB;
701
 
702
        if (empty($this->_instance)) {
703
            // Quiz not created yet, so no overrides.
704
            return 1;
705
        }
706
 
707
        if ($this->maxattemptsanyoverride === null) {
708
            $this->maxattemptsanyoverride = $DB->get_field_sql("
709
                    SELECT MAX(CASE WHEN attempts = 0 THEN 1000 ELSE attempts END)
710
                      FROM {quiz_overrides}
711
                     WHERE quiz = ?",
712
                    [$this->_instance]);
713
            if ($this->maxattemptsanyoverride < 1) {
714
                // This happens when no override alters the number of attempts.
715
                $this->maxattemptsanyoverride = 1;
716
            }
717
        }
718
 
719
        return $this->maxattemptsanyoverride;
720
    }
721
}