diff --git a/mod/lesson/tests/generator/behat_mod_lesson_generator.php b/mod/lesson/tests/generator/behat_mod_lesson_generator.php index 4e023113fa0..8909a2b5f7a 100644 --- a/mod/lesson/tests/generator/behat_mod_lesson_generator.php +++ b/mod/lesson/tests/generator/behat_mod_lesson_generator.php @@ -49,6 +49,12 @@ class behat_mod_lesson_generator extends behat_generator_base { 'datagenerator' => 'answer', 'required' => ['page'], ], + 'submissions' => [ + 'singular' => 'submission', + 'datagenerator' => 'submission', + 'required' => ['lesson', 'user'], + 'switchids' => ['lesson' => 'lessonid', 'user' => 'userid'], + ], ]; } diff --git a/mod/lesson/tests/generator/lib.php b/mod/lesson/tests/generator/lib.php index ae2c47624de..7adfd02c78f 100644 --- a/mod/lesson/tests/generator/lib.php +++ b/mod/lesson/tests/generator/lib.php @@ -802,4 +802,66 @@ class mod_lesson_generator extends testing_module_generator { return $jumptolist; } + + /** + * Creates a lesson submission for testing purposes. + * + * @param mixed $record + * @throws \coding_exception + * @return bool|int + */ + public function create_submission($record = null) { + $db = \core\di::get(\moodle_database::class); + + [ + 'lessonid' => $lessonid, + 'userid' => $userid, + 'grade' => $grade, + ] = $record; + + [, $cm] = get_course_and_cm_from_instance($lessonid, 'lesson'); + $lesson = new lesson($cm->get_instance_record()); + + // Check if the lesson exists and retakes are allowed. + if (!$lesson->retake && $db->record_exists('lesson_grades', ['lessonid' => $lessonid, 'userid' => $userid])) { + throw new coding_exception("Grade for user $userid in lesson $lessonid already exists and retakes are not allowed."); + } + + // Get the highest score answer for each page in the lesson. + $sql = "SELECT la.id AS answerid, la.answer, la.pageid FROM {lesson_answers} la + JOIN ( + SELECT lessonid, pageid, MAX(score) AS maxscore FROM {lesson_answers} + GROUP BY lessonid, pageid + ) mla ON mla.lessonid = la.lessonid AND mla.pageid = la.pageid AND mla.maxscore = la.score + WHERE ( + SELECT COUNT(*) + FROM {lesson_answers} lac + WHERE lac.lessonid = la.lessonid + AND lac.pageid = la.pageid + AND lac.score = la.score + ) = 1 + AND la.lessonid = :lessonid"; + + $answers = $db->get_records_sql($sql, ['lessonid' => $lessonid]); + foreach ($answers as $answer) { + // Create an attempt for each answer. + $db->insert_record('lesson_attempts', [ + 'lessonid' => $lessonid, + 'userid' => $userid, + 'pageid' => $answer->pageid, + 'answerid' => $answer->answerid, + 'retry' => 0, + 'useranswer' => $answer->answer, + 'timeseen' => time(), + ]); + } + + return $db->insert_record('lesson_grades', [ + 'lessonid' => $lessonid, + 'userid' => $userid, + 'grade' => $grade, + 'late' => 0, + 'completed' => time(), + ]); + } } diff --git a/mod/lesson/tests/generator_test.php b/mod/lesson/tests/generator_test.php index e7214f9c57c..5cff1f6e911 100644 --- a/mod/lesson/tests/generator_test.php +++ b/mod/lesson/tests/generator_test.php @@ -749,4 +749,56 @@ final class generator_test extends \advanced_testcase { $lessongenerator->finish_generate_answer(); } + /** + * Test create a submission and the related attempts. + * + * @covers ::create_submission + */ + public function test_create_submission(): void { + $db = \core\di::get(\moodle_database::class); + $this->resetAfterTest(); + $this->setAdminUser(); + + $course = $this->getDataGenerator()->create_course(); + $lesson = $this->getDataGenerator()->create_module('lesson', ['course' => $course]); + $student1 = $this->getDataGenerator()->create_and_enrol($course, 'student'); + + /** @var \mod_lesson_generator $lessongenerator */ + $lessongenerator = $this->getDataGenerator()->get_plugin_generator('mod_lesson'); + + // Create pages and answers. + $lessongenerator->create_page([ + 'title' => 'Multichoice question 1', + 'content' => 'What animal is an amphibian?', + 'qtype' => 'multichoice', + 'lessonid' => $lesson->id, + ]); + $lessongenerator->create_answer(['page' => 'Multichoice question 1', 'answer' => 'Frog', 'score' => 1]); + $lessongenerator->create_answer(['page' => 'Multichoice question 1', 'answer' => 'Cat']); + $lessongenerator->create_page([ + 'title' => 'Multichoice question 2', + 'content' => 'What animal is an mammal?', + 'qtype' => 'multichoice', + 'lessonid' => $lesson->id, + ]); + $lessongenerator->create_answer(['page' => 'Multichoice question 2', 'answer' => 'Dog', 'score' => 1]); + $lessongenerator->create_answer(['page' => 'Multichoice question 2', 'answer' => 'spider']); + $lessongenerator->finish_generate_answer(); + + // Create a submission. + $submissionid = $lessongenerator->create_submission([ + 'lessonid' => $lesson->id, + 'userid' => $student1->id, + 'grade' => 100, + ]); + + // Check that the submission was created. + $submission = $db->get_record('lesson_grades', ['lessonid' => $lesson->id, 'userid' => $student1->id]); + $this->assertNotEmpty($submission); + $this->assertEquals($submissionid, $submission->id); + + // Check that the attempts were created. + $attempts = $db->get_records('lesson_attempts', ['lessonid' => $lesson->id, 'userid' => $student1->id]); + $this->assertCount(2, $attempts); + } }