1441 |
ariadna |
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 sms gateway for a given instance.
|
|
|
19 |
*
|
|
|
20 |
* @package core_sms
|
|
|
21 |
* @copyright 2024 Safat Shahin <safat.shahin@moodle.com>
|
|
|
22 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
23 |
*/
|
|
|
24 |
|
|
|
25 |
require_once('../config.php');
|
|
|
26 |
|
|
|
27 |
require_login();
|
|
|
28 |
$context = context_system::instance();
|
|
|
29 |
require_capability('moodle/site:config', $context);
|
|
|
30 |
|
|
|
31 |
$id = optional_param('id', null, PARAM_INT);
|
|
|
32 |
$gateway = optional_param('smsgateway', null, PARAM_PLUGIN);
|
|
|
33 |
$returnurl = optional_param('returnurl', null, PARAM_LOCALURL);
|
|
|
34 |
|
|
|
35 |
$title = get_string('createnewgateway', 'sms');
|
|
|
36 |
$data = [];
|
|
|
37 |
$urlparams = [];
|
|
|
38 |
if ($id) {
|
|
|
39 |
$urlparams['id'] = $id;
|
|
|
40 |
}
|
|
|
41 |
if ($gateway) {
|
|
|
42 |
$urlparams['gateway'] = $gateway;
|
|
|
43 |
}
|
|
|
44 |
|
|
|
45 |
if (!empty($gateway)) {
|
|
|
46 |
$configs = new stdClass();
|
|
|
47 |
$configs->smsgateway = $gateway;
|
|
|
48 |
$data = [
|
|
|
49 |
'gatewayconfigs' => $configs,
|
|
|
50 |
];
|
|
|
51 |
}
|
|
|
52 |
|
|
|
53 |
if (!empty($id)) {
|
|
|
54 |
$manager = \core\di::get(\core_sms\manager::class);
|
|
|
55 |
$gatewayrecord = $manager->get_gateway_records(['id' => $id]);
|
|
|
56 |
$gatewayrecord = reset($gatewayrecord);
|
|
|
57 |
$plugin = explode('\\', $gatewayrecord->gateway);
|
|
|
58 |
$plugin = $plugin[0];
|
|
|
59 |
$configs = json_decode($gatewayrecord->config, true, 512, JSON_THROW_ON_ERROR);
|
|
|
60 |
$configs = (object) $configs;
|
|
|
61 |
$configs->smsgateway = $plugin;
|
|
|
62 |
$configs->id = $gatewayrecord->id;
|
|
|
63 |
$configs->name = $gatewayrecord->name;
|
|
|
64 |
$data = [
|
|
|
65 |
'gatewayconfigs' => $configs,
|
|
|
66 |
];
|
|
|
67 |
|
|
|
68 |
$a = ['gateway' => $gatewayrecord->name];
|
|
|
69 |
$title = get_string('edit_sms_gateway', 'sms', $a);
|
|
|
70 |
}
|
|
|
71 |
|
|
|
72 |
$PAGE->set_context($context);
|
|
|
73 |
$PAGE->set_url('/sms/configure.php', $urlparams);
|
|
|
74 |
$PAGE->set_title($title);
|
|
|
75 |
$PAGE->set_heading($title);
|
|
|
76 |
|
|
|
77 |
if (empty($returnurl)) {
|
|
|
78 |
$returnurl = new moodle_url('/sms/sms_gateways.php');
|
|
|
79 |
} else {
|
|
|
80 |
$returnurl = new moodle_url($returnurl);
|
|
|
81 |
}
|
|
|
82 |
$data['returnurl'] = $returnurl;
|
|
|
83 |
|
|
|
84 |
$mform = new \core_sms\form\sms_gateway_form(customdata: $data);
|
|
|
85 |
|
|
|
86 |
if ($mform->is_cancelled()) {
|
|
|
87 |
$data = $mform->get_data();
|
|
|
88 |
if (isset($data->returnurl)) {
|
|
|
89 |
redirect($data->returnurl);
|
|
|
90 |
} else {
|
|
|
91 |
redirect($returnurl);
|
|
|
92 |
}
|
|
|
93 |
}
|
|
|
94 |
|
|
|
95 |
if ($data = $mform->get_data()) {
|
|
|
96 |
$manager = \core\di::get(\core_sms\manager::class);
|
|
|
97 |
$smsgateway = $data->smsgateway;
|
|
|
98 |
$gatewayname = $data->name;
|
|
|
99 |
// The $data will go into the database config column. If any data is not needed, unset it here.
|
|
|
100 |
unset($data->smsgateway, $data->name, $data->id, $data->saveandreturn, $data->returnurl);
|
|
|
101 |
if (!empty($id)) {
|
|
|
102 |
$gatewayinstance = $manager->get_gateway_instances(['id' => $id]);
|
|
|
103 |
$gatewayinstance = reset($gatewayinstance);
|
|
|
104 |
$gatewayinstance->name = $gatewayname;
|
|
|
105 |
|
|
|
106 |
$manager->update_gateway_instance($gatewayinstance, $data);
|
|
|
107 |
} else {
|
|
|
108 |
$classname = $smsgateway . '\\' . 'gateway';
|
|
|
109 |
$manager->create_gateway_instance(
|
|
|
110 |
classname: $classname,
|
|
|
111 |
name: $gatewayname,
|
|
|
112 |
enabled: true,
|
|
|
113 |
config: $data,
|
|
|
114 |
);
|
|
|
115 |
}
|
|
|
116 |
redirect($returnurl);
|
|
|
117 |
}
|
|
|
118 |
|
|
|
119 |
echo $OUTPUT->header();
|
|
|
120 |
$mform->display();
|
|
|
121 |
echo $OUTPUT->footer();
|