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
 * 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
 
1441 ariadna 136
        $fromform->unitrole = '0';
1 efrain 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
 
1441 ariadna 190
        $fromform->hint = [
191
            [
192
                'text' => 'Add',
193
                'format' => FORMAT_HTML,
194
            ],
195
        ];
196
 
197
        $fromform->unit = [
198
            'x',
199
        ];
200
        $fromform->multiplier = [
201
            '1.0',
202
        ];
203
 
1 efrain 204
        return $fromform;
205
    }
206
 
207
    /**
208
     * Makes a calculated question about multiplying two numbers.
209
     *
210
     * @return qtype_calculated_question
211
     * @throws coding_exception
212
     */
213
    public function make_calculated_question_mult() {
214
        question_bank::load_question_definition_classes('calculated');
215
        $q = new qtype_calculated_question();
216
        test_question_maker::initialise_a_question($q);
217
        $q->name = 'Simple multiplication';
218
        $q->questiontext = 'What is {a} * {b}?';
219
        $q->generalfeedback = 'Generalfeedback: {={a} * {b}} is the right answer.';
220
 
221
        $q->answers = [
222
            13 => new \qtype_calculated\qtype_calculated_answer(13, '{a} * {b}', 1.0, 'Positive.', FORMAT_HTML, 0),
223
            14 => new \qtype_calculated\qtype_calculated_answer(14, '-{a} * {b}', 0.0, 'Negative.', FORMAT_HTML, 0),
224
        ];
225
        foreach ($q->answers as $answer) {
226
            $answer->correctanswerlength = 2;
227
            $answer->correctanswerformat = 1;
228
        }
229
 
230
        $q->qtype = question_bank::get_qtype('calculated');
231
        $q->unitdisplay = qtype_numerical::UNITOPTIONAL;
232
        $q->unitgradingtype = 0;
233
        $q->unitpenalty = 0;
234
        $q->unit = 'cm';
235
        $q->ap = new qtype_numerical_answer_processor([]);
236
        $q->synchronised = false;
237
 
238
        $q->datasetloader = new qtype_calculated_test_dataset_loader(0, [
239
            ['a' => 1, 'b' => 5],
240
            ['a' => 3, 'b' => 4],
241
            ['a' => 3, 'b' => 0.01416],
242
            ['a' => 31, 'b' => 0.01416],
243
        ]);
244
 
245
        return $q;
246
    }
247
 
248
    /**
249
     * Makes a calculated question about multiplying two numbers.
250
     *
251
     * @return qtype_calculated_question
252
     * @throws coding_exception
253
     */
254
    public function get_calculated_question_data_mult() {
255
        question_bank::load_question_definition_classes('calculated');
256
        $qdata = new stdClass();
257
        test_question_maker::initialise_question_data($qdata);
258
 
259
        $qdata->qtype = 'calculated';
260
        $qdata->name = 'Simple multiplication';
261
        $qdata->questiontext = 'What is {a} * {b}?';
262
        $qdata->generalfeedback = 'Generalfeedback: {={a} * {b}} is the right answer.';
263
        $qdata->status = \core_question\local\bank\question_version_status::QUESTION_STATUS_READY;
264
 
265
        $qdata->options = new stdClass();
266
        $qdata->options->unitgradingtype = 0;
267
        $qdata->options->unitpenalty = 0.0;
268
        $qdata->options->showunits = qtype_numerical::UNITOPTIONAL;
269
        $qdata->unit = 'cm';
270
        $qdata->options->unitsleft = 0;
271
        $qdata->options->synchronize = 0;
272
 
273
        $qdata->options->answers = [
274
            13 => new \qtype_calculated\qtype_calculated_answer(13, '{a} * {b}', 1.0, 'Positive.', FORMAT_HTML, 0.001),
275
            14 => new \qtype_calculated\qtype_calculated_answer(14, '-{a} * {b}', 0.0, 'Negative.', FORMAT_HTML, 0.001),
276
        ];
277
        foreach ($qdata->options->answers as $answer) {
278
            $answer->correctanswerlength = 2;
279
            $answer->correctanswerformat = 1;
280
        }
281
 
282
        $qdata->options->units = [];
283
 
284
        return $qdata;
285
    }
