forked from moodle/moodle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfigure.php
134 lines (118 loc) · 4.4 KB
/
configure.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
<?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 provider instances.
*
* @package core_ai
* @copyright 2024 Matt Porritt <matt.porritt@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
require_once('../config.php');
require_login();
$context = context_system::instance();
require_capability('moodle/site:config', $context);
$id = optional_param('id', 0, PARAM_INT); // If we have an id we have existing settings.
$provider = optional_param('aiprovider', null, PARAM_PLUGIN);
$returnurl = optional_param('returnurl', null, PARAM_LOCALURL);
$title = get_string('createnewprovider', 'core_ai');
$data = [];
// Handle return URL.
if (empty($returnurl)) {
$returnurl = new moodle_url(
url: '/admin/settings.php',
params: ['section' => 'aiprovider']
);
} else {
$returnurl = new moodle_url($returnurl);
}
$data['returnurl'] = $returnurl;
if ($provider) {
$configs = ['aiprovider' => $provider];
$data['providerconfigs'] = $configs;
}
if ($id !== 0) { // If we have an id we are updating an existing provider instance.
$manager = \core\di::get(\core_ai\manager::class);
$providerrecord = $manager->get_provider_record(['id' => $id], MUST_EXIST);
$plugin = explode('\\', $providerrecord->provider);
$plugin = $plugin[0];
$configs = json_decode($providerrecord->config, true, 512, JSON_THROW_ON_ERROR);
$configs['aiprovider'] = $plugin;
$configs['id'] = $providerrecord->id;
$configs['name'] = $providerrecord->name;
$data['providerconfigs'] = $configs;
$title = get_string('configureprovider', 'core_ai');
}
// Initial Page setup.
$PAGE->set_context($context);
$PAGE->set_url('/ai/configure.php', ['id' => $id]);
$PAGE->set_pagelayout('admin');
$PAGE->set_title($title);
$PAGE->set_heading($title);
// Provider instance form processing.
$mform = new \core_ai\form\ai_provider_form(customdata: $data);
if ($mform->is_cancelled()) {
$data = $mform->get_data();
if (isset($data->returnurl)) {
redirect($data->returnurl);
} else {
redirect($returnurl);
}
}
if ($data = $mform->get_data()) {
$data = (array)$data;
$manager = \core\di::get(\core_ai\manager::class);
$aiprovider = $data['aiprovider'];
$providername = $data['name'];
unset($data->aiprovider, $data->name, $data->id, $data->returnurl, $data->updateandreturn);
if ($id !== 0) {
$providerinstance = $manager->get_provider_instances(['id' => $id]);
$providerinstance = reset($providerinstance);
$providerinstance->name = $providername;
$manager->update_provider_instance(
provider:$providerinstance,
config: $data
);
\core\notification::add(
get_string('providerinstanceupdated', 'core_ai', $providername),
\core\notification::SUCCESS
);
} else {
$classname = $aiprovider . '\\' . 'provider';
$manager->create_provider_instance(
classname: $classname,
name: $providername,
config: $data,
);
\core\notification::add(
get_string('providerinstancecreated', 'core_ai', $providername),
\core\notification::SUCCESS
);
}
redirect($returnurl);
}
// Page output.
echo $OUTPUT->header();
// Add the provider instance form.
$mform->display();
// Add the per provider action settings only if we have an existing provider.
if ($id !== 0) {
echo $OUTPUT->render_from_template('core_ai/admin_action_settings', []);
$provider = $mform->get_customdata()['aiprovider'];
$tableid = $provider . '-' . $id; // This is the table id for the action settings table.
$actiontable = new \core_ai\table\aiprovider_action_management_table($tableid);
$actiontable->out();
}
echo $OUTPUT->footer();