Exemplo n.º 1
0
 /**
  * Gets the value for the requested setting
  *
  * @param string $name
  * @return mixed
  */
 public function get_setting_value($name, $default = false)
 {
     try {
         return $this->controller->get_plan()->get_setting($name)->get_value();
     } catch (Exception $e) {
         debugging('Failed to find the setting: ' . $name, DEBUG_DEVELOPER);
         return $default;
     }
 }
Exemplo n.º 2
0
/**
 * Duplicate a module on the course.
 *
 * @param object $course The course
 * @param object $cm The course module to duplicate
 * @throws moodle_exception if the plugin doesn't support duplication
 * @return Object containing:
 * - fullcontent: The HTML markup for the created CM
 * - cmid: The CMID of the newly created CM
 * - redirect: Whether to trigger a redirect following this change
 */
function mod_duplicate_activity($course, $cm, $sr = null)
{
    global $CFG, $USER, $PAGE, $DB;
    require_once $CFG->dirroot . '/backup/util/includes/backup_includes.php';
    require_once $CFG->dirroot . '/backup/util/includes/restore_includes.php';
    require_once $CFG->libdir . '/filelib.php';
    $a = new stdClass();
    $a->modtype = get_string('modulename', $cm->modname);
    $a->modname = format_string($cm->name);
    if (!plugin_supports('mod', $cm->modname, FEATURE_BACKUP_MOODLE2)) {
        throw new moodle_exception('duplicatenosupport', 'error');
    }
    // backup the activity
    $bc = new backup_controller(backup::TYPE_1ACTIVITY, $cm->id, backup::FORMAT_MOODLE, backup::INTERACTIVE_NO, backup::MODE_IMPORT, $USER->id);
    $backupid = $bc->get_backupid();
    $backupbasepath = $bc->get_plan()->get_basepath();
    $bc->execute_plan();
    $bc->destroy();
    // restore the backup immediately
    $rc = new restore_controller($backupid, $course->id, backup::INTERACTIVE_NO, backup::MODE_IMPORT, $USER->id, backup::TARGET_CURRENT_ADDING);
    $cmcontext = context_module::instance($cm->id);
    if (!$rc->execute_precheck()) {
        $precheckresults = $rc->get_precheck_results();
        if (is_array($precheckresults) && !empty($precheckresults['errors'])) {
            if (empty($CFG->keeptempdirectoriesonbackup)) {
                fulldelete($backupbasepath);
            }
        }
    }
    $rc->execute_plan();
    // now a bit hacky part follows - we try to get the cmid of the newly
    // restored copy of the module
    $newcmid = null;
    $tasks = $rc->get_plan()->get_tasks();
    foreach ($tasks as $task) {
        error_log("Looking at a task");
        if (is_subclass_of($task, 'restore_activity_task')) {
            error_log("Looking at a restore_activity_task task");
            if ($task->get_old_contextid() == $cmcontext->id) {
                error_log("Contexts match");
                $newcmid = $task->get_moduleid();
                break;
            }
        }
    }
    // if we know the cmid of the new course module, let us move it
    // right below the original one. otherwise it will stay at the
    // end of the section
    if ($newcmid) {
        $info = get_fast_modinfo($course);
        $newcm = $info->get_cm($newcmid);
        $section = $DB->get_record('course_sections', array('id' => $cm->section, 'course' => $cm->course));
        moveto_module($newcm, $section, $cm);
        moveto_module($cm, $section, $newcm);
        // Trigger course module created event. We can trigger the event only if we know the newcmid.
        $event = \core\event\course_module_created::create_from_cm($newcm);
        $event->trigger();
    }
    rebuild_course_cache($cm->course);
    $rc->destroy();
    if (empty($CFG->keeptempdirectoriesonbackup)) {
        fulldelete($backupbasepath);
    }
    $resp = new stdClass();
    if ($newcm) {
        $courserenderer = $PAGE->get_renderer('core', 'course');
        $completioninfo = new completion_info($course);
        $modulehtml = $courserenderer->course_section_cm($course, $completioninfo, $newcm, null, array());
        $resp->fullcontent = $courserenderer->course_section_cm_list_item($course, $completioninfo, $newcm, $sr);
        $resp->cmid = $newcm->id;
    } else {
        // Trigger a redirect
        $resp->redirect = true;
    }
    return $resp;
}
 /**
  * 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;
 }
Exemplo n.º 4
0
 /**
  * Duplicates a single activity within a course.
  *
  * This is based on the code from course/modduplicate.php, but reduced for
  * simplicity.
  *
  * @param stdClass $course Course object
  * @param int $cmid Activity to duplicate
  * @return int ID of new activity
  */
 protected function duplicate($course, $cmid)
 {
     global $USER;
     // Do backup.
     $bc = new backup_controller(backup::TYPE_1ACTIVITY, $cmid, backup::FORMAT_MOODLE, backup::INTERACTIVE_NO, backup::MODE_IMPORT, $USER->id);
     $backupid = $bc->get_backupid();
     $bc->execute_plan();
     $bc->destroy();
     // Do restore.
     $rc = new restore_controller($backupid, $course->id, backup::INTERACTIVE_NO, backup::MODE_IMPORT, $USER->id, backup::TARGET_CURRENT_ADDING);
     $this->assertTrue($rc->execute_precheck());
     $rc->execute_plan();
     // Find cmid.
     $tasks = $rc->get_plan()->get_tasks();
     $cmcontext = context_module::instance($cmid);
     $newcmid = 0;
     foreach ($tasks as $task) {
         if (is_subclass_of($task, 'restore_activity_task')) {
             if ($task->get_old_contextid() == $cmcontext->id) {
                 $newcmid = $task->get_moduleid();
                 break;
             }
         }
     }
     $rc->destroy();
     if (!$newcmid) {
         throw new coding_exception('Unexpected: failure to find restored cmid');
     }
     return $newcmid;
 }
