PageRenderTime 24ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 1ms

/question/type/multichoice/question.php

https://bitbucket.org/kudutest1/moodlegit
PHP | 438 lines | 312 code | 59 blank | 67 comment | 51 complexity | 57fed9ad1d934b599ab1d09ec14a61cd 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. * Multiple choice question definition classes.
  18. *
  19. * @package qtype
  20. * @subpackage multichoice
  21. * @copyright 2009 The Open University
  22. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23. */
  24. defined('MOODLE_INTERNAL') || die();
  25. /**
  26. * Base class for multiple choice questions. The parts that are common to
  27. * single select and multiple select.
  28. *
  29. * @copyright 2009 The Open University
  30. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  31. */
  32. abstract class qtype_multichoice_base extends question_graded_automatically {
  33. const LAYOUT_DROPDOWN = 0;
  34. const LAYOUT_VERTICAL = 1;
  35. const LAYOUT_HORIZONTAL = 2;
  36. public $answers;
  37. public $shuffleanswers;
  38. public $answernumbering;
  39. public $layout = self::LAYOUT_VERTICAL;
  40. public $correctfeedback;
  41. public $correctfeedbackformat;
  42. public $partiallycorrectfeedback;
  43. public $partiallycorrectfeedbackformat;
  44. public $incorrectfeedback;
  45. public $incorrectfeedbackformat;
  46. protected $order = null;
  47. public function start_attempt(question_attempt_step $step, $variant) {
  48. $this->order = array_keys($this->answers);
  49. if ($this->shuffleanswers) {
  50. shuffle($this->order);
  51. }
  52. $step->set_qt_var('_order', implode(',', $this->order));
  53. }
  54. public function apply_attempt_state(question_attempt_step $step) {
  55. $this->order = explode(',', $step->get_qt_var('_order'));
  56. }
  57. public function get_question_summary() {
  58. $question = $this->html_to_text($this->questiontext, $this->questiontextformat);
  59. $choices = array();
  60. foreach ($this->order as $ansid) {
  61. $choices[] = $this->html_to_text($this->answers[$ansid]->answer,
  62. $this->answers[$ansid]->answerformat);
  63. }
  64. return $question . ': ' . implode('; ', $choices);
  65. }
  66. public function get_order(question_attempt $qa) {
  67. $this->init_order($qa);
  68. return $this->order;
  69. }
  70. protected function init_order(question_attempt $qa) {
  71. if (is_null($this->order)) {
  72. $this->order = explode(',', $qa->get_step(0)->get_qt_var('_order'));
  73. }
  74. }
  75. public abstract function get_response(question_attempt $qa);
  76. public abstract function is_choice_selected($response, $value);
  77. public function check_file_access($qa, $options, $component, $filearea, $args, $forcedownload) {
  78. if ($component == 'question' && in_array($filearea,
  79. array('correctfeedback', 'partiallycorrectfeedback', 'incorrectfeedback'))) {
  80. return $this->check_combined_feedback_file_access($qa, $options, $filearea);
  81. } else if ($component == 'question' && $filearea == 'answer') {
  82. $answerid = reset($args); // Itemid is answer id.
  83. return in_array($answerid, $this->order);
  84. } else if ($component == 'question' && $filearea == 'answerfeedback') {
  85. $answerid = reset($args); // Itemid is answer id.
  86. $response = $this->get_response($qa);
  87. $isselected = false;
  88. foreach ($this->order as $value => $ansid) {
  89. if ($ansid == $answerid) {
  90. $isselected = $this->is_choice_selected($response, $value);
  91. break;
  92. }
  93. }
  94. // Param $options->suppresschoicefeedback is a hack specific to the
  95. // oumultiresponse question type. It would be good to refactor to
  96. // avoid refering to it here.
  97. return $options->feedback && empty($options->suppresschoicefeedback) &&
  98. $isselected;
  99. } else if ($component == 'question' && $filearea == 'hint') {
  100. return $this->check_hint_file_access($qa, $options, $args);
  101. } else {
  102. return parent::check_file_access($qa, $options, $component, $filearea,
  103. $args, $forcedownload);
  104. }
  105. }
  106. public function make_html_inline($html) {
  107. $html = preg_replace('~\s*<p>\s*~u', '', $html);
  108. $html = preg_replace('~\s*</p>\s*~u', '<br />', $html);
  109. $html = preg_replace('~(<br\s*/?>)+$~u', '', $html);
  110. return trim($html);
  111. }
  112. }
  113. /**
  114. * Represents a multiple choice question where only one choice should be selected.
  115. *
  116. * @copyright 2009 The Open University
  117. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  118. */
  119. class qtype_multichoice_single_question extends qtype_multichoice_base {
  120. public function get_renderer(moodle_page $page) {
  121. return $page->get_renderer('qtype_multichoice', 'single');
  122. }
  123. public function get_min_fraction() {
  124. $minfraction = 0;
  125. foreach ($this->answers as $ans) {
  126. $minfraction = min($minfraction, $ans->fraction);
  127. }
  128. return $minfraction;
  129. }
  130. /**
  131. * Return an array of the question type variables that could be submitted
  132. * as part of a question of this type, with their types, so they can be
  133. * properly cleaned.
  134. * @return array variable name => PARAM_... constant.
  135. */
  136. public function get_expected_data() {
  137. return array('answer' => PARAM_INT);
  138. }
  139. public function summarise_response(array $response) {
  140. if (!array_key_exists('answer', $response) ||
  141. !array_key_exists($response['answer'], $this->order)) {
  142. return null;
  143. }
  144. $ansid = $this->order[$response['answer']];
  145. return $this->html_to_text($this->answers[$ansid]->answer,
  146. $this->answers[$ansid]->answerformat);
  147. }
  148. public function classify_response(array $response) {
  149. if (!array_key_exists('answer', $response) ||
  150. !array_key_exists($response['answer'], $this->order)) {
  151. return array($this->id => question_classified_response::no_response());
  152. }
  153. $choiceid = $this->order[$response['answer']];
  154. $ans = $this->answers[$choiceid];
  155. return array($this->id => new question_classified_response($choiceid,
  156. $this->html_to_text($ans->answer, $ans->answerformat), $ans->fraction));
  157. }
  158. public function get_correct_response() {
  159. foreach ($this->order as $key => $answerid) {
  160. if (question_state::graded_state_for_fraction(
  161. $this->answers[$answerid]->fraction)->is_correct()) {
  162. return array('answer' => $key);
  163. }
  164. }
  165. return array();
  166. }
  167. public function is_same_response(array $prevresponse, array $newresponse) {
  168. return question_utils::arrays_same_at_key($prevresponse, $newresponse, 'answer');
  169. }
  170. public function is_complete_response(array $response) {
  171. return array_key_exists('answer', $response) && $response['answer'] !== '';
  172. }
  173. public function is_gradable_response(array $response) {
  174. return $this->is_complete_response($response);
  175. }
  176. public function grade_response(array $response) {
  177. if (array_key_exists('answer', $response) &&
  178. array_key_exists($response['answer'], $this->order)) {
  179. $fraction = $this->answers[$this->order[$response['answer']]]->fraction;
  180. } else {
  181. $fraction = 0;
  182. }
  183. return array($fraction, question_state::graded_state_for_fraction($fraction));
  184. }
  185. public function get_validation_error(array $response) {
  186. if ($this->is_gradable_response($response)) {
  187. return '';
  188. }
  189. return get_string('pleaseselectananswer', 'qtype_multichoice');
  190. }
  191. public function get_response(question_attempt $qa) {
  192. return $qa->get_last_qt_var('answer', -1);
  193. }
  194. public function is_choice_selected($response, $value) {
  195. return (string) $response === (string) $value;
  196. }
  197. }
  198. /**
  199. * Represents a multiple choice question where multiple choices can be selected.
  200. *
  201. * @copyright 2009 The Open University
  202. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  203. */
  204. class qtype_multichoice_multi_question extends qtype_multichoice_base {
  205. public function get_renderer(moodle_page $page) {
  206. return $page->get_renderer('qtype_multichoice', 'multi');
  207. }
  208. public function get_min_fraction() {
  209. return 0;
  210. }
  211. public function clear_wrong_from_response(array $response) {
  212. foreach ($this->order as $key => $ans) {
  213. if (array_key_exists($this->field($key), $response) &&
  214. question_state::graded_state_for_fraction(
  215. $this->answers[$ans]->fraction)->is_incorrect()) {
  216. $response[$this->field($key)] = 0;
  217. }
  218. }
  219. return $response;
  220. }
  221. public function get_num_parts_right(array $response) {
  222. $numright = 0;
  223. foreach ($this->order as $key => $ans) {
  224. $fieldname = $this->field($key);
  225. if (!array_key_exists($fieldname, $response) || !$response[$fieldname]) {
  226. continue;
  227. }
  228. if (!question_state::graded_state_for_fraction(
  229. $this->answers[$ans]->fraction)->is_incorrect()) {
  230. $numright += 1;
  231. }
  232. }
  233. return array($numright, count($this->order));
  234. }
  235. /**
  236. * @param int $key choice number
  237. * @return string the question-type variable name.
  238. */
  239. protected function field($key) {
  240. return 'choice' . $key;
  241. }
  242. public function get_expected_data() {
  243. $expected = array();
  244. foreach ($this->order as $key => $notused) {
  245. $expected[$this->field($key)] = PARAM_BOOL;
  246. }
  247. return $expected;
  248. }
  249. public function summarise_response(array $response) {
  250. $selectedchoices = array();
  251. foreach ($this->order as $key => $ans) {
  252. $fieldname = $this->field($key);
  253. if (array_key_exists($fieldname, $response) && $response[$fieldname]) {
  254. $selectedchoices[] = $this->html_to_text($this->answers[$ans]->answer,
  255. $this->answers[$ans]->answerformat);
  256. }
  257. }
  258. if (empty($selectedchoices)) {
  259. return null;
  260. }
  261. return implode('; ', $selectedchoices);
  262. }
  263. public function classify_response(array $response) {
  264. $selectedchoices = array();
  265. foreach ($this->order as $key => $ansid) {
  266. $fieldname = $this->field($key);
  267. if (array_key_exists($fieldname, $response) && $response[$fieldname]) {
  268. $selectedchoices[$ansid] = 1;
  269. }
  270. }
  271. $choices = array();
  272. foreach ($this->answers as $ansid => $ans) {
  273. if (isset($selectedchoices[$ansid])) {
  274. $choices[$ansid] = new question_classified_response($ansid,
  275. $this->html_to_text($ans->answer, $ans->answerformat), $ans->fraction);
  276. }
  277. }
  278. return $choices;
  279. }
  280. public function get_correct_response() {
  281. $response = array();
  282. foreach ($this->order as $key => $ans) {
  283. if (!question_state::graded_state_for_fraction(
  284. $this->answers[$ans]->fraction)->is_incorrect()) {
  285. $response[$this->field($key)] = 1;
  286. }
  287. }
  288. return $response;
  289. }
  290. public function is_same_response(array $prevresponse, array $newresponse) {
  291. foreach ($this->order as $key => $notused) {
  292. $fieldname = $this->field($key);
  293. if (!question_utils::arrays_same_at_key($prevresponse, $newresponse, $fieldname)) {
  294. return false;
  295. }
  296. }
  297. return true;
  298. }
  299. public function is_complete_response(array $response) {
  300. foreach ($this->order as $key => $notused) {
  301. if (!empty($response[$this->field($key)])) {
  302. return true;
  303. }
  304. }
  305. return false;
  306. }
  307. public function is_gradable_response(array $response) {
  308. return $this->is_complete_response($response);
  309. }
  310. /**
  311. * @param array $response responses, as returned by
  312. * {@link question_attempt_step::get_qt_data()}.
  313. * @return int the number of choices that were selected. in this response.
  314. */
  315. public function get_num_selected_choices(array $response) {
  316. $numselected = 0;
  317. foreach ($response as $key => $value) {
  318. if (!empty($value)) {
  319. $numselected += 1;
  320. }
  321. }
  322. return $numselected;
  323. }
  324. /**
  325. * @return int the number of choices that are correct.
  326. */
  327. public function get_num_correct_choices() {
  328. $numcorrect = 0;
  329. foreach ($this->answers as $ans) {
  330. if (!question_state::graded_state_for_fraction($ans->fraction)->is_incorrect()) {
  331. $numcorrect += 1;
  332. }
  333. }
  334. return $numcorrect;
  335. }
  336. public function grade_response(array $response) {
  337. $fraction = 0;
  338. foreach ($this->order as $key => $ansid) {
  339. if (!empty($response[$this->field($key)])) {
  340. $fraction += $this->answers[$ansid]->fraction;
  341. }
  342. }
  343. $fraction = min(max(0, $fraction), 1.0);
  344. return array($fraction, question_state::graded_state_for_fraction($fraction));
  345. }
  346. public function get_validation_error(array $response) {
  347. if ($this->is_gradable_response($response)) {
  348. return '';
  349. }
  350. return get_string('pleaseselectatleastoneanswer', 'qtype_multichoice');
  351. }
  352. /**
  353. * Disable those hint settings that we don't want when the student has selected
  354. * more choices than the number of right choices. This avoids giving the game away.
  355. * @param question_hint_with_parts $hint a hint.
  356. */
  357. protected function disable_hint_settings_when_too_many_selected(
  358. question_hint_with_parts $hint) {
  359. $hint->clearwrong = false;
  360. }
  361. public function get_hint($hintnumber, question_attempt $qa) {
  362. $hint = parent::get_hint($hintnumber, $qa);
  363. if (is_null($hint)) {
  364. return $hint;
  365. }
  366. if ($this->get_num_selected_choices($qa->get_last_qt_data()) >
  367. $this->get_num_correct_choices()) {
  368. $hint = clone($hint);
  369. $this->disable_hint_settings_when_too_many_selected($hint);
  370. }
  371. return $hint;
  372. }
  373. public function get_response(question_attempt $qa) {
  374. return $qa->get_last_qt_data();
  375. }
  376. public function is_choice_selected($response, $value) {
  377. return !empty($response['choice' . $value]);
  378. }
  379. }