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