weekly release 5.0dev
[moodle.git] / ai / configure.php
blobaf57ede19003186aa8950d2859f510ebc2861296
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 provider instances.
20 * @package core_ai
21 * @copyright 2024 Matt Porritt <matt.porritt@moodle.com>
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 require_once('../config.php');
27 require_login();
28 $context = context_system::instance();
29 require_capability('moodle/site:config', $context);
31 $id = optional_param('id', 0, PARAM_INT); // If we have an id we have existing settings.
32 $provider = optional_param('aiprovider', null, PARAM_PLUGIN);
33 $returnurl = optional_param('returnurl', null, PARAM_LOCALURL);
34 $title = get_string('createnewprovider', 'core_ai');
35 $data = [];
37 // Handle return URL.
38 if (empty($returnurl)) {
39 $returnurl = new moodle_url(
40 url: '/admin/settings.php',
41 params: ['section' => 'aiprovider']
43 } else {
44 $returnurl = new moodle_url($returnurl);
46 $data['returnurl'] = $returnurl;
48 if ($provider) {
49 $configs = ['aiprovider' => $provider];
50 $data['providerconfigs'] = $configs;
53 if ($id !== 0) { // If we have an id we are updating an existing provider instance.
54 $manager = \core\di::get(\core_ai\manager::class);
55 $providerrecord = $manager->get_provider_records(['id' => $id]);
56 $providerrecord = reset($providerrecord);
57 $plugin = explode('\\', $providerrecord->provider);
58 $plugin = $plugin[0];
60 $configs = json_decode($providerrecord->config, true, 512, JSON_THROW_ON_ERROR);
61 $configs['aiprovider'] = $plugin;
62 $configs['id'] = $providerrecord->id;
63 $configs['name'] = $providerrecord->name;
65 $data['providerconfigs'] = $configs;
66 $title = get_string('configureprovider', 'core_ai');
69 // Initial Page setup.
70 $PAGE->set_context($context);
71 $PAGE->set_url('/ai/configure.php', ['id' => $id]);
72 $PAGE->set_pagelayout('admin');
73 $PAGE->set_title($title);
74 $PAGE->set_heading($title);
76 // Provider instance form processing.
77 $mform = new \core_ai\form\ai_provider_form(customdata: $data);
78 if ($mform->is_cancelled()) {
79 $data = $mform->get_data();
80 if (isset($data->returnurl)) {
81 redirect($data->returnurl);
82 } else {
83 redirect($returnurl);
87 if ($data = $mform->get_data()) {
88 $data = (array)$data;
89 $manager = \core\di::get(\core_ai\manager::class);
90 $aiprovider = $data['aiprovider'];
91 $providername = $data['name'];
92 unset($data->aiprovider, $data->name, $data->id, $data->returnurl, $data->updateandreturn);
93 if ($id !== 0) {
94 $providerinstance = $manager->get_provider_instances(['id' => $id]);
95 $providerinstance = reset($providerinstance);
96 $providerinstance->name = $providername;
98 $manager->update_provider_instance(
99 provider:$providerinstance,
100 config: $data
102 \core\notification::add(
103 get_string('providerinstanceupdated', 'core_ai', $providername),
104 \core\notification::SUCCESS
106 } else {
107 $classname = $aiprovider . '\\' . 'provider';
108 $manager->create_provider_instance(
109 classname: $classname,
110 name: $providername,
111 config: $data,
113 \core\notification::add(
114 get_string('providerinstancecreated', 'core_ai', $providername),
115 \core\notification::SUCCESS
118 redirect($returnurl);
121 // Page output.
122 echo $OUTPUT->header();
124 // Add the provider instance form.
125 $mform->display();
127 // Add the per provider action settings only if we have an existing provider.
128 if ($id !== 0) {
129 echo $OUTPUT->render_from_template('core_ai/admin_action_settings', []);
130 $provider = $mform->get_customdata()['aiprovider'];
131 $tableid = $provider . '-' . $id; // This is the table id for the action settings table.
132 $actiontable = new \core_ai\table\aiprovider_action_management_table($tableid);
133 $actiontable->out();
135 echo $OUTPUT->footer();