Skip to content

Commit

Permalink
MDL-80960 core_sms: Add new subsystem and plugintype
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewnicols authored and mickhawkins committed Jul 11, 2024
1 parent 62ab88f commit 11f00f0
Show file tree
Hide file tree
Showing 24 changed files with 2,392 additions and 5 deletions.
6 changes: 6 additions & 0 deletions .upgradenotes/MDL-80960-2024052106033830.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
issueNumber: MDL-80960
notes:
core:
- message: |
A new `\core_sms` subsystem has been created
type: improved
5 changes: 5 additions & 0 deletions .upgradenotes/MDL-80960-2024071006131218.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
issueNumber: MDL-80960
notes:
core_sms:
- message: A new `\core_sms` subsystem has been created
type: improved
41 changes: 41 additions & 0 deletions lang/en/sms.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?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/>.

/**
* Strings for component 'sms', language 'en'
*
* @package core
* @category string
* @copyright 2024 Andrew Lyons <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
$string['phonenumbernotvalid'] = 'Format of phone number not recognised: {$a->message}';
$string['privacy:metadata:sms_messages'] = 'Stores messages sent via SMS';
$string['privacy:metadata:sms_messages:content'] = 'The message text';
$string['privacy:metadata:sms_messages:id'] = 'The ID of the message';
$string['privacy:metadata:sms_messages:recipient'] = 'The phone number that the message was sent to';
$string['privacy:metadata:sms_messages:recipientuserid'] = 'The user who the message was sent to, if known';
$string['privacy:metadata:sms_messages:status'] = 'The status of the message';
$string['privacy:metadata:sms_messages:timecreated'] = 'The time the message was created';
$string['privacy:sms:sensitive_not_shown'] = 'The content of this message was not stored as it was marked as containing sensitive content.';
$string['sms'] = 'SMS';
$string['status:gateway_not_available'] = 'The gateway is not available to send the message';
$string['status:gateway_failed'] = 'The gateway has failed to send the message';
$string['status:gateway_queued'] = 'The message is queued to be sent by the gateway';
$string['status:gateway_rejected'] = 'The gateway has rejected the message';
$string['status:gateway_sent'] = 'The message has been sent by the gateway';
$string['status:message_over_size'] = 'The message is too large to be sent by the gateway';
$string['status:unknown'] = 'Unable to determine the status of the message';
3 changes: 3 additions & 0 deletions lib/classes/di.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,9 @@ protected static function create_container(): ContainerInterface {
// Alias the PSR-20 clock interface to the Moodle clock. They are compatible.
\core\clock::class => fn() => new \core\system_clock(),
\Psr\Clock\ClockInterface::class => \DI\get(\core\clock::class),

// Note: libphonenumber PhoneNumberUtil uses a singleton.
\libphonenumber\PhoneNumberUtil::class => fn() => \libphonenumber\PhoneNumberUtil::getInstance(),
]);

// Add any additional definitions using hooks.
Expand Down
72 changes: 72 additions & 0 deletions lib/classes/plugininfo/smsgateway.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?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 core\plugininfo;

use moodle_url;

/**
* SMS gateway subplugin info class.
*
* @package core
* @copyright 2024 Andrew Lyons <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class smsgateway extends base {
public function is_uninstall_allowed(): bool {
return true;
}

public function get_settings_section_name(): string {
return "smsgateway{$this->name}";
}

public function load_settings(
\part_of_admin_tree $adminroot,
$parentnodename,
$hassiteconfig,
): void {
global $CFG, $USER, $DB, $OUTPUT, $PAGE; // In case settings.php wants to refer to them.
/** @var \admin_root $ADMIN */
$ADMIN = $adminroot; // May be used in settings.php.
$plugininfo = $this; // Also can be used inside settings.php.

if (!$this->is_installed_and_upgraded()) {
return;
}

if (!$hassiteconfig) {
return;
}

