[ Index ]

PHP Cross Reference of moodle-2.8

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


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