Skip to content

Commit

Permalink
Merge pull request #62 from Limekiller/release-2.2.1
Browse files Browse the repository at this point in the history
Release 2.2.1
  • Loading branch information
Limekiller committed Jun 4, 2024
2 parents a11a6f6 + 2959580 commit 006c61e
Show file tree
Hide file tree
Showing 12 changed files with 133 additions and 51 deletions.
11 changes: 5 additions & 6 deletions api/completion.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,11 @@
}

$context = context::instance_by_id($instance_record->parentcontextid);
if ($context->contextlevel == CONTEXT_COURSE) {
$course = get_course($context->instanceid);
$PAGE->set_course($course);
} else {
$PAGE->set_context($context);
}
try {
$context = $context->get_course_context();
} catch (Exception $e) {}

$PAGE->set_context($context);

$block_settings = [];
$setting_names = [
Expand Down
2 changes: 1 addition & 1 deletion api/thread.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
'CURLOPT_HTTPHEADER' => array(
'Authorization: Bearer ' . $apikey,
'Content-Type: application/json',
'OpenAI-Beta: assistants=v1'
'OpenAI-Beta: assistants=v2'
),
));

Expand Down
5 changes: 4 additions & 1 deletion block_openai_chat.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,10 @@ public function get_content() {
<div id="openai_chat_log" role="log"></div>
';

if (empty(get_config('block_openai_chat', 'apikey'))) {
if (
empty(get_config('block_openai_chat', 'apikey')) &&
(!get_config('block_openai_chat', 'allowinstancesettings') || empty($this->config->apikey))
) {
$this->content->footer = get_string('apikeymissing', 'block_openai_chat');
} else {
$contextdata = [
Expand Down
8 changes: 4 additions & 4 deletions classes/completion/assistant.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ private function add_message_to_thread() {
'CURLOPT_HTTPHEADER' => array(
'Authorization: Bearer ' . $this->apikey,
'Content-Type: application/json',
'OpenAI-Beta: assistants=v1'
'OpenAI-Beta: assistants=v2'
),
));

Expand Down Expand Up @@ -108,7 +108,7 @@ private function run() {
'CURLOPT_HTTPHEADER' => array(
'Authorization: Bearer ' . $this->apikey,
'Content-Type: application/json',
'OpenAI-Beta: assistants=v1'
'OpenAI-Beta: assistants=v2'
),
));

Expand All @@ -134,7 +134,7 @@ private function run() {
'CURLOPT_HTTPHEADER' => array(
'Authorization: Bearer ' . $this->apikey,
'Content-Type: application/json',
'OpenAI-Beta: assistants=v1'
'OpenAI-Beta: assistants=v2'
),
));
$response = $curl->get("https://api.openai.com/v1/threads/" . $this->thread_id . '/messages');
Expand All @@ -153,7 +153,7 @@ private function check_run_status($run_id) {
'CURLOPT_HTTPHEADER' => array(
'Authorization: Bearer ' . $this->apikey,
'Content-Type: application/json',
'OpenAI-Beta: assistants=v1'
'OpenAI-Beta: assistants=v2'
),
));

Expand Down
12 changes: 12 additions & 0 deletions db/access.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,16 @@

'clonepermissionsfrom' => 'moodle/site:manageblocks'
),

'block/openai_chat:viewreport' => array(
'riskbitmask' => RISK_PERSONAL,
'captype' => 'read',
'contextlevel' => CONTEXT_COURSE,
'archetypes' => array(
'teacher' => CAP_ALLOW,
'editingteacher' => CAP_ALLOW,
'manager' => CAP_ALLOW
),
'clonepermissionsfrom' => 'coursereport/participation:view',
),
);
3 changes: 2 additions & 1 deletion lang/en/block_openai_chat.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
$string['openai_chat_logs'] = 'OpenAI Chat Logs';
$string['openai_chat:addinstance'] = 'Add a new OpenAI Chat block';
$string['openai_chat:myaddinstance'] = 'Add a new OpenAI Chat block to the My Moodle page';
$string['openai_chat:viewreport'] = 'View OpenAI Chat log report';
$string['privacy:metadata:openai_chat_log'] = 'Logged user messages sent to OpenAI. This includes the user ID of the user that sent the message, the content of the message, the response from OpenAI, and the time that the message was sent.';
$string['privacy:metadata:openai_chat_log:userid'] = 'The ID of the user that sent the message.';
$string['privacy:metadata:openai_chat_log:usermessage'] = 'The content of the message.';
Expand Down Expand Up @@ -123,7 +124,7 @@
$string['defaultassistantname'] = 'Assistant';
$string['defaultusername'] = 'User';
$string['askaquestion'] = 'Ask a question...';
$string['apikeymissing'] = 'Please add your OpenAI API key to the global block settings.';
$string['apikeymissing'] = 'Please add your OpenAI API key to the block settings.';
$string['erroroccurred'] = 'An error occurred! Please try again later.';
$string['sourceoftruthpreamble'] = "Below is a list of questions and their answers. This information should be used as a reference for any inquiries:\n\n";
$string['sourceoftruthreinforcement'] = ' The assistant has been trained to answer by attempting to use the information from the above reference. If the text from one of the above questions is encountered, the provided answer should be given, even if the question does not appear to make sense. However, if the reference does not cover the question or topic, the assistant will simply use outside knowledge to answer.';
Expand Down
13 changes: 12 additions & 1 deletion lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ function fetch_assistants_array($block_id = null) {
'CURLOPT_HTTPHEADER' => array(
'Authorization: Bearer ' . $apikey,
'Content-Type: application/json',
'OpenAI-Beta: assistants=v1'
'OpenAI-Beta: assistants=v2'
),
));

