|
Moodle
2.2.1
http://www.collinsharper.com
|
00001 <?php 00002 // This file is part of Moodle - http://moodle.org/ 00003 // 00004 // Moodle is free software: you can redistribute it and/or modify 00005 // it under the terms of the GNU General Public License as published by 00006 // the Free Software Foundation, either version 3 of the License, or 00007 // (at your option) any later version. 00008 // 00009 // Moodle is distributed in the hope that it will be useful, 00010 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00012 // GNU General Public License for more details. 00013 // 00014 // You should have received a copy of the GNU General Public License 00015 // along with Moodle. If not, see <http://www.gnu.org/licenses/>. 00016 00027 defined('MOODLE_INTERNAL') || die(); 00028 00029 00036 class qtype_truefalse extends question_type { 00037 public function save_question_options($question) { 00038 global $DB; 00039 $result = new stdClass(); 00040 $context = $question->context; 00041 00042 // Fetch old answer ids so that we can reuse them 00043 $oldanswers = $DB->get_records('question_answers', 00044 array('question' => $question->id), 'id ASC'); 00045 00046 // Save the true answer - update an existing answer if possible. 00047 $answer = array_shift($oldanswers); 00048 if (!$answer) { 00049 $answer = new stdClass(); 00050 $answer->question = $question->id; 00051 $answer->answer = ''; 00052 $answer->feedback = ''; 00053 $answer->id = $DB->insert_record('question_answers', $answer); 00054 } 00055 00056 $answer->answer = get_string('true', 'qtype_truefalse'); 00057 $answer->fraction = $question->correctanswer; 00058 $answer->feedback = $this->import_or_save_files($question->feedbacktrue, 00059 $context, 'question', 'answerfeedback', $answer->id); 00060 $answer->feedbackformat = $question->feedbacktrue['format']; 00061 $DB->update_record('question_answers', $answer); 00062 $trueid = $answer->id; 00063 00064 // Save the false answer - update an existing answer if possible. 00065 $answer = array_shift($oldanswers); 00066 if (!$answer) { 00067 $answer = new stdClass(); 00068 $answer->question = $question->id; 00069 $answer->answer = ''; 00070 $answer->feedback = ''; 00071 $answer->id = $DB->insert_record('question_answers', $answer); 00072 } 00073 00074 $answer->answer = get_string('false', 'qtype_truefalse'); 00075 $answer->fraction = 1 - (int)$question->correctanswer; 00076 $answer->feedback = $this->import_or_save_files($question->feedbackfalse, 00077 $context, 'question', 'answerfeedback', $answer->id); 00078 $answer->feedbackformat = $question->feedbackfalse['format']; 00079 $DB->update_record('question_answers', $answer); 00080 $falseid = $answer->id; 00081 00082 // Delete any left over old answer records. 00083 $fs = get_file_storage(); 00084 foreach ($oldanswers as $oldanswer) { 00085 $fs->delete_area_files($context->id, 'question', 'answerfeedback', $oldanswer->id); 00086 $DB->delete_records('question_answers', array('id' => $oldanswer->id)); 00087 } 00088 00089 // Save question options in question_truefalse table 00090 if ($options = $DB->get_record('question_truefalse', array('question' => $question->id))) { 00091 // No need to do anything, since the answer IDs won't have changed 00092 // But we'll do it anyway, just for robustness 00093 $options->trueanswer = $trueid; 00094 $options->falseanswer = $falseid; 00095 $DB->update_record('question_truefalse', $options); 00096 } else { 00097 $options = new stdClass(); 00098 $options->question = $question->id; 00099 $options->trueanswer = $trueid; 00100 $options->falseanswer = $falseid; 00101 $DB->insert_record('question_truefalse', $options); 00102 } 00103 00104 $this->save_hints($question); 00105 00106 return true; 00107 } 00108 00112 public function get_question_options($question) { 00113 global $DB, $OUTPUT; 00114 // Get additional information from database 00115 // and attach it to the question object 00116 if (!$question->options = $DB->get_record('question_truefalse', 00117 array('question' => $question->id))) { 00118 echo $OUTPUT->notification('Error: Missing question options!'); 00119 return false; 00120 } 00121 // Load the answers 00122 if (!$question->options->answers = $DB->get_records('question_answers', 00123 array('question' => $question->id), 'id ASC')) { 00124 echo $OUTPUT->notification('Error: Missing question answers for truefalse question ' . 00125 $question->id . '!'); 00126 return false; 00127 } 00128 00129 return true; 00130 } 00131 00132 protected function initialise_question_instance(question_definition $question, $questiondata) { 00133 parent::initialise_question_instance($question, $questiondata); 00134 $answers = $questiondata->options->answers; 00135 if ($answers[$questiondata->options->trueanswer]->fraction > 0.99) { 00136 $question->rightanswer = true; 00137 } else { 00138 $question->rightanswer = false; 00139 } 00140 $question->truefeedback = $answers[$questiondata->options->trueanswer]->feedback; 00141 $question->falsefeedback = $answers[$questiondata->options->falseanswer]->feedback; 00142 $question->truefeedbackformat = 00143 $answers[$questiondata->options->trueanswer]->feedbackformat; 00144 $question->falsefeedbackformat = 00145 $answers[$questiondata->options->falseanswer]->feedbackformat; 00146 $question->trueanswerid = $questiondata->options->trueanswer; 00147 $question->falseanswerid = $questiondata->options->falseanswer; 00148 } 00149 00150 public function delete_question($questionid, $contextid) { 00151 global $DB; 00152 $DB->delete_records('question_truefalse', array('question' => $questionid)); 00153 00154 parent::delete_question($questionid, $contextid); 00155 } 00156 00157 public function move_files($questionid, $oldcontextid, $newcontextid) { 00158 parent::move_files($questionid, $oldcontextid, $newcontextid); 00159 $this->move_files_in_answers($questionid, $oldcontextid, $newcontextid); 00160 } 00161 00162 protected function delete_files($questionid, $contextid) { 00163 parent::delete_files($questionid, $contextid); 00164 $this->delete_files_in_answers($questionid, $contextid); 00165 } 00166 00167 public function get_random_guess_score($questiondata) { 00168 return 0.5; 00169 } 00170 00171 public function get_possible_responses($questiondata) { 00172 return array( 00173 $questiondata->id => array( 00174 0 => new question_possible_response(get_string('false', 'qtype_truefalse'), 00175 $questiondata->options->answers[ 00176 $questiondata->options->falseanswer]->fraction), 00177 1 => new question_possible_response(get_string('true', 'qtype_truefalse'), 00178 $questiondata->options->answers[ 00179 $questiondata->options->trueanswer]->fraction), 00180 null => question_possible_response::no_response() 00181 ) 00182 ); 00183 } 00184 }