|
| 1 | +<?php |
| 2 | +// This file is part of Moodle - http://moodle.org/ |
| 3 | +// |
| 4 | +// Moodle is free software: you can redistribute it and/or modify |
| 5 | +// it under the terms of the GNU General Public License as published by |
| 6 | +// the Free Software Foundation, either version 3 of the License, or |
| 7 | +// (at your option) any later version. |
| 8 | +// |
| 9 | +// Moodle is distributed in the hope that it will be useful, |
| 10 | +// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | +// GNU General Public License for more details. |
| 13 | +// |
| 14 | +// You should have received a copy of the GNU General Public License |
| 15 | +// along with Moodle. If not, see <http://www.gnu.org/licenses/>. |
| 16 | + |
| 17 | +/** |
| 18 | + * Abstract base renderer for different response formats. |
| 19 | + * |
| 20 | + * @package qtype_aitext |
| 21 | + * @subpackage aitext |
| 22 | + * @copyright 2026 ISB Bayern |
| 23 | + * @author Dr. Peter Mayer |
| 24 | + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later |
| 25 | + */ |
| 26 | + |
| 27 | +/** |
| 28 | + * Abstract out the differences between different type of response format. |
| 29 | + * |
| 30 | + * @copyright 2026 ISB Bayern |
| 31 | + * @author Dr. Peter Mayer |
| 32 | + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later |
| 33 | + */ |
| 34 | +abstract class qtype_aitext_format_renderer_base extends plugin_renderer_base { |
| 35 | + /** @var question_display_options Question display options instance for any necessary information for rendering the question. */ |
| 36 | + protected $displayoptions; |
| 37 | + |
| 38 | + /** |
| 39 | + * Question number setter. |
| 40 | + * |
| 41 | + * @param question_display_options $displayoptions |
| 42 | + */ |
| 43 | + public function set_displayoptions(question_display_options $displayoptions): void { |
| 44 | + $this->displayoptions = $displayoptions; |
| 45 | + } |
| 46 | + |
| 47 | + /** |
| 48 | + * Render the students response when the question is in read-only mode. |
| 49 | + * |
| 50 | + * @param string $name the variable name this input edits. |
| 51 | + * @param question_attempt $qa the question attempt being display. |
| 52 | + * @param question_attempt_step $step the current step. |
| 53 | + * @param int $lines approximate size of input box to display. |
| 54 | + * @param object $context the context teh output belongs to. |
| 55 | + * @return string html to display the response. |
| 56 | + */ |
| 57 | + public function response_area_read_only($name, $qa, $step, $lines, $context) { |
| 58 | + global $USER; |
| 59 | + |
| 60 | + $question = $qa->get_question(); |
| 61 | + $uniqid = uniqid(); |
| 62 | + $readonlyareaid = 'aitext_readonly_area' . $uniqid; |
| 63 | + $spellcheckeditbuttonid = 'aitext_spellcheckedit' . $uniqid; |
| 64 | + |
| 65 | + if ($question->spellcheck) { |
| 66 | + $this->page->requires->js_call_amd('qtype_aitext/diff'); |
| 67 | + $this->page->requires->js_call_amd( |
| 68 | + 'qtype_aitext/spellcheck', |
| 69 | + 'init', |
| 70 | + ['#' . $readonlyareaid, '#' . $spellcheckeditbuttonid] |
| 71 | + ); |
| 72 | + $stepspellcheck = $qa->get_last_step_with_qt_var('-spellcheckresponse'); |
| 73 | + $stepanswer = $qa->get_last_step_with_qt_var('answer'); |
| 74 | + } |
| 75 | + // Lib to display the spellcheck diff. |
| 76 | + $labelbyid = $qa->get_qt_field_name($name) . '_label'; |
| 77 | + $responselabel = $this->displayoptions->add_question_identifier_to_label(get_string('answertext', 'qtype_aitext')); |
| 78 | + $output = html_writer::tag('h4', $responselabel, ['id' => $labelbyid, 'class' => 'sr-only']); |
| 79 | + |
| 80 | + $divoptions = [ |
| 81 | + 'id' => $readonlyareaid, |
| 82 | + 'role' => 'textbox', |
| 83 | + 'aria-readonly' => 'true', |
| 84 | + 'aria-labelledby' => $labelbyid, |
| 85 | + 'class' => $this->class_name() . ' qtype_aitext_response readonly', |
| 86 | + 'style' => 'min-height: ' . ($lines * 1.25) . 'em;', |
| 87 | + ]; |
| 88 | + |
| 89 | + if ($qa->get_question()->spellcheck) { |
| 90 | + $divoptions['data-spellcheck'] = $this->prepare_response('-spellcheckresponse', $qa, $stepspellcheck, $context); |
| 91 | + $divoptions['data-spellcheckattemptstepid'] = $stepspellcheck->get_id(); |
| 92 | + $divoptions['data-spellcheckattemptstepanswerid'] = $stepanswer->get_id(); |
| 93 | + $divoptions['data-answer'] = $this->prepare_response($name, $qa, $step, $context); |
| 94 | + } |
| 95 | + |
| 96 | + $output .= html_writer::tag('div', $this->prepare_response($name, $qa, $step, $context), $divoptions); |
| 97 | + |
| 98 | + if ( |
| 99 | + $qa->get_question()->spellcheck && |
| 100 | + ( |
| 101 | + has_capability('mod/quiz:grade', $context) || |
| 102 | + has_capability('mod/quiz:regrade', $context) || |
| 103 | + ($context->contextlevel === CONTEXT_USER && intval($USER->id) === intval($context->instanceid)) |
| 104 | + ) |
| 105 | + ) { |
| 106 | + $btnoptions = ['id' => $spellcheckeditbuttonid, 'class' => 'btn btn-link']; |
| 107 | + $output .= html_writer::tag( |
| 108 | + 'button', |
| 109 | + $this->output->pix_icon( |
| 110 | + 'i/edit', |
| 111 | + get_string('spellcheckedit', 'qtype_aitext'), |
| 112 | + 'moodle' |
| 113 | + ) . " " . get_string('spellcheckedit', 'qtype_aitext'), |
| 114 | + $btnoptions |
| 115 | + ); |
| 116 | + } |
| 117 | + // Height $lines * 1.25 because that is a typical line-height on web pages. |
| 118 | + // That seems to give results that look OK. |
| 119 | + |
| 120 | + return $output; |
| 121 | + } |
| 122 | + |
| 123 | + /** |
| 124 | + * Render the students respone when the question is in read-only mode. |
| 125 | + * @param string $name the variable name this input edits. |
| 126 | + * @param question_attempt $qa the question attempt being display. |
| 127 | + * @param question_attempt_step $step the current step. |
| 128 | + * @param int $lines approximate size of input box to display. |
| 129 | + * @param object $context the context teh output belongs to. |
| 130 | + * @return string html to display the response for editing. |
| 131 | + */ |
| 132 | + abstract public function response_area_input( |
| 133 | + $name, |
| 134 | + question_attempt $qa, |
| 135 | + question_attempt_step $step, |
| 136 | + $lines, |
| 137 | + $context |
| 138 | + ); |
| 139 | + |
| 140 | + /** |
| 141 | + * Specific class name to add to the input element. |
| 142 | + * |
| 143 | + * @return string |
| 144 | + */ |
| 145 | + abstract protected function class_name(); |
| 146 | +} |
0 commit comments