3 // This file is part of Moodle - http://moodle.org/
5 // Moodle is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
10 // Moodle is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
19 * Question type class for the embedded element in question text question types.
22 * @subpackage gapselect
23 * @copyright 2011 The Open University
24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
28 defined('MOODLE_INTERNAL') || die();
30 require_once($CFG->libdir . '/questionlib.php');
31 require_once($CFG->dirroot . '/question/engine/lib.php');
32 require_once($CFG->dirroot . '/question/format/xml/format.php');
36 * The embedded element in question text question type class.
38 * @copyright 2011 The Open University
39 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
41 abstract class qtype_gapselect_base extends question_type {
43 * Choices are stored in the question_answers table, and any options need to
44 * be put into the feedback field somehow. This method is responsible for
45 * converting all the options to a single string for this purpose. It is used
46 * by {@link save_question_options()}.
47 * @param array $choice the form data relating to this choice.
48 * @return string ready to store in the database.
50 protected abstract function choice_options_to_feedback($choice);
52 public function save_question_options($question) {
54 $context = $question->context;
55 $result = new stdClass();
57 $oldanswers = $DB->get_records('question_answers',
58 array('question' => $question->id), 'id ASC');
60 // Insert all the new answers
61 foreach ($question->choices as $key => $choice) {
63 if (trim($choice['answer']) == '') {
67 $feedback = $this->choice_options_to_feedback($choice);
69 if ($answer = array_shift($oldanswers)) {
70 $answer->answer = $choice['answer'];
71 $answer->feedback = $feedback;
72 $DB->update_record('question_answers', $answer);
75 $answer = new stdClass();
76 $answer->question = $question->id;
77 $answer->answer = $choice['answer'];
78 $answer->answerformat = FORMAT_HTML;
79 $answer->fraction = 0;
80 $answer->feedback = $feedback;
81 $answer->feedbackformat = 0;
82 $DB->insert_record('question_answers', $answer);
86 // Delete old answer records
87 foreach($oldanswers as $oa) {
88 delete_records('question_answers', 'id', $oa->id);
91 $options = $DB->get_record('question_' . $this->name(), array('questionid' => $question->id));
93 $options = new stdClass();
94 $options->questionid = $question->id;
95 $options->correctfeedback = '';
96 $options->partiallycorrectfeedback = '';
97 $options->incorrectfeedback = '';
98 $options->id = $DB->insert_record('question_' . $this->name(), $options);
101 $options->shuffleanswers = !empty($question->shuffleanswers);
102 $options = $this->save_combined_feedback_helper($options, $question, $context, true);
103 $DB->update_record('question_' . $this->name(), $options);
105 $this->save_hints($question, true);
108 public function get_question_options($question) {
110 $question->options = $DB->get_record('question_'.$this->name(),
111 array('questionid' => $question->id), '*', MUST_EXIST);
112 parent::get_question_options($question);
115 public function delete_question($questionid, $contextid) {
117 $DB->delete_records('question_'.$this->name(), array('questionid' => $questionid));
118 return parent::delete_question($questionid, $contextid);
122 * Used by {@link initialise_question_instance()} to set up the choice-specific data.
123 * @param object $choicedata as loaded from the question_answers table.
124 * @return object an appropriate object for representing the choice.
126 protected abstract function make_choice($choicedata);
128 protected function initialise_question_instance(question_definition $question, $questiondata) {
129 parent::initialise_question_instance($question, $questiondata);
131 $question->shufflechoices = $questiondata->options->shuffleanswers;
133 $this->initialise_combined_feedback($question, $questiondata, true);
135 $question->choices = array();
136 $choiceindexmap= array();
138 // Store the choices in arrays by group.
140 foreach ($questiondata->options->answers as $choicedata) {
141 $choice = $this->make_choice($choicedata);
143 if (array_key_exists($choice->choice_group(), $question->choices)) {
144 $question->choices[$choice->choice_group()][] = $choice;
146 $question->choices[$choice->choice_group()][1] = $choice;
149 end($question->choices[$choice->choice_group()]);
150 $choiceindexmap[$i] = array($choice->choice_group(),
151 key($question->choices[$choice->choice_group()]));
155 $question->places = array();
156 $question->textfragments = array();
157 $question->rightchoices = array();
158 // Break up the question text, and store the fragments, places and right answers.
160 $bits = preg_split('/\[\[(\d+)]]/', $question->questiontext, null, PREG_SPLIT_DELIM_CAPTURE);
161 $question->textfragments[0] = array_shift($bits);
164 while (!empty($bits)) {
165 $choice = array_shift($bits);
167 list($group, $choiceindex) = $choiceindexmap[$choice];
168 $question->places[$i] = $group;
169 $question->rightchoices[$i] = $choiceindex;
171 $question->textfragments[$i] = array_shift($bits);
176 protected function make_hint($hint) {
177 return question_hint_with_parts::load_from_record($hint);
180 public function get_random_guess_score($questiondata) {
181 $question = $this->make_question($questiondata);
182 return $question->get_random_guess_score();
186 * This function should reverse {@link choice_options_to_feedback()}.
187 * @param string $feedback the data loaded from the database.
188 * @return array the choice options.
190 protected abstract function feedback_to_choice_options($feedback);
193 * This method gets the choices (answers)
194 * in a 2 dimentional array.
196 * @param object $question
197 * @return array of groups
199 protected function get_array_of_choices($question) {
200 $subquestions = $question->options->answers;
202 foreach ($subquestions as $key=>$subquestion) {
203 $answers[$count]['id'] = $subquestion->id;
204 $answers[$count]['answer'] = $subquestion->answer;
205 $answers[$count]['fraction'] = $subquestion->fraction;
206 $answers[$count] += $this->feedback_to_choice_options($subquestion->feedback);
207 $answers[$count]['choice'] = $count+1;
214 * This method gets the choices (answers) and sort them by groups
215 * in a 2 dimentional array.
217 * @param object $question
218 * @return array of groups
220 protected function get_array_of_groups($question, $state) {
221 $answers = $this->get_array_of_choices($question);
223 for ($group=1; $group < count($answers); $group++) {
224 $players = $this->get_group_of_players($question, $state, $answers, $group);
226 $arr[$group]= $players;
233 * This method gets the correct answers in a 2 dimentional array.
235 * @param object $question
236 * @return array of groups
238 protected function get_correct_answers($question) {
239 $arrayofchoices = $this->get_array_of_choices($question);
240 $arrayofplaceholdeers = $this->get_array_of_placeholders($question);
242 $correctplayers = array();
243 foreach($arrayofplaceholdeers as $ph) {
244 foreach($arrayofchoices as $key=>$choice) {
245 if(($key+1) == $ph) {
246 $correctplayers[]= $choice;
250 return $correctplayers;
253 protected function get_array_of_placeholders($question) {
254 $qtext = $question->questiontext;
255 $error = '<b> ERROR</b>: Please check the form for this question. ';
257 echo $error . 'The question text is empty!';
262 $slots = $this->getEmbeddedTextArray($question);
265 echo $error . 'The question text is not in the correct format!';
270 foreach ($slots as $slot) {
271 $output[] = substr($slot, 2, strlen($slot) - 4); //2 is for '[[' and 4 is for '[[]]'.
276 protected function get_group_of_players($question, $state, $subquestions, $group) {
277 $goupofanswers = array();
278 foreach ($subquestions as $key => $subquestion) {
279 if ($subquestion[$this->choice_group_key()] == $group) {
280 $goupofanswers[] = $subquestion;
284 // Shuffle answers within this group
285 if ($question->options->shuffleanswers == 1) {
286 shuffle($goupofanswers);
288 return $goupofanswers;
291 public function get_possible_responses($questiondata) {
292 $question = $this->make_question($questiondata);
295 foreach ($question->places as $place => $group) {
298 foreach ($question->choices[$group] as $i => $choice) {
299 $choices[$i] = new question_possible_response(
300 html_to_text($choice->text, 0, false),
301 $question->rightchoices[$place] == $i);
303 $choices[null] = question_possible_response::no_response();
305 $parts[$place] = $choices;
311 function move_files($questionid, $oldcontextid, $newcontextid) {
312 parent::move_files($questionid, $oldcontextid, $newcontextid);
314 $fs = get_file_storage();
315 $fs->move_area_files_to_new_context($oldcontextid,
316 $newcontextid, 'question', 'correctfeedback', $questionid);
317 $fs->move_area_files_to_new_context($oldcontextid,
318 $newcontextid, 'question', 'partiallycorrectfeedback', $questionid);
319 $fs->move_area_files_to_new_context($oldcontextid,
320 $newcontextid, 'question', 'incorrectfeedback', $questionid);
323 protected function delete_files($questionid, $contextid) {
324 parent::delete_files($questionid, $contextid);
326 $fs = get_file_storage();
327 $fs->delete_area_files($contextid, 'question', 'correctfeedback', $questionid);
328 $fs->delete_area_files($contextid, 'question', 'partiallycorrectfeedback', $questionid);
329 $fs->delete_area_files($contextid, 'question', 'incorrectfeedback', $questionid);