edit_form_base.php 10.5 KB
Newer Older
1
<?php
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.

/**
 * Base class for editing question types like this one.
 *
20
 * @package    qtype_gapselect
21
22
 * @copyright  2011 The Open University
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23
24
 */

25
26
27
defined('MOODLE_INTERNAL') || die();


28
29
30
/**
 * Elements embedded in question text editing form definition.
 *
31
32
 * @copyright  2011 The Open University
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
33
34
35
36
37
38
39
40
41
42
 */
class qtype_gapselect_edit_form_base extends question_edit_form {

    /** @var array of HTML tags allowed in choices / drag boxes. */
    protected $allowedhtmltags = array(
        'sub',
        'sup',
        'b',
        'i',
        'em',
43
44
        'strong',
        'span',
45
46
47
    );

    /** @var string regex to match HTML open tags. */
48
    private $htmltstarttagsandattributes = '~<\s*\w+\b[^>]*>~';
49
50

    /** @var string regex to match HTML close tags or br. */
51
    private $htmltclosetags = '~<\s*/\s*\w+\b[^>]*>~';
52
53

    /** @var string regex to select text like [[cat]] (including the square brackets). */
54
    private $squarebracketsregex = '/\[\[[^]]*?\]\]/';
55

56
57
58
    /**
     * Vaidate some input to make sure it does not contain any tags other than
     * $this->allowedhtmltags.
59
     * @param string $text the input to validate.
60
61
62
63
64
     * @return string any validation errors.
     */
    protected function get_illegal_tag_error($text) {
        // Remove legal tags.
        $strippedtext = $text;
65
        foreach ($this->allowedhtmltags as $htmltag) {
66
67
            $tagpair = "~<\s*/?\s*$htmltag\b\s*[^>]*>~";
            $strippedtext = preg_replace($tagpair, '', $strippedtext);
68
        }
69

70
71
        $textarray = array();
        preg_match_all($this->htmltstarttagsandattributes, $strippedtext, $textarray);
72
        if ($textarray[0]) {
73
            return $this->allowed_tags_message($textarray[0][0]);
74
        }
75

76
        preg_match_all($this->htmltclosetags, $strippedtext, $textarray);
77
        if ($textarray[0]) {
78
            return $this->allowed_tags_message($textarray[0][0]);
79
        }
80

81
82
83
        return '';
    }

84
85
86
87
88
89
    /**
     * Returns a message indicating what tags are allowed.
     *
     * @param string $badtag The disallowed tag that was supplied
     * @return string Message indicating what tags are allowed
     */
90
91
92
93
94
95
96
97
98
    private function allowed_tags_message($badtag) {
        $a = new stdClass();
        $a->tag = htmlspecialchars($badtag);
        $a->allowed = $this->get_list_of_printable_allowed_tags($this->allowedhtmltags);
        if ($a->allowed) {
            return get_string('tagsnotallowed', 'qtype_gapselect', $a);
        } else {
            return get_string('tagsnotallowedatall', 'qtype_gapselect', $a);
        }
99
100
    }

101
102
103
104
105
106
    /**
     * Returns a prinatble list of allowed HTML tags.
     *
     * @param array $allowedhtmltags An array for tag strings that are allowed
     * @return string A printable list of tags
     */
107
    private function get_list_of_printable_allowed_tags($allowedhtmltags) {
108
        $allowedtaglist = array();
109
        foreach ($allowedhtmltags as $htmltag) {
110
            $allowedtaglist[] = htmlspecialchars('<' . $htmltag . '>');
111
        }
112
        return implode(', ', $allowedtaglist);
113
114
115
116
117
118
    }

