Moodle  2.2.1
http://www.collinsharper.com
C:/xampp/htdocs/moodle/question/type/truefalse/question.php
Go to the documentation of this file.
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_question extends question_graded_automatically {
00037     public $rightanswer;
00038     public $truefeedback;
00039     public $falsefeedback;
00040     public $trueanswerid;
00041     public $falseanswerid;
00042 
00043     public function get_expected_data() {
00044         return array('answer' => PARAM_INTEGER);
00045     }
00046 
00047     public function get_correct_response() {
00048         return array('answer' => (int) $this->rightanswer);
00049     }
00050 
00051     public function summarise_response(array $response) {
00052         if (!array_key_exists('answer', $response)) {
00053             return null;
00054         } else if ($response['answer']) {
00055             return get_string('true', 'qtype_truefalse');
00056         } else {
00057             return get_string('false', 'qtype_truefalse');
00058         }
00059     }
00060 
00061     public function classify_response(array $response) {
00062         if (!array_key_exists('answer', $response)) {
00063             return array($this->id => question_classified_response::no_response());
00064         }
00065         list($fraction) = $this->grade_response($response);
00066         if ($response['answer']) {
00067             return array($this->id => new question_classified_response(1,
00068                     get_string('true', 'qtype_truefalse'), $fraction));
00069         } else {
00070             return array($this->id => new question_classified_response(0,
00071                     get_string('false', 'qtype_truefalse'), $fraction));
00072         }
00073     }
00074 
00075     public function is_complete_response(array $response) {
00076         return array_key_exists('answer', $response);
00077     }
00078 
00079     public function get_validation_error(array $response) {
00080         if ($this->is_gradable_response($response)) {
00081             return '';
00082         }
00083         return get_string('pleaseselectananswer', 'qtype_truefalse');
00084     }
00085 
00086     public function is_same_response(array $prevresponse, array $newresponse) {
00087         return question_utils::arrays_same_at_key_missing_is_blank(
00088                 $prevresponse, $newresponse, 'answer');
00089     }
00090 
00091     public function grade_response(array $response) {
00092         if ($this->rightanswer == true && $response['answer'] == true) {
00093             $fraction = 1;
00094         } else if ($this->rightanswer == false && $response['answer'] == false) {
00095             $fraction = 1;
00096         } else {
00097             $fraction = 0;
00098         }
00099         return array($fraction, question_state::graded_state_for_fraction($fraction));
00100     }
00101 
00102     public function check_file_access($qa, $options, $component, $filearea, $args, $forcedownload) {
00103         if ($component == 'question' && $filearea == 'answerfeedback') {
00104             $answerid = reset($args); // itemid is answer id.
00105             $response = $qa->get_last_qt_var('answer', '');
00106             return $options->feedback && (
00107                     ($answerid == $this->trueanswerid && $response) ||
00108                     ($answerid == $this->falseanswerid && $response !== ''));
00109 
00110         } else {
00111             return parent::check_file_access($qa, $options, $component, $filearea,
00112                     $args, $forcedownload);
00113         }
00114     }
00115 }
 All Data Structures Namespaces Files Functions Variables Enumerations