|
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 00017 00027 require_once($CFG->dirroot . '/question/type/shortanswer/question.php'); 00028 require_once($CFG->dirroot . '/question/type/numerical/question.php'); 00029 require_once($CFG->dirroot . '/question/type/multichoice/question.php'); 00030 00031 00042 class qtype_multianswer_question extends question_graded_automatically { 00044 public $subquestions = array(); 00045 00050 public $places; 00051 00056 public $textfragments; 00057 00064 protected function get_substep($step, $i) { 00065 return new question_attempt_step_subquestion_adapter($step, 'sub' . $i . '_'); 00066 } 00067 00068 public function start_attempt(question_attempt_step $step, $variant) { 00069 foreach ($this->subquestions as $i => $subq) { 00070 $subq->start_attempt($this->get_substep($step, $i), $variant); 00071 } 00072 } 00073 00074 public function apply_attempt_state(question_attempt_step $step) { 00075 foreach ($this->subquestions as $i => $subq) { 00076 $subq->apply_attempt_state($this->get_substep($step, $i)); 00077 } 00078 } 00079 00080 public function get_question_summary() { 00081 $summary = $this->html_to_text($this->questiontext, $this->questiontextformat); 00082 foreach ($this->subquestions as $i => $subq) { 00083 switch ($subq->qtype->name()) { 00084 case 'multichoice': 00085 $choices = array(); 00086 $dummyqa = new question_attempt($subq, $this->contextid); 00087 foreach ($subq->get_order($dummyqa) as $ansid) { 00088 $choices[] = $this->html_to_text($subq->answers[$ansid]->answer, 00089 $subq->answers[$ansid]->answerformat); 00090 } 00091 $answerbit = '{' . implode('; ', $choices) . '}'; 00092 break; 00093 case 'numerical': 00094 case 'shortanswer': 00095 $answerbit = '_____'; 00096 break; 00097 default: 00098 $answerbit = '{ERR unknown sub-question type}'; 00099 } 00100 $summary = str_replace('{#' . $i . '}', $answerbit, $summary); 00101 } 00102 return $summary; 00103 } 00104 00105 public function get_min_fraction() { 00106 $fractionsum = 0; 00107 $fractionmax = 0; 00108 foreach ($this->subquestions as $i => $subq) { 00109 $fractionmax += $subq->defaultmark; 00110 $fractionsum += $subq->defaultmark * $subq->get_min_fraction(); 00111 } 00112 return $fractionsum / $fractionmax; 00113 } 00114 00115 public function get_expected_data() { 00116 $expected = array(); 00117 foreach ($this->subquestions as $i => $subq) { 00118 $substep = $this->get_substep(null, $i); 00119 foreach ($subq->get_expected_data() as $name => $type) { 00120 if ($subq->qtype->name() == 'multichoice' && 00121 $subq->layout == qtype_multichoice_base::LAYOUT_DROPDOWN) { 00122 // Hack or MC inline does not work. 00123 $expected[$substep->add_prefix($name)] = PARAM_RAW; 00124 } else { 00125 $expected[$substep->add_prefix($name)] = $type; 00126 } 00127 } 00128 } 00129 return $expected; 00130 } 00131 00132 public function get_correct_response() { 00133 $right = array(); 00134 foreach ($this->subquestions as $i => $subq) { 00135 $substep = $this->get_substep(null, $i); 00136 foreach ($subq->get_correct_response() as $name => $type) { 00137 $right[$substep->add_prefix($name)] = $type; 00138 } 00139 } 00140 return $right; 00141 } 00142 00143 public function is_complete_response(array $response) { 00144 foreach ($this->subquestions as $i => $subq) { 00145 $substep = $this->get_substep(null, $i); 00146 if (!$subq->is_complete_response($substep->filter_array($response))) { 00147 return false; 00148 } 00149 } 00150 return true; 00151 } 00152 00153 public function is_gradable_response(array $response) { 00154 foreach ($this->subquestions as $i => $subq) { 00155 $substep = $this->get_substep(null, $i); 00156 if ($subq->is_gradable_response($substep->filter_array($response))) { 00157 return true; 00158 } 00159 } 00160 return false; 00161 } 00162 00163 public function is_same_response(array $prevresponse, array $newresponse) { 00164 foreach ($this->subquestions as $i => $subq) { 00165 $substep = $this->get_substep(null, $i); 00166 if (!$subq->is_same_response($substep->filter_array($prevresponse), 00167 $substep->filter_array($newresponse))) { 00168 return false; 00169 } 00170 } 00171 return true; 00172 } 00173 00174 public function get_validation_error(array $response) { 00175 $errors = array(); 00176 foreach ($this->subquestions as $i => $subq) { 00177 $substep = $this->get_substep(null, $i); 00178 $errors[] = $subq->get_validation_error($substep->filter_array($response)); 00179 } 00180 return implode('<br />', $errors); 00181 } 00182 00192 protected function combine_states($overallstate, $newstate) { 00193 if (is_null($overallstate)) { 00194 return $newstate; 00195 } else if ($overallstate == question_state::$gaveup && 00196 $newstate == question_state::$gaveup) { 00197 return question_state::$gaveup; 00198 } else if ($overallstate == question_state::$gaveup && 00199 $newstate == question_state::$gradedwrong) { 00200 return question_state::$gradedwrong; 00201 } else if ($overallstate == question_state::$gradedwrong && 00202 $newstate == question_state::$gaveup) { 00203 return question_state::$gradedwrong; 00204 } else if ($overallstate == question_state::$gradedwrong && 00205 $newstate == question_state::$gradedwrong) { 00206 return question_state::$gradedwrong; 00207 } else if ($overallstate == question_state::$gradedright && 00208 $newstate == question_state::$gradedright) { 00209 return question_state::$gradedright; 00210 } else { 00211 return question_state::$gradedpartial; 00212 } 00213 } 00214 00215 public function grade_response(array $response) { 00216 $overallstate = null; 00217 $fractionsum = 0; 00218 $fractionmax = 0; 00219 foreach ($this->subquestions as $i => $subq) { 00220 $fractionmax += $subq->defaultmark; 00221 $substep = $this->get_substep(null, $i); 00222 $subresp = $substep->filter_array($response); 00223 if (!$subq->is_gradable_response($subresp)) { 00224 $overallstate = $this->combine_states($overallstate, question_state::$gaveup); 00225 } else { 00226 list($subfraction, $newstate) = $subq->grade_response($subresp); 00227 $fractionsum += $subfraction * $subq->defaultmark; 00228 $overallstate = $this->combine_states($overallstate, $newstate); 00229 } 00230 } 00231 return array($fractionsum / $fractionmax, $overallstate); 00232 } 00233 00234 public function summarise_response(array $response) { 00235 $summary = array(); 00236 foreach ($this->subquestions as $i => $subq) { 00237 $substep = $this->get_substep(null, $i); 00238 $a = new stdClass(); 00239 $a->i = $i; 00240 $a->response = $subq->summarise_response($substep->filter_array($response)); 00241 $summary[] = get_string('subqresponse', 'qtype_multianswer', $a); 00242 } 00243 00244 return implode('; ', $summary); 00245 } 00246 00247 public function check_file_access($qa, $options, $component, $filearea, $args, $forcedownload) { 00248 if ($component == 'question' && $filearea == 'answer') { 00249 return true; 00250 00251 } else if ($component == 'question' && $filearea == 'answerfeedback') { 00252 // Full logic to control which feedbacks a student can see is too complex. 00253 // Just allow access to all images. There is a theoretical chance the 00254 // students could see files they are not meant to see by guessing URLs, 00255 // but it is remote. 00256 return $options->feedback; 00257 00258 } else if ($component == 'question' && $filearea == 'hint') { 00259 return $this->check_hint_file_access($qa, $options, $args); 00260 00261 } else { 00262 return parent::check_file_access($qa, $options, $component, $filearea, 00263 $args, $forcedownload); 00264 } 00265 } 00266 }