Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.2.x will end 22 April 2024 (12 months).
  • Bug fixes for security issues in 4.2.x will end 7 October 2024 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.1.x is supported too.

Differences Between: [Versions 311 and 402] [Versions 400 and 402] [Versions 401 and 402]

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