[ Index ]

PHP Cross Reference of moodle-2.8

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  /**
  26   * upgrade this assignment instance - this function could be skipped but it will be needed later
  27   * @param int $oldversion The old version of the assign module
  28   * @return bool
  29   */
  30  function xmldb_assign_upgrade($oldversion) {
  31      global $CFG, $DB;
  32  
  33      $dbman = $DB->get_manager();
  34  
  35      if ($oldversion < 2012051700) {
  36  
  37          // Define field to be added to assign.
  38          $table = new xmldb_table('assign');
  39          $field = new xmldb_field('sendlatenotifications', XMLDB_TYPE_INTEGER, '2', null,
  40                                   XMLDB_NOTNULL, null, '0', 'sendnotifications');
  41  
  42          // Conditionally launch add field.
  43          if (!$dbman->field_exists($table, $field)) {
  44              $dbman->add_field($table, $field);
  45          }
  46  
  47          // Assign savepoint reached.
  48          upgrade_mod_savepoint(true, 2012051700, 'assign');
  49      }
  50  
  51      // Moodle v2.3.0 release upgrade line.
  52      // Put any upgrade step following this.
  53  
  54      if ($oldversion < 2012071800) {
  55  
  56          // Define field requiresubmissionstatement to be added to assign.
  57          $table = new xmldb_table('assign');
  58          $field = new xmldb_field('requiresubmissionstatement', XMLDB_TYPE_INTEGER, '2', null,
  59                                   XMLDB_NOTNULL, null, '0', 'timemodified');
  60  
  61          // Conditionally launch add field requiresubmissionstatement.
  62  
  63          if (!$dbman->field_exists($table, $field)) {
  64              $dbman->add_field($table, $field);
  65          }
  66  
  67          // Assign savepoint reached.
  68          upgrade_mod_savepoint(true, 2012071800, 'assign');
  69      }
  70  
  71      if ($oldversion < 2012081600) {
  72  
  73          // Define field to be added to assign.
  74          $table = new xmldb_table('assign');
  75          $field = new xmldb_field('completionsubmit', XMLDB_TYPE_INTEGER, '2', null,
  76                                   XMLDB_NOTNULL, null, '0', 'timemodified');
  77  
  78          // Conditionally launch add field.
  79          if (!$dbman->field_exists($table, $field)) {
  80              $dbman->add_field($table, $field);
  81          }
  82  
  83          // Assign savepoint reached.
  84          upgrade_mod_savepoint(true, 2012081600, 'assign');
  85      }
  86  
  87      // Individual extension dates support.
  88      if ($oldversion < 2012082100) {
  89  
  90          // Define field cutoffdate to be added to assign.
  91          $table = new xmldb_table('assign');
  92          $field = new xmldb_field('cutoffdate', XMLDB_TYPE_INTEGER, '10', null,
  93                                   XMLDB_NOTNULL, null, '0', 'completionsubmit');
  94  
  95          // Conditionally launch add field cutoffdate.
  96          if (!$dbman->field_exists($table, $field)) {
  97              $dbman->add_field($table, $field);
  98          }
  99          // If prevent late is on - set cutoffdate to due date.
 100  
 101          // Now remove the preventlatesubmissions column.
 102          $field = new xmldb_field('preventlatesubmissions', XMLDB_TYPE_INTEGER, '2', null,
 103                                   XMLDB_NOTNULL, null, '0', 'nosubmissions');
 104          if ($dbman->field_exists($table, $field)) {
 105              // Set the cutoffdate to the duedate if preventlatesubmissions was enabled.
 106              $sql = 'UPDATE {assign} SET cutoffdate = duedate WHERE preventlatesubmissions = 1';
 107              $DB->execute($sql);
 108  
 109              $dbman->drop_field($table, $field);
 110          }
 111  
 112          // Define field extensionduedate to be added to assign_grades.
 113          $table = new xmldb_table('assign_grades');
 114          $field = new xmldb_field('extensionduedate', XMLDB_TYPE_INTEGER, '10', null,
 115                                   XMLDB_NOTNULL, null, '0', 'mailed');
 116  
 117          // Conditionally launch add field extensionduedate.
 118          if (!$dbman->field_exists($table, $field)) {
 119              $dbman->add_field($table, $field);
 120          }
 121  
 122          // Assign savepoint reached.
 123          upgrade_mod_savepoint(true, 2012082100, 'assign');
 124      }
 125  
 126      // Team assignment support.
 127      if ($oldversion < 2012082300) {
 128  
 129          // Define field to be added to assign.
 130          $table = new xmldb_table('assign');
 131          $field = new xmldb_field('teamsubmission', XMLDB_TYPE_INTEGER, '2', null,
 132                                   XMLDB_NOTNULL, null, '0', 'cutoffdate');
 133  
 134          // Conditionally launch add field.
 135          if (!$dbman->field_exists($table, $field)) {
 136              $dbman->add_field($table, $field);
 137          }
 138          $field = new xmldb_field('requireallteammemberssubmit', XMLDB_TYPE_INTEGER, '2', null,
 139                                   XMLDB_NOTNULL, null, '0', 'teamsubmission');
 140          // Conditionally launch add field.
 141          if (!$dbman->field_exists($table, $field)) {
 142              $dbman->add_field($table, $field);
 143          }
 144          $field = new xmldb_field('teamsubmissiongroupingid', XMLDB_TYPE_INTEGER, '10', null,
 145                                   XMLDB_NOTNULL, null, '0', 'requireallteammemberssubmit');
 146          // Conditionally launch add field.
 147          if (!$dbman->field_exists($table, $field)) {
 148              $dbman->add_field($table, $field);
 149          }
 150          $index = new xmldb_index('teamsubmissiongroupingid',
 151                                   XMLDB_INDEX_NOTUNIQUE,
 152                                   array('teamsubmissiongroupingid'));
 153          // Conditionally launch add index.
 154          if (!$dbman->index_exists($table, $index)) {
 155              $dbman->add_index($table, $index);
 156          }
 157          $table = new xmldb_table('assign_submission');
 158          $field = new xmldb_field('groupid', XMLDB_TYPE_INTEGER, '10', null,
 159                                   XMLDB_NOTNULL, null, '0', 'status');
 160          // Conditionally launch add field.
 161          if (!$dbman->field_exists($table, $field)) {
 162              $dbman->add_field($table, $field);
 163          }
 164          upgrade_mod_savepoint(true, 2012082300, 'assign');
 165      }
 166      if ($oldversion < 2012082400) {
 167  
 168          // Define table assign_user_mapping to be created.
 169          $table = new xmldb_table('assign_user_mapping');
 170  
 171          // Adding fields to table assign_user_mapping.
 172          $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
 173          $table->add_field('assignment', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0');
 174          $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0');
 175  
 176          // Adding keys to table assign_user_mapping.
 177          $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
 178          $table->add_key('assignment', XMLDB_KEY_FOREIGN, array('assignment'), 'assign', array('id'));
 179          $table->add_key('user', XMLDB_KEY_FOREIGN, array('userid'), 'user', array('id'));
 180  
 181          // Conditionally launch create table for assign_user_mapping.
 182          if (!$dbman->table_exists($table)) {
 183              $dbman->create_table($table);
 184          }
 185  
 186          // Define field blindmarking to be added to assign.
 187          $table = new xmldb_table('assign');
 188          $field = new xmldb_field('blindmarking', XMLDB_TYPE_INTEGER, '2', null,
 189                                   XMLDB_NOTNULL, null, '0', 'teamsubmissiongroupingid');
 190  
 191          if (!$dbman->field_exists($table, $field)) {
 192              $dbman->add_field($table, $field);
 193          }
 194  
 195          // Define field revealidentities to be added to assign.
 196          $table = new xmldb_table('assign');
 197          $field = new xmldb_field('revealidentities', XMLDB_TYPE_INTEGER, '2', null,
 198                                   XMLDB_NOTNULL, null, '0', 'blindmarking');
 199  
 200          if (!$dbman->field_exists($table, $field)) {
 201              $dbman->add_field($table, $field);
 202          }
 203  
 204          // Assignment savepoint reached.
 205          upgrade_mod_savepoint(true, 2012082400, 'assign');
 206      }
 207  
 208      // Moodle v2.4.0 release upgrade line.
 209      // Put any upgrade step following this.
 210  
 211      if ($oldversion < 2013030600) {
 212          upgrade_set_timeout(60*20);
 213  
 214          // Some assignments (upgraded from 2.2 assignment) have duplicate entries in the assign_submission
 215          // and assign_grades tables for a single user. This needs to be cleaned up before we can add the unique indexes
 216          // below.
 217  
 218          // Only do this cleanup if the attempt number field has not been added to the table yet.
 219          $table = new xmldb_table('assign_submission');
 220          $field = new xmldb_field('attemptnumber', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0', 'groupid');
 221          if (!$dbman->field_exists($table, $field)) {
 222              // OK safe to cleanup duplicates here.
 223  
 224              $sql = 'SELECT assignment, userid, groupid from {assign_submission} ' .
 225                     'GROUP BY assignment, userid, groupid HAVING (count(id) > 1)';
 226              $badrecords = $DB->get_recordset_sql($sql);
 227  
 228              foreach ($badrecords as $badrecord) {
 229                  $params = array('userid'=>$badrecord->userid,
 230                                  'groupid'=>$badrecord->groupid,
 231                                  'assignment'=>$badrecord->assignment);
 232                  $duplicates = $DB->get_records('assign_submission', $params, 'timemodified DESC', 'id, timemodified');
 233                  if ($duplicates) {
 234                      // Take the first (last updated) entry out of the list so it doesn't get deleted.
 235                      $valid = array_shift($duplicates);
 236                      $deleteids = array();
 237                      foreach ($duplicates as $duplicate) {
 238                          $deleteids[] = $duplicate->id;
 239                      }
 240  
 241                      list($sqlids, $sqlidparams) = $DB->get_in_or_equal($deleteids);
 242                      $DB->delete_records_select('assign_submission', 'id ' . $sqlids, $sqlidparams);
 243                  }
 244              }
 245  
 246              $badrecords->close();
 247          }
 248  
 249          // Same cleanup required for assign_grades
 250          // Only do this cleanup if the attempt number field has not been added to the table yet.
 251          $table = new xmldb_table('assign_grades');
 252          $field = new xmldb_field('attemptnumber', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0', 'grade');
 253          if (!$dbman->field_exists($table, $field)) {
 254              // OK safe to cleanup duplicates here.
 255  
 256              $sql = 'SELECT assignment, userid from {assign_grades} GROUP BY assignment, userid HAVING (count(id) > 1)';
 257              $badrecords = $DB->get_recordset_sql($sql);
 258  
 259              foreach ($badrecords as $badrecord) {
 260                  $params = array('userid'=>$badrecord->userid,
 261                                  'assignment'=>$badrecord->assignment);
 262                  $duplicates = $DB->get_records('assign_grades', $params, 'timemodified DESC', 'id, timemodified');
 263                  if ($duplicates) {
 264                      // Take the first (last updated) entry out of the list so it doesn't get deleted.
 265                      $valid = array_shift($duplicates);
 266                      $deleteids = array();
 267                      foreach ($duplicates as $duplicate) {
 268                          $deleteids[] = $duplicate->id;
 269                      }
 270  
 271                      list($sqlids, $sqlidparams) = $DB->get_in_or_equal($deleteids);
 272                      $DB->delete_records_select('assign_grades', 'id ' . $sqlids, $sqlidparams);
 273                  }
 274              }
 275  
 276              $badrecords->close();
 277          }
 278  
 279          // Define table assign_user_flags to be created.
 280          $table = new xmldb_table('assign_user_flags');
 281  
 282          // Adding fields to table assign_user_flags.
 283          $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
 284          $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0');
 285          $table->add_field('assignment', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0');
 286          $table->add_field('locked', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0');
 287          $table->add_field('mailed', XMLDB_TYPE_INTEGER, '4', null, XMLDB_NOTNULL, null, '0');
 288          $table->add_field('extensionduedate', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0');
 289  
 290          // Adding keys to table assign_user_flags.
 291          $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
 292          $table->add_key('userid', XMLDB_KEY_FOREIGN, array('userid'), 'user', array('id'));
 293          $table->add_key('assignment', XMLDB_KEY_FOREIGN, array('assignment'), 'assign', array('id'));
 294  
 295          // Adding indexes to table assign_user_flags.
 296          $table->add_index('mailed', XMLDB_INDEX_NOTUNIQUE, array('mailed'));
 297  
 298          // Conditionally launch create table for assign_user_flags.
 299          if (!$dbman->table_exists($table)) {
 300              $dbman->create_table($table);
 301  
 302              // Copy the flags from the old table to the new one.
 303              $sql = 'INSERT INTO {assign_user_flags}
 304                          (assignment, userid, locked, mailed, extensionduedate)
 305                      SELECT assignment, userid, locked, mailed, extensionduedate
 306                      FROM {assign_grades}';
 307              $DB->execute($sql);
 308          }
 309  
 310          // And delete the old columns.
 311          // Define index mailed (not unique) to be dropped form assign_grades.
 312          $table = new xmldb_table('assign_grades');
 313          $index = new xmldb_index('mailed', XMLDB_INDEX_NOTUNIQUE, array('mailed'));
 314  
 315          // Conditionally launch drop index mailed.
 316          if ($dbman->index_exists($table, $index)) {
 317              $dbman->drop_index($table, $index);
 318          }
 319  
 320          // Define field locked to be dropped from assign_grades.
 321          $table = new xmldb_table('assign_grades');
 322          $field = new xmldb_field('locked');
 323  
 324          // Conditionally launch drop field locked.
 325          if ($dbman->field_exists($table, $field)) {
 326              $dbman->drop_field($table, $field);
 327          }
 328  
 329          // Define field mailed to be dropped from assign_grades.
 330          $table = new xmldb_table('assign_grades');
 331          $field = new xmldb_field('mailed');
 332  
 333          // Conditionally launch drop field mailed.
 334          if ($dbman->field_exists($table, $field)) {
 335              $dbman->drop_field($table, $field);
 336          }
 337  
 338          // Define field extensionduedate to be dropped from assign_grades.
 339          $table = new xmldb_table('assign_grades');
 340          $field = new xmldb_field('extensionduedate');
 341  
 342          // Conditionally launch drop field extensionduedate.
 343          if ($dbman->field_exists($table, $field)) {
 344              $dbman->drop_field($table, $field);
 345          }
 346  
 347          // Define field attemptreopenmethod to be added to assign.
 348          $table = new xmldb_table('assign');
 349          $field = new xmldb_field('attemptreopenmethod', XMLDB_TYPE_CHAR, '10', null,
 350                                   XMLDB_NOTNULL, null, 'none', 'revealidentities');
 351  
 352          // Conditionally launch add field attemptreopenmethod.
 353          if (!$dbman->field_exists($table, $field)) {
 354              $dbman->add_field($table, $field);
 355          }
 356  
 357          // Define field maxattempts to be added to assign.
 358          $table = new xmldb_table('assign');
 359          $field = new xmldb_field('maxattempts', XMLDB_TYPE_INTEGER, '6', null, XMLDB_NOTNULL, null, '-1', 'attemptreopenmethod');
 360  
 361          // Conditionally launch add field maxattempts.
 362          if (!$dbman->field_exists($table, $field)) {
 363              $dbman->add_field($table, $field);
 364          }
 365  
 366          // Define field attemptnumber to be added to assign_submission.
 367          $table = new xmldb_table('assign_submission');
 368          $field = new xmldb_field('attemptnumber', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0', 'groupid');
 369  
 370          // Conditionally launch add field attemptnumber.
 371          if (!$dbman->field_exists($table, $field)) {
 372              $dbman->add_field($table, $field);
 373          }
 374  
 375          // Define index attemptnumber (not unique) to be added to assign_submission.
 376          $table = new xmldb_table('assign_submission');
 377          $index = new xmldb_index('attemptnumber', XMLDB_INDEX_NOTUNIQUE, array('attemptnumber'));
 378          // Conditionally launch add index attemptnumber.
 379          if (!$dbman->index_exists($table, $index)) {
 380              $dbman->add_index($table, $index);
 381          }
 382  
 383          // Define field attemptnumber to be added to assign_grades.
 384          $table = new xmldb_table('assign_grades');
 385          $field = new xmldb_field('attemptnumber', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0', 'grade');
 386  
 387          // Conditionally launch add field attemptnumber.
 388          if (!$dbman->field_exists($table, $field)) {
 389              $dbman->add_field($table, $field);
 390          }
 391  
 392          // Define index attemptnumber (not unique) to be added to assign_grades.
 393          $table = new xmldb_table('assign_grades');
 394          $index = new xmldb_index('attemptnumber', XMLDB_INDEX_NOTUNIQUE, array('attemptnumber'));
 395  
 396          // Conditionally launch add index attemptnumber.
 397          if (!$dbman->index_exists($table, $index)) {
 398              $dbman->add_index($table, $index);
 399          }
 400  
 401          // Define index uniqueattemptsubmission (unique) to be added to assign_submission.
 402          $table = new xmldb_table('assign_submission');
 403          $index = new xmldb_index('uniqueattemptsubmission',
 404                                   XMLDB_INDEX_UNIQUE,
 405                                   array('assignment', 'userid', 'groupid', 'attemptnumber'));
 406  
 407          // Conditionally launch add index uniqueattempt.
 408          if (!$dbman->index_exists($table, $index)) {
 409              $dbman->add_index($table, $index);
 410          }
 411  
 412          // Define index uniqueattemptgrade (unique) to be added to assign_grades.
 413          $table = new xmldb_table('assign_grades');
 414          $index = new xmldb_index('uniqueattemptgrade', XMLDB_INDEX_UNIQUE, array('assignment', 'userid', 'attemptnumber'));
 415  
 416          // Conditionally launch add index uniqueattempt.
 417          if (!$dbman->index_exists($table, $index)) {
 418              $dbman->add_index($table, $index);
 419          }
 420  
 421          // Module assign savepoint reached.
 422          upgrade_mod_savepoint(true, 2013030600, 'assign');
 423      }
 424  
 425      // Moodle v2.5.0 release upgrade line.
 426      // Put any upgrade step following this.
 427  
 428      if ($oldversion < 2013061101) {
 429          // Define field markingworkflow to be added to assign.
 430          $table = new xmldb_table('assign');
 431          $field = new xmldb_field('markingworkflow', XMLDB_TYPE_INTEGER, '2', null, XMLDB_NOTNULL, null, '0', 'maxattempts');
 432  
 433          // Conditionally launch add field markingworkflow.
 434          if (!$dbman->field_exists($table, $field)) {
 435              $dbman->add_field($table, $field);
 436          }
 437  
 438          // Define field markingallocation to be added to assign.
 439          $field = new xmldb_field('markingallocation', XMLDB_TYPE_INTEGER, '2', null, XMLDB_NOTNULL, null, '0', 'markingworkflow');
 440          if (!$dbman->field_exists($table, $field)) {
 441              $dbman->add_field($table, $field);
 442          }
 443  
 444          // Define field workflowstate to be added to assign_grades.
 445          $table = new xmldb_table('assign_user_flags');
 446          $field = new xmldb_field('workflowstate', XMLDB_TYPE_CHAR, '20', null, null, null, null, 'extensionduedate');
 447  
 448          // Conditionally launch add field workflowstate.
 449          if (!$dbman->field_exists($table, $field)) {
 450              $dbman->add_field($table, $field);
 451          }
 452  
 453          // Define field allocatedmarker to be added to assign_grades.
 454          $field = new xmldb_field('allocatedmarker', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0', 'workflowstate');
 455          // Conditionally launch add field workflowstate.
 456          if (!$dbman->field_exists($table, $field)) {
 457              $dbman->add_field($table, $field);
 458          }
 459          upgrade_mod_savepoint(true, 2013061101, 'assign');
 460      }
 461  
 462      // Moodle v2.6.0 release upgrade line.
 463      // Put any upgrade step following this.
 464  
 465      if ($oldversion < 2014010801) {
 466  
 467          // Define field sendstudentnotifications to be added to assign.
 468          $table = new xmldb_table('assign');
 469          $field = new xmldb_field('sendstudentnotifications',
 470                                   XMLDB_TYPE_INTEGER,
 471                                   '2',
 472                                   null,
 473                                   XMLDB_NOTNULL,
 474                                   null,
 475                                   '1',
 476                                   'markingallocation');
 477  
 478          // Conditionally launch add field sendstudentnotifications.
 479          if (!$dbman->field_exists($table, $field)) {
 480              $dbman->add_field($table, $field);
 481          }
 482  
 483          // Assign savepoint reached.
 484          upgrade_mod_savepoint(true, 2014010801, 'assign');
 485      }
 486  
 487      // Moodle v2.7.0 release upgrade line.
 488      // Put any upgrade step following this.
 489  
 490      if ($oldversion < 2014051201) {
 491  
 492          // Cleanup bad database records where assignid is missing.
 493  
 494          $DB->delete_records('assign_user_mapping', array('assignment'=>0));
 495          // Assign savepoint reached.
 496          upgrade_mod_savepoint(true, 2014051201, 'assign');
 497      }
 498      if ($oldversion < 2014072400) {
 499  
 500          // Add "latest" column to submissions table to mark the latest attempt.
 501          $table = new xmldb_table('assign_submission');
 502          $field = new xmldb_field('latest', XMLDB_TYPE_INTEGER, '2', null, XMLDB_NOTNULL, null, '0', 'attemptnumber');
 503  
 504          // Conditionally launch add field latest.
 505          if (!$dbman->field_exists($table, $field)) {
 506              $dbman->add_field($table, $field);
 507          }
 508  
 509          // Assign savepoint reached.
 510          upgrade_mod_savepoint(true, 2014072400, 'assign');
 511      }
 512      if ($oldversion < 2014072401) {
 513  
 514           // Define index latestattempt (not unique) to be added to assign_submission.
 515          $table = new xmldb_table('assign_submission');
 516          $index = new xmldb_index('latestattempt', XMLDB_INDEX_NOTUNIQUE, array('assignment', 'userid', 'groupid', 'latest'));
 517  
 518          // Conditionally launch add index latestattempt.
 519          if (!$dbman->index_exists($table, $index)) {
 520              $dbman->add_index($table, $index);
 521          }
 522  
 523          // Assign savepoint reached.
 524          upgrade_mod_savepoint(true, 2014072401, 'assign');
 525      }
 526      if ($oldversion < 2014072405) {
 527  
 528          // Prevent running this multiple times.
 529  
 530          $countsql = 'SELECT COUNT(id) FROM {assign_submission} WHERE latest = ?';
 531  
 532          $count = $DB->count_records_sql($countsql, array(1));
 533          if ($count == 0) {
 534  
 535              // Mark the latest attempt for every submission in mod_assign.
 536              $maxattemptsql = 'SELECT assignment, userid, groupid, max(attemptnumber) AS maxattempt
 537                                  FROM {assign_submission}
 538                              GROUP BY assignment, groupid, userid';
 539  
 540              // Note: souterouter looks redundant below, but it forces
 541              // MySQL to use an in memory table to store the results of the
 542              // inner query. Without this MySQL would complain that the UPDATE
 543              // is operating on the same table as the FROM (which is true).
 544              $maxattemptidssql = 'SELECT souterouter.id FROM (
 545                                      SELECT souter.id
 546                                         FROM {assign_submission} souter
 547                                         JOIN (' . $maxattemptsql . ') sinner
 548                                           ON souter.assignment = sinner.assignment
 549                                          AND souter.userid = sinner.userid
 550                                          AND souter.groupid = sinner.groupid
 551                                          AND souter.attemptnumber = sinner.maxattempt
 552                                  ) souterouter';
 553              $select = 'id IN(' . $maxattemptidssql . ')';
 554              $DB->set_field_select('assign_submission', 'latest', 1, $select);
 555  
 556              // Look for grade records with no submission record.
 557              // This is when a teacher has marked a student before they submitted anything.
 558              $records = $DB->get_records_sql('SELECT g.id, g.assignment, g.userid
 559                                                 FROM {assign_grades} g
 560                                            LEFT JOIN {assign_submission} s
 561                                                   ON s.assignment = g.assignment
 562                                                  AND s.userid = g.userid
 563                                                WHERE s.id IS NULL');
 564              $submissions = array();
 565              foreach ($records as $record) {
 566                  $submission = new stdClass();
 567                  $submission->assignment = $record->assignment;
 568                  $submission->userid = $record->userid;
 569                  $submission->status = 'new';
 570                  $submission->groupid = 0;
 571                  $submission->latest = 1;
 572                  $submission->timecreated = time();
 573                  $submission->timemodified = time();
 574                  array_push($submissions, $submission);
 575              }
 576  
 577              $DB->insert_records('assign_submission', $submissions);
 578          }
 579  
 580          // Assign savepoint reached.
 581          upgrade_mod_savepoint(true, 2014072405, 'assign');
 582      }
 583  
 584      // Moodle v2.8.0 release upgrade line.
 585      // Put any upgrade step following this.
 586  
 587      return true;
 588  }


Generated: Fri Nov 28 20:29:05 2014 Cross-referenced by PHPXref 0.7.1