MDL-20636 Fix new stdClass -> stdClass(), and trailing whitespace.
[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;
0ff4bd08 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) {
0ff4bd08 70 $subquestion = new stdClass();
69988ed4
TH
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;
1c2ed7c5 112 $options = $this->save_combined_feedback_helper($options, $question, $context, true);
93cadb1e
TH
113 $DB->update_record('question_match', $options);
114
115 $this->save_hints($question, true);
116
a58ffe3f 117 if (!empty($result->notice)) {
118 return $result;
119 }
120
121 if (count($subquestions) < 3) {
122 $result->notice = get_string('notenoughanswers', 'quiz', 3);
123 return $result;
124 }
125
516cf3eb 126 return true;
127 }
128
93cadb1e
TH
129 protected function initialise_question_instance(question_definition $question, $questiondata) {
130 parent::initialise_question_instance($question, $questiondata);
516cf3eb 131
93cadb1e 132 $question->shufflestems = $questiondata->options->shuffleanswers;
1c2ed7c5 133 $this->initialise_combined_feedback($question, $questiondata, true);
516cf3eb 134
93cadb1e
TH
135 $question->stems = array();
136 $question->choices = array();
137 $question->right = array();
516cf3eb 138
93cadb1e
TH
139 foreach ($questiondata->options->subquestions as $matchsub) {
140 $ans = $matchsub->answertext;
141 $key = array_search($matchsub->answertext, $question->choices);
142 if ($key === false) {
143 $key = $matchsub->id;
144 $question->choices[$key] = $matchsub->answertext;
869309b8 145 }
516cf3eb 146
93cadb1e
TH
147 if ($matchsub->questiontext !== '') {
148 $question->stems[$matchsub->id] = $matchsub->questiontext;
149 $question->right[$matchsub->id] = $key;
0c24ee0f 150 }
516cf3eb 151 }
516cf3eb 152 }
153
93cadb1e
TH
154 protected function make_hint($hint) {
155 return question_hint_with_parts::load_from_record($hint);
516cf3eb 156 }
157
93cadb1e
TH
158 function delete_question($questionid, $contextid) {
159 global $DB;
160 $DB->delete_records('question_match', array('question' => $questionid));
161 $DB->delete_records('question_match_sub', array('question' => $questionid));
f30bbcaf 162
93cadb1e 163 parent::delete_question($questionid, $contextid);
516cf3eb 164 }
165
93cadb1e
TH
166 public function get_random_guess_score($questiondata) {
167 $q = $this->make_question($questiondata);
168 return 1 / count($q->choices);
b25486fc 169 }
170
93cadb1e
TH
171 function get_possible_responses($questiondata) {
172 $subqs = array();
516cf3eb 173
93cadb1e 174 $q = $this->make_question($questiondata);
aeb15530 175
93cadb1e 176 foreach ($q->stems as $stemid => $stem) {
aeb15530 177
93cadb1e
TH
178 $responses = array();
179 foreach ($q->choices as $choiceid => $choice) {
180 $responses[$choiceid] = new question_possible_response(
181 $q->html_to_text($stem) . ': ' . $q->html_to_text($choice),
182 ($choiceid == $q->right[$stemid]) / count($q->stems));
869309b8 183 }
93cadb1e 184 $responses[null] = question_possible_response::no_response();
069a820a 185
93cadb1e 186 $subqs[$stemid] = $responses;
b9bd6da4 187 }
188
93cadb1e 189 return $subqs;
b9bd6da4 190 }
fe6ce234 191
5d548d3e 192 function move_files($questionid, $oldcontextid, $newcontextid) {
fe6ce234 193 global $DB;
fe6ce234 194 $fs = get_file_storage();
fe6ce234 195
5d548d3e
TH
196 parent::move_files($questionid, $oldcontextid, $newcontextid);
197
198 $subquestionids = $DB->get_records_menu('question_match_sub',
199 array('question' => $questionid), 'id', 'id,1');
200 foreach ($subquestionids as $subquestionid => $notused) {
201 $fs->move_area_files_to_new_context($oldcontextid,
202 $newcontextid, 'qtype_match', 'subquestion', $subquestionid);
fe6ce234
DC
203 }
204 }
5d548d3e 205
9203b705
TH
206 protected function delete_files($questionid, $contextid) {
207 global $DB;
208 $fs = get_file_storage();
209
210 parent::delete_files($questionid, $contextid);
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->delete_area_files($contextid, 'qtype_match', 'subquestion', $subquestionid);
216 }
1c2ed7c5
TH
217
218 $fs->delete_area_files($contextid, 'qtype_multichoice', 'correctfeedback', $questionid);
219 $fs->delete_area_files($contextid, 'qtype_multichoice', 'partiallycorrectfeedback', $questionid);
220 $fs->delete_area_files($contextid, 'qtype_multichoice', 'incorrectfeedback', $questionid);
9203b705 221 }
516cf3eb 222}