-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi_test_web.php
More file actions
219 lines (200 loc) · 8.31 KB
/
api_test_web.php
File metadata and controls
219 lines (200 loc) · 8.31 KB
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
<?php
// This file is part of Moodle - http://moodle.org/
/**
* Web-based API test script for Chatbot block.
*
* @package block_chatbo
* @copyright 2025 Your Name <your.email@example.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
// This is a test file that should only be accessible to admins in development environments
// Check for admin rights before allowing access
require_once('../../config.php');
require_login();
require_capability('moodle/site:config', context_system::instance());
// Security check for admin access - development mode is no longer required
// Comment this line out if you want to enforce development mode
// if (!debugging('', DEBUG_DEVELOPER)) {
// die('This script can only be run in development mode with debugging enabled.');
// }
// Include API client class
require_once($CFG->dirroot . '/blocks/chatbot/classes/api_client.php');
// Get API keys from Moodle config
$config = get_config('block_chatbot');
$openai_key = !empty($config->openai_apikey) ? $config->openai_apikey : '';
$google_key = !empty($config->google_apikey) ? $config->google_apikey : '';
// Set page layou
$PAGE->set_url(new moodle_url('/blocks/chatbot/api_test_web.php'));
$PAGE->set_context(context_system::instance());
$PAGE->set_pagelayout('admin');
$PAGE->set_title('Chatbot API Test');
$PAGE->set_heading('Chatbot API Test');
echo $OUTPUT->header();
// Display test interface
echo '<div class="container">
<div class="row mb-4">
<div class="col">
<div class="alert alert-info">
<h4>API Test Information</h4>
<p>This page tests the API clients for different AI providers. It sends a simple query to each configured API provider and displays the response.</p>
<p><strong>Note:</strong> This test uses real API calls and may incur costs with your API provider.</p>
</div>
</div>
</div>
<div class="row mb-4">
<div class="col">
<div class="card">
<div class="card-header">
<h3 class="m-0">API Key Status</h3>
</div>
<div class="card-body">
<div class="mb-3">
<strong>OpenAI API Key:</strong>
<span class="badge ' . (empty($openai_key) ? 'badge-danger' : 'badge-success') . '">
' . (empty($openai_key) ? 'Not Configured' : 'Configured') . '
</span>
</div>
<div>
<strong>Google API Key:</strong>
<span class="badge ' . (empty($google_key) ? 'badge-danger' : 'badge-success') . '">
' . (empty($google_key) ? 'Not Configured' : 'Configured') . '
</span>
</div>
</div>
</div>
</div>
</div>';
// Test OpenAI API if key is configured
if (!empty($openai_key)) {
echo '<div class="row mb-4">
<div class="col">
<div class="card" id="openai-test">
<div class="card-header bg-primary text-white">
<h3 class="m-0">Testing OpenAI API</h3>
</div>
<div class="card-body">
<div class="progress mb-3">
<div class="progress-bar progress-bar-striped progress-bar-animated" style="width: 100%"></div>
</div>
<p>Sending test request to OpenAI API...</p>
<div id="openai-result">
<div class="alert alert-info">Waiting for response...</div>
</div>
</div>
</div>
</div>
</div>';
}
// Test Google API if key is configured
if (!empty($google_key)) {
echo '<div class="row mb-4">
<div class="col">
<div class="card" id="google-test">
<div class="card-header bg-primary text-white">
<h3 class="m-0">Testing Google Gemini API</h3>
</div>
<div class="card-body">
<div class="progress mb-3">
<div class="progress-bar progress-bar-striped progress-bar-animated" style="width: 100%"></div>
</div>
<p>Sending test request to Google Gemini API...</p>
<div id="google-result">
<div class="alert alert-info">Waiting for response...</div>
</div>
</div>
</div>
</div>
</div>';
}
// Display troubleshooting section
echo '<div class="row">
<div class="col">
<div class="card">
<div class="card-header">
<h3 class="m-0">Troubleshooting</h3>
</div>
<div class="card-body">
<p>If you see errors, check:</p>
<ul>
<li>API keys are correctly configured in the plugin settings</li>
<li>Network connectivity to the API endpoints</li>
<li>PHP cURL extension is enabled and working properly</li>
<li>Error logs for more detailed information</li>
</ul>
<p>For more details, you can also run the command-line test script:</p>
<pre>php blocks/chatbot/api_test.php</pre>
</div>
</div>
</div>
</div>
</div>';
// Add JavaScript for AJAX calls
echo '<script>
document.addEventListener("DOMContentLoaded", function() {
// Test OpenAI API
' . (!empty($openai_key) ? 'testProvider("openai");' : '') . '
// Test Google API
' . (!empty($google_key) ? 'testProvider("google");' : '') . '
function testProvider(provider) {
fetch("api_test_ajax.php?provider=" + provider, {
method: "POST",
headers: {
"Content-Type": "application/json",
"X-Requested-With": "XMLHttpRequest"
}
})
.then(response => response.json())
.then(data => {
const resultDiv = document.getElementById(provider + "-result");
const cardDiv = document.getElementById(provider + "-test");
const cardHeader = cardDiv.querySelector(".card-header");
// Remove progress bar
cardDiv.querySelector(".progress").remove();
if (data.success) {
cardHeader.className = "card-header bg-success text-white";
resultDiv.innerHTML = `
<div class="alert alert-success">
<strong>Test successful!</strong>
</div>
<div class="card">
<div class="card-header">Response:</div>
<div class="card-body">
<p>${data.message}</p>
</div>
</div>`;
} else {
cardHeader.className = "card-header bg-danger text-white";
resultDiv.innerHTML = `
<div class="alert alert-danger">
<strong>Test failed:</strong>
</div>
<div class="card">
<div class="card-header">Error:</div>
<div class="card-body">
<p>${data.error}</p>
</div>
</div>`;
}
})
.catch(error => {
const resultDiv = document.getElementById(provider + "-result");
const cardDiv = document.getElementById(provider + "-test");
const cardHeader = cardDiv.querySelector(".card-header");
// Remove progress bar
cardDiv.querySelector(".progress").remove();
cardHeader.className = "card-header bg-danger text-white";
resultDiv.innerHTML = `
<div class="alert alert-danger">
<strong>Request failed:</strong>
</div>
<div class="card">
<div class="card-header">Error:</div>
<div class="card-body">
<p>${error}</p>
</div>
</div>`;
});
}
});
</script>';
echo $OUTPUT->footer();