Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
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 provider instances.
19
 *
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
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', 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 = [];
36
 
37
// Handle return URL.
38
if (empty($returnurl)) {
39
    $returnurl = new moodle_url(
40
        url: '/admin/settings.php',
41
        params: ['section' => 'aiprovider']
42
    );
43
} else {
44
    $returnurl = new moodle_url($returnurl);
45
}
46
$data['returnurl'] = $returnurl;
47
 
48
if ($provider) {
49
    $configs = ['aiprovider' => $provider];
50
    $data['providerconfigs'] = $configs;
51
}
52
 
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_record(['id' => $id], MUST_EXIST);
56
    $plugin = explode('\\', $providerrecord->provider);
57
    $plugin = $plugin[0];
58
 
59
    $configs = json_decode($providerrecord->config, true, 512, JSON_THROW_ON_ERROR);
60
    $configs['aiprovider'] = $plugin;
61
    $configs['id'] = $providerrecord->id;
62
    $configs['name'] = $providerrecord->name;
63
 
64
    $data['providerconfigs'] = $configs;
65
    $title = get_string('configureprovider', 'core_ai');
66
}
67
 
68
// Initial Page setup.
69
$PAGE->set_context($context);
70
$PAGE->set_url('/ai/configure.php', ['id' => $id]);
71
$PAGE->set_pagelayout('admin');
72
$PAGE->set_title($title);
73
$PAGE->set_heading($title);
74
 
75
// Provider instance form processing.
76
$mform = new \core_ai\form\ai_provider_form(customdata: $data);
77
if ($mform->is_cancelled()) {
78
    $data = $mform->get_data();
79
    if (isset($data->returnurl)) {
80
        redirect($data->returnurl);
81
    } else {
82
        redirect($returnurl);
83
    }
84
}
85
 
86
if ($data = $mform->get_data()) {
87
    $data = (array)$data;
88
    $manager = \core\di::get(\core_ai\manager::class);
89
    $aiprovider = $data['aiprovider'];
90
    $providername = $data['name'];
91
    unset($data->aiprovider, $data->name, $data->id, $data->returnurl, $data->updateandreturn);
92
    if ($id !== 0) {
93
        $providerinstance = $manager->get_provider_instances(['id' => $id]);
94
        $providerinstance = reset($providerinstance);
95
        $providerinstance->name = $providername;
96
 
97
        $manager->update_provider_instance(
98
            provider:$providerinstance,
99
            config: $data
100
        );
101
        \core\notification::add(
102
            get_string('providerinstanceupdated', 'core_ai', $providername),
103
            \core\notification::SUCCESS
104
        );
105
    } else {
106
        $classname = $aiprovider . '\\' . 'provider';
107
        $manager->create_provider_instance(
108
            classname: $classname,
109
            name: $providername,
110
            config: $data,
111
        );
112
        \core\notification::add(
113
            get_string('providerinstancecreated', 'core_ai', $providername),
114
            \core\notification::SUCCESS
115
        );
116
    }
117
    redirect($returnurl);
118
}
119
 
120
// Page output.
121
echo $OUTPUT->header();
122
 
123
// Add the provider instance form.
124
$mform->display();
125
 
126
// Add the per provider action settings only if we have an existing provider.
127
if ($id !== 0) {
128
    echo $OUTPUT->render_from_template('core_ai/admin_action_settings', []);
129
    $provider = $mform->get_customdata()['aiprovider'];
130
    $tableid = $provider . '-' . $id; // This is the table id for the action settings table.
131
    $actiontable = new \core_ai\table\aiprovider_action_management_table($tableid);
132
    $actiontable->out();
133
}
134
echo $OUTPUT->footer();