2 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
18 * Question type class for the embedded element in question text question types.
21 * @subpackage gapselect
22 * @copyright 2011 The Open University
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
27 defined('MOODLE_INTERNAL') || die();
29 require_once($CFG->libdir . '/questionlib.php');
30 require_once($CFG->dirroot . '/question/engine/lib.php');
31 require_once($CFG->dirroot . '/question/format/xml/format.php');
35 * The embedded element in question text question type class.
37 * @copyright 2011 The Open University
38 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
40 abstract class qtype_gapselect_base extends question_type {
42 * Choices are stored in the question_answers table, and any options need to
43 * be put into the feedback field somehow. This method is responsible for
44 * converting all the options to a single string for this purpose. It is used
45 * by {@link save_question_options()}.
46 * @param array $choice the form data relating to this choice.
47 * @return string ready to store in the database.
49 protected abstract function choice_options_to_feedback($choice);
51 public function save_question_options($question) {
53 $context = $question->context;
54 $result = new stdClass();
56 $oldanswers = $DB->get_records('question_answers',
57 array('question' => $question->id), 'id ASC');
59 // Insert all the new answers
60 foreach ($question->choices as $key => $choice) {
62 if (trim($choice['answer']) == '') {
66 $feedback = $this->choice_options_to_feedback($choice);
68 if ($answer = array_shift($oldanswers)) {
69 $answer->answer = $choice['answer'];
70 $answer->feedback = $feedback;
71 $DB->update_record('question_answers', $answer);
74 $answer = new stdClass();
75 $answer->question = $question->id;
76 $answer->answer = $choice['answer'];
77 $answer->answerformat = FORMAT_HTML;
78 $answer->fraction = 0;
79 $answer->feedback = $feedback;
80 $answer->feedbackformat = 0;
81 $DB->insert_record('question_answers', $answer);
85 // Delete old answer records
86 foreach ($oldanswers as $oa) {
87 $DB->delete_records('question_answers', array('id' => $oa->id));
90 $options = $DB->get_record('question_' . $this->name(),
91 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,
161 null, PREG_SPLIT_DELIM_CAPTURE);
162 $question->textfragments[0] = array_shift($bits);
165 while (!empty($bits)) {
166 $choice = array_shift($bits);
168 list($group, $choiceindex) = $choiceindexmap[$choice];
169 $question->places[$i] = $group;
170 $question->rightchoices[$i] = $choiceindex;
172 $question->textfragments[$i] = array_shift($bits);
177 protected function make_hint($hint) {
178 return question_hint_with_parts::load_from_record($hint);
181 public function get_random_guess_score($questiondata) {
182 $question = $this->make_question($questiondata);
183 return $question->get_random_guess_score();
187 * This function should reverse {@link choice_options_to_feedback()}.
188 * @param string $feedback the data loaded from the database.
189 * @return array the choice options.
191 protected abstract function feedback_to_choice_options($feedback);
194 * This method gets the choices (answers)
195 * in a 2 dimentional array.
197 * @param object $question
198 * @return array of groups
200 protected function get_array_of_choices($question) {
201 $subquestions = $question->options->answers;
203 foreach ($subquestions as $key => $subquestion) {
204 $answers[$count]['id'] = $subquestion->id;
205 $answers[$count]['answer'] = $subquestion->answer;
206 $answers[$count]['fraction'] = $subquestion->fraction;
207 $answers[$count] += $this->feedback_to_choice_options($subquestion->feedback);
208 $answers[$count]['choice'] = $count+1;
215 * This method gets the choices (answers) and sort them by groups
216 * in a 2 dimentional array.
218 * @param object $question
219 * @return array of groups
221 protected function get_array_of_groups($question, $state) {
222 $answers = $this->get_array_of_choices($question);
224 for ($group=1; $group < count($answers); $group++) {
225 $players = $this->get_group_of_players($question, $state, $answers, $group);
227 $arr[$group]= $players;
234 * This method gets the correct answers in a 2 dimentional array.
236 * @param object $question
237 * @return array of groups
239 protected function get_correct_answers($question) {
240 $arrayofchoices = $this->get_array_of_choices($question);
241 $arrayofplaceholdeers = $this->get_array_of_placeholders($question);
243 $correctplayers = array();
244 foreach ($arrayofplaceholdeers as $ph) {
245 foreach ($arrayofchoices as $key => $choice) {
246 if ($key + 1 == $ph) {
247 $correctplayers[]= $choice;
251 return $correctplayers;
254 protected function get_array_of_placeholders($question) {
255 $qtext = $question->questiontext;
256 $error = '<b> ERROR</b>: Please check the form for this question. ';
258 echo $error . 'The question text is empty!';
263 $slots = $this->getEmbeddedTextArray($question);
266 echo $error . 'The question text is not in the correct format!';
271 foreach ($slots as $slot) {
272 $output[] = substr($slot, 2, strlen($slot) - 4); //2 is for '[[' and 4 is for '[[]]'.
277 protected function get_group_of_players($question, $state, $subquestions, $group) {
278 $goupofanswers = array();
279 foreach ($subquestions as $key => $subquestion) {
280 if ($subquestion[$this->choice_group_key()] == $group) {
281 $goupofanswers[] = $subquestion;
285 // Shuffle answers within this group
286 if ($question->options->shuffleanswers == 1) {
287 shuffle($goupofanswers);
289 return $goupofanswers;
292 public function get_possible_responses($questiondata) {
293 $question = $this->make_question($questiondata);
296 foreach ($question->places as $place => $group) {
299 foreach ($question->choices[$group] as $i => $choice) {
300 $choices[$i] = new question_possible_response(
301 html_to_text($choice->text, 0, false),
302 $question->rightchoices[$place] == $i);
304 $choices[null] = question_possible_response::no_response();
306 $parts[$place] = $choices;
312 public function move_files($questionid, $oldcontextid, $newcontextid) {
313 parent::move_files($questionid, $oldcontextid, $newcontextid);
315 $fs = get_file_storage();
316 $fs->move_area_files_to_new_context($oldcontextid,
317 $newcontextid, 'question', 'correctfeedback', $questionid);
318 $fs->move_area_files_to_new_context($oldcontextid,
319 $newcontextid, 'question', 'partiallycorrectfeedback', $questionid);
320 $fs->move_area_files_to_new_context($oldcontextid,
321 $newcontextid, 'question', 'incorrectfeedback', $questionid);
324 protected function delete_files($questionid, $contextid) {
325 parent::delete_files($questionid, $contextid);
327 $fs = get_file_storage();
328 $fs->delete_area_files($contextid, 'question', 'correctfeedback', $questionid);
329 $fs->delete_area_files($contextid, 'question', 'partiallycorrectfeedback', $questionid);
330 $fs->delete_area_files($contextid, 'question', 'incorrectfeedback', $questionid);