Exemplo n.º 5
0
 /**
  * Restore a course.
  *
  * @param int $backupid The backup ID.
  * @param int $courseid The course ID to restore in, or 0.
  * @param int $userid The ID of the user performing the restore.
  * @return stdClass The updated course object.
  */
 protected function restore_course($backupid, $courseid, $userid)
 {
     global $DB;
     $target = backup::TARGET_CURRENT_ADDING;
     if (!$courseid) {
         $target = backup::TARGET_NEW_COURSE;
         $categoryid = $DB->get_field_sql("SELECT MIN(id) FROM {course_categories}");
         $courseid = restore_dbops::create_new_course('Tmp', 'tmp', $categoryid);
     }
     $rc = new restore_controller($backupid, $courseid, backup::INTERACTIVE_NO, backup::MODE_GENERAL, $userid, $target);
     $target == backup::TARGET_NEW_COURSE ?: $rc->get_plan()->get_setting('overwrite_conf')->set_value(true);
     $rc->execute_precheck();
     $rc->execute_plan();
     $course = $DB->get_record('course', array('id' => $rc->get_courseid()));
     $rc->destroy();
     unset($rc);
     return $course;
 }
Exemplo n.º 6
0
/**
 * Api to duplicate a module.
 *
 * @param object $course course object.
 * @param object $cm course module object to be duplicated.
 * @since Moodle 2.8
 *
 * @throws Exception
 * @throws coding_exception
 * @throws moodle_exception
 * @throws restore_controller_exception
 *
 * @return cm_info|null cminfo object if we sucessfully duplicated the mod and found the new cm.
 */
function duplicate_module($course, $cm)
{
    global $CFG, $DB, $USER;
    require_once $CFG->dirroot . '/backup/util/includes/backup_includes.php';
    require_once $CFG->dirroot . '/backup/util/includes/restore_includes.php';
    require_once $CFG->libdir . '/filelib.php';
    $a = new stdClass();
    $a->modtype = get_string('modulename', $cm->modname);
    $a->modname = format_string($cm->name);
    if (!plugin_supports('mod', $cm->modname, FEATURE_BACKUP_MOODLE2)) {
        throw new moodle_exception('duplicatenosupport', 'error', '', $a);
    }
    // Backup the activity.
    $bc = new backup_controller(backup::TYPE_1ACTIVITY, $cm->id, backup::FORMAT_MOODLE, backup::INTERACTIVE_NO, backup::MODE_IMPORT, $USER->id);
    $backupid = $bc->get_backupid();
    $backupbasepath = $bc->get_plan()->get_basepath();
    $bc->execute_plan();
    $bc->destroy();
    // Restore the backup immediately.
    $rc = new restore_controller($backupid, $course->id, backup::INTERACTIVE_NO, backup::MODE_IMPORT, $USER->id, backup::TARGET_CURRENT_ADDING);
    $cmcontext = context_module::instance($cm->id);
    if (!$rc->execute_precheck()) {
        $precheckresults = $rc->get_precheck_results();
        if (is_array($precheckresults) && !empty($precheckresults['errors'])) {
            if (empty($CFG->keeptempdirectoriesonbackup)) {
                fulldelete($backupbasepath);
            }
        }
    }
    $rc->execute_plan();
    // Now a bit hacky part follows - we try to get the cmid of the newly
    // restored copy of the module.
    $newcmid = null;
    $tasks = $rc->get_plan()->get_tasks();
    foreach ($tasks as $task) {
        if (is_subclass_of($task, 'restore_activity_task')) {
            if ($task->get_old_contextid() == $cmcontext->id) {
                $newcmid = $task->get_moduleid();
                break;
            }
        }
    }
    // If we know the cmid of the new course module, let us move it
    // right below the original one. otherwise it will stay at the
    // end of the section.
    if ($newcmid) {
        $info = get_fast_modinfo($course);
        $newcm = $info->get_cm($newcmid);
        $section = $DB->get_record('course_sections', array('id' => $cm->section, 'course' => $cm->course));
        moveto_module($newcm, $section, $cm);
        moveto_module($cm, $section, $newcm);
        // Update calendar events with the duplicated module.
        $refresheventsfunction = $newcm->modname . '_refresh_events';
        if (function_exists($refresheventsfunction)) {
            call_user_func($refresheventsfunction, $newcm->course);
        }
        // Trigger course module created event. We can trigger the event only if we know the newcmid.
        $event = \core\event\course_module_created::create_from_cm($newcm);
        $event->trigger();
    }
    rebuild_course_cache($cm->course);
    $rc->destroy();
    if (empty($CFG->keeptempdirectoriesonbackup)) {
        fulldelete($backupbasepath);
    }
    return isset($newcm) ? $newcm : null;
}
/**
 * Duplicates a Moodle module in an existing course
 * @param  int $cmid     Course module id
 * @param  int $courseid Course id
 * @return int           New course module id
 */
