Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is 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   * This file contains the backup structure for the lesson module
  20   *
  21   * This is the "graphical" structure of the lesson module:
  22   *
  23   *         lesson ---------->-------------|------------>---------|----------->----------|
  24   *      (CL,pk->id)                       |                      |                      |
  25   *            |                           |                      |                      |
  26   *            |                     lesson_grades           lesson_timer           lesson_overrides
  27   *            |            (UL, pk->id,fk->lessonid)  (UL, pk->id,fk->lessonid) (UL, pk->id,fk->lessonid)
  28   *            |                           |
  29   *            |                           |
  30   *            |                           |
  31   *            |                           |
  32   *      lesson_pages-------->-------lesson_branch
  33   *   (CL,pk->id,fk->lessonid)     (UL, pk->id,fk->pageid)
  34   *            |
  35   *            |
  36   *            |
  37   *      lesson_answers
  38   *   (CL,pk->id,fk->pageid)
  39   *            |
  40   *            |
  41   *            |
  42   *      lesson_attempts
  43   *  (UL,pk->id,fk->answerid)
  44   *
  45   * Meaning: pk->primary key field of the table
  46   *          fk->foreign key to link with parent
  47   *          nt->nested field (recursive data)
  48   *          CL->course level info
  49   *          UL->user level info
  50   *          files->table may have files)
  51   *
  52   * @package mod_lesson
  53   * @copyright  2010 Sam Hemelryk
  54   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  55   */
  56  
  57  /**
  58   * Structure step class that informs a backup task how to backup the lesson module.
  59   *
  60   * @copyright  2010 Sam Hemelryk
  61   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  62   */
  63  class backup_lesson_activity_structure_step extends backup_activity_structure_step {
  64  
  65      protected function define_structure() {
  66  
  67          // The lesson table
  68          // This table contains all of the goodness for the lesson module, quite
  69          // alot goes into it but nothing relational other than course when will
  70          // need to be corrected upon restore.
  71          $lesson = new backup_nested_element('lesson', array('id'), array(
  72              'course', 'name', 'intro', 'introformat', 'practice', 'modattempts',
  73              'usepassword', 'password',
  74              'dependency', 'conditions', 'grade', 'custom', 'ongoing', 'usemaxgrade',
  75              'maxanswers', 'maxattempts', 'review', 'nextpagedefault', 'feedback',
  76              'minquestions', 'maxpages', 'timelimit', 'retake', 'activitylink',
  77              'mediafile', 'mediaheight', 'mediawidth', 'mediaclose', 'slideshow',
  78              'width', 'height', 'bgcolor', 'displayleft', 'displayleftif', 'progressbar',
  79              'available', 'deadline', 'timemodified',
  80              'completionendreached', 'completiontimespent', 'allowofflineattempts'
  81          ));
  82  
  83          // The lesson_pages table
  84          // Grouped within a `pages` element, important to note that page is relational
  85          // to the lesson, and also to the previous/next page in the series.
  86          // Upon restore prevpageid and nextpageid will need to be corrected.
  87          $pages = new backup_nested_element('pages');
  88          $page = new backup_nested_element('page', array('id'), array(
  89              'prevpageid','nextpageid','qtype','qoption','layout',
  90              'display','timecreated','timemodified','title','contents',
  91              'contentsformat'
  92          ));
  93  
  94          // The lesson_answers table
  95          // Grouped within an answers `element`, the lesson_answers table relates
  96          // to the page and lesson with `pageid` and `lessonid` that will both need
  97          // to be corrected during restore.
  98          $answers = new backup_nested_element('answers');
  99          $answer = new backup_nested_element('answer', array('id'), array(
 100              'jumpto','grade','score','flags','timecreated','timemodified','answer_text',
 101              'response', 'answerformat', 'responseformat'
 102          ));
 103          // Tell the answer element about the answer_text elements mapping to the answer
 104          // database field.
 105          $answer->set_source_alias('answer', 'answer_text');
 106  
 107          // The lesson_attempts table
 108          // Grouped by an `attempts` element this is relational to the page, lesson,
 109          // and user.
 110          $attempts = new backup_nested_element('attempts');
 111          $attempt = new backup_nested_element('attempt', array('id'), array(
 112              'userid','retry','correct','useranswer','timeseen'
 113          ));
 114  
 115          // The lesson_branch table
 116          // Grouped by a `branch` element this is relational to the page, lesson,
 117          // and user.
 118          $branches = new backup_nested_element('branches');
 119          $branch = new backup_nested_element('branch', array('id'), array(
 120               'userid', 'retry', 'flag', 'timeseen', 'nextpageid'
 121          ));
 122  
 123          // The lesson_grades table
 124          // Grouped by a grades element this is relational to the lesson and user.
 125          $grades = new backup_nested_element('grades');
 126          $grade = new backup_nested_element('grade', array('id'), array(
 127              'userid','grade','late','completed'
 128          ));
 129  
 130          // The lesson_timer table
 131          // Grouped by a `timers` element this is relational to the lesson and user.
 132          $timers = new backup_nested_element('timers');
 133          $timer = new backup_nested_element('timer', array('id'), array(
 134              'userid', 'starttime', 'lessontime', 'completed', 'timemodifiedoffline'
 135          ));
 136  
 137          $overrides = new backup_nested_element('overrides');
 138          $override = new backup_nested_element('override', array('id'), array(
 139              'groupid', 'userid', 'available', 'deadline', 'timelimit',
 140              'review', 'maxattempts', 'retake', 'password'));
 141  
 142          // Now that we have all of the elements created we've got to put them
 143          // together correctly.
 144          $lesson->add_child($pages);
 145          $pages->add_child($page);
 146          $page->add_child($answers);
 147          $answers->add_child($answer);
 148          $answer->add_child($attempts);
 149          $attempts->add_child($attempt);
 150          $page->add_child($branches);
 151          $branches->add_child($branch);
 152          $lesson->add_child($grades);
 153          $grades->add_child($grade);
 154          $lesson->add_child($timers);
 155          $timers->add_child($timer);
 156          $lesson->add_child($overrides);
 157          $overrides->add_child($override);
 158  
 159          // Set the source table for the elements that aren't reliant on the user
 160          // at this point (lesson, lesson_pages, lesson_answers)
 161          $lesson->set_source_table('lesson', array('id' => backup::VAR_ACTIVITYID));
 162          //we use SQL here as it must be ordered by prevpageid so that restore gets the pages in the right order.
 163          $page->set_source_table('lesson_pages', array('lessonid' => backup::VAR_PARENTID), 'prevpageid ASC');
 164  
 165          // We use SQL here as answers must be ordered by id so that the restore gets them in the right order
 166          $answer->set_source_table('lesson_answers', array('pageid' => backup::VAR_PARENTID), 'id ASC');
 167  
 168          // Lesson overrides to backup are different depending of user info.
 169          $overrideparams = array('lessonid' => backup::VAR_PARENTID);
 170  
 171          // Check if we are also backing up user information
 172          if ($this->get_setting_value('userinfo')) {
 173              // Set the source table for elements that are reliant on the user
 174              // lesson_attempts, lesson_branch, lesson_grades, lesson_timer.
 175              $attempt->set_source_table('lesson_attempts', array('answerid' => backup::VAR_PARENTID));
 176              $branch->set_source_table('lesson_branch', array('pageid' => backup::VAR_PARENTID));
 177              $grade->set_source_table('lesson_grades', array('lessonid'=>backup::VAR_PARENTID));
 178              $timer->set_source_table('lesson_timer', array('lessonid' => backup::VAR_PARENTID));
 179          } else {
 180              $overrideparams['userid'] = backup_helper::is_sqlparam(null); //  Without userinfo, skip user overrides.
 181          }
 182  
 183          // Skip group overrides if not including groups.
 184          $groupinfo = $this->get_setting_value('groups');
 185          if (!$groupinfo) {
 186              $overrideparams['groupid'] = backup_helper::is_sqlparam(null);
 187          }
 188  
 189          $override->set_source_table('lesson_overrides', $overrideparams);
 190  
 191          // Annotate the user id's where required.
 192          $attempt->annotate_ids('user', 'userid');
 193          $branch->annotate_ids('user', 'userid');
 194          $grade->annotate_ids('user', 'userid');
 195          $timer->annotate_ids('user', 'userid');
 196          $override->annotate_ids('user', 'userid');
 197          $override->annotate_ids('group', 'groupid');
 198  
 199          // Annotate the file areas in user by the lesson module.
 200          $lesson->annotate_files('mod_lesson', 'intro', null);
 201          $lesson->annotate_files('mod_lesson', 'mediafile', null);
 202          $page->annotate_files('mod_lesson', 'page_contents', 'id');
 203          $answer->annotate_files('mod_lesson', 'page_answers', 'id');
 204          $answer->annotate_files('mod_lesson', 'page_responses', 'id');
 205          $attempt->annotate_files('mod_lesson', 'essay_responses', 'id');
 206          $attempt->annotate_files('mod_lesson', 'essay_answers', 'id');
 207  
 208          // Prepare and return the structure we have just created for the lesson module.
 209          return $this->prepare_activity_structure($lesson);
 210      }
 211  }