MDL-20636 Fix codechecker issues in qtype_shortanswer.
[moodle.git] / question / type / truefalse / question.php
CommitLineData
d1b7e03d
TH
1<?php
2
3// This file is part of Moodle - http://moodle.org/
4//
5// Moodle is free software: you can redistribute it and/or modify
6// it under the terms of the GNU General Public License as published by
7// the Free Software Foundation, either version 3 of the License, or
8// (at your option) any later version.
9//
10// Moodle is distributed in the hope that it will be useful,
11// but WITHOUT ANY WARRANTY; without even the implied warranty of
12// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13// GNU General Public License for more details.
14//
15// You should have received a copy of the GNU General Public License
16// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
17
d1b7e03d
TH
18/**
19 * True-false question definition class.
20 *
7764183a
TH
21 * @package qtype
22 * @subpackage truefalse
23 * @copyright 2009 The Open University
24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
d1b7e03d
TH
25 */
26
27
a17b297d
TH
28defined('MOODLE_INTERNAL') || die();
29
30
d1b7e03d
TH
31/**
32 * Represents a true-false question.
33 *
7764183a
TH
34 * @copyright 2009 The Open University
35 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
d1b7e03d
TH
36 */
37class qtype_truefalse_question extends question_graded_automatically {
38 public $rightanswer;
39 public $truefeedback;
40 public $falsefeedback;
7a719748
TH
41 public $trueanswerid;
42 public $falseanswerid;
d1b7e03d
TH
43
44 public function get_expected_data() {
45 return array('answer' => PARAM_INTEGER);
46 }
47
48 public function get_correct_response() {
49 return array('answer' => (int) $this->rightanswer);
50 }
51
52 public function summarise_response(array $response) {
53 if (!array_key_exists('answer', $response)) {
54 return null;
55 } else if ($response['answer']) {
56 return get_string('true', 'qtype_truefalse');
57 } else {
58 return get_string('false', 'qtype_truefalse');
59 }
60 }
61
62 public function classify_response(array $response) {
63 if (!array_key_exists('answer', $response)) {
64 return array($this->id => question_classified_response::no_response());
65 }
66 list($fraction) = $this->grade_response($response);
67 if ($response['answer']) {
68 return array($this->id => new question_classified_response(1,
69 get_string('true', 'qtype_truefalse'), $fraction));
70 } else {
71 return array($this->id => new question_classified_response(0,
72 get_string('false', 'qtype_truefalse'), $fraction));
73 }
74 }
75
76 public function is_complete_response(array $response) {
77 return array_key_exists('answer', $response);
78 }
79
80 public function get_validation_error(array $response) {
81 if ($this->is_gradable_response($response)) {
82 return '';
83 }
84 return get_string('pleaseselectananswer', 'qtype_truefalse');
85 }
86
87 public function is_same_response(array $prevresponse, array $newresponse) {
88 return question_utils::arrays_same_at_key_missing_is_blank(
89 $prevresponse, $newresponse, 'answer');
90 }
91
92 public function grade_response(array $response) {
93 if ($this->rightanswer == true && $response['answer'] == true) {
94 $fraction = 1;
95 } else if ($this->rightanswer == false && $response['answer'] == false) {
96 $fraction = 1;
97 } else {
98 $fraction = 0;
99 }
100 return array($fraction, question_state::graded_state_for_fraction($fraction));
101 }
7a719748 102
6d03299d 103 public function check_file_access($qa, $options, $component, $filearea, $args, $forcedownload) {
7a719748
TH
104 if ($component == 'question' && $filearea == 'answerfeedback') {
105 $answerid = reset($args); // itemid is answer id.
106 $response = $qa->get_last_qt_var('answer', '');
107 return $options->feedback && (
108 ($answerid == $this->trueanswerid && $response) ||
109 ($answerid == $this->falseanswerid && $response !== ''));
110
111 } else {
112 return parent::check_file_access($qa, $options, $component, $filearea, $args, $forcedownload);
113 }
114 }
d1b7e03d 115}