Merge branch 'MDL-81457-main' of https://github.com/andrewnicols/moodle
[moodle.git] / communication / configure.php
blobb3e16881e5bf8abb061fe0df4ba112fb039824e1
1 <?php
2 // This file is part of Moodle - http://moodle.org/
3 //
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
17 /**
18 * Configure communication for a given instance.
20 * @package core_communication
21 * @copyright 2023 David Woloszyn <david.woloszyn@moodle.com>
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 require_once('../config.php');
26 require_once('lib.php');
28 require_login();
30 $contextid = required_param('contextid', PARAM_INT);
31 $instanceid = required_param('instanceid', PARAM_INT);
32 $instancetype = required_param('instancetype', PARAM_TEXT);
33 $component = required_param('component', PARAM_COMPONENT);
34 $selectedcommunication = optional_param('selectedcommunication', null, PARAM_PLUGIN);
36 $context = \core\context::instance_by_id($contextid);
37 $instanceinfo = [
38 'contextid' => $context->id,
39 'instanceid' => $instanceid,
40 'instancetype' => $instancetype,
41 'component' => $component,
44 // Requires communication to be enabled.
45 if (!core_communication\api::is_available()) {
46 throw new \moodle_exception('communicationdisabled', 'communication');
49 // Attempt to load the communication instance with the provided params.
50 $communication = \core_communication\api::load_by_instance(
51 context: $context,
52 component: $component,
53 instancetype: $instancetype,
54 instanceid: $instanceid,
55 provider: $selectedcommunication,
58 // No communication, no way this form can be used.
59 if (!$communication) {
60 throw new \moodle_exception('nocommunicationinstance', 'communication');
63 // Set variables according to the component callback and use them on the page.
64 [$instance, $context, $heading, $returnurl] = component_callback(
65 $component,
66 'get_communication_instance_data',
67 [$instanceid]
70 // Set up the page.
71 $PAGE->set_context($context);
72 $PAGE->set_url('/communication/configure.php', $instanceinfo);
73 $PAGE->set_title(get_string('communication', 'communication'));
74 $PAGE->set_heading($heading);
75 $PAGE->add_body_class('limitedwidth');
77 // Append the instance data before passing to form object.
78 $instanceinfo['instancedata'] = $instance;
80 // Get our form definitions.
81 $form = new \core_communication\form\configure_form(
82 context: $context,
83 instanceid: $instanceinfo['instanceid'],
84 instancetype: $instanceinfo['instancetype'],
85 component: $instanceinfo['component'],
86 selectedcommunication: $selectedcommunication,
87 instancedata: $instanceinfo['instancedata'],
91 if ($form->is_cancelled()) {
92 redirect($returnurl);
93 } else if ($data = $form->get_data()) {
94 component_callback($component, 'update_communication_instance_data', [$data]);
95 redirect($returnurl);
98 // Display the page contents.
99 echo $OUTPUT->header();
100 echo $OUTPUT->heading(get_string('communication', 'communication'), 2);
101 $form->display();
102 echo $OUTPUT->footer();