function local_ltiprovider_duplicate_module($cmid, $courseid, $newidnumber, $lticontext)
{
    global $CFG, $DB, $USER;
    require_once $CFG->dirroot . '/backup/util/includes/backup_includes.php';
    require_once $CFG->dirroot . '/backup/util/includes/restore_includes.php';
    require_once $CFG->libdir . '/filelib.php';
    if (empty($USER)) {
        // Emulate session.
        cron_setup_user();
    }
    $course = $DB->get_record('course', array('id' => $courseid), '*', MUST_EXIST);
    $cm = get_coursemodule_from_id('', $cmid, 0, true, MUST_EXIST);
    $cmcontext = context_module::instance($cm->id);
    $context = context_course::instance($course->id);
    if (!plugin_supports('mod', $cm->modname, FEATURE_BACKUP_MOODLE2)) {
        $url = course_get_url($course, $cm->sectionnum, array('sr' => $sectionreturn));
        print_error('duplicatenosupport', 'error', $url, $a);
    }
    // backup the activity
    $admin = get_admin();
    $bc = new backup_controller(backup::TYPE_1ACTIVITY, $cm->id, backup::FORMAT_MOODLE, backup::INTERACTIVE_NO, backup::MODE_IMPORT, $admin->id);
    $backupid = $bc->get_backupid();
    $backupbasepath = $bc->get_plan()->get_basepath();
    $bc->execute_plan();
    $bc->destroy();
    // restore the backup immediately
    $rc = new restore_controller($backupid, $courseid, backup::INTERACTIVE_NO, backup::MODE_IMPORT, $admin->id, backup::TARGET_CURRENT_ADDING);
    if (!$rc->execute_precheck()) {
        $precheckresults = $rc->get_precheck_results();
        if (is_array($precheckresults) && !empty($precheckresults['errors'])) {
            if (empty($CFG->keeptempdirectoriesonbackup)) {
                fulldelete($backupbasepath);
            }
            print_r($precheckresults);
            die;
        }
    }
    $rc->execute_plan();
    $newcmid = null;
    $tasks = $rc->get_plan()->get_tasks();
    foreach ($tasks as $task) {
        if (is_subclass_of($task, 'restore_activity_task')) {
            if ($task->get_old_contextid() == $cmcontext->id) {
                $newcmid = $task->get_moduleid();
                break;
            }
        }
    }
    $rc->destroy();
    if ($module = $DB->get_record('course_modules', array('id' => $newcmid))) {
        $module->idnumber = $newidnumber;
        $DB->update_record('course_modules', $module);
    }
    $newtoolid = 0;
    if ($tools = $DB->get_records('local_ltiprovider', array('contextid' => $cmcontext->id))) {
        $newcmcontext = context_module::instance($newcmid);
        foreach ($tools as $tool) {
            $tool->courseid = $course->id;
            $tool->contextid = $newcmcontext->id;
            $newtoolid = $DB->insert_record('local_ltiprovider', $tool);
        }
    }
    if (!$newtoolid) {
        $tool = local_ltiprovider_create_tool($course->id, $newcmcontext->id, $lticontext);
    }
    if (empty($CFG->keeptempdirectoriesonbackup)) {
        fulldelete($backupbasepath);
    }
    return $newcmid;
}
Exemplo n.º 8
0
 public function test_mod_jclic_duplicate()
 {
     global $USER, $DB, $CFG;
     require_once $CFG->dirroot . '/backup/util/includes/backup_includes.php';
     require_once $CFG->dirroot . '/backup/util/includes/restore_includes.php';
     $this->resetAfterTest(true);
     $this->setUser(2);
     // Admin user.
     // Create a course with some availability data set.
     $generator = $this->getDataGenerator();
     $course = $generator->create_course(array('format' => 'topics', 'numsections' => 3, 'enablecompletion' => COMPLETION_ENABLED), array('createsections' => true));
     $jclic = $generator->create_module('jclic', array('course' => $course->id));
     $cmid = $jclic->cmid;
     // Do backup.
     $bc = new backup_controller(backup::TYPE_1ACTIVITY, $cmid, backup::FORMAT_MOODLE, backup::INTERACTIVE_NO, backup::MODE_IMPORT, $USER->id);
     $backupid = $bc->get_backupid();
     $bc->execute_plan();
     $bc->destroy();
     // Do restore.
     $rc = new restore_controller($backupid, $course->id, backup::INTERACTIVE_NO, backup::MODE_IMPORT, $USER->id, backup::TARGET_CURRENT_ADDING);
     $this->assertTrue($rc->execute_precheck());
     $rc->execute_plan();
     // Find cmid.
     $tasks = $rc->get_plan()->get_tasks();
     $cmcontext = context_module::instance($cmid);
     $newcmid = 0;
     foreach ($tasks as $task) {
         if (is_subclass_of($task, 'restore_activity_task')) {
             if ($task->get_old_contextid() == $cmcontext->id) {
                 $newcmid = $task->get_moduleid();
                 break;
             }
         }
     }
     $rc->destroy();
     if (!$newcmid) {
         throw new coding_exception('Unexpected: failure to find restored cmid');
     }
     // Check settings in new course.
     $newjclic = new jclic(context_module::instance($newcmid), false, false);
     $newjclicmodule = $newjclic->get_instance();
     $this->assertNotEmpty($newjclic);
     $jclic = new jclic(context_module::instance($jclic->cmid), false, false);
     $jclicmodule = $jclic->get_instance();
     $fields = (array) $jclicmodule;
     unset($fields['id']);
     unset($fields['course']);
     foreach ($fields as $key => $unused) {
         $this->assertEquals($newjclicmodule->{$key}, $jclicmodule->{$key}, "Failed on field {$key}");
     }
     // Avoid errors...
     ini_set('max_execution_time', 0);
 }
