<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.

/**
 * locallib tests.
 *
 * @package    mod_lesson
 * @category   test
 * @copyright  2016 Adrian Greeve <adrian@moodle.com>
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 */
namespace mod_lesson;

use lesson;
use core\context\module as context_module;

defined('MOODLE_INTERNAL') || die();

global $CFG;

require_once($CFG->dirroot.'/mod/lesson/locallib.php');

/**
 * locallib testcase.
 *
 * @package    mod_lesson
 * @category   test
 * @copyright  2016 Adrian Greeve <adrian@moodle.com>
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 */
final class locallib_test extends \advanced_testcase {

    /**
     * Test duplicating a lesson page element.
     */
    public function test_duplicate_page(): void {
        global $DB;

        $this->resetAfterTest();
        $this->setAdminUser();
        $course = $this->getDataGenerator()->create_course();
        $lessonmodule = $this->getDataGenerator()->create_module('lesson', array('course' => $course->id));
        // Convert to a lesson object.
        $lesson = new lesson($lessonmodule);

        // Set up a generator to create content.
        $generator = $this->getDataGenerator()->get_plugin_generator('mod_lesson');
        $tfrecord = $generator->create_question_truefalse($lesson);
        $lesson->duplicate_page($tfrecord->id);

        // Lesson pages.
        $records = $DB->get_records('lesson_pages', array('qtype' => 2));
        $sameelements = array('lessonid', 'qtype', 'qoption', 'layout', 'display', 'title', 'contents', 'contentsformat');
        $baserecord = array_shift($records);
        $secondrecord = array_shift($records);
        foreach ($sameelements as $element) {
            $this->assertEquals($baserecord->$element, $secondrecord->$element);
        }
        // Need lesson answers as well.
        $baserecordanswers = array_values($DB->get_records('lesson_answers', array('pageid' => $baserecord->id)));
        $secondrecordanswers = array_values($DB->get_records('lesson_answers', array('pageid' => $secondrecord->id)));
        $sameanswerelements = array('lessonid', 'jumpto', 'grade', 'score', 'flags', 'answer', 'answerformat', 'response',
                'responseformat');
        foreach ($baserecordanswers as $key => $baseanswer) {
            foreach ($sameanswerelements as $element) {
                $this->assertEquals($baseanswer->$element, $secondrecordanswers[$key]->$element);
            }
        }
    }