286
 
287
    /**
288
     * Makes a calculated question about multiplying two numbers.
289
     *
290
     * @return qtype_calculated_question
291
     * @throws coding_exception
292
     */
293
    public function get_calculated_question_form_data_mult() {
294
        question_bank::load_question_definition_classes('calculated');
295
        $fromform = new stdClass();
296
 
297
        $fromform->name = 'Simple multiplication';
298
        $fromform->questiontext = 'What is {a} * {b}?';
299
        $fromform->defaultmark = 1.0;
300
        $fromform->generalfeedback = 'Generalfeedback: {={a} * {b}} is the right answer.';
301
 
302
        $fromform->unitrole = '0';
303
        $fromform->unitpenalty = 0.1;
304
        $fromform->unitgradingtypes = '1';
305
        $fromform->unitsleft = '0';
306
        $fromform->nounits = 1;
307
        $fromform->multiplier = [];
308
        $fromform->multiplier[0] = '1.0';
309
        $fromform->synchronize = 0;
310
        $fromform->answernumbering = 0;
311
        $fromform->shuffleanswers = 0;
312
 
313
        $fromform->noanswers = 6;
314
        $fromform->answer = [];
315
        $fromform->answer[0] = '{a} * {b}';
316
        $fromform->answer[1] = '-{a} * {b}';
317
 
318
        $fromform->fraction = [];
319
        $fromform->fraction[0] = '1.0';
320
        $fromform->fraction[1] = '0.0';
321
 
322
        $fromform->tolerance = [];
323
        $fromform->tolerance[0] = 0.001;
324
        $fromform->tolerance[1] = 0.001;
325
 
326
        $fromform->tolerancetype[0] = 1;
327
        $fromform->tolerancetype[1] = 1;
328
 
329
        $fromform->correctanswerlength[0] = 2;
330
        $fromform->correctanswerlength[1] = 2;
331
 
332
        $fromform->correctanswerformat[0] = 1;
333
        $fromform->correctanswerformat[1] = 1;
334
 
335
        $fromform->feedback = [];
336
        $fromform->feedback[0] = [];
337
        $fromform->feedback[0]['format'] = FORMAT_HTML;
338
        $fromform->feedback[0]['text'] = 'Positive.';
339
 
340
        $fromform->feedback[1] = [];
341
        $fromform->feedback[1]['format'] = FORMAT_HTML;
342
        $fromform->feedback[1]['text'] = 'Negative.';
343
 
344
        $fromform->status = \core_question\local\bank\question_version_status::QUESTION_STATUS_READY;
345
 
346
        return $fromform;
347
    }
348
 
349
}
350
 
351
 
352
/**
353
 * Test implementation of {@link qtype_calculated_dataset_loader}. Gets the values
354
 * from an array passed to the constructor, rather than querying the database.
355
 *
356
 * @copyright  2011 The Open University
357
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
358
 */
359
class qtype_calculated_test_dataset_loader extends qtype_calculated_dataset_loader {
360
    protected $valuesets;
361
    protected $aresynchronised = [];
362
 
363
    public function __construct($questionid, array $valuesets) {
364
        parent::__construct($questionid);
365
        $this->valuesets = $valuesets;
366
    }
367
 
368
    public function get_number_of_items() {
369
        return count($this->valuesets);
370
    }
371
 
372
    public function load_values($itemnumber) {
373
        return $this->valuesets[$itemnumber - 1];
374
    }
375
 
376
    public function datasets_are_synchronised($category) {
377
        return !empty($this->aresynchronised[$category]);
378
    }
379
 
380
    /**
381
     * Allows the test to mock the return value of {@link datasets_are_synchronised()}.
382
     * @param int $category
383
     * @param bool $aresychronised
384
     */
385
    public function set_are_synchronised($category, $aresychronised) {
386
        $this->aresynchronised[$category] = $aresychronised;
387
    }
388
}