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 |
namespace mod_lesson;
|
|
|
18 |
|
|
|
19 |
/**
|
|
|
20 |
* Genarator tests class for mod_lesson.
|
|
|
21 |
*
|
|
|
22 |
* @package mod_lesson
|
|
|
23 |
* @category test
|
|
|
24 |
* @copyright 2013 Marina Glancy
|
|
|
25 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
26 |
* @covers \mod_lesson_generator
|
|
|
27 |
*/
|
|
|
28 |
class generator_test extends \advanced_testcase {
|
|
|
29 |
|
11 |
efrain |
30 |
public function test_create_instance(): void {
|
1 |
efrain |
31 |
global $DB;
|
|
|
32 |
$this->resetAfterTest();
|
|
|
33 |
$this->setAdminUser();
|
|
|
34 |
|
|
|
35 |
$course = $this->getDataGenerator()->create_course();
|
|
|
36 |
|
|
|
37 |
$this->assertFalse($DB->record_exists('lesson', array('course' => $course->id)));
|
|
|
38 |
$lesson = $this->getDataGenerator()->create_module('lesson', array('course' => $course));
|
|
|
39 |
$records = $DB->get_records('lesson', array('course' => $course->id), 'id');
|
|
|
40 |
$this->assertEquals(1, count($records));
|
|
|
41 |
$this->assertTrue(array_key_exists($lesson->id, $records));
|
|
|
42 |
|
|
|
43 |
$params = array('course' => $course->id, 'name' => 'Another lesson');
|
|
|
44 |
$lesson = $this->getDataGenerator()->create_module('lesson', $params);
|
|
|
45 |
$records = $DB->get_records('lesson', array('course' => $course->id), 'id');
|
|
|
46 |
$this->assertEquals(2, count($records));
|
|
|
47 |
$this->assertEquals('Another lesson', $records[$lesson->id]->name);
|
|
|
48 |
}
|
|
|
49 |
|
11 |
efrain |
50 |
public function test_create_content(): void {
|
1 |
efrain |
51 |
global $DB;
|
|
|
52 |
$this->resetAfterTest();
|
|
|
53 |
$this->setAdminUser();
|
|
|
54 |
|
|
|
55 |
$course = $this->getDataGenerator()->create_course();
|
|
|
56 |
$lesson = $this->getDataGenerator()->create_module('lesson', array('course' => $course));
|
|
|
57 |
$lessongenerator = $this->getDataGenerator()->get_plugin_generator('mod_lesson');
|
|
|
58 |
|
|
|
59 |
$page1 = $lessongenerator->create_content($lesson);
|
|
|
60 |
$page2 = $lessongenerator->create_content($lesson, array('title' => 'Custom title'));
|
|
|
61 |
$records = $DB->get_records('lesson_pages', array('lessonid' => $lesson->id), 'id');
|
|
|
62 |
$this->assertEquals(2, count($records));
|
|
|
63 |
$this->assertEquals($page1->id, $records[$page1->id]->id);
|
|
|
64 |
$this->assertEquals($page2->id, $records[$page2->id]->id);
|
|
|
65 |
$this->assertEquals('Custom title', $records[$page2->id]->title);
|
|
|
66 |
}
|
|
|
67 |
|
|
|
68 |
/**
|
|
|
69 |
* This tests the true/false question generator.
|
|
|
70 |
*/
|
11 |
efrain |
71 |
public function test_create_question_truefalse(): void {
|
1 |
efrain |
72 |
global $DB;
|
|
|
73 |
$this->resetAfterTest();
|
|
|
74 |
$this->setAdminUser();
|
|
|
75 |
|
|
|
76 |
$course = $this->getDataGenerator()->create_course();
|
|
|
77 |
$lesson = $this->getDataGenerator()->create_module('lesson', array('course' => $course));
|
|
|
78 |
$lessongenerator = $this->getDataGenerator()->get_plugin_generator('mod_lesson');
|
|
|
79 |
|
|
|
80 |
$page1 = $lessongenerator->create_question_truefalse($lesson);
|
|
|
81 |
$page2 = $lessongenerator->create_question_truefalse($lesson, array('title' => 'Custom title'));
|
|
|
82 |
$records = $DB->get_records('lesson_pages', array('lessonid' => $lesson->id), 'id');
|
|
|
83 |
$p1answers = $DB->get_records('lesson_answers', array('lessonid' => $lesson->id, 'pageid' => $page1->id), 'id');
|
|
|
84 |
$p2answers = $DB->get_records('lesson_answers', array('lessonid' => $lesson->id, 'pageid' => $page2->id), 'id');
|
|
|
85 |
$this->assertCount(2, $records);
|
|
|
86 |
$this->assertCount(2, $p1answers); // True/false only supports 2 answer records.
|
|
|
87 |
$this->assertCount(2, $p2answers);
|
|
|
88 |
$this->assertEquals($page1->id, $records[$page1->id]->id);
|
|
|
89 |
$this->assertEquals($page2->id, $records[$page2->id]->id);
|
|
|
90 |
$this->assertEquals($page2->title, $records[$page2->id]->title);
|
|
|
91 |
}
|
|
|
92 |
|
|
|
93 |
/**
|
|
|
94 |
* This tests the multichoice question generator.
|
|
|
95 |
*/
|
11 |
efrain |
96 |
public function test_create_question_multichoice(): void {
|
1 |
efrain |
97 |
global $DB;
|
|
|
98 |
$this->resetAfterTest();
|
|
|
99 |
$this->setAdminUser();
|
|
|
100 |
|
|
|
101 |
$course = $this->getDataGenerator()->create_course();
|
|
|
102 |
$lesson = $this->getDataGenerator()->create_module('lesson', array('course' => $course));
|
|
|
103 |
$lessongenerator = $this->getDataGenerator()->get_plugin_generator('mod_lesson');
|
|
|
104 |
|
|
|
105 |
$page1 = $lessongenerator->create_question_multichoice($lesson);
|
|
|
106 |
$page2 = $lessongenerator->create_question_multichoice($lesson, array('title' => 'Custom title'));
|
|
|
107 |
$records = $DB->get_records('lesson_pages', array('lessonid' => $lesson->id), 'id');
|
|
|
108 |
$p1answers = $DB->get_records('lesson_answers', array('lessonid' => $lesson->id, 'pageid' => $page1->id), 'id');
|
|
|
109 |
$p2answers = $DB->get_records('lesson_answers', array('lessonid' => $lesson->id, 'pageid' => $page2->id), 'id');
|
|
|
110 |
$this->assertCount(2, $records);
|
|
|
111 |
$this->assertCount(2, $p1answers); // Multichoice requires at least 2 records.
|
|
|
112 |
$this->assertCount(2, $p2answers);
|
|
|
113 |
$this->assertEquals($page1->id, $records[$page1->id]->id);
|
|
|
114 |
$this->assertEquals($page2->id, $records[$page2->id]->id);
|
|
|
115 |
$this->assertEquals($page2->title, $records[$page2->id]->title);
|
|
|
116 |
}
|
|
|
117 |
|
|
|
118 |
/**
|
|
|
119 |
* This tests the essay question generator.
|
|
|
120 |
*/
|
11 |
efrain |
121 |
public function test_create_question_essay(): void {
|
1 |
efrain |
122 |
global $DB;
|
|
|
123 |
$this->resetAfterTest();
|
|
|
124 |
$this->setAdminUser();
|
|
|
125 |
|
|
|
126 |
$course = $this->getDataGenerator()->create_course();
|
|
|
127 |
$lesson = $this->getDataGenerator()->create_module('lesson', array('course' => $course));
|
|
|
128 |
$lessongenerator = $this->getDataGenerator()->get_plugin_generator('mod_lesson');
|
|
|
129 |
|
|
|
130 |
$page1 = $lessongenerator->create_question_essay($lesson);
|
|
|
131 |
$page2 = $lessongenerator->create_question_essay($lesson, array('title' => 'Custom title'));
|
|
|
132 |
$records = $DB->get_records('lesson_pages', array('lessonid' => $lesson->id), 'id');
|
|
|
133 |
$p1answers = $DB->get_records('lesson_answers', array('lessonid' => $lesson->id, 'pageid' => $page1->id), 'id');
|
|
|
134 |
$p2answers = $DB->get_records('lesson_answers', array('lessonid' => $lesson->id, 'pageid' => $page2->id), 'id');
|
|
|
135 |
$this->assertCount(2, $records);
|
|
|
136 |
$this->assertCount(1, $p1answers); // Essay creates a single (empty) answer record.
|
|
|
137 |
$this->assertCount(1, $p2answers);
|
|
|
138 |
$this->assertEquals($page1->id, $records[$page1->id]->id);
|
|
|
139 |
$this->assertEquals($page2->id, $records[$page2->id]->id);
|
|
|
140 |
$this->assertEquals($page2->title, $records[$page2->id]->title);
|
|
|
141 |
}
|
|
|
142 |
|
|
|
143 |
/**
|
|
|
144 |
* This tests the matching question generator.
|
|
|
145 |
*/
|
11 |
efrain |
146 |
public function test_create_question_matching(): void {
|
1 |
efrain |
147 |
global $DB;
|
|
|
148 |
$this->resetAfterTest();
|
|
|
149 |
$this->setAdminUser();
|
|
|
150 |
|
|
|
151 |
$course = $this->getDataGenerator()->create_course();
|
|
|
152 |
$lesson = $this->getDataGenerator()->create_module('lesson', array('course' => $course));
|
|
|
153 |
$lessongenerator = $this->getDataGenerator()->get_plugin_generator('mod_lesson');
|
|
|
154 |
|
|
|
155 |
$page1 = $lessongenerator->create_question_matching($lesson);
|
|
|
156 |
$page2 = $lessongenerator->create_question_matching($lesson, array('title' => 'Custom title'));
|
|
|
157 |
$records = $DB->get_records('lesson_pages', array('lessonid' => $lesson->id), 'id');
|
|
|
158 |
$p1answers = $DB->get_records('lesson_answers', array('lessonid' => $lesson->id, 'pageid' => $page1->id), 'id');
|
|
|
159 |
$p2answers = $DB->get_records('lesson_answers', array('lessonid' => $lesson->id, 'pageid' => $page2->id), 'id');
|
|
|
160 |
$this->assertCount(2, $records);
|
|
|
161 |
$this->assertCount(4, $p1answers); // Matching creates two extra records plus 1 for each answer value.
|
|
|
162 |
$this->assertCount(4, $p2answers);
|
|
|
163 |
$this->assertEquals($page1->id, $records[$page1->id]->id);
|
|
|
164 |
$this->assertEquals($page2->id, $records[$page2->id]->id);
|
|
|
165 |
$this->assertEquals($page2->title, $records[$page2->id]->title);
|
|
|
166 |
}
|
|
|
167 |
|
|
|
168 |
/**
|
|
|
169 |
* This tests the numeric question generator.
|
|
|
170 |
*/
|
11 |
efrain |
171 |
public function test_create_question_numeric(): void {
|
1 |
efrain |
172 |
global $DB;
|
|
|
173 |
$this->resetAfterTest();
|
|
|
174 |
$this->setAdminUser();
|
|
|
175 |
|
|
|
176 |
$course = $this->getDataGenerator()->create_course();
|
|
|
177 |
$lesson = $this->getDataGenerator()->create_module('lesson', array('course' => $course));
|
|
|
178 |
$lessongenerator = $this->getDataGenerator()->get_plugin_generator('mod_lesson');
|
|
|
179 |
|
|
|
180 |
$page1 = $lessongenerator->create_question_numeric($lesson);
|
|
|
181 |
$page2 = $lessongenerator->create_question_numeric($lesson, array('title' => 'Custom title'));
|
|
|
182 |
$records = $DB->get_records('lesson_pages', array('lessonid' => $lesson->id), 'id');
|
|
|
183 |
$p1answers = $DB->get_records('lesson_answers', array('lessonid' => $lesson->id, 'pageid' => $page1->id), 'id');
|
|
|
184 |
$p2answers = $DB->get_records('lesson_answers', array('lessonid' => $lesson->id, 'pageid' => $page2->id), 'id');
|
|
|
185 |
$this->assertCount(2, $records);
|
|
|
186 |
$this->assertCount(1, $p1answers); // Numeric only requires 1 answer.
|
|
|
187 |
$this->assertCount(1, $p2answers);
|
|
|
188 |
$this->assertEquals($page1->id, $records[$page1->id]->id);
|
|
|
189 |
$this->assertEquals($page2->id, $records[$page2->id]->id);
|
|
|
190 |
$this->assertEquals($page2->title, $records[$page2->id]->title);
|
|
|
191 |
}
|
|
|
192 |
|
|
|
193 |
/**
|
|
|
194 |
* This tests the shortanswer question generator.
|
|
|
195 |
*/
|
11 |
efrain |
196 |
public function test_create_question_shortanswer(): void {
|
1 |
efrain |
197 |
global $DB;
|
|
|
198 |
$this->resetAfterTest();
|
|
|
199 |
$this->setAdminUser();
|
|
|
200 |
|
|
|
201 |
$course = $this->getDataGenerator()->create_course();
|
|
|
202 |
$lesson = $this->getDataGenerator()->create_module('lesson', array('course' => $course));
|
|
|
203 |
$lessongenerator = $this->getDataGenerator()->get_plugin_generator('mod_lesson');
|
|
|
204 |
|
|
|
205 |
$page1 = $lessongenerator->create_question_shortanswer($lesson);
|
|
|
206 |
$page2 = $lessongenerator->create_question_shortanswer($lesson, array('title' => 'Custom title'));
|
|
|
207 |
$records = $DB->get_records('lesson_pages', array('lessonid' => $lesson->id), 'id');
|
|
|
208 |
$p1answers = $DB->get_records('lesson_answers', array('lessonid' => $lesson->id, 'pageid' => $page1->id), 'id');
|
|
|
209 |
$p2answers = $DB->get_records('lesson_answers', array('lessonid' => $lesson->id, 'pageid' => $page2->id), 'id');
|
|
|
210 |
$this->assertCount(2, $records);
|
|
|
211 |
$this->assertCount(1, $p1answers); // Shortanswer only requires 1 answer.
|
|
|
212 |
$this->assertCount(1, $p2answers);
|
|
|
213 |
$this->assertEquals($page1->id, $records[$page1->id]->id);
|
|
|
214 |
$this->assertEquals($page2->id, $records[$page2->id]->id);
|
|
|
215 |
$this->assertEquals($page2->title, $records[$page2->id]->title);
|
|
|
216 |
}
|
|
|
217 |
|
|
|
218 |
/**
|
|
|
219 |
* This tests the generators for cluster, endofcluster and endofbranch pages.
|
|
|
220 |
*
|
|
|
221 |
* @covers ::create_cluster
|
|
|
222 |
* @covers ::create_endofcluster
|
|
|
223 |
* @covers ::create_endofbranch
|
|
|
224 |
* @dataProvider create_cluster_pages_provider
|
|
|
225 |
*
|
|
|
226 |
* @param string $type Type of page to test: LESSON_PAGE_CLUSTER, LESSON_PAGE_ENDOFCLUSTER or LESSON_PAGE_ENDOFBRANCH.
|
|
|
227 |
*/
|
|
|
228 |
public function test_create_cluster_pages(string $type): void {
|
|
|
229 |
global $CFG, $DB;
|
|
|
230 |
require_once($CFG->dirroot . '/mod/lesson/locallib.php');
|
|
|
231 |
require_once($CFG->dirroot . '/mod/lesson/pagetypes/cluster.php');
|
|
|
232 |
$this->resetAfterTest();
|
|
|
233 |
$this->setAdminUser();
|
|
|
234 |
|
|
|
235 |
$course = $this->getDataGenerator()->create_course();
|
|
|
236 |
$lesson = $this->getDataGenerator()->create_module('lesson', ['course' => $course]);
|
|
|
237 |
$lessongenerator = $this->getDataGenerator()->get_plugin_generator('mod_lesson');
|
|
|
238 |
|
|
|
239 |
$page2data = [
|
|
|
240 |
'title' => 'Custom title',
|
|
|
241 |
'contents_editor' => [
|
|
|
242 |
'text' => 'Custom content',
|
|
|
243 |
'format' => FORMAT_MOODLE,
|
|
|
244 |
'itemid' => 0,
|
|
|
245 |
],
|
|
|
246 |
'jumpto' => [LESSON_EOL],
|
|
|
247 |
];
|
|
|
248 |
|
|
|
249 |
switch ($type) {
|
|
|
250 |
case LESSON_PAGE_CLUSTER:
|
|
|
251 |
$page1 = $lessongenerator->create_cluster($lesson);
|
|
|
252 |
$page2 = $lessongenerator->create_cluster($lesson, $page2data);
|
|
|
253 |
break;
|
|
|
254 |
case LESSON_PAGE_ENDOFCLUSTER:
|
|
|
255 |
$page1 = $lessongenerator->create_endofcluster($lesson);
|
|
|
256 |
$page2 = $lessongenerator->create_endofcluster($lesson, $page2data);
|
|
|
257 |
break;
|
|
|
258 |
case LESSON_PAGE_ENDOFBRANCH:
|
|
|
259 |
$page1 = $lessongenerator->create_endofbranch($lesson);
|
|
|
260 |
$page2 = $lessongenerator->create_endofbranch($lesson, $page2data);
|
|
|
261 |
break;
|
|
|
262 |
default:
|
|
|
263 |
throw new coding_exception('Cluster page type not valid: ' . $type);
|
|
|
264 |
}
|
|
|
265 |
|
|
|
266 |
$records = $DB->get_records('lesson_pages', ['lessonid' => $lesson->id], 'id');
|
|
|
267 |
$p1answers = $DB->get_records('lesson_answers', ['lessonid' => $lesson->id, 'pageid' => $page1->id], 'id');
|
|
|
268 |
$p2answers = $DB->get_records('lesson_answers', ['lessonid' => $lesson->id, 'pageid' => $page2->id], 'id');
|
|
|
269 |
|
|
|
270 |
$this->assertCount(2, $records);
|
|
|
271 |
$this->assertEquals($page1->id, $records[$page1->id]->id);
|
|
|
272 |
$this->assertEquals($type, $records[$page1->id]->qtype);
|
|
|
273 |
$this->assertEquals($page2->id, $records[$page2->id]->id);
|
|
|
274 |
$this->assertEquals($type, $records[$page2->id]->qtype);
|
|
|
275 |
$this->assertEquals($page2->title, $records[$page2->id]->title);
|
|
|
276 |
$this->assertEquals($page2data['contents_editor']['text'], $records[$page2->id]->contents);
|
|
|
277 |
$this->assertCount(1, $p1answers);
|
|
|
278 |
$this->assertCount(1, $p2answers);
|
|
|
279 |
$this->assertEquals(LESSON_THISPAGE, array_pop($p1answers)->jumpto);
|
|
|
280 |
$this->assertEquals(LESSON_EOL, array_pop($p2answers)->jumpto);
|
|
|
281 |
}
|
|
|
282 |
|
|
|
283 |
/**
|
|
|
284 |
* Data provider for test_create_cluster_pages().
|
|
|
285 |
*
|
|
|
286 |
* @return array
|
|
|
287 |
*/
|
|
|
288 |
public static function create_cluster_pages_provider(): array {
|
|
|
289 |
// Using the page constants here throws an error: Undefined constant "mod_lesson\LESSON_PAGE_CLUSTER".
|
|
|
290 |
return [
|
|
|
291 |
'Cluster' => [
|
|
|
292 |
'type' => '30',
|
|
|
293 |
],
|
|
|
294 |
'End of cluster' => [
|
|
|
295 |
'type' => '31',
|
|
|
296 |
],
|
|
|
297 |
'End of branch' => [
|
|
|
298 |
'type' => '21',
|
|
|
299 |
],
|
|
|
300 |
];
|
|
|
301 |
}
|
|
|
302 |
|
|
|
303 |
/**
|
|
|
304 |
* Test create some pages and their answers.
|
|
|
305 |
*
|
|
|
306 |
* @covers ::create_page
|
|
|
307 |
* @covers ::create_answer
|
|
|
308 |
* @covers ::finish_generate_answer
|
|
|
309 |
*/
|
|
|
310 |
public function test_create_page_and_answers(): void {
|
|
|
311 |
global $DB;
|
|
|
312 |
$this->resetAfterTest();
|
|
|
313 |
$this->setAdminUser();
|
|
|
314 |
|
|
|
315 |
$course = $this->getDataGenerator()->create_course();
|
|
|
316 |
$lesson = $this->getDataGenerator()->create_module('lesson', ['course' => $course]);
|
|
|
317 |
$lessongenerator = $this->getDataGenerator()->get_plugin_generator('mod_lesson');
|
|
|
318 |
|
|
|
319 |
// Define the pages. Only a couple pages will be created since each page type has their own unit tests.
|
|
|
320 |
$contentpage = [
|
|
|
321 |
'title' => 'First page name',
|
|
|
322 |
'content' => 'First page contents',
|
|
|
323 |
'qtype' => 'content',
|
|
|
324 |
'lessonid' => $lesson->id,
|
|
|
325 |
];
|
|
|
326 |
$multichoicepage = [
|
|
|
327 |
'title' => 'Multichoice question',
|
|
|
328 |
'content' => 'What animal is an amphibian?',
|
|
|
329 |
'qtype' => 'multichoice',
|
|
|
330 |
'lessonid' => $lesson->id,
|
|
|
331 |
];
|
|
|
332 |
|
|
|
333 |
$lessongenerator->create_page($contentpage);
|
|
|
334 |
$lessongenerator->create_page($multichoicepage);
|
|
|
335 |
|
|
|
336 |
// Check that pages haven't been generated yet because no answers were added.
|
|
|
337 |
$pages = $DB->get_records('lesson_pages', ['lessonid' => $lesson->id], 'id');
|
|
|
338 |
$this->assertEquals(0, count($pages));
|
|
|
339 |
|
|
|
340 |
// Now add answers to the pages.
|
|
|
341 |
$contentpagecontinueanswer = [
|
|
|
342 |
'page' => $contentpage['title'],
|
|
|
343 |
'answer' => 'Continue',
|
|
|
344 |
'jumpto' => 'Next page',
|
|
|
345 |
'score' => 1,
|
|
|
346 |
];
|
|
|
347 |
$contentpagestayanswer = [
|
|
|
348 |
'page' => $contentpage['title'],
|
|
|
349 |
'answer' => 'Stay',
|
|
|
350 |
'jumpto' => 'This page',
|
|
|
351 |
'score' => 0,
|
|
|
352 |
];
|
|
|
353 |
$multichoicepagefroganswer = [
|
|
|
354 |
'page' => $multichoicepage['title'],
|
|
|
355 |
'answer' => 'Frog',
|
|
|
356 |
'response' => 'Correct answer',
|
|
|
357 |
'jumpto' => 'Next page',
|
|
|
358 |
'score' => 1,
|
|
|
359 |
];
|
|
|
360 |
$multichoicepagecatanswer = [
|
|
|
361 |
'page' => $multichoicepage['title'],
|
|
|
362 |
'answer' => 'Cat',
|
|
|
363 |
'response' => 'Incorrect answer',
|
|
|
364 |
'jumpto' => 'This page',
|
|
|
365 |
'score' => 0,
|
|
|
366 |
];
|
|
|
367 |
$multichoicepagedoganswer = [
|
|
|
368 |
'page' => $multichoicepage['title'],
|
|
|
369 |
'answer' => 'Dog',
|
|
|
370 |
'response' => 'Incorrect answer',
|
|
|
371 |
'jumpto' => 'This page',
|
|
|
372 |
'score' => 0,
|
|
|
373 |
];
|
|
|
374 |
|
|
|
375 |
$lessongenerator->create_answer($contentpagecontinueanswer);
|
|
|
376 |
$lessongenerator->create_answer($contentpagestayanswer);
|
|
|
377 |
$lessongenerator->create_answer($multichoicepagefroganswer);
|
|
|
378 |
$lessongenerator->create_answer($multichoicepagecatanswer);
|
|
|
379 |
$lessongenerator->create_answer($multichoicepagedoganswer);
|
|
|
380 |
|
|
|
381 |
// Check that pages and answers haven't been generated yet because maybe not all answers have been added yet.
|
|
|
382 |
$pages = $DB->get_records('lesson_pages', ['lessonid' => $lesson->id], 'id');
|
|
|
383 |
$answers = $DB->get_records('lesson_answers', ['lessonid' => $lesson->id], 'id');
|
|
|
384 |
$this->assertEquals(0, count($pages));
|
|
|
385 |
$this->assertEquals(0, count($answers));
|
|
|
386 |
|
|
|
387 |
// Notify that all answers have been added, so pages can be created.
|
|
|
388 |
$lessongenerator->finish_generate_answer();
|
|
|
389 |
|
|
|
390 |
// Check that pages and answers have been created.
|
|
|
391 |
$pages = $DB->get_records('lesson_pages', ['lessonid' => $lesson->id], $DB->sql_order_by_text('title') . ' DESC');
|
|
|
392 |
$this->assertEquals(2, count($pages));
|
|
|
393 |
|
|
|
394 |
$contentpagedb = array_pop($pages);
|
|
|
395 |
$multichoicepagedb = array_pop($pages);
|
|
|
396 |
$this->assertEquals($contentpage['title'], $contentpagedb->title);
|
|
|
397 |
$this->assertEquals($contentpage['content'], $contentpagedb->contents);
|
|
|
398 |
$this->assertEquals(LESSON_PAGE_BRANCHTABLE, $contentpagedb->qtype);
|
|
|
399 |
$this->assertEquals($multichoicepage['title'], $multichoicepagedb->title);
|
|
|
400 |
$this->assertEquals($multichoicepage['content'], $multichoicepagedb->contents);
|
|
|
401 |
$this->assertEquals(LESSON_PAGE_MULTICHOICE, $multichoicepagedb->qtype);
|
|
|
402 |
|
|
|
403 |
$answers = $DB->get_records('lesson_answers', ['lessonid' => $lesson->id], $DB->sql_order_by_text('answer') . ' DESC');
|
|
|
404 |
$this->assertEquals(5, count($answers));
|
|
|
405 |
|
|
|
406 |
$multichoicepagecatanswerdb = array_pop($answers);
|
|
|
407 |
$contentpagecontinueanswerdb = array_pop($answers);
|
|
|
408 |
$multichoicepagedoganswerdb = array_pop($answers);
|
|
|
409 |
$multichoicepagefroganswerdb = array_pop($answers);
|
|
|
410 |
$contentpagestayanswerdb = array_pop($answers);
|
|
|
411 |
$this->assertEquals($contentpagedb->id, $contentpagecontinueanswerdb->pageid);
|
|
|
412 |
$this->assertEquals($contentpagecontinueanswer['answer'], $contentpagecontinueanswerdb->answer);
|
|
|
413 |
$this->assertEquals(LESSON_NEXTPAGE, $contentpagecontinueanswerdb->jumpto);
|
|
|
414 |
$this->assertEquals($contentpagecontinueanswer['score'], $contentpagecontinueanswerdb->score);
|
|
|
415 |
$this->assertEquals($contentpagedb->id, $contentpagestayanswerdb->pageid);
|
|
|
416 |
$this->assertEquals($contentpagestayanswer['answer'], $contentpagestayanswerdb->answer);
|
|
|
417 |
$this->assertEquals(LESSON_THISPAGE, $contentpagestayanswerdb->jumpto);
|
|
|
418 |
$this->assertEquals($contentpagestayanswer['score'], $contentpagestayanswerdb->score);
|
|
|
419 |
$this->assertEquals($multichoicepagedb->id, $multichoicepagefroganswerdb->pageid);
|
|
|
420 |
$this->assertEquals($multichoicepagefroganswer['answer'], $multichoicepagefroganswerdb->answer);
|
|
|
421 |
$this->assertEquals($multichoicepagefroganswer['response'], $multichoicepagefroganswerdb->response);
|
|
|
422 |
$this->assertEquals(LESSON_NEXTPAGE, $multichoicepagefroganswerdb->jumpto);
|
|
|
423 |
$this->assertEquals($multichoicepagefroganswer['score'], $multichoicepagefroganswerdb->score);
|
|
|
424 |
$this->assertEquals($multichoicepagedb->id, $multichoicepagedoganswerdb->pageid);
|
|
|
425 |
$this->assertEquals($multichoicepagedoganswer['answer'], $multichoicepagedoganswerdb->answer);
|
|
|
426 |
$this->assertEquals($multichoicepagedoganswer['response'], $multichoicepagedoganswerdb->response);
|
|
|
427 |
$this->assertEquals(LESSON_THISPAGE, $multichoicepagedoganswerdb->jumpto);
|
|
|
428 |
$this->assertEquals($multichoicepagedoganswer['score'], $multichoicepagedoganswerdb->score);
|
|
|
429 |
$this->assertEquals($multichoicepagedb->id, $multichoicepagecatanswerdb->pageid);
|
|
|
430 |
$this->assertEquals($multichoicepagecatanswer['answer'], $multichoicepagecatanswerdb->answer);
|
|
|
431 |
$this->assertEquals($multichoicepagecatanswer['response'], $multichoicepagecatanswerdb->response);
|
|
|
432 |
$this->assertEquals(LESSON_THISPAGE, $multichoicepagecatanswerdb->jumpto);
|
|
|
433 |
$this->assertEquals($multichoicepagecatanswer['score'], $multichoicepagecatanswerdb->score);
|
|
|
434 |
}
|
|
|
435 |
|
|
|
436 |
/**
|
|
|
437 |
* Test creating pages defining the previous pages.
|
|
|
438 |
*
|
|
|
439 |
* @covers ::create_page
|
|
|
440 |
*/
|
|
|
441 |
public function test_create_page_with_previouspage(): void {
|
|
|
442 |
global $DB;
|
|
|
443 |
$this->resetAfterTest();
|
|
|
444 |
$this->setAdminUser();
|
|
|
445 |
|
|
|
446 |
$course = $this->getDataGenerator()->create_course();
|
|
|
447 |
$lesson = $this->getDataGenerator()->create_module('lesson', ['course' => $course]);
|
|
|
448 |
$lessongenerator = $this->getDataGenerator()->get_plugin_generator('mod_lesson');
|
|
|
449 |
|
|
|
450 |
$firstpage = [
|
|
|
451 |
'title' => 'First page name',
|
|
|
452 |
'content' => 'First page contents',
|
|
|
453 |
'qtype' => 'content',
|
|
|
454 |
'lessonid' => $lesson->id,
|
|
|
455 |
'previouspage' => 0, // No previous page, this will be the first page.
|
|
|
456 |
];
|
|
|
457 |
$secondpage = [
|
|
|
458 |
'title' => 'Second page name',
|
|
|
459 |
'content' => 'Second page contents',
|
|
|
460 |
'qtype' => 'content',
|
|
|
461 |
'lessonid' => $lesson->id,
|
|
|
462 |
'previouspage' => 'First page name',
|
|
|
463 |
];
|
|
|
464 |
$thirdpage = [
|
|
|
465 |
'title' => 'Third page name',
|
|
|
466 |
'content' => 'Third page contents',
|
|
|
467 |
'qtype' => 'content',
|
|
|
468 |
'lessonid' => $lesson->id,
|
|
|
469 |
'previouspage' => 'Second page name',
|
|
|
470 |
];
|
|
|
471 |
|
|
|
472 |
// Create the third page first to check that the added order is not important, the order will still be calculated right.
|
|
|
473 |
$lessongenerator->create_page($thirdpage);
|
|
|
474 |
$lessongenerator->create_page($firstpage);
|
|
|
475 |
$lessongenerator->create_page($secondpage);
|
|
|
476 |
|
|
|
477 |
// Don't define any answers, the default answers will be added.
|
|
|
478 |
$lessongenerator->finish_generate_answer();
|
|
|
479 |
|
|
|
480 |
$pages = $DB->get_records('lesson_pages', ['lessonid' => $lesson->id], $DB->sql_order_by_text('title') . ' DESC');
|
|
|
481 |
$this->assertEquals(3, count($pages));
|
|
|
482 |
|
|
|
483 |
$firstpagedb = array_pop($pages);
|
|
|
484 |
$secondpagedb = array_pop($pages);
|
|
|
485 |
$thirdpagedb = array_pop($pages);
|
|
|
486 |
$this->assertEquals($firstpage['title'], $firstpagedb->title);
|
|
|
487 |
$this->assertEquals(0, $firstpagedb->prevpageid);
|
|
|
488 |
$this->assertEquals($secondpagedb->id, $firstpagedb->nextpageid);
|
|
|
489 |
$this->assertEquals($secondpage['title'], $secondpagedb->title);
|
|
|
490 |
$this->assertEquals($firstpagedb->id, $secondpagedb->prevpageid);
|
|
|
491 |
$this->assertEquals($thirdpagedb->id, $secondpagedb->nextpageid);
|
|
|
492 |
$this->assertEquals($thirdpage['title'], $thirdpagedb->title);
|
|
|
493 |
$this->assertEquals($secondpagedb->id, $thirdpagedb->prevpageid);
|
|
|
494 |
$this->assertEquals(0, $thirdpagedb->nextpageid);
|
|
|
495 |
}
|
|
|
496 |
|
|
|
497 |
/**
|
|
|
498 |
* Test creating a page with a previous page that doesn't exist.
|
|
|
499 |
*
|
|
|
500 |
* @covers ::create_page
|
|
|
501 |
*/
|
|
|
502 |
public function test_create_page_invalid_previouspage(): void {
|
|
|
503 |
$this->resetAfterTest();
|
|
|
504 |
$this->setAdminUser();
|
|
|
505 |
|
|
|
506 |
$course = $this->getDataGenerator()->create_course();
|
|
|
507 |
$lesson = $this->getDataGenerator()->create_module('lesson', ['course' => $course]);
|
|
|
508 |
$lessongenerator = $this->getDataGenerator()->get_plugin_generator('mod_lesson');
|
|
|
509 |
|
|
|
510 |
$this->expectException('coding_exception');
|
|
|
511 |
$lessongenerator->create_page([
|
|
|
512 |
'title' => 'First page name',
|
|
|
513 |
'content' => 'First page contents',
|
|
|
514 |
'qtype' => 'content',
|
|
|
515 |
'lessonid' => $lesson->id,
|
|
|
516 |
'previouspage' => 'Invalid page',
|
|
|
517 |
]);
|
|
|
518 |
$lessongenerator->finish_generate_answer();
|
|
|
519 |
}
|
|
|
520 |
|
|
|
521 |
/**
|
|
|
522 |
* Test that circular dependencies are not allowed in previous pages.
|
|
|
523 |
*
|
|
|
524 |
* @covers ::create_page
|
|
|
525 |
*/
|
|
|
526 |
public function test_create_page_previouspage_circular_dependency(): void {
|
|
|
527 |
$this->resetAfterTest();
|
|
|
528 |
$this->setAdminUser();
|
|
|
529 |
|
|
|
530 |
$course = $this->getDataGenerator()->create_course();
|
|
|
531 |
$lesson = $this->getDataGenerator()->create_module('lesson', ['course' => $course]);
|
|
|
532 |
$lessongenerator = $this->getDataGenerator()->get_plugin_generator('mod_lesson');
|
|
|
533 |
|
|
|
534 |
$this->expectException('coding_exception');
|
|
|
535 |
$lessongenerator->create_page([
|
|
|
536 |
'title' => 'First page name',
|
|
|
537 |
'content' => 'First page contents',
|
|
|
538 |
'qtype' => 'content',
|
|
|
539 |
'lessonid' => $lesson->id,
|
|
|
540 |
'previouspage' => 'Second page name',
|
|
|
541 |
]);
|
|
|
542 |
$lessongenerator->create_page([
|
|
|
543 |
'title' => 'Second page name',
|
|
|
544 |
'content' => 'Second page contents',
|
|
|
545 |
'qtype' => 'content',
|
|
|
546 |
'lessonid' => $lesson->id,
|
|
|
547 |
'previouspage' => 'First page name',
|
|
|
548 |
]);
|
|
|
549 |
$lessongenerator->finish_generate_answer();
|
|
|
550 |
}
|
|
|
551 |
|
|
|
552 |
/**
|
|
|
553 |
* Test creating an answer in a page that doesn't exist.
|
|
|
554 |
*
|
|
|
555 |
* @covers ::create_answer
|
|
|
556 |
*/
|
|
|
557 |
public function test_create_answer_invalid_page(): void {
|
|
|
558 |
$lessongenerator = $this->getDataGenerator()->get_plugin_generator('mod_lesson');
|
|
|
559 |
|
|
|
560 |
$this->expectException('coding_exception');
|
|
|
561 |
$lessongenerator->create_answer([
|
|
|
562 |
'page' => 'Invalid page',
|
|
|
563 |
]);
|
|
|
564 |
}
|
|
|
565 |
|
|
|
566 |
/**
|
|
|
567 |
* Test that all the possible values of jumpto work as expected when creating an answer.
|
|
|
568 |
*
|
|
|
569 |
* @covers ::create_answer
|
|
|
570 |
*/
|
|
|
571 |
public function test_create_answer_jumpto(): void {
|
|
|
572 |
global $DB;
|
|
|
573 |
$this->resetAfterTest();
|
|
|
574 |
$this->setAdminUser();
|
|
|
575 |
|
|
|
576 |
$course = $this->getDataGenerator()->create_course();
|
|
|
577 |
$lesson = $this->getDataGenerator()->create_module('lesson', ['course' => $course]);
|
|
|
578 |
$lessongenerator = $this->getDataGenerator()->get_plugin_generator('mod_lesson');
|
|
|
579 |
|
|
|
580 |
$contentpage = [
|
|
|
581 |
'title' => 'First page name',
|
|
|
582 |
'content' => 'First page contents',
|
|
|
583 |
'qtype' => 'content',
|
|
|
584 |
'lessonid' => $lesson->id,
|
|
|
585 |
];
|
|
|
586 |
$secondcontentpage = [
|
|
|
587 |
'title' => 'Second page name',
|
|
|
588 |
'content' => 'Second page contents',
|
|
|
589 |
'qtype' => 'content',
|
|
|
590 |
'lessonid' => $lesson->id,
|
|
|
591 |
];
|
|
|
592 |
$thirdcontentpage = [
|
|
|
593 |
'title' => 'Third page name',
|
|
|
594 |
'content' => 'Third page contents',
|
|
|
595 |
'qtype' => 'content',
|
|
|
596 |
'lessonid' => $lesson->id,
|
|
|
597 |
];
|
|
|
598 |
$lessongenerator->create_page($contentpage);
|
|
|
599 |
$lessongenerator->create_page($secondcontentpage);
|
|
|
600 |
$lessongenerator->create_page($thirdcontentpage);
|
|
|
601 |
|
|
|
602 |
$lessongenerator->create_answer([
|
|
|
603 |
'page' => $contentpage['title'],
|
|
|
604 |
'answer' => 'A',
|
|
|
605 |
'jumpto' => 'This page',
|
|
|
606 |
]);
|
|
|
607 |
$lessongenerator->create_answer([
|
|
|
608 |
'page' => $contentpage['title'],
|
|
|
609 |
'answer' => 'B',
|
|
|
610 |
'jumpto' => 'Next page',
|
|
|
611 |
]);
|
|
|
612 |
$lessongenerator->create_answer([
|
|
|
613 |
'page' => $contentpage['title'],
|
|
|
614 |
'answer' => 'C',
|
|
|
615 |
'jumpto' => 'Previous page',
|
|
|
616 |
]);
|
|
|
617 |
$lessongenerator->create_answer([
|
|
|
618 |
'page' => $secondcontentpage['title'],
|
|
|
619 |
'answer' => 'D',
|
|
|
620 |
'jumpto' => 'End of lesson',
|
|
|
621 |
]);
|
|
|
622 |
$lessongenerator->create_answer([
|
|
|
623 |
'page' => $secondcontentpage['title'],
|
|
|
624 |
'answer' => 'E',
|
|
|
625 |
'jumpto' => 'Unseen question within a content page',
|
|
|
626 |
]);
|
|
|
627 |
$lessongenerator->create_answer([
|
|
|
628 |
'page' => $secondcontentpage['title'],
|
|
|
629 |
'answer' => 'F',
|
|
|
630 |
'jumpto' => 'Random question within a content page',
|
|
|
631 |
]);
|
|
|
632 |
$lessongenerator->create_answer([
|
|
|
633 |
'page' => $thirdcontentpage['title'],
|
|
|
634 |
'answer' => 'G',
|
|
|
635 |
'jumpto' => 'Random content page',
|
|
|
636 |
]);
|
|
|
637 |
$lessongenerator->create_answer([
|
|
|
638 |
'page' => $thirdcontentpage['title'],
|
|
|
639 |
'answer' => 'H',
|
|
|
640 |
'jumpto' => 'Unseen question within a cluster',
|
|
|
641 |
]);
|
|
|
642 |
$lessongenerator->create_answer([
|
|
|
643 |
'page' => $thirdcontentpage['title'],
|
|
|
644 |
'answer' => 'I',
|
|
|
645 |
'jumpto' => 1234, // A page ID, it doesn't matter that it doesn't exist.
|
|
|
646 |
]);
|
|
|
647 |
$lessongenerator->create_answer([
|
|
|
648 |
'page' => $contentpage['title'],
|
|
|
649 |
'answer' => 'J',
|
|
|
650 |
'jumpto' => 'Third page name',
|
|
|
651 |
]);
|
|
|
652 |
$lessongenerator->create_answer([
|
|
|
653 |
'page' => $thirdcontentpage['title'],
|
|
|
654 |
'answer' => 'K',
|
|
|
655 |
'jumpto' => 'Second page name',
|
|
|
656 |
]);
|
|
|
657 |
|
|
|
658 |
$lessongenerator->finish_generate_answer();
|
|
|
659 |
|
|
|
660 |
$secondcontentpagedb = $DB->get_record('lesson_pages', ['lessonid' => $lesson->id, 'title' => $secondcontentpage['title']]);
|
|
|
661 |
$thirdcontentpagedb = $DB->get_record('lesson_pages', ['lessonid' => $lesson->id, 'title' => $thirdcontentpage['title']]);
|
|
|
662 |
$answers = $DB->get_records('lesson_answers', ['lessonid' => $lesson->id], $DB->sql_order_by_text('answer') . ' DESC');
|
|
|
663 |
$this->assertEquals(11, count($answers));
|
|
|
664 |
|
|
|
665 |
$this->assertEquals(LESSON_THISPAGE, array_pop($answers)->jumpto);
|
|
|
666 |
$this->assertEquals(LESSON_NEXTPAGE, array_pop($answers)->jumpto);
|
|
|
667 |
$this->assertEquals(LESSON_PREVIOUSPAGE, array_pop($answers)->jumpto);
|
|
|
668 |
$this->assertEquals(LESSON_EOL, array_pop($answers)->jumpto);
|
|
|
669 |
$this->assertEquals(LESSON_UNSEENBRANCHPAGE, array_pop($answers)->jumpto);
|
|
|
670 |
$this->assertEquals(LESSON_RANDOMPAGE, array_pop($answers)->jumpto);
|
|
|
671 |
$this->assertEquals(LESSON_RANDOMBRANCH, array_pop($answers)->jumpto);
|
|
|
672 |
$this->assertEquals(LESSON_CLUSTERJUMP, array_pop($answers)->jumpto);
|
|
|
673 |
$this->assertEquals(1234, array_pop($answers)->jumpto);
|
|
|
674 |
$this->assertEquals($thirdcontentpagedb->id, array_pop($answers)->jumpto);
|
|
|
675 |
$this->assertEquals($secondcontentpagedb->id, array_pop($answers)->jumpto);
|
|
|
676 |
}
|
|
|
677 |
|
|
|
678 |
/**
|
|
|
679 |
* Test invalid jumpto when creating answers.
|
|
|
680 |
*
|
|
|
681 |
* @covers ::create_answer
|
|
|
682 |
*/
|
|
|
683 |
public function test_create_answer_invalid_jumpto(): void {
|
|
|
684 |
$this->resetAfterTest();
|
|
|
685 |
$this->setAdminUser();
|
|
|
686 |
|
|
|
687 |
$course = $this->getDataGenerator()->create_course();
|
|
|
688 |
$lesson = $this->getDataGenerator()->create_module('lesson', ['course' => $course]);
|
|
|
689 |
$lessongenerator = $this->getDataGenerator()->get_plugin_generator('mod_lesson');
|
|
|
690 |
|
|
|
691 |
$contentpage = [
|
|
|
692 |
'title' => 'First page name',
|
|
|
693 |
'content' => 'First page contents',
|
|
|
694 |
'qtype' => 'content',
|
|
|
695 |
'lessonid' => $lesson->id,
|
|
|
696 |
];
|
|
|
697 |
$lessongenerator->create_page($contentpage);
|
|
|
698 |
|
|
|
699 |
$lessongenerator->create_answer([
|
|
|
700 |
'page' => $contentpage['title'],
|
|
|
701 |
'answer' => 'Next',
|
|
|
702 |
'jumpto' => 'Invalid page',
|
|
|
703 |
]);
|
|
|
704 |
|
|
|
705 |
$this->expectException('coding_exception');
|
|
|
706 |
$lessongenerator->finish_generate_answer();
|
|
|
707 |
}
|
|
|
708 |
|
|
|
709 |
/**
|
|
|
710 |
* Test that circular dependencies are not allowed when creating answers.
|
|
|
711 |
*
|
|
|
712 |
* @covers ::create_answer
|
|
|
713 |
*/
|
|
|
714 |
public function test_create_answer_jumpto_circular_dependency(): void {
|
|
|
715 |
$this->resetAfterTest();
|
|
|
716 |
$this->setAdminUser();
|
|
|
717 |
|
|
|
718 |
$course = $this->getDataGenerator()->create_course();
|
|
|
719 |
$lesson = $this->getDataGenerator()->create_module('lesson', ['course' => $course]);
|
|
|
720 |
$lessongenerator = $this->getDataGenerator()->get_plugin_generator('mod_lesson');
|
|
|
721 |
|
|
|
722 |
$contentpage = [
|
|
|
723 |
'title' => 'First page name',
|
|
|
724 |
'content' => 'First page contents',
|
|
|
725 |
'qtype' => 'content',
|
|
|
726 |
'lessonid' => $lesson->id,
|
|
|
727 |
];
|
|
|
728 |
$secondcontentpage = [
|
|
|
729 |
'title' => 'Second page name',
|
|
|
730 |
'content' => 'Second page contents',
|
|
|
731 |
'qtype' => 'content',
|
|
|
732 |
'lessonid' => $lesson->id,
|
|
|
733 |
];
|
|
|
734 |
$lessongenerator->create_page($contentpage);
|
|
|
735 |
$lessongenerator->create_page($secondcontentpage);
|
|
|
736 |
|
|
|
737 |
$lessongenerator->create_answer([
|
|
|
738 |
'page' => $contentpage['title'],
|
|
|
739 |
'answer' => 'Next',
|
|
|
740 |
'jumpto' => 'Second page name',
|
|
|
741 |
]);
|
|
|
742 |
$lessongenerator->create_answer([
|
|
|
743 |
'page' => $contentpage['title'],
|
|
|
744 |
'answer' => 'Back',
|
|
|
745 |
'jumpto' => 'First page name',
|
|
|
746 |
]);
|
|
|
747 |
|
|
|
748 |
$this->expectException('coding_exception');
|
|
|
749 |
$lessongenerator->finish_generate_answer();
|
|
|
750 |
}
|
|
|
751 |
|
|
|
752 |
}
|