-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.php
104 lines (86 loc) · 2.63 KB
/
index.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
<?php
/**
* A lot of this is based on the Oauth2 issuers.php file.
*/
require_once("../../config.php");
require_once($CFG->libdir.'/adminlib.php');
require_once($CFG->libdir.'/tablelib.php');
use local_ai\api;
$PAGE->set_url('/ai/index.php');
$PAGE->set_context(context_system::instance());
$PAGE->set_pagelayout("admin");
$renderer = $PAGE->get_renderer('local_ai');
$action = optional_param('action', '', PARAM_ALPHAEXT);
// We're using pid as "id" is used to specify contextids.
$providerid = optional_param('pid', '', PARAM_RAW);
$incontextid = optional_param('contextid', null, PARAM_RAW);
//var_dump($incontextid);
$context = !empty($incontextid) ? \context::instance_by_id($incontextid) : null;
if (empty($context)) {
$strheading = get_string('pluginname', 'local_ai');
} else {
$strheading = get_string('aiprovidersin', 'local_ai', $context->get_context_name());
}
$PAGE->set_heading($strheading);
$PAGE->set_title($strheading);
$provider = null;
$mform = null;
if ($providerid) {
$provider = api::get_provider($providerid);
if (!$provider) {
throw new moodle_exception('invaliddata');
}
}
if ($action == api::ACTION_EDIT_PROVIDER) {
if ($provider) {
// Edit
$type = "openaipi";// Should store in and read from provider.
} else {
// Create new
$type = required_param('type', PARAM_RAW);
}
$mform = new \local_ai\form\openaiapiprovider(null, [
'persistent' => $provider,
'type' => $type,
'contextid' => $incontextid,
'enabled' => true,
]);
}
if ($mform && $mform->is_cancelled()) {
redirect(new moodle_url('/admin/tool/oauth2/issuers.php'));
} else if ($action == api::ACTION_EDIT_PROVIDER) {
// Handle edit.
if ($mform->is_cancelled()) {
redirect(new moodle_url('/ai/index.php'));
}
if ($data = $mform->get_data()) {
// var_dump($data);
try {
if (!empty($data->id)) {
api::update_provider($data);
} else {
api::create_provider($data);
}
redirect(new moodle_url('/local/ai/index.php'));
}
catch (moodle_exception $e) {
throw $e;
}
exit();
} else {
echo $OUTPUT->header();
$mform->display();
echo $OUTPUT->footer();
}
exit;
} else if ($action == api::ACTION_REMOVE_PROVIDER) {
// Handle remove.
} else {
// Display list of providers.
$indexpage = new \local_ai\output\index_page(
api::get_providers($incontextid)
);
echo $OUTPUT->header();
echo $renderer->render_index_page($indexpage);
echo $OUTPUT->footer();
}