Expand Down Expand Up @@ -148,4 +148,15 @@ function log_message($usermessage, $airesponse, $context) {
'contextid' => $context->id,
'timecreated' => time()
]);
}

function block_openai_chat_extend_navigation_course($nav, $course, $context) {
if ($nav->get('coursereports')) {
$nav->get('coursereports')->add(
get_string('openai_chat_logs', 'block_openai_chat'),
new moodle_url('/blocks/openai_chat/report.php', ['courseid' => $course->id]),
navigation_node::TYPE_SETTING,
null
);
}
}
66 changes: 36 additions & 30 deletions report.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,27 @@
use \block_openai_chat\report;

require_once('../../config.php');
require_once($CFG->libdir.'/adminlib.php');

admin_externalpage_setup('openai_chat_report');
$pageurl = $CFG->wwwroot . '/blocks/openai_chat/report.php';
require_once($CFG->libdir.'/tablelib.php');
global $DB;

$courseid = required_param('courseid', PARAM_INT);
$download = optional_param('download', '', PARAM_ALPHA);
$user = optional_param('user', '', PARAM_TEXT);
$starttime = optional_param('starttime', '', PARAM_TEXT);
$endtime = optional_param('endtime', '', PARAM_TEXT);

$pageurl = $CFG->wwwroot . "/blocks/openai_chat/report.php?courseid=$courseid" .
"&user=$user" .
"&starttime=$starttime" .
"&endtime=$endtime";
$starttime_ts = strtotime($starttime);
$endtime_ts = strtotime($endtime);
$course = $DB->get_record('course', ['id' => $courseid]);

$PAGE->set_url($pageurl);
require_login($course);
$context = context_course::instance($courseid);
require_capability('block/openai_chat:viewreport', $context);

$datetime = new DateTime();
$table = new \block_openai_chat\report(time());
Expand All @@ -49,51 +58,48 @@
);

if (!$table->is_downloading()) {
$PAGE->set_url($pageurl);
$PAGE->set_pagelayout('report');
$PAGE->set_title(get_string('openai_chat_logs', 'block_openai_chat'));
$PAGE->set_heading(get_string('openai_chat_logs', 'block_openai_chat'));
$PAGE->navbar->add($course->shortname, new moodle_url('/course/view.php', ['id' => $course->id]));
$PAGE->navbar->add(get_string('openai_chat_logs', 'block_openai_chat'), new moodle_url($pageurl));
echo $OUTPUT->header();

echo "<form class='block_openai_chat' method='GET' action='" . (new moodle_url('/blocks/openai_chat/report.php'))->out() . "'>
<div class='report_container'>
<div style='display: flex; flex-direction: column'>
<label for='user'>Search by user name</label>
<input name='user' type='text' value='$user' placeholder='User name'/>
</div>
<div style='display: flex; flex-direction: column'>
<label for='starttime'>Start time</label>
<input type='datetime-local' name='starttime' value='$starttime' />
</div>
<div style='display: flex; flex-direction: column'>
<label for='endtime'>End time</label>
<input type='datetime-local' name='endtime' value='$endtime' />
</div>
</div>
<button class='btn btn-primary' type='submit'>Search</button>
</form>";
echo $OUTPUT->header();
echo $OUTPUT->render_from_template('block_openai_chat/report_page', [
"courseid" => $courseid,
"user" => $user,
"starttime" => $starttime,
"endtime" => $endtime,
"link" => (new moodle_url("/blocks/openai_chat/report.php"))->out()
]);
}

