-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.php
133 lines (103 loc) · 4.52 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
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
<?php
// This file is part of Moodle - http://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 <http://www.gnu.org/licenses/>.
/**
* Index file of the GenAI question generation plugin.
*
* @package qbank_genai
* @copyright 2023 Christian Grévisse <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
require('../../../config.php');
require_once($CFG->dirroot.'/question/bank/genai/lib.php');
$courseid = required_param('courseid', PARAM_INT);
$url = new moodle_url('/question/bank/genai/index.php', ['courseid' => $courseid]);
$PAGE->set_url($url);
$course = get_course($courseid);
require_login($course);
$course = course_get_format($course)->get_course();
$context = context_course::instance($course->id);
$PAGE->set_context($context);
require_all_capabilities(qbank_genai_required_capabilities(), $context);
$PAGE->set_pagelayout('standard');
$PAGE->set_title(get_string('title', 'qbank_genai'));
$PAGE->set_heading(format_string($course->fullname));
echo $OUTPUT->header();
// Print tertiary navigation.
$renderer = $PAGE->get_renderer('core_question', 'bank');
$qbankaction = new \core_question\output\qbank_action_menu($url);
echo $renderer->render($qbankaction);
echo $OUTPUT->heading(get_string('title', 'qbank_genai'));
// Check for OpenAI API key.
$openaiapikey = qbank_genai_get_openai_apikey($course->id);
if (empty($openaiapikey)) {
echo html_writer::tag('div', get_string('noopenaiapikey', 'qbank_genai'), ['class' => 'alert alert-warning']);
}
// Show ongoing generation tasks (if any).
$existingtasks = $DB->get_records('task_adhoc', ['userid' => $USER->id, 'component' => 'qbank_genai']);
if (!empty($existingtasks)) {
echo html_writer::start_tag('div', ['class' => 'alert alert-info']);
echo html_writer::tag('p', get_string('ongoingtasks', 'qbank_genai'));
echo html_writer::start_tag('ul');
foreach ($existingtasks as $task) {
echo html_writer::start_tag('li');
$resourcenames = qbank_genai_get_resource_names_string(json_decode($task->customdata)->resources);
echo html_writer::tag('span', format_text($resourcenames, FORMAT_PLAIN));
echo html_writer::tag('small', userdate($task->timecreated), ['class' => 'text-muted ml-2']);
}
echo html_writer::end_tag('li');
echo html_writer::end_tag('ul');
echo html_writer::end_tag('div');
}
// Get course resources.
$resources = qbank_genai_get_course_resources($course);
if (count($resources) == 0) {
echo html_writer::start_tag('div', ['class' => 'alert alert-warning']);
echo html_writer::tag('p', get_string('noresources', 'qbank_genai'));
echo html_writer::end_tag('div');
} else {
// Form handling.
$mform = new \qbank_genai\form\generation_form($url, $resources);
if ($fromform = $mform->get_data()) {
$ids = [];
foreach ($fromform->resource as $id => $selected) {
if (boolval($selected)) {
$ids[] = $id;
}
}
$selectedresources = [];
foreach ($resources as $resource) {
if (in_array($resource->id, $ids)) {
$selectedresources[] = (object) [
"id" => $resource->id,
"name" => $resource->name,
"visible" => $resource->visible,
];
}
}
// Launch generation task.
$task = \qbank_genai\task\generation_task::instance($selectedresources, $USER->id, $context->id, $course->id);
\core\task\manager::queue_adhoc_task($task); // Add true to avoid duplicates.
// Log generation task launched.
$event = \qbank_genai\event\generation_launched::create(['context' => $context, 'other' => ["ids" => $ids]]);
$event->trigger();
// Redirect to this page again (seems to interfere with mtrace in adhoc task ...).
// Also, consider redirecting before any $OUTPUT->...
redirect($PAGE->url);
} else {
$mform->display();
}
}
echo $OUTPUT->footer();