$section = $this->get_settings_section_name();

$settings = null;
if (file_exists($this->full_path('settings.php'))) {
$settings = new \admin_settingpage($section, $this->displayname, 'moodle/site:config', $this->is_enabled() === false);
include($this->full_path('settings.php')); // This may also set $settings to null.
}
if ($settings) {
$ADMIN->add($parentnodename, $settings);
}
}

public static function get_manage_url(): moodle_url {
return new \moodle_url('/admin/settings.php', [
'section' => 'smsgateway',
]);
}
}
4 changes: 3 additions & 1 deletion lib/components.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@
"theme": "theme",
"local": "local",
"h5plib": "h5p\/h5plib",
"paygw": "payment\/gateway"
"paygw": "payment\/gateway",
"smsgateway": "sms/gateway"
},
"subsystems": {
"access": null,
Expand Down Expand Up @@ -115,6 +116,7 @@
"rss": "rss",
"role": "admin\/roles",
"search": "search",
"sms": "sms",
"table": "lib\/table",
"tag": "tag",
"timezones": null,
Expand Down
31 changes: 30 additions & 1 deletion lib/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="lib/db" VERSION="20240408" COMMENT="XMLDB file for core Moodle tables"
<XMLDB PATH="lib/db" VERSION="20240230" COMMENT="XMLDB file for core Moodle tables"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="../../lib/xmldb/xmldb.xsd"
>
Expand Down Expand Up @@ -4842,5 +4842,34 @@
<KEY NAME="primary" TYPE="primary" FIELDS="id"/>
</KEYS>
</TABLE>
<TABLE NAME="sms_messages" COMMENT="SMS Messages sent via the SMS subsystem">
<FIELDS>
<FIELD NAME="id" TYPE="int" LENGTH="10" NOTNULL="true" SEQUENCE="true"/>
<FIELD NAME="recipientnumber" TYPE="char" LENGTH="30" NOTNULL="true" SEQUENCE="false"/>
<FIELD NAME="content" TYPE="text" NOTNULL="false" SEQUENCE="false"/>
<FIELD NAME="component" TYPE="char" LENGTH="100" NOTNULL="true" SEQUENCE="false"/>
<FIELD NAME="messagetype" TYPE="char" LENGTH="100" NOTNULL="true" SEQUENCE="false"/>
<FIELD NAME="recipientuserid" TYPE="int" LENGTH="10" NOTNULL="false" SEQUENCE="false"/>
<FIELD NAME="issensitive" TYPE="int" LENGTH="2" NOTNULL="true" DEFAULT="0" SEQUENCE="false"/>
<FIELD NAME="gatewayid" TYPE="int" LENGTH="10" NOTNULL="false" SEQUENCE="false"/>
<FIELD NAME="status" TYPE="char" LENGTH="100" NOTNULL="false" SEQUENCE="false"/>
<FIELD NAME="timecreated" TYPE="int" LENGTH="10" NOTNULL="true" SEQUENCE="false"/>
</FIELDS>
<KEYS>
<KEY NAME="primary" TYPE="primary" FIELDS="id"/>
<KEY NAME="gateway" TYPE="foreign" FIELDS="gatewayid" REFTABLE="sms_gateways" REFFIELDS="id"/>
</KEYS>
</TABLE>
<TABLE NAME="sms_gateways" COMMENT="Instances of SMS gateways">
<FIELDS>
<FIELD NAME="id" TYPE="int" LENGTH="10" NOTNULL="true" SEQUENCE="true"/>
<FIELD NAME="gateway" TYPE="char" LENGTH="255" NOTNULL="true" SEQUENCE="false"/>
<FIELD NAME="enabled" TYPE="int" LENGTH="2" NOTNULL="true" DEFAULT="1" SEQUENCE="false"/>
<FIELD NAME="config" TYPE="text" NOTNULL="true" SEQUENCE="false"/>
</FIELDS>
<KEYS>
<KEY NAME="primary" TYPE="primary" FIELDS="id"/>
</KEYS>
</TABLE>
</TABLES>
</XMLDB>
47 changes: 46 additions & 1 deletion lib/db/upgrade.php
Original file line number Diff line number Diff line change
Expand Up @@ -1105,7 +1105,6 @@ function xmldb_main_upgrade($oldversion) {
// Main savepoint reached.
upgrade_main_savepoint(true, 2024030500.01);
}

}

