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