on-demand release 4.5dev+
[moodle.git] / question / type / ordering / tests / helper.php
blob661dab8741211d055200eb26ecc4e581f2e027a3
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.
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 helper for the ordering question type.
20 * @package qtype_ordering
21 * @copyright 2018 The Open University
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 defined('MOODLE_INTERNAL') || die();
27 global $CFG;
28 require_once($CFG->dirroot . '/question/type/ordering/question.php');
30 /**
31 * Test helper for the ordering question type.
33 * The class has code to generate question data structures for sample ordering questions.
35 * @copyright 2018 The Open University
36 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
38 class qtype_ordering_test_helper extends question_test_helper {
39 /**
40 * Get the question types that this helper can handle.
42 * @return array the question types.
44 public function get_test_questions(): array {
45 return ['moodle'];
48 /**
49 * Makes an ordering question to sort the words Modular Object Oriented Dynamic Learning Environment.
51 * @return qtype_ordering_question the question instance.
53 public function make_ordering_question_moodle(): qtype_ordering_question {
54 question_bank::load_question_definition_classes('ordering');
55 $q = new qtype_ordering_question();
56 $q->hints = [
58 'text' => 'Hint 1',
59 'format' => FORMAT_HTML,
62 'text' => 'Hint 2',
63 'format' => FORMAT_HTML,
66 test_question_maker::initialise_a_question($q);
67 $q->qtype = question_bank::get_qtype('ordering');
68 $q->name = 'Moodle';
69 $q->questiontext = 'Put these words in order';
70 $q->generalfeedback = 'The correct answer is "Modular Object Oriented Dynamic Learning Environment".';
71 test_question_maker::set_standard_combined_feedback_fields($q);
72 $q->answers = [
73 13 => $this->make_answer(13, 'Modular', FORMAT_HTML, 1, true),
74 14 => $this->make_answer(14, 'Object', FORMAT_HTML, 2, true),
75 15 => $this->make_answer(15, 'Oriented', FORMAT_HTML, 3, true),
76 16 => $this->make_answer(16, 'Dynamic', FORMAT_HTML, 4, true),
77 17 => $this->make_answer(17, 'Learning', FORMAT_HTML, 5, true),
78 18 => $this->make_answer(18, 'Environment', FORMAT_HTML, 6, true),
80 $q->layouttype = qtype_ordering_question::LAYOUT_HORIZONTAL;
81 $q->selecttype = qtype_ordering_question::SELECT_ALL;
82 $q->selectcount = qtype_ordering_question::MIN_SUBSET_ITEMS;
83 $q->gradingtype = qtype_ordering_question::GRADING_RELATIVE_ALL_PREVIOUS_AND_NEXT;
84 $q->showgrading = true;
85 $q->numberingstyle = qtype_ordering_question::NUMBERING_STYLE_DEFAULT;
86 $q->shownumcorrect = 1;
88 return $q;
91 /**
92 * Create an answer record to use in a test question.
94 * @param int $id the id to set.
95 * @param string $text
96 * @param int $textformat one of the FORMAT_... constants.
97 * @param int $order the position in order, numbered from 1.
98 * @param bool $addmd5 whether to add the md5key property.
99 * @return stdClass the answer.
101 public function make_answer(int $id, string $text, int $textformat, int $order, bool $addmd5 = false): stdClass {
102 global $CFG;
104 $answer = new stdClass();
105 $answer->id = $id;
106 $answer->question = 0;
107 $answer->answer = $text;
108 $answer->answerformat = $textformat;
109 $answer->fraction = $order;
110 $answer->feedback = '';
111 $answer->feedbackformat = FORMAT_MOODLE;
113 if ($addmd5) {
114 if (isset($CFG->passwordsaltmain)) {
115 $salt = $CFG->passwordsaltmain;
116 } else {
117 $salt = '';
119 $answer->md5key = 'ordering_item_' . md5($salt . $answer->answer);
122 return $answer;
126 * Get the form data that corresponds an ordering question.
128 * The question is to sort the words Modular Object Oriented Dynamic Learning Environment.
130 * @return stdClass simulated question form data.
132 public function get_ordering_question_form_data_moodle(): stdClass {
133 $form = new stdClass();
134 $form->name = 'Moodle';
135 $form->questiontext = ['text' => 'Put these words in order.', 'format' => FORMAT_HTML];
136 $form->defaultmark = 1;
137 $form->generalfeedback = [
138 'text' => 'The correct answer is "Modular Object Oriented Dynamic Learning Environment".',
139 'format' => FORMAT_HTML,
142 $form->layouttype = qtype_ordering_question::LAYOUT_HORIZONTAL;
143 $form->selecttype = qtype_ordering_question::SELECT_ALL;
144 $form->selectcount = qtype_ordering_question::MIN_SUBSET_ITEMS;
145 $form->gradingtype = qtype_ordering_question::GRADING_RELATIVE_ALL_PREVIOUS_AND_NEXT;
146 $form->showgrading = true;
147 $form->numberingstyle = qtype_ordering_question::NUMBERING_STYLE_DEFAULT;
149 $form->countanswers = 6;
150 $form->answer = [
151 ['text' => 'Modular', 'format' => FORMAT_HTML],
152 ['text' => 'Object', 'format' => FORMAT_HTML],
153 ['text' => 'Oriented', 'format' => FORMAT_HTML],
154 ['text' => 'Dynamic', 'format' => FORMAT_HTML],
155 ['text' => 'Learning', 'format' => FORMAT_HTML],
156 ['text' => 'Environment', 'format' => FORMAT_HTML],
159 test_question_maker::set_standard_combined_feedback_form_data($form);
161 $form->penalty = '0.3333333';
162 // Build the expected hint base.
163 $form->numhints = 2;
164 $form->hint = [
166 'text' => 'Hint 1',
167 'format' => FORMAT_HTML,
170 'text' => 'Hint 2',
171 'format' => FORMAT_HTML,
175 $form->qtype = 'ordering';
176 return $form;
180 * Get the raw data that corresponds an ordering question.
182 * The question is to sort the words Modular Object Oriented Dynamic Learning Environment.
184 * @return stdClass simulated question form data.
186 public function get_ordering_question_data_moodle(): stdClass {
187 $questiondata = new stdClass();
188 test_question_maker::initialise_question_data($questiondata);
189 $questiondata->qtype = 'ordering';
190 $questiondata->name = 'Moodle';
191 $questiondata->questiontext = 'Put these words in order';
192 $questiondata->generalfeedback = 'The correct answer is "Modular Object Oriented Dynamic Learning Environment".';
194 $questiondata->options = new stdClass();
195 test_question_maker::set_standard_combined_feedback_fields($questiondata->options);
196 $questiondata->options->layouttype = qtype_ordering_question::LAYOUT_HORIZONTAL;
197 $questiondata->options->selecttype = qtype_ordering_question::SELECT_ALL;
198 $questiondata->options->selectcount = qtype_ordering_question::MIN_SUBSET_ITEMS;
199 $questiondata->options->gradingtype = qtype_ordering_question::GRADING_RELATIVE_ALL_PREVIOUS_AND_NEXT;
200 $questiondata->options->showgrading = true;
201 $questiondata->options->numberingstyle = qtype_ordering_question::NUMBERING_STYLE_DEFAULT;
203 $questiondata->options->answers = [
204 13 => $this->make_answer(13, 'Modular', FORMAT_HTML, 1),
205 14 => $this->make_answer(14, 'Object', FORMAT_HTML, 2),
206 15 => $this->make_answer(15, 'Oriented', FORMAT_HTML, 3),
207 16 => $this->make_answer(16, 'Dynamic', FORMAT_HTML, 4),
208 17 => $this->make_answer(17, 'Learning', FORMAT_HTML, 5),
209 18 => $this->make_answer(18, 'Environment', FORMAT_HTML, 6),
211 return $questiondata;
215 * Return an array of answer codes in the order of given response.
217 * @param question_definition $question The question object.
218 * @param array $items The array of input items.
219 * @return array The array of answer codes in the order of given response.
221 public static function get_response(question_definition $question, array $items): array {
223 $md5keys = [];
224 foreach ($items as $item) {
225 foreach ($question->answers as $answer) {
226 if ($item === $answer->answer) {
227 $md5keys[] = $answer->md5key;
228 break;
233 return ['response_' . $question->id => implode(',', $md5keys)];