Beispiel #1
0
 /**
  * Function responsible for executing the tasks of any plan
  */
 public function execute()
 {
     if ($this->controller->get_status() != backup::STATUS_AWAITING) {
         throw new restore_controller_exception('restore_not_executable_awaiting_required', $this->controller->get_status());
     }
     $this->controller->set_status(backup::STATUS_EXECUTING);
     parent::execute();
     $this->controller->set_status(backup::STATUS_FINISHED_OK);
     events_trigger('course_restored', (object) array('courseid' => $this->get_courseid(), 'userid' => $this->get_userid(), 'type' => $this->controller->get_type(), 'target' => $this->controller->get_target(), 'mode' => $this->controller->get_mode(), 'operation' => $this->controller->get_operation(), 'samesite' => $this->controller->is_samesite()));
 }
 /**
  * Function responsible for executing the tasks of any plan
  */
 public function execute()
 {
     if ($this->controller->get_status() != backup::STATUS_AWAITING) {
         throw new restore_controller_exception('restore_not_executable_awaiting_required', $this->controller->get_status());
     }
     $this->controller->set_status(backup::STATUS_EXECUTING);
     parent::execute();
     $this->controller->set_status(backup::STATUS_FINISHED_OK);
     // Trigger a course restored event.
     $event = \core\event\course_restored::create(array('objectid' => $this->get_courseid(), 'userid' => $this->get_userid(), 'context' => context_course::instance($this->get_courseid()), 'other' => array('type' => $this->controller->get_type(), 'target' => $this->controller->get_target(), 'mode' => $this->controller->get_mode(), 'operation' => $this->controller->get_operation(), 'samesite' => $this->controller->is_samesite())));
     $event->trigger();
 }
Beispiel #3
0
 /**
  * Function responsible for executing the tasks of any plan
  */
 public function execute()
 {
     if ($this->controller->get_status() != backup::STATUS_AWAITING) {
         throw new restore_controller_exception('restore_not_executable_awaiting_required', $this->controller->get_status());
     }
     $this->controller->set_status(backup::STATUS_EXECUTING);
     parent::execute();
     $this->controller->set_status(backup::STATUS_FINISHED_OK);
     // Check if we are restoring a course.
     if ($this->controller->get_type() === backup::TYPE_1COURSE) {
         // Check to see if we are on the same site to pass original course info.
         $issamesite = $this->controller->is_samesite();
         $otherarray = array('type' => $this->controller->get_type(), 'target' => $this->controller->get_target(), 'mode' => $this->controller->get_mode(), 'operation' => $this->controller->get_operation(), 'samesite' => $issamesite);
         if ($this->controller->is_samesite()) {
             $otherarray['originalcourseid'] = $this->controller->get_info()->original_course_id;
         }
         // Trigger a course restored event.
         $event = \core\event\course_restored::create(array('objectid' => $this->get_courseid(), 'userid' => $this->get_userid(), 'context' => context_course::instance($this->get_courseid()), 'other' => $otherarray));
         $event->trigger();
     }
 }
Beispiel #4
0
 public function is_samesite()
 {
     return $this->controller->is_samesite();
 }
