mirrored from git://git.moodle.org/moodle.git
-
Notifications
You must be signed in to change notification settings - Fork 6.4k
/
configure_form.php
140 lines (125 loc) · 5.04 KB
/
configure_form.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
<?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;
use core\context;
use stdClass;
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;
/**
* Class constructor
*
* @param context $context Context 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)
* @param stdClass|null $instancedata Instance data
*/
public function __construct(
context $context,
?int $instanceid = null,
?string $instancetype = null,
?string $component = null,
?string $selectedcommunication = null,
?stdClass $instancedata = null,
) {
parent::__construct(
null,
[
'context' => $context,
'instanceid' => $instanceid,
'instancetype' => $instancetype,
'component' => $component,
'selectedcommunication' => $selectedcommunication,
'instancedata' => $instancedata,
],
);
}
/**
* Defines the form fields.
*/
public function definition() {
$mform = $this->_form;
$context = $this->_customdata['context'];
$instanceid = $this->_customdata['instanceid'];
$instancetype = $this->_customdata['instancetype'];
$component = $this->_customdata['component'];
$instancedata = $this->_customdata['instancedata'];
// Add communication plugins to the form.
$this->communication = \core_communication\api::load_by_instance(
context: $context,
component: $component,
instancetype: $instancetype,
instanceid: $instanceid,
provider: $this->_customdata['selectedcommunication'],
);
$this->communication->form_definition($mform);
$this->communication->set_data($instancedata);
$this->set_form_definition_for_provider();
// 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', 'contextid', $context->id);
$mform->setType('contextid', PARAM_INT);
$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($instancedata);
}
/**
* Defines the requested/current provider
*
* Get the selected communication service (provider),
* and then use it to show the provider form fields.
*/
private function set_form_definition_for_provider(): void {
$instancedata = $this->_customdata['instancedata'];
if ($selectedcommunication = $this->_customdata['selectedcommunication']) {
// First is to check whether the selected communication was selected from the form.
$provider = $selectedcommunication;
} else if (isset($instancedata->selectedcommunication)) {
// If the form is not yet submitted, get the value from the DB.
$provider = $instancedata->selectedcommunication;
} else {
// Otherwise, set to PROVIDER_NONE.
$provider = \core_communication\processor::PROVIDER_NONE;
}
$this->communication->form_definition_for_provider($this->_form, $provider);
}
}