Skip to content

Commit

Permalink
MDL-77357 communication_matrix: Implement dynamic form fields
Browse files Browse the repository at this point in the history
  • Loading branch information
safatshahin committed May 31, 2023
1 parent 982938f commit 5fc1ded
Show file tree
Hide file tree
Showing 10 changed files with 234 additions and 15 deletions.
53 changes: 49 additions & 4 deletions communication/provider/matrix/classes/communication_feature.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -234,7 +234,7 @@ public function check_room_membership(string $matrixuserid): bool {
}

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.
Expand All @@ -245,12 +245,21 @@ public function create_chat_room(): bool {
'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;
Expand All @@ -274,6 +283,13 @@ public function update_chat_room(): bool {
$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();

Expand Down Expand Up @@ -313,4 +329,33 @@ public function get_chat_room_url(): ?string {

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);
}
}
28 changes: 24 additions & 4 deletions communication/provider/matrix/classes/matrix_rooms.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,26 +56,34 @@ public function load_matrix_room_data(int $commid): void {
* 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;
}

/**
* 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);
}
}
Expand Down Expand Up @@ -105,6 +113,18 @@ public function get_matrix_room_id(): ?string {
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.
*
Expand Down
1 change: 1 addition & 0 deletions communication/provider/matrix/db/install.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
<FIELD NAME="id" TYPE="int" LENGTH="10" NOTNULL="true" SEQUENCE="true"/>
<FIELD NAME="commid" TYPE="int" LENGTH="10" NOTNULL="true" SEQUENCE="false" COMMENT="ID of the communication record"/>
<FIELD NAME="roomid" TYPE="char" LENGTH="255" NOTNULL="false" SEQUENCE="false" COMMENT="ID of the matrix room instance"/>
<FIELD NAME="topic" TYPE="char" LENGTH="255" NOTNULL="false" SEQUENCE="false" COMMENT="Topic of the matrix room instance."/>
</FIELDS>
<KEYS>
<KEY NAME="primary" TYPE="primary" FIELDS="id"/>
Expand Down
45 changes: 45 additions & 0 deletions communication/provider/matrix/db/upgrade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?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/>.

/**
* Install steps for communication_matrix.
*
* @package communication_matrix
* @copyright 2023 Safat Shahin <safat.shahin@moodle.com>
* @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;

}
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,7 @@
$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.';
Original file line number Diff line number Diff line change
@@ -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"
23 changes: 23 additions & 0 deletions communication/provider/matrix/tests/communication_feature_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -321,4 +321,27 @@ public function test_add_and_remove_members_from_room(): void {
$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());
}

}
26 changes: 26 additions & 0 deletions communication/provider/matrix/tests/matrix_communication_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -916,4 +916,30 @@ public function test_show_communication_room_status_notification(): void {
$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);
}
}
16 changes: 10 additions & 6 deletions communication/provider/matrix/tests/matrix_rooms_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ public function test_create_matrix_room_record(): void {
$course = $this->get_course();

$sampleroomid = 'samplematrixroomid';
$sampleroomtopic = 'samplematrixroomtopic';

// Communication internal api call.
$communicationprocessor = processor::load_by_instance(
Expand All @@ -64,9 +65,9 @@ public function test_create_matrix_room_record(): void {

// 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.
Expand All @@ -92,6 +93,7 @@ public function test_update_matrix_room_record(): void {
$course = $this->get_course();

$sampleroomid = 'samplematrixroomid';
$sampleroomtopic = 'samplematrixroomtopic';

// Communication internal api call.
$communicationprocessor = processor::load_by_instance(
Expand All @@ -102,9 +104,9 @@ public function test_update_matrix_room_record(): void {

// 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.
Expand All @@ -118,6 +120,7 @@ public function test_update_matrix_room_record(): void {

$matrixroom->update_matrix_room_record(
$sampleroomidupdated,
$sampleroomtopic
);

// Test the object.
Expand All @@ -144,6 +147,7 @@ public function test_delete_matrix_room_record(): void {
$course = $this->get_course();

$sampleroomid = 'samplematrixroomid';
$sampleroomtopic = 'samplematrixroomtopic';

// Communication internal api call.
$communicationprocessor = processor::load_by_instance(
Expand All @@ -154,9 +158,9 @@ public function test_delete_matrix_room_record(): void {

// 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.
Expand Down
2 changes: 1 addition & 1 deletion communication/provider/matrix/version.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

0 comments on commit 5fc1ded

Please sign in to comment.