2 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
18 * mod_lesson data generator.
22 * @copyright 2013 Marina Glancy
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 defined('MOODLE_INTERNAL') || die();
29 * mod_lesson data generator class.
33 * @copyright 2013 Marina Glancy
34 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
36 class mod_lesson_generator extends testing_module_generator {
39 * @var int keep track of how many pages have been created.
41 protected $pagecount = 0;
44 * To be called from data reset code only,
45 * do not use in tests.
48 public function reset() {
53 public function create_instance($record = null, array $options = null) {
56 // Add default values for lesson.
57 $record = (array)$record + array(
63 'maxanswers' => $CFG->lesson_maxanswers,
73 'gradebetterthan' => 0,
77 'nextpagedefault' => $CFG->lesson_defaultnextpage,
86 if (!isset($record['mediafile'])) {
87 require_once($CFG->libdir.'/filelib.php');
88 $record['mediafile'] = file_get_unused_draft_itemid();
91 return parent::create_instance($record, (array)$options);
94 public function create_content($lesson, $record = array()) {
96 require_once($CFG->dirroot.'/mod/lesson/locallib.php');
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.
106 if (!isset($record['contents_editor'])) {
107 $record['contents_editor'] = array(
108 'text' => 'Contents of lesson page '.$this->pagecount,
109 'format' => FORMAT_MOODLE,
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);
119 * Create True/false question pages.
120 * @param object $lesson
121 * @param array $record
124 public function create_question_truefalse($lesson, $record = array()) {
126 require_once($CFG->dirroot.'/mod/lesson/locallib.php');
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.
136 if (!isset($record['contents_editor'])) {
137 $record['contents_editor'] = array(
138 'text' => 'The answer is TRUE '.$this->pagecount,
139 'format' => FORMAT_HTML,
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
151 if (!isset($record['jumpto'][0])) {
152 $record['jumpto'][0] = LESSON_NEXTPAGE;
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
162 if (!isset($record['jumpto'][1])) {
163 $record['jumpto'][1] = LESSON_THISPAGE;
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);
172 * Create multichoice question pages.
173 * @param object $lesson
174 * @param array $record
177 public function create_question_multichoice($lesson, $record = array()) {
179 require_once($CFG->dirroot.'/mod/lesson/locallib.php');
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.
189 if (!isset($record['contents_editor'])) {
190 $record['contents_editor'] = array(
191 'text' => 'Pick the correct answer '.$this->pagecount,
192 'format' => FORMAT_HTML,
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
204 if (!isset($record['jumpto'][0])) {
205 $record['jumpto'][0] = LESSON_NEXTPAGE;
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
215 if (!isset($record['jumpto'][1])) {
216 $record['jumpto'][1] = LESSON_THISPAGE;
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);
225 * Create essay question pages.
226 * @param object $lesson
227 * @param array $record
230 public function create_question_essay($lesson, $record = array()) {
232 require_once($CFG->dirroot.'/mod/lesson/locallib.php');
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.
242 if (!isset($record['contents_editor'])) {
243 $record['contents_editor'] = array(
244 'text' => 'Write an Essay '.$this->pagecount,
245 'format' => FORMAT_HTML,
250 // Essays have an answer of NULL.
251 if (!isset($record['answer_editor'][0])) {
252 $record['answer_editor'][0] = array(
254 'format' => FORMAT_MOODLE
257 if (!isset($record['jumpto'][0])) {
258 $record['jumpto'][0] = LESSON_NEXTPAGE;
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);
267 * Create matching question pages.
268 * @param object $lesson
269 * @param array $record
272 public function create_question_matching($lesson, $record = array()) {
274 require_once($CFG->dirroot.'/mod/lesson/locallib.php');
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.
284 if (!isset($record['contents_editor'])) {
285 $record['contents_editor'] = array(
286 'text' => 'Match the values '.$this->pagecount,
287 'format' => FORMAT_HTML,
291 // Feedback for correct result.
292 if (!isset($record['answer_editor'][0])) {
293 $record['answer_editor'][0] = array(
295 'format' => FORMAT_HTML
298 // Feedback for wrong result.
299 if (!isset($record['answer_editor'][1])) {
300 $record['answer_editor'][1] = array(
302 'format' => FORMAT_HTML
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
312 // First response value.
313 if (!isset($record['response_editor'][2])) {
314 $record['response_editor'][2] = 'Match answer 1';
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
323 // Second Matching answer.
324 if (!isset($record['response_editor'][3])) {
325 $record['response_editor'][3] = 'Match answer 2';
329 if (!isset($record['jumpto'][0])) {
330 $record['jumpto'][0] = LESSON_NEXTPAGE;
332 if (!isset($record['jumpto'][1])) {
333 $record['jumpto'][1] = LESSON_THISPAGE;
336 // Mark the correct values.
337 if (!isset($record['score'][0])) {
338 $record['score'][0] = 1;
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);
346 * Create shortanswer question pages.
347 * @param object $lesson
348 * @param array $record
351 public function create_question_shortanswer($lesson, $record = array()) {
353 require_once($CFG->dirroot.'/mod/lesson/locallib.php');
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.
363 if (!isset($record['contents_editor'])) {
364 $record['contents_editor'] = array(
365 'text' => 'Fill in the blank '.$this->pagecount,
366 'format' => FORMAT_HTML,
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
378 if (!isset($record['jumpto'][0])) {
379 $record['jumpto'][0] = LESSON_NEXTPAGE;
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);
388 * Create shortanswer question pages.
389 * @param object $lesson
390 * @param array $record
393 public function create_question_numeric($lesson, $record = array()) {
395 require_once($CFG->dirroot.'/mod/lesson/locallib.php');
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.
405 if (!isset($record['contents_editor'])) {
406 $record['contents_editor'] = array(
407 'text' => 'Numerical question '.$this->pagecount,
408 'format' => FORMAT_HTML,
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
420 if (!isset($record['jumpto'][0])) {
421 $record['jumpto'][0] = LESSON_NEXTPAGE;
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);