Merge branch 'MDL-70148-310' of git://github.com/andrewnicols/moodle into MOODLE_310_...
[moodle.git] / question / type / truefalse / questiontype.php
CommitLineData
aeb15530 1<?php
fe6ce234
DC
2// This file is part of Moodle - http://moodle.org/
3//
4// Moodle is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// Moodle is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
16
41a89a07 17/**
d1b7e03d
TH
18 * Question type class for the true-false question type.
19 *
7764183a
TH
20 * @package qtype
21 * @subpackage truefalse
22 * @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com}
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
53a4d39f 24 */
516cf3eb 25
516cf3eb 26
a17b297d
TH
27defined('MOODLE_INTERNAL') || die();
28
603bd001
PS
29require_once($CFG->libdir . '/questionlib.php');
30
a17b297d 31
d1b7e03d
TH
32/**
33 * The true-false question type class.
34 *
7764183a
TH
35 * @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com}
36 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
d1b7e03d
TH
37 */
38class qtype_truefalse extends question_type {
39 public function save_question_options($question) {
2b7da645
TH
40 global $DB;
41 $result = new stdClass();
42 $context = $question->context;
43
3d9645ae 44 // Fetch old answer ids so that we can reuse them.
2b7da645
TH
45 $oldanswers = $DB->get_records('question_answers',
46 array('question' => $question->id), 'id ASC');
47
48 // Save the true answer - update an existing answer if possible.
49 $answer = array_shift($oldanswers);
50 if (!$answer) {
51 $answer = new stdClass();
52 $answer->question = $question->id;
53 $answer->answer = '';
54 $answer->feedback = '';
55 $answer->id = $DB->insert_record('question_answers', $answer);
d1b7e03d
TH
56 }
57
5e8a85aa 58 $answer->answer = get_string('true', 'qtype_truefalse');
2b7da645
TH
59 $answer->fraction = $question->correctanswer;
60 $answer->feedback = $this->import_or_save_files($question->feedbacktrue,
61 $context, 'question', 'answerfeedback', $answer->id);
62 $answer->feedbackformat = $question->feedbacktrue['format'];
63 $DB->update_record('question_answers', $answer);
64 $trueid = $answer->id;
65
66 // Save the false answer - update an existing answer if possible.
67 $answer = array_shift($oldanswers);
68 if (!$answer) {
69 $answer = new stdClass();
70 $answer->question = $question->id;
71 $answer->answer = '';
72 $answer->feedback = '';
73 $answer->id = $DB->insert_record('question_answers', $answer);
516cf3eb 74 }
75
5e8a85aa 76 $answer->answer = get_string('false', 'qtype_truefalse');
2b7da645
TH
77 $answer->fraction = 1 - (int)$question->correctanswer;
78 $answer->feedback = $this->import_or_save_files($question->feedbackfalse,
79 $context, 'question', 'answerfeedback', $answer->id);
80 $answer->feedbackformat = $question->feedbackfalse['format'];
81 $DB->update_record('question_answers', $answer);
82 $falseid = $answer->id;
83
84 // Delete any left over old answer records.
85 $fs = get_file_storage();
dcd69f8d 86 foreach ($oldanswers as $oldanswer) {
2b7da645
TH
87 $fs->delete_area_files($context->id, 'question', 'answerfeedback', $oldanswer->id);
88 $DB->delete_records('question_answers', array('id' => $oldanswer->id));
516cf3eb 89 }
90
3d9645ae 91 // Save question options in question_truefalse table.
2b7da645 92 if ($options = $DB->get_record('question_truefalse', array('question' => $question->id))) {
516cf3eb 93 // No need to do anything, since the answer IDs won't have changed
3d9645ae 94 // But we'll do it anyway, just for robustness.
2b7da645
TH
95 $options->trueanswer = $trueid;
96 $options->falseanswer = $falseid;
97 $DB->update_record('question_truefalse', $options);
516cf3eb 98 } else {
2b7da645 99 $options = new stdClass();
516cf3eb 100 $options->question = $question->id;
2b7da645
TH
101 $options->trueanswer = $trueid;
102 $options->falseanswer = $falseid;
103 $DB->insert_record('question_truefalse', $options);
516cf3eb 104 }
69988ed4 105
d1b7e03d
TH
106 $this->save_hints($question);
107
516cf3eb 108 return true;
109 }
110
111 /**
2b7da645
TH
112 * Loads the question type specific options for the question.
113 */
d1b7e03d 114 public function get_question_options($question) {
2b7da645 115 global $DB, $OUTPUT;
bb930b7d 116 parent::get_question_options($question);
516cf3eb 117 // Get additional information from database
3d9645ae 118 // and attach it to the question object.
dcd69f8d
TH
119 if (!$question->options = $DB->get_record('question_truefalse',
120 array('question' => $question->id))) {
2b7da645 121 echo $OUTPUT->notification('Error: Missing question options!');
516cf3eb 122 return false;
123 }
3d9645ae 124 // Load the answers.
dcd69f8d
TH
125 if (!$question->options->answers = $DB->get_records('question_answers',
126 array('question' => $question->id), 'id ASC')) {
127 echo $OUTPUT->notification('Error: Missing question answers for truefalse question ' .
128 $question->id . '!');
129 return false;
2b7da645 130 }
516cf3eb 131
132 return true;
133 }
134
d1b7e03d
TH
135 protected function initialise_question_instance(question_definition $question, $questiondata) {
136 parent::initialise_question_instance($question, $questiondata);
137 $answers = $questiondata->options->answers;
138 if ($answers[$questiondata->options->trueanswer]->fraction > 0.99) {
139 $question->rightanswer = true;
140 } else {
141 $question->rightanswer = false;
142 }
dcd69f8d 143 $question->truefeedback = $answers[$questiondata->options->trueanswer]->feedback;
d1b7e03d 144 $question->falsefeedback = $answers[$questiondata->options->falseanswer]->feedback;
dcd69f8d
TH
145 $question->truefeedbackformat =
146 $answers[$questiondata->options->trueanswer]->feedbackformat;
147 $question->falsefeedbackformat =
148 $answers[$questiondata->options->falseanswer]->feedbackformat;
149 $question->trueanswerid = $questiondata->options->trueanswer;
7a719748 150 $question->falseanswerid = $questiondata->options->falseanswer;
d1b7e03d 151 }
9203b705 152
c7df5006 153 public function delete_question($questionid, $contextid) {
2b7da645
TH
154 global $DB;
155 $DB->delete_records('question_truefalse', array('question' => $questionid));
156
157 parent::delete_question($questionid, $contextid);
158 }
159
c7df5006 160 public function move_files($questionid, $oldcontextid, $newcontextid) {
2b7da645
TH
161 parent::move_files($questionid, $oldcontextid, $newcontextid);
162 $this->move_files_in_answers($questionid, $oldcontextid, $newcontextid);
163 }
164
165 protected function delete_files($questionid, $contextid) {
166 parent::delete_files($questionid, $contextid);
167 $this->delete_files_in_answers($questionid, $contextid);
168 }
169
c7df5006 170 public function get_random_guess_score($questiondata) {
d1b7e03d 171 return 0.5;
613aff50
TH
172 }
173
c7df5006 174 public function get_possible_responses($questiondata) {
d1b7e03d
TH
175 return array(
176 $questiondata->id => array(
177 0 => new question_possible_response(get_string('false', 'qtype_truefalse'),
178 $questiondata->options->answers[
179 $questiondata->options->falseanswer]->fraction),
180 1 => new question_possible_response(get_string('true', 'qtype_truefalse'),
181 $questiondata->options->answers[
182 $questiondata->options->trueanswer]->fraction),
183 null => question_possible_response::no_response()
184 )
185 );
516cf3eb 186 }
516cf3eb 187}