Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 3.9.x will end* 10 May 2021 (12 months).
  • Bug fixes for security issues in 3.9.x will end* 8 May 2023 (36 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.
   1  <?php
   2  
   3  // This file is part of Moodle - http://moodle.org/
   4  //
   5  // Moodle is free software: you can redistribute it and/or modify
   6  // it under the terms of the GNU General Public License as published by
   7  // the Free Software Foundation, either version 3 of the License, or
   8  // (at your option) any later version.
   9  //
  10  // Moodle is distributed in the hope that it will be useful,
  11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13  // GNU General Public License for more details.
  14  //
  15  // You should have received a copy of the GNU General Public License
  16  // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
  17  
  18  /**
  19   * @package mod_lesson
  20   * @subpackage backup-moodle2
  21   * @copyright 2010 onwards Eloy Lafuente (stronk7) {@link http://stronk7.com}
  22   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  defined('MOODLE_INTERNAL') || die();
  26  
  27  require_once($CFG->dirroot . '/mod/lesson/backup/moodle2/restore_lesson_stepslib.php'); // Because it exists (must)
  28  
  29  /**
  30   * lesson restore task that provides all the settings and steps to perform one
  31   * complete restore of the activity
  32   */
  33  class restore_lesson_activity_task extends restore_activity_task {
  34  
  35      /**
  36       * Define (add) particular settings this activity can have
  37       */
  38      protected function define_my_settings() {
  39          // No particular settings for this activity
  40      }
  41  
  42      /**
  43       * Define (add) particular steps this activity can have
  44       */
  45      protected function define_my_steps() {
  46          // lesson only has one structure step
  47          $this->add_step(new restore_lesson_activity_structure_step('lesson_structure', 'lesson.xml'));
  48      }
  49  
  50      /**
  51       * Define the contents in the activity that must be
  52       * processed by the link decoder
  53       */
  54      static public function define_decode_contents() {
  55          $contents = array();
  56  
  57          $contents[] = new restore_decode_content('lesson', array('intro'), 'lesson');
  58          $contents[] = new restore_decode_content('lesson_pages', array('contents'), 'lesson_page');
  59          $contents[] = new restore_decode_content('lesson_answers', array('answer', 'response'), 'lesson_answer');
  60  
  61          return $contents;
  62      }
  63  
  64      /**
  65       * Define the decoding rules for links belonging
  66       * to the activity to be executed by the link decoder
  67       */
  68      static public function define_decode_rules() {
  69          $rules = array();
  70  
  71          $rules[] = new restore_decode_rule('LESSONEDIT', '/mod/lesson/edit.php?id=$1', 'course_module');
  72          $rules[] = new restore_decode_rule('LESSONESAY', '/mod/lesson/essay.php?id=$1', 'course_module');
  73          $rules[] = new restore_decode_rule('LESSONREPORT', '/mod/lesson/report.php?id=$1', 'course_module');
  74          $rules[] = new restore_decode_rule('LESSONMEDIAFILE', '/mod/lesson/mediafile.php?id=$1', 'course_module');
  75          $rules[] = new restore_decode_rule('LESSONVIEWBYID', '/mod/lesson/view.php?id=$1', 'course_module');
  76          $rules[] = new restore_decode_rule('LESSONINDEX', '/mod/lesson/index.php?id=$1', 'course');
  77          $rules[] = new restore_decode_rule('LESSONVIEWPAGE', '/mod/lesson/view.php?id=$1&pageid=$2', array('course_module', 'lesson_page'));
  78          $rules[] = new restore_decode_rule('LESSONEDITPAGE', '/mod/lesson/edit.php?id=$1&pageid=$2', array('course_module', 'lesson_page'));
  79  
  80          return $rules;
  81  
  82      }
  83  
  84      /**
  85       * Define the restore log rules that will be applied
  86       * by the {@link restore_logs_processor} when restoring
  87       * lesson logs. It must return one array
  88       * of {@link restore_log_rule} objects
  89       */
  90      static public function define_restore_log_rules() {
  91          $rules = array();
  92  
  93          $rules[] = new restore_log_rule('lesson', 'add', 'view.php?id={course_module}', '{lesson}');
  94          $rules[] = new restore_log_rule('lesson', 'update', 'view.php?id={course_module}', '{lesson}');
  95          $rules[] = new restore_log_rule('lesson', 'view', 'view.php?id={course_module}', '{lesson}');
  96          $rules[] = new restore_log_rule('lesson', 'start', 'view.php?id={course_module}', '{lesson}');
  97          $rules[] = new restore_log_rule('lesson', 'end', 'view.php?id={course_module}', '{lesson}');
  98          $rules[] = new restore_log_rule('lesson', 'view grade', 'essay.php?id={course_module}', '[name]');
  99          $rules[] = new restore_log_rule('lesson', 'update grade', 'essay.php?id={course_module}', '[name]');
 100          $rules[] = new restore_log_rule('lesson', 'update email essay grade', 'essay.php?id={course_module}', '[name]');
 101  
 102          return $rules;
 103      }
 104  
 105      /**
 106       * Define the restore log rules that will be applied
 107       * by the {@link restore_logs_processor} when restoring
 108       * course logs. It must return one array
 109       * of {@link restore_log_rule} objects
 110       *
 111       * Note this rules are applied when restoring course logs
 112       * by the restore final task, but are defined here at
 113       * activity level. All them are rules not linked to any module instance (cmid = 0)
 114       */
 115      static public function define_restore_log_rules_for_course() {
 116          $rules = array();
 117  
 118          $rules[] = new restore_log_rule('lesson', 'view all', 'index.php?id={course}', null);
 119  
 120          return $rules;
 121      }
 122  
 123  
 124      /**
 125       * Re-map the dependency and activitylink information
 126       * If a depency or activitylink has no mapping in the backup data then it could either be a duplication of a
 127       * lesson, or a backup/restore of a single lesson. We have no way to determine which and whether this is the
 128       * same site and/or course. Therefore we try and retrieve a mapping, but fallback to the original value if one
 129       * was not found. We then test to see whether the value found is valid for the course being restored into.
 130       */
 131      public function after_restore() {
 132          global $DB;
 133  
 134          $lesson = $DB->get_record('lesson', array('id' => $this->get_activityid()), 'id, course, dependency, activitylink');
 135          $updaterequired = false;
 136  
 137          if (!empty($lesson->dependency)) {
 138              $updaterequired = true;
 139              if ($newitem = restore_dbops::get_backup_ids_record($this->get_restoreid(), 'lesson', $lesson->dependency)) {
 140                  $lesson->dependency = $newitem->newitemid;
 141              }
 142              if (!$DB->record_exists('lesson', array('id' => $lesson->dependency, 'course' => $lesson->course))) {
 143                  $lesson->dependency = 0;
 144              }
 145          }
 146  
 147          if (!empty($lesson->activitylink)) {
 148              $updaterequired = true;
 149              if ($newitem = restore_dbops::get_backup_ids_record($this->get_restoreid(), 'course_module', $lesson->activitylink)) {
 150                  $lesson->activitylink = $newitem->newitemid;
 151              }
 152              if (!$DB->record_exists('course_modules', array('id' => $lesson->activitylink, 'course' => $lesson->course))) {
 153                  $lesson->activitylink = 0;
 154              }
 155          }
 156  
 157          if ($updaterequired) {
 158              $DB->update_record('lesson', $lesson);
 159          }
 160      }
 161  }