[ Index ]

PHP Cross Reference of moodle-2.8

title

Body

[close]

/mod/lesson/backup/moodle2/ -> restore_lesson_stepslib.php (source)

   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_lesson
  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_lesson_activity_task
  27   */
  28  
  29  /**
  30   * Structure step to restore one lesson activity
  31   */
  32  class restore_lesson_activity_structure_step extends restore_activity_structure_step {
  33      // Store the answers as they're received but only process them at the
  34      // end of the lesson
  35      protected $answers = array();
  36  
  37      protected function define_structure() {
  38  
  39          $paths = array();
  40          $userinfo = $this->get_setting_value('userinfo');
  41  
  42          $paths[] = new restore_path_element('lesson', '/activity/lesson');
  43          $paths[] = new restore_path_element('lesson_page', '/activity/lesson/pages/page');
  44          $paths[] = new restore_path_element('lesson_answer', '/activity/lesson/pages/page/answers/answer');
  45          if ($userinfo) {
  46              $paths[] = new restore_path_element('lesson_attempt', '/activity/lesson/pages/page/answers/answer/attempts/attempt');
  47              $paths[] = new restore_path_element('lesson_grade', '/activity/lesson/grades/grade');
  48              $paths[] = new restore_path_element('lesson_branch', '/activity/lesson/pages/page/branches/branch');
  49              $paths[] = new restore_path_element('lesson_highscore', '/activity/lesson/highscores/highscore');
  50              $paths[] = new restore_path_element('lesson_timer', '/activity/lesson/timers/timer');
  51          }
  52  
  53          // Return the paths wrapped into standard activity structure
  54          return $this->prepare_activity_structure($paths);
  55      }
  56  
  57      protected function process_lesson($data) {
  58          global $DB;
  59  
  60          $data = (object)$data;
  61          $oldid = $data->id;
  62          $data->course = $this->get_courseid();
  63  
  64          $data->available = $this->apply_date_offset($data->available);
  65          $data->deadline = $this->apply_date_offset($data->deadline);
  66          $data->timemodified = $this->apply_date_offset($data->timemodified);
  67  
  68          // lesson->highscores can come both in data->highscores and
  69          // data->showhighscores, handle both. MDL-26229
  70          if (isset($data->showhighscores)) {
  71              $data->highscores = $data->showhighscores;
  72              unset($data->showhighscores);
  73          }
  74  
  75          // insert the lesson record
  76          $newitemid = $DB->insert_record('lesson', $data);
  77          // immediately after inserting "activity" record, call this
  78          $this->apply_activity_instance($newitemid);
  79      }
  80  
  81      protected function process_lesson_page($data) {
  82          global $DB;
  83  
  84          $data = (object)$data;
  85          $oldid = $data->id;
  86          $data->lessonid = $this->get_new_parentid('lesson');
  87  
  88          // We'll remap all the prevpageid and nextpageid at the end, once all pages have been created
  89          $data->timemodified = $this->apply_date_offset($data->timemodified);
  90          $data->timecreated = $this->apply_date_offset($data->timecreated);
  91  
  92          $newitemid = $DB->insert_record('lesson_pages', $data);
  93          $this->set_mapping('lesson_page', $oldid, $newitemid, true); // Has related fileareas
  94      }
  95  
  96      protected function process_lesson_answer($data) {
  97          global $DB;
  98  
  99          $data = (object)$data;
 100          $data->lessonid = $this->get_new_parentid('lesson');
 101          $data->pageid = $this->get_new_parentid('lesson_page');
 102          $data->answer = $data->answer_text;
 103          $data->timemodified = $this->apply_date_offset($data->timemodified);
 104          $data->timecreated = $this->apply_date_offset($data->timecreated);
 105  
 106          // Set a dummy mapping to get the old ID so that it can be used by get_old_parentid when
 107          // processing attempts. It will be corrected in after_execute
 108          $this->set_mapping('lesson_answer', $data->id, 0, true); // Has related fileareas.
 109  
 110          // Answers need to be processed in order, so we store them in an
 111          // instance variable and insert them in the after_execute stage
 112          $this->answers[$data->id] = $data;
 113      }
 114  
 115      protected function process_lesson_attempt($data) {
 116          global $DB;
 117  
 118          $data = (object)$data;
 119          $oldid = $data->id;
 120          $data->lessonid = $this->get_new_parentid('lesson');
 121          $data->pageid = $this->get_new_parentid('lesson_page');
 122  
 123          // We use the old answerid here as the answer isn't created until after_execute
 124          $data->answerid = $this->get_old_parentid('lesson_answer');
 125          $data->userid = $this->get_mappingid('user', $data->userid);
 126          $data->timeseen = $this->apply_date_offset($data->timeseen);
 127  
 128          $newitemid = $DB->insert_record('lesson_attempts', $data);
 129      }
 130  
 131      protected function process_lesson_grade($data) {
 132          global $DB;
 133  
 134          $data = (object)$data;
 135          $oldid = $data->id;
 136          $data->lessonid = $this->get_new_parentid('lesson');
 137          $data->userid = $this->get_mappingid('user', $data->userid);
 138          $data->completed = $this->apply_date_offset($data->completed);
 139  
 140          $newitemid = $DB->insert_record('lesson_grades', $data);
 141          $this->set_mapping('lesson_grade', $oldid, $newitemid);
 142      }
 143  
 144      protected function process_lesson_branch($data) {
 145          global $DB;
 146  
 147          $data = (object)$data;
 148          $oldid = $data->id;
 149          $data->lessonid = $this->get_new_parentid('lesson');
 150          $data->pageid = $this->get_new_parentid('lesson_page');
 151          $data->userid = $this->get_mappingid('user', $data->userid);
 152          $data->timeseen = $this->apply_date_offset($data->timeseen);
 153  
 154          $newitemid = $DB->insert_record('lesson_branch', $data);
 155      }
 156  
 157      protected function process_lesson_highscore($data) {
 158          global $DB;
 159  
 160          $data = (object)$data;
 161          $oldid = $data->id;
 162          $data->lessonid = $this->get_new_parentid('lesson');
 163          $data->userid = $this->get_mappingid('user', $data->userid);
 164          $data->gradeid = $this->get_mappingid('lesson_grade', $data->gradeid);
 165  
 166          $newitemid = $DB->insert_record('lesson_high_scores', $data);
 167      }
 168  
 169      protected function process_lesson_timer($data) {
 170          global $DB;
 171  
 172          $data = (object)$data;
 173          $oldid = $data->id;
 174          $data->lessonid = $this->get_new_parentid('lesson');
 175          $data->userid = $this->get_mappingid('user', $data->userid);
 176          $data->starttime = $this->apply_date_offset($data->starttime);
 177          $data->lessontime = $this->apply_date_offset($data->lessontime);
 178  
 179          $newitemid = $DB->insert_record('lesson_timer', $data);
 180      }
 181  
 182      protected function after_execute() {
 183          global $DB;
 184  
 185          // Answers must be sorted by id to ensure that they're shown correctly
 186          ksort($this->answers);
 187          foreach ($this->answers as $answer) {
 188              $newitemid = $DB->insert_record('lesson_answers', $answer);
 189              $this->set_mapping('lesson_answer', $answer->id, $newitemid, true);
 190  
 191              // Update the lesson attempts to use the newly created answerid
 192              $DB->set_field('lesson_attempts', 'answerid', $newitemid, array(
 193                      'lessonid' => $answer->lessonid,
 194                      'pageid' => $answer->pageid,
 195                      'answerid' => $answer->id));
 196          }
 197  
 198          // Add lesson mediafile, no need to match by itemname (just internally handled context)
 199          $this->add_related_files('mod_lesson', 'mediafile', null);
 200          // Add lesson page files, by lesson_page itemname
 201          $this->add_related_files('mod_lesson', 'page_contents', 'lesson_page');
 202          $this->add_related_files('mod_lesson', 'page_answers', 'lesson_answer');
 203          $this->add_related_files('mod_lesson', 'page_responses', 'lesson_answer');
 204  
 205          // Remap all the restored prevpageid and nextpageid now that we have all the pages and their mappings
 206          $rs = $DB->get_recordset('lesson_pages', array('lessonid' => $this->task->get_activityid()),
 207                                   '', 'id, prevpageid, nextpageid');
 208          foreach ($rs as $page) {
 209              $page->prevpageid = (empty($page->prevpageid)) ? 0 : $this->get_mappingid('lesson_page', $page->prevpageid);
 210              $page->nextpageid = (empty($page->nextpageid)) ? 0 : $this->get_mappingid('lesson_page', $page->nextpageid);
 211              $DB->update_record('lesson_pages', $page);
 212          }
 213          $rs->close();
 214  
 215          // Remap all the restored 'jumpto' fields now that we have all the pages and their mappings
 216          $rs = $DB->get_recordset('lesson_answers', array('lessonid' => $this->task->get_activityid()),
 217                                   '', 'id, jumpto');
 218          foreach ($rs as $answer) {
 219              if ($answer->jumpto > 0) {
 220                  $answer->jumpto = $this->get_mappingid('lesson_page', $answer->jumpto);
 221                  $DB->update_record('lesson_answers', $answer);
 222              }
 223          }
 224          $rs->close();
 225  
 226          // Re-map the dependency and activitylink information
 227          // If a depency or activitylink has no mapping in the backup data then it could either be a duplication of a
 228          // lesson, or a backup/restore of a single lesson. We have no way to determine which and whether this is the
 229          // same site and/or course. Therefore we try and retrieve a mapping, but fallback to the original value if one
 230          // was not found. We then test to see whether the value found is valid for the course being restored into.
 231          $lesson = $DB->get_record('lesson', array('id' => $this->task->get_activityid()), 'id, course, dependency, activitylink');
 232          $updaterequired = false;
 233          if (!empty($lesson->dependency)) {
 234              $updaterequired = true;
 235              $lesson->dependency = $this->get_mappingid('lesson', $lesson->dependency, $lesson->dependency);
 236              if (!$DB->record_exists('lesson', array('id' => $lesson->dependency, 'course' => $lesson->course))) {
 237                  $lesson->dependency = 0;
 238              }
 239          }
 240  
 241          if (!empty($lesson->activitylink)) {
 242              $updaterequired = true;
 243              $lesson->activitylink = $this->get_mappingid('course_module', $lesson->activitylink, $lesson->activitylink);
 244              if (!$DB->record_exists('course_modules', array('id' => $lesson->activitylink, 'course' => $lesson->course))) {
 245                  $lesson->activitylink = 0;
 246              }
 247          }
 248  
 249          if ($updaterequired) {
 250              $DB->update_record('lesson', $lesson);
 251          }
 252      }
 253  }


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