diff --git a/communication/provider/matrix/classes/communication_feature.php b/communication/provider/matrix/classes/communication_feature.php
index b43ac7c222a..739c06a385e 100644
--- a/communication/provider/matrix/classes/communication_feature.php
+++ b/communication/provider/matrix/classes/communication_feature.php
@@ -29,8 +29,8 @@ class communication_feature implements
\core_communication\communication_provider,
\core_communication\user_provider,
\core_communication\room_chat_provider,
- \core_communication\room_user_provider {
-
+ \core_communication\room_user_provider,
+ \core_communication\form_provider {
/** @var matrix_events_manager $eventmanager The event manager object to get the endpoints */
private matrix_events_manager $eventmanager;
@@ -234,7 +234,7 @@ class communication_feature implements
}
public function create_chat_room(): bool {
- if ($this->matrixrooms->room_record_exists()) {
+ if ($this->matrixrooms->room_record_exists() && $this->matrixrooms->get_matrix_room_id()) {
return $this->update_chat_room();
}
// Create a new room.
@@ -245,12 +245,21 @@ class communication_feature implements
'initial_state' => [],
];
+ // Set the room topic if set.
+ if (!empty($matrixroomtopic = $this->matrixrooms->get_matrix_room_topic())) {
+ $json['topic'] = $matrixroomtopic;
+ }
+
$response = $this->eventmanager->request($json)->post($this->eventmanager->get_create_room_endpoint());
$response = json_decode($response->getBody(), false, 512, JSON_THROW_ON_ERROR);
// Check if room was created.
if (!empty($roomid = $response->room_id)) {
- $this->matrixrooms->create_matrix_room_record($this->communication->get_id(), $roomid);
+ if ($this->matrixrooms->room_record_exists()) {
+ $this->matrixrooms->update_matrix_room_record($roomid, $matrixroomtopic);
+ } else {
+ $this->matrixrooms->create_matrix_room_record($this->communication->get_id(), $roomid, $matrixroomtopic);
+ }
$this->eventmanager->roomid = $roomid;
$this->update_room_avatar();
return true;
@@ -274,6 +283,13 @@ class communication_feature implements
$this->eventmanager->request($json)->put($this->eventmanager->get_update_room_name_endpoint());
}
+ // Update the room topic if set.
+ if (!empty($matrixroomtopic = $this->matrixrooms->get_matrix_room_topic())) {
+ $json = ['topic' => $matrixroomtopic];
+ $this->eventmanager->request($json)->put($this->eventmanager->get_update_room_topic_endpoint());
+ $this->matrixrooms->update_matrix_room_record($this->matrixrooms->get_matrix_room_id(), $matrixroomtopic);
+ }
+
// Update room avatar.
$this->update_room_avatar();
@@ -313,4 +329,33 @@ class communication_feature implements
return $this->eventmanager->matrixwebclienturl . '#/room/' . $this->matrixrooms->get_matrix_room_id();
}
+
+ public function save_form_data(\stdClass $instance): void {
+ $matrixroomtopic = $instance->matrixroomtopic ?? null;
+ if ($this->matrixrooms->room_record_exists()) {
+ $this->matrixrooms->update_matrix_room_record($this->matrixrooms->get_matrix_room_id(), $matrixroomtopic);
+ } else {
+ // Create the record with empty room id as we don't have it yet.
+ $this->matrixrooms->create_matrix_room_record(
+ $this->communication->get_id(),
+ $this->matrixrooms->get_matrix_room_id(),
+ $matrixroomtopic,
+ );
+ }
+ }
+
+ public function set_form_data(\stdClass $instance): void {
+ if (!empty($instance->id) && !empty($this->communication->get_id())) {
+ $instance->matrixroomtopic = $this->matrixrooms->get_matrix_room_topic();
+ }
+ }
+
+ public static function set_form_definition(\MoodleQuickForm $mform): void {
+ // Room description for the communication provider.
+ $mform->insertElementBefore($mform->createElement('text', 'matrixroomtopic',
+ get_string('matrixroomtopic', 'communication_matrix'),
+ 'maxlength="255" size="20"'), 'addcommunicationoptionshere');
+ $mform->addHelpButton('matrixroomtopic', 'matrixroomtopic', 'communication_matrix');
+ $mform->setType('matrixroomtopic', PARAM_TEXT);
+ }
}
diff --git a/communication/provider/matrix/classes/matrix_rooms.php b/communication/provider/matrix/classes/matrix_rooms.php
index cd33ef802d7..a3b6d74d3dd 100644
--- a/communication/provider/matrix/classes/matrix_rooms.php
+++ b/communication/provider/matrix/classes/matrix_rooms.php
@@ -56,13 +56,19 @@ class matrix_rooms {
* Create matrix room data.
*
* @param int $commid The id of the communication record
- * @param string $roomid The id of the room from matrix
+ * @param string|null $roomid The id of the room from matrix
+ * @param string|null $roomtopic The topic of the room for matrix
*/
- public function create_matrix_room_record(int $commid, string $roomid): void {
+ public function create_matrix_room_record(
+ int $commid,
+ ?string $roomid,
+ ?string $roomtopic
+ ): void {
global $DB;
$roomrecord = new \stdClass();
$roomrecord->commid = $commid;
$roomrecord->roomid = $roomid;
+ $roomrecord->topic = $roomtopic;
$roomrecord->id = $DB->insert_record('matrix_rooms', $roomrecord);
$this->matrixroomrecord = $roomrecord;
}
@@ -70,12 +76,14 @@ class matrix_rooms {
/**
* Update matrix room data.
*
- * @param string $roomid The id of the room from matrix
+ * @param string|null $roomid The id of the room from matrix
+ * @param string|null $roomtopic The topic of the room for matrix
*/
- public function update_matrix_room_record(string $roomid): void {
+ public function update_matrix_room_record(?string $roomid, ?string $roomtopic): void {
global $DB;
if ($this->room_record_exists()) {
$this->matrixroomrecord->roomid = $roomid;
+ $this->matrixroomrecord->topic = $roomtopic;
$DB->update_record('matrix_rooms', $this->matrixroomrecord);
}
}
@@ -105,6 +113,18 @@ class matrix_rooms {
return null;
}
+ /**
+ * Get the matrix room topic.
+ *
+ * @return string|null
+ */
+ public function get_matrix_room_topic(): ?string {
+ if ($this->room_record_exists()) {
+ return $this->matrixroomrecord->topic;
+ }
+ return null;
+ }
+
/**
* Check if room record exist for matrix.
*
diff --git a/communication/provider/matrix/db/install.xml b/communication/provider/matrix/db/install.xml
index ac373256b78..3fa23eb7b24 100644
--- a/communication/provider/matrix/db/install.xml
+++ b/communication/provider/matrix/db/install.xml
@@ -9,6 +9,7 @@
+
diff --git a/communication/provider/matrix/db/upgrade.php b/communication/provider/matrix/db/upgrade.php
new file mode 100644
index 00000000000..c6e1aebeb25
--- /dev/null
+++ b/communication/provider/matrix/db/upgrade.php
@@ -0,0 +1,45 @@
+.
+
+/**
+ * Install steps for communication_matrix.
+ *
+ * @package communication_matrix
+ * @copyright 2023 Safat Shahin
+ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+
+/**
+ * Upgrade procedures for the matrix plugin.
+ *
+ * @return bool
+ */
+function xmldb_communication_matrix_upgrade($oldversion) {
+ global $DB;
+
+ $dbman = $DB->get_manager();
+ if ($oldversion < 2023041100) {
+ $table = new xmldb_table('matrix_rooms');
+ $field = new xmldb_field('topic', XMLDB_TYPE_CHAR, '255', null, false, false, null, 'roomid');
+
+ if (!$dbman->field_exists($table, $field)) {
+ $dbman->add_field($table, $field);
+ }
+ }
+
+ return true;
+
+}
diff --git a/communication/provider/matrix/lang/en/communication_matrix.php b/communication/provider/matrix/lang/en/communication_matrix.php
index a394b556892..79fe9aa3c20 100644
--- a/communication/provider/matrix/lang/en/communication_matrix.php
+++ b/communication/provider/matrix/lang/en/communication_matrix.php
@@ -32,5 +32,7 @@ $string['matrixrefreshtoken'] = 'Refresh token';
$string['matrixrefreshtoken_desc'] = 'Admin refresh token to associated with the access token.';
$string['matrixelementurl'] = 'Element web URL';
$string['matrixelementurl_desc'] = 'The URL to Element Web instance.';
+$string['matrixroomtopic'] = 'Room topic';
+$string['matrixroomtopic_help'] = 'A short description of what this room is for.';
$string['pluginname'] = 'Matrix';
$string['privacy:metadata'] = 'Matrix communication plugin does not store any personal data.';
diff --git a/communication/provider/matrix/tests/behat/matrix_form_fields.feature b/communication/provider/matrix/tests/behat/matrix_form_fields.feature
new file mode 100644
index 00000000000..f403db6fd6a
--- /dev/null
+++ b/communication/provider/matrix/tests/behat/matrix_form_fields.feature
@@ -0,0 +1,53 @@
+@communication @communication_matrix @javascript
+Feature: Communication matrix form field
+ In order to create a new communication room in matrix
+ As a teacher
+ I can update the room the information from course
+
+ Background: Make sure the mock server is initialized and a course is created for teacher
+ Given the following "users" exist:
+ | username | firstname | lastname | email |
+ | teacher1 | Teacher | 1 | teacher1@example.com |
+ And the following "courses" exist:
+ | fullname | shortname | category |
+ | Test course | Test course | 0 |
+ And the following "course enrolments" exist:
+ | user | course | role |
+ | teacher1 | Test course | editingteacher |
+
+ Scenario: I can add room name for matrix room
+ Given a Matrix mock server is configured
+ And I log in as "teacher1"
+ And I am on "Test course" course homepage
+ When I navigate to "Settings" in current page administration
+ And I click on "Communication" "link"
+ And I set the field "id_selectedcommunication" to "Matrix"
+ And I wait to be redirected
+ And I should see "Room name"
+ And I set the field "id_communicationroomname" to "Sampleroomname"
+ And I press "Save and display"
+ And I navigate to "Settings" in current page administration
+ And I expand all fieldsets
+ Then the field "id_communicationroomname" matches value "Sampleroomname"
+
+ Scenario: I can add room topic for matrix room
+ Given a Matrix mock server is configured
+ And I log in as "teacher1"
+ And I am on "Test course" course homepage
+ When I navigate to "Settings" in current page administration
+ And I click on "Communication" "link"
+ And I set the field "id_selectedcommunication" to "Matrix"
+ And I wait to be redirected
+ And I should see "Room name"
+ And I should see "Room topic"
+ And I set the field "id_communicationroomname" to "Sampleroomname"
+ And I set the field "id_matrixroomtopic" to "Sampleroomtopic"
+ And I press "Save and display"
+ And I navigate to "Settings" in current page administration
+ And I click on "Communication" "link"
+ Then the field "id_communicationroomname" matches value "Sampleroomname"
+ And I press "Cancel"
+ And I run all adhoc tasks
+ And I navigate to "Settings" in current page administration
+ And I click on "Communication" "link"
+ And the field "id_matrixroomtopic" matches value "Sampleroomtopic"
diff --git a/communication/provider/matrix/tests/communication_feature_test.php b/communication/provider/matrix/tests/communication_feature_test.php
index 56d03b03df4..7805892ecb0 100644
--- a/communication/provider/matrix/tests/communication_feature_test.php
+++ b/communication/provider/matrix/tests/communication_feature_test.php
@@ -321,4 +321,27 @@ class communication_feature_test extends \advanced_testcase {
$this->assertFalse($communicationprocessor->get_room_provider()->check_room_membership($matrixuserid));
}
+ /**
+ * Test save form data options.
+ *
+ * @covers ::save_form_data
+ */
+ public function test_save_form_data(): void {
+ $this->resetAfterTest();
+ $course = $this->get_course();
+
+ $communicationprocessor = processor::load_by_instance(
+ 'core_course',
+ 'coursecommunication',
+ $course->id
+ );
+
+ $course->matrixroomtopic = 'Sampletopicupdated';
+ $communicationprocessor->get_form_provider()->save_form_data($course);
+
+ // Test the updated topic.
+ $matrixroomdata = new matrix_rooms($communicationprocessor->get_id());
+ $this->assertEquals('Sampletopicupdated', $matrixroomdata->get_matrix_room_topic());
+ }
+
}
diff --git a/communication/provider/matrix/tests/matrix_communication_test.php b/communication/provider/matrix/tests/matrix_communication_test.php
index 0714ba11877..d2e3e699565 100644
--- a/communication/provider/matrix/tests/matrix_communication_test.php
+++ b/communication/provider/matrix/tests/matrix_communication_test.php
@@ -916,4 +916,30 @@ class matrix_communication_test extends \advanced_testcase {
$notifications = \core\notification::fetch();
$this->assertStringContainsString('Your Matrix room is ready!', $notifications[0]->get_message());
}
+
+ /**
+ * Test set provider data from handler.
+ *
+ * @covers ::set_data
+ */
+ public function test_set_provider_data(): void {
+ $this->resetAfterTest();
+ $course = $this->get_course();
+ $communication = \core_communication\api::load_by_instance(
+ 'core_course',
+ 'coursecommunication',
+ $course->id
+ );
+
+ // Sample data.
+ $roomname = 'Sampleroom';
+ $provider = 'communication_matrix';
+
+ // Set the data.
+ $communication->set_data($course);
+
+ // Test the set data.
+ $this->assertEquals($roomname, $course->communicationroomname);
+ $this->assertEquals($provider, $course->selectedcommunication);
+ }
}
diff --git a/communication/provider/matrix/tests/matrix_rooms_test.php b/communication/provider/matrix/tests/matrix_rooms_test.php
index 7e1b6e66e4a..783d6f4c7e9 100644
--- a/communication/provider/matrix/tests/matrix_rooms_test.php
+++ b/communication/provider/matrix/tests/matrix_rooms_test.php
@@ -54,6 +54,7 @@ class matrix_rooms_test extends \advanced_testcase {
$course = $this->get_course();
$sampleroomid = 'samplematrixroomid';
+ $sampleroomtopic = 'samplematrixroomtopic';
// Communication internal api call.
$communicationprocessor = processor::load_by_instance(
@@ -64,9 +65,9 @@ class matrix_rooms_test extends \advanced_testcase {
// Call matrix room object to create the matrix data.
$matrixroom = new \communication_matrix\matrix_rooms($communicationprocessor->get_id());
- $matrixroom->create_matrix_room_record(
- $communicationprocessor->get_id(),
+ $matrixroom->update_matrix_room_record(
$sampleroomid,
+ $sampleroomtopic
);
// Test the object.
@@ -92,6 +93,7 @@ class matrix_rooms_test extends \advanced_testcase {
$course = $this->get_course();
$sampleroomid = 'samplematrixroomid';
+ $sampleroomtopic = 'samplematrixroomtopic';
// Communication internal api call.
$communicationprocessor = processor::load_by_instance(
@@ -102,9 +104,9 @@ class matrix_rooms_test extends \advanced_testcase {
// Call matrix room object to create the matrix data.
$matrixroom = new \communication_matrix\matrix_rooms($communicationprocessor->get_id());
- $matrixroom->create_matrix_room_record(
- $communicationprocessor->get_id(),
+ $matrixroom->update_matrix_room_record(
$sampleroomid,
+ $sampleroomtopic
);
// Get the record from db.
@@ -118,6 +120,7 @@ class matrix_rooms_test extends \advanced_testcase {
$matrixroom->update_matrix_room_record(
$sampleroomidupdated,
+ $sampleroomtopic
);
// Test the object.
@@ -144,6 +147,7 @@ class matrix_rooms_test extends \advanced_testcase {
$course = $this->get_course();
$sampleroomid = 'samplematrixroomid';
+ $sampleroomtopic = 'samplematrixroomtopic';
// Communication internal api call.
$communicationprocessor = processor::load_by_instance(
@@ -154,9 +158,9 @@ class matrix_rooms_test extends \advanced_testcase {
// Call matrix room object to create the matrix data.
$matrixroom = new \communication_matrix\matrix_rooms($communicationprocessor->get_id());
- $matrixroom->create_matrix_room_record(
- $communicationprocessor->get_id(),
+ $matrixroom->update_matrix_room_record(
$sampleroomid,
+ $sampleroomtopic
);
// Get the record from db.
diff --git a/communication/provider/matrix/version.php b/communication/provider/matrix/version.php
index 9f66a994e08..29b2c6c8ea2 100644
--- a/communication/provider/matrix/version.php
+++ b/communication/provider/matrix/version.php
@@ -25,6 +25,6 @@
defined('MOODLE_INTERNAL') || die();
$plugin->component = 'communication_matrix';
-$plugin->version = 2023051900;
+$plugin->version = 2023060100;
$plugin->requires = 2023011300;
$plugin->maturity = MATURITY_ALPHA;