weekly release 2.4dev
[moodle.git] / question / type / multianswer / tests / helper.php
CommitLineData
603bd001
PS
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 multianswer question type.
19 *
20 * @package qtype
21 * @subpackage multianswer
22 * @copyright 2011 The Open University
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 */
25
26
27defined('MOODLE_INTERNAL') || die();
28
29global $CFG;
30require_once($CFG->dirroot . '/question/type/multianswer/question.php');
31
32
33/**
34 * Test helper class for the multianswer question type.
35 *
36 * @copyright 2011 The Open University
37 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
38 */
39class qtype_multianswer_test_helper extends question_test_helper {
40 public function get_test_questions() {
be1d8ed1 41 return array('twosubq', 'fourmc');
603bd001
PS
42 }
43
44 /**
45 * Makes a multianswer question about completing two blanks in some text.
46 * @return qtype_multianswer_question
47 */
48 public function make_multianswer_question_twosubq() {
49 question_bank::load_question_definition_classes('multianswer');
50 $q = new qtype_multianswer_question();
51 test_question_maker::initialise_a_question($q);
52 $q->name = 'Simple multianswer';
53 $q->questiontext =
54 'Complete this opening line of verse: "The {#1} and the {#2} went to sea".';
55 $q->generalfeedback = 'General feedback: It\'s from "The Owl and the Pussy-cat" by Lear: ' .
56 '"The owl and the pussycat went to sea';
be1d8ed1
TH
57 $q->qtype = question_bank::get_qtype('multianswer');
58
59 $q->textfragments = array(
60 'Complete this opening line of verse: "The ',
61 ' and the ',
62 ' went to sea".',
63 );
64 $q->places = array('1' => '1', '2' => '2');
603bd001
PS
65
66 // Shortanswer subquestion.
67 question_bank::load_question_definition_classes('shortanswer');
68 $sa = new qtype_shortanswer_question();
69 test_question_maker::initialise_a_question($sa);
70 $sa->name = 'Simple multianswer';
71 $sa->questiontext = '{1:SHORTANSWER:Dog#Wrong, silly!~=Owl#Well done!~*#Wrong answer}';
72 $sa->questiontextformat = FORMAT_HTML;
73 $sa->generalfeedback = '';
74 $sa->generalfeedbackformat = FORMAT_HTML;
75 $sa->usecase = true;
76 $sa->answers = array(
77 13 => new question_answer(13, 'Dog', 0.0, 'Wrong, silly!', FORMAT_HTML),
78 14 => new question_answer(14, 'Owl', 1.0, 'Well done!', FORMAT_HTML),
79 15 => new question_answer(15, '*', 0.0, 'Wrong answer', FORMAT_HTML),
80 );
81 $sa->qtype = question_bank::get_qtype('shortanswer');
82 $sa->maxmark = 1;
83
84 // Multiple-choice subquestion.
85 question_bank::load_question_definition_classes('multichoice');
86 $mc = new qtype_multichoice_single_question();
87 test_question_maker::initialise_a_question($mc);
88 $mc->name = 'Simple multianswer';
89 $mc->questiontext = '{1:MULTICHOICE:Bow-wow#You seem to have a dog obsessions!' .
90 '~Wiggly worm#Now you are just being rediculous!~=Pussy-cat#Well done!}';
91 $mc->questiontextformat = FORMAT_HTML;
92 $mc->generalfeedback = '';
93 $mc->generalfeedbackformat = FORMAT_HTML;
94
95 $mc->shuffleanswers = 1;
96 $mc->answernumbering = 'none';
97 $mc->layout = qtype_multichoice_base::LAYOUT_DROPDOWN;
98
99 $mc->answers = array(
100 13 => new question_answer(13, 'Bow-wow', 0,
101 'You seem to have a dog obsessions!', FORMAT_HTML),
102 14 => new question_answer(14, 'Wiggly worm', 0,
103 'Now you are just being rediculous!', FORMAT_HTML),
104 15 => new question_answer(15, 'Pussy-cat', 1,
105 'Well done!', FORMAT_HTML),
106 );
107 $mc->qtype = question_bank::get_qtype('multichoice');
108 $mc->maxmark = 1;
109
110 $q->subquestions = array(
111 1 => $sa,
112 2 => $mc,
113 );
114
115 return $q;
116 }
117
118 /**
119 * Makes a multianswer question about completing two blanks in some text.
120 * @return object the question definition data, as it might be returned from
121 * get_question_options.
122 */
123 public function get_multianswer_question_data_twosubq() {
124 $qdata = new stdClass();
125 test_question_maker::initialise_question_data($qdata);
126
127 $qdata->name = 'Simple multianswer';
128 $qdata->questiontext =
129 'Complete this opening line of verse: "The {#1} and the {#2} went to sea".';
130 $qdata->generalfeedback = 'General feedback: It\'s from "The Owl and the Pussy-cat" by Lear: ' .
131 '"The owl and the pussycat went to sea';
132
133 $qdata->defaultmark = 2.0;
134 $qdata->qtype = 'multianswer';
135
136 $sa = new stdClass();
137 test_question_maker::initialise_question_data($sa);
138
139 $sa->name = 'Simple multianswer';
140 $sa->questiontext = '{1:SHORTANSWER:Dog#Wrong, silly!~=Owl#Well done!~*#Wrong answer}';
141 $sa->generalfeedback = '';
142 $sa->penalty = 0.0;
143 $sa->qtype = 'shortanswer';
144
145 $sa->options = new stdClass();
146 $sa->options->usecase = 0;
147
148 $sa->options->answers = array(
149 13 => new question_answer(13, 'Dog', 0, 'Wrong, silly!', FORMAT_HTML),
150 14 => new question_answer(14, 'Owl', 1, 'Well done!', FORMAT_HTML),
151 15 => new question_answer(15, '*', 0, 'Wrong answer', FORMAT_HTML),
152 );
153
154 $mc = new stdClass();
155 test_question_maker::initialise_question_data($mc);
156
157 $mc->name = 'Simple multianswer';
158 $mc->questiontext = '{1:MULTICHOICE:Bow-wow#You seem to have a dog obsessions!~' .
159 'Wiggly worm#Now you are just being ridiculous!~=Pussy-cat#Well done!}';
160 $mc->generalfeedback = '';
161 $mc->penalty = 0.0;
162 $mc->qtype = 'multichoice';
163
164 $mc->options = new stdClass();
165 $mc->options->layout = 0;
166 $mc->options->single = 1;
167 $mc->options->shuffleanswers = 1;
168 $mc->options->correctfeedback = '';
169 $mc->options->correctfeedbackformat = 1;
170 $mc->options->partiallycorrectfeedback = '';
171 $mc->options->partiallycorrectfeedbackformat = 1;
172 $mc->options->incorrectfeedback = '';
173 $mc->options->incorrectfeedbackformat = 1;
174 $mc->options->answernumbering = '';
175 $mc->options->shownumcorrect = 0;
176
177 $mc->options->answers = array(
178 23 => new question_answer(23, 'Bow-wow', 0,
179 'You seem to have a dog obsessions!', FORMAT_HTML),
180 24 => new question_answer(24, 'Wiggly worm', 0,
181 'Now you are just being ridiculous!', FORMAT_HTML),
182 25 => new question_answer(25, 'Pussy-cat', 1,
183 'Well done!', FORMAT_HTML),
184 );
185
186 $qdata->options = new stdClass();
187 $qdata->options->questions = array(
188 1 => $sa,
189 2 => $mc,
190 );
191
192 $qdata->hints = array(
193 new question_hint(0, 'Hint 1', FORMAT_HTML),
194 new question_hint(0, 'Hint 2', FORMAT_HTML),
195 );
196
197 return $qdata;
198 }
199
200 /**
201 * Makes a multianswer question about completing two blanks in some text.
202 * @return object the question definition data, as it might be returned from
203 * the question editing form.
204 */
205 public function get_multianswer_question_form_data_twosubq() {
206 $formdata = new stdClass();
207 test_question_maker::initialise_question_form_data($formdata);
208
209 $formdata->name = 'Simple multianswer';
210 $formdata->questiontext = 'Complete this opening line of verse: "The ' .
211 '{1:SHORTANSWER:Dog#Wrong, silly!~=Owl#Well done!~*#Wrong answer} ' .
212 'and the {1:MULTICHOICE:Bow-wow#You seem to have a dog obsessions!' .
213 '~Wiggly worm#Now you are just being ridiculous!~=Pussy-cat#Well done!}' .
214 ' went to sea".';
215 $formdata->generalfeedback = 'General feedback: It\'s from "The Owl and the Pussy-cat" ' .
216 'by Lear: "The owl and the pussycat went to sea';
217
218 $formdata->hint = array(
219 0 => array('text' => 'Hint 1', 'format' => FORMAT_HTML, 'itemid' => 0),
220 1 => array('text' => 'Hint 2', 'format' => FORMAT_HTML, 'itemid' => 0),
221 );
222
223 return $formdata;
224 }
be1d8ed1
TH
225
226 /**
227 * Makes a multianswer question about completing two blanks in some text.
228 * @return qtype_multianswer_question
229 */
230 public function make_multianswer_question_fourmc() {
231 question_bank::load_question_definition_classes('multianswer');
232 $q = new qtype_multianswer_question();
233 test_question_maker::initialise_a_question($q);
234 $q->name = 'Multianswer four multi-choice';
235 $q->questiontext = '<p>Match the following cities with the correct state:</p>
236 <ul>
237 <li>San Francisco: {#1}</li>
238 <li>Tucson: {#2}</li>
239 <li>Los Angeles: {#3}</li>
240 <li>Phoenix: {#4}</li>
241 </ul>';
242 $q->questiontextformat = FORMAT_HTML;
243 $q->generalfeedback = '';
244 $q->qtype = question_bank::get_qtype('multianswer');
245
246 $q->textfragments = array('<p>Match the following cities with the correct state:</p>
247 <ul>
248 <li>San Francisco: ', '</li>
249 <li>Tucson: ', '</li>
250 <li>Los Angeles: ', '</li>
251 <li>Phoenix: ', '</li>
252 </ul>');
253 $q->places = array('1' => '1', '2' => '2', '3' => '3', '4' => '4');
254
255 $subqdata = array(
256 1 => array('qt' => '{1:MULTICHOICE:=California#OK~Arizona#Wrong}', 'California' => 'OK', 'Arizona' => 'Wrong'),
257 2 => array('qt' => '{1:MULTICHOICE:%0%California#Wrong~=Arizona#OK}', 'California' => 'Wrong', 'Arizona' => 'OK'),
258 3 => array('qt' => '{1:MULTICHOICE:=California#OK~Arizona#Wrong}', 'California' => 'OK', 'Arizona' => 'Wrong'),
259 4 => array('qt' => '{1:MULTICHOICE:%0%California#Wrong~=Arizona#OK}', 'California' => 'Wrong', 'Arizona' => 'OK'),
260 );
261
262 foreach ($subqdata as $i => $data) {
263 // Multiple-choice subquestion.
264 question_bank::load_question_definition_classes('multichoice');
265 $mc = new qtype_multichoice_single_question();
266 test_question_maker::initialise_a_question($mc);
267 $mc->name = 'Multianswer four multi-choice';
268 $mc->questiontext = $data['qt'];
269 $mc->questiontextformat = FORMAT_HTML;
270 $mc->generalfeedback = '';
271 $mc->generalfeedbackformat = FORMAT_HTML;
272
273 $mc->shuffleanswers = 0; // TODO this is a cheat to make the unit tests easier to write.
274 // In reality, multianswer questions always shuffle.
275 $mc->answernumbering = 'none';
276 $mc->layout = qtype_multichoice_base::LAYOUT_DROPDOWN;
277
278 $mc->answers = array(
279 10 * $i => new question_answer(13, 'California', $data['California'] == 'OK', $data['California'], FORMAT_HTML),
280 10 * $i + 1 => new question_answer(14, 'Arizona', $data['Arizona'] == 'OK', $data['Arizona'], FORMAT_HTML),
281 );
282 $mc->qtype = question_bank::get_qtype('multichoice');
283 $mc->maxmark = 1;
284
285 $q->subquestions[$i] = $mc;
286 }
287
288 return $q;
289 }
603bd001 290}