From f5361cceac26ef8a8b97fc1cd444318426cf63c4 Mon Sep 17 00:00:00 2001 From: amayard Date: Mon, 16 Aug 2021 16:50:35 +0200 Subject: [PATCH 01/10] #367565 automatic export --- classes/task/automatic_export.php | 153 ++++++++++++++++++++++++++++++ db/tasks.php | 36 +++++++ lang/en/block_admin_presets.php | 5 +- lib/admin_presets_base.class.php | 23 +++-- version.php | 2 +- 5 files changed, 211 insertions(+), 8 deletions(-) create mode 100644 classes/task/automatic_export.php create mode 100644 db/tasks.php diff --git a/classes/task/automatic_export.php b/classes/task/automatic_export.php new file mode 100644 index 0000000..2a62b5b --- /dev/null +++ b/classes/task/automatic_export.php @@ -0,0 +1,153 @@ +. + +/** + * @author amayard@cblue.be + * @date 16/08/2021 + * @copyright 2021, CBlue SPRL, support@cblue.be + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + * @package block_admin_presets + */ + +namespace block_admin_presets\task; + +use admin_presets_export; +use coding_exception; +use core\task\scheduled_task; +use dml_exception; +use stdClass; + +defined('MOODLE_INTERNAL') || die(); + +class automatic_export extends scheduled_task { + + /** + * @return string + * @throws coding_exception + */ + public function get_name() { + return get_string('automaticexport', 'block_admin_presets'); + } + + /** + * This function is meant to be called by a CRON task, see the ../db/tasks.php file to check how it is programmed. + * This function will get all site's settings through the _get_setting() function, it'll then will loop through them and construct a $_POST array that'll be handled to create an admin_presets valid database record. The record will be named with a timestamp YearMonthDayHourMinuteSecond. + * If the config is successfully recorded, the function will return true, if not it'll throw the appropriate Exception along the way. + * + * For testing purposes you can manually launch this function : sudo -u www-data php7.3 admin/cli/scheduled_task.php --execute="\block_admin_presets\task\automatic_export" + * + * @return bool + * @throws \moodle_exception + * @throws coding_exception + * @throws dml_exception + */ + public function execute() { + global $CFG, $DB; + + require_once $CFG->dirroot . "/blocks/admin_presets/lib/admin_presets_export.class.php"; + + $export = new admin_presets_export(); + + // Reload site settings. + $sitesettings = $export->_get_site_settings(); + + // Set up $preset that should be in execute() $_POST header + $_POST['sesskey'] = 'yms935m3dl'; + $_POST['_qf__admin_presets_export_form'] = '1'; + $_POST['mform_isexpanded_id_general'] = '1'; + $_POST['name'] = 'YYYYMMDDmmss'; + $_POST['comments'] = [ + 'text' => 'This is an automatic export for backup.', + 'format' => '1' + ]; + $_POST['author'] = 'Automatic Export'; + $_POST['admin_presets_submit'] = 'Save Changes'; + + //Construct $_POST for each site setting + foreach ($sitesettings as $sitesetting => $settingvalue) { + foreach ($settingvalue as $key => $value) { + $_POST[$key . '@@' . $sitesetting] = '1'; + } + } + + + // admin_preset record. + $preset = new stdClass(); + $preset->userid = '1'; + $preset->name = date('YmdGis'); + $preset->comments = $_POST['comments']['text']; + $preset->site = $CFG->wwwroot; + $preset->author = $_POST['author']; + $preset->moodleversion = $CFG->version; + $preset->moodlerelease = $CFG->release; + $preset->timecreated = time(); + $preset->timemodified = 0; + if (!$preset->id = $DB->insert_record('block_admin_presets', $preset)) { + print_error('errorinserting', 'block_admin_presets'); + } + + // Store it here for logging and other future id-oriented stuff. + $this->id = $preset->id; + + // We must ensure that there are settings selected. + foreach ($_POST as $varname => $value) { + + unset($setting); + + if (strstr($varname, '@@') != false) { + + $settingsfound = true; + + $name = explode('@@', $varname); + $setting = new StdClass(); + $setting->adminpresetid = $preset->id; + $setting->plugin = $name[1]; + $setting->name = $name[0]; + $setting->value = $sitesettings[$setting->plugin][$setting->name]->get_value(); + + if (!$setting->id = $DB->insert_record('block_admin_presets_it', $setting)) { + print_error('errorinserting', 'block_admin_presets'); + } + + // Setting attributes must also be exported. + if ($attributes = $sitesettings[$setting->plugin][$setting->name]->get_attributes_values()) { + foreach ($attributes as $attributename => $value) { + + $attr = new StdClass(); + $attr->itemid = $setting->id; + $attr->name = $attributename; + $attr->value = $value; + + $DB->insert_record('block_admin_presets_it_a', $attr); + } + } + } + } + + // If there are no valid or selected settings we should delete the admin preset record. + if (empty($settingsfound)) { + $DB->delete_records('block_admin_presets', array('id' => $preset->id)); + redirect($CFG->wwwroot . '/blocks/admin_presets/index.php?action=export', + get_string('novalidsettingsselected', 'block_admin_presets'), 4); + } + + + // Trigger the as it is usually triggered after execute finishes. + $export->log(); + + return true; + } +} \ No newline at end of file diff --git a/db/tasks.php b/db/tasks.php new file mode 100644 index 0000000..f6b35da --- /dev/null +++ b/db/tasks.php @@ -0,0 +1,36 @@ +. + +/** + * @author amayard@cblue.be + * @date 16/08/2021 + * @copyright 2021, CBlue SPRL, support@cblue.be + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + * @package block_admin_presets + */ + +defined('MOODLE_INTERNAL') || die(); + +$tasks = [ + [ + 'classname' => 'block_admin_presets\task\automatic_export', + 'blocking' => false, + 'minute' => 0, + 'hour' => 23, + 'day' => '*', + 'month' => '*', + ], +]; \ No newline at end of file diff --git a/lang/en/block_admin_presets.php b/lang/en/block_admin_presets.php index 033cec5..0034cf6 100755 --- a/lang/en/block_admin_presets.php +++ b/lang/en/block_admin_presets.php @@ -117,4 +117,7 @@ $string['voidvalue'] = 'that setting does not have a value'; $string['wrongfile'] = 'Wrong file'; $string['wrongid'] = 'Wrong id'; -$string['privacy:null_reason'] = 'The admin presets block does not effect or store any user data'; \ No newline at end of file +$string['privacy:null_reason'] = 'The admin presets block does not effect or store any user data'; +//CBLUE START #367565 +$string['automaticexport'] = 'This is an automatic export of this moodle config'; +//CBLUE END #367565 diff --git a/lib/admin_presets_base.class.php b/lib/admin_presets_base.class.php index 697a899..381b64f 100755 --- a/lib/admin_presets_base.class.php +++ b/lib/admin_presets_base.class.php @@ -24,6 +24,17 @@ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ +//CBLUE EDIT : +//list of function that went from protected > public +//_get_site_settings +//_get_settings +//_get_setting +//_get_settings_branches +//_get_settings_elements +//_get_settings_from_db +//This was necessary in order to make the classes/task/automatic_export class to work as it calls _get_setting() which will use all the other functions. That class is used by a CRON job to automatically make an export every 24hours. +//END CBLUE EDIT + defined('MOODLE_INTERNAL') || die(); require_once($CFG->libdir . '/adminlib.php'); @@ -272,7 +283,7 @@ public function log() { * @return array $settings Array format $array['plugin']['settingname'] = admin_preset_setting child class * @throws dml_exception */ - protected function _get_site_settings() { + public function _get_site_settings() { global $DB; @@ -319,7 +330,7 @@ protected function _get_site_settings() { * @return array Array format $array['plugin']['settingname'] = admin_preset_setting child class * @throws dml_exception */ - protected function _get_settings($dbsettings, $sitedbvalues = false, $settings, $children = false) { + public function _get_settings($dbsettings, $sitedbvalues = false, $settings, $children = false) { global $DB; // If there are no children, load admin tree and iterate through. @@ -441,7 +452,7 @@ protected function _get_settings($dbsettings, $sitedbvalues = false, $settings, * @param mixed $currentvalue * @return bool */ - protected function _get_setting($settingdata, $currentvalue) { + public function _get_setting($settingdata, $currentvalue) { // Getting the appropiate class to get the correct setting value. $settingtype = get_class($settingdata); @@ -464,7 +475,7 @@ protected function _get_setting($settingdata, $currentvalue) { * * @param array $settings Array format $array['plugin']['settingname'] = admin_preset_setting child class */ - protected function _get_settings_branches($settings) { + public function _get_settings_branches($settings) { global $PAGE; @@ -498,7 +509,7 @@ protected function _get_settings_branches($settings) { * @param array $nodes Tree nodes * @return array Code to output */ - protected function _get_settings_elements($allsettings, $admintree = false, $jsparentnode = false, &$nodes) { + public function _get_settings_elements($allsettings, $admintree = false, $jsparentnode = false, &$nodes) { if (empty($this->adminroot)) { $this->adminroot = admin_get_root(false, true); @@ -580,7 +591,7 @@ protected function _get_settings_elements($allsettings, $admintree = false, $jsp * @return array Standarized array, * format $array['plugin']['name'] = obj('name'=>'settingname', 'value'=>'settingvalue') */ - protected function _get_settings_from_db($dbsettings) { + public function _get_settings_from_db($dbsettings) { if (!$dbsettings) { return false; diff --git a/version.php b/version.php index 54cc6df..5d88dde 100755 --- a/version.php +++ b/version.php @@ -26,7 +26,7 @@ defined('MOODLE_INTERNAL') || die(); -$plugin->version = 2021052700; +$plugin->version = 2021052702; $plugin->requires = 2016052300; // Requires this Moodle version $plugin->component = 'block_admin_presets'; $plugin->release = '3.4'; From 7628ef4640eded9b0483b99e2867957767187f08 Mon Sep 17 00:00:00 2001 From: amayard Date: Tue, 17 Aug 2021 08:47:14 +0200 Subject: [PATCH 02/10] #367565 fixed config problem --- classes/task/automatic_export.php | 5 +++++ lang/en/block_admin_presets.php | 1 + settings.php | 10 +++++++++- 3 files changed, 15 insertions(+), 1 deletion(-) diff --git a/classes/task/automatic_export.php b/classes/task/automatic_export.php index 2a62b5b..9622e90 100644 --- a/classes/task/automatic_export.php +++ b/classes/task/automatic_export.php @@ -57,6 +57,11 @@ public function get_name() { public function execute() { global $CFG, $DB; + $config = get_config('block_admin_presets', 'automaticexport'); + if (empty($config)) { + return; + } + require_once $CFG->dirroot . "/blocks/admin_presets/lib/admin_presets_export.class.php"; $export = new admin_presets_export(); diff --git a/lang/en/block_admin_presets.php b/lang/en/block_admin_presets.php index 0034cf6..e3df31b 100755 --- a/lang/en/block_admin_presets.php +++ b/lang/en/block_admin_presets.php @@ -120,4 +120,5 @@ $string['privacy:null_reason'] = 'The admin presets block does not effect or store any user data'; //CBLUE START #367565 $string['automaticexport'] = 'This is an automatic export of this moodle config'; +$string['automaticexportconfig'] = 'Enable daily automatic backup exports'; //CBLUE END #367565 diff --git a/settings.php b/settings.php index 4a57c38..631962f 100755 --- a/settings.php +++ b/settings.php @@ -33,8 +33,16 @@ $sensiblesettingsdefault .= 'smtppass@none, proxypassword@@none, password@@quiz, '; $sensiblesettingsdefault .= 'enrolpassword@@moodlecourse, allowedip@@none, blockedip@@none'; - $settings->add(new admin_setting_configtextarea('admin_presets/sensiblesettings', + $automaticexportdefault = false ; + + $settings->add(new admin_setting_configtextarea('block_admin_presets/sensiblesettings', get_string('sensiblesettings', 'block_admin_presets'), get_string('sensiblesettingstext', 'block_admin_presets'), $sensiblesettingsdefault, PARAM_TEXT)); + + $settings->add(new admin_setting_configcheckbox( + 'block_admin_presets/automaticexport', + get_string('automaticexportconfig', 'block_admin_presets'), + get_string('automaticexportconfig', 'block_admin_presets'), + $automaticexportdefault, PARAM_BOOL)); } From 6c5e68f8da9620efe8612591148467ae82fde593 Mon Sep 17 00:00:00 2001 From: amayard Date: Tue, 17 Aug 2021 09:20:31 +0200 Subject: [PATCH 03/10] #367565 updated readme --- README.md | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/README.md b/README.md index 703b046..467df6b 100755 --- a/README.md +++ b/README.md @@ -29,3 +29,25 @@ Any Problems, questions, suggestions If you have a problem with this block, suggestions for improvement, drop an email at : - Pimenko : contact@pimenko.com - Github : https://github.com/DigiDago/moodle-block_admin_presets + +Usage +=================== +- **Check if plugin is installed :** +go to *Admin > Plugins > Plugins overview > Additional Plugins > Admin presets* +- **Set-up the block :** Make the block appear in your 'Site Home' page by adding it to the page, go to *Site Home > Turn Editing On*. Then go to *Add Block > Admin presets* +- **Usage :** In the 'Site Home' page, an admin_preset block should now appear. from there you can + + - **Import a config file :** Just drag&drop a config_file generated by admin_presets from this moodle or another one. Just in case, before importing anything you should always export your actual configuration to be able to retrieve it later. + - **Export your current config :** Select all the settings you wish to be in your export file, **by default all settings are selected**. Be careful not to select unnecessary settings because the selected settings will always override previous settings at the import. The config.xml file won't be downloaded right away, you will find it in the presets list. + - **See the presets list :** Here are listed all the imported & exported config files, from there you can + - Preview : see the config to check which settings are contained + - Load : load the config onto the site + - Download : download a .xml fil containing the config, allowing you to import it to any moodle project running the admin_presets plugin + - Delete : permanently remove that configuration from de presets list +> Carefull : settings related to the Moodle Theme are exported/imported but the used theme still needs to be set manually. + + +Cblue Automatic Export +=================== +Cblue added a feature allowing the plugin to perform an automatic export which will provide a daily back-up if enabled. You can enable this setting the same way you're configuring the plugin following : Admin > Plugins > Plugins overview > Additional Plugins > Admin presets > Settings. +These backups are named with a timestamp looking like '20210816160918' and will be found with any other record of admin_presets : in the "presets" list that regroups exports, imports and automatic backups. From b7038ccd84695a8ad16ab703af4b7746c241d90d Mon Sep 17 00:00:00 2001 From: amayard Date: Wed, 18 Aug 2021 10:20:25 +0200 Subject: [PATCH 04/10] #367568 automatic export cleanup --- README.md | 18 +- classes/task/automatic_export_cleanup.php | 203 ++++++++++++++++++++++ db/tasks.php | 8 + lang/en/block_admin_presets.php | 2 + settings.php | 15 +- version.php | 2 +- 6 files changed, 241 insertions(+), 7 deletions(-) create mode 100644 classes/task/automatic_export_cleanup.php diff --git a/README.md b/README.md index 467df6b..6009245 100755 --- a/README.md +++ b/README.md @@ -33,7 +33,7 @@ If you have a problem with this block, suggestions for improvement, drop an emai Usage =================== - **Check if plugin is installed :** -go to *Admin > Plugins > Plugins overview > Additional Plugins > Admin presets* +go to *Admin > Plugins > Plugins overview > Additional Plugins > Admin presets*. It's installed if it appears. - **Set-up the block :** Make the block appear in your 'Site Home' page by adding it to the page, go to *Site Home > Turn Editing On*. Then go to *Add Block > Admin presets* - **Usage :** In the 'Site Home' page, an admin_preset block should now appear. from there you can @@ -47,7 +47,19 @@ go to *Admin > Plugins > Plugins overview > Additional Plugins > Admin presets* > Carefull : settings related to the Moodle Theme are exported/imported but the used theme still needs to be set manually. -Cblue Automatic Export +Automatic Export =================== -Cblue added a feature allowing the plugin to perform an automatic export which will provide a daily back-up if enabled. You can enable this setting the same way you're configuring the plugin following : Admin > Plugins > Plugins overview > Additional Plugins > Admin presets > Settings. +A feature was added allowing the plugin to perform an automatic export which will provide a daily back-up if enabled. You can enable this setting the same way you're configuring the plugin following : *Admin > Plugins > Plugins overview > Additional Plugins > Admin presets > Settings*. These backups are named with a timestamp looking like '20210816160918' and will be found with any other record of admin_presets : in the "presets" list that regroups exports, imports and automatic backups. + +###How it works +- Automatic export daily at 23:00, creates an export with full config named wit ha time stamp **default : Enabled** +- Automatic export cleanup runs daily at 23:30, will delete automatic exports as follows + - Last 7 weeks : keeps all + - Last 14 days : keeps one export out of 2 + - Last month : keeps one export out of 4 + - Last 3 months : keeps one export out of 8 + - Older than 3 months : deletes all +> This feature only targets automatic exports, if you perform a manual export or import they'll stay untouched. +> +> If you import an automatic export in this project or another, it won't be targeted by this cleanup feature diff --git a/classes/task/automatic_export_cleanup.php b/classes/task/automatic_export_cleanup.php new file mode 100644 index 0000000..2b679d1 --- /dev/null +++ b/classes/task/automatic_export_cleanup.php @@ -0,0 +1,203 @@ +. + +/** + * @author amayard@cblue.be + * @date 16/08/2021 + * @copyright 2021, CBlue SPRL, support@cblue.be + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + * @package block_admin_presets + */ + +namespace block_admin_presets\task; + +use coding_exception; +use core\task\scheduled_task; +use DateTime; + +defined('MOODLE_INTERNAL') || die(); + +class automatic_export_cleanup extends scheduled_task { + + /** + * @return string + * @throws coding_exception + */ + public function get_name() { + return get_string('automaticexportcleanup', 'block_admin_presets'); + } + + /** + * This function is meant to be called by a CRON task, see the ../db/tasks.php file to check how it is programmed. + * This function will get all records of the admin_presets plugin than will sort them into 4 arrays depending on the time creation of the record. + * Each array will then be processed according to what cleanup we want to do : + * records that are younger than 1 week will stay untouched + * records that are older than a week but younger than 2 weeks will be deleted 1 day out of 2 + * records that are older than two weeks but younger than 1 month will be deleted 3 day out of 4 + * records that are older than 1 month but younger than 3 months will be deleted 3 day out of 4 + * records that are older than 3 months will all be deleted + * + * This function will return true if deletion was successfull. Otherwise it'll trhow the apropriate Exception along the way. + * + * For testing purposes you can manually launch this function : sudo -u www-data php7.3 admin/cli/scheduled_task.php --execute="\block_admin_presets\task\automatic_export_cleanup" + * * + * @return bool|void + * @throws \dml_exception + */ + public function execute() { + global $CFG, $DB; + + $config = get_config('block_admin_presets', 'automaticexportcleanup'); + if (empty($config)) { + return; + } + + //get all admin_presets records from DB + $presets = $DB->get_records('block_admin_presets'); + + //set-up arrays for dispatching presets records + $twoweeks = []; + $onemonth = []; + $threemonths = []; + $older = []; + + foreach ($presets as $preset) { + + // checks name to see if automatic backup + if (preg_match('/^[0-9]{14}$/', $preset->name)) { + // check if record is imported or local and sort records according to their age + if ($preset->timeimported == 0) { //if backup is local, check on creation datetime + if ($preset->timecreated > strtotime('-1 weeks')) { + continue; + } elseif ($preset->timecreated > strtotime('-2 weeks')) { + $twoweeks[] = $preset; + continue; + } elseif ($preset->timecreated > strtotime('-1 months')) { + $onemonth[] = $preset; + continue; + } elseif ($preset->timecreated > strtotime('-3 months')) { + $threemonths[] = $preset; + continue; + } elseif ($preset->timecreated < strtotime('-3 months')) { + $older[] = $preset; + continue; + } + } + } + } + + //loop through each now sorted array to delete unwanted records + $this->delete_all_except_nth(2, $twoweeks); + $this->delete_all_except_nth(4, $onemonth); + $this->delete_all_except_nth(8, $threemonths); + $this->delete_all($older); + + return true; + } + + /** + * This function is used to delete records from the admin_presets plugin + * It is called by the delete_all_but_nth() and delete_all() functions. + * @param $record + * @throws \dml_exception + * @throws \moodle_exception + */ + private function delete($record) { + global $DB; + + if (!$DB->delete_records('block_admin_presets', array('id' => $record->id))) { + print_error('errordeleting', 'block_admin_presets'); + } + + // Getting items ids before deleting to delete item attributes. + $items = $DB->get_records('block_admin_presets_it', array('adminpresetid' => $record->id), 'id'); + foreach ($items as $item) { + $DB->delete_records('block_admin_presets_it_a', array('itemid' => $item->id)); + } + + if (!$DB->delete_records('block_admin_presets_it', array('adminpresetid' => $record->id))) { + print_error('errordeleting', 'block_admin_presets'); + } + + // Deleting the preset applications. + if ($previouslyapplied = $DB->get_records('block_admin_presets_app', + array('adminpresetid' => $record->id), 'id')) { + + foreach ($previouslyapplied as $application) { + + // Deleting items. + if (!$DB->delete_records('block_admin_presets_app_it', + array('adminpresetapplyid' => $application->id))) { + + print_error('errordeleting', 'block_admin_presets'); + } + + // Deleting attributes. + if (!$DB->delete_records('block_admin_presets_app_it_a', + array('adminpresetapplyid' => $application->id))) { + + print_error('errordeleting', 'block_admin_presets'); + } + } + + if (!$DB->delete_records('block_admin_presets_app', + array('adminpresetid' => $record->id))) { + + print_error('errordeleting', 'block_admin_presets'); + } + } + } + + /** + * This function will delete every record except 1 out of nth. + * so if nth = 8, it'll delete 7/8 record + * + * The object parameter should be a collection of record of admin_prestes. You can get such a collenction using : + * $object = $DB->get_records('block_admin_presets'); + * + * @param int $nth + * @param $object + * @throws \dml_exception + * @throws \moodle_exception + */ + private function delete_all_except_nth(int $nth, $object) { + $reference_date = new DateTime("1970-01-01"); + + foreach ($object as $record) { + $recorddate = new DateTime(); + $recorddate->setTimestamp($record->timecreated); + if((date_diff($reference_date, $recorddate, true)->days)%$nth != 0) { + $this->delete($record); + } + } + } + + /** + * This function loops through a collection of admin_presets records to delete each of them. + * You can get such a collenction using : + * $object = $DB->get_records('block_admin_presets'); + * + * @param $object + * @throws \dml_exception + * @throws \moodle_exception + */ + private function delete_all($object) { + foreach ($object as $record) { + $this->delete($record); + } + } + +} \ No newline at end of file diff --git a/db/tasks.php b/db/tasks.php index f6b35da..de625d6 100644 --- a/db/tasks.php +++ b/db/tasks.php @@ -33,4 +33,12 @@ 'day' => '*', 'month' => '*', ], + [ + 'classname' => 'block_admin_presets\task\automatic_export_cleanup', + 'blocking' => false, + 'minute' => 0, + 'hour' => 22, + 'day' => '*', + 'month' => '*', + ] ]; \ No newline at end of file diff --git a/lang/en/block_admin_presets.php b/lang/en/block_admin_presets.php index e3df31b..17301ec 100755 --- a/lang/en/block_admin_presets.php +++ b/lang/en/block_admin_presets.php @@ -121,4 +121,6 @@ //CBLUE START #367565 $string['automaticexport'] = 'This is an automatic export of this moodle config'; $string['automaticexportconfig'] = 'Enable daily automatic backup exports'; +$string['automaticexportcleanupconfig'] = 'Enable automatic export cleanup'; +$string['automaticexportcleanup'] = 'Enable automatic export cleanup'; //CBLUE END #367565 diff --git a/settings.php b/settings.php index 631962f..3941610 100755 --- a/settings.php +++ b/settings.php @@ -33,16 +33,25 @@ $sensiblesettingsdefault .= 'smtppass@none, proxypassword@@none, password@@quiz, '; $sensiblesettingsdefault .= 'enrolpassword@@moodlecourse, allowedip@@none, blockedip@@none'; - $automaticexportdefault = false ; - $settings->add(new admin_setting_configtextarea('block_admin_presets/sensiblesettings', get_string('sensiblesettings', 'block_admin_presets'), get_string('sensiblesettingstext', 'block_admin_presets'), $sensiblesettingsdefault, PARAM_TEXT)); + //START CBLUE MODIFICATION + $automaticexportdefault = false ; + $automaticexportcleanupdefault = true ; + $settings->add(new admin_setting_configcheckbox( 'block_admin_presets/automaticexport', get_string('automaticexportconfig', 'block_admin_presets'), get_string('automaticexportconfig', 'block_admin_presets'), - $automaticexportdefault, PARAM_BOOL)); + $automaticexportdefault, true, false)); + + $settings->add(new admin_setting_configcheckbox( + 'block_admin_presets/automaticexportcleanup', + get_string('automaticexportcleanupconfig', 'block_admin_presets'), + get_string('automaticexportcleanupconfig', 'block_admin_presets'), + $automaticexportcleanupdefault, true, false)); + //END CBLUE MODIFICATION } diff --git a/version.php b/version.php index 5d88dde..98939c3 100755 --- a/version.php +++ b/version.php @@ -26,7 +26,7 @@ defined('MOODLE_INTERNAL') || die(); -$plugin->version = 2021052702; +$plugin->version = 2021052706; $plugin->requires = 2016052300; // Requires this Moodle version $plugin->component = 'block_admin_presets'; $plugin->release = '3.4'; From cd262007e43743065584aeebd9f788c6e48f2dc5 Mon Sep 17 00:00:00 2001 From: amayard Date: Wed, 18 Aug 2021 10:29:35 +0200 Subject: [PATCH 05/10] #367568 updated interface doc --- lang/en/block_admin_presets.php | 1 + settings.php | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/lang/en/block_admin_presets.php b/lang/en/block_admin_presets.php index 17301ec..8c3b9bd 100755 --- a/lang/en/block_admin_presets.php +++ b/lang/en/block_admin_presets.php @@ -122,5 +122,6 @@ $string['automaticexport'] = 'This is an automatic export of this moodle config'; $string['automaticexportconfig'] = 'Enable daily automatic backup exports'; $string['automaticexportcleanupconfig'] = 'Enable automatic export cleanup'; +$string['automaticexportcleanupconfigtext'] = 'If enabled, automatic exports younger than a week will be kept, then up to 14 days 1/2 will be saved, up to a month 1/4 will be saved, then up to 3 months 1/8 will be saved. After 3 months all automatic exports will be deleted. Manual exports and imports won\'t be affected by this functionnality.'; $string['automaticexportcleanup'] = 'Enable automatic export cleanup'; //CBLUE END #367565 diff --git a/settings.php b/settings.php index 3941610..7660ff2 100755 --- a/settings.php +++ b/settings.php @@ -51,7 +51,7 @@ $settings->add(new admin_setting_configcheckbox( 'block_admin_presets/automaticexportcleanup', get_string('automaticexportcleanupconfig', 'block_admin_presets'), - get_string('automaticexportcleanupconfig', 'block_admin_presets'), + get_string('automaticexportcleanupconfigtext', 'block_admin_presets'), $automaticexportcleanupdefault, true, false)); //END CBLUE MODIFICATION } From a7e8ef813b6a49137b9474755d8f9d79bf30c648 Mon Sep 17 00:00:00 2001 From: amayard Date: Wed, 18 Aug 2021 11:11:16 +0200 Subject: [PATCH 06/10] #367568 fixed cleanup cron datetime --- db/tasks.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/db/tasks.php b/db/tasks.php index de625d6..6bb8323 100644 --- a/db/tasks.php +++ b/db/tasks.php @@ -36,8 +36,8 @@ [ 'classname' => 'block_admin_presets\task\automatic_export_cleanup', 'blocking' => false, - 'minute' => 0, - 'hour' => 22, + 'minute' => 30, + 'hour' => 23, 'day' => '*', 'month' => '*', ] From 12e229ceb9c0c57d1110db85940216b6d76b4267 Mon Sep 17 00:00:00 2001 From: amayard Date: Tue, 14 Sep 2021 14:13:14 +0200 Subject: [PATCH 07/10] fix typo in readme --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 6009245..4172e5a 100755 --- a/README.md +++ b/README.md @@ -52,7 +52,7 @@ Automatic Export A feature was added allowing the plugin to perform an automatic export which will provide a daily back-up if enabled. You can enable this setting the same way you're configuring the plugin following : *Admin > Plugins > Plugins overview > Additional Plugins > Admin presets > Settings*. These backups are named with a timestamp looking like '20210816160918' and will be found with any other record of admin_presets : in the "presets" list that regroups exports, imports and automatic backups. -###How it works +### How it works - Automatic export daily at 23:00, creates an export with full config named wit ha time stamp **default : Enabled** - Automatic export cleanup runs daily at 23:30, will delete automatic exports as follows - Last 7 weeks : keeps all From 072d53a1142b359d1202c925f06503033aab44f8 Mon Sep 17 00:00:00 2001 From: amayard Date: Tue, 14 Sep 2021 14:13:48 +0200 Subject: [PATCH 08/10] fix typo in readme --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 4172e5a..07e36ae 100755 --- a/README.md +++ b/README.md @@ -53,7 +53,7 @@ A feature was added allowing the plugin to perform an automatic export which wil These backups are named with a timestamp looking like '20210816160918' and will be found with any other record of admin_presets : in the "presets" list that regroups exports, imports and automatic backups. ### How it works -- Automatic export daily at 23:00, creates an export with full config named wit ha time stamp **default : Enabled** +- Automatic export daily at 23:00, creates an export with full config named with a time stamp **default : Enabled** - Automatic export cleanup runs daily at 23:30, will delete automatic exports as follows - Last 7 weeks : keeps all - Last 14 days : keeps one export out of 2 From f4f2c510ecb59978cc17fb5aa2351cbc26da0ca9 Mon Sep 17 00:00:00 2001 From: amayard Date: Tue, 14 Sep 2021 16:17:28 +0200 Subject: [PATCH 09/10] refactor class to keep methods protected --- classes/task/automatic_export.php | 2 +- lib/admin_presets_base.class.php | 33 ++++++++++++++++--------------- 2 files changed, 18 insertions(+), 17 deletions(-) diff --git a/classes/task/automatic_export.php b/classes/task/automatic_export.php index 9622e90..1f28c75 100644 --- a/classes/task/automatic_export.php +++ b/classes/task/automatic_export.php @@ -67,7 +67,7 @@ public function execute() { $export = new admin_presets_export(); // Reload site settings. - $sitesettings = $export->_get_site_settings(); + $sitesettings = $export->load_site_settings(); // Set up $preset that should be in execute() $_POST header $_POST['sesskey'] = 'yms935m3dl'; diff --git a/lib/admin_presets_base.class.php b/lib/admin_presets_base.class.php index 381b64f..88bc79c 100755 --- a/lib/admin_presets_base.class.php +++ b/lib/admin_presets_base.class.php @@ -24,16 +24,6 @@ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ -//CBLUE EDIT : -//list of function that went from protected > public -//_get_site_settings -//_get_settings -//_get_setting -//_get_settings_branches -//_get_settings_elements -//_get_settings_from_db -//This was necessary in order to make the classes/task/automatic_export class to work as it calls _get_setting() which will use all the other functions. That class is used by a CRON job to automatically make an export every 24hours. -//END CBLUE EDIT defined('MOODLE_INTERNAL') || die(); @@ -59,6 +49,17 @@ class admin_presets_base { protected $moodleform; protected $rel; + /** + * This function will be used to call _get_site_settings with public privileges + * + * @return array + * @throws dml_exception + */ + public function load_site_settings() + { + return $this->_get_site_settings(); + } + /** * Loads common class attributes and initializes sensible settings and DB - XML relations */ @@ -283,7 +284,7 @@ public function log() { * @return array $settings Array format $array['plugin']['settingname'] = admin_preset_setting child class * @throws dml_exception */ - public function _get_site_settings() { + protected function _get_site_settings() { global $DB; @@ -330,7 +331,7 @@ public function _get_site_settings() { * @return array Array format $array['plugin']['settingname'] = admin_preset_setting child class * @throws dml_exception */ - public function _get_settings($dbsettings, $sitedbvalues = false, $settings, $children = false) { + protected function _get_settings($dbsettings, $sitedbvalues = false, $settings, $children = false) { global $DB; // If there are no children, load admin tree and iterate through. @@ -452,7 +453,7 @@ public function _get_settings($dbsettings, $sitedbvalues = false, $settings, $ch * @param mixed $currentvalue * @return bool */ - public function _get_setting($settingdata, $currentvalue) { + protected function _get_setting($settingdata, $currentvalue) { // Getting the appropiate class to get the correct setting value. $settingtype = get_class($settingdata); @@ -475,7 +476,7 @@ public function _get_setting($settingdata, $currentvalue) { * * @param array $settings Array format $array['plugin']['settingname'] = admin_preset_setting child class */ - public function _get_settings_branches($settings) { + protected function _get_settings_branches($settings) { global $PAGE; @@ -509,7 +510,7 @@ public function _get_settings_branches($settings) { * @param array $nodes Tree nodes * @return array Code to output */ - public function _get_settings_elements($allsettings, $admintree = false, $jsparentnode = false, &$nodes) { + protected function _get_settings_elements($allsettings, $admintree = false, $jsparentnode = false, &$nodes) { if (empty($this->adminroot)) { $this->adminroot = admin_get_root(false, true); @@ -591,7 +592,7 @@ public function _get_settings_elements($allsettings, $admintree = false, $jspare * @return array Standarized array, * format $array['plugin']['name'] = obj('name'=>'settingname', 'value'=>'settingvalue') */ - public function _get_settings_from_db($dbsettings) { + protected function _get_settings_from_db($dbsettings) { if (!$dbsettings) { return false; From 3b346396356e6eb0147de148d93c0c55a5a68b1a Mon Sep 17 00:00:00 2001 From: amayard Date: Wed, 15 Sep 2021 08:37:00 +0200 Subject: [PATCH 10/10] Refactor --- classes/task/automatic_export.php | 25 ++++++------------------- lang/en/block_admin_presets.php | 4 ++-- lib/admin_presets_base.class.php | 4 +--- settings.php | 4 ++-- 4 files changed, 11 insertions(+), 26 deletions(-) diff --git a/classes/task/automatic_export.php b/classes/task/automatic_export.php index 1f28c75..050af8e 100644 --- a/classes/task/automatic_export.php +++ b/classes/task/automatic_export.php @@ -69,33 +69,20 @@ public function execute() { // Reload site settings. $sitesettings = $export->load_site_settings(); - // Set up $preset that should be in execute() $_POST header - $_POST['sesskey'] = 'yms935m3dl'; - $_POST['_qf__admin_presets_export_form'] = '1'; - $_POST['mform_isexpanded_id_general'] = '1'; - $_POST['name'] = 'YYYYMMDDmmss'; - $_POST['comments'] = [ - 'text' => 'This is an automatic export for backup.', - 'format' => '1' - ]; - $_POST['author'] = 'Automatic Export'; - $_POST['admin_presets_submit'] = 'Save Changes'; - - //Construct $_POST for each site setting + //Construct $presets for each site setting foreach ($sitesettings as $sitesetting => $settingvalue) { foreach ($settingvalue as $key => $value) { - $_POST[$key . '@@' . $sitesetting] = '1'; + $presets[$key . '@@' . $sitesetting] = '1'; } } - // admin_preset record. $preset = new stdClass(); $preset->userid = '1'; $preset->name = date('YmdGis'); - $preset->comments = $_POST['comments']['text']; + $preset->comments = $presets['comments']['text']; $preset->site = $CFG->wwwroot; - $preset->author = $_POST['author']; + $preset->author = $presets['author']; $preset->moodleversion = $CFG->version; $preset->moodlerelease = $CFG->release; $preset->timecreated = time(); @@ -108,7 +95,7 @@ public function execute() { $this->id = $preset->id; // We must ensure that there are settings selected. - foreach ($_POST as $varname => $value) { + foreach ($presets as $varname => $value) { unset($setting); @@ -144,7 +131,7 @@ public function execute() { // If there are no valid or selected settings we should delete the admin preset record. if (empty($settingsfound)) { - $DB->delete_records('block_admin_presets', array('id' => $preset->id)); + $DB->delete_records('block_admin_presets', ['id' => $preset->id]); redirect($CFG->wwwroot . '/blocks/admin_presets/index.php?action=export', get_string('novalidsettingsselected', 'block_admin_presets'), 4); } diff --git a/lang/en/block_admin_presets.php b/lang/en/block_admin_presets.php index 8c3b9bd..3153fd1 100755 --- a/lang/en/block_admin_presets.php +++ b/lang/en/block_admin_presets.php @@ -118,10 +118,10 @@ $string['wrongfile'] = 'Wrong file'; $string['wrongid'] = 'Wrong id'; $string['privacy:null_reason'] = 'The admin presets block does not effect or store any user data'; -//CBLUE START #367565 +//CBLUE START $string['automaticexport'] = 'This is an automatic export of this moodle config'; $string['automaticexportconfig'] = 'Enable daily automatic backup exports'; $string['automaticexportcleanupconfig'] = 'Enable automatic export cleanup'; $string['automaticexportcleanupconfigtext'] = 'If enabled, automatic exports younger than a week will be kept, then up to 14 days 1/2 will be saved, up to a month 1/4 will be saved, then up to 3 months 1/8 will be saved. After 3 months all automatic exports will be deleted. Manual exports and imports won\'t be affected by this functionnality.'; $string['automaticexportcleanup'] = 'Enable automatic export cleanup'; -//CBLUE END #367565 +//CBLUE END diff --git a/lib/admin_presets_base.class.php b/lib/admin_presets_base.class.php index 88bc79c..b230080 100755 --- a/lib/admin_presets_base.class.php +++ b/lib/admin_presets_base.class.php @@ -24,7 +24,6 @@ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ - defined('MOODLE_INTERNAL') || die(); require_once($CFG->libdir . '/adminlib.php'); @@ -55,8 +54,7 @@ class admin_presets_base { * @return array * @throws dml_exception */ - public function load_site_settings() - { + public function load_site_settings() { return $this->_get_site_settings(); } diff --git a/settings.php b/settings.php index 7660ff2..e0042bf 100755 --- a/settings.php +++ b/settings.php @@ -38,7 +38,7 @@ get_string('sensiblesettingstext', 'block_admin_presets'), $sensiblesettingsdefault, PARAM_TEXT)); - //START CBLUE MODIFICATION + //CBLUE START $automaticexportdefault = false ; $automaticexportcleanupdefault = true ; @@ -53,5 +53,5 @@ get_string('automaticexportcleanupconfig', 'block_admin_presets'), get_string('automaticexportcleanupconfigtext', 'block_admin_presets'), $automaticexportcleanupdefault, true, false)); - //END CBLUE MODIFICATION + //CBLUE END }