Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 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.
13
//
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/>.
16
 
17
/**
18
 * Configure communication for a given instance - the form definition.
19
 *
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
23
 */
24
 
25
namespace core_communication\form;
26
 
27
use core\context;
28
use stdClass;
29
 
30
defined('MOODLE_INTERNAL') || die();
31
 
32
require_once($CFG->libdir . '/formslib.php');
33
 
34
/**
35
 * Defines the configure communication form.
36
 */
37
class configure_form extends \moodleform {
38
    /**
39
     * @var \core_communication\api $communication The communication api object.
40
     */
41
    protected $communication;
42
 
43
    /**
44
     * Class constructor
45
     *
46
     * @param context $context Context object
47
     * @param int|null $instanceid Instance ID
48
     * @param string|null $instancetype Instance type
49
     * @param string|null $component Component name
50
     * @param string|null $selectedcommunication Selected communication service (provider)
51
     * @param stdClass|null $instancedata Instance data
52
     */
53
    public function __construct(
54
        context $context,
55
        ?int $instanceid = null,
56
        ?string $instancetype = null,
57
        ?string $component = null,
58
        ?string $selectedcommunication = null,
59
        ?stdClass $instancedata = null,
60
    ) {
61
        parent::__construct(
62
            null,
63
            [
64
                'context' => $context,
65
                'instanceid' => $instanceid,
66
                'instancetype' => $instancetype,
67
                'component' => $component,
68
                'selectedcommunication' => $selectedcommunication,
69
                'instancedata' => $instancedata,
70
            ],
71
        );
72
    }
73
 
74
    /**
75
     * Defines the form fields.
76
     */
77
    public function definition() {
78
        $mform = $this->_form;
79
        $context = $this->_customdata['context'];
80
        $instanceid = $this->_customdata['instanceid'];
81
        $instancetype = $this->_customdata['instancetype'];
82
        $component = $this->_customdata['component'];
83
        $instancedata = $this->_customdata['instancedata'];
84
 
85
        // Add communication plugins to the form.
86
        $this->communication = \core_communication\api::load_by_instance(
87
            context: $context,
88
            component: $component,
89
            instancetype: $instancetype,
90
            instanceid: $instanceid,
91
            provider: $this->_customdata['selectedcommunication'],
92
        );
93
        $this->communication->form_definition($mform);
94
        $this->communication->set_data($instancedata);
95
 
96
        $this->set_form_definition_for_provider();
97
 
98
        // Form buttons.
99
        $buttonarray = [];
100
        $buttonarray[] = $mform->createElement('submit', 'saveandreturn', get_string('savechanges'));
101
        $buttonarray[] = $mform->createElement('cancel');
102
        $mform->addGroup($buttonarray, 'buttonar', '', [' '], false);
103
        $mform->closeHeaderBefore('buttonar');
104
 
105
        // Hidden elements.
106
        $mform->addElement('hidden', 'contextid', $context->id);
107
        $mform->setType('contextid', PARAM_INT);
108
        $mform->addElement('hidden', 'instanceid', $instanceid);
109
        $mform->setType('instanceid', PARAM_INT);
110
        $mform->addElement('hidden', 'instancetype', $instancetype);
111
        $mform->setType('instancetype', PARAM_TEXT);
112
        $mform->addElement('hidden', 'component', $component);
113
        $mform->setType('component', PARAM_TEXT);
114
 
115
        // Finally set the current form data.
116
        $this->set_data($instancedata);
117
    }
118
 
119
    /**
120
     * Defines the requested/current provider
121
     *
122
     * Get the selected communication service (provider),
123
     * and then use it to show the provider form fields.
124
     */
125
    private function set_form_definition_for_provider(): void {
126
        $instancedata = $this->_customdata['instancedata'];
127
        if ($selectedcommunication = $this->_customdata['selectedcommunication']) {
128
            // First is to check whether the selected communication was selected from the form.
129
            $provider = $selectedcommunication;
130
        } else if (isset($instancedata->selectedcommunication)) {
131
            // If the form is not yet submitted, get the value from the DB.
132
            $provider = $instancedata->selectedcommunication;
133
        } else {
134
            // Otherwise, set to PROVIDER_NONE.
135
            $provider = \core_communication\processor::PROVIDER_NONE;
136
        }
137
 
138
        $this->communication->form_definition_for_provider($this->_form, $provider);
139
    }
140
}