Exemplo n.º 9
0
 /**
  * Duplicates a single dataform within a course.
  *
  * This is based on the code from course/modduplicate.php, but reduced for
  * simplicity.
  *
  * @param stdClass $course Course object
  * @param int $cmid Dataform to duplicate
  * @return stdClass The new dataform instance with cmid
  */
 public function duplicate_instance($course, $cmid)
 {
     global $DB, $USER;
     // Do backup.
     $bc = new backup_controller(backup::TYPE_1ACTIVITY, $cmid, backup::FORMAT_MOODLE, backup::INTERACTIVE_NO, backup::MODE_IMPORT, $USER->id);
     $backupid = $bc->get_backupid();
     $backupbasepath = $bc->get_plan()->get_basepath();
     $bc->execute_plan();
     $bc->destroy();
     // Do restore.
     $rc = new restore_controller($backupid, $course->id, backup::INTERACTIVE_NO, backup::MODE_IMPORT, $USER->id, backup::TARGET_CURRENT_ADDING);
     if (!$rc->execute_precheck()) {
         $precheckresults = $rc->get_precheck_results();
         if (is_array($precheckresults) && !empty($precheckresults['errors'])) {
             if (empty($CFG->keeptempdirectoriesonbackup)) {
                 fulldelete($backupbasepath);
             }
         }
     }
     $rc->execute_plan();
     // Find cmid.
     $tasks = $rc->get_plan()->get_tasks();
     $cmcontext = context_module::instance($cmid);
     $newcmid = 0;
     $newactivityid = 0;
     foreach ($tasks as $task) {
         if (is_subclass_of($task, 'restore_activity_task')) {
             if ($task->get_old_contextid() == $cmcontext->id) {
                 $newcmid = $task->get_moduleid();
                 $newactivityid = $task->get_activityid();
                 break;
             }
         }
     }
     $rc->destroy();
     if (empty($CFG->keeptempdirectoriesonbackup)) {
         fulldelete($backupbasepath);
     }
     if (!$newcmid) {
         throw new coding_exception('Unexpected: failure to find restored cmid');
     }
     if (!($instance = $DB->get_record('dataform', array('id' => $newactivityid)))) {
         throw new coding_exception('Unexpected: failure to find restored activityid');
     }
     $instance->cmid = $newcmid;
     // Clear the time limit, otherwise phpunit complains.
     set_time_limit(0);
     return $instance;
 }
Exemplo n.º 10
0
 /**
  * Takes backupid and original course object as arguments and returns a new courseid.
  *
  * @param string $backupid The backupid
  * @param object $newcourse Target course object
  * @throws coding_exception Moodle coding exception
  */
 private function restore($backupid, $newcourse)
 {
     global $CFG, $DB, $USER;
     // Call restore.
     $rc = new \restore_controller($backupid, $newcourse->id, \backup::INTERACTIVE_NO, \backup::MODE_GENERAL, $USER->id, \backup::TARGET_EXISTING_ADDING);
     // Setup custom logger that prints dots.
     $logger = new logger(\backup::LOG_INFO, false, true);
     self::$currentlogger = $logger;
     $logger->potential_dot();
     $rc->get_logger()->set_next($logger);
     $rc->set_progress(new progress());
     foreach ($rc->get_plan()->get_tasks() as $taskindex => $task) {
         $settings = $task->get_settings();
         foreach ($settings as $settingindex => $setting) {
             // Set userinfo true for activity, since we controlled it
             // more accurately (i.e. true only for glossary) in backup.
             if (preg_match('/^glossary_([0-9])*_userinfo$$/', $setting->get_name())) {
                 $setting->set_value(true);
             }
             if ($taskindex == 0 && isset($this->backupsettings[$setting->get_name()])) {
                 $setting->set_value($this->backupsettings[$setting->get_name()]);
             }
         }
     }
     if (!$rc->execute_precheck()) {
         if (empty($CFG->keeptempdirectoriesonbackup)) {
             fulldelete($this->backupbasepath);
         }
         $results = print_r($rc->get_precheck_results(), true);
         print \html_writer::tag('pre', s($results));
         throw new \coding_exception('Restore precheck error.');
     }
     $logger->potential_dot();
     $rc->execute_plan();
     $rc->destroy();
     // Delete backup file.
     if (empty($CFG->keeptempdirectoriesonbackup)) {
         fulldelete($this->backupbasepath);
     }
 }