Beispiel #5
0
 /**
  * Test that triggering a course_restored event works as expected.
  */
 public function test_course_restored_event()
 {
     global $CFG;
     // Get the necessary files to perform backup and restore.
     require_once $CFG->dirroot . '/backup/util/includes/backup_includes.php';
     require_once $CFG->dirroot . '/backup/util/includes/restore_includes.php';
     $this->resetAfterTest();
     // Set to admin user.
     $this->setAdminUser();
     // The user id is going to be 2 since we are the admin user.
     $userid = 2;
     // Create a course.
     $course = $this->getDataGenerator()->create_course();
     // Create backup file and save it to the backup location.
     $bc = new backup_controller(backup::TYPE_1COURSE, $course->id, backup::FORMAT_MOODLE, backup::INTERACTIVE_NO, backup::MODE_GENERAL, $userid);
     $bc->execute_plan();
     $results = $bc->get_results();
     $file = $results['backup_destination'];
     $fp = get_file_packer('application/vnd.moodle.backup');
     $filepath = $CFG->dataroot . '/temp/backup/test-restore-course-event';
     $file->extract_to_pathname($fp, $filepath);
     $bc->destroy();
     unset($bc);
     // Now we want to catch the restore course event.
     $sink = $this->redirectEvents();
     // Now restore the course to trigger the event.
     $rc = new restore_controller('test-restore-course-event', $course->id, backup::INTERACTIVE_NO, backup::MODE_GENERAL, $userid, backup::TARGET_NEW_COURSE);
     $rc->execute_precheck();
     $rc->execute_plan();
     // Capture the event.
     $events = $sink->get_events();
     $sink->close();
     // Validate the event.
     $event = array_pop($events);
     $this->assertInstanceOf('\\core\\event\\course_restored', $event);
     $this->assertEquals('course', $event->objecttable);
     $this->assertEquals($rc->get_courseid(), $event->objectid);
     $this->assertEquals(context_course::instance($rc->get_courseid())->id, $event->contextid);
     $this->assertEquals('course_restored', $event->get_legacy_eventname());
     $legacydata = (object) array('courseid' => $rc->get_courseid(), 'userid' => $rc->get_userid(), 'type' => $rc->get_type(), 'target' => $rc->get_target(), 'mode' => $rc->get_mode(), 'operation' => $rc->get_operation(), 'samesite' => $rc->is_samesite());
     $url = new moodle_url('/course/view.php', array('id' => $event->objectid));
     $this->assertEquals($url, $event->get_url());
     $this->assertEventLegacyData($legacydata, $event);
     $this->assertEventContextNotUsed($event);
     // Destroy the resource controller since we are done using it.
     $rc->destroy();
     unset($rc);
 }
 /**
  * Entry point for all the prechecks to be performed before restore
  *
  * Returns empty array or warnings/errors array
  */
 public static function execute_prechecks(restore_controller $controller, $droptemptablesafter = false)
 {
     global $CFG;
     $errors = array();
     $warnings = array();
     // Some handy vars to be used along the prechecks
     $samesite = $controller->is_samesite();
     $restoreusers = $controller->get_plan()->get_setting('users')->get_value();
     $hasmnetusers = (int) $controller->get_info()->mnet_remoteusers;
     $restoreid = $controller->get_restoreid();
     $courseid = $controller->get_courseid();
     $userid = $controller->get_userid();
     $rolemappings = $controller->get_info()->role_mappings;
     $progress = $controller->get_progress();
     // Start tracking progress. There are currently 8 major steps, corresponding
     // to $majorstep++ lines in this code; we keep track of the total so as to
     // verify that it's still correct. If you add a major step, you need to change
     // the total here.
     $majorstep = 1;
     $majorsteps = 8;
     $progress->start_progress('Carrying out pre-restore checks', $majorsteps);
     // Load all the included tasks to look for inforef.xml files
     $inforeffiles = array();
     $tasks = restore_dbops::get_included_tasks($restoreid);
     $progress->start_progress('Listing inforef files', count($tasks));
     $minorstep = 1;
     foreach ($tasks as $task) {
         // Add the inforef.xml file if exists
         $inforefpath = $task->get_taskbasepath() . '/inforef.xml';
         if (file_exists($inforefpath)) {
             $inforeffiles[] = $inforefpath;
         }
         $progress->progress($minorstep++);
     }
     $progress->end_progress();
     $progress->progress($majorstep++);
     // Create temp tables
     restore_controller_dbops::create_restore_temp_tables($controller->get_restoreid());
     // Check we are restoring one backup >= $min20version (very first ok ever)
     $min20version = 2010072300;
     if ($controller->get_info()->backup_version < $min20version) {
         $message = new stdclass();
         $message->backup = $controller->get_info()->backup_version;
         $message->min = $min20version;
         $errors[] = get_string('errorminbackup20version', 'backup', $message);
     }
     // Compare Moodle's versions
     if ($CFG->version < $controller->get_info()->moodle_version) {
         $message = new stdclass();
         $message->serverversion = $CFG->version;
         $message->serverrelease = $CFG->release;
         $message->backupversion = $controller->get_info()->moodle_version;
         $message->backuprelease = $controller->get_info()->moodle_release;
         $warnings[] = get_string('noticenewerbackup', '', $message);
     }
     // Error if restoring over frontpage
     // TODO: Review the whole restore process in order to transform this into one warning (see 1.9)
     if ($controller->get_courseid() == SITEID) {
         $errors[] = get_string('errorrestorefrontpage', 'backup');
     }
     // If restoring to different site and restoring users and backup has mnet users warn/error
     if (!$samesite && $restoreusers && $hasmnetusers) {
         // User is admin (can create users at sysctx), warn
         if (has_capability('moodle/user:create', context_system::instance(), $controller->get_userid())) {
             $warnings[] = get_string('mnetrestore_extusers_admin', 'admin');
             // User not admin
         } else {
             $errors[] = get_string('mnetrestore_extusers_noadmin', 'admin');
         }
     }
     // Load all the inforef files, we are going to need them
     $progress->start_progress('Loading temporary IDs', count($inforeffiles));
     $minorstep = 1;
     foreach ($inforeffiles as $inforeffile) {
         // Load each inforef file to temp_ids.
         restore_dbops::load_inforef_to_tempids($restoreid, $inforeffile, $progress);
         $progress->progress($minorstep++);
     }
     $progress->end_progress();
     $progress->progress($majorstep++);
     // If restoring users, check we are able to create all them
     if ($restoreusers) {
         $file = $controller->get_plan()->get_basepath() . '/users.xml';
         // Load needed users to temp_ids.
         restore_dbops::load_users_to_tempids($restoreid, $file, $progress);
         $progress->progress($majorstep++);
         if ($problems = restore_dbops::precheck_included_users($restoreid, $courseid, $userid, $samesite, $progress)) {
             $errors = array_merge($errors, $problems);
         }
     } else {
         // To ensure consistent number of steps in progress tracking,
         // mark progress even though we didn't do anything.
         $progress->progress($majorstep++);
     }
     $progress->progress($majorstep++);
     // Note: restore won't create roles at all. Only mapping/skip!
     $file = $controller->get_plan()->get_basepath() . '/roles.xml';
     restore_dbops::load_roles_to_tempids($restoreid, $file);
     // Load needed roles to temp_ids
     if ($problems = restore_dbops::precheck_included_roles($restoreid, $courseid, $userid, $samesite, $rolemappings)) {
         $errors = array_key_exists('errors', $problems) ? array_merge($errors, $problems['errors']) : $errors;
         $warnings = array_key_exists('warnings', $problems) ? array_merge($warnings, $problems['warnings']) : $warnings;
     }
     $progress->progress($majorstep++);
     // Check we are able to restore and the categories and questions
     $file = $controller->get_plan()->get_basepath() . '/questions.xml';
     restore_dbops::load_categories_and_questions_to_tempids($restoreid, $file);
     if ($problems = restore_dbops::precheck_categories_and_questions($restoreid, $courseid, $userid, $samesite)) {
         $errors = array_key_exists('errors', $problems) ? array_merge($errors, $problems['errors']) : $errors;
         $warnings = array_key_exists('warnings', $problems) ? array_merge($warnings, $problems['warnings']) : $warnings;
     }
     $progress->progress($majorstep++);
     // Prepare results.
     $results = array();
     if (!empty($errors)) {
         $results['errors'] = $errors;
     }
     if (!empty($warnings)) {
         $results['warnings'] = $warnings;
     }
     // Warnings/errors detected or want to do so explicitly, drop temp tables
     if (!empty($results) || $droptemptablesafter) {
         restore_controller_dbops::drop_restore_temp_tables($controller->get_restoreid());
     }
     // Finish progress and check we got the initial number of steps right.
     $progress->progress($majorstep++);
     if ($majorstep != $majorsteps) {
         throw new coding_exception('Progress step count wrong: ' . $majorstep);
     }
     $progress->end_progress();
     return $results;
 }