    /**
     * Test test_lesson_get_user_deadline().
     */
    public function test_lesson_get_user_deadline(): void {
        global $DB;

        $this->resetAfterTest();
        $this->setAdminUser();

        $basetimestamp = time(); // The timestamp we will base the enddates on.

        // Create generator, course and lessons.
        $student1 = $this->getDataGenerator()->create_user();
        $student2 = $this->getDataGenerator()->create_user();
        $student3 = $this->getDataGenerator()->create_user();
        $teacher = $this->getDataGenerator()->create_user();
        $course = $this->getDataGenerator()->create_course();
        $lessongenerator = $this->getDataGenerator()->get_plugin_generator('mod_lesson');

        // Both lessons close in two hours.
        $lesson1 = $lessongenerator->create_instance(array('course' => $course->id, 'deadline' => $basetimestamp + 7200));
        $lesson2 = $lessongenerator->create_instance(array('course' => $course->id, 'deadline' => $basetimestamp + 7200));
        $group1 = $this->getDataGenerator()->create_group(array('courseid' => $course->id));
        $group2 = $this->getDataGenerator()->create_group(array('courseid' => $course->id));

        $student1id = $student1->id;
        $student2id = $student2->id;
        $student3id = $student3->id;
        $teacherid = $teacher->id;

        // Users enrolments.
        $studentrole = $DB->get_record('role', array('shortname' => 'student'));
        $teacherrole = $DB->get_record('role', array('shortname' => 'editingteacher'));
        $this->getDataGenerator()->enrol_user($student1id, $course->id, $studentrole->id, 'manual');
        $this->getDataGenerator()->enrol_user($student2id, $course->id, $studentrole->id, 'manual');
        $this->getDataGenerator()->enrol_user($student3id, $course->id, $studentrole->id, 'manual');
        $this->getDataGenerator()->enrol_user($teacherid, $course->id, $teacherrole->id, 'manual');

        // Create groups.
        $group1 = $this->getDataGenerator()->create_group(array('courseid' => $course->id));
        $group2 = $this->getDataGenerator()->create_group(array('courseid' => $course->id));
        $group1id = $group1->id;
        $group2id = $group2->id;
        $this->getDataGenerator()->create_group_member(array('userid' => $student1id, 'groupid' => $group1id));
        $this->getDataGenerator()->create_group_member(array('userid' => $student2id, 'groupid' => $group2id));

        // Group 1 gets an group override for lesson 1 to close in three hours.
        $record1 = (object) [
            'lessonid' => $lesson1->id,
            'groupid' => $group1id,
            'deadline' => $basetimestamp + 10800 // In three hours.
        ];
        $DB->insert_record('lesson_overrides', $record1);

        // Let's test lesson 1 closes in three hours for user student 1 since member of group 1.
        // lesson 2 closes in two hours.
        $this->setUser($student1id);
        $params = new \stdClass();

        $comparearray = array();
        $object = new \stdClass();
        $object->id = $lesson1->id;
        $object->userdeadline = $basetimestamp + 10800; // The overriden deadline for lesson 1.

        $comparearray[$lesson1->id] = $object;

        $object = new \stdClass();
        $object->id = $lesson2->id;
        $object->userdeadline = $basetimestamp + 7200; // The unchanged deadline for lesson 2.

        $comparearray[$lesson2->id] = $object;

        $this->assertEquals($comparearray, lesson_get_user_deadline($course->id));

        // Let's test lesson 1 closes in two hours (the original value) for user student 3 since member of no group.
        $this->setUser($student3id);
        $params = new \stdClass();

        $comparearray = array();
        $object = new \stdClass();
        $object->id = $lesson1->id;
        $object->userdeadline = $basetimestamp + 7200; // The original deadline for lesson 1.

        $comparearray[$lesson1->id] = $object;

        $object = new \stdClass();
        $object->id = $lesson2->id;
        $object->userdeadline = $basetimestamp + 7200; // The original deadline for lesson 2.

        $comparearray[$lesson2->id] = $object;

        $this->assertEquals($comparearray, lesson_get_user_deadline($course->id));

        // User 2 gets an user override for lesson 1 to close in four hours.
        $record2 = (object) [
            'lessonid' => $lesson1->id,
            'userid' => $student2id,
            'deadline' => $basetimestamp + 14400 // In four hours.
        ];
        $DB->insert_record('lesson_overrides', $record2);

        // Let's test lesson 1 closes in four hours for user student 2 since personally overriden.
        // lesson 2 closes in two hours.
        $this->setUser($student2id);

        $comparearray = array();
        $object = new \stdClass();
        $object->id = $lesson1->id;
        $object->userdeadline = $basetimestamp + 14400; // The overriden deadline for lesson 1.

        $comparearray[$lesson1->id] = $object;

        $object = new \stdClass();
        $object->id = $lesson2->id;
        $object->userdeadline = $basetimestamp + 7200; // The unchanged deadline for lesson 2.

        $comparearray[$lesson2->id] = $object;

        $this->assertEquals($comparearray, lesson_get_user_deadline($course->id));

        // Let's test a teacher sees the original times.
        // lesson 1 and lesson 2 close in two hours.
        $this->setUser($teacherid);

        $comparearray = array();
        $object = new \stdClass();
        $object->id = $lesson1->id;
        $object->userdeadline = $basetimestamp + 7200; // The unchanged deadline for lesson 1.

        $comparearray[$lesson1->id] = $object;

        $object = new \stdClass();
        $object->id = $lesson2->id;
        $object->userdeadline = $basetimestamp + 7200; // The unchanged deadline for lesson 2.

        $comparearray[$lesson2->id] = $object;

        $this->assertEquals($comparearray, lesson_get_user_deadline($course->id));
    }

    public function test_is_participant(): void {
        global $USER, $DB;
        $this->resetAfterTest();
        $this->setAdminUser();
        $course = $this->getDataGenerator()->create_course();
        $student = $this->getDataGenerator()->create_and_enrol($course, 'student');
        $student2 = $this->getDataGenerator()->create_and_enrol($course, 'student', [], 'manual', 0, 0, ENROL_USER_SUSPENDED);
        $lessonmodule = $this->getDataGenerator()->create_module('lesson', array('course' => $course->id));

        // Login as student.
        $this->setUser($student);
        // Convert to a lesson object.
        $lesson = new lesson($lessonmodule);
        $this->assertEquals(true, $lesson->is_participant($student->id),
            'Student is enrolled, active and can participate');

        // Login as student2.
        $this->setUser($student2);
        $this->assertEquals(false, $lesson->is_participant($student2->id),
            'Student is enrolled, suspended and can NOT participate');

        // Login as an admin.
        $this->setAdminUser();
        $this->assertEquals(false, $lesson->is_participant($USER->id),
            'Admin is not enrolled and can NOT participate');

        $this->getDataGenerator()->enrol_user(2, $course->id);
        $this->assertEquals(true, $lesson->is_participant($USER->id),
            'Admin is enrolled and can participate');

        $this->getDataGenerator()->enrol_user(2, $course->id, null, 'manual', 0, 0, ENROL_USER_SUSPENDED);
        $this->assertEquals(true, $lesson->is_participant($USER->id),
            'Admin is enrolled, suspended and can participate');
    }

    /**
     * Data provider for test_get_last_attempt.
     *
     * @return array
     */
    public static function get_last_attempt_dataprovider(): array {
        return [
            [0, [(object)['id' => 1], (object)['id' => 2], (object)['id' => 3]], (object)['id' => 3]],
            [1, [(object)['id' => 1], (object)['id' => 2], (object)['id' => 3]], (object)['id' => 1]],
            [2, [(object)['id' => 1], (object)['id' => 2], (object)['id' => 3]], (object)['id' => 2]],
            [3, [(object)['id' => 1], (object)['id' => 2], (object)['id' => 3]], (object)['id' => 3]],
            [4, [(object)['id' => 1], (object)['id' => 2], (object)['id' => 3]], (object)['id' => 3]],
        ];
    }

    /**
     * Test the get_last_attempt() method.
     *
     * @dataProvider get_last_attempt_dataprovider
     * @param int $maxattempts Lesson setting.
     * @param array $attempts The list of student attempts.
     * @param object $expected Expected result.
     */
    public function test_get_last_attempt($maxattempts, $attempts, $expected): void {
        $this->resetAfterTest();
        $this->setAdminUser();
        $course = $this->getDataGenerator()->create_course();
        $lesson = $this->getDataGenerator()->create_module('lesson', ['course' => $course, 'maxattempts' => $maxattempts]);
        $lesson = new lesson($lesson);
        $this->assertEquals($expected, $lesson->get_last_attempt($attempts));
    }

    /**
     * Helper function to create attempts for a lesson.
     *
     * @param lesson $lesson The lesson object.
     * @param int $userid The user ID for whom the attempts are created.
     * @param int $count The number of attempts to create.
     */
    private function create_user_attempts(lesson $lesson, int $userid, int $count): void {
        /** @var \mod_lesson_generator $lessongenerator */
        $lessongenerator = $this->getDataGenerator()->get_plugin_generator('mod_lesson');

        for ($i = 0; $i < $count; $i++) {
            $lessongenerator->create_attempt([
                'lessonid' => $lesson->id,
                'userid' => $userid,
                'grade' => 100,
                'retry' => $i,
            ]);
        }
    }

