diff --git a/communication/classes/api.php b/communication/classes/api.php index db3f454bec7..e7ee4c7fe96 100644 --- a/communication/classes/api.php +++ b/communication/classes/api.php @@ -229,13 +229,11 @@ class api { /** * Set the form definitions for the plugins. * - * @param \MoodleQuickForm $mform - * @return void + * @param \MoodleQuickForm $mform The moodle form + * @param string $provider The provider name */ - public function form_definition_for_provider(\MoodleQuickForm $mform): void { - $provider = $mform->getElementValue('selectedcommunication'); - - if ($provider[0] !== processor::PROVIDER_NONE) { + public function form_definition_for_provider(\MoodleQuickForm $mform, string $provider = processor::PROVIDER_NONE): void { + if ($provider !== processor::PROVIDER_NONE) { // Room name for the communication provider. $mform->insertElementBefore( $mform->createElement( @@ -247,7 +245,7 @@ class api { $mform->addHelpButton('communicationroomname', 'communicationroomname', 'communication'); $mform->setType('communicationroomname', PARAM_TEXT); - processor::set_proider_form_definition($provider[0], $mform); + processor::set_provider_specific_form_definition($provider, $mform); } } diff --git a/communication/classes/form/configure_form.php b/communication/classes/form/configure_form.php index e3cc7f6a4e7..6b6fa251483 100644 --- a/communication/classes/form/configure_form.php +++ b/communication/classes/form/configure_form.php @@ -38,6 +38,34 @@ class configure_form extends \moodleform { */ protected $communication; + /** + * Class constructor + * + * @param \stdClass $course Course object + * @param int|null $instanceid Instance ID + * @param string|null $instancetype Instance type + * @param string|null $component Component name + * @param string|null $selectedcommunication Selected communication service (provider) + */ + public function __construct( + \stdClass $course, + ?int $instanceid = null, + ?string $instancetype = null, + ?string $component = null, + ?string $selectedcommunication = null, + ) { + parent::__construct( + null, + [ + 'instance' => $course, + 'instanceid' => $instanceid, + 'instancetype' => $instancetype, + 'component' => $component, + 'selectedcommunication' => $selectedcommunication, + ], + ); + } + /** * Defines the form fields. */ @@ -57,6 +85,8 @@ class configure_form extends \moodleform { $this->communication->form_definition($mform); $this->communication->set_data($instance); + $this->set_form_definition_for_provider(); + // Form buttons. $buttonarray = []; $buttonarray[] = $mform->createElement('submit', 'saveandreturn', get_string('savechanges')); @@ -77,11 +107,24 @@ class configure_form extends \moodleform { } /** - * Fill in the communication page data depending on provider selected. + * Defines the requested/current provider + * + * Get the selected communication service (provider), + * and then use it to show the provider form fields. */ - 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); + private function set_form_definition_for_provider(): void { + $instance = $this->_customdata['instance']; + if ($selectedcommunication = $this->_customdata['selectedcommunication']) { + // First is to check whether the selected communication was selected from the form. + $provider = $selectedcommunication; + } else if (isset($instance->selectedcommunication)) { + // If the form is not yet submitted, get the value from the DB. + $provider = $instance->selectedcommunication; + } else { + // Otherwise, set to PROVIDER_NONE. + $provider = \core_communication\processor::PROVIDER_NONE; + } + + $this->communication->form_definition_for_provider($this->_form, $provider); } } diff --git a/communication/classes/processor.php b/communication/classes/processor.php index 82f40965302..40b96ba24be 100644 --- a/communication/classes/processor.php +++ b/communication/classes/processor.php @@ -462,12 +462,12 @@ class processor { } /** - * Get communication provider for form feature. + * Set provider specific form definition. * * @param string $provider The provider name * @param \MoodleQuickForm $mform The moodle form */ - public static function set_proider_form_definition(string $provider, \MoodleQuickForm $mform): void { + public static function set_provider_specific_form_definition(string $provider, \MoodleQuickForm $mform): void { $providerclass = "{$provider}\\communication_feature"; $providerclass::set_form_definition($mform); } diff --git a/communication/configure.php b/communication/configure.php index 36724e082f8..dd3ce0f987f 100644 --- a/communication/configure.php +++ b/communication/configure.php @@ -30,6 +30,7 @@ require_login(); $instanceid = required_param('instanceid', PARAM_INT); $instancetype = required_param('instancetype', PARAM_TEXT); $component = required_param('component', PARAM_COMPONENT); +$selectedcommunication = optional_param('selectedcommunication', null, PARAM_PLUGIN); $instanceinfo = [ 'instanceid' => $instanceid, @@ -66,8 +67,16 @@ $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); +$form = new \core_communication\form\configure_form( + course: $instanceinfo['instance'], + instanceid: $instanceinfo['instanceid'], + instancetype: $instanceinfo['instancetype'], + component: $instanceinfo['component'], + selectedcommunication: $selectedcommunication +); + if ($form->is_cancelled()) {