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.
   1  <?php
   2  
   3  // This file is part of Moodle - http://moodle.org/
   4  //
   5  // Moodle is free software: you can redistribute it and/or modify
   6  // it under the terms of the GNU General Public License as published by
   7  // the Free Software Foundation, either version 3 of the License, or
   8  // (at your option) any later version.
   9  //
  10  // Moodle is distributed in the hope that it will be useful,
  11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13  // GNU General Public License for more details.
  14  //
  15  // You should have received a copy of the GNU General Public License
  16  // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
  17  
  18  /**
  19   * @package    mod_assignment
  20   * @subpackage backup-moodle2
  21   * @copyright 2010 onwards Eloy Lafuente (stronk7) {@link http://stronk7.com}
  22   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  /**
  26   * Define all the restore steps that will be used by the restore_assignment_activity_task
  27   */
  28  
  29  /**
  30   * Structure step to restore one assignment activity
  31   */
  32  class restore_assignment_activity_structure_step extends restore_activity_structure_step {
  33  
  34      protected function define_structure() {
  35  
  36          $paths = array();
  37          $userinfo = $this->get_setting_value('userinfo');
  38  
  39          $assignment = new restore_path_element('assignment', '/activity/assignment');
  40          $paths[] = $assignment;
  41  
  42          // Apply for 'assignment' subplugins optional paths at assignment level
  43          $this->add_subplugin_structure('assignment', $assignment);
  44  
  45          if ($userinfo) {
  46              $submission = new restore_path_element('assignment_submission', '/activity/assignment/submissions/submission');
  47              $paths[] = $submission;
  48              // Apply for 'assignment' subplugins optional stuff at submission level
  49              $this->add_subplugin_structure('assignment', $submission);
  50          }
  51  
  52          // Return the paths wrapped into standard activity structure
  53          return $this->prepare_activity_structure($paths);
  54      }
  55  
  56      protected function process_assignment($data) {
  57          global $DB;
  58  
  59          $data = (object)$data;
  60          $oldid = $data->id;
  61          $data->course = $this->get_courseid();
  62  
  63          $data->timedue = $this->apply_date_offset($data->timedue);
  64          $data->timeavailable = $this->apply_date_offset($data->timeavailable);
  65          $data->timemodified = $this->apply_date_offset($data->timemodified);
  66  
  67          if ($data->grade < 0) { // scale found, get mapping
  68              $data->grade = -($this->get_mappingid('scale', abs($data->grade)));
  69          }
  70  
  71          // insert the assignment record
  72          $newitemid = $DB->insert_record('assignment', $data);
  73          // immediately after inserting "activity" record, call this
  74          $this->apply_activity_instance($newitemid);
  75  
  76          // Hide unsupported sub-plugins
  77          if (!$this->is_valid_assignment_subplugin($data->assignmenttype)) {
  78              $DB->set_field('course_modules', 'visible', 0, array('id' => $this->get_task()->get_moduleid()));
  79          }
  80      }
  81  
  82      protected function process_assignment_submission($data) {
  83          global $DB;
  84  
  85          $data = (object)$data;
  86          $oldid = $data->id;
  87  
  88          $data->assignment = $this->get_new_parentid('assignment');
  89          $data->timecreated = $this->apply_date_offset($data->timecreated);
  90          $data->timemodified = $this->apply_date_offset($data->timemodified);
  91          $data->timemarked = $this->apply_date_offset($data->timemarked);
  92  
  93          $data->userid = $this->get_mappingid('user', $data->userid);
  94          $data->teacher = $this->get_mappingid('user', $data->teacher);
  95  
  96          $newitemid = $DB->insert_record('assignment_submissions', $data);
  97          $this->set_mapping('assignment_submission', $oldid, $newitemid, true); // Going to have files
  98          $this->set_mapping(restore_gradingform_plugin::itemid_mapping('submission'), $oldid, $newitemid);
  99      }
 100  
 101      /**
 102       * This function will attempt to upgrade the newly restored assignment to an instance of mod_assign if
 103       * mod_assignment is currently disabled and mod_assign is enabled and mod_assign says it can upgrade this assignment.
 104       *
 105       * @return none
 106       */
 107      private function upgrade_mod_assign() {
 108          global $DB, $CFG;
 109  
 110          // The current module must exist.
 111          $pluginmanager = core_plugin_manager::instance();
 112  
 113          $plugininfo = $pluginmanager->get_plugin_info('mod_assign');
 114  
 115          // Check that the assignment module is installed.
 116          if ($plugininfo && $plugininfo->is_installed_and_upgraded()) {
 117              // Include the required mod assign upgrade code.
 118              require_once($CFG->dirroot . '/mod/assign/upgradelib.php');
 119              require_once($CFG->dirroot . '/mod/assign/locallib.php');
 120  
 121              // Get the id and type of this assignment.
 122              $newinstance = $this->task->get_activityid();
 123  
 124              $record = $DB->get_record('assignment', array('id'=>$newinstance), 'assignmenttype', MUST_EXIST);
 125              $type = $record->assignmenttype;
 126  
 127              $subplugininfo = $pluginmanager->get_plugin_info('assignment_' . $type);
 128  
 129              // See if it is possible to upgrade.
 130              if (assign::can_upgrade_assignment($type, $subplugininfo->versiondb)) {
 131                  $assignment_upgrader = new assign_upgrade_manager();
 132                  $log = '';
 133                  $success = $assignment_upgrader->upgrade_assignment($newinstance, $log);
 134                  if (!$success) {
 135                      throw new restore_step_exception('mod_assign_upgrade_failed', $log);
 136                  }
 137              }
 138          }
 139      }
 140  
 141      protected function after_execute() {
 142          // Add assignment related files, no need to match by itemname (just internally handled context)
 143          $this->add_related_files('mod_assignment', 'intro', null);
 144          // Add assignment submission files, matching by assignment_submission itemname
 145          $this->add_related_files('mod_assignment', 'submission', 'assignment_submission');
 146          $this->add_related_files('mod_assignment', 'response', 'assignment_submission');
 147      }
 148  
 149      /**
 150       * Hook to execute assignment upgrade after restore.
 151       */
 152      protected function after_restore() {
 153  
 154          if ($this->get_task()->get_mode() != backup::MODE_IMPORT) {
 155              // Moodle 2.2 assignment upgrade
 156              $this->upgrade_mod_assign();
 157          }
 158      }
 159  
 160      /**
 161       * Determine if a sub-plugin is supported or not
 162       *
 163       * @param string $type
 164       * @return bool
 165       */
 166      protected function is_valid_assignment_subplugin($type) {
 167          static $subplugins = null;
 168  
 169          if (is_null($subplugins)) {
 170              $subplugins = get_plugin_list('assignment');
 171          }
 172          return array_key_exists($type, $subplugins);
 173      }
 174  }