[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/mod/assign/db/ -> upgrade.php (source)

   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      if ($oldversion < 2014051201) {
  38  
  39          // Cleanup bad database records where assignid is missing.
  40  
  41          $DB->delete_records('assign_user_mapping', array('assignment'=>0));
  42          // Assign savepoint reached.
  43          upgrade_mod_savepoint(true, 2014051201, 'assign');
  44      }
  45  
  46      if ($oldversion < 2014072400) {
  47  
  48          // Add "latest" column to submissions table to mark the latest attempt.
  49          $table = new xmldb_table('assign_submission');
  50          $field = new xmldb_field('latest', XMLDB_TYPE_INTEGER, '2', null, XMLDB_NOTNULL, null, '0', 'attemptnumber');
  51  
  52          // Conditionally launch add field latest.
  53          if (!$dbman->field_exists($table, $field)) {
  54              $dbman->add_field($table, $field);
  55          }
  56  
  57          // Assign savepoint reached.
  58          upgrade_mod_savepoint(true, 2014072400, 'assign');
  59      }
  60      if ($oldversion < 2014072401) {
  61  
  62           // Define index latestattempt (not unique) to be added to assign_submission.
  63          $table = new xmldb_table('assign_submission');
  64          $index = new xmldb_index('latestattempt', XMLDB_INDEX_NOTUNIQUE, array('assignment', 'userid', 'groupid', 'latest'));
  65  
  66          // Conditionally launch add index latestattempt.
  67          if (!$dbman->index_exists($table, $index)) {
  68              $dbman->add_index($table, $index);
  69          }
  70  
  71          // Assign savepoint reached.
  72          upgrade_mod_savepoint(true, 2014072401, 'assign');
  73      }
  74      if ($oldversion < 2014072405) {
  75  
  76          // Prevent running this multiple times.
  77  
  78          $countsql = 'SELECT COUNT(id) FROM {assign_submission} WHERE latest = ?';
  79  
  80          $count = $DB->count_records_sql($countsql, array(1));
  81          if ($count == 0) {
  82  
  83              // Mark the latest attempt for every submission in mod_assign.
  84              $maxattemptsql = 'SELECT assignment, userid, groupid, max(attemptnumber) AS maxattempt
  85                                  FROM {assign_submission}
  86                              GROUP BY assignment, groupid, userid';
  87  
  88              $maxattemptidssql = 'SELECT souter.id
  89                                     FROM {assign_submission} souter
  90                                     JOIN (' . $maxattemptsql . ') sinner
  91                                       ON souter.assignment = sinner.assignment
  92                                      AND souter.userid = sinner.userid
  93                                      AND souter.groupid = sinner.groupid
  94                                      AND souter.attemptnumber = sinner.maxattempt';
  95  
  96              // We need to avoid using "WHERE ... IN(SELECT ...)" clause with MySQL for performance reason.
  97              // TODO MDL-29589 Remove this dbfamily exception when implemented.
  98              if ($DB->get_dbfamily() === 'mysql') {
  99                  $params = array('latest' => 1);
 100                  $sql = 'UPDATE {assign_submission}
 101                      INNER JOIN (' . $maxattemptidssql . ') souterouter ON souterouter.id = {assign_submission}.id
 102                             SET latest = :latest';
 103                  $DB->execute($sql, $params);
 104              } else {
 105                  $select = 'id IN(' . $maxattemptidssql . ')';
 106                  $DB->set_field_select('assign_submission', 'latest', 1, $select);
 107              }
 108  
 109              // Look for grade records with no submission record.
 110              // This is when a teacher has marked a student before they submitted anything.
 111              $records = $DB->get_records_sql('SELECT g.id, g.assignment, g.userid
 112                                                 FROM {assign_grades} g
 113                                            LEFT JOIN {assign_submission} s
 114                                                   ON s.assignment = g.assignment
 115                                                  AND s.userid = g.userid
 116                                                WHERE s.id IS NULL');
 117              $submissions = array();
 118              foreach ($records as $record) {
 119                  $submission = new stdClass();
 120                  $submission->assignment = $record->assignment;
 121                  $submission->userid = $record->userid;
 122                  $submission->status = 'new';
 123                  $submission->groupid = 0;
 124                  $submission->latest = 1;
 125                  $submission->timecreated = time();
 126                  $submission->timemodified = time();
 127                  array_push($submissions, $submission);
 128              }
 129  
 130              $DB->insert_records('assign_submission', $submissions);
 131          }
 132  
 133          // Assign savepoint reached.
 134          upgrade_mod_savepoint(true, 2014072405, 'assign');
 135      }
 136  
 137      // Moodle v2.8.0 release upgrade line.
 138      // Put any upgrade step following this.
 139  
 140      if ($oldversion < 2014122600) {
 141          // Delete any entries from the assign_user_flags and assign_user_mapping that are no longer required.
 142          if ($DB->get_dbfamily() === 'mysql') {
 143              $sql1 = "DELETE {assign_user_flags}
 144                         FROM {assign_user_flags}
 145                    LEFT JOIN {assign}
 146                           ON {assign_user_flags}.assignment = {assign}.id
 147                        WHERE {assign}.id IS NULL";
 148  
 149              $sql2 = "DELETE {assign_user_mapping}
 150                         FROM {assign_user_mapping}
 151                    LEFT JOIN {assign}
 152                           ON {assign_user_mapping}.assignment = {assign}.id
 153                        WHERE {assign}.id IS NULL";
 154          } else {
 155              $sql1 = "DELETE FROM {assign_user_flags}
 156                  WHERE NOT EXISTS (
 157                            SELECT 'x' FROM {assign}
 158                             WHERE {assign_user_flags}.assignment = {assign}.id)";
 159  
 160              $sql2 = "DELETE FROM {assign_user_mapping}
 161                  WHERE NOT EXISTS (
 162                            SELECT 'x' FROM {assign}
 163                             WHERE {assign_user_mapping}.assignment = {assign}.id)";
 164          }
 165  
 166          $DB->execute($sql1);
 167          $DB->execute($sql2);
 168  
 169          upgrade_mod_savepoint(true, 2014122600, 'assign');
 170      }
 171  
 172      if ($oldversion < 2015022300) {
 173  
 174          // Define field preventsubmissionnotingroup to be added to assign.
 175          $table = new xmldb_table('assign');
 176          $field = new xmldb_field('preventsubmissionnotingroup',
 177              XMLDB_TYPE_INTEGER,
 178              '2',
 179              null,
 180              XMLDB_NOTNULL,
 181              null,
 182              '0',
 183              'sendstudentnotifications');
 184  
 185          // Conditionally launch add field preventsubmissionnotingroup.
 186          if (!$dbman->field_exists($table, $field)) {
 187              $dbman->add_field($table, $field);
 188          }
 189  
 190          // Assign savepoint reached.
 191          upgrade_mod_savepoint(true, 2015022300, 'assign');
 192      }
 193  
 194      // Moodle v2.9.0 release upgrade line.
 195      // Put any upgrade step following this.
 196  
 197      // Moodle v3.0.0 release upgrade line.
 198      // Put any upgrade step following this.
 199  
 200      // Moodle v3.1.0 release upgrade line.
 201      // Put any upgrade step following this.
 202  
 203      return true;
 204  }


Generated: Thu Aug 11 10:00:09 2016 Cross-referenced by PHPXref 0.7.1