MDL-20636 Current work-in-progress converting the question engine to Moodle 2.0
[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
18
19/**
20 * True-false question definition class.
21 *
22 * @package qtype_truefalse
23 * @copyright 2009 The Open University
24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 */
26
27
28/**
29 * Represents a true-false question.
30 *
31 * @copyright 2009 The Open University
32 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
33 */
34class qtype_truefalse_question extends question_graded_automatically {
35 public $rightanswer;
36 public $truefeedback;
37 public $falsefeedback;
38
39 public function get_expected_data() {
40 return array('answer' => PARAM_INTEGER);
41 }
42
43 public function get_correct_response() {
44 return array('answer' => (int) $this->rightanswer);
45 }
46
47 public function summarise_response(array $response) {
48 if (!array_key_exists('answer', $response)) {
49 return null;
50 } else if ($response['answer']) {
51 return get_string('true', 'qtype_truefalse');
52 } else {
53 return get_string('false', 'qtype_truefalse');
54 }
55 }
56
57 public function classify_response(array $response) {
58 if (!array_key_exists('answer', $response)) {
59 return array($this->id => question_classified_response::no_response());
60 }
61 list($fraction) = $this->grade_response($response);
62 if ($response['answer']) {
63 return array($this->id => new question_classified_response(1,
64 get_string('true', 'qtype_truefalse'), $fraction));
65 } else {
66 return array($this->id => new question_classified_response(0,
67 get_string('false', 'qtype_truefalse'), $fraction));
68 }
69 }
70
71 public function is_complete_response(array $response) {
72 return array_key_exists('answer', $response);
73 }
74
75 public function get_validation_error(array $response) {
76 if ($this->is_gradable_response($response)) {
77 return '';
78 }
79 return get_string('pleaseselectananswer', 'qtype_truefalse');
80 }
81
82 public function is_same_response(array $prevresponse, array $newresponse) {
83 return question_utils::arrays_same_at_key_missing_is_blank(
84 $prevresponse, $newresponse, 'answer');
85 }
86
87 public function grade_response(array $response) {
88 if ($this->rightanswer == true && $response['answer'] == true) {
89 $fraction = 1;
90 } else if ($this->rightanswer == false && $response['answer'] == false) {
91 $fraction = 1;
92 } else {
93 $fraction = 0;
94 }
95 return array($fraction, question_state::graded_state_for_fraction($fraction));
96 }
97}