PageRenderTime 57ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/question/type/gapselect/questionbase.php

https://gitlab.com/unofficial-mirrors/moodle
PHP | 334 lines | 220 code | 42 blank | 72 comment | 31 complexity | c926b5d8cee5a6533477a7b5e726380a 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. * Definition class for embedded element in question text question.
  18. *
  19. * Used by gap-select, drag and drop and possibly others.
  20. *
  21. * @package qtype_gapselect
  22. * @copyright 2011 The Open University
  23. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24. */
  25. defined('MOODLE_INTERNAL') || die();
  26. require_once($CFG->dirroot . '/question/type/questionbase.php');
  27. /**
  28. * Represents embedded element in question text question.
  29. *
  30. * Parent of drag and drop and select from drop down list and others.
  31. *
  32. * @copyright 2011 The Open University
  33. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  34. */
  35. abstract class qtype_gapselect_question_base extends question_graded_automatically_with_countback {
  36. /** @var boolean Whether the question stems should be shuffled. */
  37. public $shufflechoices;
  38. /** @var string Feedback for any correct response. */
  39. public $correctfeedback;
  40. /** @var int format of $correctfeedback. */
  41. public $correctfeedbackformat;
  42. /** @var string Feedback for any partially correct response. */
  43. public $partiallycorrectfeedback;
  44. /** @var int format of $partiallycorrectfeedback. */
  45. public $partiallycorrectfeedbackformat;
  46. /** @var string Feedback for any incorrect response. */
  47. public $incorrectfeedback;
  48. /** @var int format of $incorrectfeedback. */
  49. public $incorrectfeedbackformat;
  50. /**
  51. * @var array of arrays. The outer keys are the choice group numbers.
  52. * The inner keys for most question types number sequentialy from 1. However
  53. * for ddimageortext questions it is strange (and difficult to change now).
  54. * the first item in each group gets numbered 1, and the other items get numbered
  55. * $choice->no. Be careful!
  56. * The values are arrays of qtype_gapselect_choice objects (or a subclass).
  57. */
  58. public $choices;
  59. /**
  60. * @var array place number => group number of the places in the question
  61. * text where choices can be put. Places are numbered from 1.
  62. */
  63. public $places;
  64. /**
  65. * @var array of strings, one longer than $places, which is achieved by
  66. * indexing from 0. The bits of question text that go between the placeholders.
  67. */
  68. public $textfragments;
  69. /** @var array index of the right choice for each stem. */
  70. public $rightchoices;
  71. /** @var array shuffled choice indexes. */
  72. protected $choiceorder;
  73. public function start_attempt(question_attempt_step $step, $variant) {
  74. foreach ($this->choices as $group => $choices) {
  75. $choiceorder = array_keys($choices);
  76. if ($this->shufflechoices) {
  77. shuffle($choiceorder);
  78. }
  79. $step->set_qt_var('_choiceorder' . $group, implode(',', $choiceorder));
  80. $this->set_choiceorder($group, $choiceorder);
  81. }
  82. }
  83. public function apply_attempt_state(question_attempt_step $step) {
  84. foreach ($this->choices as $group => $choices) {
  85. $this->set_choiceorder($group, explode(',',
  86. $step->get_qt_var('_choiceorder' . $group)));
  87. }
  88. }
  89. /**
  90. * Helper method used by both {@link start_attempt()} and
  91. * {@link apply_attempt_state()}.
  92. * @param int $group the group number.
  93. * @param array $choiceorder the choices, in order.
  94. */
  95. protected function set_choiceorder($group, $choiceorder) {
  96. foreach ($choiceorder as $key => $value) {
  97. $this->choiceorder[$group][$key + 1] = $value;
  98. }
  99. }
  100. public function get_question_summary() {
  101. $question = $this->html_to_text($this->questiontext, $this->questiontextformat);
  102. $groups = array();
  103. foreach ($this->choices as $group => $choices) {
  104. $cs = array();
  105. foreach ($choices as $choice) {
  106. $cs[] = html_to_text($choice->text, 0, false);
  107. }
  108. $groups[] = '[[' . $group . ']] -> {' . implode(' / ', $cs) . '}';
  109. }
  110. return $question . '; ' . implode('; ', $groups);
  111. }
  112. protected function get_selected_choice($group, $shuffledchoicenumber) {
  113. $choiceno = $this->choiceorder[$group][$shuffledchoicenumber];
  114. return $this->choices[$group][$choiceno];
  115. }
  116. public function summarise_response(array $response) {
  117. $matches = array();
  118. $allblank = true;
  119. foreach ($this->places as $place => $group) {
  120. if (array_key_exists($this->field($place), $response) &&
  121. $response[$this->field($place)]) {
  122. $choices[] = '{' . $this->summarise_choice(
  123. $this->get_selected_choice($group, $response[$this->field($place)])) . '}';
  124. $allblank = false;
  125. } else {
  126. $choices[] = '{}';
  127. }
  128. }
  129. if ($allblank) {
  130. return null;
  131. }
  132. return implode(' ', $choices);
  133. }
  134. /**
  135. * Convert a choice to plain text.
  136. * @param qtype_gapselect_choice $choice one of the choices for a place.
  137. * @return a plain text summary of the choice.
  138. */
  139. public function summarise_choice($choice) {
  140. return $this->html_to_text($choice->text, FORMAT_PLAIN);
  141. }
  142. public function get_random_guess_score() {
  143. $accum = 0;
  144. foreach ($this->places as $placegroup) {
  145. $accum += 1 / count($this->choices[$placegroup]);
  146. }
  147. return $accum / count($this->places);
  148. }
  149. public function clear_wrong_from_response(array $response) {
  150. foreach ($this->places as $place => $notused) {
  151. if (array_key_exists($this->field($place), $response) &&
  152. $response[$this->field($place)] != $this->get_right_choice_for($place)) {
  153. $response[$this->field($place)] = '0';
  154. }
  155. }
  156. return $response;
  157. }
  158. public function get_num_parts_right(array $response) {
  159. $numright = 0;
  160. foreach ($this->places as $place => $notused) {
  161. if (!array_key_exists($this->field($place), $response)) {
  162. continue;
  163. }
  164. if ($response[$this->field($place)] == $this->get_right_choice_for($place)) {
  165. $numright += 1;
  166. }
  167. }
  168. return array($numright, count($this->places));
  169. }
  170. /**
  171. * Get the field name corresponding to a given place.
  172. * @param int $place stem number
  173. * @return string the question-type variable name.
  174. */
  175. public function field($place) {
  176. return 'p' . $place;
  177. }
  178. public function get_expected_data() {
  179. $vars = array();
  180. foreach ($this->places as $place => $notused) {
  181. $vars[$this->field($place)] = PARAM_INTEGER;
  182. }
  183. return $vars;
  184. }
  185. public function get_correct_response() {
  186. $response = array();
  187. foreach ($this->places as $place => $notused) {
  188. $response[$this->field($place)] = $this->get_right_choice_for($place);
  189. }
  190. return $response;
  191. }
  192. public function get_right_choice_for($place) {
  193. $group = $this->places[$place];
  194. foreach ($this->choiceorder[$group] as $choicekey => $choiceid) {
  195. if ($this->rightchoices[$place] == $choiceid) {
  196. return $choicekey;
  197. }
  198. }
  199. }
  200. public function get_ordered_choices($group) {
  201. $choices = array();
  202. foreach ($this->choiceorder[$group] as $choicekey => $choiceid) {
  203. $choices[$choicekey] = $this->choices[$group][$choiceid];
  204. }
  205. return $choices;
  206. }
  207. public function is_complete_response(array $response) {
  208. $complete = true;
  209. foreach ($this->places as $place => $notused) {
  210. $complete = $complete && !empty($response[$this->field($place)]);
  211. }
  212. return $complete;
  213. }
  214. public function is_gradable_response(array $response) {
  215. foreach ($this->places as $place => $notused) {
  216. if (!empty($response[$this->field($place)])) {
  217. return true;
  218. }
  219. }
  220. return false;
  221. }
  222. public function is_same_response(array $prevresponse, array $newresponse) {
  223. foreach ($this->places as $place => $notused) {
  224. $fieldname = $this->field($place);
  225. if (!question_utils::arrays_same_at_key_integer(
  226. $prevresponse, $newresponse, $fieldname)) {
  227. return false;
  228. }
  229. }
  230. return true;
  231. }
  232. public function get_validation_error(array $response) {
  233. if ($this->is_complete_response($response)) {
  234. return '';
  235. }
  236. return get_string('pleaseputananswerineachbox', 'qtype_gapselect');
  237. }
  238. public function grade_response(array $response) {
  239. list($right, $total) = $this->get_num_parts_right($response);
  240. $fraction = $right / $total;
  241. return array($fraction, question_state::graded_state_for_fraction($fraction));
  242. }
  243. public function compute_final_grade($responses, $totaltries) {
  244. $totalscore = 0;
  245. foreach ($this->places as $place => $notused) {
  246. $fieldname = $this->field($place);
  247. $lastwrongindex = -1;
  248. $finallyright = false;
  249. foreach ($responses as $i => $response) {
  250. if (!array_key_exists($fieldname, $response) ||
  251. $response[$fieldname] != $this->get_right_choice_for($place)) {
  252. $lastwrongindex = $i;
  253. $finallyright = false;
  254. } else {
  255. $finallyright = true;
  256. }
  257. }
  258. if ($finallyright) {
  259. $totalscore += max(0, 1 - ($lastwrongindex + 1) * $this->penalty);
  260. }
  261. }
  262. return $totalscore / count($this->places);
  263. }
  264. public function classify_response(array $response) {
  265. $parts = array();
  266. foreach ($this->places as $place => $group) {
  267. if (!array_key_exists($this->field($place), $response) ||
  268. !$response[$this->field($place)]) {
  269. $parts[$place] = question_classified_response::no_response();
  270. continue;
  271. }
  272. $fieldname = $this->field($place);
  273. $choiceno = $this->choiceorder[$group][$response[$fieldname]];
  274. $choice = $this->choices[$group][$choiceno];
  275. $parts[$place] = new question_classified_response(
  276. $choiceno, html_to_text($choice->text, 0, false),
  277. ($this->get_right_choice_for($place) == $response[$fieldname]) / count($this->places));
  278. }
  279. return $parts;
  280. }
  281. public function check_file_access($qa, $options, $component, $filearea, $args, $forcedownload) {
  282. if ($component == 'question' && in_array($filearea,
  283. array('correctfeedback', 'partiallycorrectfeedback', 'incorrectfeedback'))) {
  284. return $this->check_combined_feedback_file_access($qa, $options, $filearea, $args);
  285. } else if ($component == 'question' && $filearea == 'hint') {
  286. return $this->check_hint_file_access($qa, $options, $args);
  287. } else {
  288. return parent::check_file_access($qa, $options, $component, $filearea,
  289. $args, $forcedownload);
  290. }
  291. }
  292. }