Skip to content

Commit

Permalink
Convert showprompt in preview from inline js in PHP to amd module
Browse files Browse the repository at this point in the history
  • Loading branch information
marcusgreen committed May 5, 2024
1 parent fcd1888 commit 35356c6
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 21 deletions.
3 changes: 3 additions & 0 deletions amd/build/showprompt.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions amd/build/showprompt.min.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

43 changes: 43 additions & 0 deletions amd/src/showprompt.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// 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/>.

/**
* Display a button in testing to reveal the prompt that was sent
*
* @module qtype_aitext/showprompt
* @copyright 2024 Marcus Green
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
export const init = () => {
var button = document.getElementById('showprompt');
button.addEventListener('click', (event) => {
toggleFullPrompt(event);
});

/**
* Togle the visibility of the prompt that is sent to
* the AI System
* @param {*} event
*/
function toggleFullPrompt(event) {
event.preventDefault();
var text = document.getElementById("fullprompt");
if (text.className === "hidden") {
text.className = "visible";
} else {
text.className = "hidden";
}
}
};
8 changes: 4 additions & 4 deletions question.php
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ public function apply_attempt_state(question_attempt_step $step) {
* @return void
*/
public function grade_response(array $response) : array {
if(!$this->is_complete_response($response)) {
if (!$this->is_complete_response($response)) {
$grade = [0 => 0, question_state::$needsgrading];
return $grade;
}
Expand All @@ -144,7 +144,7 @@ public function grade_response(array $response) : array {
$responsetext = '[['.$responsetext.']]';
$prompt = get_config('qtype_aitext', 'prompt');
$prompt = preg_replace("/\[responsetext\]/", $responsetext, $prompt);
$prompt .= ' '.trim($this->aiprompt);
$prompt .= ' '.trim($this->aiprompt);
if ($this->markscheme > '') {
$prompt .= ' '.$this->markscheme;
} else {
Expand Down Expand Up @@ -187,10 +187,10 @@ public function grade_response(array $response) : array {
*/
public function process_feedback(string $feedback) {
if (preg_match('/\{[^{}]*\}/', $feedback, $matches)) {
// $matches[1] contains the captured text inside the braces
// Array $matches[1] contains the captured text inside the braces.
$feedback = $matches[0];
}

xdebug_break();
$contentobject = json_decode($feedback);
if (json_last_error() === JSON_ERROR_NONE) {
$contentobject->feedback = trim($contentobject->feedback);
Expand Down
19 changes: 4 additions & 15 deletions renderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,23 +124,12 @@ public function feedback(question_attempt $qa, question_display_options $options
// This probably should be retrieved by an api call.
$comment = $qa->get_current_manual_comment();
if ($this->page->pagetype == 'question-bank-previewquestion-preview') {
$this->page->requires->js_call_amd('qtype_aitext/showprompt', 'init', []);
if ($comment[0] > '') {
$prompt = $qa->get_last_qt_var('-aiprompt');
$js = '<script>
function toggleVisibility(event) {
event.preventDefault();
var text = document.getElementById("fullprompt");
if (text.className === "hidden") {
text.className = "visible";
} else {
text.className = "hidden";
}
}
</script>';
$showprompt = get_string('showprompt', 'qtype_aitext');
$js .= '<br/><button class="rounded" onclick="toggleVisibility(event)">'.$showprompt.'</button>';
$js .= '<div id="fullprompt" class="hidden">'.$prompt .'</div>';
$comment[0] = $comment[0].$js;
$showprompt = '<br/><button id=showprompt class="rounded">'. get_string('showprompt', 'qtype_aitext').'</button>';
$showprompt .= '<div id="fullprompt" class="hidden">'.$prompt .'</div>';
$comment[0] = $comment[0].$showprompt;
}
return $comment[0];
}
Expand Down
3 changes: 1 addition & 2 deletions settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,12 @@
new lang_string('defaultmarksscheme', 'qtype_aitext'),
new lang_string('defaultmarksscheme_setting', 'qtype_aitext'),
new lang_string('thedefaultmarksscheme', 'qtype_aitext')));

$settings->add(new admin_setting_configtext(
'qtype_aitext/disclaimer',
new lang_string('disclaimer', 'qtype_aitext'),
new lang_string('disclaimer_setting', 'qtype_aitext'),
'(Response provided by ChatGPT)'
));
));
$settings->add(new admin_setting_configtextarea(
'qtype_aitext/prompt',
new lang_string('prompt', 'qtype_aitext'),
Expand Down

0 comments on commit 35356c6

Please sign in to comment.