Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.
   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   * This file contains the upgrade code to upgrade from mod_assignment to mod_assign
  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  require_once($CFG->dirroot.'/mod/assign/locallib.php');
  28  require_once($CFG->libdir.'/accesslib.php');
  29  require_once($CFG->dirroot.'/course/lib.php');
  30  
  31  /*
  32   * The maximum amount of time to spend upgrading a single assignment.
  33   * This is intentionally generous (5 mins) as the effect of a timeout
  34   * for a legitimate upgrade would be quite harsh (roll back code will not run)
  35   */
  36  define('ASSIGN_MAX_UPGRADE_TIME_SECS', 300);
  37  
  38  /**
  39   * Class to manage upgrades from mod_assignment to mod_assign
  40   *
  41   * @package   mod_assign
  42   * @copyright 2012 NetSpot {@link http://www.netspot.com.au}
  43   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  44   */
  45  class assign_upgrade_manager {
  46  
  47      /**
  48       * This function converts all of the base settings for an instance of
  49       * the old assignment to the new format. Then it calls each of the plugins
  50       * to see if they can help upgrade this assignment.
  51       * @param int $oldassignmentid (don't rely on the old assignment type even being installed)
  52       * @param string $log This string gets appended to during the conversion process
  53       * @return bool true or false
  54       */
  55      public function upgrade_assignment($oldassignmentid, & $log) {
  56          global $DB, $CFG, $USER;
  57          // Steps to upgrade an assignment.
  58  
  59          core_php_time_limit::raise(ASSIGN_MAX_UPGRADE_TIME_SECS);
  60  
  61          // Get the module details.
  62          $oldmodule = $DB->get_record('modules', array('name'=>'assignment'), '*', MUST_EXIST);
  63          $params = array('module'=>$oldmodule->id, 'instance'=>$oldassignmentid);
  64          $oldcoursemodule = $DB->get_record('course_modules',
  65                                             $params,
  66                                             '*',
  67                                             MUST_EXIST);
  68          $oldcontext = context_module::instance($oldcoursemodule->id);
  69          // We used to check for admin capability, but since Moodle 2.7 this is called
  70          // during restore of a mod_assignment module.
  71          // Also note that we do not check for any mod_assignment capabilities, because they can
  72          // be removed so that users don't add new instances of the broken old thing.
  73          if (!has_capability('mod/assign:addinstance', $oldcontext)) {
  74              $log = get_string('couldnotcreatenewassignmentinstance', 'mod_assign');
  75              return false;
  76          }
  77  
  78          // First insert an assign instance to get the id.
  79          $oldassignment = $DB->get_record('assignment', array('id'=>$oldassignmentid), '*', MUST_EXIST);
  80  
  81          $oldversion = get_config('assignment_' . $oldassignment->assignmenttype, 'version');
  82  
  83          $data = new stdClass();
  84          $data->course = $oldassignment->course;
  85          $data->name = $oldassignment->name;
  86          $data->intro = $oldassignment->intro;
  87          $data->introformat = $oldassignment->introformat;
  88          $data->alwaysshowdescription = 1;
  89          $data->sendnotifications = $oldassignment->emailteachers;
  90          $data->sendlatenotifications = $oldassignment->emailteachers;
  91          $data->duedate = $oldassignment->timedue;
  92          $data->allowsubmissionsfromdate = $oldassignment->timeavailable;
  93          $data->grade = $oldassignment->grade;
  94          $data->submissiondrafts = $oldassignment->resubmit;
  95          $data->requiresubmissionstatement = 0;
  96          $data->markingworkflow = 0;
  97          $data->markingallocation = 0;
  98          $data->cutoffdate = 0;
  99          $data->gradingduedate = 0;
 100          // New way to specify no late submissions.
 101          if ($oldassignment->preventlate) {
 102              $data->cutoffdate = $data->duedate;
 103          }
 104          $data->teamsubmission = 0;
 105          $data->requireallteammemberssubmit = 0;
 106          $data->teamsubmissiongroupingid = 0;
 107          $data->blindmarking = 0;
 108          $data->attemptreopenmethod = 'none';
 109          $data->maxattempts = ASSIGN_UNLIMITED_ATTEMPTS;
 110  
 111          $newassignment = new assign(null, null, null);
 112  
 113          if (!$newassignment->add_instance($data, false)) {
 114              $log = get_string('couldnotcreatenewassignmentinstance', 'mod_assign');
 115              return false;
 116          }
 117  
 118          // Now create a new coursemodule from the old one.
 119          $newmodule = $DB->get_record('modules', array('name'=>'assign'), '*', MUST_EXIST);
 120          $newcoursemodule = $this->duplicate_course_module($oldcoursemodule,
 121                                                            $newmodule->id,
 122                                                            $newassignment->get_instance()->id);
 123          if (!$newcoursemodule) {
 124              $log = get_string('couldnotcreatenewcoursemodule', 'mod_assign');
 125              return false;
 126          }
 127  
 128          // Convert the base database tables (assignment, submission, grade).
 129  
 130          // These are used to store information in case a rollback is required.
 131          $gradingarea = null;
 132          $gradingdefinitions = null;
 133          $gradeidmap = array();
 134          $completiondone = false;
 135          $gradesdone = false;
 136  
 137          // From this point we want to rollback on failure.
 138          $rollback = false;
 139          try {
 140              $newassignment->set_context(context_module::instance($newcoursemodule->id));
 141  
 142              // The course module has now been created - time to update the core tables.
 143  
 144              // Copy intro files.
 145              $newassignment->copy_area_files_for_upgrade($oldcontext->id, 'mod_assignment', 'intro', 0,
 146                                              $newassignment->get_context()->id, 'mod_assign', 'intro', 0);
 147  
 148              // Get the plugins to do their bit.
 149              foreach ($newassignment->get_submission_plugins() as $plugin) {
 150                  if ($plugin->can_upgrade($oldassignment->assignmenttype, $oldversion)) {
 151                      $plugin->enable();
 152                      if (!$plugin->upgrade_settings($oldcontext, $oldassignment, $log)) {
 153                          $rollback = true;
 154                      }
 155                  } else {
 156                      $plugin->disable();
 157                  }
 158              }
 159              foreach ($newassignment->get_feedback_plugins() as $plugin) {
 160                  if ($plugin->can_upgrade($oldassignment->assignmenttype, $oldversion)) {
 161                      $plugin->enable();
 162                      if (!$plugin->upgrade_settings($oldcontext, $oldassignment, $log)) {
 163                          $rollback = true;
 164                      }
 165                  } else {
 166                      $plugin->disable();
 167                  }
 168              }
 169  
 170              // See if there is advanced grading upgrades required.
 171              $gradingarea = $DB->get_record('grading_areas',
 172                                             array('contextid'=>$oldcontext->id, 'areaname'=>'submission'),
 173                                             '*',
 174                                             IGNORE_MISSING);
 175              if ($gradingarea) {
 176                  $params = array('id'=>$gradingarea->id,
 177                                  'contextid'=>$newassignment->get_context()->id,
 178                                  'component'=>'mod_assign',
 179                                  'areaname'=>'submissions');
 180                  $DB->update_record('grading_areas', $params);
 181                  $gradingdefinitions = $DB->get_records('grading_definitions',
 182                                                         array('areaid'=>$gradingarea->id));
 183              }
 184  
 185              // Upgrade availability data.
 186              \core_availability\info::update_dependency_id_across_course(
 187                      $newcoursemodule->course, 'course_modules', $oldcoursemodule->id, $newcoursemodule->id);
 188  
 189              // Upgrade completion data.
 190              $DB->set_field('course_modules_completion',
 191                             'coursemoduleid',
 192                             $newcoursemodule->id,
 193                             array('coursemoduleid'=>$oldcoursemodule->id));
 194              $allcriteria = $DB->get_records('course_completion_criteria',
 195                                              array('moduleinstance'=>$oldcoursemodule->id));
 196              foreach ($allcriteria as $criteria) {
 197                  $criteria->module = 'assign';
 198                  $criteria->moduleinstance = $newcoursemodule->id;
 199                  $DB->update_record('course_completion_criteria', $criteria);
 200              }
 201              $completiondone = true;
 202  
 203              // Migrate log entries so we don't lose them.
 204              $logparams = array('cmid' => $oldcoursemodule->id, 'course' => $oldcoursemodule->course);
 205              $DB->set_field('log', 'module', 'assign', $logparams);
 206              $DB->set_field('log', 'cmid', $newcoursemodule->id, $logparams);
 207  
 208              // Copy all the submission data (and get plugins to do their bit).
 209              $oldsubmissions = $DB->get_records('assignment_submissions',
 210                                                 array('assignment'=>$oldassignmentid));
 211  
 212              foreach ($oldsubmissions as $oldsubmission) {
 213                  $submission = new stdClass();
 214                  $submission->assignment = $newassignment->get_instance()->id;
 215                  $submission->userid = $oldsubmission->userid;
 216                  $submission->timecreated = $oldsubmission->timecreated;
 217                  $submission->timemodified = $oldsubmission->timemodified;
 218                  $submission->status = ASSIGN_SUBMISSION_STATUS_SUBMITTED;
 219                  // Because in mod_assignment there could only be one submission per student, it is always the latest.
 220                  $submission->latest = 1;
 221                  $submission->id = $DB->insert_record('assign_submission', $submission);
 222                  if (!$submission->id) {
 223                      $log .= get_string('couldnotinsertsubmission', 'mod_assign', $submission->userid);
 224                      $rollback = true;
 225                  }
 226                  foreach ($newassignment->get_submission_plugins() as $plugin) {
 227                      if ($plugin->can_upgrade($oldassignment->assignmenttype, $oldversion)) {
 228                          if (!$plugin->upgrade($oldcontext,
 229                                                $oldassignment,
 230                                                $oldsubmission,
 231                                                $submission,
 232                                                $log)) {
 233                              $rollback = true;
 234                          }
 235                      }
 236                  }
 237                  if ($oldsubmission->timemarked) {
 238                      // Submission has been graded - create a grade record.
 239                      $grade = new stdClass();
 240                      $grade->assignment = $newassignment->get_instance()->id;
 241                      $grade->userid = $oldsubmission->userid;
 242                      $grade->grader = $oldsubmission->teacher;
 243                      $grade->timemodified = $oldsubmission->timemarked;
 244                      $grade->timecreated = $oldsubmission->timecreated;
 245                      $grade->grade = $oldsubmission->grade;
 246                      if ($oldsubmission->mailed) {
 247                          // The mailed flag goes in the flags table.
 248                          $flags = new stdClass();
 249                          $flags->userid = $oldsubmission->userid;
 250                          $flags->assignment = $newassignment->get_instance()->id;
 251                          $flags->mailed = 1;
 252                          $DB->insert_record('assign_user_flags', $flags);
 253                      }
 254                      $grade->id = $DB->insert_record('assign_grades', $grade);
 255                      if (!$grade->id) {
 256                          $log .= get_string('couldnotinsertgrade', 'mod_assign', $grade->userid);
 257                          $rollback = true;
 258                      }
 259  
 260                      // Copy any grading instances.
 261                      if ($gradingarea) {
 262  
 263                          $gradeidmap[$grade->id] = $oldsubmission->id;
 264  
 265                          foreach ($gradingdefinitions as $definition) {
 266                              $params = array('definitionid'=>$definition->id,
 267                                              'itemid'=>$oldsubmission->id);
 268                              $DB->set_field('grading_instances', 'itemid', $grade->id, $params);
 269                          }
 270  
 271                      }
 272                      foreach ($newassignment->get_feedback_plugins() as $plugin) {
 273                          if ($plugin->can_upgrade($oldassignment->assignmenttype, $oldversion)) {
 274                              if (!$plugin->upgrade($oldcontext,
 275                                                    $oldassignment,
 276                                                    $oldsubmission,
 277                                                    $grade,
 278                                                    $log)) {
 279                                  $rollback = true;
 280                              }
 281                          }
 282                      }
 283                  }
 284              }
 285  
 286              $newassignment->update_calendar($newcoursemodule->id);
 287  
 288              // Reassociate grade_items from the old assignment instance to the new assign instance.
 289              // This includes outcome linked grade_items.
 290              $params = array('assign', $newassignment->get_instance()->id, 'assignment', $oldassignment->id);
 291              $sql = 'UPDATE {grade_items} SET itemmodule = ?, iteminstance = ? WHERE itemmodule = ? AND iteminstance = ?';
 292              $DB->execute($sql, $params);
 293  
 294              // Create a mapping record to map urls from the old to the new assignment.
 295              $mapping = new stdClass();
 296              $mapping->oldcmid = $oldcoursemodule->id;
 297              $mapping->oldinstance = $oldassignment->id;
 298              $mapping->newcmid = $newcoursemodule->id;
 299              $mapping->newinstance = $newassignment->get_instance()->id;
 300              $mapping->timecreated = time();
 301              $DB->insert_record('assignment_upgrade', $mapping);
 302  
 303              $gradesdone = true;
 304  
 305          } catch (Exception $exception) {
 306              $rollback = true;
 307              $log .= get_string('conversionexception', 'mod_assign', $exception->getMessage());
 308          }
 309  
 310          if ($rollback) {
 311              // Roll back the grades changes.
 312              if ($gradesdone) {
 313                  // Reassociate grade_items from the new assign instance to the old assignment instance.
 314                  $params = array('assignment', $oldassignment->id, 'assign', $newassignment->get_instance()->id);
 315                  $sql = 'UPDATE {grade_items} SET itemmodule = ?, iteminstance = ? WHERE itemmodule = ? AND iteminstance = ?';
 316                  $DB->execute($sql, $params);
 317              }
 318              // Roll back the completion changes.
 319              if ($completiondone) {
 320                  $DB->set_field('course_modules_completion',
 321                                 'coursemoduleid',
 322                                 $oldcoursemodule->id,
 323                                 array('coursemoduleid'=>$newcoursemodule->id));
 324  
 325                  $allcriteria = $DB->get_records('course_completion_criteria',
 326                                                  array('moduleinstance'=>$newcoursemodule->id));
 327                  foreach ($allcriteria as $criteria) {
 328                      $criteria->module = 'assignment';
 329                      $criteria->moduleinstance = $oldcoursemodule->id;
 330                      $DB->update_record('course_completion_criteria', $criteria);
 331                  }
 332              }
 333              // Roll back the log changes.
 334              $logparams = array('cmid' => $newcoursemodule->id, 'course' => $newcoursemodule->course);
 335              $DB->set_field('log', 'module', 'assignment', $logparams);
 336              $DB->set_field('log', 'cmid', $oldcoursemodule->id, $logparams);
 337              // Roll back the advanced grading update.
 338              if ($gradingarea) {
 339                  foreach ($gradeidmap as $newgradeid => $oldsubmissionid) {
 340                      foreach ($gradingdefinitions as $definition) {
 341                          $DB->set_field('grading_instances',
 342                                         'itemid',
 343                                         $oldsubmissionid,
 344                                         array('definitionid'=>$definition->id, 'itemid'=>$newgradeid));
 345                      }
 346                  }
 347                  $params = array('id'=>$gradingarea->id,
 348                                  'contextid'=>$oldcontext->id,
 349                                  'component'=>'mod_assignment',
 350                                  'areaname'=>'submission');
 351                  $DB->update_record('grading_areas', $params);
 352              }
 353              $newassignment->delete_instance();
 354  
 355              return false;
 356          }
 357          // Delete the old assignment (use object delete).
 358          $cm = get_coursemodule_from_id('', $oldcoursemodule->id, $oldcoursemodule->course);
 359          if ($cm) {
 360              course_delete_module($cm->id);
 361          }
 362          rebuild_course_cache($oldcoursemodule->course);
 363          return true;
 364      }
 365  
 366  
 367      /**
 368       * Create a duplicate course module record so we can create the upgraded
 369       * assign module alongside the old assignment module.
 370       *
 371       * @param stdClass $cm The old course module record
 372       * @param int $moduleid The id of the new assign module
 373       * @param int $newinstanceid The id of the new instance of the assign module
 374       * @return mixed stdClass|bool The new course module record or FALSE
 375       */
 376      private function duplicate_course_module(stdClass $cm, $moduleid, $newinstanceid) {
 377          global $DB, $CFG;
 378  
 379          $newcm = new stdClass();
 380          $newcm->course           = $cm->course;
 381          $newcm->module           = $moduleid;
 382          $newcm->instance         = $newinstanceid;
 383          $newcm->visible          = $cm->visible;
 384          $newcm->section          = $cm->section;
 385          $newcm->score            = $cm->score;
 386          $newcm->indent           = $cm->indent;
 387          $newcm->groupmode        = $cm->groupmode;
 388          $newcm->groupingid       = $cm->groupingid;
 389          $newcm->completion                = $cm->completion;
 390          $newcm->completiongradeitemnumber = $cm->completiongradeitemnumber;
 391          $newcm->completionview            = $cm->completionview;
 392          $newcm->completionexpected        = $cm->completionexpected;
 393          if (!empty($CFG->enableavailability)) {
 394              $newcm->availability = $cm->availability;
 395          }
 396          $newcm->showdescription = $cm->showdescription;
 397  
 398          $newcmid = add_course_module($newcm);
 399          $newcm = get_coursemodule_from_id('', $newcmid, $cm->course);
 400          if (!$newcm) {
 401              return false;
 402          }
 403          $section = $DB->get_record("course_sections", array("id"=>$newcm->section));
 404          if (!$section) {
 405              return false;
 406          }
 407  
 408          $newcm->section = course_add_cm_to_section($newcm->course, $newcm->id, $section->section, $cm->id);
 409  
 410          // Make sure visibility is set correctly (in particular in calendar).
 411          // Note: Allow them to set it even without moodle/course:activityvisibility.
 412          set_coursemodule_visible($newcm->id, $newcm->visible);
 413  
 414          return $newcm;
 415      }
 416  }