    /**
     * definition_inner adds all specific fields to the form.
     * @param object $mform (the form being built).
     */
119
    protected function definition_inner($mform) {
120
121
        global $CFG;

122
        // Add the answer (choice) fields to the form.
123
124
125
126
127
128
        $this->definition_answer_choice($mform);

        $this->add_combined_feedback_fields(true);
        $this->add_interactive_settings(true, true);
    }

129
130
131
132
133
    /**
     * Defines form elements for answer choices.
     *
     * @param object $mform The Moodle form object being built
     */
134
    protected function definition_answer_choice(&$mform) {
135
        $mform->addElement('header', 'choicehdr', get_string('choices', 'qtype_gapselect'));
136
        $mform->setExpanded('choicehdr', 1);
137

138
        $mform->addElement('checkbox', 'shuffleanswers', get_string('shuffle', 'qtype_gapselect'));
139
        $mform->setDefault('shuffleanswers', $this->get_default_value('shuffleanswers', 0));
140
141

        $textboxgroup = array();
142
143
        $textboxgroup[] = $mform->createElement('group', 'choices',
                get_string('choicex', 'qtype_gapselect'), $this->choice_group($mform));
144
145
146
147
148
149
150
151

        if (isset($this->question->options)) {
            $countanswers = count($this->question->options->answers);
        } else {
            $countanswers = 0;
        }

        if ($this->question->formoptions->repeatelements) {
152
            $defaultstartnumbers = QUESTION_NUMANS_START * 2;
153
154
            $repeatsatstart = max($defaultstartnumbers, QUESTION_NUMANS_START,
                    $countanswers + QUESTION_NUMANS_ADD);
155
156
157
158
159
160
        } else {
            $repeatsatstart = $countanswers;
        }

        $repeatedoptions = $this->repeated_options();
        $mform->setType('answer', PARAM_RAW);
161
162
        $this->repeat_elements($textboxgroup, $repeatsatstart, $repeatedoptions,
                'noanswers', 'addanswers', QUESTION_NUMANS_ADD,
163
                get_string('addmorechoiceblanks', 'qtype_gapselect'), true);
164
165
    }

166
167
168
169
170
171
172
173
174
    /**
     * Return how many different groups of choices there should be.
     *
     * @return int the maximum group number.
     */
    function get_maximum_choice_group_number() {
        return 8;
    }

175
176
177
178
    /**
     * Creates an array with elements for a choice group.
     *
     * @param object $mform The Moodle form we are working with
179
     * @param int $maxgroup The number of max group generate element select.
180
181
     * @return array Array for form elements
     */
182
183
    protected function choice_group($mform) {
        $options = array();
184
        for ($i = 1; $i <= $this->get_maximum_choice_group_number(); $i += 1) {
185
            $options[$i] = question_utils::int_to_letter($i);
186
187
        }
        $grouparray = array();
188
        $grouparray[] = $mform->createElement('text', 'answer',
189
                get_string('answer', 'qtype_gapselect'), array('size' => 30, 'class' => 'tweakcss'));
190
191
        $grouparray[] = $mform->createElement('select', 'choicegroup',
                get_string('group', 'qtype_gapselect'), $options);
192
193
        return $grouparray;
    }
194

195
196
197
198
199
    /**
     * Returns an array for form repeat options.
     *
     * @return array Array of repeate options
     */
200
201
202
    protected function repeated_options() {
        $repeatedoptions = array();
        $repeatedoptions['choicegroup']['default'] = '1';
203
        $repeatedoptions['choices[answer]']['type'] = PARAM_RAW;
204
205
        return $repeatedoptions;
    }
206

207
208
209
210
211
212
213
214
215
216
217
    public function data_preprocessing($question) {
        $question = parent::data_preprocessing($question);
        $question = $this->data_preprocessing_combined_feedback($question, true);
        $question = $this->data_preprocessing_hints($question, true, true);

        $question = $this->data_preprocessing_answers($question, true);
        if (!empty($question->options->answers)) {
            $key = 0;
            foreach ($question->options->answers as $answer) {
                $question = $this->data_preprocessing_choice($question, $answer, $key);
                $key++;
218
            }
219
        }
220

221
222
        if (!empty($question->options)) {
            $question->shuffleanswers = $question->options->shuffleanswers;
223
        }
224
225
226
227
228
229
230
231

        return $question;
    }

    protected function data_preprocessing_choice($question, $answer, $key) {
        $question->choices[$key]['answer'] = $answer->answer;
        $question->choices[$key]['choicegroup'] = $answer->feedback;
        return $question;
232
233
234
235
236
237
238
    }

    public function validation($data, $files) {
        $errors = parent::validation($data, $files);
        $questiontext = $data['questiontext'];
        $choices = $data['choices'];

239
        // Check the whether the slots are valid.
240
        $errorsinquestiontext = $this->validate_slots($questiontext['text'], $choices);
241
242
243
244
245
246
        if ($errorsinquestiontext) {
            $errors['questiontext'] = $errorsinquestiontext;
        }
        foreach ($choices as $key => $choice) {
            $answer = $choice['answer'];

247
            // Check whether the HTML tags are allowed tags.
248
249
250
            $tagerror = $this->get_illegal_tag_error($answer);
            if ($tagerror) {
                $errors['choices['.$key.']'] = $tagerror;
251
252
253
254
255
            }
        }
        return $errors;
    }

256
257
258
259
260
261
262
    /**
     * Finds errors in question slots.
     *
     * @param string $questiontext The question text
     * @param array $choices Question choices
     * @return string|bool Error message or false if no errors
     */
263
264
265
    private function validate_slots($questiontext, $choices) {
        $error = 'Please check the Question text: ';
        if (!$questiontext) {
266
            return get_string('errorquestiontextblank', 'qtype_gapselect');
267
268
269
        }

        $matches = array();
270
        preg_match_all($this->squarebracketsregex, $questiontext, $matches);
271
272
273
        $slots = $matches[0];

        if (!$slots) {
274
            return get_string('errornoslots', 'qtype_gapselect');
275
276
        }

277
        $cleanedslots = array();
278
279
        foreach ($slots as $slot) {
            // The 2 is for'[[' and 4 is for '[[]]'.
280
            $cleanedslots[] = substr($slot, 2, (strlen($slot) - 4));
281
        }
282
        $slots = $cleanedslots;
283
284
285
286
287
288

        $found = false;
        foreach ($slots as $slot) {
            $found = false;
            foreach ($choices as $key => $choice) {
                if ($slot == $key + 1) {
289
290
291
                    if ($choice['answer'] === '') {
                        return get_string('errorblankchoice', 'qtype_gapselect',
                                html_writer::tag('b', $slot));
292
293
294
295
296
297
                    }
                    $found = true;
                    break;
                }
            }
            if (!$found) {
298
299
                return get_string('errormissingchoice', 'qtype_gapselect',
                        html_writer::tag('b', $slot));
300
301
302
303
            }
        }
        return false;
    }
304

305
    public function qtype() {
306
307
        return '';
    }
308
}