Skip to content

Commit

Permalink
MDL-78553 core_communication: Move settings to their own page
Browse files Browse the repository at this point in the history
  • Loading branch information
davewoloszyn committed Aug 3, 2023
1 parent 5aea409 commit 317251a
Show file tree
Hide file tree
Showing 21 changed files with 383 additions and 75 deletions.
2 changes: 1 addition & 1 deletion communication/amd/build/providerchooser.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion communication/amd/build/providerchooser.min.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 0 additions & 3 deletions communication/amd/src/providerchooser.js
Expand Up @@ -36,10 +36,7 @@ export const init = () => {
document.querySelector(Selectors.fields.selector).addEventListener('change', e => {
const form = e.target.closest('form');
const updateButton = form.querySelector(Selectors.fields.updateButton);
const fieldset = updateButton.closest('fieldset');

const url = new URL(form.action);
url.hash = fieldset.id;

form.action = url.toString();
updateButton.click();
Expand Down
2 changes: 0 additions & 2 deletions communication/classes/api.php
Expand Up @@ -148,8 +148,6 @@ public function form_definition(

$PAGE->requires->js_call_amd('core_communication/providerchooser', 'init');

$mform->addElement('header', 'communication', get_string('communication', 'communication'));

// List the communication providers.
$mform->addElement(
'select',
Expand Down
87 changes: 87 additions & 0 deletions communication/classes/form/configure_form.php
@@ -0,0 +1,87 @@
<?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/>.

/**
* Configure communication for a given instance - the form definition.
*
* @package core_communication
* @copyright 2023 David Woloszyn <david.woloszyn@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

namespace core_communication\form;

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

require_once($CFG->libdir.'/formslib.php');

/**
* Defines the configure communication form.
*/
class configure_form extends \moodleform {

/**
* @var \core_communication\api $communication The communication api object.
*/
protected $communication;

/**
* Defines the form fields.
*/
public function definition() {
$mform = $this->_form;
$instanceid = $this->_customdata['instanceid'];
$instancetype = $this->_customdata['instancetype'];
$component = $this->_customdata['component'];
$instance = $this->_customdata['instance'];

// Add communication plugins to the form.
$this->communication = \core_communication\api::load_by_instance(
$component,
$instancetype,
$instanceid
);
$this->communication->form_definition($mform);
$this->communication->set_data($instance);

// Form buttons.
$buttonarray = [];
$buttonarray[] = $mform->createElement('submit', 'saveandreturn', get_string('savechanges'));
$buttonarray[] = $mform->createElement('cancel');
$mform->addGroup($buttonarray, 'buttonar', '', [' '], false);
$mform->closeHeaderBefore('buttonar');

// Hidden elements.
$mform->addElement('hidden', 'instanceid', $instanceid);
$mform->setType('instanceid', PARAM_INT);
$mform->addElement('hidden', 'instancetype', $instancetype);
$mform->setType('instancetype', PARAM_TEXT);
$mform->addElement('hidden', 'component', $component);
$mform->setType('component', PARAM_TEXT);

// Finally set the current form data.
$this->set_data($instance);
}

/**
* Fill in the communication page data depending on provider selected.
*/
public function definition_after_data() {
$mform = $this->_form;
// Add communication plugins to the form with respect to the provider.
$this->communication->form_definition_for_provider($mform);
}
}
86 changes: 86 additions & 0 deletions communication/configure.php
@@ -0,0 +1,86 @@
<?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/>.

/**
* Configure communication for a given instance.
*
* @package core_communication
* @copyright 2023 David Woloszyn <david.woloszyn@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

require_once('../config.php');
require_once('lib.php');

require_login();

$instanceid = required_param('instanceid', PARAM_INT);
$instancetype = required_param('instancetype', PARAM_TEXT);
$component = required_param('component', PARAM_COMPONENT);

$instanceinfo = [
'instanceid' => $instanceid,
'instancetype' => $instancetype,
'component' => $component,
];

// Requires communication to be enabled.
if (!core_communication\api::is_available()) {
throw new \moodle_exception('communicationdisabled', 'communication');
}

// Attempt to load the communication instance with the provided params.
$communication = \core_communication\api::load_by_instance($component, $instancetype, $instanceid);

// No communication, no way this form can be used.
if (!$communication) {
throw new \moodle_exception('nocommunicationinstance', 'communication');
}

// Set variables according to the component callback and use them on the page.
list($instance, $context, $heading, $returnurl) = component_callback(
$component,
'get_communication_instance_data',
[$instanceid]
);

// Set up the page.
$PAGE->set_context($context);
$PAGE->set_url('/communication/configure.php', $instanceinfo);
$PAGE->set_title(get_string('communication', 'communication'));
$PAGE->set_heading($heading);
$PAGE->add_body_class('limitedwidth');

// Append the instance data before passing to form object.
$instanceinfo['instance'] = $instance;
// Get our form definitions.
$form = new \core_communication\form\configure_form(null, $instanceinfo);

if ($form->is_cancelled()) {

redirect($returnurl);

} else if ($data = $form->get_data()) {

component_callback($component, 'update_communication_instance_data', [$data]);
redirect($returnurl);
}

// Display the page contents.
echo $OUTPUT->header();
echo $OUTPUT->heading(get_string('communication', 'communication'), 2);
$form->display();
echo $OUTPUT->footer();
Expand Up @@ -19,35 +19,30 @@ Feature: Communication matrix form field
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"
When I navigate to "Communication" in current page administration
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
And I press "Save changes"
And I navigate to "Communication" in current page administration
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"
When I navigate to "Communication" in current page administration
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"
And I press "Save changes"
And I navigate to "Communication" in current page administration
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 I navigate to "Communication" in current page administration
And the field "id_matrixroomtopic" matches value "Sampleroomtopic"
Expand Up @@ -37,10 +37,11 @@ Feature: Communication matrix
And ".footer-link-communication" "css_element" should be visible

Scenario: I cannot see the matrix room link when communication provider is disabled
Given I am on the "Test course" "Course editing" page logged in as "teacher1"
Given I am on the "Test course" "Course" page logged in as "teacher1"
When I navigate to "Communication" in current page administration
And I set the following fields to these values:
| selectedcommunication | none |
And I press "Save and display"
And I press "Save changes"
And I run all adhoc tasks
And I reload the page
Then ".btn-footer-communication" "css_element" should not be visible
Expand Down
2 changes: 1 addition & 1 deletion communication/tests/api_test.php
Expand Up @@ -191,7 +191,7 @@ public function test_create_and_configure_room(): void {
*/
public function test_create_and_configure_room_without_communication_provider_selected(): void {
// Get the course by disabling communication so that we can create it manually calling the api.
$course = $this->getDataGenerator()->create_course();
$course = $this->get_course('Sampleroom', 'none');

// Test the tasks added.
$adhoctask = \core\task\manager::get_adhoc_tasks('\\core_communication\\task\\create_and_configure_room_task');
Expand Down
41 changes: 41 additions & 0 deletions communication/tests/behat/communication_configuration.feature
@@ -0,0 +1,41 @@
@communication
Feature: Access the communication configuration page
As an editing teacher
See dynamic form fields based on selected provider

Background: Set up teachers and course for the communication confifiguration page
Given I enable communication experimental feature
And the following "users" exist:
| username | firstname | lastname | email |
| teacher1 | Teacher | 1 | teacher1@example.com |
| teacher2 | Teacher | 2 | teacher2@example.com |
And the following "courses" exist:
| fullname | shortname | category | selectedcommunication |
| Test course | Test course | 0 | none |
And the following "course enrolments" exist:
| user | course | role |
| teacher1 | Test course | editingteacher |
| teacher2 | Test course | teacher |

Scenario: A teacher with the correct capability can access the communication configuration page
Given I am on the "Test course" "Course" page logged in as "teacher1"
When I navigate to "Communication" in current page administration
Then I should see "Communication"

Scenario: A teacher without the correct capability cannot access the communication configuration page
Given I am on the "Test course" "Course" page logged in as "teacher2"
Then "Communication" "link" should not exist in current page administration

Scenario: I cannot see the communication link when communication provider is disabled
Given I disable communication experimental feature
And I am on the "Test course" "Course" page logged in as "teacher1"
Then "Communication" "link" should not exist in current page administration

@javascript
Scenario: The communication form fields toggle dynamically when valid provider is set
Given I am on the "Test course" "Course" page logged in as "teacher1"
When I navigate to "Communication" in current page administration
And I set the following fields to these values:
| selectedcommunication | communication_matrix |
Then I should see "Room name"
And I should see "Room topic"
31 changes: 0 additions & 31 deletions course/edit_form.php
Expand Up @@ -401,28 +401,6 @@ function definition() {
$handler->set_parent_context($categorycontext); // For course handler only.
$handler->instance_form_definition($mform, empty($course->id) ? 0 : $course->id);

// Add communication plugins to the form.
if (core_communication\api::is_available()) {

$instanceconfig = core_communication\processor::PROVIDER_NONE;
// For new courses.
if (empty($course->id)) {
$instanceid = 0;
if (!empty($courseconfig->coursecommunicationprovider)) {
$instanceconfig = $courseconfig->coursecommunicationprovider;
}
} else {
// For existing courses.
$instanceid = $course->id;
}

$communication = \core_communication\api::load_by_instance(
'core_course',
'coursecommunication',
$instanceid);
$communication->form_definition($mform, $instanceconfig);
$communication->set_data($course);
}

// When two elements we need a group.
$buttonarray = array();
Expand Down Expand Up @@ -494,15 +472,6 @@ function definition_after_data() {
$handler = core_course\customfield\course_handler::create();
$handler->instance_form_definition_after_data($mform, empty($courseid) ? 0 : $courseid);

// Add communication plugins to the form.
if (core_communication\api::is_available()) {
$communication = \core_communication\api::load_by_instance(
'core_course',
'coursecommunication',
empty($course->id) ? 0 : $course->id
);
$communication->form_definition_for_provider($mform);
}
}

/**
Expand Down

0 comments on commit 317251a

Please sign in to comment.