    /**
     * Helper function to create lesson pages with multichoice questions.
     *
     * @param lesson $lesson The lesson object.
     * @param int $count The number of multichoice questions to create.
     */
    private function create_lesson_pages(lesson $lesson, int $count): void {
        /** @var \mod_lesson_generator $lessongenerator */
        $lessongenerator = $this->getDataGenerator()->get_plugin_generator('mod_lesson');

        for ($i = 0; $i < $count; $i++) {
            $lessongenerator->create_page([
                'title' => 'Multichoice question' . ($i + 1),
                'content' => 'Question content',
                'qtype' => 'multichoice',
                'lessonid' => $lesson->id,
            ]);
            $lessongenerator->create_answer(['page' => 'Multichoice question' . ($i + 1), 'answer' => 'A', 'score' => 1]);
            $lessongenerator->create_answer(['page' => 'Multichoice question' . ($i + 1), 'answer' => 'B']);
        }
        $lessongenerator->finish_generate_answer();
    }

    /**
     * Test the count_all_attempts, count_attempted_participants and count_all_participants methods.
     *
     * @covers \lesson::count_all_submissions
     * @covers \lesson::count_submitted_participants
     * @covers \lesson::count_all_participants
     */
    public function test_count_attempts_and_participants(): void {
        global $DB;

        $this->resetAfterTest();
        $this->setAdminUser();

        $course = $this->getDataGenerator()->create_course();
        $student1 = $this->getDataGenerator()->create_and_enrol($course, 'student');
        $student2 = $this->getDataGenerator()->create_and_enrol($course, 'student');
        $student3 = $this->getDataGenerator()->create_and_enrol($course, 'student');
        $student4 = $this->getDataGenerator()->create_and_enrol($course, 'student');

        $teacher = $this->getDataGenerator()->create_and_enrol($course, 'teacher');

        $lessonrecord = $this->getDataGenerator()->create_module(
            'lesson',
            ['course' => $course, 'retake' => 1]
        );

        $lesson = new lesson($lessonrecord);
        $this->create_lesson_pages($lesson, 2);
        $this->create_user_attempts($lesson, $student1->id, 1);
        $this->create_user_attempts($lesson, $student2->id, 2);
        $this->create_user_attempts($lesson, $student3->id, 2);

        $this->setUser($teacher->id);

        $this->assertEquals(5, $lesson->count_all_submissions([]));
        $this->assertEquals(3, $lesson->count_submitted_participants([]));
        $this->assertEquals(4, $lesson->count_all_participants([]));

        // Check that the lesson is not counting teachers as participants.
        $teacher2 = $this->getDataGenerator()->create_and_enrol($course, 'teacher');
        $this->assertEquals(4, $lesson->count_all_participants([]));
        $student5 = $this->getDataGenerator()->create_and_enrol($course, 'student');
        $this->assertEquals(5, $lesson->count_all_participants([]));

        // Prohibit mod/lesson:view capability on student role to ensure it does not count students as participants/submissions.
        $studentrole = $DB->get_record('role', ['shortname' => 'student']);
        assign_capability(
            'mod/lesson:view',
            CAP_PROHIBIT,
            $studentrole->id,
            context_module::instance($lesson->get_cm()->id)
        );
        $this->assertEquals(0, $lesson->count_all_submissions([]));
        $this->assertEquals(0, $lesson->count_submitted_participants([]));
        $this->assertEquals(0, $lesson->count_all_participants([]));
    }

