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