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 instance action settings.
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
$provider = required_param('provider', PARAM_PLUGIN);
32
$action = required_param('action', PARAM_TEXT);
33
$id = required_param('providerid', PARAM_INT);
34
$returnurl = optional_param('returnurl', null, PARAM_LOCALURL);
35
$customdata = ['providerid' => $id];
36
 
37
// Handle return URL.
38
if (empty($returnurl)) {
39
    $returnurl = new moodle_url(
40
        url: '/ai/configure.php',
41
        params: ['id' => $id]
42
    );
43
} else {
44
    $returnurl = new moodle_url($returnurl);
45
}
46
$customdata['returnurl'] = $returnurl;
47
 
48
$manager = \core\di::get(\core_ai\manager::class);
49
$providerrecord = $manager->get_provider_record(['id' => $id], MUST_EXIST);
50
 
51
$actionconfig = json_decode($providerrecord->actionconfig, true, 512, JSON_THROW_ON_ERROR);
52
$actionconfig = $actionconfig[$action];
53
 
54
$customdata['actionconfig'] = $actionconfig;
55
$customdata['providername'] = $provider;
56
 
57
$urlparams = [
58
    'provider' => $provider,
59
    'action' => $action,
60
    'id' => $id,
61
];
62
 
63
// Page setup.
64
$title = get_string('actionsettingprovider', 'core_ai', $action::get_name());
65
$PAGE->set_context($context);
66
$PAGE->set_url('/ai/configure.php_actions', $urlparams);
67
$PAGE->set_pagelayout('admin');
68
$PAGE->set_title($title);
69
$PAGE->set_heading($title);
70
 
71
$providerclass = "\\$provider\\provider";
72
$mform = $providerclass::get_action_settings($action, $customdata);
73
 
74
if ($mform->is_cancelled()) {
75
    $data = $mform->get_data();
76
    if (isset($data->returnurl)) {
77
        redirect($data->returnurl);
78
    } else {
79
        redirect($returnurl);
80
    }
81
}
82
 
83
if ($data = $mform->get_data()) {
84
    $manager = \core\di::get(\core_ai\manager::class);
85
    $aiprovider = $data->provider;
86
    unset($data->provider, $data->id, $data->action, $data->returnurl, $data->submitbutton);
87
    $providerinstance = $manager->get_provider_instances(['id' => $id]);
88
    $providerinstance = reset($providerinstance);
89
    $actionconfig = $providerinstance->actionconfig;
90
    $actionconfig[$action]['settings'] = (array)$data;
91
    $actionconfig[$action]['enabled'] = $providerinstance->actionconfig[$action]['enabled'];
92
 
93
    $manager->update_provider_instance(
94
        provider: $providerinstance,
95
        actionconfig: $actionconfig,
96
    );
97
 
98
    \core\notification::add(
99
        get_string('providerinstanceactionupdated', 'core_ai', $action::get_name()),
100
        \core\notification::SUCCESS
101
    );
102
 
103
    redirect($returnurl);
104
}
105
 
106
echo $OUTPUT->header();
107
$mform->display();
108
echo $OUTPUT->footer();