[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/mod/assign/ -> upgradelib.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   * 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          // New way to specify no late submissions.
 100          if ($oldassignment->preventlate) {
 101              $data->cutoffdate = $data->duedate;
 102          }
 103          $data->teamsubmission = 0;
 104          $data->requireallteammemberssubmit = 0;
 105          $data->teamsubmissiongroupingid = 0;
 106          $data->blindmarking = 0;
 107          $data->attemptreopenmethod = 'none';
 108          $data->maxattempts = ASSIGN_UNLIMITED_ATTEMPTS;
 109  
 110          $newassignment = new assign(null, null, null);
 111  
 112          if (!$newassignment->add_instance($data, false)) {
 113              $log = get_string('couldnotcreatenewassignmentinstance', 'mod_assign');
 114              return false;
 115          }
 116  
 117          // Now create a new coursemodule from the old one.
 118          $newmodule = $DB->get_record('modules', array('name'=>'assign'), '*', MUST_EXIST);
 119          $newcoursemodule = $this->duplicate_course_module($oldcoursemodule,
 120                                                            $newmodule->id,
 121                                                            $newassignment->get_instance()->id);
 122          if (!$newcoursemodule) {
 123              $log = get_string('couldnotcreatenewcoursemodule', 'mod_assign');
 124              return false;
 125          }
 126  
 127          // Convert the base database tables (assignment, submission, grade).
 128  
 129          // These are used to store information in case a rollback is required.
 130          $gradingarea = null;
 131          $gradingdefinitions = null;
 132          $gradeidmap = array();
 133          $completiondone = false;
 134          $gradesdone = false;
 135  
 136          // From this point we want to rollback on failure.
 137          $rollback = false;
 138          try {
 139              $newassignment->set_context(context_module::instance($newcoursemodule->id));
 140  
 141              // The course module has now been created - time to update the core tables.
 142  
 143              // Copy intro files.
 144              $newassignment->copy_area_files_for_upgrade($oldcontext->id, 'mod_assignment', 'intro', 0,
 145                                              $newassignment->get_context()->id, 'mod_assign', 'intro', 0);
 146  
 147              // Get the plugins to do their bit.
 148              foreach ($newassignment->get_submission_plugins() as $plugin) {
 149                  if ($plugin->can_upgrade($oldassignment->assignmenttype, $oldversion)) {
 150                      $plugin->enable();
 151                      if (!$plugin->upgrade_settings($oldcontext, $oldassignment, $log)) {
 152                          $rollback = true;
 153                      }
 154                  } else {
 155                      $plugin->disable();
 156                  }
 157              }
 158              foreach ($newassignment->get_feedback_plugins() as $plugin) {
 159                  if ($plugin->can_upgrade($oldassignment->assignmenttype, $oldversion)) {
 160                      $plugin->enable();
 161                      if (!$plugin->upgrade_settings($oldcontext, $oldassignment, $log)) {
 162                          $rollback = true;
 163                      }
 164                  } else {
 165                      $plugin->disable();
 166                  }
 167              }
 168  
 169              // See if there is advanced grading upgrades required.
 170              $gradingarea = $DB->get_record('grading_areas',
 171                                             array('contextid'=>$oldcontext->id, 'areaname'=>'submission'),
 172                                             '*',
 173                                             IGNORE_MISSING);
 174              if ($gradingarea) {
 175                  $params = array('id'=>$gradingarea->id,
 176                                  'contextid'=>$newassignment->get_context()->id,
 177                                  'component'=>'mod_assign',
 178                                  'areaname'=>'submissions');
 179                  $DB->update_record('grading_areas', $params);
 180                  $gradingdefinitions = $DB->get_records('grading_definitions',
 181                                                         array('areaid'=>$gradingarea->id));
 182              }
 183  
 184              // Upgrade availability data.
 185              \core_availability\info::update_dependency_id_across_course(
 186                      $newcoursemodule->course, 'course_modules', $oldcoursemodule->id, $newcoursemodule->id);
 187  
 188              // Upgrade completion data.
 189              $DB->set_field('course_modules_completion',
 190                             'coursemoduleid',
 191                             $newcoursemodule->id,
 192                             array('coursemoduleid'=>$oldcoursemodule->id));
 193              $allcriteria = $DB->get_records('course_completion_criteria',
 194                                              array('moduleinstance'=>$oldcoursemodule->id));
 195              foreach ($allcriteria as $criteria) {
 196                  $criteria->module = 'assign';
 197                  $criteria->moduleinstance = $newcoursemodule->id;
 198                  $DB->update_record('course_completion_criteria', $criteria);
 199              }
 200              $completiondone = true;
 201  
 202              // Migrate log entries so we don't lose them.
 203              $logparams = array('cmid' => $oldcoursemodule->id, 'course' => $oldcoursemodule->course);
 204              $DB->set_field('log', 'module', 'assign', $logparams);
 205              $DB->set_field('log', 'cmid', $newcoursemodule->id, $logparams);
 206  
 207              // Copy all the submission data (and get plugins to do their bit).
 208              $oldsubmissions = $DB->get_records('assignment_submissions',
 209                                                 array('assignment'=>$oldassignmentid));
 210  
 211              foreach ($oldsubmissions as $oldsubmission) {
 212                  $submission = new stdClass();
 213                  $submission->assignment = $newassignment->get_instance()->id;
 214                  $submission->userid = $oldsubmission->userid;
 215                  $submission->timecreated = $oldsubmission->timecreated;
 216                  $submission->timemodified = $oldsubmission->timemodified;
 217                  $submission->status = ASSIGN_SUBMISSION_STATUS_SUBMITTED;
 218                  // Because in mod_assignment there could only be one submission per student, it is always the latest.
 219                  $submission->latest = 1;
 220                  $submission->id = $DB->insert_record('assign_submission', $submission);
 221                  if (!$submission->id) {
 222                      $log .= get_string('couldnotinsertsubmission', 'mod_assign', $submission->userid);
 223                      $rollback = true;
 224                  }
 225                  foreach ($newassignment->get_submission_plugins() as $plugin) {
 226                      if ($plugin->can_upgrade($oldassignment->assignmenttype, $oldversion)) {
 227                          if (!$plugin->upgrade($oldcontext,
 228                                                $oldassignment,
 229                                                $oldsubmission,
 230                                                $submission,
 231                                                $log)) {
 232                              $rollback = true;
 233                          }
 234                      }
 235                  }
 236                  if ($oldsubmission->timemarked) {
 237                      // Submission has been graded - create a grade record.
 238                      $grade = new stdClass();
 239                      $grade->assignment = $newassignment->get_instance()->id;
 240                      $grade->userid = $oldsubmission->userid;
 241                      $grade->grader = $oldsubmission->teacher;
 242                      $grade->timemodified = $oldsubmission->timemarked;
 243                      $grade->timecreated = $oldsubmission->timecreated;
 244                      $grade->grade = $oldsubmission->grade;
 245                      if ($oldsubmission->mailed) {
 246                          // The mailed flag goes in the flags table.
 247                          $flags = new stdClass();
 248                          $flags->userid = $oldsubmission->userid;
 249                          $flags->assignment = $newassignment->get_instance()->id;
 250                          $flags->mailed = 1;
 251                          $DB->insert_record('assign_user_flags', $flags);
 252                      }
 253                      $grade->id = $DB->insert_record('assign_grades', $grade);
 254                      if (!$grade->id) {
 255                          $log .= get_string('couldnotinsertgrade', 'mod_assign', $grade->userid);
 256                          $rollback = true;
 257                      }
 258  
 259                      // Copy any grading instances.
 260                      if ($gradingarea) {
 261  
 262                          $gradeidmap[$grade->id] = $oldsubmission->id;
 263  
 264                          foreach ($gradingdefinitions as $definition) {
 265                              $params = array('definitionid'=>$definition->id,
 266                                              'itemid'=>$oldsubmission->id);
 267                              $DB->set_field('grading_instances', 'itemid', $grade->id, $params);
 268                          }
 269  
 270                      }
 271                      foreach ($newassignment->get_feedback_plugins() as $plugin) {
 272                          if ($plugin->can_upgrade($oldassignment->assignmenttype, $oldversion)) {
 273                              if (!$plugin->upgrade($oldcontext,
 274                                                    $oldassignment,
 275                                                    $oldsubmission,
 276                                                    $grade,
 277                                                    $log)) {
 278                                  $rollback = true;
 279                              }
 280                          }
 281                      }
 282                  }
 283              }
 284  
 285              $newassignment->update_calendar($newcoursemodule->id);
 286  
 287              // Reassociate grade_items from the old assignment instance to the new assign instance.
 288              // This includes outcome linked grade_items.
 289              $params = array('assign', $newassignment->get_instance()->id, 'assignment', $oldassignment->id);
 290              $sql = 'UPDATE {grade_items} SET itemmodule = ?, iteminstance = ? WHERE itemmodule = ? AND iteminstance = ?';
 291              $DB->execute($sql, $params);
 292  
 293              // Create a mapping record to map urls from the old to the new assignment.
 294              $mapping = new stdClass();
 295              $mapping->oldcmid = $oldcoursemodule->id;
 296              $mapping->oldinstance = $oldassignment->id;
 297              $mapping->newcmid = $newcoursemodule->id;
 298              $mapping->newinstance = $newassignment->get_instance()->id;
 299              $mapping->timecreated = time();
 300              $DB->insert_record('assignment_upgrade', $mapping);
 301  
 302              $gradesdone = true;
 303  
 304          } catch (Exception $exception) {
 305              $rollback = true;
 306              $log .= get_string('conversionexception', 'mod_assign', $exception->getMessage());
 307          }
 308  
 309          if ($rollback) {
 310              // Roll back the grades changes.
 311              if ($gradesdone) {
 312                  // Reassociate grade_items from the new assign instance to the old assignment instance.
 313                  $params = array('assignment', $oldassignment->id, 'assign', $newassignment->get_instance()->id);
 314                  $sql = 'UPDATE {grade_items} SET itemmodule = ?, iteminstance = ? WHERE itemmodule = ? AND iteminstance = ?';
 315                  $DB->execute($sql, $params);
 316              }
 317              // Roll back the completion changes.
 318              if ($completiondone) {
 319                  $DB->set_field('course_modules_completion',
 320                                 'coursemoduleid',
 321                                 $oldcoursemodule->id,
 322                                 array('coursemoduleid'=>$newcoursemodule->id));
 323  
 324                  $allcriteria = $DB->get_records('course_completion_criteria',
 325                                                  array('moduleinstance'=>$newcoursemodule->id));
 326                  foreach ($allcriteria as $criteria) {
 327                      $criteria->module = 'assignment';
 328                      $criteria->moduleinstance = $oldcoursemodule->id;
 329                      $DB->update_record('course_completion_criteria', $criteria);
 330                  }
 331              }
 332              // Roll back the log changes.
 333              $logparams = array('cmid' => $newcoursemodule->id, 'course' => $newcoursemodule->course);
 334              $DB->set_field('log', 'module', 'assignment', $logparams);
 335              $DB->set_field('log', 'cmid', $oldcoursemodule->id, $logparams);
 336              // Roll back the advanced grading update.
 337              if ($gradingarea) {
 338                  foreach ($gradeidmap as $newgradeid => $oldsubmissionid) {
 339                      foreach ($gradingdefinitions as $definition) {
 340                          $DB->set_field('grading_instances',
 341                                         'itemid',
 342                                         $oldsubmissionid,
 343                                         array('definitionid'=>$definition->id, 'itemid'=>$newgradeid));
 344                      }
 345                  }
 346                  $params = array('id'=>$gradingarea->id,
 347                                  'contextid'=>$oldcontext->id,
 348                                  'component'=>'mod_assignment',
 349                                  'areaname'=>'submission');
 350                  $DB->update_record('grading_areas', $params);
 351              }
 352              $newassignment->delete_instance();
 353  
 354              return false;
 355          }
 356          // Delete the old assignment (use object delete).
 357          $cm = get_coursemodule_from_id('', $oldcoursemodule->id, $oldcoursemodule->course);
 358          if ($cm) {
 359              course_delete_module($cm->id);
 360          }
 361          rebuild_course_cache($oldcoursemodule->course);
 362          return true;
 363      }
 364  
 365  
 366      /**
 367       * Create a duplicate course module record so we can create the upgraded
 368       * assign module alongside the old assignment module.
 369       *
 370       * @param stdClass $cm The old course module record
 371       * @param int $moduleid The id of the new assign module
 372       * @param int $newinstanceid The id of the new instance of the assign module
 373       * @return mixed stdClass|bool The new course module record or FALSE
 374       */
 375      private function duplicate_course_module(stdClass $cm, $moduleid, $newinstanceid) {
 376          global $DB, $CFG;
 377  
 378          $newcm = new stdClass();
 379          $newcm->course           = $cm->course;
 380          $newcm->module           = $moduleid;
 381          $newcm->instance         = $newinstanceid;
 382          $newcm->visible          = $cm->visible;
 383          $newcm->section          = $cm->section;
 384          $newcm->score            = $cm->score;
 385          $newcm->indent           = $cm->indent;
 386          $newcm->groupmode        = $cm->groupmode;
 387          $newcm->groupingid       = $cm->groupingid;
 388          $newcm->completion                = $cm->completion;
 389          $newcm->completiongradeitemnumber = $cm->completiongradeitemnumber;
 390          $newcm->completionview            = $cm->completionview;
 391          $newcm->completionexpected        = $cm->completionexpected;
 392          if (!empty($CFG->enableavailability)) {
 393              $newcm->availability = $cm->availability;
 394          }
 395          $newcm->showdescription = $cm->showdescription;
 396  
 397          $newcmid = add_course_module($newcm);
 398          $newcm = get_coursemodule_from_id('', $newcmid, $cm->course);
 399          if (!$newcm) {
 400              return false;
 401          }
 402          $section = $DB->get_record("course_sections", array("id"=>$newcm->section));
 403          if (!$section) {
 404              return false;
 405          }
 406  
 407          $newcm->section = course_add_cm_to_section($newcm->course, $newcm->id, $section->section, $cm->id);
 408  
 409          // Make sure visibility is set correctly (in particular in calendar).
 410          // Note: Allow them to set it even without moodle/course:activityvisibility.
 411          set_coursemodule_visible($newcm->id, $newcm->visible);
 412  
 413          return $newcm;
 414      }
 415  }


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