PageRenderTime 50ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/question/type/gapselect/questiontypebase.php

http://github.com/moodle/moodle
PHP | 327 lines | 199 code | 51 blank | 77 comment | 20 complexity | 78a783becffb2315e3c68cb9f5f4e95f MD5 | raw file
Possible License(s): MIT, AGPL-3.0, MPL-2.0-no-copyleft-exception, LGPL-3.0, GPL-3.0, Apache-2.0, LGPL-2.1, BSD-3-Clause
  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. * Question type class for the embedded element in question text question types.
  18. *
  19. * @package qtype_gapselect
  20. * @copyright 2011 The Open University
  21. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  22. */
  23. defined('MOODLE_INTERNAL') || die();
  24. require_once($CFG->libdir . '/questionlib.php');
  25. require_once($CFG->dirroot . '/question/engine/lib.php');
  26. require_once($CFG->dirroot . '/question/format/xml/format.php');
  27. /**
  28. * The embedded element in question text question type class.
  29. *
  30. * @copyright 2011 The Open University
  31. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  32. */
  33. abstract class qtype_gapselect_base extends question_type {
  34. /**
  35. * Choices are stored in the question_answers table, and any options need to
  36. * be put into the feedback field somehow. This method is responsible for
  37. * converting all the options to a single string for this purpose. It is used
  38. * by {@link save_question_options()}.
  39. * @param array $choice the form data relating to this choice.
  40. * @return string ready to store in the database.
  41. */
  42. protected abstract function choice_options_to_feedback($choice);
  43. public function save_question_options($question) {
  44. global $DB;
  45. $context = $question->context;
  46. $result = new stdClass();
  47. $oldanswers = $DB->get_records('question_answers',
  48. array('question' => $question->id), 'id ASC');
  49. // Insert all the new answers.
  50. foreach ($question->choices as $key => $choice) {
  51. if (trim($choice['answer']) == '') {
  52. continue;
  53. }
  54. $feedback = $this->choice_options_to_feedback($choice);
  55. if ($answer = array_shift($oldanswers)) {
  56. $answer->answer = $choice['answer'];
  57. $answer->feedback = $feedback;
  58. $DB->update_record('question_answers', $answer);
  59. } else {
  60. $answer = new stdClass();
  61. $answer->question = $question->id;
  62. $answer->answer = $choice['answer'];
  63. $answer->answerformat = FORMAT_HTML;
  64. $answer->fraction = 0;
  65. $answer->feedback = $feedback;
  66. $answer->feedbackformat = 0;
  67. $DB->insert_record('question_answers', $answer);
  68. }
  69. }
  70. // Delete old answer records.
  71. foreach ($oldanswers as $oa) {
  72. $DB->delete_records('question_answers', array('id' => $oa->id));
  73. }
  74. $options = $DB->get_record('question_' . $this->name(),
  75. array('questionid' => $question->id));
  76. if (!$options) {
  77. $options = new stdClass();
  78. $options->questionid = $question->id;
  79. $options->correctfeedback = '';
  80. $options->partiallycorrectfeedback = '';
  81. $options->incorrectfeedback = '';
  82. $options->id = $DB->insert_record('question_' . $this->name(), $options);
  83. }
  84. $options->shuffleanswers = !empty($question->shuffleanswers);
  85. $options = $this->save_combined_feedback_helper($options, $question, $context, true);
  86. $DB->update_record('question_' . $this->name(), $options);
  87. $this->save_hints($question, true);
  88. }
  89. public function get_question_options($question) {
  90. global $DB;
  91. $question->options = $DB->get_record('question_'.$this->name(),
  92. array('questionid' => $question->id), '*', MUST_EXIST);
  93. parent::get_question_options($question);
  94. }
  95. public function delete_question($questionid, $contextid) {
  96. global $DB;
  97. $DB->delete_records('question_'.$this->name(), array('questionid' => $questionid));
  98. return parent::delete_question($questionid, $contextid);
  99. }
  100. /**
  101. * Used by {@link initialise_question_instance()} to set up the choice-specific data.
  102. * @param object $choicedata as loaded from the question_answers table.
  103. * @return object an appropriate object for representing the choice.
  104. */
  105. protected abstract function make_choice($choicedata);
  106. protected function initialise_question_instance(question_definition $question, $questiondata) {
  107. parent::initialise_question_instance($question, $questiondata);
  108. $question->shufflechoices = $questiondata->options->shuffleanswers;
  109. $this->initialise_combined_feedback($question, $questiondata, true);
  110. $question->choices = array();
  111. $choiceindexmap = array();
  112. // Store the choices in arrays by group.
  113. $i = 1;
  114. foreach ($questiondata->options->answers as $choicedata) {
  115. $choice = $this->make_choice($choicedata);
  116. if (array_key_exists($choice->choice_group(), $question->choices)) {
  117. $question->choices[$choice->choice_group()][] = $choice;
  118. } else {
  119. $question->choices[$choice->choice_group()][1] = $choice;
  120. }
  121. end($question->choices[$choice->choice_group()]);
  122. $choiceindexmap[$i] = array($choice->choice_group(),
  123. key($question->choices[$choice->choice_group()]));
  124. $i += 1;
  125. }
  126. $question->places = array();
  127. $question->textfragments = array();
  128. $question->rightchoices = array();
  129. // Break up the question text, and store the fragments, places and right answers.
  130. $bits = preg_split('/\[\[(\d+)]]/', $question->questiontext,
  131. null, PREG_SPLIT_DELIM_CAPTURE);
  132. $question->textfragments[0] = array_shift($bits);
  133. $i = 1;
  134. while (!empty($bits)) {
  135. $choice = array_shift($bits);
  136. list($group, $choiceindex) = $choiceindexmap[$choice];
  137. $question->places[$i] = $group;
  138. $question->rightchoices[$i] = $choiceindex;
  139. $question->textfragments[$i] = array_shift($bits);
  140. $i += 1;
  141. }
  142. }
  143. protected function make_hint($hint) {
  144. return question_hint_with_parts::load_from_record($hint);
  145. }
  146. public function get_random_guess_score($questiondata) {
  147. $question = $this->make_question($questiondata);
  148. return $question->get_random_guess_score();
  149. }
  150. /**
  151. * This function should reverse {@link choice_options_to_feedback()}.
  152. * @param string $feedback the data loaded from the database.
  153. * @return array the choice options.
  154. */
  155. protected abstract function feedback_to_choice_options($feedback);
  156. /**
  157. * This method gets the choices (answers)
  158. * in a 2 dimentional array.
  159. *
  160. * @param object $question
  161. * @return array of groups
  162. */
  163. protected function get_array_of_choices($question) {
  164. $subquestions = $question->options->answers;
  165. $count = 0;
  166. foreach ($subquestions as $key => $subquestion) {
  167. $answers[$count]['id'] = $subquestion->id;
  168. $answers[$count]['answer'] = $subquestion->answer;
  169. $answers[$count]['fraction'] = $subquestion->fraction;
  170. $answers[$count] += $this->feedback_to_choice_options($subquestion->feedback);
  171. $answers[$count]['choice'] = $count + 1;
  172. ++$count;
  173. }
  174. return $answers;
  175. }
  176. /**
  177. * This method gets the choices (answers) and sort them by groups
  178. * in a 2 dimentional array.
  179. *
  180. * @param object $question
  181. * @param object $state Question state object
  182. * @return array of groups
  183. */
  184. protected function get_array_of_groups($question, $state) {
  185. $answers = $this->get_array_of_choices($question);
  186. $arr = array();
  187. for ($group = 1; $group < count($answers); $group++) {
  188. $players = $this->get_group_of_players($question, $state, $answers, $group);
  189. if ($players) {
  190. $arr[$group] = $players;
  191. }
  192. }
  193. return $arr;
  194. }
  195. /**
  196. * This method gets the correct answers in a 2 dimentional array.
  197. *
  198. * @param object $question
  199. * @return array of groups
  200. */
  201. protected function get_correct_answers($question) {
  202. $arrayofchoices = $this->get_array_of_choices($question);
  203. $arrayofplaceholdeers = $this->get_array_of_placeholders($question);
  204. $correctplayers = array();
  205. foreach ($arrayofplaceholdeers as $ph) {
  206. foreach ($arrayofchoices as $key => $choice) {
  207. if ($key + 1 == $ph) {
  208. $correctplayers[] = $choice;
  209. }
  210. }
  211. }
  212. return $correctplayers;
  213. }
  214. /**
  215. * Return the list of groups used in a question.
  216. * @param stdClass $question the question data.
  217. * @return array the groups used, or false if an error occurs.
  218. */
  219. protected function get_array_of_placeholders($question) {
  220. $qtext = $question->questiontext;
  221. $error = '<b> ERROR</b>: Please check the form for this question. ';
  222. if (!$qtext) {
  223. echo $error . 'The question text is empty!';
  224. return false;
  225. }
  226. // Get the slots.
  227. $slots = $this->getEmbeddedTextArray($question);
  228. if (!$slots) {
  229. echo $error . 'The question text is not in the correct format!';
  230. return false;
  231. }
  232. $output = array();
  233. foreach ($slots as $slot) {
  234. $output[] = substr($slot, 2, strlen($slot) - 4); // 2 is for '[[' and 4 is for '[[]]'.
  235. }
  236. return $output;
  237. }
  238. protected function get_group_of_players($question, $state, $subquestions, $group) {
  239. $goupofanswers = array();
  240. foreach ($subquestions as $key => $subquestion) {
  241. if ($subquestion[$this->choice_group_key()] == $group) {
  242. $goupofanswers[] = $subquestion;
  243. }
  244. }
  245. // Shuffle answers within this group.
  246. if ($question->options->shuffleanswers == 1) {
  247. shuffle($goupofanswers);
  248. }
  249. return $goupofanswers;
  250. }
  251. public function get_possible_responses($questiondata) {
  252. $question = $this->make_question($questiondata);
  253. $parts = array();
  254. foreach ($question->places as $place => $group) {
  255. $choices = array();
  256. foreach ($question->choices[$group] as $i => $choice) {
  257. $choices[$i] = new question_possible_response(
  258. html_to_text($choice->text, 0, false),
  259. ($question->rightchoices[$place] == $i) / count($question->places));
  260. }
  261. $choices[null] = question_possible_response::no_response();
  262. $parts[$place] = $choices;
  263. }
  264. return $parts;
  265. }
  266. public function move_files($questionid, $oldcontextid, $newcontextid) {
  267. parent::move_files($questionid, $oldcontextid, $newcontextid);
  268. $this->move_files_in_combined_feedback($questionid, $oldcontextid, $newcontextid);
  269. $this->move_files_in_hints($questionid, $oldcontextid, $newcontextid);
  270. }
  271. protected function delete_files($questionid, $contextid) {
  272. parent::delete_files($questionid, $contextid);
  273. $this->delete_files_in_combined_feedback($questionid, $contextid);
  274. $this->delete_files_in_hints($questionid, $contextid);
  275. }
  276. }