Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Angleichung Question UI Renderer an SDK #118

Merged
merged 7 commits into from
Jul 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 58 additions & 0 deletions classes/api/attempt_file.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?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\api;

use qtype_questionpy\array_converter\array_converter;
use qtype_questionpy\array_converter\converter_config;

defined('MOODLE_INTERNAL') || die;

/**
* A file used in an attempt at a QuestionPy question.
*
* @package qtype_questionpy
* @author Jan Britz
* @copyright 2024 TU Berlin, innoCampus {@link https://www.questionpy.org}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class attempt_file {
/** @var string */
public string $name;

/** @var string|null */
public ?string $mimetype = null;

/** @var string $data */
public string $data; // TODO: replace by QPy-URL when functionality is implemented.

/**
* Initializes a new instance.
*
* @param string $name
* @param string $data
* @param string|null $mimetype
*/
public function __construct(string $name, string $data, ?string $mimetype = null) {
$this->name = $name;
$this->data = $data;
$this->mimetype = $mimetype;
}
}

array_converter::configure(attempt_file::class, function (converter_config $config) {
$config->rename("mimetype", "mime_type");
});
41 changes: 25 additions & 16 deletions classes/api/attempt_ui.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,36 +31,45 @@
*/
class attempt_ui {
/** @var string */
public string $content;
public string $formulation;

/** @var array string to string mapping of placeholder names to the values (to be replaced in the content) */
public array $placeholders = [];
/** @var string|null */
public ?string $generalfeedback = null;

/** @var string|null specifics TBD */
public ?string $includeinlinecss = null;
/** @var string|null */
public ?string $specificfeedback = null;

/** @var string|null specifics TBD */
public ?string $includecssfile = null;
/** @var string|null */
public ?string $rightanswer = null;

/** @var string specifics TBD */
public string $cachecontrol = "private";
/** @var array<string, string> string to string mapping of placeholder names to the values (to be replaced in the content) */
larsbonczek marked this conversation as resolved.
Show resolved Hide resolved
public array $placeholders = [];

/** @var object[] specifics TBD */
/** @var string[]|null */
public ?array $cssfiles = null;

/** @var array<string, attempt_file> specifics TBD */
larsbonczek marked this conversation as resolved.
Show resolved Hide resolved
public array $files = [];

/** @var string specifics TBD */
public string $cachecontrol = "PRIVATE_CACHE";

/**
* Initializes a new instance.
*
* @param string $content
* @param string $formulation
*/
public function __construct(string $content) {
$this->content = $content;
public function __construct(string $formulation) {
$this->formulation = $formulation;
}
}

array_converter::configure(attempt_ui::class, function (converter_config $config) {
$config
->rename("includeinlinecss", "include_inline_css")
->rename("includecssfile", "include_css_file")
->rename("cachecontrol", "cache_control");
->rename("generalfeedback", "general_feedback")
->rename("specificfeedback", "specific_feedback")
->rename("rightanswer", "right_answer")
->rename("cssfiles", "css_files")
->rename("cachecontrol", "cache_control")
->array_elements("files", attempt_file::class);
});
32 changes: 32 additions & 0 deletions classes/constants.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?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;

/**
* Constants used in multiple places.
*
* @package qtype_questionpy
* @author Jan Britz
* @copyright 2024 TU Berlin, innoCampus {@link https://www.questionpy.org}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class constants {
/** @var string XML namespace for XHTML */
public const NAMESPACE_XHTML = 'http://www.w3.org/1999/xhtml';
/** @var string XML namespace for our custom things */
public const NAMESPACE_QPY = 'http://questionpy.org/ns/question';
}
107 changes: 107 additions & 0 deletions classes/question_ui_metadata_extractor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
<?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;

use DOMAttr;
use DOMDocument;
use DOMElement;
use DOMXPath;

/**
* Parses the question UI XML and extracts the metadata.
*
* @package qtype_questionpy
* @author Maximilian Haye
* @copyright 2023 TU Berlin, innoCampus {@link https://www.questionpy.org}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class question_ui_metadata_extractor {
/** @var DOMDocument $xml */
private DOMDocument $xml;

/** @var DOMXPath $xpath */
private DOMXPath $xpath;

/** @var question_metadata|null $metadata */
private ?question_metadata $metadata = null;

/**
* Parses the given XML and initializes a new {@see question_ui_metadata_extractor} instance.
*
* @param string $xml XML as returned by the QPy Server
*/
public function __construct(string $xml) {
$this->xml = new DOMDocument();
$this->xml->loadXML($xml);

$this->xpath = new DOMXPath($this->xml);
$this->xpath->registerNamespace("xhtml", constants::NAMESPACE_XHTML);
$this->xpath->registerNamespace("qpy", constants::NAMESPACE_QPY);
}

/**
* Extracts metadata from the question UI.
*
* @return question_metadata
*/
public function extract(): question_metadata {
if (!is_null($this->metadata)) {
return $this->metadata;
}

$this->metadata = new question_metadata();
/** @var DOMAttr $attr */
foreach ($this->xpath->query("//@qpy:correct-response") as $attr) {
/** @var DOMElement $element */
$element = $attr->ownerElement;
$name = $element->getAttribute("name");
if (!$name) {
continue;
}

if (is_null($this->metadata->correctresponse)) {
$this->metadata->correctresponse = [];
}

if ($element->tagName == "input" && $element->getAttribute("type") == "radio") {
// On radio buttons, we expect the correct option to be marked with correct-response.
$radiovalue = $element->getAttribute("value");
$this->metadata->correctresponse[$name] = $radiovalue;
} else {
$this->metadata->correctresponse[$name] = $attr->value;
}
}

/** @var DOMElement $element */
foreach (
$this->xpath->query(
"//*[self::xhtml:input or self::xhtml:select or self::xhtml:textarea or self::xhtml:button]"
) as $element
) {
$name = $element->getAttribute("name");
if ($name) {
$this->metadata->expecteddata[$name] = PARAM_RAW;

if ($element->hasAttribute("required")) {
$this->metadata->requiredfields[] = $name;
}
}
}

return $this->metadata;
}
}
Loading
Loading