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