Moodle  2.2.1
http://www.collinsharper.com
C:/xampp/htdocs/moodle/mod/lesson/backup/moodle2/restore_lesson_stepslib.php
Go to the documentation of this file.
00001 <?php
00002 
00003 // This file is part of Moodle - http://moodle.org/
00004 //
00005 // Moodle is free software: you can redistribute it and/or modify
00006 // it under the terms of the GNU General Public License as published by
00007 // the Free Software Foundation, either version 3 of the License, or
00008 // (at your option) any later version.
00009 //
00010 // Moodle is distributed in the hope that it will be useful,
00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013 // GNU General Public License for more details.
00014 //
00015 // You should have received a copy of the GNU General Public License
00016 // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
00017 
00032 class restore_lesson_activity_structure_step extends restore_activity_structure_step {
00033     // Store the answers as they're received but only process them at the
00034     // end of the lesson
00035     protected $answers = array();
00036 
00037     protected function define_structure() {
00038 
00039         $paths = array();
00040         $userinfo = $this->get_setting_value('userinfo');
00041 
00042         $paths[] = new restore_path_element('lesson', '/activity/lesson');
00043         $paths[] = new restore_path_element('lesson_page', '/activity/lesson/pages/page');
00044         $paths[] = new restore_path_element('lesson_answer', '/activity/lesson/pages/page/answers/answer');
00045         if ($userinfo) {
00046             $paths[] = new restore_path_element('lesson_attempt', '/activity/lesson/pages/page/answers/answer/attempts/attempt');
00047             $paths[] = new restore_path_element('lesson_grade', '/activity/lesson/grades/grade');
00048             $paths[] = new restore_path_element('lesson_branch', '/activity/lesson/pages/page/branches/branch');
00049             $paths[] = new restore_path_element('lesson_highscore', '/activity/lesson/highscores/highscore');
00050             $paths[] = new restore_path_element('lesson_timer', '/activity/lesson/timers/timer');
00051         }
00052 
00053         // Return the paths wrapped into standard activity structure
00054         return $this->prepare_activity_structure($paths);
00055     }
00056 
00057     protected function process_lesson($data) {
00058         global $DB;
00059 
00060         $data = (object)$data;
00061         $oldid = $data->id;
00062         $data->course = $this->get_courseid();
00063 
00064         $data->available = $this->apply_date_offset($data->available);
00065         $data->deadline = $this->apply_date_offset($data->deadline);
00066         $data->timemodified = $this->apply_date_offset($data->timemodified);
00067 
00068         // lesson->highscores can come both in data->highscores and
00069         // data->showhighscores, handle both. MDL-26229
00070         if (isset($data->showhighscores)) {
00071             $data->highscores = $data->showhighscores;
00072             unset($data->showhighscores);
00073         }
00074 
00075         // insert the lesson record
00076         $newitemid = $DB->insert_record('lesson', $data);
00077         // immediately after inserting "activity" record, call this
00078         $this->apply_activity_instance($newitemid);
00079     }
00080 
00081     protected function process_lesson_page($data) {
00082         global $DB;
00083 
00084         $data = (object)$data;
00085         $oldid = $data->id;
00086         $data->lessonid = $this->get_new_parentid('lesson');
00087 
00088         // We'll remap all the prevpageid and nextpageid at the end, once all pages have been created
00089         $data->timemodified = $this->apply_date_offset($data->timemodified);
00090         $data->timecreated = $this->apply_date_offset($data->timecreated);
00091 
00092         $newitemid = $DB->insert_record('lesson_pages', $data);
00093         $this->set_mapping('lesson_page', $oldid, $newitemid, true); // Has related fileareas
00094     }
00095 
00096     protected function process_lesson_answer($data) {
00097         global $DB;
00098 
00099         $data = (object)$data;
00100         $data->lessonid = $this->get_new_parentid('lesson');
00101         $data->pageid = $this->get_new_parentid('lesson_page');
00102         $data->answer = $data->answer_text;
00103         $data->timemodified = $this->apply_date_offset($data->timemodified);
00104         $data->timecreated = $this->apply_date_offset($data->timecreated);
00105 
00106         // Answers need to be processed in order, so we store them in an
00107         // instance variable and insert them in the after_execute stage
00108         $this->answers[$data->id] = $data;
00109     }
00110 
00111     protected function process_lesson_attempt($data) {
00112         global $DB;
00113 
00114         $data = (object)$data;
00115         $oldid = $data->id;
00116         $data->lessonid = $this->get_new_parentid('lesson');
00117         $data->pageid = $this->get_new_parentid('lesson_page');
00118         $data->answerid = $this->get_new_parentid('lesson_answer');
00119         $data->userid = $this->get_mappingid('user', $data->userid);
00120         $data->timeseen = $this->apply_date_offset($data->timeseen);
00121 
00122         $newitemid = $DB->insert_record('lesson_attempts', $data);
00123     }
00124 
00125     protected function process_lesson_grade($data) {
00126         global $DB;
00127 
00128         $data = (object)$data;
00129         $oldid = $data->id;
00130         $data->lessonid = $this->get_new_parentid('lesson');
00131         $data->userid = $this->get_mappingid('user', $data->userid);
00132         $data->completed = $this->apply_date_offset($data->completed);
00133 
00134         $newitemid = $DB->insert_record('lesson_grades', $data);
00135         $this->set_mapping('lesson_grade', $oldid, $newitemid);
00136     }
00137 
00138     protected function process_lesson_branch($data) {
00139         global $DB;
00140 
00141         $data = (object)$data;
00142         $oldid = $data->id;
00143         $data->lessonid = $this->get_new_parentid('lesson');
00144         $data->pageid = $this->get_new_parentid('lesson_page');
00145         $data->userid = $this->get_mappingid('user', $data->userid);
00146         $data->timeseen = $this->apply_date_offset($data->timeseen);
00147 
00148         $newitemid = $DB->insert_record('lesson_branch', $data);
00149     }
00150 
00151     protected function process_lesson_highscore($data) {
00152         global $DB;
00153 
00154         $data = (object)$data;
00155         $oldid = $data->id;
00156         $data->lessonid = $this->get_new_parentid('lesson');
00157         $data->userid = $this->get_mappingid('user', $data->userid);
00158         $data->gradeid = $this->get_mappingid('lesson_grade', $data->gradeid);
00159 
00160         $newitemid = $DB->insert_record('lesson_high_scores', $data);
00161     }
00162 
00163     protected function process_lesson_timer($data) {
00164         global $DB;
00165 
00166         $data = (object)$data;
00167         $oldid = $data->id;
00168         $data->lessonid = $this->get_new_parentid('lesson');
00169         $data->userid = $this->get_mappingid('user', $data->userid);
00170         $data->starttime = $this->apply_date_offset($data->starttime);
00171         $data->lessontime = $this->apply_date_offset($data->lessontime);
00172 
00173         $newitemid = $DB->insert_record('lesson_timer', $data);
00174     }
00175 
00176     protected function after_execute() {
00177         global $DB;
00178 
00179         // Answers must be sorted by id to ensure that they're shown correctly
00180         ksort($this->answers);
00181         foreach ($this->answers as $answer) {
00182             $newitemid = $DB->insert_record('lesson_answers', $answer);
00183             $this->set_mapping('lesson_answer', $answer->id, $newitemid);
00184         }
00185 
00186         // Add lesson mediafile, no need to match by itemname (just internally handled context)
00187         $this->add_related_files('mod_lesson', 'mediafile', null);
00188         // Add lesson page files, by lesson_page itemname
00189         $this->add_related_files('mod_lesson', 'page_contents', 'lesson_page');
00190 
00191         // Remap all the restored prevpageid and nextpageid now that we have all the pages and their mappings
00192         $rs = $DB->get_recordset('lesson_pages', array('lessonid' => $this->task->get_activityid()),
00193                                  '', 'id, prevpageid, nextpageid');
00194         foreach ($rs as $page) {
00195             $page->prevpageid = (empty($page->prevpageid)) ? 0 : $this->get_mappingid('lesson_page', $page->prevpageid);
00196             $page->nextpageid = (empty($page->nextpageid)) ? 0 : $this->get_mappingid('lesson_page', $page->nextpageid);
00197             $DB->update_record('lesson_pages', $page);
00198         }
00199         $rs->close();
00200 
00201         // Remap all the restored 'jumpto' fields now that we have all the pages and their mappings
00202         $rs = $DB->get_recordset('lesson_answers', array('lessonid' => $this->task->get_activityid()),
00203                                  '', 'id, jumpto');
00204         foreach ($rs as $answer) {
00205             if ($answer->jumpto > 0) {
00206                 $answer->jumpto = $this->get_mappingid('lesson_page', $answer->jumpto);
00207                 $DB->update_record('lesson_answers', $answer);
00208             }
00209         }
00210         $rs->close();
00211 
00212         // Re-map the dependency and activitylink information
00213         // If a depency or activitylink has no mapping in the backup data then it could either be a duplication of a
00214         // lesson, or a backup/restore of a single lesson. We have no way to determine which and whether this is the
00215         // same site and/or course. Therefore we try and retrieve a mapping, but fallback to the original value if one
00216         // was not found. We then test to see whether the value found is valid for the course being restored into.
00217         $lesson = $DB->get_record('lesson', array('id' => $this->task->get_activityid()), 'id, course, dependency, activitylink');
00218         $updaterequired = false;
00219         if (!empty($lesson->dependency)) {
00220             $updaterequired = true;
00221             $lesson->dependency = $this->get_mappingid('lesson', $lesson->dependency, $lesson->dependency);
00222             if (!$DB->record_exists('lesson', array('id' => $lesson->dependency, 'course' => $lesson->course))) {
00223                 $lesson->dependency = 0;
00224             }
00225         }
00226 
00227         if (!empty($lesson->activitylink)) {
00228             $updaterequired = true;
00229             $lesson->activitylink = $this->get_mappingid('course_module', $lesson->activitylink, $lesson->activitylink);
00230             if (!$DB->record_exists('course_modules', array('id' => $lesson->activitylink, 'course' => $lesson->course))) {
00231                 $lesson->activitylink = 0;
00232             }
00233         }
00234 
00235         if ($updaterequired) {
00236             $DB->update_record('lesson', $lesson);
00237         }
00238     }
00239 }
 All Data Structures Namespaces Files Functions Variables Enumerations