Skip to content
Snippets Groups Projects
Unverified Commit ffff7a24 authored by Tobias Reischmann's avatar Tobias Reischmann
Browse files

First working version of backup step

parent 86ba8cca
Branches
No related tags found
No related merge requests found
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Manager to create & restore backups for courses
*
* @package tool_cleanupcourses
* @copyright 2017 Tobias Reischmann WWU
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace tool_cleanupcourses\manager;
use WebDriver\Exception;
defined('MOODLE_INTERNAL') || die();
class backup_manager {
/**
* Creates a course backup in a specific cleanup courses backup folder
* @param int $courseid id of the course the backup should be created for.
* @return bool tells if the backup was completed successfully.
*/
public static function create_course_backup($courseid) {
global $CFG;
try {
// Build filename.
$archivefile = date("Y-m-d") . "-ID-{$courseid}.mbz";
// Path of backup folder.
$path = $CFG->dataroot . '/cleanupcourses_backups';
// If the path doesn't exist, make it so!
if (!is_dir($path)) {
umask(0000);
// Create the directory for Backups.
if (!mkdir($path, $CFG->directorypermissions, true)) {
throw new \moodle_exception(get_string('errorbackuppath', 'tool_cleanupcourses'));
}
}
// Perform Backup.
$bc = new \backup_controller(\backup::TYPE_1COURSE, $courseid, \backup::FORMAT_MOODLE,
\backup::INTERACTIVE_NO, \backup::MODE_AUTOMATED, get_admin()->id);
$bc->execute_plan(); // Execute backup.
$results = $bc->get_results(); // Get the file information needed.
$file = $results['backup_destination'];
if (!empty($file)) {
$file->copy_content_to($path . '/' . $archivefile);
}
$bc->destroy();
unset($bc);
return true;
} catch (\moodle_exception $e) {
debugging('There was a problem during backup!');
debugging($e->getMessage());
return false;
}
}
}
...@@ -26,6 +26,7 @@ ...@@ -26,6 +26,7 @@
namespace tool_cleanupcourses\step; namespace tool_cleanupcourses\step;
use tool_cleanupcourses\response\step_response; use tool_cleanupcourses\response\step_response;
use tool_cleanupcourses\manager\backup_manager;
defined('MOODLE_INTERNAL') || die(); defined('MOODLE_INTERNAL') || die();
...@@ -51,12 +52,11 @@ class createbackup extends libbase { ...@@ -51,12 +52,11 @@ class createbackup extends libbase {
* @return step_response * @return step_response
*/ */
public function process_course($processid, $instanceid, $course) { public function process_course($processid, $instanceid, $course) {
$bc = new \backup_controller(\backup::TYPE_1COURSE, $course->id, \backup::FORMAT_MOODLE, if (backup_manager::create_course_backup($course->id)) {
\backup::INTERACTIVE_NO, \backup::MODE_AUTOMATED, get_admin()->id);
$bc->execute_plan();
$results = $bc->get_results();
return step_response::proceed(); return step_response::proceed();
} }
return step_response::waiting();
}
public function get_subpluginname() { public function get_subpluginname() {
return 'createbackup'; return 'createbackup';
......
...@@ -23,5 +23,5 @@ ...@@ -23,5 +23,5 @@
defined('MOODLE_INTERNAL') || die; defined('MOODLE_INTERNAL') || die;
$plugin->version = 2017061901; $plugin->version = 2017061902;
$plugin->component = 'tool_cleanupcourses'; $plugin->component = 'tool_cleanupcourses';
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment