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 400 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   * Upgrade script for the quiz module.
  19   *
  20   * @package    mod_quiz
  21   * @copyright  2006 Eloy Lafuente (stronk7)
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  defined('MOODLE_INTERNAL') || die();
  26  
  27  /**
  28   * Quiz module upgrade function.
  29   * @param string $oldversion the version we are upgrading from.
  30   */
  31  function xmldb_quiz_upgrade($oldversion) {
  32      global $CFG, $DB;
  33      $dbman = $DB->get_manager();
  34  
  35      // Automatically generated Moodle v3.9.0 release upgrade line.
  36      // Put any upgrade step following this.
  37  
  38      if ($oldversion < 2020061501) {
  39  
  40          // Define field completionminattempts to be added to quiz.
  41          $table = new xmldb_table('quiz');
  42          $field = new xmldb_field('completionminattempts', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0',
  43              'completionpass');
  44  
  45          // Conditionally launch add field completionminattempts.
  46          if (!$dbman->field_exists($table, $field)) {
  47              $dbman->add_field($table, $field);
  48          }
  49  
  50          // Quiz savepoint reached.
  51          upgrade_mod_savepoint(true, 2020061501, 'quiz');
  52      }
  53  
  54      if ($oldversion < 2021052503) {
  55          $table = new xmldb_table('quiz');
  56          $field = new xmldb_field('completionpass');
  57  
  58          if ($dbman->field_exists($table, $field)) {
  59              $sql = "SELECT q.id, m.id as quizid " .
  60                  "FROM {quiz} q " .
  61                  "INNER JOIN {course_modules} cm ON cm.instance = q.id " .
  62                  "INNER JOIN {modules} m ON m.id = cm.module " .
  63                  "WHERE m.name = :name AND q.completionpass = :completionpass";
  64  
  65              /** @var moodle_recordset $records */
  66              $records = $DB->get_recordset_sql($sql, ['name' => 'quiz', 'completionpass' => 1], 0, 1000);
  67              while ($records->valid()) {
  68                  $quizmodule = null;
  69                  foreach ($records as $record) {
  70                      $ids[] = $record->id;
  71                      $quizmodule = $record->quizid;
  72                  }
  73  
  74                  if ($ids) {
  75                      list($insql, $params) = $DB->get_in_or_equal($ids, SQL_PARAMS_NAMED);
  76                      $DB->set_field_select('course_modules', 'completionpassgrade', 1,
  77                          "module = :quiz AND instance $insql", $params + ['quiz' => $quizmodule]);
  78  
  79                      // Reset the value so it doesn't get picked on the next run. The field will be dropped later.
  80                      $DB->set_field_select('quiz', 'completionpass', 0, "id $insql", $params);
  81  
  82                      // Get the next batch of records.
  83                      $records = $DB->get_recordset_sql($sql, ['name' => 'quiz', 'completionpass' => 1], 0, 1000);
  84                  }
  85              }
  86              $records->close();
  87  
  88              // We have completed our checks. Drop the field.
  89              if ($dbman->field_exists($table, $field)) {
  90                  $dbman->drop_field($table, $field);
  91              }
  92          }
  93  
  94          upgrade_mod_savepoint(true, 2021052503, 'quiz');
  95      }
  96  
  97      if ($oldversion < 2021101900) {
  98  
  99          // Define field gradednotificationsenttime to be added to quiz_attempts.
 100          $table = new xmldb_table('quiz_attempts');
 101          $field = new xmldb_field('gradednotificationsenttime', XMLDB_TYPE_INTEGER, '10', null, null, null, null, 'sumgrades');
 102  
 103          // Conditionally launch add field gradednotificationsenttime.
 104          if (!$dbman->field_exists($table, $field)) {
 105              $dbman->add_field($table, $field);
 106  
 107              $DB->execute('UPDATE {quiz_attempts} SET gradednotificationsenttime = timefinish');
 108          }
 109  
 110          // Quiz savepoint reached.
 111          upgrade_mod_savepoint(true, 2021101900, 'quiz');
 112      }
 113  
 114      if ($oldversion < 2022020300) {
 115          // Define table quiz_slot_tags to be dropped.
 116          $table = new xmldb_table('quiz_slot_tags');
 117  
 118          // Conditionally launch drop table for quiz_slot_tags.
 119          if ($dbman->table_exists($table)) {
 120              $dbman->drop_table($table);
 121          }
 122  
 123          // Define fields to be dropped from quiz_slots.
 124          $table = new xmldb_table('quiz_slots');
 125  
 126          // Define key questionid (foreign) to be dropped form quiz_slots.
 127          $key = new xmldb_key('questionid', XMLDB_KEY_FOREIGN, ['questionid'], 'question', ['id']);
 128  
 129          // Launch drop key questionid.
 130          $dbman->drop_key($table, $key);
 131  
 132          // Define key questioncategoryid (foreign) to be dropped form quiz_slots.
 133          $key = new xmldb_key('questioncategoryid', XMLDB_KEY_FOREIGN, ['questioncategoryid'], 'question_categories', ['id']);
 134  
 135          // Launch drop key questioncategoryid.
 136          $dbman->drop_key($table, $key);
 137  
 138          $field = new xmldb_field('questionid');
 139          // Conditionally launch drop field questionid.
 140          if ($dbman->field_exists($table, $field)) {
 141              $dbman->drop_field($table, $field);
 142          }
 143  
 144          $field = new xmldb_field('questioncategoryid');
 145          // Conditionally launch drop field questioncategoryid.
 146          if ($dbman->field_exists($table, $field)) {
 147              $dbman->drop_field($table, $field);
 148          }
 149  
 150          $field = new xmldb_field('includingsubcategories');
 151          // Conditionally launch drop field includingsubcategories.
 152          if ($dbman->field_exists($table, $field)) {
 153              $dbman->drop_field($table, $field);
 154          }
 155  
 156          // Quiz savepoint reached.
 157          upgrade_mod_savepoint(true, 2022020300, 'quiz');
 158      }
 159  
 160      // Automatically generated Moodle v4.0.0 release upgrade line.
 161      // Put any upgrade step following this.
 162  
 163      // Automatically generated Moodle v4.1.0 release upgrade line.
 164      // Put any upgrade step following this.
 165  
 166      return true;
 167  }