diff --git a/classes/lifecycle/step.php b/classes/lifecycle/step.php index d34c179..37094c6 100644 --- a/classes/lifecycle/step.php +++ b/classes/lifecycle/step.php @@ -23,6 +23,7 @@ use admin_externalpage; use backup_plan_dbops; +use core\output\notification; use moodle_url; use tool_lcbackupcoursestep\s3\helper; use tool_lifecycle\local\manager\settings_manager; @@ -279,7 +280,7 @@ public function extend_add_instance_form_definition($mform) { $this->add_amazon_s3_settings($mform); } else { $mform->addElement('html', $OUTPUT->notification(get_string('s3_unmet_dependency', 'tool_lcbackupcoursestep'), - \core\output\notification::NOTIFY_WARNING)); + notification::NOTIFY_WARNING)); } } @@ -365,6 +366,15 @@ public function extend_add_instance_form_validation(&$error, $data) { if (empty($data['s3_region'])) { $error['s3_region'] = get_string('required'); } + + // Check connection if there is no error. + if (empty($error)) { + $connection = helper::check_connection($data); + if (!$connection->success) { + // We already show error on s3_status field, so no need to show it here. + $error['s3_status'] = ''; + } + } } return $error; @@ -379,14 +389,15 @@ public function extend_add_instance_form_validation(&$error, $data) { public function extend_add_instance_form_definition_after_data($mform, $settings) { global $OUTPUT; if (!empty($settings['uses3'])) { - $connection = helper::check_connection($settings); + $data = $mform->exportValues(); + $connection = helper::check_connection($data); if (!$connection->success) { $message = $OUTPUT->notification(get_string('s3_connection_error', 'tool_lcbackupcoursestep', $connection->details), - \core\output\notification::NOTIFY_ERROR); + notification::NOTIFY_ERROR); $mform->setDefault('s3_status', $message); } else { $message = $OUTPUT->notification(get_string('s3_connection_success', 'tool_lcbackupcoursestep'), - \core\output\notification::NOTIFY_SUCCESS); + notification::NOTIFY_SUCCESS); $mform->setDefault('s3_status', $message); } } diff --git a/classes/s3/helper.php b/classes/s3/helper.php index f048421..4afe112 100644 --- a/classes/s3/helper.php +++ b/classes/s3/helper.php @@ -42,6 +42,7 @@ public static function met_dependency(): bool { if (!file_exists($CFG->dirroot . '/local/aws/version.php')) { return false; } + require_once($CFG->dirroot . '/local/aws/sdk/aws-autoloader.php'); return true; } @@ -133,7 +134,7 @@ public static function upload_file(int $processid, int $instanceid, int $coursei $connection = self::check_connection($settings); if (!$connection->success) { - throw new \moodle_exception('s3_connection_error', 'tool_lcbackupcoursestep', $connection->details); + throw new \moodle_exception('s3_connection_error', 'tool_lcbackupcoursestep', '', $connection->details); } // Upload file. @@ -151,6 +152,8 @@ public static function upload_file(int $processid, int $instanceid, int $coursei $filedetails->courseid = $courseid; $filedetails->filename = $file->get_filename(); $filedetails->contenthash = $file->get_contenthash(); + $filedetails->bucketname = $settings['s3_bucket']; + $filedetails->timecreated = time(); $DB->insert_record('tool_lcbackupcoursestep_s3', $filedetails); } diff --git a/db/install.xml b/db/install.xml index 9dd452f..67ddad8 100644 --- a/db/install.xml +++ b/db/install.xml @@ -1,5 +1,5 @@ - @@ -12,6 +12,8 @@ + + diff --git a/db/upgrade.php b/db/upgrade.php index 23a03ae..7a635b6 100644 --- a/db/upgrade.php +++ b/db/upgrade.php @@ -36,6 +36,8 @@ function xmldb_tool_lcbackupcoursestep_upgrade($oldversion) { global $DB; + $dbman = $DB->get_manager(); + if ($oldversion < 2023100401) { // New table to be created. $table = new xmldb_table('tool_lcbackupcoursestep_s3'); @@ -50,12 +52,33 @@ function xmldb_tool_lcbackupcoursestep_upgrade($oldversion) { $table->add_index('processid_idx', XMLDB_INDEX_NOTUNIQUE, ['processid']); $table->add_index('courseid_idx', XMLDB_INDEX_NOTUNIQUE, ['courseid']); - if (!$DB->get_manager()->table_exists($table)) { - $DB->get_manager()->create_table($table); + if (!$dbman->table_exists($table)) { + $dbman->create_table($table); } upgrade_plugin_savepoint(true, 2023100401, 'tool', 'lcbackupcoursestep'); } + if ($oldversion < 2023100402) { + + // Define field bucketname to be added to tool_lcbackupcoursestep_s3. + $table = new xmldb_table('tool_lcbackupcoursestep_s3'); + + // Conditionally launch add field bucketname. + $field = new xmldb_field('bucketname', XMLDB_TYPE_CHAR, '512', null, null, null, null, 'contenthash'); + if (!$dbman->field_exists($table, $field)) { + $dbman->add_field($table, $field); + } + + // Conditionally launch add field timecreated. + $field = new xmldb_field('timecreated', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0', 'bucketname'); + if (!$dbman->field_exists($table, $field)) { + $dbman->add_field($table, $field); + } + + // Lcbackupcoursestep savepoint reached. + upgrade_plugin_savepoint(true, 2023100402, 'tool', 'lcbackupcoursestep'); + } + return true; } diff --git a/tests/step_test.php b/tests/step_test.php index 4aa2408..b0eb694 100644 --- a/tests/step_test.php +++ b/tests/step_test.php @@ -261,5 +261,6 @@ public function test_backup_course_step_s3() { $filedetails = $DB->get_record('tool_lcbackupcoursestep_s3', ['processid' => $process->id]); $this->assertEquals($filedetails->courseid, $this->course->id); $this->assertEquals($filedetails->filename, $file->filename); + $this->assertEquals('testbucket', $filedetails->bucketname); } } diff --git a/version.php b/version.php index 99d20cd..5a828c3 100644 --- a/version.php +++ b/version.php @@ -24,7 +24,7 @@ defined('MOODLE_INTERNAL') || die(); -$plugin->version = 2023100401; +$plugin->version = 2023100402; $plugin->requires = 2020061500; $plugin->component = 'tool_lcbackupcoursestep';