650674b423cd42e1facfad8485e9e9b8b551aac3
[moodle.git] / question / type / numerical / tests / helper.php
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/>.
17 /**
18  * Test helpers for the numerical question type.
19  *
20  * @package    qtype
21  * @subpackage numerical
22  * @copyright  2011 The Open University
23  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24  */
27 defined('MOODLE_INTERNAL') || die();
30 /**
31  * Test helper class for the numerical question type.
32  *
33  * @copyright  2011 The Open University
34  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
35  */
36 class qtype_numerical_test_helper extends question_test_helper {
37     public function get_test_questions() {
38         return array('pi', 'unit', 'currency', 'pi3tries');
39     }
41     /**
42      * Makes a numerical question with correct ansewer 3.14, and various incorrect
43      * answers with different feedback.
44      * @return qtype_numerical_question
45      */
46     public function make_numerical_question_pi() {
47         question_bank::load_question_definition_classes('numerical');
48         $num = new qtype_numerical_question();
49         test_question_maker::initialise_a_question($num);
50         $num->name = 'Pi to two d.p.';
51         $num->questiontext = 'What is pi to two d.p.?';
52         $num->generalfeedback = 'Generalfeedback: 3.14 is the right answer.';
53         $num->answers = array(
54             13 => new qtype_numerical_answer(13, '3.14',  1.0, 'Very good.',
55                     FORMAT_HTML, 0),
56             14 => new qtype_numerical_answer(14, '3.142', 0.0, 'Too accurate.',
57                     FORMAT_HTML, 0.005),
58             15 => new qtype_numerical_answer(15, '3.1',   0.0, 'Not accurate enough.',
59                     FORMAT_HTML, 0.05),
60             16 => new qtype_numerical_answer(16, '3',     0.0, 'Not accurate enough.',
61                     FORMAT_HTML, 0.5),
62             17 => new qtype_numerical_answer(17, '*',     0.0, 'Completely wrong.',
63                     FORMAT_HTML, 0),
64         );
65         $num->qtype = question_bank::get_qtype('numerical');
66         $num->unitdisplay = qtype_numerical::UNITOPTIONAL;
67         $num->unitgradingtype = 0;
68         $num->unitpenalty = 0.2;
69         $num->ap = new qtype_numerical_answer_processor(array());
71         return $num;
72     }
74     /**
75      * Get the form data that corresponds to saving a numerical question.
76      *
77      * This question asks for Pi to two decimal places. It has feedback
78      * for various wrong responses. There is hint data there, but
79      * it is all blank, so no hints are created if this question is saved.
80      *
81      * @return stdClass simulated question form data.
82      */
83     public function get_numerical_question_form_data_pi() {
84         $form = new stdClass();
85         $form->name = 'Pi to two d.p.';
86         $form->questiontext = array();
87         $form->questiontext['format'] = '1';
88         $form->questiontext['text'] = 'What is pi to two d.p.?';
90         $form->defaultmark = 1;
91         $form->generalfeedback = array();
92         $form->generalfeedback['format'] = '1';
93         $form->generalfeedback['text'] = 'Generalfeedback: 3.14 is the right answer.';
95         $form->noanswers = 6;
96         $form->answer = array();
97         $form->answer[0] = '3.14';
98         $form->answer[1] = '3.142';
99         $form->answer[2] = '3.1';
100         $form->answer[3] = '3';
101         $form->answer[4] = '*';
102         $form->answer[5] = '';
104         $form->tolerance = array();
105         $form->tolerance[0] = 0;
106         $form->tolerance[1] = 0;
107         $form->tolerance[2] = 0;
108         $form->tolerance[3] = 0;
109         $form->tolerance[4] = 0;
110         $form->tolerance[5] = 0;
112         $form->fraction = array();
113         $form->fraction[0] = '1.0';
114         $form->fraction[1] = '0.0';
115         $form->fraction[2] = '0.0';
116         $form->fraction[3] = '0.0';
117         $form->fraction[4] = '0.0';
118         $form->fraction[5] = '0.0';
120         $form->feedback = array();
121         $form->feedback[0] = array();
122         $form->feedback[0]['format'] = '1';
123         $form->feedback[0]['text'] = 'Very good.';
125         $form->feedback[1] = array();
126         $form->feedback[1]['format'] = '1';
127         $form->feedback[1]['text'] = 'Too accurate.';
129         $form->feedback[2] = array();
130         $form->feedback[2]['format'] = '1';
131         $form->feedback[2]['text'] = 'Not accurate enough.';
133         $form->feedback[3] = array();
134         $form->feedback[3]['format'] = '1';
135         $form->feedback[3]['text'] = 'Not accurate enough.';
137         $form->feedback[4] = array();
138         $form->feedback[4]['format'] = '1';
139         $form->feedback[4]['text'] = 'Completely wrong.';
141         $form->feedback[5] = array();
142         $form->feedback[5]['format'] = '1';
143         $form->feedback[5]['text'] = '';
145         $form->unitrole = '3';
146         $form->unitpenalty = 0.1;
147         $form->unitgradingtypes = '1';
148         $form->unitsleft = '0';
149         $form->nounits = 1;
150         $form->multiplier = array();
151         $form->multiplier[0] = '1.0';
153         $form->penalty = '0.3333333';
154         $form->numhints = 2;
155         $form->hint = array();
156         $form->hint[0] = array();
157         $form->hint[0]['format'] = '1';
158         $form->hint[0]['text'] = '';
160         $form->hint[1] = array();
161         $form->hint[1]['format'] = '1';
162         $form->hint[1]['text'] = '';
164         $form->qtype = 'numerical';
165         return $form;
166     }
168     /**
169      * Get the form data that corresponds to saving a numerical question.
170      *
171      * Like {@link get_numerical_question_form_data_pi()}, but
172      * this time with two hints, making this suitable for use
173      * with the Interactive with multiple tries behaviour.
174      *
175      * @return stdClass simulated question form data.
176      */
177     public function get_numerical_question_form_data_pi3tries() {
178         $form = $this->get_numerical_question_form_data_pi();
179         $form->hint[0]['text'] = 'First hint';
180         $form->hint[1]['text'] = 'Second hint';
181         return $form;
182     }
184     public function get_numerical_question_data_pi() {
185         $q = new stdClass();
186         $q->name = 'Pi to two d.p.';
187         $q->questiontext = 'What is pi to two d.p.?';
188         $q->questiontextformat = FORMAT_HTML;
189         $q->generalfeedback = 'Generalfeedback: 3.14 is the right answer.';
190         $q->generalfeedbackformat = FORMAT_HTML;
191         $q->defaultmark = 1;
192         $q->penalty = 0.3333333;
193         $q->qtype = 'numerical';
194         $q->length = '1';
195         $q->hidden = '0';
196         $q->createdby = '2';
197         $q->modifiedby = '2';
198         $q->options = new stdClass();
199         $q->options->answers = array();
200         $q->options->answers[0] = new stdClass();
201         $q->options->answers[0]->answer = '3.14';
202         $q->options->answers[0]->fraction = '1.0000000';
203         $q->options->answers[0]->feedback = 'Very good.';
204         $q->options->answers[0]->feedbackformat = FORMAT_HTML;
205         $q->options->answers[0]->tolerance = '0';
207         $q->options->answers[1] = new stdClass();
208         $q->options->answers[1]->answer = '3.142';
209         $q->options->answers[1]->fraction = '0.0000000';
210         $q->options->answers[1]->feedback = 'Too accurate.';
211         $q->options->answers[1]->feedbackformat = FORMAT_HTML;
212         $q->options->answers[1]->tolerance = '0';
214         $q->options->answers[2] = new stdClass();
215         $q->options->answers[2]->answer = '3.1';
216         $q->options->answers[2]->fraction = '0.0000000';
217         $q->options->answers[2]->feedback = 'Not accurate enough.';
218         $q->options->answers[2]->feedbackformat = FORMAT_HTML;
219         $q->options->answers[2]->tolerance = '0';
221         $q->options->answers[3] = new stdClass();
222         $q->options->answers[3]->answer = '3';
223         $q->options->answers[3]->answerformat = '0';
224         $q->options->answers[3]->fraction = '0.0000000';
225         $q->options->answers[3]->feedback = 'Not accurate enough.';
226         $q->options->answers[3]->feedbackformat = FORMAT_HTML;
227         $q->options->answers[3]->tolerance = '0';
229         $q->options->answers[4] = new stdClass();
230         $q->options->answers[4]->answer = '*';
231         $q->options->answers[4]->answerformat = '0';
232         $q->options->answers[4]->fraction = '0.0000000';
233         $q->options->answers[4]->feedback = 'Completely wrong.';
234         $q->options->answers[4]->feedbackformat = FORMAT_HTML;
235         $q->options->answers[4]->tolerance = '0';
237         $q->options->units = array();
239         $q->options->unitgradingtype = '0';
240         $q->options->unitpenalty = '0.1000000';
241         $q->options->showunits = '3';
242         $q->options->unitsleft = '0';
244         return $q;
245     }
247         /**
248      * Makes a numerical question with a choice (select menu) of units.
249      * @return qtype_numerical_question
250      */
251     public function make_numerical_question_unit() {
252         question_bank::load_question_definition_classes('numerical');
253         $num = new qtype_numerical_question();
254         test_question_maker::initialise_a_question($num);
255         $num->name = 'Numerical with units';
256         $num->questiontext = 'What is 1 m + 25 cm?';
257         $num->generalfeedback = 'Generalfeedback: 1.25m or 125cm is the right answer.';
258         $num->answers = array(
259             13 => new qtype_numerical_answer(13, '1.25', 1.0, 'Very good.', FORMAT_HTML, 0),
260             14 => new qtype_numerical_answer(14, '1.25', 0.5, 'Vaguely right.', FORMAT_HTML, 0.05),
261             17 => new qtype_numerical_answer(17, '*', 0.0, 'Completely wrong.', FORMAT_HTML, 0),
262         );
263         $num->qtype = question_bank::get_qtype('numerical');
264         $num->unitdisplay = qtype_numerical::UNITSELECT;
265         $num->unitgradingtype = qtype_numerical::UNITGRADEDOUTOFMARK;
266         $num->unitpenalty = 0.5;
267         $num->ap = new qtype_numerical_answer_processor(array('m' => 1, 'cm' => 100));
269         return $num;
270     }
272     /**
273      * Makes a numerical question with correct ansewer 3.14, and various incorrect
274      * answers with different feedback.
275      * @return qtype_numerical_question
276      */
277     public function make_numerical_question_currency() {
278         question_bank::load_question_definition_classes('numerical');
279         $num = new qtype_numerical_question();
280         test_question_maker::initialise_a_question($num);
281         $num->name = 'Add money';
282         $num->questiontext = 'What is $666 + $666?';
283         $num->generalfeedback = 'Generalfeedback: $1,332 is the right answer.';
284         $num->answers = array(
285             13 => new qtype_numerical_answer(13, '1332', 1.0, 'Very good.', FORMAT_HTML, 0),
286             14 => new qtype_numerical_answer(14, '*', 0.0, 'Wrong.', FORMAT_HTML, 0),
287         );
288         $num->qtype = question_bank::get_qtype('numerical');
289         $num->unitdisplay = qtype_numerical::UNITINPUT;
290         $num->unitgradingtype = qtype_numerical::UNITGRADEDOUTOFMAX;
291         $num->unitpenalty = 0.2;
292         $num->ap = new qtype_numerical_answer_processor(array('$' => 1), true);
294         return $num;
295     }