/question/type/multichoice/tests/helper.php

https://gitlab.com/unofficial-mirrors/moodle · PHP · 400 lines · 327 code · 29 blank · 44 comment · 1 complexity · 5bd2fa65528261c83d679c7d1caf5483 MD5 · raw file

  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. * Test helper code for the multiple choice question type.
  18. *
  19. * @package qtype_multichoice
  20. * @copyright 2013 The Open University
  21. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  22. */
  23. defined('MOODLE_INTERNAL') || die();
  24. /**
  25. * Test helper class for the multiple choice question type.
  26. *
  27. * @copyright 2013 The Open University
  28. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  29. */
  30. class qtype_multichoice_test_helper extends question_test_helper {
  31. public function get_test_questions() {
  32. return array('two_of_four', 'one_of_four');
  33. }
  34. /**
  35. * Get the question data, as it would be loaded by get_question_options.
  36. * @return object
  37. */
  38. public static function get_multichoice_question_data_two_of_four() {
  39. global $USER;
  40. $qdata = new stdClass();
  41. $qdata->createdby = $USER->id;
  42. $qdata->modifiedby = $USER->id;
  43. $qdata->qtype = 'multichoice';
  44. $qdata->name = 'Multiple choice question';
  45. $qdata->questiontext = 'Which are the odd numbers?';
  46. $qdata->questiontextformat = FORMAT_HTML;
  47. $qdata->generalfeedback = 'The odd numbers are One and Three.';
  48. $qdata->generalfeedbackformat = FORMAT_HTML;
  49. $qdata->defaultmark = 1;
  50. $qdata->length = 1;
  51. $qdata->penalty = 0.3333333;
  52. $qdata->hidden = 0;
  53. $qdata->options = new stdClass();
  54. $qdata->options->shuffleanswers = 1;
  55. $qdata->options->answernumbering = '123';
  56. $qdata->options->layout = 0;
  57. $qdata->options->single = 0;
  58. $qdata->options->correctfeedback =
  59. test_question_maker::STANDARD_OVERALL_CORRECT_FEEDBACK;
  60. $qdata->options->correctfeedbackformat = FORMAT_HTML;
  61. $qdata->options->partiallycorrectfeedback =
  62. test_question_maker::STANDARD_OVERALL_PARTIALLYCORRECT_FEEDBACK;
  63. $qdata->options->partiallycorrectfeedbackformat = FORMAT_HTML;
  64. $qdata->options->shownumcorrect = 1;
  65. $qdata->options->incorrectfeedback =
  66. test_question_maker::STANDARD_OVERALL_INCORRECT_FEEDBACK;
  67. $qdata->options->incorrectfeedbackformat = FORMAT_HTML;
  68. $qdata->options->answers = array(
  69. 13 => (object) array(
  70. 'id' => 13,
  71. 'answer' => 'One',
  72. 'answerformat' => FORMAT_PLAIN,
  73. 'fraction' => '0.5',
  74. 'feedback' => 'One is odd.',
  75. 'feedbackformat' => FORMAT_HTML,
  76. ),
  77. 14 => (object) array(
  78. 'id' => 14,
  79. 'answer' => 'Two',
  80. 'answerformat' => FORMAT_PLAIN,
  81. 'fraction' => '0.0',
  82. 'feedback' => 'Two is even.',
  83. 'feedbackformat' => FORMAT_HTML,
  84. ),
  85. 15 => (object) array(
  86. 'id' => 15,
  87. 'answer' => 'Three',
  88. 'answerformat' => FORMAT_PLAIN,
  89. 'fraction' => '0.5',
  90. 'feedback' => 'Three is odd.',
  91. 'feedbackformat' => FORMAT_HTML,
  92. ),
  93. 16 => (object) array(
  94. 'id' => 16,
  95. 'answer' => 'Four',
  96. 'answerformat' => FORMAT_PLAIN,
  97. 'fraction' => '0.0',
  98. 'feedback' => 'Four is even.',
  99. 'feedbackformat' => FORMAT_HTML,
  100. ),
  101. );
  102. $qdata->hints = array(
  103. 1 => (object) array(
  104. 'hint' => 'Hint 1.',
  105. 'hintformat' => FORMAT_HTML,
  106. 'shownumcorrect' => 1,
  107. 'clearwrong' => 0,
  108. 'options' => 0,
  109. ),
  110. 2 => (object) array(
  111. 'hint' => 'Hint 2.',
  112. 'hintformat' => FORMAT_HTML,
  113. 'shownumcorrect' => 1,
  114. 'clearwrong' => 1,
  115. 'options' => 1,
  116. ),
  117. );
  118. return $qdata;
  119. }
  120. /**
  121. * Get the question data, as it would be loaded by get_question_options.
  122. * @return object
  123. */
  124. public static function get_multichoice_question_form_data_two_of_four() {
  125. $qdata = new stdClass();
  126. $qdata->name = 'multiple choice question';
  127. $qdata->questiontext = array('text' => 'Which are the odd numbers?', 'format' => FORMAT_HTML);
  128. $qdata->generalfeedback = array('text' => 'The odd numbers are One and Three.', 'format' => FORMAT_HTML);
  129. $qdata->defaultmark = 1;
  130. $qdata->noanswers = 5;
  131. $qdata->numhints = 2;
  132. $qdata->penalty = 0.3333333;
  133. $qdata->shuffleanswers = 1;
  134. $qdata->answernumbering = '123';
  135. $qdata->single = '0';
  136. $qdata->correctfeedback = array('text' => test_question_maker::STANDARD_OVERALL_CORRECT_FEEDBACK,
  137. 'format' => FORMAT_HTML);
  138. $qdata->partiallycorrectfeedback = array('text' => test_question_maker::STANDARD_OVERALL_PARTIALLYCORRECT_FEEDBACK,
  139. 'format' => FORMAT_HTML);
  140. $qdata->shownumcorrect = 1;
  141. $qdata->incorrectfeedback = array('text' => test_question_maker::STANDARD_OVERALL_INCORRECT_FEEDBACK,
  142. 'format' => FORMAT_HTML);
  143. $qdata->fraction = array('0.5', '0.0', '0.5', '0.0', '0.0');
  144. $qdata->answer = array(
  145. 0 => array(
  146. 'text' => 'One',
  147. 'format' => FORMAT_PLAIN
  148. ),
  149. 1 => array(
  150. 'text' => 'Two',
  151. 'format' => FORMAT_PLAIN
  152. ),
  153. 2 => array(
  154. 'text' => 'Three',
  155. 'format' => FORMAT_PLAIN
  156. ),
  157. 3 => array(
  158. 'text' => 'Four',
  159. 'format' => FORMAT_PLAIN
  160. ),
  161. 4 => array(
  162. 'text' => '',
  163. 'format' => FORMAT_PLAIN
  164. )
  165. );
  166. $qdata->feedback = array(
  167. 0 => array(
  168. 'text' => 'One is odd.',
  169. 'format' => FORMAT_HTML
  170. ),
  171. 1 => array(
  172. 'text' => 'Two is even.',
  173. 'format' => FORMAT_HTML
  174. ),
  175. 2 => array(
  176. 'text' => 'Three is odd.',
  177. 'format' => FORMAT_HTML
  178. ),
  179. 3 => array(
  180. 'text' => 'Four is even.',
  181. 'format' => FORMAT_HTML
  182. ),
  183. 4 => array(
  184. 'text' => '',
  185. 'format' => FORMAT_HTML
  186. )
  187. );
  188. $qdata->hint = array(
  189. 0 => array(
  190. 'text' => 'Hint 1.',
  191. 'format' => FORMAT_HTML
  192. ),
  193. 1 => array(
  194. 'text' => 'Hint 2.',
  195. 'format' => FORMAT_HTML
  196. )
  197. );
  198. $qdata->hintclearwrong = array(0, 1);
  199. $qdata->hintshownumcorrect = array(1, 1);
  200. return $qdata;
  201. }
  202. /**
  203. * Get the question data, as it would be loaded by get_question_options.
  204. * @return object
  205. */
  206. public static function get_multichoice_question_data_one_of_four() {
  207. global $USER;
  208. $qdata = new stdClass();
  209. $qdata->createdby = $USER->id;
  210. $qdata->modifiedby = $USER->id;
  211. $qdata->qtype = 'multichoice';
  212. $qdata->name = 'Multiple choice question';
  213. $qdata->questiontext = 'Which is the oddest number?';
  214. $qdata->questiontextformat = FORMAT_HTML;
  215. $qdata->generalfeedback = 'The oddest number is One.'; // Arguable possibly but it is a quick way to make a variation on
  216. //this question with one correct answer.
  217. $qdata->generalfeedbackformat = FORMAT_HTML;
  218. $qdata->defaultmark = 1;
  219. $qdata->length = 1;
  220. $qdata->penalty = 0.3333333;
  221. $qdata->hidden = 0;
  222. $qdata->options = new stdClass();
  223. $qdata->options->shuffleanswers = 1;
  224. $qdata->options->answernumbering = '123';
  225. $qdata->options->layout = 0;
  226. $qdata->options->single = 1;
  227. $qdata->options->correctfeedback =
  228. test_question_maker::STANDARD_OVERALL_CORRECT_FEEDBACK;
  229. $qdata->options->correctfeedbackformat = FORMAT_HTML;
  230. $qdata->options->partiallycorrectfeedback =
  231. test_question_maker::STANDARD_OVERALL_PARTIALLYCORRECT_FEEDBACK;
  232. $qdata->options->partiallycorrectfeedbackformat = FORMAT_HTML;
  233. $qdata->options->shownumcorrect = 1;
  234. $qdata->options->incorrectfeedback =
  235. test_question_maker::STANDARD_OVERALL_INCORRECT_FEEDBACK;
  236. $qdata->options->incorrectfeedbackformat = FORMAT_HTML;
  237. $qdata->options->answers = array(
  238. 13 => (object) array(
  239. 'id' => 13,
  240. 'answer' => 'One',
  241. 'answerformat' => FORMAT_PLAIN,
  242. 'fraction' => '1',
  243. 'feedback' => 'One is the oddest.',
  244. 'feedbackformat' => FORMAT_HTML,
  245. ),
  246. 14 => (object) array(
  247. 'id' => 14,
  248. 'answer' => 'Two',
  249. 'answerformat' => FORMAT_PLAIN,
  250. 'fraction' => '0.0',
  251. 'feedback' => 'Two is even.',
  252. 'feedbackformat' => FORMAT_HTML,
  253. ),
  254. 15 => (object) array(
  255. 'id' => 15,
  256. 'answer' => 'Three',
  257. 'answerformat' => FORMAT_PLAIN,
  258. 'fraction' => '0',
  259. 'feedback' => 'Three is odd.',
  260. 'feedbackformat' => FORMAT_HTML,
  261. ),
  262. 16 => (object) array(
  263. 'id' => 16,
  264. 'answer' => 'Four',
  265. 'answerformat' => FORMAT_PLAIN,
  266. 'fraction' => '0.0',
  267. 'feedback' => 'Four is even.',
  268. 'feedbackformat' => FORMAT_HTML,
  269. ),
  270. );
  271. $qdata->hints = array(
  272. 1 => (object) array(
  273. 'hint' => 'Hint 1.',
  274. 'hintformat' => FORMAT_HTML,
  275. 'shownumcorrect' => 1,
  276. 'clearwrong' => 0,
  277. 'options' => 0,
  278. ),
  279. 2 => (object) array(
  280. 'hint' => 'Hint 2.',
  281. 'hintformat' => FORMAT_HTML,
  282. 'shownumcorrect' => 1,
  283. 'clearwrong' => 1,
  284. 'options' => 1,
  285. ),
  286. );
  287. return $qdata;
  288. }
  289. /**
  290. * Get the question data, as it would be loaded by get_question_options.
  291. * @return object
  292. */
  293. public static function get_multichoice_question_form_data_one_of_four() {
  294. $qdata = new stdClass();
  295. $qdata->name = 'multiple choice question';
  296. $qdata->questiontext = array('text' => 'Which is the oddest number?', 'format' => FORMAT_HTML);
  297. $qdata->generalfeedback = array('text' => 'The oddest number is One.', 'format' => FORMAT_HTML);
  298. $qdata->defaultmark = 1;
  299. $qdata->noanswers = 5;
  300. $qdata->numhints = 2;
  301. $qdata->penalty = 0.3333333;
  302. $qdata->shuffleanswers = 1;
  303. $qdata->answernumbering = '123';
  304. $qdata->single = '1';
  305. $qdata->correctfeedback = array('text' => test_question_maker::STANDARD_OVERALL_CORRECT_FEEDBACK,
  306. 'format' => FORMAT_HTML);
  307. $qdata->partiallycorrectfeedback = array('text' => test_question_maker::STANDARD_OVERALL_PARTIALLYCORRECT_FEEDBACK,
  308. 'format' => FORMAT_HTML);
  309. $qdata->shownumcorrect = 1;
  310. $qdata->incorrectfeedback = array('text' => test_question_maker::STANDARD_OVERALL_INCORRECT_FEEDBACK,
  311. 'format' => FORMAT_HTML);
  312. $qdata->fraction = array('1.0', '0.0', '0.0', '0.0', '0.0');
  313. $qdata->answer = array(
  314. 0 => array(
  315. 'text' => 'One',
  316. 'format' => FORMAT_PLAIN
  317. ),
  318. 1 => array(
  319. 'text' => 'Two',
  320. 'format' => FORMAT_PLAIN
  321. ),
  322. 2 => array(
  323. 'text' => 'Three',
  324. 'format' => FORMAT_PLAIN
  325. ),
  326. 3 => array(
  327. 'text' => 'Four',
  328. 'format' => FORMAT_PLAIN
  329. ),
  330. 4 => array(
  331. 'text' => '',
  332. 'format' => FORMAT_PLAIN
  333. )
  334. );
  335. $qdata->feedback = array(
  336. 0 => array(
  337. 'text' => 'One is the oddest.',
  338. 'format' => FORMAT_HTML
  339. ),
  340. 1 => array(
  341. 'text' => 'Two is even.',
  342. 'format' => FORMAT_HTML
  343. ),
  344. 2 => array(
  345. 'text' => 'Three is odd.',
  346. 'format' => FORMAT_HTML
  347. ),
  348. 3 => array(
  349. 'text' => 'Four is even.',
  350. 'format' => FORMAT_HTML
  351. ),
  352. 4 => array(
  353. 'text' => '',
  354. 'format' => FORMAT_HTML
  355. )
  356. );
  357. $qdata->hint = array(
  358. 0 => array(
  359. 'text' => 'Hint 1.',
  360. 'format' => FORMAT_HTML
  361. ),
  362. 1 => array(
  363. 'text' => 'Hint 2.',
  364. 'format' => FORMAT_HTML
  365. )
  366. );
  367. $qdata->hintclearwrong = array(0, 1);
  368. $qdata->hintshownumcorrect = array(1, 1);
  369. return $qdata;
  370. }
  371. }