See Release Notes
Long Term Support Release
Differences Between: [Versions 401 and 402] [Versions 401 and 403]
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\backup; 18 19 defined('MOODLE_INTERNAL') || die(); 20 21 global $CFG; 22 require_once($CFG->libdir . "/phpunit/classes/restore_date_testcase.php"); 23 24 /** 25 * Restore date tests. 26 * 27 * @package mod_lesson 28 * @copyright 2017 onwards Ankit Agarwal <ankit.agrr@gmail.com> 29 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 30 */ 31 class restore_date_test extends \restore_date_testcase { 32 33 /** 34 * Creates an attempt for the given userwith a correct or incorrect answer and optionally finishes it. 35 * 36 * TODO This api can be better extracted to a generator. 37 * 38 * @param \stdClass $lesson Lesson object. 39 * @param \stdClass $page page object. 40 * @param boolean $correct If the answer should be correct. 41 * @param boolean $finished If we should finish the attempt. 42 * 43 * @return array the result of the attempt creation or finalisation. 44 */ 45 protected function create_attempt($lesson, $page, $correct = true, $finished = false) { 46 global $DB, $USER; 47 48 // First we need to launch the lesson so the timer is on. 49 \mod_lesson_external::launch_attempt($lesson->id); 50 51 $DB->set_field('lesson', 'feedback', 1, array('id' => $lesson->id)); 52 $DB->set_field('lesson', 'progressbar', 1, array('id' => $lesson->id)); 53 $DB->set_field('lesson', 'custom', 0, array('id' => $lesson->id)); 54 $DB->set_field('lesson', 'maxattempts', 3, array('id' => $lesson->id)); 55 56 $answercorrect = 0; 57 $answerincorrect = 0; 58 $p2answers = $DB->get_records('lesson_answers', array('lessonid' => $lesson->id, 'pageid' => $page->id), 'id'); 59 foreach ($p2answers as $answer) { 60 if ($answer->jumpto == 0) { 61 $answerincorrect = $answer->id; 62 } else { 63 $answercorrect = $answer->id; 64 } 65 } 66 67 $data = array( 68 array( 69 'name' => 'answerid', 70 'value' => $correct ? $answercorrect : $answerincorrect, 71 ), 72 array( 73 'name' => '_qf__lesson_display_answer_form_truefalse', 74 'value' => 1, 75 ) 76 ); 77 $result = \mod_lesson_external::process_page($lesson->id, $page->id, $data); 78 $result = \external_api::clean_returnvalue(\mod_lesson_external::process_page_returns(), $result); 79 80 // Create attempt. 81 $newpageattempt = [ 82 'lessonid' => $lesson->id, 83 'pageid' => $page->id, 84 'userid' => $USER->id, 85 'answerid' => $answercorrect, 86 'retry' => 1, // First attempt is always 0. 87 'correct' => 1, 88 'useranswer' => '1', 89 'timeseen' => time(), 90 ]; 91 $DB->insert_record('lesson_attempts', (object) $newpageattempt); 92 93 if ($finished) { 94 $result = \mod_lesson_external::finish_attempt($lesson->id); 95 $result = \external_api::clean_returnvalue(\mod_lesson_external::finish_attempt_returns(), $result); 96 } 97 return $result; 98 } 99 100 /** 101 * Test restore dates. 102 */ 103 public function test_restore_dates() { 104 global $DB, $USER; 105 106 // Create lesson data. 107 $record = ['available' => 100, 'deadline' => 100, 'timemodified' => 100]; 108 list($course, $lesson) = $this->create_course_and_module('lesson', $record); 109 $lessongenerator = $this->getDataGenerator()->get_plugin_generator('mod_lesson'); 110 $page = $lessongenerator->create_content($lesson); 111 $page2 = $lessongenerator->create_question_truefalse($lesson); 112 $this->create_attempt($lesson, $page2, true, true); 113 114 $timer = $DB->get_record('lesson_timer', ['lessonid' => $lesson->id]); 115 // Lesson grade. 116 $timestamp = 100; 117 $grade = new \stdClass(); 118 $grade->lessonid = $lesson->id; 119 $grade->userid = $USER->id; 120 $grade->grade = 8.9; 121 $grade->completed = $timestamp; 122 $grade->id = $DB->insert_record('lesson_grades', $grade); 123 124 // User override. 125 $override = (object)[ 126 'lessonid' => $lesson->id, 127 'groupid' => 0, 128 'userid' => $USER->id, 129 'sortorder' => 1, 130 'available' => 100, 131 'deadline' => 200 132 ]; 133 $DB->insert_record('lesson_overrides', $override); 134 135 // Set time fields to a constant for easy validation. 136 $DB->set_field('lesson_pages', 'timecreated', $timestamp); 137 $DB->set_field('lesson_pages', 'timemodified', $timestamp); 138 $DB->set_field('lesson_answers', 'timecreated', $timestamp); 139 $DB->set_field('lesson_answers', 'timemodified', $timestamp); 140 $DB->set_field('lesson_attempts', 'timeseen', $timestamp); 141 142 // Do backup and restore. 143 $newcourseid = $this->backup_and_restore($course); 144 $newlesson = $DB->get_record('lesson', ['course' => $newcourseid]); 145 146 $this->assertFieldsNotRolledForward($lesson, $newlesson, ['timemodified']); 147 $props = ['available', 'deadline']; 148 $this->assertFieldsRolledForward($lesson, $newlesson, $props); 149 150 $newpages = $DB->get_records('lesson_pages', ['lessonid' => $newlesson->id]); 151 $newanswers = $DB->get_records('lesson_answers', ['lessonid' => $newlesson->id]); 152 $newgrade = $DB->get_record('lesson_grades', ['lessonid' => $newlesson->id]); 153 $newoverride = $DB->get_record('lesson_overrides', ['lessonid' => $newlesson->id]); 154 $newtimer = $DB->get_record('lesson_timer', ['lessonid' => $newlesson->id]); 155 $newattempt = $DB->get_record('lesson_attempts', ['lessonid' => $newlesson->id]); 156 157 // Page time checks. 158 foreach ($newpages as $newpage) { 159 $this->assertEquals($timestamp, $newpage->timemodified); 160 $this->assertEquals($timestamp, $newpage->timecreated); 161 } 162 163 // Page answers time checks. 164 foreach ($newanswers as $newanswer) { 165 $this->assertEquals($timestamp, $newanswer->timemodified); 166 $this->assertEquals($timestamp, $newanswer->timecreated); 167 } 168 169 // Lesson override time checks. 170 $diff = $this->get_diff(); 171 $this->assertEquals($override->available + $diff, $newoverride->available); 172 $this->assertEquals($override->deadline + $diff, $newoverride->deadline); 173 174 // Lesson grade time checks. 175 $this->assertEquals($timestamp, $newgrade->completed); 176 177 // Lesson timer time checks. 178 $this->assertEquals($timer->starttime, $newtimer->starttime); 179 $this->assertEquals($timer->lessontime, $newtimer->lessontime); 180 181 // Lesson attempt time check. 182 $this->assertEquals($timestamp, $newattempt->timeseen); 183 } 184 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body