MDL-49178 Lesson: Add question pages to generators
[moodle.git] / mod / lesson / tests / generator / lib.php
CommitLineData
78135fd2
MG
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 * mod_lesson data generator.
19 *
20 * @package mod_lesson
21 * @category test
22 * @copyright 2013 Marina Glancy
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 */
25
26defined('MOODLE_INTERNAL') || die();
27
28/**
29 * mod_lesson data generator class.
30 *
31 * @package mod_lesson
32 * @category test
33 * @copyright 2013 Marina Glancy
34 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
35 */
36class mod_lesson_generator extends testing_module_generator {
37
38 /**
39 * @var int keep track of how many pages have been created.
40 */
41 protected $pagecount = 0;
42
43 /**
44 * To be called from data reset code only,
45 * do not use in tests.
46 * @return void
47 */
48 public function reset() {
49 $this->pagecount = 0;
50 parent::reset();
51 }
52
53 public function create_instance($record = null, array $options = null) {
54 global $CFG;
55
56 // Add default values for lesson.
57 $record = (array)$record + array(
58 'progressbar' => 0,
59 'ongoing' => 0,
60 'displayleft' => 0,
61 'displayleftif' => 0,
62 'slideshow' => 0,
63 'maxanswers' => $CFG->lesson_maxanswers,
64 'feedback' => 0,
65 'activitylink' => 0,
66 'available' => 0,
67 'deadline' => 0,
68 'usepassword' => 0,
69 'password' => '',
70 'dependency' => 0,
71 'timespent' => 0,
72 'completed' => 0,
73 'gradebetterthan' => 0,
74 'modattempts' => 0,
75 'review' => 0,
76 'maxattempts' => 1,
77 'nextpagedefault' => $CFG->lesson_defaultnextpage,
78 'maxpages' => 0,
79 'practice' => 0,
80 'custom' => 1,
81 'retake' => 0,
82 'usemaxgrade' => 0,
83 'minquestions' => 0,
84 'grade' => 100,
85 );
86 if (!isset($record['mediafile'])) {
87 require_once($CFG->libdir.'/filelib.php');
88 $record['mediafile'] = file_get_unused_draft_itemid();
89 }
90
91 return parent::create_instance($record, (array)$options);
92 }
93
94 public function create_content($lesson, $record = array()) {
95 global $DB, $CFG;
96 require_once($CFG->dirroot.'/mod/lesson/locallib.php');
97 $now = time();
98 $this->pagecount++;
99 $record = (array)$record + array(
100 'lessonid' => $lesson->id,
101 'title' => 'Lesson page '.$this->pagecount,
102 'timecreated' => $now,
103 'qtype' => 20, // LESSON_PAGE_BRANCHTABLE
104 'pageid' => 0, // By default insert in the beginning.
105 );
106 if (!isset($record['contents_editor'])) {
107 $record['contents_editor'] = array(
108 'text' => 'Contents of lesson page '.$this->pagecount,
109 'format' => FORMAT_MOODLE,
110 'itemid' => 0,
111 );
112 }
113 $context = context_module::instance($lesson->cmid);
114 $page = lesson_page::create((object)$record, new lesson($lesson), $context, $CFG->maxbytes);
115 return $DB->get_record('lesson_pages', array('id' => $page->id), '*', MUST_EXIST);
116 }
6f5d9a93
SB
117
118 /**
119 * Create True/false question pages.
120 * @param object $lesson
121 * @param array $record
122 * @return int
123 */
124 public function create_question_truefalse($lesson, $record = array()) {
125 global $DB, $CFG;
126 require_once($CFG->dirroot.'/mod/lesson/locallib.php');
127 $now = time();
128 $this->pagecount++;
129 $record = (array)$record + array(
130 'lessonid' => $lesson->id,
131 'title' => 'Lesson TF question '.$this->pagecount,
132 'timecreated' => $now,
133 'qtype' => 2, // LESSON_PAGE_TRUEFALSE.
134 'pageid' => 0, // By default insert in the beginning.
135 );
136 if (!isset($record['contents_editor'])) {
137 $record['contents_editor'] = array(
138 'text' => 'The answer is TRUE '.$this->pagecount,
139 'format' => FORMAT_HTML,
140 'itemid' => 0
141 );
142 }
143
144 // First Answer (TRUE).
145 if (!isset($record['answer_editor'][0])) {
146 $record['answer_editor'][0] = array(
147 'text' => 'TRUE answer for '.$this->pagecount,
148 'format' => FORMAT_HTML
149 );
150 }
151 if (!isset($record['jumpto'][0])) {
152 $record['jumpto'][0] = LESSON_NEXTPAGE;
153 }
154
155 // Second Answer (FALSE).
156 if (!isset($record['answer_editor'][1])) {
157 $record['answer_editor'][1] = array(
158 'text' => 'FALSE answer for '.$this->pagecount,
159 'format' => FORMAT_HTML
160 );
161 }
162 if (!isset($record['jumpto'][1])) {
163 $record['jumpto'][1] = LESSON_THISPAGE;
164 }
165
166 $context = context_module::instance($lesson->cmid);
167 $page = lesson_page::create((object)$record, new lesson($lesson), $context, $CFG->maxbytes);
168 return $DB->get_record('lesson_pages', array('id' => $page->id), '*', MUST_EXIST);
169 }
170
171 /**
172 * Create multichoice question pages.
173 * @param object $lesson
174 * @param array $record
175 * @return int
176 */
177 public function create_question_multichoice($lesson, $record = array()) {
178 global $DB, $CFG;
179 require_once($CFG->dirroot.'/mod/lesson/locallib.php');
180 $now = time();
181 $this->pagecount++;
182 $record = (array)$record + array(
183 'lessonid' => $lesson->id,
184 'title' => 'Lesson multichoice question '.$this->pagecount,
185 'timecreated' => $now,
186 'qtype' => 3, // LESSON_PAGE_MULTICHOICE.
187 'pageid' => 0, // By default insert in the beginning.
188 );
189 if (!isset($record['contents_editor'])) {
190 $record['contents_editor'] = array(
191 'text' => 'Pick the correct answer '.$this->pagecount,
192 'format' => FORMAT_HTML,
193 'itemid' => 0
194 );
195 }
196
197 // First Answer (correct).
198 if (!isset($record['answer_editor'][0])) {
199 $record['answer_editor'][0] = array(
200 'text' => 'correct answer for '.$this->pagecount,
201 'format' => FORMAT_HTML
202 );
203 }
204 if (!isset($record['jumpto'][0])) {
205 $record['jumpto'][0] = LESSON_NEXTPAGE;
206 }
207
208 // Second Answer (incorrect).
209 if (!isset($record['answer_editor'][1])) {
210 $record['answer_editor'][1] = array(
211 'text' => 'correct answer for '.$this->pagecount,
212 'format' => FORMAT_HTML
213 );
214 }
215 if (!isset($record['jumpto'][1])) {
216 $record['jumpto'][1] = LESSON_THISPAGE;
217 }
218
219 $context = context_module::instance($lesson->cmid);
220 $page = lesson_page::create((object)$record, new lesson($lesson), $context, $CFG->maxbytes);
221 return $DB->get_record('lesson_pages', array('id' => $page->id), '*', MUST_EXIST);
222 }
223
224 /**
225 * Create essay question pages.
226 * @param object $lesson
227 * @param array $record
228 * @return int
229 */
230 public function create_question_essay($lesson, $record = array()) {
231 global $DB, $CFG;
232 require_once($CFG->dirroot.'/mod/lesson/locallib.php');
233 $now = time();
234 $this->pagecount++;
235 $record = (array)$record + array(
236 'lessonid' => $lesson->id,
237 'title' => 'Lesson Essay question '.$this->pagecount,
238 'timecreated' => $now,
239 'qtype' => 10, // LESSON_PAGE_ESSAY.
240 'pageid' => 0, // By default insert in the beginning.
241 );
242 if (!isset($record['contents_editor'])) {
243 $record['contents_editor'] = array(
244 'text' => 'Write an Essay '.$this->pagecount,
245 'format' => FORMAT_HTML,
246 'itemid' => 0
247 );
248 }
249
250 // Essays have an answer of NULL.
251 if (!isset($record['answer_editor'][0])) {
252 $record['answer_editor'][0] = array(
253 'text' => null,
254 'format' => FORMAT_MOODLE
255 );
256 }
257 if (!isset($record['jumpto'][0])) {
258 $record['jumpto'][0] = LESSON_NEXTPAGE;
259 }
260
261 $context = context_module::instance($lesson->cmid);
262 $page = lesson_page::create((object)$record, new lesson($lesson), $context, $CFG->maxbytes);
263 return $DB->get_record('lesson_pages', array('id' => $page->id), '*', MUST_EXIST);
264 }
265
266 /**
267 * Create matching question pages.
268 * @param object $lesson
269 * @param array $record
270 * @return int
271 */
272 public function create_question_matching($lesson, $record = array()) {
273 global $DB, $CFG;
274 require_once($CFG->dirroot.'/mod/lesson/locallib.php');
275 $now = time();
276 $this->pagecount++;
277 $record = (array)$record + array(
278 'lessonid' => $lesson->id,
279 'title' => 'Lesson Matching question '.$this->pagecount,
280 'timecreated' => $now,
281 'qtype' => 5, // LESSON_PAGE_MATCHING.
282 'pageid' => 0, // By default insert in the beginning.
283 );
284 if (!isset($record['contents_editor'])) {
285 $record['contents_editor'] = array(
286 'text' => 'Match the values '.$this->pagecount,
287 'format' => FORMAT_HTML,
288 'itemid' => 0
289 );
290 }
291 // Feedback for correct result.
292 if (!isset($record['answer_editor'][0])) {
293 $record['answer_editor'][0] = array(
294 'text' => '',
295 'format' => FORMAT_HTML
296 );
297 }
298 // Feedback for wrong result.
299 if (!isset($record['answer_editor'][1])) {
300 $record['answer_editor'][1] = array(
301 'text' => '',
302 'format' => FORMAT_HTML
303 );
304 }
305 // First answer value.
306 if (!isset($record['answer_editor'][2])) {
307 $record['answer_editor'][2] = array(
308 'text' => 'Match value 1',
309 'format' => FORMAT_HTML
310 );
311 }
312 // First response value.
313 if (!isset($record['response_editor'][2])) {
314 $record['response_editor'][2] = 'Match answer 1';
315 }
316 // Second Matching value.
317 if (!isset($record['answer_editor'][3])) {
318 $record['answer_editor'][3] = array(
319 'text' => 'Match value 2',
320 'format' => FORMAT_HTML
321 );
322 }
323 // Second Matching answer.
324 if (!isset($record['response_editor'][3])) {
325 $record['response_editor'][3] = 'Match answer 2';
326 }
327
328 // Jump Values.
329 if (!isset($record['jumpto'][0])) {
330 $record['jumpto'][0] = LESSON_NEXTPAGE;
331 }
332 if (!isset($record['jumpto'][1])) {
333 $record['jumpto'][1] = LESSON_THISPAGE;
334 }
335
336 // Mark the correct values.
337 if (!isset($record['score'][0])) {
338 $record['score'][0] = 1;
339 }
340 $context = context_module::instance($lesson->cmid);
341 $page = lesson_page::create((object)$record, new lesson($lesson), $context, $CFG->maxbytes);
342 return $DB->get_record('lesson_pages', array('id' => $page->id), '*', MUST_EXIST);
343 }
344
345 /**
346 * Create shortanswer question pages.
347 * @param object $lesson
348 * @param array $record
349 * @return int
350 */
351 public function create_question_shortanswer($lesson, $record = array()) {
352 global $DB, $CFG;
353 require_once($CFG->dirroot.'/mod/lesson/locallib.php');
354 $now = time();
355 $this->pagecount++;
356 $record = (array)$record + array(
357 'lessonid' => $lesson->id,
358 'title' => 'Lesson Shortanswer question '.$this->pagecount,
359 'timecreated' => $now,
360 'qtype' => 1, // LESSON_PAGE_SHORTANSWER.
361 'pageid' => 0, // By default insert in the beginning.
362 );
363 if (!isset($record['contents_editor'])) {
364 $record['contents_editor'] = array(
365 'text' => 'Fill in the blank '.$this->pagecount,
366 'format' => FORMAT_HTML,
367 'itemid' => 0
368 );
369 }
370
371 // First Answer (correct).
372 if (!isset($record['answer_editor'][0])) {
373 $record['answer_editor'][0] = array(
374 'text' => 'answer'.$this->pagecount,
375 'format' => FORMAT_MOODLE
376 );
377 }
378 if (!isset($record['jumpto'][0])) {
379 $record['jumpto'][0] = LESSON_NEXTPAGE;
380 }
381
382 $context = context_module::instance($lesson->cmid);
383 $page = lesson_page::create((object)$record, new lesson($lesson), $context, $CFG->maxbytes);
384 return $DB->get_record('lesson_pages', array('id' => $page->id), '*', MUST_EXIST);
385 }
386
387 /**
388 * Create shortanswer question pages.
389 * @param object $lesson
390 * @param array $record
391 * @return int
392 */
393 public function create_question_numeric($lesson, $record = array()) {
394 global $DB, $CFG;
395 require_once($CFG->dirroot.'/mod/lesson/locallib.php');
396 $now = time();
397 $this->pagecount++;
398 $record = (array)$record + array(
399 'lessonid' => $lesson->id,
400 'title' => 'Lesson numerical question '.$this->pagecount,
401 'timecreated' => $now,
402 'qtype' => 8, // LESSON_PAGE_NUMERICAL.
403 'pageid' => 0, // By default insert in the beginning.
404 );
405 if (!isset($record['contents_editor'])) {
406 $record['contents_editor'] = array(
407 'text' => 'Numerical question '.$this->pagecount,
408 'format' => FORMAT_HTML,
409 'itemid' => 0
410 );
411 }
412
413 // First Answer (correct).
414 if (!isset($record['answer_editor'][0])) {
415 $record['answer_editor'][0] = array(
416 'text' => $this->pagecount,
417 'format' => FORMAT_MOODLE
418 );
419 }
420 if (!isset($record['jumpto'][0])) {
421 $record['jumpto'][0] = LESSON_NEXTPAGE;
422 }
423
424 $context = context_module::instance($lesson->cmid);
425 $page = lesson_page::create((object)$record, new lesson($lesson), $context, $CFG->maxbytes);
426 return $DB->get_record('lesson_pages', array('id' => $page->id), '*', MUST_EXIST);
427 }
78135fd2 428}