Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.0.x will end 8 May 2023 (12 months).
  • Bug fixes for security issues in 4.0.x will end 13 November 2023 (18 months).
  • PHP version: minimum PHP 7.3.0 Note: the minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is also supported.

Differences Between: [Versions 310 and 400] [Versions 311 and 400] [Versions 39 and 400] [Versions 400 and 401] [Versions 400 and 402] [Versions 400 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 code for install
  19   *
  20   * @package   mod_assign
  21   * @copyright 2012 NetSpot {@link http://www.netspot.com.au}
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  defined('MOODLE_INTERNAL') || die();
  26  
  27  /**
  28   * upgrade this assignment instance - this function could be skipped but it will be needed later
  29   * @param int $oldversion The old version of the assign module
  30   * @return bool
  31   */
  32  function xmldb_assign_upgrade($oldversion) {
  33      global $CFG, $DB;
  34  
  35      $dbman = $DB->get_manager();
  36  
  37      // Automatically generated Moodle v3.6.0 release upgrade line.
  38      // Put any upgrade step following this.
  39  
  40      if ($oldversion < 2018120500) {
  41          // Define field hidegrader to be added to assign.
  42          $table = new xmldb_table('assign');
  43          $field = new xmldb_field('hidegrader', XMLDB_TYPE_INTEGER, '2', null, XMLDB_NOTNULL, null, '0', 'blindmarking');
  44  
  45          if (!$dbman->field_exists($table, $field)) {
  46              $dbman->add_field($table, $field);
  47          }
  48  
  49          // Assignment savepoint reached.
  50          upgrade_mod_savepoint(true, 2018120500, 'assign');
  51      }
  52  
  53      // Automatically generated Moodle v3.7.0 release upgrade line.
  54      // Put any upgrade step following this.
  55  
  56      // Automatically generated Moodle v3.8.0 release upgrade line.
  57      // Put any upgrade step following this.
  58  
  59      // Automatically generated Moodle v3.9.0 release upgrade line.
  60      // Put any upgrade step following this.
  61  
  62      if ($oldversion < 2021110901) {
  63          // Define field activity to be added to assign.
  64          $table = new xmldb_table('assign');
  65          $field = new xmldb_field('activity', XMLDB_TYPE_TEXT, null, null, null, null, null, 'alwaysshowdescription');
  66  
  67          // Conditionally launch add field activity.
  68          if (!$dbman->field_exists($table, $field)) {
  69              $dbman->add_field($table, $field);
  70          }
  71  
  72          $field = new xmldb_field('activityformat', XMLDB_TYPE_INTEGER, '4', null, XMLDB_NOTNULL, null, '0', 'activity');
  73  
  74          // Conditionally launch add field activityformat.
  75          if (!$dbman->field_exists($table, $field)) {
  76              $dbman->add_field($table, $field);
  77          }
  78  
  79          $field = new xmldb_field('timelimit', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0', 'cutoffdate');
  80  
  81          // Conditionally launch add field timelimit.
  82          if (!$dbman->field_exists($table, $field)) {
  83              $dbman->add_field($table, $field);
  84          }
  85  
  86          $field = new xmldb_field('submissionattachments', XMLDB_TYPE_INTEGER, '2',
  87              null, XMLDB_NOTNULL, null, '0', 'activityformat');
  88  
  89          // Conditionally launch add field submissionattachments.
  90          if (!$dbman->field_exists($table, $field)) {
  91              $dbman->add_field($table, $field);
  92          }
  93  
  94          $table = new xmldb_table('assign_submission');
  95          $field = new xmldb_field('timestarted', XMLDB_TYPE_INTEGER, '10', null, null, null, null, 'timemodified');
  96  
  97          // Conditionally launch add field timestarted.
  98          if (!$dbman->field_exists($table, $field)) {
  99              $dbman->add_field($table, $field);
 100          }
 101  
 102          // Define field timelimit to be added to assign_overrides.
 103          $table = new xmldb_table('assign_overrides');
 104          $field = new xmldb_field('timelimit', XMLDB_TYPE_INTEGER, '10', null, null, null, null, 'cutoffdate');
 105  
 106          // Conditionally launch add field timelimit.
 107          if (!$dbman->field_exists($table, $field)) {
 108              $dbman->add_field($table, $field);
 109          }
 110  
 111          // Assign savepoint reached.
 112          upgrade_mod_savepoint(true, 2021110901, 'assign');
 113      }
 114  
 115      // Automatically generated Moodle v4.0.0 release upgrade line.
 116      // Put any upgrade step following this.
 117  
 118      if ($oldversion < 2022041901) {
 119          // The most recent assign submission should always have latest = 1, we want to find all records where this is not the case.
 120          // Find the records with the maximum timecreated for each assign and user combination where latest is also 0.
 121          $sqluser = "SELECT s.id
 122                        FROM {assign_submission} s
 123                       WHERE s.timecreated = (
 124                            SELECT  MAX(timecreated) timecreated
 125                              FROM {assign_submission} sm
 126                             WHERE s.assignment = sm.assignment
 127                                   AND s.userid = sm.userid
 128                                   AND sm.groupid = 0)
 129                             AND s.groupid = 0
 130                             AND s.latest = 0";
 131          $idstofixuser = $DB->get_records_sql($sqluser, null);
 132  
 133          $sqlgroup = "SELECT s.id
 134                         FROM {assign_submission} s
 135                        WHERE s.timecreated = (
 136                            SELECT  MAX(timecreated) timecreated
 137                              FROM {assign_submission} sm
 138                             WHERE s.assignment = sm.assignment
 139                                   AND s.groupid = sm.groupid
 140                                   AND sm.groupid <> 0)
 141                              AND s.groupid <> 0
 142                              AND s.latest = 0";
 143          $idstofixgroup = $DB->get_records_sql($sqlgroup, null);
 144  
 145          $idstofix = array_merge(array_keys($idstofixuser), array_keys($idstofixgroup));
 146  
 147          if (count($idstofix)) {
 148              [$insql, $inparams] = $DB->get_in_or_equal($idstofix);
 149              $DB->set_field_select('assign_submission', 'latest', 1, "id $insql", $inparams);
 150          }
 151  
 152          // Assignment savepoint reached.
 153          upgrade_mod_savepoint(true, 2022041901, 'assign');
 154      }
 155      return true;
 156  }