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