Skip to content

Commit

Permalink
Merge pull request #11 from catalyst/validation
Browse files Browse the repository at this point in the history
Validation
  • Loading branch information
tuanngocnguyen authored Oct 25, 2024
2 parents e0d97fe + 5f836d7 commit 55d7cb8
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 9 deletions.
19 changes: 15 additions & 4 deletions classes/lifecycle/step.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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));
}

}
Expand Down Expand Up @@ -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;
Expand All @@ -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);
}
}
Expand Down
5 changes: 4 additions & 1 deletion classes/s3/helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -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.
Expand All @@ -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);
}

Expand Down
4 changes: 3 additions & 1 deletion db/install.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8" ?>
<XMLDB PATH="admin/tool/lcbackupcoursestep/db" VERSION="20241023" COMMENT="XMLDB file for Moodle admin/tool/lcbackupcoursestep"
<XMLDB PATH="admin/tool/lcbackupcoursestep/db" VERSION="20241025" COMMENT="XMLDB file for Moodle admin/tool/lcbackupcoursestep"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="../../../../lib/xmldb/xmldb.xsd"
>
Expand All @@ -12,6 +12,8 @@
<FIELD NAME="courseid" TYPE="int" LENGTH="10" NOTNULL="true" SEQUENCE="false" COMMENT="Course ID"/>
<FIELD NAME="filename" TYPE="text" NOTNULL="true" SEQUENCE="false" COMMENT="Name of the uploaded file"/>
<FIELD NAME="contenthash" TYPE="char" LENGTH="40" NOTNULL="true" SEQUENCE="false" COMMENT="Content hash of the uploaded file"/>
<FIELD NAME="bucketname" TYPE="char" LENGTH="512" NOTNULL="false" SEQUENCE="false" COMMENT="Name of the s3 bucket"/>
<FIELD NAME="timecreated" TYPE="int" LENGTH="10" NOTNULL="true" DEFAULT="0" SEQUENCE="false"/>
</FIELDS>
<KEYS>
<KEY NAME="primary" TYPE="primary" FIELDS="id"/>
Expand Down
27 changes: 25 additions & 2 deletions db/upgrade.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -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;
}
1 change: 1 addition & 0 deletions tests/step_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
2 changes: 1 addition & 1 deletion version.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

defined('MOODLE_INTERNAL') || die();

$plugin->version = 2023100401;
$plugin->version = 2023100402;
$plugin->requires = 2020061500;
$plugin->component = 'tool_lcbackupcoursestep';

Expand Down

0 comments on commit 55d7cb8

Please sign in to comment.