[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/mod/assign/backup/moodle2/ -> restore_assign_stepslib.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   * Define all the restore steps that will be used by the restore_assign_activity_task
  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   * Define the complete assignment structure for restore, with file and id annotations
  29   *
  30   * @package   mod_assign
  31   * @copyright 2012 NetSpot {@link http://www.netspot.com.au}
  32   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  33   */
  34  class restore_assign_activity_structure_step extends restore_activity_structure_step {
  35  
  36      /**
  37       * Store whether submission details should be included. Details may not be included if the
  38       * this is a team submission, but groups/grouping information was not included in the backup.
  39       */
  40      protected $includesubmission = true;
  41  
  42      /**
  43       * Define the structure of the restore workflow.
  44       *
  45       * @return restore_path_element $structure
  46       */
  47      protected function define_structure() {
  48  
  49          $paths = array();
  50          // To know if we are including userinfo.
  51          $userinfo = $this->get_setting_value('userinfo');
  52  
  53          // Define each element separated.
  54          $paths[] = new restore_path_element('assign', '/activity/assign');
  55          if ($userinfo) {
  56              $submission = new restore_path_element('assign_submission',
  57                                                     '/activity/assign/submissions/submission');
  58              $paths[] = $submission;
  59              $this->add_subplugin_structure('assignsubmission', $submission);
  60              $grade = new restore_path_element('assign_grade', '/activity/assign/grades/grade');
  61              $paths[] = $grade;
  62              $this->add_subplugin_structure('assignfeedback', $grade);
  63              $userflag = new restore_path_element('assign_userflag',
  64                                                     '/activity/assign/userflags/userflag');
  65              $paths[] = $userflag;
  66          }
  67          $paths[] = new restore_path_element('assign_plugin_config',
  68                                              '/activity/assign/plugin_configs/plugin_config');
  69  
  70          return $this->prepare_activity_structure($paths);
  71      }
  72  
  73      /**
  74       * Process an assign restore.
  75       *
  76       * @param object $data The data in object form
  77       * @return void
  78       */
  79      protected function process_assign($data) {
  80          global $DB;
  81  
  82          $data = (object)$data;
  83          $oldid = $data->id;
  84          $data->course = $this->get_courseid();
  85  
  86          $data->timemodified = $this->apply_date_offset($data->timemodified);
  87          $data->allowsubmissionsfromdate = $this->apply_date_offset($data->allowsubmissionsfromdate);
  88          $data->duedate = $this->apply_date_offset($data->duedate);
  89  
  90          // If this is a team submission, but there is no group info we need to flag that the submission
  91          // information should not be included. It should not be restored.
  92          $groupinfo = $this->task->get_setting_value('groups');
  93          if ($data->teamsubmission && !$groupinfo) {
  94              $this->includesubmission = false;
  95          }
  96  
  97          if (!empty($data->teamsubmissiongroupingid)) {
  98              $data->teamsubmissiongroupingid = $this->get_mappingid('grouping',
  99                                                                     $data->teamsubmissiongroupingid);
 100          } else {
 101              $data->teamsubmissiongroupingid = 0;
 102          }
 103  
 104          if (!isset($data->cutoffdate)) {
 105              $data->cutoffdate = 0;
 106          }
 107          if (!isset($data->markingworkflow)) {
 108              $data->markingworkflow = 0;
 109          }
 110          if (!isset($data->markingallocation)) {
 111              $data->markingallocation = 0;
 112          }
 113          if (!isset($data->preventsubmissionnotingroup)) {
 114              $data->preventsubmissionnotingroup = 0;
 115          }
 116  
 117          if (!empty($data->preventlatesubmissions)) {
 118              $data->cutoffdate = $data->duedate;
 119          } else {
 120              $data->cutoffdate = $this->apply_date_offset($data->cutoffdate);
 121          }
 122  
 123          if ($data->grade < 0) { // Scale found, get mapping.
 124              $data->grade = -($this->get_mappingid('scale', abs($data->grade)));
 125          }
 126  
 127          $newitemid = $DB->insert_record('assign', $data);
 128  
 129          $this->apply_activity_instance($newitemid);
 130      }
 131  
 132      /**
 133       * Process a submission restore
 134       * @param object $data The data in object form
 135       * @return void
 136       */
 137      protected function process_assign_submission($data) {
 138          global $DB;
 139  
 140          if (!$this->includesubmission) {
 141              return;
 142          }
 143  
 144          $data = (object)$data;
 145          $oldid = $data->id;
 146  
 147          $data->assignment = $this->get_new_parentid('assign');
 148  
 149          $data->timemodified = $this->apply_date_offset($data->timemodified);
 150          $data->timecreated = $this->apply_date_offset($data->timecreated);
 151          if ($data->userid > 0) {
 152              $data->userid = $this->get_mappingid('user', $data->userid);
 153          }
 154          if (!empty($data->groupid)) {
 155              $data->groupid = $this->get_mappingid('group', $data->groupid);
 156          } else {
 157              $data->groupid = 0;
 158          }
 159  
 160          // We will correct this in set_latest_submission_field() once all submissions are restored.
 161          $data->latest = 0;
 162  
 163          $newitemid = $DB->insert_record('assign_submission', $data);
 164  
 165          // Note - the old contextid is required in order to be able to restore files stored in
 166          // sub plugin file areas attached to the submissionid.
 167          $this->set_mapping('submission', $oldid, $newitemid, false, null, $this->task->get_old_contextid());
 168      }
 169  
 170      /**
 171       * Process a user_flags restore
 172       * @param object $data The data in object form
 173       * @return void
 174       */
 175      protected function process_assign_userflag($data) {
 176          global $DB;
 177  
 178          $data = (object)$data;
 179          $oldid = $data->id;
 180  
 181          $data->assignment = $this->get_new_parentid('assign');
 182  
 183          $data->userid = $this->get_mappingid('user', $data->userid);
 184          if (!empty($data->allocatedmarker)) {
 185              $data->allocatedmarker = $this->get_mappingid('user', $data->allocatedmarker);
 186          }
 187          if (!empty($data->extensionduedate)) {
 188              $data->extensionduedate = $this->apply_date_offset($data->extensionduedate);
 189          } else {
 190              $data->extensionduedate = 0;
 191          }
 192          // Flags mailed and locked need no translation on restore.
 193  
 194          $newitemid = $DB->insert_record('assign_user_flags', $data);
 195      }
 196  
 197      /**
 198       * Process a grade restore
 199       * @param object $data The data in object form
 200       * @return void
 201       */
 202      protected function process_assign_grade($data) {
 203          global $DB;
 204  
 205          $data = (object)$data;
 206          $oldid = $data->id;
 207  
 208          $data->assignment = $this->get_new_parentid('assign');
 209  
 210          $data->timemodified = $this->apply_date_offset($data->timemodified);
 211          $data->timecreated = $this->apply_date_offset($data->timecreated);
 212          $data->userid = $this->get_mappingid('user', $data->userid);
 213          $data->grader = $this->get_mappingid('user', $data->grader);
 214  
 215          // Handle flags restore to a different table (for upgrade from old backups).
 216          if (!empty($data->extensionduedate) ||
 217                  !empty($data->mailed) ||
 218                  !empty($data->locked)) {
 219              $flags = new stdClass();
 220              $flags->assignment = $this->get_new_parentid('assign');
 221              if (!empty($data->extensionduedate)) {
 222                  $flags->extensionduedate = $this->apply_date_offset($data->extensionduedate);
 223              }
 224              if (!empty($data->mailed)) {
 225                  $flags->mailed = $data->mailed;
 226              }
 227              if (!empty($data->locked)) {
 228                  $flags->locked = $data->locked;
 229              }
 230              $flags->userid = $this->get_mappingid('user', $data->userid);
 231              $DB->insert_record('assign_user_flags', $flags);
 232          }
 233  
 234          $newitemid = $DB->insert_record('assign_grades', $data);
 235  
 236          // Note - the old contextid is required in order to be able to restore files stored in
 237          // sub plugin file areas attached to the gradeid.
 238          $this->set_mapping('grade', $oldid, $newitemid, false, null, $this->task->get_old_contextid());
 239          $this->set_mapping(restore_gradingform_plugin::itemid_mapping('submissions'), $oldid, $newitemid);
 240      }
 241  
 242      /**
 243       * Process a plugin-config restore
 244       * @param object $data The data in object form
 245       * @return void
 246       */
 247      protected function process_assign_plugin_config($data) {
 248          global $DB;
 249  
 250          $data = (object)$data;
 251          $oldid = $data->id;
 252  
 253          $data->assignment = $this->get_new_parentid('assign');
 254  
 255          $newitemid = $DB->insert_record('assign_plugin_config', $data);
 256      }
 257  
 258      /**
 259       * For all submissions in this assignment, either set the
 260       * submission->latest field to 1 for the latest attempts
 261       * or create a new submission record for grades with no submission.
 262       *
 263       * @return void
 264       */
 265      protected function set_latest_submission_field() {
 266          global $DB, $CFG;
 267  
 268          // Required for constants.
 269          require_once($CFG->dirroot . '/mod/assign/locallib.php');
 270  
 271          $assignmentid = $this->get_new_parentid('assign');
 272          // This code could be rewritten as a monster SQL - but the point of adding this "latest" field
 273          // to the submissions table in the first place was to get away from those hard to maintain SQL queries.
 274  
 275          // First user submissions.
 276          $sql = 'SELECT DISTINCT userid FROM {assign_submission} WHERE assignment = ? AND groupid = ?';
 277          $params = array($assignmentid, 0);
 278          $users = $DB->get_records_sql($sql, $params);
 279  
 280          foreach ($users as $userid => $unused) {
 281              $params = array('assignment'=>$assignmentid, 'groupid'=>0, 'userid'=>$userid);
 282  
 283              // Only return the row with the highest attemptnumber.
 284              $submission = null;
 285              $submissions = $DB->get_records('assign_submission', $params, 'attemptnumber DESC', '*', 0, 1);
 286              if ($submissions) {
 287                  $submission = reset($submissions);
 288                  $submission->latest = 1;
 289                  $DB->update_record('assign_submission', $submission);
 290              }
 291          }
 292          // Then group submissions (if any).
 293          $sql = 'SELECT DISTINCT groupid FROM {assign_submission} WHERE assignment = ? AND userid = ?';
 294          $params = array($assignmentid, 0);
 295          $groups = $DB->get_records_sql($sql, $params);
 296  
 297          foreach ($groups as $groupid => $unused) {
 298              $params = array('assignment'=>$assignmentid, 'userid'=>0, 'groupid'=>$groupid);
 299  
 300              // Only return the row with the highest attemptnumber.
 301              $submission = null;
 302              $submissions = $DB->get_records('assign_submission', $params, 'attemptnumber DESC', '*', 0, 1);
 303              if ($submissions) {
 304                  $submission = reset($submissions);
 305                  $submission->latest = 1;
 306                  $DB->update_record('assign_submission', $submission);
 307              }
 308          }
 309  
 310          // Now check for records with a grade, but no submission record.
 311          // This happens when a teacher marks a student before they have submitted anything.
 312          $records = $DB->get_recordset_sql('SELECT g.id, g.userid
 313                                             FROM {assign_grades} g
 314                                        LEFT JOIN {assign_submission} s
 315                                               ON s.assignment = g.assignment
 316                                              AND s.userid = g.userid
 317                                            WHERE s.id IS NULL AND g.assignment = ?', array($assignmentid));
 318  
 319          $submissions = array();
 320          foreach ($records as $record) {
 321              $submission = new stdClass();
 322              $submission->assignment = $assignmentid;
 323              $submission->userid = $record->userid;
 324              $submission->status = ASSIGN_SUBMISSION_STATUS_NEW;
 325              $submission->groupid = 0;
 326              $submission->latest = 1;
 327              $submission->timecreated = time();
 328              $submission->timemodified = time();
 329              array_push($submissions, $submission);
 330          }
 331  
 332          $records->close();
 333  
 334          $DB->insert_records('assign_submission', $submissions);
 335      }
 336  
 337      /**
 338       * Once the database tables have been fully restored, restore the files
 339       * @return void
 340       */
 341      protected function after_execute() {
 342          $this->add_related_files('mod_assign', 'intro', null);
 343          $this->add_related_files('mod_assign', 'introattachment', null);
 344  
 345          $this->set_latest_submission_field();
 346      }
 347  }


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