Example #1
0
 /**
  * C'est une copie de la commande moosh
  * https://github.com/tmuras/moosh/blob/master/Moosh/Command/Moodle23/Course/CourseBackup.php
  * @param $shortname
  * @param $savepath
  */
 public static function execute($shortname, $savepath)
 {
     global $CFG, $DB, $USER;
     require_once $CFG->dirroot . '/backup/util/includes/backup_includes.php';
     error_reporting(E_ALL);
     ini_set('display_errors', true);
     //check if course id exists
     $course = $DB->get_record('course', array('shortname' => $shortname), '*', MUST_EXIST);
     $shortname = str_replace(' ', '_', $course->shortname);
     $filename = $savepath . '/backup_' . str_replace('/', '_', $shortname) . '_' . date('Y.m.d') . '.mbz';
     //check if destination file does not exist and can be created
     if (file_exists($filename)) {
         cli_error("File '{$filename}' already exists, I will not over-write it.");
     }
     $bc = new \backup_controller(\backup::TYPE_1COURSE, $course->id, \backup::FORMAT_MOODLE, \backup::INTERACTIVE_YES, \backup::MODE_GENERAL, 2);
     // 2 correspond à user admin!
     $tasks = $bc->get_plan()->get_tasks();
     foreach ($tasks as &$task) {
         if ($task instanceof \backup_root_task) {
             $setting = $task->get_setting('users');
             $setting->set_value('0');
             $setting = $task->get_setting('anonymize');
             $setting->set_value('1');
             $setting = $task->get_setting('role_assignments');
             $setting->set_value('1');
             $setting = $task->get_setting('filters');
             $setting->set_value('1');
             $setting = $task->get_setting('comments');
             $setting->set_value('0');
             $setting = $task->get_setting('logs');
             $setting->set_value('0');
             $setting = $task->get_setting('grade_histories');
             $setting->set_value('0');
         }
     }
     $bc->set_status(\backup::STATUS_AWAITING);
     $bc->execute_plan();
     $result = $bc->get_results();
     if (isset($result['backup_destination']) && $result['backup_destination']) {
         $file = $result['backup_destination'];
         /** @var $file stored_file */
         if (!$file->copy_content_to($filename)) {
             cli_error("Problems copying final backup to '" . $filename . "'");
         } else {
             printf("%s\n", $filename);
         }
     } else {
         echo $bc->get_backupid();
     }
 }
 /**
  * Launches a automated backup routine for the given course
  *
  * @param stdClass $course
  * @param int $starttime
  * @param int $userid
  * @return bool
  */
 public static function launch_automated_backup($course, $starttime, $userid)
 {
     $outcome = self::BACKUP_STATUS_OK;
     $config = get_config('backup');
     $dir = $config->backup_auto_destination;
     $storage = (int) $config->backup_auto_storage;
     $bc = new backup_controller(backup::TYPE_1COURSE, $course->id, backup::FORMAT_MOODLE, backup::INTERACTIVE_NO, backup::MODE_AUTOMATED, $userid);
     try {
         // Set the default filename.
         $format = $bc->get_format();
         $type = $bc->get_type();
         $id = $bc->get_id();
         $users = $bc->get_plan()->get_setting('users')->get_value();
         $anonymised = $bc->get_plan()->get_setting('anonymize')->get_value();
         $bc->get_plan()->get_setting('filename')->set_value(backup_plan_dbops::get_default_backup_filename($format, $type, $id, $users, $anonymised));
         $bc->set_status(backup::STATUS_AWAITING);
         $bc->execute_plan();
         $results = $bc->get_results();
         $outcome = self::outcome_from_results($results);
         $file = $results['backup_destination'];
         // May be empty if file already moved to target location.
         if (empty($dir) && $storage !== 0) {
             // This is intentionally left as a warning instead of an error because of the current behaviour of backup settings.
             // See MDL-48266 for details.
             $bc->log('No directory specified for automated backups', backup::LOG_WARNING);
             $outcome = self::BACKUP_STATUS_WARNING;
         } else {
             if ($storage !== 0 && (!file_exists($dir) || !is_dir($dir) || !is_writable($dir))) {
                 // If we need to copy the backup file to an external dir and it is not writable, change status to error.
                 $bc->log('Specified backup directory is not writable - ', backup::LOG_ERROR, $dir);
                 $dir = null;
                 $outcome = self::BACKUP_STATUS_ERROR;
             }
         }
         // Copy file only if there was no error.
         if ($file && !empty($dir) && $storage !== 0 && $outcome != self::BACKUP_STATUS_ERROR) {
             $filename = backup_plan_dbops::get_default_backup_filename($format, $type, $course->id, $users, $anonymised, !$config->backup_shortname);
             if (!$file->copy_content_to($dir . '/' . $filename)) {
                 $bc->log('Attempt to copy backup file to the specified directory failed - ', backup::LOG_ERROR, $dir);
                 $outcome = self::BACKUP_STATUS_ERROR;
             }
             if ($outcome != self::BACKUP_STATUS_ERROR && $storage === 1) {
                 if (!$file->delete()) {
                     $outcome = self::BACKUP_STATUS_WARNING;
                     $bc->log('Attempt to delete the backup file from course automated backup area failed - ', backup::LOG_WARNING, $file->get_filename());
                 }
             }
         }
     } catch (moodle_exception $e) {
         $bc->log('backup_auto_failed_on_course', backup::LOG_ERROR, $course->shortname);
         // Log error header.
         $bc->log('Exception: ' . $e->errorcode, backup::LOG_ERROR, $e->a, 1);
         // Log original exception problem.
         $bc->log('Debug: ' . $e->debuginfo, backup::LOG_DEBUG, null, 1);
         // Log original debug information.
         $outcome = self::BACKUP_STATUS_ERROR;
     }
     // Delete the backup file immediately if something went wrong.
     if ($outcome === self::BACKUP_STATUS_ERROR) {
         // Delete the file from file area if exists.
         if (!empty($file)) {
             $file->delete();
         }
         // Delete file from external storage if exists.
         if ($storage !== 0 && !empty($filename) && file_exists($dir . '/' . $filename)) {
             @unlink($dir . '/' . $filename);
         }
     }
     $bc->destroy();
     unset($bc);
     return $outcome;
 }
 /**
  * Launches a automated backup routine for the given course
  *
  * @param stdClass $course
  * @param int $starttime
  * @param int $userid
  * @return bool
  */
 public static function launch_automated_backup($course, $starttime, $userid)
 {
     $config = get_config('backup');
     $bc = new backup_controller(backup::TYPE_1COURSE, $course->id, backup::FORMAT_MOODLE, backup::INTERACTIVE_NO, backup::MODE_AUTOMATED, $userid);
     try {
         $settings = array('users' => 'backup_auto_users', 'role_assignments' => 'backup_auto_role_assignments', 'activities' => 'backup_auto_activities', 'blocks' => 'backup_auto_blocks', 'filters' => 'backup_auto_filters', 'comments' => 'backup_auto_comments', 'completion_information' => 'backup_auto_userscompletion', 'logs' => 'backup_auto_logs', 'histories' => 'backup_auto_histories');
         foreach ($settings as $setting => $configsetting) {
             if ($bc->get_plan()->setting_exists($setting)) {
                 $bc->get_plan()->get_setting($setting)->set_value($config->{$configsetting});
             }
         }
         // Set the default filename
         $format = $bc->get_format();
         $type = $bc->get_type();
         $id = $bc->get_id();
         $users = $bc->get_plan()->get_setting('users')->get_value();
         $anonymised = $bc->get_plan()->get_setting('anonymize')->get_value();
         $bc->get_plan()->get_setting('filename')->set_value(backup_plan_dbops::get_default_backup_filename($format, $type, $id, $users, $anonymised));
         $bc->set_status(backup::STATUS_AWAITING);
         $outcome = $bc->execute_plan();
         $results = $bc->get_results();
         $file = $results['backup_destination'];
         $dir = $config->backup_auto_destination;
         $storage = (int) $config->backup_auto_storage;
         if (!file_exists($dir) || !is_dir($dir) || !is_writable($dir)) {
             $dir = null;
         }
         if (!empty($dir) && $storage !== 0) {
             $filename = backup_plan_dbops::get_default_backup_filename($format, $type, $course->id, $users, $anonymised, true);
             $outcome = $file->copy_content_to($dir . '/' . $filename);
             if ($outcome && $storage === 1) {
                 $file->delete();
             }
         }
         $outcome = true;
     } catch (backup_exception $e) {
         $bc->log('backup_auto_failed_on_course', backup::LOG_WARNING, $course->shortname);
         $outcome = false;
     }
     $bc->destroy();
     unset($bc);
     return true;
 }
 /**
  * Launches a automated backup routine for the given course
  *
  * @param stdClass $course
  * @param int $starttime
  * @param int $userid
  * @return bool
  */
 public static function launch_automated_backup($course, $starttime, $userid)
 {
     $outcome = self::BACKUP_STATUS_OK;
     $config = get_config('backup');
     $dir = $config->backup_auto_destination;
     $storage = (int) $config->backup_auto_storage;
     $bc = new backup_controller(backup::TYPE_1COURSE, $course->id, backup::FORMAT_MOODLE, backup::INTERACTIVE_NO, backup::MODE_AUTOMATED, $userid);
     try {
         $settings = array('users' => 'backup_auto_users', 'role_assignments' => 'backup_auto_role_assignments', 'activities' => 'backup_auto_activities', 'blocks' => 'backup_auto_blocks', 'filters' => 'backup_auto_filters', 'comments' => 'backup_auto_comments', 'badges' => 'backup_auto_badges', 'completion_information' => 'backup_auto_userscompletion', 'logs' => 'backup_auto_logs', 'histories' => 'backup_auto_histories', 'questionbank' => 'backup_auto_questionbank');
         foreach ($settings as $setting => $configsetting) {
             if ($bc->get_plan()->setting_exists($setting)) {
                 if (isset($config->{$configsetting})) {
                     $bc->get_plan()->get_setting($setting)->set_value($config->{$configsetting});
                 }
             }
         }
         // Set the default filename.
         $format = $bc->get_format();
         $type = $bc->get_type();
         $id = $bc->get_id();
         $users = $bc->get_plan()->get_setting('users')->get_value();
         $anonymised = $bc->get_plan()->get_setting('anonymize')->get_value();
         $bc->get_plan()->get_setting('filename')->set_value(backup_plan_dbops::get_default_backup_filename($format, $type, $id, $users, $anonymised));
         $bc->set_status(backup::STATUS_AWAITING);
         $bc->execute_plan();
         $results = $bc->get_results();
         $outcome = self::outcome_from_results($results);
         $file = $results['backup_destination'];
         // May be empty if file already moved to target location.
         if (!file_exists($dir) || !is_dir($dir) || !is_writable($dir)) {
             $dir = null;
         }
         // Copy file only if there was no error.
         if ($file && !empty($dir) && $storage !== 0 && $outcome != self::BACKUP_STATUS_ERROR) {
             $filename = backup_plan_dbops::get_default_backup_filename($format, $type, $course->id, $users, $anonymised, !$config->backup_shortname);
             if (!$file->copy_content_to($dir . '/' . $filename)) {
                 $outcome = self::BACKUP_STATUS_ERROR;
             }
             if ($outcome != self::BACKUP_STATUS_ERROR && $storage === 1) {
                 $file->delete();
             }
         }
     } catch (moodle_exception $e) {
         $bc->log('backup_auto_failed_on_course', backup::LOG_ERROR, $course->shortname);
         // Log error header.
         $bc->log('Exception: ' . $e->errorcode, backup::LOG_ERROR, $e->a, 1);
         // Log original exception problem.
         $bc->log('Debug: ' . $e->debuginfo, backup::LOG_DEBUG, null, 1);
         // Log original debug information.
         $outcome = self::BACKUP_STATUS_ERROR;
     }
     // Delete the backup file immediately if something went wrong.
     if ($outcome === self::BACKUP_STATUS_ERROR) {
         // Delete the file from file area if exists.
         if (!empty($file)) {
             $file->delete();
         }
         // Delete file from external storage if exists.
         if ($storage !== 0 && !empty($filename) && file_exists($dir . '/' . $filename)) {
             @unlink($dir . '/' . $filename);
         }
     }
     $bc->destroy();
     unset($bc);
     return $outcome;
 }
 /**
  *
  */
 public function create_preset_from_backup($userdata)
 {
     global $CFG, $USER, $SESSION;
     require_once "{$CFG->dirroot}/backup/util/includes/backup_includes.php";
     $df = mod_dataform_dataform::instance($this->_dataformid);
     $users = 0;
     $anon = 0;
     switch ($userdata) {
         case 'dataanon':
             $anon = 1;
         case 'data':
             $users = 1;
     }
     // Store preset settings in $SESSION.
     $SESSION->{"dataform_{$df->cm->id}_preset"} = "{$users} {$anon}";
     $bc = new backup_controller(backup::TYPE_1ACTIVITY, $df->cm->id, backup::FORMAT_MOODLE, backup::INTERACTIVE_NO, backup::MODE_GENERAL, $USER->id);
     // Clear preset settings from $SESSION.
     unset($SESSION->{"dataform_{$df->cm->id}_preset"});
     // Set users and anon in plan.
     $bc->get_plan()->get_setting('users')->set_value($users);
     $bc->get_plan()->get_setting('anonymize')->set_value($anon);
     $bc->set_status(backup::STATUS_AWAITING);
     $bc->execute_plan();
     $bc->destroy();
     $fs = get_file_storage();
     if ($users and !$anon) {
         $contextid = $df->context->id;
         $files = $fs->get_area_files($contextid, 'backup', 'activity', 0, 'timemodified', false);
     } else {
         $usercontext = context_user::instance($USER->id);
         $contextid = $usercontext->id;
         $files = $fs->get_area_files($contextid, 'user', 'backup', 0, 'timemodified', false);
     }
     if (!empty($files)) {
         $coursecontext = context_course::instance($df->course->id);
         foreach ($files as $file) {
             if ($file->get_contextid() != $contextid) {
                 continue;
             }
             $preset = new object();
             $preset->contextid = $coursecontext->id;
             $preset->component = 'mod_dataform';
             $preset->filearea = self::PRESET_COURSEAREA;
             $preset->filepath = '/';
             $preset->filename = clean_filename(str_replace(' ', '_', $df->name) . '-dataform-preset-' . gmdate("Ymd_Hi") . '-' . str_replace(' ', '-', get_string("preset{$userdata}", 'dataform')) . '.mbz');
             $fs->create_file_from_storedfile($preset, $file);
             $file->delete();
             return true;
         }
     }
     return false;
 }