if ($oldversion < 2024030500.02) {
Expand Down Expand Up @@ -1170,5 +1169,51 @@ function xmldb_main_upgrade($oldversion) {
// Automatically generated Moodle v4.4.0 release upgrade line.
// Put any upgrade step following this.

if ($oldversion < 2024071000.00) {
// Define table sms_gateways to be created.
$table = new xmldb_table('sms_gateways');

// Adding fields to table sms_gateways.
$table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
$table->add_field('gateway', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
$table->add_field('enabled', XMLDB_TYPE_INTEGER, '2', null, XMLDB_NOTNULL, null, '1');
$table->add_field('config', XMLDB_TYPE_TEXT, null, null, XMLDB_NOTNULL, null, null);

// Adding keys to table sms_gateways.
$table->add_key('primary', XMLDB_KEY_PRIMARY, ['id']);

// Conditionally launch create table for sms_gateways.
if (!$dbman->table_exists($table)) {
$dbman->create_table($table);
}

// Define table sms_messages to be created.
$table = new xmldb_table('sms_messages');

// Adding fields to table sms_messages.
$table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
$table->add_field('recipientnumber', XMLDB_TYPE_CHAR, '30', null, XMLDB_NOTNULL, null, null);
$table->add_field('content', XMLDB_TYPE_TEXT, null, null, null, null, null);
$table->add_field('component', XMLDB_TYPE_CHAR, '100', null, XMLDB_NOTNULL, null, null);
$table->add_field('messagetype', XMLDB_TYPE_CHAR, '100', null, XMLDB_NOTNULL, null, null);
$table->add_field('recipientuserid', XMLDB_TYPE_INTEGER, '10', null, null, null, null);
$table->add_field('issensitive', XMLDB_TYPE_INTEGER, '2', null, XMLDB_NOTNULL, null, '0');
$table->add_field('gatewayid', XMLDB_TYPE_INTEGER, '10', null, null, null, null);
$table->add_field('status', XMLDB_TYPE_CHAR, '100', null, null, null, null);
$table->add_field('timecreated', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);

// Adding keys to table sms_messages.
$table->add_key('primary', XMLDB_KEY_PRIMARY, ['id']);
$table->add_key('gatewayid', XMLDB_KEY_FOREIGN, ['gatewayid'], 'sms_gateways', ['id']);

// Conditionally launch create table for sms_messages.
if (!$dbman->table_exists($table)) {
$dbman->create_table($table);
}

// Main savepoint reached.
upgrade_main_savepoint(true, 2024071000.00);
}

return true;
}
2 changes: 1 addition & 1 deletion lib/tests/attribute_helper_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ public function test_get_attributes_references(): void {
$this->assertInstanceOf(attribute_helper_attribute_b::class, $instance);
}

public function test_get_attributes_invalid(): void {
public function test_get_attributes_not_valid(): void {
$this->assertNull(attribute_helper::from(non_existent_class::class));
$this->assertNull(attribute_helper::from([non_existent_class::class]));
$this->assertNull(attribute_helper::from([attribute_helper_example::class, 'non_existent']));
Expand Down
28 changes: 28 additions & 0 deletions sms/classes/description.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?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 core_sms;

/**
* A language string attribute used for a description.
*
* @package core_sms
* @copyright 2024 Andrew Lyons <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
#[\Attribute]
class description extends \lang_string {
}
Loading

0 comments on commit 11f00f0

Please sign in to comment.