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
 * Test helpers for the calculated question type.
19
 *
20
 * @package    qtype
21
 * @subpackage calculated
22
 * @copyright  2011 The Open University
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->dirroot . '/question/type/calculated/question.php');
31
require_once($CFG->dirroot . '/question/type/numerical/question.php');
32
require_once($CFG->dirroot . '/question/type/numerical/questiontype.php');
33
require_once($CFG->dirroot . '/question/engine/tests/helpers.php');
34
 
35
 
36
/**
37
 * Test helper class for the calculated question type.
38
 *
39
 * @copyright  2011 The Open University
40
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
41
 */
42
class qtype_calculated_test_helper extends question_test_helper {
43
    public function get_test_questions() {
44
        return ['sum', 'mult'];
45
    }
46
 
47
    /**
48
     * Makes a calculated question about summing two numbers.
49
     * @return qtype_calculated_question
50
     */
51
    public function make_calculated_question_sum() {
52
        question_bank::load_question_definition_classes('calculated');
53
        $q = new qtype_calculated_question();
54
        test_question_maker::initialise_a_question($q);
55
        $q->name = 'Simple sum';
56
        $q->questiontext = 'What is {a} + {b}?';
57
        $q->generalfeedback = 'Generalfeedback: {={a} + {b}} is the right answer.';
58
 
59
        $q->answers = [
60
            13 => new \qtype_calculated\qtype_calculated_answer(13, '{a} + {b}', 1.0, 'Very good.', FORMAT_HTML, 0),
61
            14 => new \qtype_calculated\qtype_calculated_answer(14, '{a} - {b}', 0.0, 'Add. not subtract!.', FORMAT_HTML, 0),
62
            17 => new \qtype_calculated\qtype_calculated_answer(17, '*', 0.0, 'Completely wrong.', FORMAT_HTML, 0),
63
        ];
64
        foreach ($q->answers as $answer) {
65
            $answer->correctanswerlength = 2;
66
            $answer->correctanswerformat = 1;
67
        }
68
 
69
        $q->qtype = question_bank::get_qtype('calculated');
70
        $q->unitdisplay = qtype_numerical::UNITNONE;
71
        $q->unitgradingtype = 0;
72
        $q->unitpenalty = 0;
73
        $q->ap = new qtype_numerical_answer_processor([]);
74
        $q->synchronised = false;
75
 
76
        $q->datasetloader = new qtype_calculated_test_dataset_loader(0, [
77
            ['a' => 1, 'b' => 5],
78
            ['a' => 3, 'b' => 4],
79
            ['a' => 3, 'b' => 0.01416],
80
            ['a' => 31, 'b' => 0.01416],
81
        ]);
82
 
83
        return $q;
84
    }
85
 
86
    /**
87
     * Makes a calculated question about summing two numbers.
88
     * @return qtype_calculated_question
89
     */
90
    public function get_calculated_question_data_sum() {
91
        question_bank::load_question_definition_classes('calculated');
92
        $qdata = new stdClass();
93
        test_question_maker::initialise_question_data($qdata);
94
 
95
        $qdata->qtype = 'calculated';
96
        $qdata->name = 'Simple sum';
97
        $qdata->questiontext = 'What is {a} + {b}?';
98
        $qdata->generalfeedback = 'Generalfeedback: {={a} + {b}} is the right answer.';
99
        $qdata->status = \core_question\local\bank\question_version_status::QUESTION_STATUS_READY;
100
 
101
        $qdata->options = new stdClass();
102
        $qdata->options->unitgradingtype = 0;
103
        $qdata->options->unitpenalty = 0.0;
104
        $qdata->options->showunits = qtype_numerical::UNITNONE;
105
        $qdata->options->unitsleft = 0;
106
        $qdata->options->synchronize = 0;
107
 
108
        $qdata->options->answers = [
109
            13 => new \qtype_calculated\qtype_calculated_answer(13, '{a} + {b}', 1.0, 'Very good.', FORMAT_HTML, 0.001),
110
            14 => new \qtype_calculated\qtype_calculated_answer(14, '{a} - {b}', 0.0, 'Add. not subtract!.', FORMAT_HTML, 0.001),
111
            17 => new \qtype_calculated\qtype_calculated_answer(17, '*', 0.0, 'Completely wrong.', FORMAT_HTML, 0),
112
        ];
113
        foreach ($qdata->options->answers as $answer) {
114
            $answer->correctanswerlength = 2;
115
            $answer->correctanswerformat = 1;
116
        }
117
 
118
        $qdata->options->units = [];
119
 
120
        return $qdata;
121
    }
122
 
123
    /**
124
     * Makes a calculated question about summing two numbers.
125
     * @return qtype_calculated_question
126
     */
127
    public function get_calculated_question_form_data_sum() {
128
        question_bank::load_question_definition_classes('calculated');
129
        $fromform = new stdClass();
130
 
131
        $fromform->name = 'Simple sum';
132
        $fromform->questiontext = 'What is {a} + {b}?';
133
        $fromform->defaultmark = 1.0;
134
        $fromform->generalfeedback = 'Generalfeedback: {={a} + {b}} is the right answer.';
135
 
136
        $fromform->unitrole = '3';
137
        $fromform->unitpenalty = 0.1;
138
        $fromform->unitgradingtypes = '1';
139
        $fromform->unitsleft = '0';
140
        $fromform->nounits = 1;
141
        $fromform->multiplier = [];
142
        $fromform->multiplier[0] = '1.0';
143
        $fromform->synchronize = 0;
144
        $fromform->answernumbering = 0;
145
        $fromform->shuffleanswers = 0;
146
 
147
        $fromform->noanswers = 6;
148
        $fromform->answer = [];
149
        $fromform->answer[0] = '{a} + {b}';
150
        $fromform->answer[1] = '{a} - {b}';
151
        $fromform->answer[2] = '*';
152
 
153
        $fromform->fraction = [];
154
        $fromform->fraction[0] = '1.0';
155
        $fromform->fraction[1] = '0.0';
156
        $fromform->fraction[2] = '0.0';
157
 
158
        $fromform->tolerance = [];
159
        $fromform->tolerance[0] = 0.001;
160
        $fromform->tolerance[1] = 0.001;
161
        $fromform->tolerance[2] = 0;
162
 
163
        $fromform->tolerancetype[0] = 1;
164
        $fromform->tolerancetype[1] = 1;
165
        $fromform->tolerancetype[2] = 1;
166
 
167
        $fromform->correctanswerlength[0] = 2;
168
        $fromform->correctanswerlength[1] = 2;
169
        $fromform->correctanswerlength[2] = 2;
170
 
171
        $fromform->correctanswerformat[0] = 1;
172
        $fromform->correctanswerformat[1] = 1;
173
        $fromform->correctanswerformat[2] = 1;
174
 
175
        $fromform->feedback = [];
176
        $fromform->feedback[0] = [];
177
        $fromform->feedback[0]['format'] = FORMAT_HTML;
178
        $fromform->feedback[0]['text'] = 'Very good.';
179
 
180
        $fromform->feedback[1] = [];
181
        $fromform->feedback[1]['format'] = FORMAT_HTML;
182
        $fromform->feedback[1]['text'] = 'Add. not subtract!';
183
 
184
        $fromform->feedback[2] = [];
185
        $fromform->feedback[2]['format'] = FORMAT_HTML;
186
        $fromform->feedback[2]['text'] = 'Completely wrong.';
187
 
188
        $fromform->status = \core_question\local\bank\question_version_status::QUESTION_STATUS_READY;
189
 
190
        return $fromform;
191
    }
192
 
193
    /**
194
     * Makes a calculated question about multiplying two numbers.
195
     *
196
     * @return qtype_calculated_question
197
     * @throws coding_exception
198
     */
199
    public function make_calculated_question_mult() {
200
        question_bank::load_question_definition_classes('calculated');
201
        $q = new qtype_calculated_question();
202
        test_question_maker::initialise_a_question($q);
203
        $q->name = 'Simple multiplication';
204
        $q->questiontext = 'What is {a} * {b}?';
205
        $q->generalfeedback = 'Generalfeedback: {={a} * {b}} is the right answer.';
206
 
207
        $q->answers = [
208
            13 => new \qtype_calculated\qtype_calculated_answer(13, '{a} * {b}', 1.0, 'Positive.', FORMAT_HTML, 0),
209
            14 => new \qtype_calculated\qtype_calculated_answer(14, '-{a} * {b}', 0.0, 'Negative.', FORMAT_HTML, 0),
210
        ];
211
        foreach ($q->answers as $answer) {
212
            $answer->correctanswerlength = 2;
213
            $answer->correctanswerformat = 1;
214
        }
215
 
216
        $q->qtype = question_bank::get_qtype('calculated');
217
        $q->unitdisplay = qtype_numerical::UNITOPTIONAL;
218
        $q->unitgradingtype = 0;
219
        $q->unitpenalty = 0;
220
        $q->unit = 'cm';
221
        $q->ap = new qtype_numerical_answer_processor([]);
222
        $q->synchronised = false;
223
 
224
        $q->datasetloader = new qtype_calculated_test_dataset_loader(0, [
225
            ['a' => 1, 'b' => 5],
226
            ['a' => 3, 'b' => 4],
227
            ['a' => 3, 'b' => 0.01416],
228
            ['a' => 31, 'b' => 0.01416],
229
        ]);
230
 
231
        return $q;
232
    }
233
 
234
    /**
235
     * Makes a calculated question about multiplying two numbers.
236
     *
237
     * @return qtype_calculated_question
238
     * @throws coding_exception
239
     */
240
    public function get_calculated_question_data_mult() {
241
        question_bank::load_question_definition_classes('calculated');
242
        $qdata = new stdClass();
243
        test_question_maker::initialise_question_data($qdata);
244
 
245
        $qdata->qtype = 'calculated';
246
        $qdata->name = 'Simple multiplication';
247
        $qdata->questiontext = 'What is {a} * {b}?';
248
        $qdata->generalfeedback = 'Generalfeedback: {={a} * {b}} is the right answer.';
249
        $qdata->status = \core_question\local\bank\question_version_status::QUESTION_STATUS_READY;
250
 
251
        $qdata->options = new stdClass();
252
        $qdata->options->unitgradingtype = 0;
253
        $qdata->options->unitpenalty = 0.0;
254
        $qdata->options->showunits = qtype_numerical::UNITOPTIONAL;
255
        $qdata->unit = 'cm';
256
        $qdata->options->unitsleft = 0;
257
        $qdata->options->synchronize = 0;
258
 
259
        $qdata->options->answers = [
260
            13 => new \qtype_calculated\qtype_calculated_answer(13, '{a} * {b}', 1.0, 'Positive.', FORMAT_HTML, 0.001),
261
            14 => new \qtype_calculated\qtype_calculated_answer(14, '-{a} * {b}', 0.0, 'Negative.', FORMAT_HTML, 0.001),
262
        ];
263
        foreach ($qdata->options->answers as $answer) {
264
            $answer->correctanswerlength = 2;
265
            $answer->correctanswerformat = 1;
266
        }
267
 
268
        $qdata->options->units = [];
269
 
270
        return $qdata;
271
    }
272
 
273
    /**
274
     * Makes a calculated question about multiplying two numbers.
275
     *
276
     * @return qtype_calculated_question
277
     * @throws coding_exception
278
     */
279
    public function get_calculated_question_form_data_mult() {
280
        question_bank::load_question_definition_classes('calculated');
281
        $fromform = new stdClass();
282
 
283
        $fromform->name = 'Simple multiplication';
284
        $fromform->questiontext = 'What is {a} * {b}?';
285
        $fromform->defaultmark = 1.0;
286
        $fromform->generalfeedback = 'Generalfeedback: {={a} * {b}} is the right answer.';
287
 
288
        $fromform->unitrole = '0';
289
        $fromform->unitpenalty = 0.1;
290
        $fromform->unitgradingtypes = '1';
291
        $fromform->unitsleft = '0';
292
        $fromform->nounits = 1;
293
        $fromform->multiplier = [];
294
        $fromform->multiplier[0] = '1.0';
295
        $fromform->synchronize = 0;
296
        $fromform->answernumbering = 0;
297
        $fromform->shuffleanswers = 0;
298
 
299
        $fromform->noanswers = 6;
300
        $fromform->answer = [];
301
        $fromform->answer[0] = '{a} * {b}';
302
        $fromform->answer[1] = '-{a} * {b}';
303
 
304
        $fromform->fraction = [];
305
        $fromform->fraction[0] = '1.0';
306
        $fromform->fraction[1] = '0.0';
307
 
308
        $fromform->tolerance = [];
309
        $fromform->tolerance[0] = 0.001;
310
        $fromform->tolerance[1] = 0.001;
311
 
312
        $fromform->tolerancetype[0] = 1;
313
        $fromform->tolerancetype[1] = 1;
314
 
315
        $fromform->correctanswerlength[0] = 2;
316
        $fromform->correctanswerlength[1] = 2;
317
 
318
        $fromform->correctanswerformat[0] = 1;
319
        $fromform->correctanswerformat[1] = 1;
320
 
321
        $fromform->feedback = [];
322
        $fromform->feedback[0] = [];
323
        $fromform->feedback[0]['format'] = FORMAT_HTML;
324
        $fromform->feedback[0]['text'] = 'Positive.';
325
 
326
        $fromform->feedback[1] = [];
327
        $fromform->feedback[1]['format'] = FORMAT_HTML;
328
        $fromform->feedback[1]['text'] = 'Negative.';
329
 
330
        $fromform->status = \core_question\local\bank\question_version_status::QUESTION_STATUS_READY;
331
 
332
        return $fromform;
333
    }
334
 
335
}
336
 
337
 
338
/**
339
 * Test implementation of {@link qtype_calculated_dataset_loader}. Gets the values
340
 * from an array passed to the constructor, rather than querying the database.
341
 *
342
 * @copyright  2011 The Open University
343
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
344
 */
345
class qtype_calculated_test_dataset_loader extends qtype_calculated_dataset_loader {
346
    protected $valuesets;
347
    protected $aresynchronised = [];
348
 
349
    public function __construct($questionid, array $valuesets) {
350
        parent::__construct($questionid);
351
        $this->valuesets = $valuesets;
352
    }
353
 
354
    public function get_number_of_items() {
355
        return count($this->valuesets);
356
    }
357
 
358
    public function load_values($itemnumber) {
359
        return $this->valuesets[$itemnumber - 1];
360
    }
361
 
362
    public function datasets_are_synchronised($category) {
363
        return !empty($this->aresynchronised[$category]);
364
    }
365
 
366
    /**
367
     * Allows the test to mock the return value of {@link datasets_are_synchronised()}.
368
     * @param int $category
369
     * @param bool $aresychronised
370
     */
371
    public function set_are_synchronised($category, $aresychronised) {
372
        $this->aresynchronised[$category] = $aresychronised;
373
    }
374
}