|
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 require_once($CFG->libdir . '/questionlib.php'); 00030 require_once($CFG->dirroot . '/question/engine/lib.php'); 00031 require_once($CFG->dirroot . '/question/type/shortanswer/question.php'); 00032 00033 00040 class qtype_shortanswer extends question_type { 00041 public function extra_question_fields() { 00042 return array('question_shortanswer', 'answers', 'usecase'); 00043 } 00044 00045 public function questionid_column_name() { 00046 return 'question'; 00047 } 00048 00049 public function move_files($questionid, $oldcontextid, $newcontextid) { 00050 parent::move_files($questionid, $oldcontextid, $newcontextid); 00051 $this->move_files_in_answers($questionid, $oldcontextid, $newcontextid); 00052 } 00053 00054 protected function delete_files($questionid, $contextid) { 00055 parent::delete_files($questionid, $contextid); 00056 $this->delete_files_in_answers($questionid, $contextid); 00057 } 00058 00059 public function save_question_options($question) { 00060 global $DB; 00061 $result = new stdClass(); 00062 00063 $context = $question->context; 00064 00065 $oldanswers = $DB->get_records('question_answers', 00066 array('question' => $question->id), 'id ASC'); 00067 00068 $answers = array(); 00069 $maxfraction = -1; 00070 00071 // Insert all the new answers 00072 foreach ($question->answer as $key => $answerdata) { 00073 // Check for, and ignore, completely blank answer from the form. 00074 if (trim($answerdata) == '' && $question->fraction[$key] == 0 && 00075 html_is_blank($question->feedback[$key]['text'])) { 00076 continue; 00077 } 00078 00079 // Update an existing answer if possible. 00080 $answer = array_shift($oldanswers); 00081 if (!$answer) { 00082 $answer = new stdClass(); 00083 $answer->question = $question->id; 00084 $answer->answer = ''; 00085 $answer->feedback = ''; 00086 $answer->id = $DB->insert_record('question_answers', $answer); 00087 } 00088 00089 $answer->answer = trim($answerdata); 00090 $answer->fraction = $question->fraction[$key]; 00091 $answer->feedback = $this->import_or_save_files($question->feedback[$key], 00092 $context, 'question', 'answerfeedback', $answer->id); 00093 $answer->feedbackformat = $question->feedback[$key]['format']; 00094 $DB->update_record('question_answers', $answer); 00095 00096 $answers[] = $answer->id; 00097 if ($question->fraction[$key] > $maxfraction) { 00098 $maxfraction = $question->fraction[$key]; 00099 } 00100 } 00101 00102 $question->answers = implode(',', $answers); 00103 $parentresult = parent::save_question_options($question); 00104 if ($parentresult !== null) { 00105 // Parent function returns null if all is OK 00106 return $parentresult; 00107 } 00108 00109 // Delete any left over old answer records. 00110 $fs = get_file_storage(); 00111 foreach ($oldanswers as $oldanswer) { 00112 $fs->delete_area_files($context->id, 'question', 'answerfeedback', $oldanswer->id); 00113 $DB->delete_records('question_answers', array('id' => $oldanswer->id)); 00114 } 00115 00116 $this->save_hints($question); 00117 00118 // Perform sanity checks on fractional grades 00119 if ($maxfraction != 1) { 00120 $result->noticeyesno = get_string('fractionsnomax', 'question', $maxfraction * 100); 00121 return $result; 00122 } 00123 } 00124 00125 protected function initialise_question_instance(question_definition $question, $questiondata) { 00126 parent::initialise_question_instance($question, $questiondata); 00127 $this->initialise_question_answers($question, $questiondata); 00128 } 00129 00130 public function get_random_guess_score($questiondata) { 00131 foreach ($questiondata->options->answers as $aid => $answer) { 00132 if ('*' == trim($answer->answer)) { 00133 return $answer->fraction; 00134 } 00135 } 00136 return 0; 00137 } 00138 00139 public function get_possible_responses($questiondata) { 00140 $responses = array(); 00141 00142 $starfound = false; 00143 foreach ($questiondata->options->answers as $aid => $answer) { 00144 $responses[$aid] = new question_possible_response($answer->answer, 00145 $answer->fraction); 00146 if ($answer->answer === '*') { 00147 $starfound = true; 00148 } 00149 } 00150 00151 if (!$starfound) { 00152 $responses[0] = new question_possible_response( 00153 get_string('didnotmatchanyanswer', 'question'), 0); 00154 } 00155 00156 $responses[null] = question_possible_response::no_response(); 00157 00158 return array($questiondata->id => $responses); 00159 } 00160 }