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 5a49abc
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 17 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";
}
}
};
4 changes: 2 additions & 2 deletions question.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 @@ -190,7 +190,7 @@ public function process_feedback(string $feedback) {
// $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

0 comments on commit 5a49abc

Please sign in to comment.