MDL-70153 qtype_essay: Add behat test for attachments max size
[moodle.git] / question / type / ddimageortext / questiontypebase.php
CommitLineData
d2c112fd
JP
1<?php
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
17/**
c109c901 18 * Question type class for the drag-and-drop onto image question type.
d2c112fd 19 *
c109c901 20 * @package qtype_ddimageortext
d2c112fd
JP
21 * @copyright 2009 The Open University
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23 */
24
25
26defined('MOODLE_INTERNAL') || die();
27
28require_once($CFG->libdir . '/questionlib.php');
29require_once($CFG->dirroot . '/question/engine/lib.php');
30require_once($CFG->dirroot . '/question/format/xml/format.php');
31require_once($CFG->dirroot . '/question/type/gapselect/questiontypebase.php');
32
33/**
c109c901 34 * The drag-and-drop onto image question type class.
d2c112fd
JP
35 *
36 * @copyright 2009 The Open University
37 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
38 */
39class qtype_ddtoimage_base extends question_type {
8bc1d28b
EM
40 /**
41 * Returns the choice group key.
42 *
43 * @return string
44 */
d2c112fd
JP
45 protected function choice_group_key() {
46 return 'draggroup';
47 }
48
d2c112fd
JP
49 public function get_question_options($question) {
50 global $DB;
eaabb891
JP
51 $dbprefix = 'qtype_'.$this->name();
52 $question->options = $DB->get_record($dbprefix,
d2c112fd 53 array('questionid' => $question->id), '*', MUST_EXIST);
eaabb891 54 $question->options->drags = $DB->get_records($dbprefix.'_drags',
d2c112fd 55 array('questionid' => $question->id), 'no ASC', '*');
eaabb891 56 $question->options->drops = $DB->get_records($dbprefix.'_drops',
d2c112fd
JP
57 array('questionid' => $question->id), 'no ASC', '*');
58 parent::get_question_options($question);
59 }
60
d2c112fd
JP
61 protected function initialise_question_instance(question_definition $question, $questiondata) {
62 parent::initialise_question_instance($question, $questiondata);
63 $question->shufflechoices = $questiondata->options->shuffleanswers;
64
65 $this->initialise_combined_feedback($question, $questiondata, true);
66
67 $question->choices = array();
13ee9a26 68 $choiceindexmap = array();
d2c112fd
JP
69
70 // Store the choices in arrays by group.
2bf2e46c
TH
71 // This code is weird. The first choice in each group gets key 1 in the
72 // $question->choices[$choice->choice_group()] array, and the others get
73 // key $choice->no. Therefore you need to think carefully whether you
74 // are using the key, or $choice->no. This is presumably a mistake, but
75 // one that is now essentially un-fixable, since many questions of this
76 // type have been attempted, and theys keys get stored in the attempt data.
d2c112fd
JP
77 foreach ($questiondata->options->drags as $dragdata) {
78
79 $choice = $this->make_choice($dragdata);
80
81 if (array_key_exists($choice->choice_group(), $question->choices)) {
82 $question->choices[$choice->choice_group()][$dragdata->no] = $choice;
83 } else {
84 $question->choices[$choice->choice_group()][1] = $choice;
85 }
86
87 end($question->choices[$choice->choice_group()]);
88 $choiceindexmap[$dragdata->no] = array($choice->choice_group(),
89 key($question->choices[$choice->choice_group()]));
90 }
91
92 $question->places = array();
93 $question->rightchoices = array();
94
95 $i = 1;
96
97 foreach ($questiondata->options->drops as $dropdata) {
98 list($group, $choiceindex) = $choiceindexmap[$dropdata->choice];
99 $dropdata->group = $group;
100 $question->places[$dropdata->no] = $this->make_place($dropdata);
101 $question->rightchoices[$dropdata->no] = $choiceindex;
102 }
103 }
104
d2c112fd
JP
105 /**
106 * Convert files into text output in the given format.
107 * This method is copied from qformat_default as a quick fix, as the method there is
108 * protected.
8bc1d28b
EM
109 * @param array $files
110 * @param int $indent Number of spaces to indent
d2c112fd
JP
111 * @return string $string
112 */
113 public function write_files($files, $indent) {
114 if (empty($files)) {
115 return '';
116 }
117 $string = '';
118 foreach ($files as $file) {
119 if ($file->is_directory()) {
120 continue;
121 }
122 $string .= str_repeat(' ', $indent);
123 $string .= '<file name="' . $file->get_filename() . '" encoding="base64">';
124 $string .= base64_encode($file->get_content());
125 $string .= "</file>\n";
126 }
127 return $string;
128 }
129
130 public function get_possible_responses($questiondata) {
131 $question = $this->make_question($questiondata);
132
133 $parts = array();
134 foreach ($question->places as $placeno => $place) {
d2c112fd
JP
135 $choices = array();
136
13ee9a26 137 foreach ($question->choices[$place->group] as $i => $choice) {
d2c112fd 138 $correct = $question->rightchoices[$placeno] == $i;
13ee9a26 139 $choices[$choice->no] = new question_possible_response($choice->summarise(), $correct ? 1 : 0);
d2c112fd
JP
140 }
141 $choices[null] = question_possible_response::no_response();
142
143 $parts[$placeno] = $choices;
144 }
145
146 return $parts;
147 }
148
149 public function get_random_guess_score($questiondata) {
150 $question = $this->make_question($questiondata);
151 return $question->get_random_guess_score();
152 }
cb5c4e62
JP
153 public function delete_question($questionid, $contextid) {
154 global $DB;
5b7d9dec 155 $DB->delete_records('qtype_'.$this->name(), array('questionid' => $questionid));
cb5c4e62
JP
156 $DB->delete_records('qtype_'.$this->name().'_drags', array('questionid' => $questionid));
157 $DB->delete_records('qtype_'.$this->name().'_drops', array('questionid' => $questionid));
158 return parent::delete_question($questionid, $contextid);
159 }
d2c112fd 160}