db0a7c9233c4314faf06a4d56d04f12c5c150406
[moodle.git] / question / type / gapselect / questiontypebase.php
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/>.
17 /**
18  * Question type class for the embedded element in question text question types.
19  *
20  * @package    qtype
21  * @subpackage gapselect
22  * @copyright  2011 The Open University
23  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24  */
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');
34 /**
35  * The embedded element in question text question type class.
36  *
37  * @copyright  2011 The Open University
38  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
39  */
40 abstract class qtype_gapselect_base extends question_type {
41     /**
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.
48      */
49     protected abstract function choice_options_to_feedback($choice);
51     public function save_question_options($question) {
52         global $DB;
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']) == '') {
63                 continue;
64             }
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);
73             } else {
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);
82             }
83         }
85         // Delete old answer records
86         foreach ($oldanswers as $oa) {
87             $DB->delete_records('question_answers', array('id' => $oa->id));
88         }
90         $options = $DB->get_record('question_' . $this->name(),
91                 array('questionid' => $question->id));
92         if (!$options) {
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);
99         }
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);
106     }
108     public function get_question_options($question) {
109         global $DB;
110         $question->options = $DB->get_record('question_'.$this->name(),
111                 array('questionid' => $question->id), '*', MUST_EXIST);
112         parent::get_question_options($question);
113     }
115     public function delete_question($questionid, $contextid) {
116         global $DB;
117         $DB->delete_records('question_'.$this->name(), array('questionid' => $questionid));
118         return parent::delete_question($questionid, $contextid);
119     }
121     /**
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.
125      */
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.
139         $i = 1;
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;
145             } else {
146                 $question->choices[$choice->choice_group()][1] = $choice;
147             }
149             end($question->choices[$choice->choice_group()]);
150             $choiceindexmap[$i] = array($choice->choice_group(),
151                     key($question->choices[$choice->choice_group()]));
152             $i += 1;
153         }
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);
163         $i = 1;
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);
173             $i += 1;
174         }
175     }
177     protected function make_hint($hint) {
178         return question_hint_with_parts::load_from_record($hint);
179     }
181     public function get_random_guess_score($questiondata) {
182         $question = $this->make_question($questiondata);
183         return $question->get_random_guess_score();
184     }
186     /**
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.
190      */
191     protected abstract function feedback_to_choice_options($feedback);
193     /**
194      * This method gets the choices (answers)
195      * in a 2 dimentional array.
196      *
197      * @param object $question
198      * @return array of groups
199      */
200     protected function get_array_of_choices($question) {
201         $subquestions = $question->options->answers;
202         $count = 0;
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;
209             ++$count;
210         }
211         return $answers;
212     }
214     /**
215      * This method gets the choices (answers) and sort them by groups
216      * in a 2 dimentional array.
217      *
218      * @param object $question
219      * @return array of groups
220      */
221     protected function get_array_of_groups($question, $state) {
222         $answers = $this->get_array_of_choices($question);
223         $arr = array();
224         for ($group=1; $group < count($answers); $group++) {
225             $players = $this->get_group_of_players($question, $state, $answers, $group);
226             if ($players) {
227                 $arr[$group]= $players;
228             }
229         }
230         return $arr;
231     }
233     /**
234      * This method gets the correct answers in a 2 dimentional array.
235      *
236      * @param object $question
237      * @return array of groups
238      */
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;
248                 }
249             }
250         }
251         return $correctplayers;
252     }
254     protected function get_array_of_placeholders($question) {
255         $qtext = $question->questiontext;
256         $error = '<b> ERROR</b>: Please check the form for this question. ';
257         if (!$qtext) {
258             echo $error . 'The question text is empty!';
259             return false;
260         }
262         //get the slots
263         $slots = $this->getEmbeddedTextArray($question);
265         if (!$slots) {
266             echo $error . 'The question text is not in the correct format!';
267             return false;
268         }
270         $output = array();
271         foreach ($slots as $slot) {
272             $output[] = substr($slot, 2, strlen($slot) - 4); //2 is for '[[' and 4 is for '[[]]'.
273         }
274         return $output;
275     }
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;
282             }
283         }
285         // Shuffle answers within this group
286         if ($question->options->shuffleanswers == 1) {
287             shuffle($goupofanswers);
288         }
289         return $goupofanswers;
290     }
292     public function get_possible_responses($questiondata) {
293         $question = $this->make_question($questiondata);
295         $parts = array();
296         foreach ($question->places as $place => $group) {
297             $choices = array();
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);
303             }
304             $choices[null] = question_possible_response::no_response();
306             $parts[$place] = $choices;
307         }
309         return $parts;
310     }
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);
322     }
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);
331     }