1 |
efrain |
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 |
/**
|
|
|
18 |
* Test helpers for the essay question type.
|
|
|
19 |
*
|
|
|
20 |
* @package qtype_essay
|
|
|
21 |
* @copyright 2013 The Open University
|
|
|
22 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
23 |
*/
|
|
|
24 |
|
|
|
25 |
|
|
|
26 |
defined('MOODLE_INTERNAL') || die();
|
|
|
27 |
|
|
|
28 |
|
|
|
29 |
/**
|
|
|
30 |
* Test helper class for the essay question type.
|
|
|
31 |
*
|
|
|
32 |
* @copyright 2013 The Open University
|
|
|
33 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
34 |
*/
|
|
|
35 |
class qtype_essay_test_helper extends question_test_helper {
|
|
|
36 |
public function get_test_questions() {
|
|
|
37 |
return array('editor', 'editorfilepicker', 'plain', 'monospaced', 'responsetemplate', 'noinline');
|
|
|
38 |
}
|
|
|
39 |
|
|
|
40 |
/**
|
|
|
41 |
* Helper method to reduce duplication.
|
|
|
42 |
* @return qtype_essay_question
|
|
|
43 |
*/
|
|
|
44 |
protected function initialise_essay_question() {
|
|
|
45 |
question_bank::load_question_definition_classes('essay');
|
|
|
46 |
$q = new qtype_essay_question();
|
|
|
47 |
test_question_maker::initialise_a_question($q);
|
|
|
48 |
$q->name = 'Essay question (HTML editor)';
|
|
|
49 |
$q->questiontext = 'Please write a story about a frog.';
|
|
|
50 |
$q->generalfeedback = 'I hope your story had a beginning, a middle and an end.';
|
|
|
51 |
$q->responseformat = 'editor';
|
|
|
52 |
$q->responserequired = 1;
|
|
|
53 |
$q->responsefieldlines = 10;
|
|
|
54 |
$q->minwordlimit = null;
|
|
|
55 |
$q->maxwordlimit = null;
|
|
|
56 |
$q->attachments = 0;
|
|
|
57 |
$q->attachmentsrequired = 0;
|
|
|
58 |
$q->maxbytes = 0;
|
|
|
59 |
$q->filetypeslist = null;
|
|
|
60 |
$q->graderinfo = '';
|
|
|
61 |
$q->graderinfoformat = FORMAT_HTML;
|
|
|
62 |
$q->qtype = question_bank::get_qtype('essay');
|
|
|
63 |
|
|
|
64 |
return $q;
|
|
|
65 |
}
|
|
|
66 |
|
|
|
67 |
/**
|
|
|
68 |
* Makes an essay question using the HTML editor as input.
|
|
|
69 |
* @return qtype_essay_question
|
|
|
70 |
*/
|
|
|
71 |
public function make_essay_question_editor() {
|
|
|
72 |
return $this->initialise_essay_question();
|
|
|
73 |
}
|
|
|
74 |
|
|
|
75 |
/**
|
|
|
76 |
* Make the data what would be received from the editing form for an essay
|
|
|
77 |
* question using the HTML editor allowing embedded files as input, and up
|
|
|
78 |
* to three attachments.
|
|
|
79 |
*
|
|
|
80 |
* @return stdClass the data that would be returned by $form->get_gata();
|
|
|
81 |
*/
|
|
|
82 |
public function get_essay_question_form_data_editor() {
|
|
|
83 |
$fromform = new stdClass();
|
|
|
84 |
|
|
|
85 |
$fromform->name = 'Essay question (HTML editor)';
|
|
|
86 |
$fromform->questiontext = array('text' => 'Please write a story about a frog.', 'format' => FORMAT_HTML);
|
|
|
87 |
$fromform->defaultmark = 1.0;
|
|
|
88 |
$fromform->generalfeedback = array('text' => 'I hope your story had a beginning, a middle and an end.', 'format' => FORMAT_HTML);
|
|
|
89 |
$fromform->responseformat = 'editor';
|
|
|
90 |
$fromform->responserequired = 1;
|
|
|
91 |
$fromform->responsefieldlines = 10;
|
|
|
92 |
$fromform->attachments = 0;
|
|
|
93 |
$fromform->attachmentsrequired = 0;
|
|
|
94 |
$fromform->maxbytes = 0;
|
|
|
95 |
$fromform->filetypeslist = ''; // Although once saved in the DB, this becomes null, the form returns '' here.
|
|
|
96 |
$fromform->graderinfo = array('text' => '', 'format' => FORMAT_HTML);
|
|
|
97 |
$fromform->responsetemplate = array('text' => '', 'format' => FORMAT_HTML);
|
|
|
98 |
$fromform->status = \core_question\local\bank\question_version_status::QUESTION_STATUS_READY;
|
|
|
99 |
|
|
|
100 |
return $fromform;
|
|
|
101 |
}
|
|
|
102 |
|
|
|
103 |
/**
|
|
|
104 |
* Makes an essay question using the HTML editor allowing embedded files as
|
|
|
105 |
* input, and up to three attachments.
|
|
|
106 |
* @return qtype_essay_question
|
|
|
107 |
*/
|
|
|
108 |
public function make_essay_question_editorfilepicker() {
|
|
|
109 |
$q = $this->initialise_essay_question();
|
|
|
110 |
$q->responseformat = 'editorfilepicker';
|
|
|
111 |
$q->attachments = 3;
|
|
|
112 |
return $q;
|
|
|
113 |
}
|
|
|
114 |
|
|
|
115 |
/**
|
|
|
116 |
* Makes an essay question using the HTML editor allowing embedded files as
|
|
|
117 |
* input, and up to two attachments, two needed.
|
|
|
118 |
* @return qtype_essay_question
|
|
|
119 |
*/
|
|
|
120 |
public function make_essay_question_editorfilepickertworequired() {
|
|
|
121 |
$q = $this->initialise_essay_question();
|
|
|
122 |
$q->responseformat = 'editorfilepicker';
|
|
|
123 |
$q->attachments = 2;
|
|
|
124 |
$q->attachmentsrequired = 2;
|
|
|
125 |
return $q;
|
|
|
126 |
}
|
|
|
127 |
|
|
|
128 |
/**
|
|
|
129 |
* Make the data what would be received from the editing form for an essay
|
|
|
130 |
* question using the HTML editor allowing embedded files as input, and up
|
|
|
131 |
* to three attachments.
|
|
|
132 |
*
|
|
|
133 |
* @return stdClass the data that would be returned by $form->get_gata();
|
|
|
134 |
*/
|
|
|
135 |
public function get_essay_question_form_data_editorfilepicker() {
|
|
|
136 |
$fromform = new stdClass();
|
|
|
137 |
|
|
|
138 |
$fromform->name = 'Essay question with filepicker and attachments';
|
|
|
139 |
$fromform->questiontext = array('text' => 'Please write a story about a frog.', 'format' => FORMAT_HTML);
|
|
|
140 |
$fromform->defaultmark = 1.0;
|
|
|
141 |
$fromform->generalfeedback = array('text' => 'I hope your story had a beginning, a middle and an end.', 'format' => FORMAT_HTML);
|
|
|
142 |
$fromform->responseformat = 'editorfilepicker';
|
|
|
143 |
$fromform->responserequired = 1;
|
|
|
144 |
$fromform->responsefieldlines = 10;
|
|
|
145 |
$fromform->attachments = 3;
|
|
|
146 |
$fromform->attachmentsrequired = 0;
|
|
|
147 |
$fromform->maxbytes = 0;
|
|
|
148 |
$fromform->filetypeslist = ''; // Although once saved in the DB, this becomes null, the form returns '' here.
|
|
|
149 |
$fromform->graderinfo = array('text' => '', 'format' => FORMAT_HTML);
|
|
|
150 |
$fromform->responsetemplate = array('text' => '', 'format' => FORMAT_HTML);
|
|
|
151 |
$fromform->status = \core_question\local\bank\question_version_status::QUESTION_STATUS_READY;
|
|
|
152 |
|
|
|
153 |
return $fromform;
|
|
|
154 |
}
|
|
|
155 |
|
|
|
156 |
/**
|
|
|
157 |
* Makes an essay question using plain text input.
|
|
|
158 |
* @return qtype_essay_question
|
|
|
159 |
*/
|
|
|
160 |
public function make_essay_question_plain() {
|
|
|
161 |
$q = $this->initialise_essay_question();
|
|
|
162 |
$q->responseformat = 'plain';
|
|
|
163 |
return $q;
|
|
|
164 |
}
|
|
|
165 |
|
|
|
166 |
/**
|
|
|
167 |
* Make the data what would be received from the editing form for an essay
|
|
|
168 |
* question using the HTML editor allowing embedded files as input, and up
|
|
|
169 |
* to three attachments.
|
|
|
170 |
*
|
|
|
171 |
* @return stdClass the data that would be returned by $form->get_gata();
|
|
|
172 |
*/
|
|
|
173 |
public function get_essay_question_form_data_plain() {
|
|
|
174 |
$fromform = new stdClass();
|
|
|
175 |
|
|
|
176 |
$fromform->name = 'Essay question with filepicker and attachments';
|
|
|
177 |
$fromform->questiontext = array('text' => 'Please write a story about a frog.', 'format' => FORMAT_HTML);
|
|
|
178 |
$fromform->defaultmark = 1.0;
|
|
|
179 |
$fromform->generalfeedback = array('text' => 'I hope your story had a beginning, a middle and an end.', 'format' => FORMAT_HTML);
|
|
|
180 |
$fromform->responseformat = 'plain';
|
|
|
181 |
$fromform->responserequired = 1;
|
|
|
182 |
$fromform->responsefieldlines = 10;
|
|
|
183 |
$fromform->attachments = 0;
|
|
|
184 |
$fromform->attachmentsrequired = 0;
|
|
|
185 |
$fromform->maxbytes = 0;
|
|
|
186 |
$fromform->filetypeslist = ''; // Although once saved in the DB, this becomes null, the form returns '' here.
|
|
|
187 |
$fromform->graderinfo = array('text' => '', 'format' => FORMAT_HTML);
|
|
|
188 |
$fromform->responsetemplate = array('text' => '', 'format' => FORMAT_HTML);
|
|
|
189 |
$fromform->status = \core_question\local\bank\question_version_status::QUESTION_STATUS_READY;
|
|
|
190 |
|
|
|
191 |
return $fromform;
|
|
|
192 |
}
|
|
|
193 |
|
|
|
194 |
/**
|
|
|
195 |
* Makes an essay question using monospaced input.
|
|
|
196 |
* @return qtype_essay_question
|
|
|
197 |
*/
|
|
|
198 |
public function make_essay_question_monospaced() {
|
|
|
199 |
$q = $this->initialise_essay_question();
|
|
|
200 |
$q->responseformat = 'monospaced';
|
|
|
201 |
return $q;
|
|
|
202 |
}
|
|
|
203 |
|
|
|
204 |
public function make_essay_question_responsetemplate() {
|
|
|
205 |
$q = $this->initialise_essay_question();
|
|
|
206 |
$q->responsetemplate = 'Once upon a time';
|
|
|
207 |
$q->responsetemplateformat = FORMAT_HTML;
|
|
|
208 |
return $q;
|
|
|
209 |
}
|
|
|
210 |
|
|
|
211 |
/**
|
|
|
212 |
* Makes an essay question without an online text editor.
|
|
|
213 |
* @return qtype_essay_question
|
|
|
214 |
*/
|
|
|
215 |
public function make_essay_question_noinline() {
|
|
|
216 |
$q = $this->initialise_essay_question();
|
|
|
217 |
$q->responseformat = 'noinline';
|
|
|
218 |
$q->attachments = 3;
|
|
|
219 |
$q->attachmentsrequired = 1;
|
|
|
220 |
$q->filetypeslist = '';
|
|
|
221 |
$q->maxbytes = 0;
|
|
|
222 |
return $q;
|
|
|
223 |
}
|
|
|
224 |
|
|
|
225 |
/**
|
|
|
226 |
* Creates an empty draft area for attachments.
|
|
|
227 |
* @return int The draft area's itemid.
|
|
|
228 |
*/
|
|
|
229 |
protected function make_attachment_draft_area() {
|
|
|
230 |
$draftid = 0;
|
|
|
231 |
$contextid = 0;
|
|
|
232 |
|
|
|
233 |
$component = 'question';
|
|
|
234 |
$filearea = 'response_attachments';
|
|
|
235 |
|
|
|
236 |
// Create an empty file area.
|
|
|
237 |
file_prepare_draft_area($draftid, $contextid, $component, $filearea, null);
|
|
|
238 |
return $draftid;
|
|
|
239 |
}
|
|
|
240 |
|
|
|
241 |
/**
|
|
|
242 |
* Creates an attachment in the provided attachment draft area.
|
|
|
243 |
* @param int $draftid The itemid for the draft area in which the file should be created.
|
|
|
244 |
* @param string $name The filename for the file to be created.
|
|
|
245 |
* @param string $contents The contents of the file to be created.
|
|
|
246 |
*/
|
|
|
247 |
protected function make_attachment($draftid, $name, $contents) {
|
|
|
248 |
global $USER;
|
|
|
249 |
|
|
|
250 |
$fs = get_file_storage();
|
|
|
251 |
$usercontext = context_user::instance($USER->id);
|
|
|
252 |
|
|
|
253 |
// Create the file in the provided draft area.
|
|
|
254 |
$fileinfo = array(
|
|
|
255 |
'contextid' => $usercontext->id,
|
|
|
256 |
'component' => 'user',
|
|
|
257 |
'filearea' => 'draft',
|
|
|
258 |
'itemid' => $draftid,
|
|
|
259 |
'filepath' => '/',
|
|
|
260 |
'filename' => $name,
|
|
|
261 |
);
|
|
|
262 |
$fs->create_file_from_string($fileinfo, $contents);
|
|
|
263 |
}
|
|
|
264 |
|
|
|
265 |
/**
|
|
|
266 |
* Generates a draft file area that contains the provided number of attachments. You should ensure
|
|
|
267 |
* that a user is logged in with setUser before you run this function.
|
|
|
268 |
*
|
|
|
269 |
* @param int $attachments The number of attachments to generate.
|
|
|
270 |
* @return int The itemid of the generated draft file area.
|
|
|
271 |
*/
|
|
|
272 |
public function make_attachments($attachments) {
|
|
|
273 |
$draftid = $this->make_attachment_draft_area();
|
|
|
274 |
|
|
|
275 |
// Create the relevant amount of dummy attachments in the given draft area.
|
|
|
276 |
for ($i = 0; $i < $attachments; ++$i) {
|
|
|
277 |
$this->make_attachment($draftid, $i, $i);
|
|
|
278 |
}
|
|
|
279 |
|
|
|
280 |
return $draftid;
|
|
|
281 |
}
|
|
|
282 |
|
|
|
283 |
/**
|
|
|
284 |
* Generates a question_file_saver that contains the provided number of attachments. You should ensure
|
|
|
285 |
* that a user is logged in with setUser before you run this function.
|
|
|
286 |
*
|
|
|
287 |
* @param int $:attachments The number of attachments to generate.
|
|
|
288 |
* @return question_file_saver a question_file_saver that contains the given amount of dummy files, for use in testing.
|
|
|
289 |
*/
|
|
|
290 |
public function make_attachments_saver($attachments) {
|
|
|
291 |
return new question_file_saver($this->make_attachments($attachments), 'question', 'response_attachments');
|
|
|
292 |
}
|
|
|
293 |
|
|
|
294 |
|
|
|
295 |
}
|