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.

Differences Between: [Versions 310 and 401] [Versions 311 and 401] [Versions 39 and 401] [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  /**
  18   * Define all the backup steps that will be used by the backup_quiz_activity_task.
  19   *
  20   * @package    mod_quiz
  21   * @subpackage backup-moodle2
  22   * @copyright  2010 onwards Eloy Lafuente (stronk7) {@link http://stronk7.com}
  23   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   */
  25  class backup_quiz_activity_structure_step extends backup_questions_activity_structure_step {
  26  
  27      protected function define_structure() {
  28  
  29          // To know if we are including userinfo.
  30          $userinfo = $this->get_setting_value('userinfo');
  31  
  32          // Define each element separated.
  33          $quiz = new backup_nested_element('quiz', ['id'], [
  34              'name', 'intro', 'introformat', 'timeopen', 'timeclose', 'timelimit',
  35              'overduehandling', 'graceperiod', 'preferredbehaviour', 'canredoquestions', 'attempts_number',
  36              'attemptonlast', 'grademethod', 'decimalpoints', 'questiondecimalpoints',
  37              'reviewattempt', 'reviewcorrectness', 'reviewmarks',
  38              'reviewspecificfeedback', 'reviewgeneralfeedback',
  39              'reviewrightanswer', 'reviewoverallfeedback',
  40              'questionsperpage', 'navmethod', 'shuffleanswers',
  41              'sumgrades', 'grade', 'timecreated',
  42              'timemodified', 'password', 'subnet', 'browsersecurity',
  43              'delay1', 'delay2', 'showuserpicture', 'showblocks', 'completionattemptsexhausted',
  44              'completionminattempts', 'allowofflineattempts']);
  45  
  46          // Define elements for access rule subplugin settings.
  47          $this->add_subplugin_structure('quizaccess', $quiz, true);
  48  
  49          $qinstances = new backup_nested_element('question_instances');
  50  
  51          $qinstance = new backup_nested_element('question_instance', ['id'],
  52                  ['quizid', 'slot', 'page', 'requireprevious', 'maxmark']);
  53  
  54          $this->add_question_references($qinstance, 'mod_quiz', 'slot');
  55  
  56          $this->add_question_set_references($qinstance, 'mod_quiz', 'slot');
  57  
  58          $sections = new backup_nested_element('sections');
  59  
  60          $section = new backup_nested_element('section', ['id'], ['firstslot', 'heading', 'shufflequestions']);
  61  
  62          $feedbacks = new backup_nested_element('feedbacks');
  63  
  64          $feedback = new backup_nested_element('feedback', ['id'], ['feedbacktext', 'feedbacktextformat', 'mingrade', 'maxgrade']);
  65  
  66          $overrides = new backup_nested_element('overrides');
  67  
  68          $override = new backup_nested_element('override', ['id'], [
  69              'userid', 'groupid', 'timeopen', 'timeclose',
  70              'timelimit', 'attempts', 'password']);
  71  
  72          $grades = new backup_nested_element('grades');
  73  
  74          $grade = new backup_nested_element('grade', ['id'], ['userid', 'gradeval', 'timemodified']);
  75  
  76          $attempts = new backup_nested_element('attempts');
  77  
  78          $attempt = new backup_nested_element('attempt', ['id'], [
  79              'userid', 'attemptnum', 'uniqueid', 'layout', 'currentpage', 'preview',
  80              'state', 'timestart', 'timefinish', 'timemodified', 'timemodifiedoffline',
  81              'timecheckstate', 'sumgrades', 'gradednotificationsenttime']);
  82  
  83          // This module is using questions, so produce the related question states and sessions
  84          // attaching them to the $attempt element based in 'uniqueid' matching.
  85          $this->add_question_usages($attempt, 'uniqueid');
  86  
  87          // Define elements for access rule subplugin attempt data.
  88          $this->add_subplugin_structure('quizaccess', $attempt, true);
  89  
  90          // Build the tree.
  91          $quiz->add_child($qinstances);
  92          $qinstances->add_child($qinstance);
  93  
  94          $quiz->add_child($sections);
  95          $sections->add_child($section);
  96  
  97          $quiz->add_child($feedbacks);
  98          $feedbacks->add_child($feedback);
  99  
 100          $quiz->add_child($overrides);
 101          $overrides->add_child($override);
 102  
 103          $quiz->add_child($grades);
 104          $grades->add_child($grade);
 105  
 106          $quiz->add_child($attempts);
 107          $attempts->add_child($attempt);
 108  
 109          // Define sources.
 110          $quiz->set_source_table('quiz', ['id' => backup::VAR_ACTIVITYID]);
 111  
 112          $qinstance->set_source_table('quiz_slots', ['quizid' => backup::VAR_PARENTID]);
 113  
 114          $section->set_source_table('quiz_sections', ['quizid' => backup::VAR_PARENTID]);
 115  
 116          $feedback->set_source_table('quiz_feedback', ['quizid' => backup::VAR_PARENTID]);
 117  
 118          // Quiz overrides to backup are different depending of user info.
 119          $overrideparams = ['quiz' => backup::VAR_PARENTID];
 120          if (!$userinfo) { //  Without userinfo, skip user overrides.
 121              $overrideparams['userid'] = backup_helper::is_sqlparam(null);
 122  
 123          }
 124  
 125          // Skip group overrides if not including groups.
 126          $groupinfo = $this->get_setting_value('groups');
 127          if (!$groupinfo) {
 128              $overrideparams['groupid'] = backup_helper::is_sqlparam(null);
 129          }
 130  
 131          $override->set_source_table('quiz_overrides', $overrideparams);
 132  
 133          // All the rest of elements only happen if we are including user info.
 134          if ($userinfo) {
 135              $grade->set_source_table('quiz_grades', ['quiz' => backup::VAR_PARENTID]);
 136              $attempt->set_source_sql('
 137                      SELECT *
 138                      FROM {quiz_attempts}
 139                      WHERE quiz = :quiz AND preview = 0', ['quiz' => backup::VAR_PARENTID]);
 140          }
 141  
 142          // Define source alias.
 143          $quiz->set_source_alias('attempts', 'attempts_number');
 144          $grade->set_source_alias('grade', 'gradeval');
 145          $attempt->set_source_alias('attempt', 'attemptnum');
 146  
 147          // Define id annotations.
 148          $override->annotate_ids('user', 'userid');
 149          $override->annotate_ids('group', 'groupid');
 150          $grade->annotate_ids('user', 'userid');
 151          $attempt->annotate_ids('user', 'userid');
 152  
 153          // Define file annotations.
 154          $quiz->annotate_files('mod_quiz', 'intro', null); // This file area hasn't itemid.
 155          $feedback->annotate_files('mod_quiz', 'feedback', 'id');
 156  
 157          // Return the root element (quiz), wrapped into standard activity structure.
 158          return $this->prepare_activity_structure($quiz);
 159      }
 160  }