Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1441 ariadna 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
defined('MOODLE_INTERNAL') || die();
18
 
19
global $CFG;
20
require_once($CFG->dirroot . '/course/moodleform_mod.php');
21
 
22
/**
23
 * mod_qbank settings form definition.
24
 *
25
 * @package    mod_qbank
26
 * @copyright  2024 onwards Catalyst IT EU {@link https://catalyst-eu.net}
27
 * @author     Simon Adams <simon.adams@catalyst-eu.net>
28
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
29
 */
30
class mod_qbank_mod_form extends moodleform_mod {
31
    #[\Override]
32
    protected function definition(): void {
33
        global $CFG;
34
 
35
        $mform = $this->_form;
36
        $striptags = !empty($CFG->formatstringstriptags);
37
        $this->standard_hidden_coursemodule_elements();
38
 
39
        // We need to force visibility on this here as we don't need the other standard course elements.
40
        $mform->addElement('hidden', 'visible', 0);
41
        $mform->setType('visible', PARAM_INT);
42
 
43
        $mform->addElement('hidden', 'type');
44
        $mform->setDefaults('type', \core_question\local\bank\question_bank_helper::TYPE_STANDARD);
45
        $mform->setType('type', PARAM_TEXT);
46
 
47
        $mform->addElement('header', 'generalhdr', get_string('general'));
48
 
49
        // Add element for name.
50
        $mform->addElement('text', 'name', get_string('qbankname', 'mod_qbank'), ['size' => '64']);
51
        if ($striptags) {
52
            $mform->setType('name', PARAM_TEXT);
53
        } else {
54
            $mform->setType('name', PARAM_CLEANHTML);
55
        }
56
        $mform->addHelpButton('name', 'qbankname', 'mod_qbank');
57
        $mform->addRule('name', null, 'required', null, 'client');
58
        $mform->addRule('name', null, 'maxlength', \core_question\local\bank\question_bank_helper::BANK_NAME_MAX_LENGTH, 'client');
59
 
60
        // Add intro editor.
61
        $mform->addElement('editor', 'introeditor', get_string('moduleintro'), ['rows' => 10], [
62
            'maxfiles' => EDITOR_UNLIMITED_FILES,
63
            'noclean' => true,
64
            'context' => $this->context,
65
            'subdirs' => true,
66
        ]);
67
        $mform->setType('introeditor', PARAM_RAW); // No XSS prevention here, users must be trusted.
68
        if ($CFG->requiremodintro) {
69
            $mform->addRule('introeditor', get_string('required'), 'required', null, 'client');
70
        }
71
 
72
        // Add show description checkbox.
73
        $mform->addElement('advcheckbox', 'showdescription', get_string('showdescription', 'mod_qbank'));
74
        $mform->addHelpButton('showdescription', 'showdescription', 'mod_qbank');
75
 
76
        $mform->addElement('header', 'modstandardelshdr', get_string('modstandardels', 'form'));
77
 
78
        // Add idnumber element.
79
        $mform->addElement('text', 'cmidnumber', get_string('idnumbermod'));
80
        if ($striptags) {
81
            $mform->setType('cmidnumber', PARAM_TEXT);
82
        } else {
83
            $mform->setType('cmidnumber', PARAM_CLEANHTML);
84
        }
85
        $mform->addHelpButton('cmidnumber', 'idnumbermod');
86
 
87
        // Add our submission buttons.
88
        $buttonarray[] = $mform->createElement('submit', 'submitbutton2', get_string('saveandreturn', 'mod_qbank'));
89
        $buttonarray[] = $mform->createElement('submit', 'submitbutton', get_string('saveanddisplay', 'mod_qbank'));
90
        $buttonarray[] = $mform->createElement('cancel');
91
 
92
        $mform->addGroup($buttonarray, 'buttonar', '', [' '], false);
93
        $mform->setType('buttonar', PARAM_RAW);
94
    }
95
 
96
    #[\Override]
97
    public function validation($data, $files): array {
98
        global $DB;
99
        $mform = $this->_form;
100
 
101
        // We don't want the parent validation as it has completion settings which we don't use.
102
        // Best call the grandparent though in case it changes in the future.
103
        $errors = moodleform::validation($data, $files);
104
 
105
        if ($mform->elementExists('name')) {
106
            $name = trim($data['name']);
107
            if ($name === '') {
108
                $errors['name'] = get_string('required');
109
            }
110
        }
111
 
112
        if (!empty($data['cmidnumber'])) {
113
            $idnumexists = $DB->record_exists_select(
114
                'course_modules',
115
                'id <> :id AND course = :course AND idnumber = :idnumber',
116
                ['course' => $data['course'], 'idnumber' => $data['cmidnumber'], 'id' => $data['coursemodule'] ?? null]
117
            );
118
            if ($idnumexists) {
119
                $errors['cmidnumber'] = get_string('idnumbertaken');
120
            }
121
        }
122
 
123
        if (!empty($data['type']) && !in_array($data['type'], core_question\local\bank\question_bank_helper::SHARED_TYPES, true)) {
124
            $errors['type'] = get_string('unknownbanktype', 'mod_qbank', $data['type']);
125
        }
126
 
127
        return $errors;
128
    }
129
}