1
0
mirror of https://github.com/moodle/moodle.git synced 2025-04-24 18:04:43 +02:00

MDL-77357 communication_matrix: Implement dynamic form fields

This commit is contained in:
Safat 2023-04-12 11:25:28 +10:00
parent 982938fa04
commit 5fc1dedeae
10 changed files with 234 additions and 15 deletions

@ -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);
}
}

@ -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.
*

@ -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"/>

@ -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;
}

@ -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.';

@ -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"

@ -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());
}
}

@ -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);
}
}

@ -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.

@ -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;