Skip to content

Commit

Permalink
wip: orphan values
Browse files Browse the repository at this point in the history
  • Loading branch information
MHajoha committed Dec 5, 2024
1 parent d7685e8 commit bb1d1d9
Show file tree
Hide file tree
Showing 12 changed files with 190 additions and 27 deletions.
1 change: 0 additions & 1 deletion classes/attempt_ui/dom_utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,6 @@ public static function html_to_fragment(DOMDocument $doc, string $html, int $opt
*
* @param DOMElement $select
* @param string $value
* @throws DOMException
* @throws coding_exception
*/
public static function set_select_value(DOMElement $select, string $value): void {
Expand Down
42 changes: 42 additions & 0 deletions classes/attempt_ui/invalid_option_warning.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php
// This file is part of the QuestionPy Moodle plugin - https://questionpy.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/>.

namespace qtype_questionpy\attempt_ui;

use coding_exception;
use html_writer;

class invalid_option_warning {
public function __construct(
public string $name,
public string $value,
public array $availablevalues
) {
}

/**
* @throws coding_exception
*/
public function localize(): string {
$availablevaluesstr = implode(', ', array_map(fn($value) => html_writer::tag('code', format_string($value)), $this->availablevalues));

return get_string('render_warning_invalid_value', 'qtype_questionpy', [
'name' => html_writer::tag('code', format_string($this->name)),
'value' => html_writer::tag('code', format_string($this->value)),
'availablevalues' => $availablevaluesstr,
]);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,7 @@
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

namespace qtype_questionpy;

use qtype_questionpy\attempt_ui\question_ui_renderer;
namespace qtype_questionpy\attempt_ui;

/**
* Metadata about a question attempt, extracted by {@see question_ui_renderer} from the XML.
Expand Down
1 change: 0 additions & 1 deletion classes/attempt_ui/question_ui_metadata_extractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
use DOMElement;
use DOMXPath;
use qtype_questionpy\constants;
use qtype_questionpy\question_metadata;

/**
* Parses the question UI XML and extracts the metadata.
Expand Down
91 changes: 75 additions & 16 deletions classes/attempt_ui/question_ui_renderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
use DOMText;
use DOMXPath;
use qtype_questionpy\constants;
use qtype_questionpy\utils;
use qtype_questionpy_question;
use question_attempt;
use question_display_options;
Expand All @@ -46,9 +47,6 @@ class question_ui_renderer {
/** @var DOMXPath $xpath */
private DOMXPath $xpath;

/** @var string|null $html */
private ?string $html = null;

/** @var array $placeholders */
private array $placeholders;

Expand All @@ -58,10 +56,13 @@ class question_ui_renderer {
/** @var question_attempt $attempt */
private question_attempt $attempt;

/** @var render_result|null $result */
private ?render_result $result = null;

/**
* Parses the given XML and initializes a new {@see question_ui_renderer} instance.
*
* @param string $xml XML as returned by the QPy Server
* @param string $xml XML as returned by the QPy Server
* @param array $placeholders string to string mapping of placeholder names to the values
* @param question_display_options $options
* @param question_attempt $attempt
Expand All @@ -87,13 +88,12 @@ public function __construct(string $xml, array $placeholders, question_display_o
/**
* Renders the given XML to HTML.
*
* @return string rendered html
* @return render_result
* @throws coding_exception
* @throws DOMException
*/
public function render(): string {
if (!is_null($this->html)) {
return $this->html;
public function render(): render_result {
if ($this->result) {
return $this->result;
}

$nextseed = mt_rand();
Expand All @@ -110,6 +110,8 @@ public function render(): string {
$this->shuffle_contents();
$this->format_floats();

$availableoptions = $this->extract_available_options();

// Remove all unhandled custom elements, attributes, comments, and non-default xmlns declarations.
$this->clean_up();

Expand All @@ -132,8 +134,9 @@ public function render(): string {
mt_srand($nextseed);
}

$this->html = $this->xml->saveHTML();
return $this->html;
$warnings = $this->check_for_unknown_options($availableoptions);
$this->result = new render_result($this->xml->saveHTML(), $warnings);
return $this->result;
}

/**
Expand Down Expand Up @@ -283,7 +286,7 @@ private function mangle_ids_and_names(): void {
*
* @return void
*
* @throws DOMException|coding_exception
* @throws coding_exception
*/
private function set_input_values_and_readonly(): void {
/** @var DOMElement $element */
Expand Down Expand Up @@ -505,9 +508,9 @@ private function hide_if_role(): void {

if (
!(in_array('teacher', $allowedroles) && $isteacher
|| in_array('proctor', $allowedroles) && $isteacher
|| in_array('scorer', $allowedroles) && $isscorer
|| in_array('developer', $allowedroles) && $isdeveloper)
|| in_array('proctor', $allowedroles) && $isteacher
|| in_array('scorer', $allowedroles) && $isscorer
|| in_array('developer', $allowedroles) && $isdeveloper)
) {
$attr->ownerElement->parentNode->removeChild($attr->ownerElement);
}
Expand Down Expand Up @@ -566,7 +569,7 @@ private function replace_qpy_urls(string $input): string {
assert($question instanceof qtype_questionpy_question);

return preg_replace_callback(
// The first two path segments are namespace and short name, and so more restrictive.
// The first two path segments are namespace and short name, and so more restrictive.
';qpy://static((?:/[a-z_][a-z0-9_]{0,126}){2}(?:/[\w\-@:%+.~=]+)+);',
function (array $match) use ($question) {
$path = $match[1];
Expand All @@ -584,4 +587,60 @@ function (array $match) use ($question) {
$input
);
}

private function extract_available_options(): array {
$optionsbyname = [];

/** @var DOMElement $select */
foreach ($this->xpath->query('//xhtml:select') as $select) {
$name = $select->getAttribute('name');
if (!$name) {
continue;
}

$values = [];
/** @var DOMElement $option */
foreach ($this->xpath->query('./xhtml:option | ./xhtml:optgroup/xhtml:option', $select) as $option) {
$values[] = $option->hasAttribute('value') ? $option->getAttribute('value') : $option->textContent;
}

$optionsbyname[$name] = array_unique($values);
}

/** @var DOMElement $input */
foreach ($this->xpath->query('//xhtml:input[(@type="checkbox" or @type="radio") and not(@qpy:warn-on-unknown-option = "no")]') as $input) {
$name = $input->getAttribute('name');
if (!$name) {
continue;
}

if (!array_key_exists($name, $optionsbyname)) {
$optionsbyname[$name] = [];
}

$value = $input->hasAttribute('value') ? $input->getAttribute('value') : 'on';
if (!in_array($value, $optionsbyname[$name])) {
$optionsbyname[$name][] = $value;
}
}

return $optionsbyname;
}

private function check_for_unknown_options(array $availableoptionsbyname): array {
$response = utils::get_last_response($this->attempt);

$warnings = [];
foreach ($availableoptionsbyname as $name => $availableoptions) {
if (!array_key_exists($name, $response)) {
continue;
}

$lastvalue = $response[$name];
if (!in_array($lastvalue, $availableoptions)) {
$warnings[] = new invalid_option_warning($name, $lastvalue, $availableoptions);
}
}
return $warnings;
}
}
27 changes: 27 additions & 0 deletions classes/attempt_ui/render_result.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php
// This file is part of the QuestionPy Moodle plugin - https://questionpy.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/>.

namespace qtype_questionpy\attempt_ui;

class render_result {
public function __construct(
/** @var string $html */
public string $html,
/** @var invalid_option_warning[] $warnings */
public array $warnings
) {
}
}
2 changes: 2 additions & 0 deletions classes/constants.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,6 @@ class constants {
public const QT_VAR_ATTEMPT_STATE = '_attemptstate';
/** @var string */
public const QT_VAR_SCORING_STATE = '_scoringstate';
/** @var string */
public const QT_VAR_RESPONSE_MARKER = 'qpy_marker';
}
12 changes: 11 additions & 1 deletion classes/utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
namespace qtype_questionpy;

use qtype_questionpy\form\elements\repetition_element;
use question_attempt;

/**
* Utility functions used in multiple places.
Expand Down Expand Up @@ -124,6 +125,15 @@ public static function reindex_integer_arrays(array &$array): void {
* @see question_attempt_step for the meaning of different step var prefixes
*/
public static function filter_for_response(array $qtvars): array {
return array_filter($qtvars, fn($key) => !str_starts_with($key, '_'), ARRAY_FILTER_USE_KEY);
return array_filter(
$qtvars,
fn($key) => !str_starts_with($key, '_') && $key !== constants::QT_VAR_RESPONSE_MARKER,
ARRAY_FILTER_USE_KEY
);
}

public static function get_last_response(question_attempt $qa): array {
$step = $qa->get_last_step_with_qt_var(constants::QT_VAR_RESPONSE_MARKER);
return self::filter_for_response($step->get_qt_data());
}
}
1 change: 1 addition & 0 deletions lang/en/qtype_questionpy.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
$string['question_package_upload'] = 'Upload your own';
$string['remove_packages_button'] = 'Remove Packages';
$string['render_error_section'] = 'An error occurred';
$string['render_warning_invalid_value'] = 'The last submission set the field {$a->name} to the value {$a->value}, but that option is no longer available. The available options are: {$a->availablevalues}.';
$string['same_version_different_hash_error'] = 'A package with the same version but different hash already exists.';
$string['search_all_header'] = 'All ({$a})';
$string['search_bar'] = 'Search...';
Expand Down
4 changes: 3 additions & 1 deletion question.php
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,9 @@ public function get_expected_data(): array|string {
// There was an error -> get all the submitted data.
return question_attempt::USE_RAW_DATA;
}
return $this->metadata->extract()->expecteddata;
return array_merge($this->metadata->extract()->expecteddata, [
constants::QT_VAR_RESPONSE_MARKER => PARAM_ALPHA,
]);
}

/**
Expand Down
28 changes: 27 additions & 1 deletion renderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
*/

use qtype_questionpy\attempt_ui\question_ui_renderer;
use qtype_questionpy\constants;

/**
* Generates the output for QuestionPy questions.
Expand Down Expand Up @@ -65,7 +66,32 @@ public function formulation_and_controls(question_attempt $qa, question_display_
}

$renderer = new question_ui_renderer($question->ui->formulation, $question->ui->placeholders, $options, $qa);
return $renderer->render();
$renderresult = $renderer->render();

$output = '';
if ($renderresult->warnings) {
// TODO: Might want to make this a template.
$output .= html_writer::start_div('alert alert-warning');
foreach ($renderresult->warnings as $warning) {
$output .= html_writer::tag('p', $warning->localize());
// Preserve the invalid value. TODO: Remove this when value is changed?
$output .= html_writer::empty_tag('input', [
'type' => 'hidden',
'name' => $warning->name,
'value' => $warning->value,
]);
}
$output .= html_writer::end_div();
}

$output .= html_writer::empty_tag('input', [
'type' => 'hidden',
'name' => $qa->get_qt_field_name(constants::QT_VAR_RESPONSE_MARKER),
]);

$output .= $renderresult->html;

return $output;
}

/**
Expand Down
4 changes: 1 addition & 3 deletions tests/attempt_ui/question_ui_metadata_extractor_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@

namespace qtype_questionpy\attempt_ui;

use qtype_questionpy\question_metadata;

/**
* Unit tests for {@see question_ui_metadata_extractor}.
*
Expand All @@ -31,7 +29,7 @@ final class question_ui_metadata_extractor_test extends \advanced_testcase {
* Tests that metadata is correctly extracted from the UI's input elements.
*
* @covers \qtype_questionpy\attempt_ui\question_ui_metadata_extractor
* @covers \qtype_questionpy\question_metadata
* @covers \qtype_questionpy\attempt_ui\question_metadata
*/
public function test_should_extract_correct_metadata(): void {
$input = file_get_contents(__DIR__ . '/question_uis/metadata.xhtml');
Expand Down

0 comments on commit bb1d1d9

Please sign in to comment.