Exemplo n.º 11
0
 /**
  * Duplicate a course
  *
  * @param int $courseid
  * @param string $fullname Duplicated course fullname
  * @param string $shortname Duplicated course shortname
  * @param int $categoryid Duplicated course parent category id
  * @param int $visible Duplicated course availability
  * @param array $options List of backup options
  * @return array New course info
  * @since Moodle 2.3
  */
 public static function duplicate_course($courseid, $fullname, $shortname, $categoryid, $visible, $options)
 {
     global $CFG, $USER, $DB;
     require_once $CFG->dirroot . '/backup/util/includes/backup_includes.php';
     require_once $CFG->dirroot . '/backup/util/includes/restore_includes.php';
     // Parameter validation.
     $params = self::validate_parameters(self::duplicate_course_parameters(), array('courseid' => $courseid, 'fullname' => $fullname, 'shortname' => $shortname, 'categoryid' => $categoryid, 'visible' => $visible, 'options' => $options));
     // Context validation.
     if (!($course = $DB->get_record('course', array('id' => $params['courseid'])))) {
         throw new moodle_exception('invalidcourseid', 'error', '', $params['courseid']);
     }
     // Category where duplicated course is going to be created.
     $categorycontext = context_coursecat::instance($params['categoryid']);
     self::validate_context($categorycontext);
     // Course to be duplicated.
     $coursecontext = context_course::instance($course->id);
     self::validate_context($coursecontext);
     $backupdefaults = array('activities' => 1, 'blocks' => 1, 'filters' => 1, 'users' => 0, 'role_assignments' => 0, 'user_files' => 0, 'comments' => 0, 'completion_information' => 0, 'logs' => 0, 'histories' => 0);
     $backupsettings = array();
     // Check for backup and restore options.
     if (!empty($params['options'])) {
         foreach ($params['options'] as $option) {
             // Strict check for a correct value (allways 1 or 0, true or false).
             $value = clean_param($option['value'], PARAM_INT);
             if ($value !== 0 and $value !== 1) {
                 throw new moodle_exception('invalidextparam', 'webservice', '', $option['name']);
             }
             if (!isset($backupdefaults[$option['name']])) {
                 throw new moodle_exception('invalidextparam', 'webservice', '', $option['name']);
             }
             $backupsettings[$option['name']] = $value;
         }
     }
     // Capability checking.
     // The backup controller check for this currently, this may be redundant.
     require_capability('moodle/course:create', $categorycontext);
     require_capability('moodle/restore:restorecourse', $categorycontext);
     require_capability('moodle/backup:backupcourse', $coursecontext);
     if (!empty($backupsettings['users'])) {
         require_capability('moodle/backup:userinfo', $coursecontext);
         require_capability('moodle/restore:userinfo', $categorycontext);
     }
     // Check if the shortname is used.
     if ($foundcourses = $DB->get_records('course', array('shortname' => $shortname))) {
         foreach ($foundcourses as $foundcourse) {
             $foundcoursenames[] = $foundcourse->fullname;
         }
         $foundcoursenamestring = implode(',', $foundcoursenames);
         throw new moodle_exception('shortnametaken', '', '', $foundcoursenamestring);
     }
     // Backup the course.
     $bc = new backup_controller(backup::TYPE_1COURSE, $course->id, backup::FORMAT_MOODLE, backup::INTERACTIVE_NO, backup::MODE_SAMESITE, $USER->id);
     foreach ($backupsettings as $name => $value) {
         $bc->get_plan()->get_setting($name)->set_value($value);
     }
     $backupid = $bc->get_backupid();
     $backupbasepath = $bc->get_plan()->get_basepath();
     $bc->execute_plan();
     $results = $bc->get_results();
     $file = $results['backup_destination'];
     $bc->destroy();
     // Restore the backup immediately.
     // Check if we need to unzip the file because the backup temp dir does not contains backup files.
     if (!file_exists($backupbasepath . "/moodle_backup.xml")) {
         $file->extract_to_pathname(get_file_packer(), $backupbasepath);
     }
     // Create new course.
     $newcourseid = restore_dbops::create_new_course($params['fullname'], $params['shortname'], $params['categoryid']);
     $rc = new restore_controller($backupid, $newcourseid, backup::INTERACTIVE_NO, backup::MODE_SAMESITE, $USER->id, backup::TARGET_NEW_COURSE);
     foreach ($backupsettings as $name => $value) {
         $setting = $rc->get_plan()->get_setting($name);
         if ($setting->get_status() == backup_setting::NOT_LOCKED) {
             $setting->set_value($value);
         }
     }
     if (!$rc->execute_precheck()) {
         $precheckresults = $rc->get_precheck_results();
         if (is_array($precheckresults) && !empty($precheckresults['errors'])) {
             if (empty($CFG->keeptempdirectoriesonbackup)) {
                 fulldelete($backupbasepath);
             }
             $errorinfo = '';
             foreach ($precheckresults['errors'] as $error) {
                 $errorinfo .= $error;
             }
             if (array_key_exists('warnings', $precheckresults)) {
                 foreach ($precheckresults['warnings'] as $warning) {
                     $errorinfo .= $warning;
                 }
             }
             throw new moodle_exception('backupprecheckerrors', 'webservice', '', $errorinfo);
         }
     }
     $rc->execute_plan();
     $rc->destroy();
     $course = $DB->get_record('course', array('id' => $newcourseid), '*', MUST_EXIST);
     $course->fullname = $params['fullname'];
     $course->shortname = $params['shortname'];
     $course->visible = $params['visible'];
     // Set shortname and fullname back.
     $DB->update_record('course', $course);
     if (empty($CFG->keeptempdirectoriesonbackup)) {
         fulldelete($backupbasepath);
     }
     // Delete the course backup file created by this WebService. Originally located in the course backups area.
     $file->delete();
     return array('id' => $course->id, 'shortname' => $course->shortname);
 }
