Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.2.x will end 22 April 2024 (12 months).
  • Bug fixes for security issues in 4.2.x will end 7 October 2024 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.1.x is supported too.
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.

/**
 * True-false question definition class.
 *
 * @package    qtype
 * @subpackage truefalse
 * @copyright  2009 The Open University
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 */


defined('MOODLE_INTERNAL') || die();

require_once($CFG->dirroot . '/question/type/questionbase.php');

/**
 * Represents a true-false question.
 *
 * @copyright  2009 The Open University
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 */
class qtype_truefalse_question extends question_graded_automatically {
    public $rightanswer;
    public $truefeedback;
    public $falsefeedback;
    public $trueanswerid;
    public $falseanswerid;

> /** @var int the format of the true feedback. */ public function get_expected_data() { > public $truefeedbackformat; return array('answer' => PARAM_INT); > } > /** @var int the format of the false feedback. */ > public $falsefeedbackformat; public function get_correct_response() { > return array('answer' => (int) $this->rightanswer); > /** @var bool true to show the standard instruction, otherwise hide it. */ } > public $showstandardinstruction; >
public function summarise_response(array $response) { if (!array_key_exists('answer', $response)) { return null; } else if ($response['answer']) { return get_string('true', 'qtype_truefalse'); } else { return get_string('false', 'qtype_truefalse'); } } public function un_summarise_response(string $summary) { if ($summary === get_string('true', 'qtype_truefalse')) { return ['answer' => '1']; } else if ($summary === get_string('false', 'qtype_truefalse')) { return ['answer' => '0']; } else { return []; } } public function classify_response(array $response) { if (!array_key_exists('answer', $response)) { return array($this->id => question_classified_response::no_response()); } list($fraction) = $this->grade_response($response); if ($response['answer']) { return array($this->id => new question_classified_response(1, get_string('true', 'qtype_truefalse'), $fraction)); } else { return array($this->id => new question_classified_response(0, get_string('false', 'qtype_truefalse'), $fraction)); } } public function is_complete_response(array $response) { return array_key_exists('answer', $response); } public function get_validation_error(array $response) { if ($this->is_gradable_response($response)) { return ''; } return get_string('pleaseselectananswer', 'qtype_truefalse'); } public function is_same_response(array $prevresponse, array $newresponse) { return question_utils::arrays_same_at_key_missing_is_blank( $prevresponse, $newresponse, 'answer'); } public function grade_response(array $response) { if ($this->rightanswer == true && $response['answer'] == true) { $fraction = 1; } else if ($this->rightanswer == false && $response['answer'] == false) { $fraction = 1; } else { $fraction = 0; } return array($fraction, question_state::graded_state_for_fraction($fraction)); } public function check_file_access($qa, $options, $component, $filearea, $args, $forcedownload) { if ($component == 'question' && $filearea == 'answerfeedback') { $answerid = reset($args); // Itemid is answer id. $response = $qa->get_last_qt_var('answer', ''); return $options->feedback && ( ($answerid == $this->trueanswerid && $response) || ($answerid == $this->falseanswerid && $response !== '')); } else { return parent::check_file_access($qa, $options, $component, $filearea, $args, $forcedownload); }
> } } > } > /** > * Return the question settings that define this question as structured data. > * > * @param question_attempt $qa the current attempt for which we are exporting the settings. > * @param question_display_options $options the question display options which say which aspects of the question > * should be visible. > * @return mixed structure representing the question settings. In web services, this will be JSON-encoded. > */ > public function get_question_definition_for_external_rendering(question_attempt $qa, question_display_options $options) { > // No need to return anything, external clients do not need additional information for rendering this question type. > return null;