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