    /**
     * Test the count_all_attempts, count_attempted_participants and count_all_participants methods with groups.
     *
     * @covers \lesson::count_all_submissions
     * @covers \lesson::count_submitted_participants
     * @covers \lesson::count_all_participants
     */
    public function test_count_attempts_and_participants_with_groups(): void {
        global $DB;

        $this->resetAfterTest();
        $this->setAdminUser();

        $course = $this->getDataGenerator()->create_course();
        $student1 = $this->getDataGenerator()->create_and_enrol($course, 'student');
        $student2 = $this->getDataGenerator()->create_and_enrol($course, 'student');
        $student3 = $this->getDataGenerator()->create_and_enrol($course, 'student');
        $student4 = $this->getDataGenerator()->create_and_enrol($course, 'student');

        $lessonrecord = $this->getDataGenerator()->create_module(
            'lesson',
            ['course' => $course, 'retake' => 1, 'groupmode' => SEPARATEGROUPS]
        );

        $lesson = new lesson($lessonrecord);
        $this->create_lesson_pages($lesson, 2);
        $this->create_user_attempts($lesson, $student1->id, 1);
        $this->create_user_attempts($lesson, $student2->id, 2);
        $this->create_user_attempts($lesson, $student3->id, 2);

        $group1 = $this->getDataGenerator()->create_group(['courseid' => $course->id]);
        $group2 = $this->getDataGenerator()->create_group(['courseid' => $course->id]);
        $this->getDataGenerator()->create_group_member(['userid' => $student1->id, 'groupid' => $group1->id]);
        $this->getDataGenerator()->create_group_member(['userid' => $student2->id, 'groupid' => $group1->id]);
        $this->getDataGenerator()->create_group_member(['userid' => $student4->id, 'groupid' => $group2->id]);

        // The following data was created for the lesson:
        // Student 1: group 1, with 1 submission.
        // Student 2: group 1, with 2 submissions.
        // Student 3: not in a group, with 2 submissions.
        // Student 4: group 2, with no submissions.

        $this->assertEquals(3, $lesson->count_all_submissions([$group1->id]));
        $this->assertEquals(2, $lesson->count_submitted_participants([$group1->id]));
        $this->assertEquals(2, $lesson->count_all_participants([$group1->id]));

        $this->assertEquals(3, $lesson->count_all_submissions([$group1->id, $group2->id]));
        $this->assertEquals(2, $lesson->count_submitted_participants([$group1->id, $group2->id]));
        $this->assertEquals(3, $lesson->count_all_participants([$group1->id, $group2->id]));

        // Check that the lesson is not counting teachers as participants.
        $teacher = $this->getDataGenerator()->create_and_enrol($course, 'teacher');
        $this->getDataGenerator()->create_group_member(['userid' => $teacher->id, 'groupid' => $group1->id]);
        $this->assertEquals(2, $lesson->count_all_participants([$group1->id]));
    }

    /**
     * Data provider for test_get_last_page_seen_with_this_page_jumpto.
     *
     * Each case defines:
     *  - maxattempts:   Lesson setting (0 = unlimited).
     *  - attemptcount:  Number of wrong attempts on the question page.
     *  - expectnextpage Whether we expect get_last_page_seen() to return the next page.
     *
     * @return array
     */
    public static function get_last_page_seen_dataprovider(): array {
        return [
            // Unlimited attempts: even with multiple wrong attempts, we should stay on the same page.
            'unlimited_attempts_one_attempt' => [0, 1, false],
            'unlimited_attempts_two_attempts' => [0, 2, false],

            // Maxattempts = 1, with one wrong attempt: attempts are exhausted, move to next page.
            'single_attempt_limit_reached' => [1, 1, true],

            // Maxattempts = 2, with one wrong attempt: not exhausted yet, stay on same page.
            'two_attempt_limit_not_reached' => [2, 1, false],

            // Maxattempts = 2, with two wrong attempts: exhausted, move to next page.
            'two_attempt_limit_reached' => [2, 2, true],
        ];
    }

