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 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.9.0 release upgrade line.
  38      // Put any upgrade step following this.
  39  
  40      if ($oldversion < 2021110901) {
  41          // Define field activity to be added to assign.
  42          $table = new xmldb_table('assign');
  43          $field = new xmldb_field('activity', XMLDB_TYPE_TEXT, null, null, null, null, null, 'alwaysshowdescription');
  44  
  45          // Conditionally launch add field activity.
  46          if (!$dbman->field_exists($table, $field)) {
  47              $dbman->add_field($table, $field);
  48          }
  49  
  50          $field = new xmldb_field('activityformat', XMLDB_TYPE_INTEGER, '4', null, XMLDB_NOTNULL, null, '0', 'activity');
  51  
  52          // Conditionally launch add field activityformat.
  53          if (!$dbman->field_exists($table, $field)) {
  54              $dbman->add_field($table, $field);
  55          }
  56  
  57          $field = new xmldb_field('timelimit', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0', 'cutoffdate');
  58  
  59          // Conditionally launch add field timelimit.
  60          if (!$dbman->field_exists($table, $field)) {
  61              $dbman->add_field($table, $field);
  62          }
  63  
  64          $field = new xmldb_field('submissionattachments', XMLDB_TYPE_INTEGER, '2',
  65              null, XMLDB_NOTNULL, null, '0', 'activityformat');
  66  
  67          // Conditionally launch add field submissionattachments.
  68          if (!$dbman->field_exists($table, $field)) {
  69              $dbman->add_field($table, $field);
  70          }
  71  
  72          $table = new xmldb_table('assign_submission');
  73          $field = new xmldb_field('timestarted', XMLDB_TYPE_INTEGER, '10', null, null, null, null, 'timemodified');
  74  
  75          // Conditionally launch add field timestarted.
  76          if (!$dbman->field_exists($table, $field)) {
  77              $dbman->add_field($table, $field);
  78          }
  79  
  80          // Define field timelimit to be added to assign_overrides.
  81          $table = new xmldb_table('assign_overrides');
  82          $field = new xmldb_field('timelimit', XMLDB_TYPE_INTEGER, '10', null, null, null, null, 'cutoffdate');
  83  
  84          // Conditionally launch add field timelimit.
  85          if (!$dbman->field_exists($table, $field)) {
  86              $dbman->add_field($table, $field);
  87          }
  88  
  89          // Assign savepoint reached.
  90          upgrade_mod_savepoint(true, 2021110901, 'assign');
  91      }
  92  
  93      // Automatically generated Moodle v4.0.0 release upgrade line.
  94      // Put any upgrade step following this.
  95  
  96      if ($oldversion < 2022071300) {
  97          // The most recent assign submission should always have latest = 1, we want to find all records where this is not the case.
  98          // Find the records with the maximum timecreated for each assign and user combination where latest is also 0.
  99          $sqluser = "SELECT s.id
 100                        FROM {assign_submission} s
 101                       WHERE s.timecreated = (
 102                            SELECT  MAX(timecreated) timecreated
 103                              FROM {assign_submission} sm
 104                             WHERE s.assignment = sm.assignment
 105                                   AND s.userid = sm.userid
 106                                   AND sm.groupid = 0)
 107                             AND s.groupid = 0
 108                             AND s.latest = 0";
 109          $idstofixuser = $DB->get_records_sql($sqluser, null);
 110  
 111          $sqlgroup = "SELECT s.id
 112                         FROM {assign_submission} s
 113                        WHERE s.timecreated = (
 114                            SELECT  MAX(timecreated) timecreated
 115                              FROM {assign_submission} sm
 116                             WHERE s.assignment = sm.assignment
 117                                   AND s.groupid = sm.groupid
 118                                   AND sm.groupid <> 0)
 119                              AND s.groupid <> 0
 120                              AND s.latest = 0";
 121          $idstofixgroup = $DB->get_records_sql($sqlgroup, null);
 122  
 123          $idstofix = array_merge(array_keys($idstofixuser), array_keys($idstofixgroup));
 124  
 125          if (count($idstofix)) {
 126              [$insql, $inparams] = $DB->get_in_or_equal($idstofix);
 127              $DB->set_field_select('assign_submission', 'latest', 1, "id $insql", $inparams);
 128          }
 129  
 130          // Assignment savepoint reached.
 131          upgrade_mod_savepoint(true, 2022071300, 'assign');
 132      }
 133      // Automatically generated Moodle v4.1.0 release upgrade line.
 134      // Put any upgrade step following this.
 135  
 136      return true;
 137  }