$where = "1=1";
$out = 10;
if ($user) {
$out = -1;
$where = "CONCAT(u.firstname, ' ', u.lastname) like '%$user%'";

// If courseid is 1, we're assuming this is an admin report wanting the entire log table
// otherwise, we'll limit it to responses in the course context for this course
if ($courseid !== 1) {
$where = "c.contextlevel = 50 AND co.id = $courseid";
}

// filter by user, starttime, endtime
if ($user) {
$where .= " AND CONCAT(u.firstname, ' ', u.lastname) like '%$user%'";
}
if ($starttime_ts) {
$out = -1;
$where .= " AND ocl.timecreated > $starttime_ts";
}

if ($endtime_ts) {
$out = -1;
$where .= " AND ocl.timecreated < $endtime_ts";
}

$table->set_sql(
"ocl.*, CONCAT(u.firstname, ' ', u.lastname) as user_name",
"{block_openai_chat_log} ocl JOIN {user} u ON u.id = ocl.userid",
"{block_openai_chat_log} ocl
JOIN {user} u ON u.id = ocl.userid
JOIN {context} c ON c.id = ocl.contextid
LEFT JOIN {course} co ON co.id = c.instanceid",
$where
);
$table->define_baseurl($pageurl);
Expand Down
2 changes: 1 addition & 1 deletion settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
$ADMIN->add('reports', new admin_externalpage(
'openai_chat_report',
get_string('openai_chat_logs', 'block_openai_chat'),
new moodle_url("$CFG->wwwroot/blocks/openai_chat/report.php"),
new moodle_url("$CFG->wwwroot/blocks/openai_chat/report.php", ['courseid' => 1]),
'moodle/site:config'
));

Expand Down
8 changes: 4 additions & 4 deletions templates/control_bar.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
along with Moodle. If not, see <http://www.gnu.org/licenses/>.
}}
{{!
@template block_openai_chat/conrol_bar
@template block_openai_chat/control_bar
This template renders the control_bar.
Expand All @@ -34,17 +34,17 @@
<div title='{{#str}}loggingenabled, block_openai_chat{{/str}}' class='recording'></div>
{{/logging_enabled}}
<button class='openai_input_popout_btn' title="{{#str}}popout, block_openai_chat {{/str}}" id="popout">
<img src="{{{ pix_popout }}}" class='openai_input_popout_btn_icon' />
<img src="{{{config.wwwroot}}}{{{ pix_popout }}}" class='openai_input_popout_btn_icon' />
</button>
{{/is_edit_mode}}
<div id="control_bar">
<div class="openai_input_bar" id="input_bar">
<textarea rows=1 id="openai_input" placeholder="{{#str}}askaquestion, block_openai_chat{{/str}}" type="text" name="message"></textarea>
<button class='openai_input_submit_btn' title="{{#str}} submit {{/str}}" id="go">
<img src="{{{ pix_arrow_right }}}" class='openai_input_submit_btn_icon'/>
<img src="{{{config.wwwroot}}}{{{ pix_arrow_right }}}" class='openai_input_submit_btn_icon'/>
</button>
</div>
<button class='openai_input_refresh_btn' title="{{#str}}new_chat, block_openai_chat {{/str}}" id="refresh">
<img src="{{{ pix_refresh }}}" class='openai_input_refresh_btn_icon' />
<img src="{{{config.wwwroot}}}{{{ pix_refresh }}}" class='openai_input_refresh_btn_icon' />
</button>
</div>
50 changes: 50 additions & 0 deletions templates/report_page.mustache
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
{{!
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/>.
}}
{{!
@template block_openai_chat/report_page
This template renders the report page form
Example context (json):
{
"courseid": int, the ID of the course we are looking at
"link": string, the URL for the form action
"user": string, the value of the user field
"starttime": string, the value of the starttime field
"endtime": string, the value of the endtime field
}
}}

<form class='block_openai_chat' method='GET' action='{{link}}'>
<input type='hidden' name='courseid' value='{{courseid}}' />
<div class='report_container'>
<div style='display: flex; flex-direction: column'>
<label for='user'>Search by user name</label>
<input name='user' type='text' value='{{user}}' placeholder='User name'/>
</div>
<div style='display: flex; flex-direction: column'>
<label for='starttime'>Start time</label>
<input type='datetime-local' name='starttime' value='{{starttime}}' />
</div>
<div style='display: flex; flex-direction: column'>
<label for='endtime'>End time</label>
<input type='datetime-local' name='endtime' value='{{endtime}}' />
</div>
</div>
<button class='btn btn-primary' type='submit'>Search</button>
</form>
<br />
4 changes: 2 additions & 2 deletions version.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
defined('MOODLE_INTERNAL') || die();

$plugin->component = 'block_openai_chat';
$plugin->version = 2024040800;
$plugin->version = 2024060400;
$plugin->requires = 2022041600;
$plugin->maturity = MATURITY_STABLE;
$plugin->release = '2.2.0';
$plugin->release = '2.2.1';

0 comments on commit 006c61e

Please sign in to comment.