    /**
     * Helper to create a lesson with a question page followed by a content page,
     * where the wrong answer on the question jumps to "This page".
     *
     * Returns:
     *  - course       => course record
     *  - lesson       => lesson object
     *  - questionpage => question page record
     *  - nextpage     => next page record
     *  - wronganswer  => wrong answer record (jumpto = LESSON_THISPAGE)
     *
     * @param int $maxattempts The lesson maxattempts setting (0 = unlimited).
     * @return array
     */
    private function create_lesson_with_this_page_wrong_answer(int $maxattempts): array {
        global $DB;

        // Course and lesson.
        $course = $this->getDataGenerator()->create_course();
        $lessonrecord = $this->getDataGenerator()->create_module('lesson', [
            'course' => $course->id,
            'maxattempts' => $maxattempts,
        ]);
        $lesson = new lesson($lessonrecord);

        /** @var \mod_lesson_generator $lessongenerator */
        $lessongenerator = $this->getDataGenerator()->get_plugin_generator('mod_lesson');

        // Create a multichoice question page followed by a content page.
        $questionpage = [
            'title' => 'Question page',
            'content' => 'What animal is an amphibian?',
            'qtype' => 'multichoice',
            'lessonid' => $lesson->id,
        ];
        $nextpage = [
            'title' => 'Next content page',
            'content' => 'This is the next page.',
            'qtype' => 'content',
            'lessonid' => $lesson->id,
        ];

        $lessongenerator->create_page($questionpage);
        $lessongenerator->create_page($nextpage);

        // Add a correct answer (Next page) and an incorrect answer (This page).
        $lessongenerator->create_answer([
            'page' => $questionpage['title'],
            'answer' => 'Frog',
            'response' => 'Correct',
            'jumpto' => 'Next page',
            'score' => 1,
        ]);
        $lessongenerator->create_answer([
            'page' => $questionpage['title'],
            'answer' => 'Cat',
            'response' => 'Incorrect',
            'jumpto' => 'This page',
            'score' => 0,
        ]);

        $lessongenerator->finish_generate_answer();

        $questionpagedb = $DB->get_record('lesson_pages', [
            'lessonid' => $lesson->id,
            'qtype' => LESSON_PAGE_MULTICHOICE,
        ], '*', MUST_EXIST);

        $nextpagedb = $DB->get_record('lesson_pages', [
            'lessonid' => $lesson->id,
            'qtype' => LESSON_PAGE_BRANCHTABLE,
        ], '*', MUST_EXIST);

        // Fetch the wrong answer record.
        $wronganswerdb = $DB->get_record('lesson_answers', [
            'lessonid' => $lesson->id,
            'pageid' => $questionpagedb->id,
            'score' => 0,
        ], '*', MUST_EXIST);

        return [
            'course' => $course,
            'lesson' => $lesson,
            'questionpage' => $questionpagedb,
            'nextpage' => $nextpagedb,
            'wronganswer' => $wronganswerdb,
        ];
    }

    /**
     * Test get_last_page_seen() when the last attempts were wrong answers with a
     * "This page" jump, for different maxattempts and attempt counts.
     *
     * Scenario:
     *  - Lesson with a multichoice question followed by a content page.
     *  - Wrong answer on the question jumps to "This page".
     *  - We record wrong attempts on that question.
     *  - We call get_last_page_seen(0) to determine where resuming should start.
     *
     * For unlimited attempts (maxattempts = 0) we should always stay on the question page.
     * For limited attempts, once count(attempts) >= maxattempts, we should go to the next page.
     *
     * @dataProvider get_last_page_seen_dataprovider
     * @param int $maxattempts Lesson setting (0 = unlimited).
     * @param int $attemptcount Number of wrong attempts to create on the question page.
     * @param bool $expectnextpage Whether we expect the next page to be returned.
     * @covers ::get_last_page_seen
     */
    public function test_get_last_page_seen_with_this_page_jumpto(int $maxattempts, int $attemptcount, bool $expectnextpage): void {
        global $DB;

        $this->resetAfterTest();
        $this->setAdminUser();

        // Build the lesson scenario.
        [
            'course' => $course,
            'lesson' => $lesson,
            'questionpage' => $questionpage,
            'nextpage' => $nextpage,
            'wronganswer' => $wronganswer,
        ] = $this->create_lesson_with_this_page_wrong_answer($maxattempts);
        // Create and enrol a student.
        $student = $this->getDataGenerator()->create_and_enrol($course, 'student');

        // Act as the student.
        $this->setUser($student);

        // Create the required number of wrong attempts on the question page.
        for ($i = 0; $i < $attemptcount; $i++) {
            $attempt = (object) [
                'lessonid' => $lesson->id,
                'pageid' => $questionpage->id,
                'userid' => $student->id,
                'answerid' => $wronganswer->id,
                'retry' => 0,
                'correct' => 0,
                'useranswer' => 'Cat', // Cat is the incorrect answer.
                'timeseen' => time(),
            ];
            $DB->insert_record('lesson_attempts', $attempt);
        }

        // Call the method under test.
        $lastpageid = $lesson->get_last_page_seen(0);

        // Decide which page we expect based on the data provider.
        $expectedpageid = $expectnextpage ? $nextpage->id : $questionpage->id;
        $this->assertEquals($expectedpageid, $lastpageid);
    }
}
