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 questiontype class for the multiple choice question type.
19
 *
20
 * @package    qtype
21
 * @subpackage multichoice
22
 * @copyright  1999 onwards Martin Dougiamas  {@link http://moodle.com}
23
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24
 */
25
 
26
 
27
defined('MOODLE_INTERNAL') || die();
28
 
29
global $CFG;
30
require_once($CFG->libdir . '/questionlib.php');
31
 
32
 
33
/**
34
 * The multiple choice question type.
35
 *
36
 * @copyright  1999 onwards Martin Dougiamas  {@link http://moodle.com}
37
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
38
 */
39
class qtype_multichoice extends question_type {
40
    /**
41
     * @var int a special value that can be set for {@see question_display_options::$feedback}.
42
     *
43
     * This is not used by the core question type, but is used by some variants of this question
44
     * types in the plugins database, including qtype_oumultiresponse and qtype_answersselect.
45
     *
46
     * If ->feedback is set to this value, then the renderer will display the combined feebdack,
47
     * but not the feedback for each specific choice.
48
     */
49
    const COMBINED_BUT_NOT_CHOICE_FEEDBACK = 0x100;
50
 
51
    /**
52
     * Helper to catch and update if a plugin is using the old version of the COMBINED_BUT_NOT_CHOICE_FEEDBACK thing.
53
     *
54
     * @param question_display_options $options to be updated before being used.
55
     */
56
    public static function support_legacy_review_options_hack(question_display_options $options): void {
57
        if (empty($options->suppresschoicefeedback)) {
58
            return; // Nothing to do.
59
        }
60
 
61
        debugging('$options->suppresschoicefeedback should no longer be used. To get a similar effect, ' .
62
            'instead set $options->feedback = $options->feedback && qtype_multichoice::COMBINED_BUT_NOT_CHOICE_FEEDBACK.');
63
        if ($options->feedback) {
64
            $options->feedback = self::COMBINED_BUT_NOT_CHOICE_FEEDBACK;
65
        }
66
        unset($options->suppresschoicefeedback);
67
    }
68
 
69
    public function get_question_options($question) {
70
        global $DB, $OUTPUT;
71
 
72
        $question->options = $DB->get_record('qtype_multichoice_options', ['questionid' => $question->id]);
73
 
74
        if ($question->options === false) {
75
            // If this has happened, then we have a problem.
76
            // For the user to be able to edit or delete this question, we need options.
77
            debugging("Question ID {$question->id} was missing an options record. Using default.", DEBUG_DEVELOPER);
78
 
79
            $question->options = $this->create_default_options($question);
80
        }
81
 
82
        parent::get_question_options($question);
83
    }
84
 
85
    /**
86
     * Create a default options object for the provided question.
87
     *
88
     * @param object $question The queston we are working with.
89
     * @return object The options object.
90
     */
91
    protected function create_default_options($question) {
92
        // Create a default question options record.
93
        $options = new stdClass();
94
        $options->questionid = $question->id;
95
 
96
        // Get the default strings and just set the format.
97
        $options->correctfeedback = get_string('correctfeedbackdefault', 'question');
98
        $options->correctfeedbackformat = FORMAT_HTML;
99
        $options->partiallycorrectfeedback = get_string('partiallycorrectfeedbackdefault', 'question');;
100
        $options->partiallycorrectfeedbackformat = FORMAT_HTML;
101
        $options->incorrectfeedback = get_string('incorrectfeedbackdefault', 'question');
102
        $options->incorrectfeedbackformat = FORMAT_HTML;
103
 
104
        $config = get_config('qtype_multichoice');
105
        $options->single = $config->answerhowmany;
106
        if (isset($question->layout)) {
107
            $options->layout = $question->layout;
108
        }
109
        $options->answernumbering = $config->answernumbering;
110
        $options->shuffleanswers = $config->shuffleanswers;
111
        $options->showstandardinstruction = 0;
112
        $options->shownumcorrect = 1;
113
 
114
        return $options;
115
    }
116
 
117
    public function save_defaults_for_new_questions(stdClass $fromform): void {
118
        parent::save_defaults_for_new_questions($fromform);
119
        $this->set_default_value('single', $fromform->single);
120
        $this->set_default_value('shuffleanswers', $fromform->shuffleanswers);
121
        $this->set_default_value('answernumbering', $fromform->answernumbering);
122
        $this->set_default_value('showstandardinstruction', $fromform->showstandardinstruction);
123
    }
124
 
125
    public function save_question_options($question) {
126
        global $DB;
127
        $context = $question->context;
128
        $result = new stdClass();
129
 
130
        $oldanswers = $DB->get_records('question_answers',
131
                array('question' => $question->id), 'id ASC');
132
 
133
        // Following hack to check at least two answers exist.
134
        $answercount = 0;
135
        foreach ($question->answer as $key => $answer) {
136
            if ($answer != '') {
137
                $answercount++;
138
            }
139
        }
140
        if ($answercount < 2) { // Check there are at lest 2 answers for multiple choice.
141
            $result->error = get_string('notenoughanswers', 'qtype_multichoice', '2');
142
            return $result;
143
        }
144
 
145
        // Insert all the new answers.
146
        $totalfraction = 0;
147
        $maxfraction = -1;
148
        foreach ($question->answer as $key => $answerdata) {
149
            if (trim($answerdata['text']) == '') {
150
                continue;
151
            }
152
 
153
            // Update an existing answer if possible.
154
            $answer = array_shift($oldanswers);
155
            if (!$answer) {
156
                $answer = new stdClass();
157
                $answer->question = $question->id;
158
                $answer->answer = '';
159
                $answer->feedback = '';
160
                $answer->id = $DB->insert_record('question_answers', $answer);
161
            }
162
 
163
            // Doing an import.
164
            $answer->answer = $this->import_or_save_files($answerdata,
165
                    $context, 'question', 'answer', $answer->id);
166
            $answer->answerformat = $answerdata['format'];
167
            $answer->fraction = $question->fraction[$key];
168
            $answer->feedback = $this->import_or_save_files($question->feedback[$key],
169
                    $context, 'question', 'answerfeedback', $answer->id);
170
            $answer->feedbackformat = $question->feedback[$key]['format'];
171
 
172
            $DB->update_record('question_answers', $answer);
173
 
174
            if ($question->fraction[$key] > 0) {
175
                $totalfraction += $question->fraction[$key];
176
            }
177
            if ($question->fraction[$key] > $maxfraction) {
178
                $maxfraction = $question->fraction[$key];
179
            }
180
        }
181
 
182
        // Delete any left over old answer records.
183
        $fs = get_file_storage();
184
        foreach ($oldanswers as $oldanswer) {
185
            $fs->delete_area_files($context->id, 'question', 'answerfeedback', $oldanswer->id);
186
            $DB->delete_records('question_answers', array('id' => $oldanswer->id));
187
        }
188
 
189
        $options = $DB->get_record('qtype_multichoice_options', array('questionid' => $question->id));
190
        if (!$options) {
191
            $options = new stdClass();
192
            $options->questionid = $question->id;
193
            $options->correctfeedback = '';
194
            $options->partiallycorrectfeedback = '';
195
            $options->incorrectfeedback = '';
196
            $options->showstandardinstruction = 0;
197
            $options->id = $DB->insert_record('qtype_multichoice_options', $options);
198
        }
199
 
200
        $options->single = $question->single;
201
        if (isset($question->layout)) {
202
            $options->layout = $question->layout;
203
        }
204
        $options->answernumbering = $question->answernumbering;
205
        $options->shuffleanswers = $question->shuffleanswers;
206
        $options->showstandardinstruction = !empty($question->showstandardinstruction);
207
        $options = $this->save_combined_feedback_helper($options, $question, $context, true);
208
        $DB->update_record('qtype_multichoice_options', $options);
209
 
210
        $this->save_hints($question, true);
211
 
212
        // Perform sanity checks on fractional grades.
213
        if ($options->single) {
214
            if ($maxfraction != 1) {
215
                $result->noticeyesno = get_string('fractionsnomax', 'qtype_multichoice',
216
                        $maxfraction * 100);
217
                return $result;
218
            }
219
        } else {
220
            $totalfraction = round($totalfraction, 2);
221
            if ($totalfraction != 1) {
222
                $result->noticeyesno = get_string('fractionsaddwrong', 'qtype_multichoice',
223
                        $totalfraction * 100);
224
                return $result;
225
            }
226
        }
227
    }
228
 
229
    protected function make_question_instance($questiondata) {
230
        question_bank::load_question_definition_classes($this->name());
231
        if ($questiondata->options->single) {
232
            $class = 'qtype_multichoice_single_question';
233
        } else {
234
            $class = 'qtype_multichoice_multi_question';
235
        }
236
        return new $class();
237
    }
238
 
239
    protected function make_hint($hint) {
240
        return question_hint_with_parts::load_from_record($hint);
241
    }
242
 
243
    protected function initialise_question_instance(question_definition $question, $questiondata) {
244
        parent::initialise_question_instance($question, $questiondata);
245
        $question->shuffleanswers = $questiondata->options->shuffleanswers;
246
        $question->answernumbering = $questiondata->options->answernumbering;
247
        $question->showstandardinstruction = $questiondata->options->showstandardinstruction;
248
        if (!empty($questiondata->options->layout)) {
249
            $question->layout = $questiondata->options->layout;
250
        } else {
251
            $question->layout = qtype_multichoice_single_question::LAYOUT_VERTICAL;
252
        }
253
        $this->initialise_combined_feedback($question, $questiondata, true);
254
 
255
        $this->initialise_question_answers($question, $questiondata, false);
256
    }
257
 
258
    public function make_answer($answer) {
259
        // Overridden just so we can make it public for use by question.php.
260
        return parent::make_answer($answer);
261
    }
262
 
263
    public function delete_question($questionid, $contextid) {
264
        global $DB;
265
        $DB->delete_records('qtype_multichoice_options', array('questionid' => $questionid));
266
 
267
        parent::delete_question($questionid, $contextid);
268
    }
269
 
270
    public function get_random_guess_score($questiondata) {
271
        if (!$questiondata->options->single) {
272
            // Pretty much impossible to compute for _multi questions. Don't try.
273
            return null;
274
        }
275
 
276
        if (empty($questiondata->options->answers)) {
277
            // A multi-choice question with no choices is senseless,
278
            // but, seemingly, it can happen (presumably as a side-effect of bugs).
279
            // Therefore, ensure it does not lead to errors here.
280
            return null;
281
        }
282
 
283
        // Single choice questions - average choice fraction.
284
        $totalfraction = 0;
285
        foreach ($questiondata->options->answers as $answer) {
286
            $totalfraction += $answer->fraction;
287
        }
288
        return $totalfraction / count($questiondata->options->answers);
289
    }
290
 
291
    public function get_possible_responses($questiondata) {
292
        if ($questiondata->options->single) {
293
            $responses = array();
294
 
295
            foreach ($questiondata->options->answers as $aid => $answer) {
296
                $responses[$aid] = new question_possible_response(
297
                        question_utils::to_plain_text($answer->answer, $answer->answerformat),
298
                        $answer->fraction);
299
            }
300
 
301
            $responses[null] = question_possible_response::no_response();
302
            return array($questiondata->id => $responses);
303
        } else {
304
            $parts = array();
305
 
306
            foreach ($questiondata->options->answers as $aid => $answer) {
307
                $parts[$aid] = array($aid => new question_possible_response(
308
                        question_utils::to_plain_text($answer->answer, $answer->answerformat),
309
                        $answer->fraction));
310
            }
311
 
312
            return $parts;
313
        }
314
    }
315
 
316
    /**
317
     * @return array of the numbering styles supported. For each one, there
318
     *      should be a lang string answernumberingxxx in teh qtype_multichoice
319
     *      language file, and a case in the switch statement in number_in_style,
320
     *      and it should be listed in the definition of this column in install.xml.
321
     */
322
    public static function get_numbering_styles() {
323
        $styles = array();
324
        foreach (array('abc', 'ABCD', '123', 'iii', 'IIII', 'none') as $numberingoption) {
325
            $styles[$numberingoption] =
326
                    get_string('answernumbering' . $numberingoption, 'qtype_multichoice');
327
        }
328
        return $styles;
329
    }
330
 
331
    public function move_files($questionid, $oldcontextid, $newcontextid) {
332
        parent::move_files($questionid, $oldcontextid, $newcontextid);
333
        $this->move_files_in_answers($questionid, $oldcontextid, $newcontextid, true);
334
        $this->move_files_in_combined_feedback($questionid, $oldcontextid, $newcontextid);
335
        $this->move_files_in_hints($questionid, $oldcontextid, $newcontextid);
336
    }
337
 
338
    protected function delete_files($questionid, $contextid) {
339
        parent::delete_files($questionid, $contextid);
340
        $this->delete_files_in_answers($questionid, $contextid, true);
341
        $this->delete_files_in_combined_feedback($questionid, $contextid);
342
        $this->delete_files_in_hints($questionid, $contextid);
343
    }
344
}