-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathblock_ai_assistant.php
394 lines (361 loc) · 15.4 KB
/
block_ai_assistant.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
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
<?php
// This file is part of Moodle - https://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <https://www.gnu.org/licenses/>.
/**
* Block ai_assistant is defined here.
*
* @package block_ai_assistant
* @copyright 2022 UIT Innovation <thibaud@yorku.ca>
* @license https://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
use block_ai_assistant\cria;
class block_ai_assistant extends block_base
{
/**
* Initializes class member variables.
*/
public function init()
{
// Needed by Moodle to differentiate between blocks.
$this->title = get_string('pluginname', 'block_ai_assistant');
}
/**
* Returns the block contents.
*
* @return stdClass The block contents.
*/
public function get_content()
{
global $OUTPUT;
global $PAGE, $DB, $USER, $CFG;
require_once($CFG->libdir . '/gradelib.php');
$config = get_config('block_ai_assistant');
$availability = cria::get_availability();
if ($availability->exception == 'success') {
if (!$course_record = $DB->get_record('block_aia_settings', array('courseid' => $this->page->course->id))) {
$record = new stdClass();
$record->courseid = $this->page->course->id;
$record->blockid = $this->instance->id;
$record->bot_name = cria::create_bot_instance($this->page->course->id);
$record->no_context_message = $config->no_context_message;
$record->subtitle = $config->subtitle;
$record->welcome_message = $config->welcome_message;
$record->lang = $config->default_language;
$record->published = 0;
$record->usermodified = $USER->id;
$record->timecreated = time();
$record->timemodified = time();
$DB->insert_record('block_aia_settings', $record);
$small_talk = cria::create_small_talk_questions($this->page->course->id);
$course_record = $DB->get_record('block_aia_settings', array('courseid' => $this->page->course->id));
}
} else {
$course_record = new stdClass();
$course_record->courseid = $this->page->course->id;
$course_record->cria_file_id = '';
$course_record->block_id = '';
$course_record->bot_name = '';
$course_record->no_context_message = '';
$course_record->subtitle = '';
$course_record->welcome_message = '';
$course_record->lang = '';
$course_record->published = 0;
}
if ($availability->exception == 'success') {
// Update $course_record->bot_api_key if empty
if (empty($course_record->bot_api_key)) {
$course_record->bot_api_key = cria::get_api_key(cria::get_bot_id($this->page->course->id));
$DB->set_field('block_aia_settings', 'bot_api_key', $course_record->bot_api_key, ['courseid' => $this->page->course->id]);
}
}
if ($this->content !== null) {
return $this->content;
}
if (empty($this->instance)) {
$this->content = '';
return $this->content;
}
$this->content = new stdClass();
$this->content->items = array();
$this->content->icons = array();
$this->content->footer = '';
$PAGE->requires->js_call_amd('block_ai_assistant/delete_file', 'init');
$PAGE->requires->js_call_amd('block_ai_assistant/publish_to_students', 'init');
$PAGE->requires->js_call_amd('block_ai_assistant/course_modules', 'init');
$PAGE->requires->js_call_amd('block_ai_assistant/training_status', 'init');
$PAGE->requires->js_call_amd('block_ai_assistant/delete_question', 'init');
$PAGE->requires->css(new moodle_url('/blocks/ai_assistant/css/styles.css'));
$course_context = \context_course::instance($this->page->course->id);
// get file from file area
$fs = get_file_storage();
//syllabus files
$syllabus_files = $fs->get_area_files(
$course_context->id,
'block_ai_assistant',
'syllabus',
$this->page->course->id
);
$bot_id = '';
if ($availability->exception == 'success') {
$bot_id = cria::get_bot_id($this->page->course->id);
}
// Set syllabus_url
$syllabus_url = '';
foreach ($syllabus_files as $file) {
if ($file->get_filename() != '.') {
$syllabus_file = moodle_url::make_pluginfile_url(
$file->get_contextid(),
$file->get_component(),
$file->get_filearea(),
$file->get_itemid(),
$file->get_filepath(),
$file->get_filename()
);
$syllabus_url = $syllabus_file->out();
}
}
//questions files
$questions_files = $fs->get_area_files(
$course_context->id,
'block_ai_assistant',
'questions',
$this->page->course->id
);
// Set questions_url
$questions_url = '';
foreach ($questions_files as $q_file) {
if (!$q_file->is_directory()) {
$question_file = moodle_url::make_pluginfile_url(
$q_file->get_contextid(),
$q_file->get_component(),
$q_file->get_filearea(),
$q_file->get_itemid(),
$q_file->get_filepath(),
$q_file->get_filename()
);
$questions_url = $question_file->out();
}
}
// Get the users grade
// Get the grade item for the course
$user_grade = 0;
$grade_item = grade_item::fetch(array('itemtype' => 'course', 'courseid' => $this->page->course->id));
if ($grade_item) {
// Get the user's grade
$grades = grade_grade::fetch_users_grades($grade_item, array($USER->id), true);
$user_grade = $grades[$USER->id]->finalgrade;
}
// Get the user's groups
$user_groups = groups_get_all_groups($this->page->course->id, $USER->id, 0, 'g.*', false);
$groups = '';
foreach ($user_groups as $group) {
$groups .= $group->name . ',';
}
// Remove trialing comma
$groups = rtrim($groups, ',');
// Check to see if user is a student
if (has_capability('block/ai_assistant:teacher', $course_context)) {
$name = get_string('teacher_and_name', 'block_ai_assistant', fullname($USER));
} else {
$name = get_string('student_and_name', 'block_ai_assistant', fullname($USER));
}
// Set payload. The payload is used to modify the prompt so that the user can get personilized information
$payload = array(
'idNumber' => $USER->idnumber,
'name' => $name,
'ip' => $_SERVER['REMOTE_ADDR'],
'grade' => $user_grade,
'groups' => $groups,
);
// get embed code data
$embed_session_data = cria::start_session(
$this->page->course->id,
$course_record->bot_api_key,
$payload) ;
$embed_code_data = '<script>' . $embed_session_data . '</script>';
$embed_code = '';
if ($availability->exception == 'success') {
if ($course_record->published == 1) {
$embed_code .= $embed_code_data;
} else {
$embed_code = '';
}
}
// Find out if there are any autotest questions uploaded
$autotest_url = '';
if ($availability->exception == 'success') {
if (!$autotest = $DB->get_records('block_aia_autotest', ['courseid' => $this->page->course->id])) {
$autotest_url = $CFG->wwwroot . '/blocks/ai_assistant/autotest_import.php?courseid=' . $this->page->course->id;
} else {
$autotest_url = $CFG->wwwroot . '/blocks/ai_assistant/autotest.php?courseid=' . $this->page->course->id;
}
}
$teacher_embed_code = '';
$training_status_id = '';
$training_status = '';
// Get training status
if ($availability->exception == 'success') {
if ($course_record->cria_file_id) {
$results = cria::get_content_training_status($course_record->cria_file_id);
$training_status_id = $results->training_status_id;
$training_status = $results->training_status;
$teacher_embed_code = $embed_code_data;
} else {
$training_status_id = 4;
$training_status = '';
}
}
// Get question files
$question_file = $DB->get_record('block_aia_question_files', array('courseid' => $this->page->course->id));
// Get training status
$question_training_status_id = '';
$question_training_status = '';
if ($availability->exception == 'success') {
if ($question_file->cria_fileid) {
$results = cria::get_content_training_status($question_file->cria_fileid);
$question_training_status_id = $results->training_status_id;
$question_training_status = $results->training_status;
$teacher_embed_code = $embed_code_data;
} else {
$question_training_status_id = 4;
$question_training_status = '';
}
}
$error_code = '';
$error_message = '';
if ($availability->exception != 'success') {
$error_code = $availability->errorcode;
$error_message = $availability->message;
}
$params = array(
'blockid' => $this->instance->id,
'courseid' => $this->page->course->id,
'cria_file_id' => $course_record->cria_file_id,
'published' => $course_record->published,
'title' => get_string('title', 'block_ai_assistant'),
'content' => 'This is the content',
'configure_settings_url' => (new \moodle_url('/blocks/ai_assistant/configure_settings.php', [
'courseid' => $this->page->course->id,
]))->out(false),
'syllabus_url' => $syllabus_url,
'questions_url' => $questions_url,
'question_id' => $question_file->id,
'embed_code' => $embed_code,
'teacher_embed_code' => $teacher_embed_code,
'autotest_url' => $autotest_url,
'embed_offset' => $config->embed_position_teacher,
'training_status' => $training_status,
'training_status_id' => $training_status_id,
'question_training_status' => $question_training_status,
'question_training_status_id' => $question_training_status_id,
'error_code' => $error_code,
'error_message' => $error_message,
'is_admin' => has_capability('block/ai_assistant:view_autotest', $course_context),
);
if (!empty($this->config->text)) {
$this->content->text = $this->config->text;
} else {
if (has_capability('block/ai_assistant:teacher', $course_context)) {
$text = $OUTPUT->render_from_template('block_ai_assistant/default', $params);
} else {
$text = $OUTPUT->render_from_template('block_ai_assistant/student', $params);
}
$this->content->text = $text;
}
return $this->content;
}
// my moodle can only have SITEID and it's redundant here, so take it away
public function applicable_formats()
{
return array(
'course-view' => true,
'site-index' => false,
'my' => false,
'mod' => false,
'tag' => false
);
}
/**
* Enables global configuration of the block in settings.php.
*
* @return bool True if the global configuration is enabled.
*/
public function has_config()
{
return true;
}
/**
* Only one block can be installed per course.
* @return false
*/
public function instance_allow_multiple()
{
return false;
}
public function instance_create()
{
global $DB, $USER;
$config = get_config('block_ai_assistant');
$availability = cria::get_availability();
if ($availability->exception == 'success') {
if (!$course_record = $DB->get_record('block_aia_settings', array('courseid' => $this->page->course->id))) {
$record = new stdClass();
$record->courseid = $this->page->course->id;
$record->blockid = $this->instance->id;
$record->bot_name = cria::create_bot_instance($this->page->course->id);
$record->no_context_message = $config->no_context_message;
$record->subtitle = $config->subtitle;
$record->welcome_message = $config->welcome_message;
$record->lang = $config->default_language;
$record->published = 0;
$record->usermodified = $USER->id;
$record->timecreated = time();
$record->timemodified = time();
$DB->insert_record('block_aia_settings', $record);
$small_talk = cria::create_small_talk_questions($this->page->course->id);
}
}
}
public function instance_delete()
{
global $COURSE, $DB;
// Get settings record
$settings = $DB->get_record('block_aia_settings', array('courseid' => $COURSE->id));
// get bot id
$bot_name = explode('-', $settings->bot_name);
$bot_id = str_replace('"', '', $bot_name[0]);
// Delete bot from Cria
$results = cria::delete_bot_instance($bot_id);
file_put_contents('/var/www/moodledata/temp/delete_cria_bot.json', json_encode($results, JSON_PRETTY_PRINT));
// Delete all settings for this course
$DB->delete_records('block_aia_settings', array('courseid' => $COURSE->id));
// Delete Autotest questions
$DB->delete_records('block_aia_autotest', array('courseid' => $COURSE->id));
// Questions
$DB->delete_records('block_aia_question_files', array('courseid' => $COURSE->id));
// Delete the files in filearea syllabus
$fs = get_file_storage();
$context = \context_course::instance($COURSE->id);
$files = $fs->get_area_files($context->id, 'block_ai_assistant', 'syllabus', $COURSE->id);
foreach ($files as $file) {
$file->delete();
}
$question_files = $fs->get_area_files($context->id, 'block_ai_assistant', 'questions', $COURSE->id);
foreach ($question_files as $question_file) {
$question_file->delete();
}
return true;
}
}