MDL-37684 - phpunit add missing definitions to phpunit.xml.dist
[moodle.git] / question / type / match / 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
1976496e 17/**
93cadb1e
TH
18 * Question type class for the matching question type.
19 *
d3603157 20 * @package qtype
93cadb1e 21 * @subpackage match
d3603157
TH
22 * @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com}
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
7375c542 24 */
516cf3eb 25
d3603157 26
a17b297d
TH
27defined('MOODLE_INTERNAL') || die();
28
93cadb1e
TH
29require_once($CFG->libdir . '/questionlib.php');
30require_once($CFG->dirroot . '/question/engine/lib.php');
31
d3603157 32
93cadb1e
TH
33/**
34 * The matching question type class.
35 *
d3603157
TH
36 * @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com}
37 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
93cadb1e
TH
38 */
39class qtype_match extends question_type {
516cf3eb 40
c7df5006 41 public function get_question_options($question) {
f34488b2 42 global $DB;
93cadb1e 43 parent::get_question_options($question);
f34488b2 44 $question->options = $DB->get_record('question_match', array('question' => $question->id));
3758786f
TH
45 $question->options->subquestions = $DB->get_records('question_match_sub',
46 array('question' => $question->id), 'id ASC');
516cf3eb 47 return true;
48 }
49
c7df5006 50 public function save_question_options($question) {
f34488b2 51 global $DB;
fe6ce234 52 $context = $question->context;
0ff4bd08 53 $result = new stdClass();
069a820a 54
69988ed4
TH
55 $oldsubquestions = $DB->get_records('question_match_sub',
56 array('question' => $question->id), 'id ASC');
516cf3eb 57
516cf3eb 58 // $subquestions will be an array with subquestion ids
59 $subquestions = array();
60
61 // Insert all the new question+answer pairs
62 foreach ($question->subquestions as $key => $questiontext) {
69988ed4
TH
63 if ($questiontext['text'] == '' && trim($question->subanswers[$key]) == '') {
64 continue;
65 }
66 if ($questiontext['text'] != '' && trim($question->subanswers[$key]) == '') {
5e8a85aa 67 $result->notice = get_string('nomatchinganswer', 'qtype_match', $questiontext);
cde2709a 68 }
69988ed4
TH
69
70 // Update an existing subquestion if possible.
71 $subquestion = array_shift($oldsubquestions);
72 if (!$subquestion) {
0ff4bd08 73 $subquestion = new stdClass();
69988ed4
TH
74 // Determine a unique random code
75 $subquestion->code = rand(1, 999999999);
3758786f
TH
76 while ($DB->record_exists('question_match_sub',
77 array('code' => $subquestion->code, 'question' => $question->id))) {
fe6ce234 78 $subquestion->code = rand(1, 999999999);
516cf3eb 79 }
69988ed4
TH
80 $subquestion->question = $question->id;
81 $subquestion->questiontext = '';
82 $subquestion->answertext = '';
83 $subquestion->id = $DB->insert_record('question_match_sub', $subquestion);
a58ffe3f 84 }
69988ed4
TH
85
86 $subquestion->questiontext = $this->import_or_save_files($questiontext,
87 $context, 'qtype_match', 'subquestion', $subquestion->id);
88 $subquestion->questiontextformat = $questiontext['format'];
89 $subquestion->answertext = trim($question->subanswers[$key]);
90
91 $DB->update_record('question_match_sub', $subquestion);
92
93 $subquestions[] = $subquestion->id;
516cf3eb 94 }
95
69988ed4
TH
96 // Delete old subquestions records
97 $fs = get_file_storage();
3758786f 98 foreach ($oldsubquestions as $oldsub) {
69988ed4
TH
99 $fs->delete_area_files($context->id, 'qtype_match', 'subquestion', $oldsub->id);
100 $DB->delete_records('question_match_sub', array('id' => $oldsub->id));
516cf3eb 101 }
102
93cadb1e
TH
103 // Save the question options.
104 $options = $DB->get_record('question_match', array('question' => $question->id));
105 if (!$options) {
106 $options = new stdClass();
516cf3eb 107 $options->question = $question->id;
93cadb1e
TH
108 $options->correctfeedback = '';
109 $options->partiallycorrectfeedback = '';
110 $options->incorrectfeedback = '';
111 $options->id = $DB->insert_record('question_match', $options);
516cf3eb 112 }
a58ffe3f 113
93cadb1e
TH
114 $options->subquestions = implode(',', $subquestions);
115 $options->shuffleanswers = $question->shuffleanswers;
1c2ed7c5 116 $options = $this->save_combined_feedback_helper($options, $question, $context, true);
93cadb1e
TH
117 $DB->update_record('question_match', $options);
118
119 $this->save_hints($question, true);
120
a58ffe3f 121 if (!empty($result->notice)) {
122 return $result;
123 }
124
125 if (count($subquestions) < 3) {
5e8a85aa 126 $result->notice = get_string('notenoughanswers', 'question', 3);
a58ffe3f 127 return $result;
128 }
129
516cf3eb 130 return true;
131 }
132
93cadb1e
TH
133 protected function initialise_question_instance(question_definition $question, $questiondata) {
134 parent::initialise_question_instance($question, $questiondata);
516cf3eb 135
93cadb1e 136 $question->shufflestems = $questiondata->options->shuffleanswers;
1c2ed7c5 137 $this->initialise_combined_feedback($question, $questiondata, true);
516cf3eb 138
93cadb1e
TH
139 $question->stems = array();
140 $question->choices = array();
141 $question->right = array();
516cf3eb 142
93cadb1e
TH
143 foreach ($questiondata->options->subquestions as $matchsub) {
144 $ans = $matchsub->answertext;
145 $key = array_search($matchsub->answertext, $question->choices);
146 if ($key === false) {
147 $key = $matchsub->id;
148 $question->choices[$key] = $matchsub->answertext;
869309b8 149 }
516cf3eb 150
93cadb1e
TH
151 if ($matchsub->questiontext !== '') {
152 $question->stems[$matchsub->id] = $matchsub->questiontext;
22cebed5 153 $question->stemformat[$matchsub->id] = $matchsub->questiontextformat;
93cadb1e 154 $question->right[$matchsub->id] = $key;
0c24ee0f 155 }
516cf3eb 156 }
516cf3eb 157 }
158
93cadb1e
TH
159 protected function make_hint($hint) {
160 return question_hint_with_parts::load_from_record($hint);
516cf3eb 161 }
162
c7df5006 163 public function delete_question($questionid, $contextid) {
93cadb1e
TH
164 global $DB;
165 $DB->delete_records('question_match', array('question' => $questionid));
166 $DB->delete_records('question_match_sub', array('question' => $questionid));
f30bbcaf 167
93cadb1e 168 parent::delete_question($questionid, $contextid);
516cf3eb 169 }
170
93cadb1e
TH
171 public function get_random_guess_score($questiondata) {
172 $q = $this->make_question($questiondata);
173 return 1 / count($q->choices);
b25486fc 174 }
175
c7df5006 176 public function get_possible_responses($questiondata) {
93cadb1e 177 $subqs = array();
516cf3eb 178
93cadb1e 179 $q = $this->make_question($questiondata);
aeb15530 180
93cadb1e 181 foreach ($q->stems as $stemid => $stem) {
aeb15530 182
93cadb1e
TH
183 $responses = array();
184 foreach ($q->choices as $choiceid => $choice) {
185 $responses[$choiceid] = new question_possible_response(
22cebed5 186 $q->html_to_text($stem, $q->stemformat[$stemid]) . ': ' . $choice,
93cadb1e 187 ($choiceid == $q->right[$stemid]) / count($q->stems));
869309b8 188 }
93cadb1e 189 $responses[null] = question_possible_response::no_response();
069a820a 190
93cadb1e 191 $subqs[$stemid] = $responses;
b9bd6da4 192 }
193
93cadb1e 194 return $subqs;
b9bd6da4 195 }
fe6ce234 196
c7df5006 197 public function move_files($questionid, $oldcontextid, $newcontextid) {
fe6ce234 198 global $DB;
fe6ce234 199 $fs = get_file_storage();
fe6ce234 200
5d548d3e
TH
201 parent::move_files($questionid, $oldcontextid, $newcontextid);
202
203 $subquestionids = $DB->get_records_menu('question_match_sub',
204 array('question' => $questionid), 'id', 'id,1');
205 foreach ($subquestionids as $subquestionid => $notused) {
206 $fs->move_area_files_to_new_context($oldcontextid,
207 $newcontextid, 'qtype_match', 'subquestion', $subquestionid);
fe6ce234 208 }
a23bda41
TH
209
210 $this->move_files_in_combined_feedback($questionid, $oldcontextid, $newcontextid);
d44480f6 211 $this->move_files_in_hints($questionid, $oldcontextid, $newcontextid);
fe6ce234 212 }
5d548d3e 213
9203b705
TH
214 protected function delete_files($questionid, $contextid) {
215 global $DB;
216 $fs = get_file_storage();
217
218 parent::delete_files($questionid, $contextid);
219
220 $subquestionids = $DB->get_records_menu('question_match_sub',
221 array('question' => $questionid), 'id', 'id,1');
222 foreach ($subquestionids as $subquestionid => $notused) {
223 $fs->delete_area_files($contextid, 'qtype_match', 'subquestion', $subquestionid);
224 }
1c2ed7c5 225
a23bda41 226 $this->delete_files_in_combined_feedback($questionid, $contextid);
d44480f6 227 $this->delete_files_in_hints($questionid, $contextid);
9203b705 228 }
516cf3eb 229}