Skip to content

Commit

Permalink
Fully configurable archives path
Browse files Browse the repository at this point in the history
  • Loading branch information
Syxton committed Aug 30, 2023
1 parent d611904 commit 281302c
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 12 deletions.
15 changes: 11 additions & 4 deletions classes/processor.php
Original file line number Diff line number Diff line change
Expand Up @@ -620,6 +620,8 @@ protected function archivecourse($obj, $delete = true) {
}

$archivefile = date("Y-m-d") . "{$suffix}-{$safeshort}.mbz";

$rootpath = trim(get_config('tool_coursearchiver', 'coursearchiverrootpath'), "/\\");
$archivepath = trim(str_replace(str_split(':*?"<>|'),
'',
get_config('tool_coursearchiver', 'coursearchiverpath')),
Expand All @@ -629,7 +631,7 @@ protected function archivecourse($obj, $delete = true) {
$folder = $this->get_archive_folder();

// Final full path of file.
$path = $CFG->dataroot . '/' . $archivepath . '/' . $folder;
$path = $rootpath . '/' . $archivepath . '/' . $folder;

// If the path doesn't exist, make it so!
if (!is_dir($path)) {
Expand Down Expand Up @@ -1540,6 +1542,7 @@ public static function get_archivelist($search, $recover = false) {
$isadmin = is_siteadmin();
$config = get_config('tool_coursearchiver');

$rootpath = trim($config->coursearchiverrootpath, "/\\");
$archivepath = trim(str_replace(str_split(':*?"<>|'),
'',
$config->coursearchiverpath),
Expand Down Expand Up @@ -1577,7 +1580,7 @@ public static function get_archivelist($search, $recover = false) {
$path = $pathinfo['dirname'];

// Make sure it is a file.
if (!file_exists($CFG->dataroot . '/' . $archivepath . '/' . $path . '/' . $file)) {
if (!file_exists($rootpath . '/' . $archivepath . '/' . $path . '/' . $file)) {
continue;
}

Expand Down Expand Up @@ -1626,14 +1629,16 @@ public static function get_archivelist($search, $recover = false) {
*/
public static function delete_archives($selected) {
global $CFG, $DB;

$config = get_config('tool_coursearchiver');
$rootpath = trim($config->coursearchiverrootpath, "/\\");
$archivepath = trim(str_replace(str_split(':*?"<>|'),
'',
$config->coursearchiverpath),
"/\\");
$delaydelete = $config->delaydeletesetting;
foreach ($selected as $course) {
if (file_exists($CFG->dataroot . '/' . $archivepath . '/' . $course)) {
if (file_exists($rootpath . '/' . $archivepath . '/' . $course)) {
$time = new DateTime("now + $delaydelete days", core_date::get_user_timezone_object());
// Check for database entry of file.
$sql = 'SELECT *
Expand Down Expand Up @@ -1661,13 +1666,15 @@ public static function delete_archives($selected) {
*/
public static function recover_archives($selected) {
global $CFG, $DB;

$rootpath = trim(get_config('tool_coursearchiver', 'coursearchiverrootpath'), "/\\");
$archivepath = trim(str_replace(str_split(':*?"<>|'),
'',
get_config('tool_coursearchiver', 'coursearchiverpath')),
"/\\");

foreach ($selected as $course) {
$file = $CFG->dataroot . '/' . $archivepath . '/' . $course;
$file = $rootpath . '/' . $archivepath . '/' . $course;
if (file_exists($file)) {
// Check for database entry of file.
$sql = 'SELECT *
Expand Down
3 changes: 2 additions & 1 deletion classes/task/cron_task.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ public function get_name() {
public function execute() {
global $CFG, $DB;

$rootpath = trim(get_config('tool_coursearchiver', 'coursearchiverrootpath'), "/\\");
$archivepath = trim(str_replace(str_split(':*?"<>|'),
'',
get_config('tool_coursearchiver', 'coursearchiverpath')),
Expand All @@ -60,7 +61,7 @@ public function execute() {

if ($markedfordeletion = $DB->get_records_sql($sql, array('timetodelete' => time()))) {
foreach ($markedfordeletion as $fileinfo) {
$file = $CFG->dataroot . '/' . $archivepath . '/' . $fileinfo->filename;
$file = $rootpath . '/' . $archivepath . '/' . $fileinfo->filename;
if (file_exists($file)) {
if (unlink($file)) { // Delete file.
$DB->delete_records('tool_coursearchiver_archived', array('id' => $fileinfo->id));
Expand Down
5 changes: 3 additions & 2 deletions db/upgrade.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,14 +139,15 @@ function xmldb_tool_coursearchiver_upgrade($oldversion) {
}

// Fill up the database with previously archived files.
$rootpath = trim(get_config('tool_coursearchiver', 'coursearchiverrootpath'), "/\\");
$archivepath = trim(str_replace(str_split(':*?"<>|'),
'',
get_config('tool_coursearchiver', 'coursearchiverpath')),
"/\\");

if (file_exists($CFG->dataroot . '/' . $archivepath)) {
if (file_exists($rootpath . '/' . $archivepath)) {
$fileinfos = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator($CFG->dataroot . '/' . $archivepath)
new RecursiveDirectoryIterator($rootpath . '/' . $archivepath)
);

if (!empty($fileinfos)) {
Expand Down
7 changes: 5 additions & 2 deletions lang/en/tool_coursearchiver.php
Original file line number Diff line number Diff line change
Expand Up @@ -210,10 +210,13 @@
Thank you.
';
$string['archivewarningsubject'] = 'Notice: Courses will be archived soon.';
$string['hidewarningsubject'] = 'Notice: Courses will be hidden soon.';

$string['coursearchiverpath'] = 'Folder path to store archived courses';
$string['coursearchiverpath_help'] = 'This path is relative to the Moodle $CFG->dataroot';
$string['hidewarningsubject'] = 'Notice: Courses will be hidden soon.';
$string['coursearchiverpath_help'] = 'This path is relative to the root path';

$string['coursearchiverrootpath'] = 'System path that archiver folders are placed in';
$string['coursearchiverrootpath_help'] = 'Defaults to CFG->dataroot';

$string['optoutmonthssetting'] = 'Course opt out persistence';
$string['optoutmonthssetting_help'] = 'How many months will the opt out apply for each course.';
Expand Down
3 changes: 2 additions & 1 deletion restorefile.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,13 @@

global $CFG, $DB;

$rootpath = trim(get_config('tool_coursearchiver', 'coursearchiverrootpath'), "/\\");
$archivepath = trim(str_replace(str_split(':*?"<>|'),
'',
get_config('tool_coursearchiver', 'coursearchiverpath')),
"/\\");

$archive = $CFG->dataroot . '/' . $archivepath . '/' . $filepath . '/' . $filename;
$archive = $rootpath . '/' . $archivepath . '/' . $filepath . '/' . $filename;
if (file_exists($archive)) {
$context = context_course::instance(SITEID);
$fs = get_file_storage();
Expand Down
8 changes: 8 additions & 0 deletions settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,14 @@
if ($hassiteconfig) {
$settings = new admin_settingpage('tool_coursearchiver', get_string('coursearchiver_settings', 'tool_coursearchiver'));

$name = new lang_string('coursearchiverrootpath', 'tool_coursearchiver');
$description = new lang_string('coursearchiverrootpath_help', 'tool_coursearchiver');
$default = $CFG->dataroot;
$settings->add(new admin_setting_configtext('tool_coursearchiver/coursearchiverrootpath',
$name,
$description,
$default));

$name = new lang_string('coursearchiverpath', 'tool_coursearchiver');
$description = new lang_string('coursearchiverpath_help', 'tool_coursearchiver');
$default = 'CourseArchives';
Expand Down
4 changes: 2 additions & 2 deletions version.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@
defined('MOODLE_INTERNAL') || die();

$plugin = new stdClass();
$plugin->version = 2023071200; // The current plugin version (Date: YYYYMMDDXX).
$plugin->version = 2023083000; // The current plugin version (Date: YYYYMMDDXX).
$plugin->requires = 2014111000; // Requires this Moodle version.
$plugin->component = 'tool_coursearchiver'; // Full name of the plugin (used for diagnostics).
$plugin->release = '4.1.4 (Build: 2016090200)';
$plugin->release = '4.1.5 (Build: 2016090200)';
$plugin->maturity = MATURITY_STABLE;

0 comments on commit 281302c

Please sign in to comment.