Exemplo n.º 12
0
 /**
  * https://github.com/tmuras/moosh/blob/master/Moosh/Command/Moodle23/Course/CourseRestore.php
  * @param $bkpfile
  * @param $categoryId
  */
 public static function execute($bkpfile, $categoryId)
 {
     global $CFG, $DB, $USER;
     require_once $CFG->dirroot . "/backup/util/includes/backup_includes.php";
     require_once $CFG->dirroot . "/backup/util/includes/restore_includes.php";
     if (empty($CFG->tempdir)) {
         $CFG->tempdir = $CFG->dataroot . DIRECTORY_SEPARATOR . 'temp';
     }
     //unzip into $CFG->tempdir / "backup" / "auto_restore_" . $split[1];
     $backupdir = "moosh_restore_" . uniqid();
     $path = $CFG->tempdir . DIRECTORY_SEPARATOR . "backup" . DIRECTORY_SEPARATOR . $backupdir;
     /** @var $fp file_packer */
     $fp = get_file_packer('application/vnd.moodle.backup');
     $fp->extract_to_pathname($bkpfile, $path);
     //extract original full & short names
     $xmlfile = $path . DIRECTORY_SEPARATOR . "course" . DIRECTORY_SEPARATOR . "course.xml";
     // Different XML file in Moodle 1.9 backup
     if (!file_exists($xmlfile)) {
         $xmlfile = $path . DIRECTORY_SEPARATOR . "moodle.xml";
     }
     $xml = simplexml_load_file($xmlfile);
     $fullname = $xml->xpath('/course/fullname');
     if (!$fullname) {
         $fullname = $xml->xpath('/MOODLE_BACKUP/COURSE/HEADER/FULLNAME');
     }
     $shortname = $xml->xpath('/course/shortname');
     if (!$shortname) {
         $shortname = $xml->xpath('/MOODLE_BACKUP/COURSE/HEADER/SHORTNAME');
     }
     $fullname = (string) $fullname[0];
     $shortname = (string) $shortname[0];
     if (!$shortname) {
         cli_error('No shortname in the backup file.');
     }
     if (!$fullname) {
         $fullname = $shortname;
     }
     $courseid = \restore_dbops::create_new_course($fullname, $shortname, $categoryId);
     $rc = new \restore_controller($backupdir, $courseid, \backup::INTERACTIVE_NO, \backup::MODE_GENERAL, 2, \backup::TARGET_NEW_COURSE);
     echo "Restoring (new course id,shortname,destination category): {$courseid}, {$shortname}," . $categoryId . "\n";
     if ($rc->get_status() == \backup::STATUS_REQUIRE_CONV) {
         $rc->convert();
     }
     $plan = $rc->get_plan();
     //TODO: valider les options réquises.
     $restopt = array('activities' => 1, 'blocks' => 1, 'filters' => 1, 'users' => 0, 'role_assignments' => 1, 'comments' => 0, 'logs' => 0, 'grade_histories' => 0);
     foreach ($restopt as $name => $value) {
         $setting = $plan->get_setting($name);
         if ($setting->get_status() == \backup_setting::NOT_LOCKED) {
             $setting->set_value($value);
         }
     }
     $rc->execute_precheck();
     $rc->execute_plan();
     $rc->destroy();
     echo "New course ID for '{$shortname}': {$courseid} in {$categoryId}\n";
     // Ajouter le idnumber dans le nouveau cours.
     $c = $DB->get_record('course', array('id' => $courseid));
     $c->idnumber = self::get_idnumber($shortname);
     $DB->update_record('course', $c);
 }
 /**
  * Backs a course up and restores it.
  *
  * @param stdClass $course Course object to backup
  * @param int $newdate If non-zero, specifies custom date for new course
  * @return int ID of newly restored course
  */
 protected function backup_and_restore($course, $newdate = 0)
 {
     global $USER, $CFG;
     // Turn off file logging, otherwise it can't delete the file (Windows).
     $CFG->backup_file_logger_level = backup::LOG_NONE;
     // Do backup with default settings. MODE_IMPORT means it will just
     // create the directory and not zip it.
     $bc = new backup_controller(backup::TYPE_1COURSE, $course->id, backup::FORMAT_MOODLE, backup::INTERACTIVE_NO, backup::MODE_IMPORT, $USER->id);
     $backupid = $bc->get_backupid();
     $bc->execute_plan();
     $bc->destroy();
     // Do restore to new course with default settings.
     $newcourseid = restore_dbops::create_new_course($course->fullname, $course->shortname . '_2', $course->category);
     $rc = new restore_controller($backupid, $newcourseid, backup::INTERACTIVE_NO, backup::MODE_GENERAL, $USER->id, backup::TARGET_NEW_COURSE);
     if ($newdate) {
         $rc->get_plan()->get_setting('course_startdate')->set_value($newdate);
     }
     $this->assertTrue($rc->execute_precheck());
     $rc->execute_plan();
     $rc->destroy();
     return $newcourseid;
 }
Exemplo n.º 14
0
        if (empty($CFG->keeptempdirectoriesonbackup)) {
            fulldelete($backupbasepath);
        }
        echo $output->header();
        echo $output->precheck_notices($precheckresults);
        $url = course_get_url($course, $cm->sectionnum, array('sr' => $sectionreturn));
        echo $output->continue_button($url);
        echo $output->footer();
        die;
    }
}
$rc->execute_plan();
// now a bit hacky part follows - we try to get the cmid of the newly
// restored copy of the module
$newcmid = null;
$tasks = $rc->get_plan()->get_tasks();
foreach ($tasks as $task) {
    if (is_subclass_of($task, 'restore_activity_task')) {
        if ($task->get_old_contextid() == $cmcontext->id) {
            $newcmid = $task->get_moduleid();
            break;
        }
    }
}
// if we know the cmid of the new course module, let us move it
// right below the original one. otherwise it will stay at the
// end of the section
if ($newcmid) {
    $newcm = get_coursemodule_from_id('', $newcmid, $course->id, true, MUST_EXIST);
    moveto_module($newcm, $section, $cm);
    moveto_module($cm, $section, $newcm);
Exemplo n.º 15
0
 /**
  *
  */
 public function apply_preset($userpreset, $torestorer = true)
 {
     global $DB, $CFG, $USER;
     $df = mod_dataform_dataform::instance($this->_dataformid);
     // Extract the backup file to the temp folder.
     $folder = 'tmp-' . $df->context->id . '-' . time();
     $backuptempdir = make_temp_directory("backup/{$folder}");
     $fs = get_file_storage();
     $file = $fs->get_file_by_id($userpreset);
     $zipper = get_file_packer($file->get_mimetype());
     $file->extract_to_pathname($zipper, $backuptempdir);
     require_once "{$CFG->dirroot}/backup/util/includes/restore_includes.php";
     // Required preparation due to restorer assumption that this should be a new activity
     // Anonymous users cleanup.
     $DB->delete_records_select('user', $DB->sql_like('firstname', '?'), array('%anonfirstname%'));
     // Grading area removal.
     $DB->delete_records('grading_areas', array('contextid' => $df->context->id));
     $transaction = $DB->start_delegated_transaction();
     $rc = new restore_controller($folder, $df->course->id, backup::INTERACTIVE_NO, backup::MODE_GENERAL, $USER->id, backup::TARGET_CURRENT_ADDING);
     if (!$rc->execute_precheck()) {
         $precheckresults = $rc->get_precheck_results();
         if (is_array($precheckresults) && !empty($precheckresults['errors'])) {
             if (empty($CFG->keeptempdirectoriesonbackup)) {
                 fulldelete($backuptempdir);
             }
         }
     }
     // Get the dataform restore activity task.
     $tasks = $rc->get_plan()->get_tasks();
     $dataformtask = null;
     foreach ($tasks as &$task) {
         if ($task instanceof restore_dataform_activity_task) {
             $dataformtask =& $task;
             break;
         }
     }
     if ($dataformtask) {
         $dataformtask->set_activityid($df->id);
         $dataformtask->set_moduleid($df->cm->id);
         $dataformtask->set_contextid($df->context->id);
         if ($torestorer) {
             $dataformtask->set_ownerid($USER->id);
         }
         $rc->set_status(backup::STATUS_AWAITING);
         $rc->execute_plan();
         $transaction->allow_commit();
         // Rc cleanup.
         $rc->destroy();
         // Anonymous users cleanup.
         $DB->delete_records_select('user', $DB->sql_like('firstname', '?'), array('%anonfirstname%'));
         return true;
     } else {
         $rc->destroy();
     }
     return false;
 }
Exemplo n.º 16
0
    /**
     * Imports a course
     *
     * @param int $importfrom The id of the course we are importing from
     * @param int $importto The id of the course we are importing to
     * @param bool $deletecontent Whether to delete the course we are importing to content
     * @param array $options List of backup options
     * @return null
     * @since Moodle 2.4
     */
    public static function import_course($importfrom, $importto, $deletecontent = 0, $options = array()) {
        global $CFG, $USER, $DB;
        require_once($CFG->dirroot . '/backup/util/includes/backup_includes.php');
        require_once($CFG->dirroot . '/backup/util/includes/restore_includes.php');

        // Parameter validation.
        $params = self::validate_parameters(
            self::import_course_parameters(),
            array(
                'importfrom' => $importfrom,
                'importto' => $importto,
                'deletecontent' => $deletecontent,
                'options' => $options
            )
        );

        if ($params['deletecontent'] !== 0 and $params['deletecontent'] !== 1) {
            throw new moodle_exception('invalidextparam', 'webservice', '', $params['deletecontent']);
        }

        // Context validation.

        if (! ($importfrom = $DB->get_record('course', array('id'=>$params['importfrom'])))) {
            throw new moodle_exception('invalidcourseid', 'error');
        }

        if (! ($importto = $DB->get_record('course', array('id'=>$params['importto'])))) {
            throw new moodle_exception('invalidcourseid', 'error');
        }

        $importfromcontext = context_course::instance($importfrom->id);
        self::validate_context($importfromcontext);

        $importtocontext = context_course::instance($importto->id);
        self::validate_context($importtocontext);

        $backupdefaults = array(
            'activities' => 1,
            'blocks' => 1,
            'filters' => 1
        );

        $backupsettings = array();

        // Check for backup and restore options.
        if (!empty($params['options'])) {
            foreach ($params['options'] as $option) {

                // Strict check for a correct value (allways 1 or 0, true or false).
                $value = clean_param($option['value'], PARAM_INT);

                if ($value !== 0 and $value !== 1) {
                    throw new moodle_exception('invalidextparam', 'webservice', '', $option['name']);
                }

                if (!isset($backupdefaults[$option['name']])) {
                    throw new moodle_exception('invalidextparam', 'webservice', '', $option['name']);
                }

                $backupsettings[$option['name']] = $value;
            }
        }

        // Capability checking.

        require_capability('moodle/backup:backuptargetimport', $importfromcontext);
        require_capability('moodle/restore:restoretargetimport', $importtocontext);

        $bc = new backup_controller(backup::TYPE_1COURSE, $importfrom->id, backup::FORMAT_MOODLE,
                backup::INTERACTIVE_NO, backup::MODE_IMPORT, $USER->id);

        foreach ($backupsettings as $name => $value) {
            $bc->get_plan()->get_setting($name)->set_value($value);
        }

        $backupid       = $bc->get_backupid();
        $backupbasepath = $bc->get_plan()->get_basepath();

        $bc->execute_plan();
        $bc->destroy();

        // Restore the backup immediately.

        // Check if we must delete the contents of the destination course.
        if ($params['deletecontent']) {
            $restoretarget = backup::TARGET_EXISTING_DELETING;
        } else {
            $restoretarget = backup::TARGET_EXISTING_ADDING;
        }

        $rc = new restore_controller($backupid, $importto->id,
                backup::INTERACTIVE_NO, backup::MODE_IMPORT, $USER->id, $restoretarget);

        foreach ($backupsettings as $name => $value) {
            $rc->get_plan()->get_setting($name)->set_value($value);
        }

        if (!$rc->execute_precheck()) {
            $precheckresults = $rc->get_precheck_results();
            if (is_array($precheckresults) && !empty($precheckresults['errors'])) {
                if (empty($CFG->keeptempdirectoriesonbackup)) {
                    fulldelete($backupbasepath);
                }

                $errorinfo = '';

                foreach ($precheckresults['errors'] as $error) {
                    $errorinfo .= $error;
                }

                if (array_key_exists('warnings', $precheckresults)) {
                    foreach ($precheckresults['warnings'] as $warning) {
                        $errorinfo .= $warning;
                    }
                }

                throw new moodle_exception('backupprecheckerrors', 'webservice', '', $errorinfo);
            }
        } else {
            if ($restoretarget == backup::TARGET_EXISTING_DELETING) {
                restore_dbops::delete_course_content($importto->id);
            }
        }

        $rc->execute_plan();
        $rc->destroy();

        if (empty($CFG->keeptempdirectoriesonbackup)) {
            fulldelete($backupbasepath);
        }

        return null;
    }
Exemplo n.º 17
0
 /**
  * Returns welcome message.
  * @param string $sourcecourse
  * @param string $destinationcourse
  * @param bool $excludequestionbank
  * @return string welcome message.
  */
 public static function copy_content($sourcecourse = '', $destinationcourse = '', $excludequestionbank = false)
 {
     global $DB;
     try {
         $courseid = $destinationcourse;
         $course = $DB->get_record('course', array('id' => $courseid), '*', MUST_EXIST);
         // The id of the course we are importing FROM (will only be set if past first stage.
         $importcourseid = $sourcecourse;
         // The target method for the restore (adding or deleting).
         $restoretarget = backup::TARGET_EXISTING_DELETING;
         $importcourse = $DB->get_record('course', array('id' => $importcourseid), '*', MUST_EXIST);
         $bc = new backup_controller(backup::TYPE_1COURSE, $importcourse->id, backup::FORMAT_MOODLE, backup::INTERACTIVE_NO, backup::MODE_IMPORT, 2);
         $bc->get_plan()->get_setting('users')->set_status(backup_setting::LOCKED_BY_CONFIG);
         $settings = $bc->get_plan()->get_settings();
         // If excludequestionbank is set, do not backup question bank.
         if ($excludequestionbank && $bc->get_plan()->setting_exists('questionbank')) {
             $setting = $bc->get_plan()->get_setting('questionbank');
             $setting->set_value(0);
         }
         // For the initial stage we want to hide all locked settings and if there are.
         // No visible settings move to the next stage.
         foreach ($settings as $setting) {
             if ($setting->get_status() !== backup_setting::NOT_LOCKED) {
                 $setting->set_visibility(backup_setting::HIDDEN);
             }
         }
         $backupid = $bc->get_backupid();
         $bc->execute_plan();
         restore_dbops::delete_course_content($course->id);
         $rc = new restore_controller($backupid, $course->id, backup::INTERACTIVE_NO, backup::MODE_GENERAL, 2, $restoretarget);
         $rc->execute_precheck();
         // If excludequestionbank is set, do not restore quizzes.
         if ($excludequestionbank) {
             $settings = $rc->get_plan()->get_settings();
             foreach ($settings as $setting) {
                 if (preg_match('/(quiz)(_)(\\d+)(_)(included)/', $setting->get_name())) {
                     $rc->get_plan()->get_setting($setting->get_name())->set_value(0);
                 }
             }
         }
         $rc->execute_plan();
         $results = $rc->get_results();
         if (isset($results["file_missing_in_backup"])) {
             if (extension_loaded('newrelic')) {
                 newrelic_notice_error($results["file_missing_in_backup"], new Exception('Missing file in backup'));
             }
         }
         $course = $DB->get_record('course', array('id' => $courseid), '*', MUST_EXIST);
         $importcourse->id = $course->id;
         $importcourse->modinfo = $course->modinfo;
         $importcourse->idnumber = $course->idnumber;
         $importcourse->fullname = $course->fullname;
         $importcourse->shortname = $course->shortname;
         $importcourse->startdate = $course->startdate;
         $importcourse->sectioncache = null;
         $sourceformat = $DB->get_field('course', 'format', array('id' => $importcourseid), MUST_EXIST);
         if (!($coursenumsections = $DB->get_field('course_format_options', 'value', array('courseid' => $importcourseid, 'name' => 'numsections', 'format' => $sourceformat)))) {
             $coursenumsections = 10;
         }
         if ($courseformatconfig = $DB->get_record('course_format_options', array('courseid' => $course->id, 'name' => 'numsections', 'format' => $sourceformat))) {
             $courseformatconfig->value = $coursenumsections;
             $DB->update_record('course_format_options', $courseformatconfig);
         } else {
             $courseformatconfig = new stdClass();
             $courseformatconfig->courseid = $course->id;
             $courseformatconfig->name = 'numsections';
             $courseformatconfig->value = $coursenumsections;
             $courseformatconfig->format = $sourceformat;
             $courseformatconfig->sectionid = 0;
             $DB->insert_record('course_format_options', $courseformatconfig);
         }
         $DB->update_record('course', $importcourse);
         return 'success';
     } catch (Exception $e) {
         if (extension_loaded('newrelic')) {
             newrelic_notice_error($e->getMessage(), $e);
         }
         return 'Exception ' . $e->getMessage();
     }
 }