Skip to content

Commit

Permalink
MDL-81924 smsgateway_aws: Add hooks for SMS gateway management
Browse files Browse the repository at this point in the history
Originally implemented as MDL-81732.

Co-authored by: Michael Hawkins <[email protected]>
  • Loading branch information
safatshahin authored and HuongNV13 committed Sep 23, 2024
1 parent a1209ab commit 3348832
Show file tree
Hide file tree
Showing 5 changed files with 167 additions and 20 deletions.
110 changes: 110 additions & 0 deletions sms/gateway/aws/classes/hook_listener.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
<?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/>.

namespace smsgateway_aws;

use core_sms\hook\after_sms_gateway_form_hook;

/**
* Hook listener for aws sms gateway.
*
* @package smsgateway_aws
* @copyright 2024 Safat Shahin <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class hook_listener {

/**
* Hook listener for the sms gateway setup form.
*
* @param after_sms_gateway_form_hook $hook The hook to add to sms gateway setup.
*/
public static function set_form_definition_for_aws_sms_gateway(after_sms_gateway_form_hook $hook): void {
if ($hook->plugin !== 'smsgateway_aws') {
return;
}

$mform = $hook->mform;

$mform->addElement('static', 'information', get_string('aws_information', 'smsgateway_aws'));

$gateways = [
'aws_sns' => get_string('aws_sns', 'smsgateway_aws'),
];
$mform->addElement(
'select',
'gateway',
get_string('gateway', 'smsgateway_aws'),
$gateways,
);
$mform->setDefault(
elementName: 'gateway',
defaultValue: 'aws_sns',
);
// Remove this if more aws gateway implemented, eg sqs.
$mform->hardFreeze('gateway');

$mform->addElement(
'checkbox',
'usecredchain',
get_string('usecredchain', 'smsgateway_aws'),
' ',
);
$mform->setDefault(
elementName: 'usecredchain',
defaultValue: 0,
);

$mform->addElement(
'text',
'api_key',
get_string('api_key', 'smsgateway_aws'),
'maxlength="255" size="20"',
);
$mform->setType('api_key', PARAM_TEXT);
$mform->addRule('api_key', get_string('maximumchars', '', 255), 'maxlength', 255);
$mform->setDefault(
elementName: 'api_key',
defaultValue: '',
);
$mform->addElement(
'passwordunmask',
'api_secret',
get_string('api_secret', 'smsgateway_aws'),
'maxlength="255" size="20"',
);
$mform->setType('api_secret', PARAM_TEXT);
$mform->addRule('api_secret', get_string('maximumchars', '', 255), 'maxlength', 255);
$mform->setDefault(
elementName: 'api_secret',
defaultValue: '',
);

$mform->addElement(
'text',
'api_region',
get_string('api_region', 'smsgateway_aws'),
'maxlength="255" size="20"',
);
$mform->setType('api_region', PARAM_TEXT);
$mform->addRule('api_region', get_string('maximumchars', '', 255), 'maxlength', 255);
$mform->setDefault(
elementName: 'api_region',
defaultValue: 'ap-southeast-2',
);
}

}
32 changes: 32 additions & 0 deletions sms/gateway/aws/db/hooks.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?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/>.

/**
* Hook listener callbacks for aws sms gateway.
*
* @package smsgateway_aws
* @copyright 2024 Safat Shahin <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

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

$callbacks = [
[
'hook' => \core_sms\hook\after_sms_gateway_form_hook::class,
'callback' => \smsgateway_aws\hook_listener::class . '::set_form_definition_for_aws_sms_gateway',
],
];
22 changes: 7 additions & 15 deletions sms/gateway/aws/lang/en/smsgateway_aws.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,21 +22,13 @@
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

$string['api_key'] = 'Key';
$string['api_key_help'] = 'Amazon API key credential.';
$string['api_region'] = 'Region';
$string['api_region_help'] = 'Amazon API gateway region.';
$string['api_secret'] = 'Secret';
$string['api_secret_help'] = 'Amazon API secret credential.';
$string['api_key'] = 'Access key';
$string['api_region'] = 'Amazon API gateway region';
$string['api_secret'] = 'Secret access key';
$string['aws_information'] = 'Complete the following fields using the information provided by AWS';
$string['aws_sns'] = 'AWS SNS';
$string['countrycode'] = 'Country number code';
$string['countrycode_help'] = 'The calling code without the leading + as a default if users do not enter an international number with a + prefix.
See this link for a list of calling codes: {$a}';
$string['gateway'] = 'SMS Gateway';
$string['gateway_help'] = 'The SMS provider you wish to send messages via';
$string['gateway'] = 'AWS service';
$string['aws_information'] = 'Complete the following fields using the information provided by AWS';
$string['pluginname'] = 'AWS';
$string['privacy:metadata'] = 'The AWS SMS gateway plugin does not store any personal data.';
$string['usecredchain'] = 'Find AWS credentials using the default credential provider chain';


$string['usecredchain'] = 'Find AWS credentials using the default provider chain';
21 changes: 17 additions & 4 deletions sms/gateway/aws/tests/gateway_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,30 @@ class gateway_test extends \advanced_testcase {
public function test_update_message_status(): void {
$this->resetAfterTest();

$config = new \stdClass();
$config->api_key = 'test_api_key';

$manager = \core\di::get(\core_sms\manager::class);
$gw = $manager->create_gateway_instance(gateway::class, true);
$othergw = $manager->create_gateway_instance(gateway::class, true);
$gw = $manager->create_gateway_instance(
classname: gateway::class,
name: 'aws',
enabled: true,
config: $config,
);
$othergw = $manager->create_gateway_instance(
classname: gateway::class,
name: 'aws',
enabled: true,
config: $config,
);

$message = new message(
recipientnumber: '1234567890',
content: 'Hello, world!',
component: 'smsgateway_aws',
messagetype: 'test',
recipientuserid: null,
sensitive: false,
issensitive: false,
gatewayid: $gw->id,
);
$message2 = new message(
Expand All @@ -51,7 +64,7 @@ public function test_update_message_status(): void {
component: 'smsgateway_aws',
messagetype: 'test',
recipientuserid: null,
sensitive: false,
issensitive: false,
gatewayid: $gw->id,
);

Expand Down
2 changes: 1 addition & 1 deletion sms/gateway/aws/version.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,6 @@
defined('MOODLE_INTERNAL') || die();

$plugin->component = 'smsgateway_aws';
$plugin->version = 2024042200;
$plugin->version = 2024082200;
$plugin->requires = 2024041600;
$plugin->maturity = MATURITY_STABLE;

0 comments on commit 3348832

Please sign in to comment.