[ Index ] |
PHP Cross Reference of Unnamed Project |
[Summary view] [Print] [Text view]
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 $paths[] = new restore_path_element('lesson_override', '/activity/lesson/overrides/override'); 46 if ($userinfo) { 47 $paths[] = new restore_path_element('lesson_attempt', '/activity/lesson/pages/page/answers/answer/attempts/attempt'); 48 $paths[] = new restore_path_element('lesson_grade', '/activity/lesson/grades/grade'); 49 $paths[] = new restore_path_element('lesson_branch', '/activity/lesson/pages/page/branches/branch'); 50 $paths[] = new restore_path_element('lesson_highscore', '/activity/lesson/highscores/highscore'); 51 $paths[] = new restore_path_element('lesson_timer', '/activity/lesson/timers/timer'); 52 } 53 54 // Return the paths wrapped into standard activity structure 55 return $this->prepare_activity_structure($paths); 56 } 57 58 protected function process_lesson($data) { 59 global $DB; 60 61 $data = (object)$data; 62 $oldid = $data->id; 63 $data->course = $this->get_courseid(); 64 65 $data->available = $this->apply_date_offset($data->available); 66 $data->deadline = $this->apply_date_offset($data->deadline); 67 $data->timemodified = $this->apply_date_offset($data->timemodified); 68 69 // The lesson->highscore code was removed in MDL-49581. 70 // Remove it if found in the backup file. 71 if (isset($data->showhighscores)) { 72 unset($data->showhighscores); 73 } 74 if (isset($data->highscores)) { 75 unset($data->highscores); 76 } 77 78 // Supply items that maybe missing from previous versions. 79 if (!isset($data->completionendreached)) { 80 $data->completionendreached = 0; 81 } 82 if (!isset($data->completiontimespent)) { 83 $data->completiontimespent = 0; 84 } 85 86 if (!isset($data->intro)) { 87 $data->intro = ''; 88 $data->introformat = FORMAT_HTML; 89 } 90 91 // Compatibility with old backups with maxtime and timed fields. 92 if (!isset($data->timelimit)) { 93 if (isset($data->timed) && isset($data->maxtime) && $data->timed) { 94 $data->timelimit = 60 * $data->maxtime; 95 } else { 96 $data->timelimit = 0; 97 } 98 } 99 // insert the lesson record 100 $newitemid = $DB->insert_record('lesson', $data); 101 // immediately after inserting "activity" record, call this 102 $this->apply_activity_instance($newitemid); 103 } 104 105 protected function process_lesson_page($data) { 106 global $DB; 107 108 $data = (object)$data; 109 $oldid = $data->id; 110 $data->lessonid = $this->get_new_parentid('lesson'); 111 112 // We'll remap all the prevpageid and nextpageid at the end, once all pages have been created 113 $data->timemodified = $this->apply_date_offset($data->timemodified); 114 $data->timecreated = $this->apply_date_offset($data->timecreated); 115 116 $newitemid = $DB->insert_record('lesson_pages', $data); 117 $this->set_mapping('lesson_page', $oldid, $newitemid, true); // Has related fileareas 118 } 119 120 protected function process_lesson_answer($data) { 121 global $DB; 122 123 $data = (object)$data; 124 $data->lessonid = $this->get_new_parentid('lesson'); 125 $data->pageid = $this->get_new_parentid('lesson_page'); 126 $data->answer = $data->answer_text; 127 $data->timemodified = $this->apply_date_offset($data->timemodified); 128 $data->timecreated = $this->apply_date_offset($data->timecreated); 129 130 // Set a dummy mapping to get the old ID so that it can be used by get_old_parentid when 131 // processing attempts. It will be corrected in after_execute 132 $this->set_mapping('lesson_answer', $data->id, 0, true); // Has related fileareas. 133 134 // Answers need to be processed in order, so we store them in an 135 // instance variable and insert them in the after_execute stage 136 $this->answers[$data->id] = $data; 137 } 138 139 protected function process_lesson_attempt($data) { 140 global $DB; 141 142 $data = (object)$data; 143 $oldid = $data->id; 144 $data->lessonid = $this->get_new_parentid('lesson'); 145 $data->pageid = $this->get_new_parentid('lesson_page'); 146 147 // We use the old answerid here as the answer isn't created until after_execute 148 $data->answerid = $this->get_old_parentid('lesson_answer'); 149 $data->userid = $this->get_mappingid('user', $data->userid); 150 $data->timeseen = $this->apply_date_offset($data->timeseen); 151 152 $newitemid = $DB->insert_record('lesson_attempts', $data); 153 $this->set_mapping('lesson_attempt', $oldid, $newitemid, true); // Has related fileareas. 154 } 155 156 protected function process_lesson_grade($data) { 157 global $DB; 158 159 $data = (object)$data; 160 $oldid = $data->id; 161 $data->lessonid = $this->get_new_parentid('lesson'); 162 $data->userid = $this->get_mappingid('user', $data->userid); 163 $data->completed = $this->apply_date_offset($data->completed); 164 165 $newitemid = $DB->insert_record('lesson_grades', $data); 166 $this->set_mapping('lesson_grade', $oldid, $newitemid); 167 } 168 169 protected function process_lesson_branch($data) { 170 global $DB; 171 172 $data = (object)$data; 173 $oldid = $data->id; 174 $data->lessonid = $this->get_new_parentid('lesson'); 175 $data->pageid = $this->get_new_parentid('lesson_page'); 176 $data->userid = $this->get_mappingid('user', $data->userid); 177 $data->timeseen = $this->apply_date_offset($data->timeseen); 178 179 $newitemid = $DB->insert_record('lesson_branch', $data); 180 } 181 182 protected function process_lesson_highscore($data) { 183 // Do not process any high score data. 184 // high scores were removed in Moodle 3.0 See MDL-49581. 185 } 186 187 protected function process_lesson_timer($data) { 188 global $DB; 189 190 $data = (object)$data; 191 $oldid = $data->id; 192 $data->lessonid = $this->get_new_parentid('lesson'); 193 $data->userid = $this->get_mappingid('user', $data->userid); 194 $data->starttime = $this->apply_date_offset($data->starttime); 195 $data->lessontime = $this->apply_date_offset($data->lessontime); 196 // Supply item that maybe missing from previous versions. 197 if (!isset($data->completed)) { 198 $data->completed = 0; 199 } 200 $newitemid = $DB->insert_record('lesson_timer', $data); 201 } 202 203 /** 204 * Process a lesson override restore 205 * @param object $data The data in object form 206 * @return void 207 */ 208 protected function process_lesson_override($data) { 209 global $DB; 210 211 $data = (object)$data; 212 $oldid = $data->id; 213 214 // Based on userinfo, we'll restore user overides or no. 215 $userinfo = $this->get_setting_value('userinfo'); 216 217 // Skip user overrides if we are not restoring userinfo. 218 if (!$userinfo && !is_null($data->userid)) { 219 return; 220 } 221 222 $data->lessonid = $this->get_new_parentid('lesson'); 223 224 if (!is_null($data->userid)) { 225 $data->userid = $this->get_mappingid('user', $data->userid); 226 } 227 if (!is_null($data->groupid)) { 228 $data->groupid = $this->get_mappingid('group', $data->groupid); 229 } 230 231 $data->available = $this->apply_date_offset($data->available); 232 $data->deadline = $this->apply_date_offset($data->deadline); 233 234 $newitemid = $DB->insert_record('lesson_overrides', $data); 235 236 // Add mapping, restore of logs needs it. 237 $this->set_mapping('lesson_override', $oldid, $newitemid); 238 } 239 240 protected function after_execute() { 241 global $DB; 242 243 // Answers must be sorted by id to ensure that they're shown correctly 244 ksort($this->answers); 245 foreach ($this->answers as $answer) { 246 $newitemid = $DB->insert_record('lesson_answers', $answer); 247 $this->set_mapping('lesson_answer', $answer->id, $newitemid, true); 248 249 // Update the lesson attempts to use the newly created answerid 250 $DB->set_field('lesson_attempts', 'answerid', $newitemid, array( 251 'lessonid' => $answer->lessonid, 252 'pageid' => $answer->pageid, 253 'answerid' => $answer->id)); 254 } 255 256 // Add lesson files, no need to match by itemname (just internally handled context). 257 $this->add_related_files('mod_lesson', 'intro', null); 258 $this->add_related_files('mod_lesson', 'mediafile', null); 259 // Add lesson page files, by lesson_page itemname 260 $this->add_related_files('mod_lesson', 'page_contents', 'lesson_page'); 261 $this->add_related_files('mod_lesson', 'page_answers', 'lesson_answer'); 262 $this->add_related_files('mod_lesson', 'page_responses', 'lesson_answer'); 263 $this->add_related_files('mod_lesson', 'essay_responses', 'lesson_attempt'); 264 265 // Remap all the restored prevpageid and nextpageid now that we have all the pages and their mappings 266 $rs = $DB->get_recordset('lesson_pages', array('lessonid' => $this->task->get_activityid()), 267 '', 'id, prevpageid, nextpageid'); 268 foreach ($rs as $page) { 269 $page->prevpageid = (empty($page->prevpageid)) ? 0 : $this->get_mappingid('lesson_page', $page->prevpageid); 270 $page->nextpageid = (empty($page->nextpageid)) ? 0 : $this->get_mappingid('lesson_page', $page->nextpageid); 271 $DB->update_record('lesson_pages', $page); 272 } 273 $rs->close(); 274 275 // Remap all the restored 'jumpto' fields now that we have all the pages and their mappings 276 $rs = $DB->get_recordset('lesson_answers', array('lessonid' => $this->task->get_activityid()), 277 '', 'id, jumpto'); 278 foreach ($rs as $answer) { 279 if ($answer->jumpto > 0) { 280 $answer->jumpto = $this->get_mappingid('lesson_page', $answer->jumpto); 281 $DB->update_record('lesson_answers', $answer); 282 } 283 } 284 $rs->close(); 285 286 // Remap all the restored 'nextpageid' fields now that we have all the pages and their mappings. 287 $rs = $DB->get_recordset('lesson_branch', array('lessonid' => $this->task->get_activityid()), 288 '', 'id, nextpageid'); 289 foreach ($rs as $answer) { 290 if ($answer->nextpageid > 0) { 291 $answer->nextpageid = $this->get_mappingid('lesson_page', $answer->nextpageid); 292 $DB->update_record('lesson_branch', $answer); 293 } 294 } 295 $rs->close(); 296 297 // Replay the upgrade step 2015030301 298 // to clean lesson answers that should be plain text. 299 // 1 = LESSON_PAGE_SHORTANSWER, 8 = LESSON_PAGE_NUMERICAL, 20 = LESSON_PAGE_BRANCHTABLE. 300 301 $sql = 'SELECT a.* 302 FROM {lesson_answers} a 303 JOIN {lesson_pages} p ON p.id = a.pageid 304 WHERE a.answerformat <> :format 305 AND a.lessonid = :lessonid 306 AND p.qtype IN (1, 8, 20)'; 307 $badanswers = $DB->get_recordset_sql($sql, array('lessonid' => $this->task->get_activityid(), 'format' => FORMAT_MOODLE)); 308 309 foreach ($badanswers as $badanswer) { 310 // Strip tags from answer text and convert back the format to FORMAT_MOODLE. 311 $badanswer->answer = strip_tags($badanswer->answer); 312 $badanswer->answerformat = FORMAT_MOODLE; 313 $DB->update_record('lesson_answers', $badanswer); 314 } 315 $badanswers->close(); 316 317 // Replay the upgrade step 2015032700. 318 // Delete any orphaned lesson_branch record. 319 if ($DB->get_dbfamily() === 'mysql') { 320 $sql = "DELETE {lesson_branch} 321 FROM {lesson_branch} 322 LEFT JOIN {lesson_pages} 323 ON {lesson_branch}.pageid = {lesson_pages}.id 324 WHERE {lesson_pages}.id IS NULL"; 325 } else { 326 $sql = "DELETE FROM {lesson_branch} 327 WHERE NOT EXISTS ( 328 SELECT 'x' FROM {lesson_pages} 329 WHERE {lesson_branch}.pageid = {lesson_pages}.id)"; 330 } 331 332 $DB->execute($sql); 333 334 // Re-map the dependency and activitylink information 335 // If a depency or activitylink has no mapping in the backup data then it could either be a duplication of a 336 // lesson, or a backup/restore of a single lesson. We have no way to determine which and whether this is the 337 // same site and/or course. Therefore we try and retrieve a mapping, but fallback to the original value if one 338 // was not found. We then test to see whether the value found is valid for the course being restored into. 339 $lesson = $DB->get_record('lesson', array('id' => $this->task->get_activityid()), 'id, course, dependency, activitylink'); 340 $updaterequired = false; 341 if (!empty($lesson->dependency)) { 342 $updaterequired = true; 343 $lesson->dependency = $this->get_mappingid('lesson', $lesson->dependency, $lesson->dependency); 344 if (!$DB->record_exists('lesson', array('id' => $lesson->dependency, 'course' => $lesson->course))) { 345 $lesson->dependency = 0; 346 } 347 } 348 349 if (!empty($lesson->activitylink)) { 350 $updaterequired = true; 351 $lesson->activitylink = $this->get_mappingid('course_module', $lesson->activitylink, $lesson->activitylink); 352 if (!$DB->record_exists('course_modules', array('id' => $lesson->activitylink, 'course' => $lesson->course))) { 353 $lesson->activitylink = 0; 354 } 355 } 356 357 if ($updaterequired) { 358 $DB->update_record('lesson', $lesson); 359 } 360 } 361 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Thu Aug 11 10:00:09 2016 | Cross-referenced by PHPXref 0.7.1 |