Skip to content

Commit

Permalink
MBS-6059: Revert removing of XML completely, because it is needed for…
Browse files Browse the repository at this point in the history
… validation and extraction of information on PHP side
  • Loading branch information
PhMemmel committed Jun 12, 2022
1 parent 2a1911d commit 93da533
Show file tree
Hide file tree
Showing 9 changed files with 58 additions and 14 deletions.
2 changes: 1 addition & 1 deletion amd/build/ggbq.min.js

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

2 changes: 1 addition & 1 deletion amd/build/ggbq.min.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion amd/build/ggbt.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion amd/build/ggbt.min.js.map

Large diffs are not rendered by default.

6 changes: 6 additions & 0 deletions amd/src/ggbq.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ define(['jquery', 'https://www.geogebra.org/apps/deployggb.js'], function ($, GG
return {
b64input: [],
ggbBase64: [],
xmlinput: [],
ggbxml: [],
currentvals: [],
answerinput: [],
responsevars: [],
Expand Down Expand Up @@ -70,6 +72,7 @@ define(['jquery', 'https://www.geogebra.org/apps/deployggb.js'], function ($, GG
window.addEventListener('resize', resizeScalingContainer);

window.GGBQ.b64input[id].val(ggbApplet.getBase64());
window.GGBQ.xmlinput[id].val(ggbApplet.getXML());

window.GGBQ.qdiv[id].style.visibility = 'visible';
if (window.GGBQ.answerinput[id].val() == '') {
Expand All @@ -90,6 +93,8 @@ define(['jquery', 'https://www.geogebra.org/apps/deployggb.js'], function ($, GG
this.b64input[slot] = $(document.getElementById(ggbDataset.b64input));
this.ggbBase64[slot] = this.b64input[slot].val();

this.xmlinput[slot] = $(document.getElementById(ggbDataset.xmlinput));
this.ggbxml[slot] = this.xmlinput[slot].val();
this.qdiv[slot] = $("#q" + (slot) + " .qtext")[0];

var parameters = JSON.parse(ggbDataset.parameters);
Expand Down Expand Up @@ -150,6 +155,7 @@ define(['jquery', 'https://www.geogebra.org/apps/deployggb.js'], function ($, GG
var ggbApplet = window['ggbApplet' + i];
if (typeof ggbApplet !== "undefined") {
window.GGBQ.b64input[i].val(ggbApplet.getBase64());
window.GGBQ.xmlinput[i].val(ggbApplet.getXML());

// Workaround, to set all randomized variables.
for (const [key, value] of Object.entries(window.GGBQ.ggbDatasetVars)) {
Expand Down
3 changes: 3 additions & 0 deletions amd/src/ggbt.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@
$('input[name="ggbcodebaseversion"]').val(window.applet1.getHTML5CodebaseVersion());

if (typeof (this.ggbcheckb) == "undefined") {
var applet = document.ggbApplet;
console.log($('input[name="ggbxml"]'));
$('input[name="ggbxml"]').val(applet.getXML());

var randomizedvar = document.getElementById('id_randomizedvar');
if (!randomizedvar.value) {
Expand Down
13 changes: 6 additions & 7 deletions edit_geogebra_form.php
Original file line number Diff line number Diff line change
Expand Up @@ -329,10 +329,8 @@ private function check_constraints($data, &$errors) {
// Check if all vars in constraints are part of randomized vars.
if (count($errors) === 0) {
foreach ($inequalitystrings as $inequalitystring) {
// TODO We need to decode from base64 here to get to the xml we do not save anymore.
// TODO Maybe we can also refactor the whole way of checking the validness for randomizedvars.
if (!qtype_geogebra_question_helper::is_valid_inequality_for_randomizedvars($inequalitystring,
$data['randomizedvar'], $data['ggbxml'])
$data['randomizedvar'])
) {
if (!isset($errors['constraints'])) {
$errors['constraints'] = '';
Expand All @@ -347,8 +345,6 @@ private function check_constraints($data, &$errors) {
// Check if constraints are within the sliders min and max.
if (count($errors) === 0) {
foreach ($inequalitystrings as $inequalitystring) {
// TODO We need to decode from base64 here to get to the xml we do not save anymore.
// TODO Maybe we can also refactor the whole way of checking the inequality of slider min max.
if (!qtype_geogebra_question_helper::is_valid_inequality_for_slider_minmax($inequalitystring,
$data['randomizedvar'], $data['ggbxml'])
) {
Expand All @@ -364,7 +360,6 @@ private function check_constraints($data, &$errors) {
}
// Check if constraints can be met i.e. are not contradictory or to hard to meet with random numbers.
if (count($errors) === 0) {
// TODO Refactor xml to base64
$vars = qtype_geogebra_question_helper::get_variables_with_minmaxstep($data['randomizedvar'], $data['ggbxml']);
$inequalities = array();
foreach ($inequalitystrings as $inequalitystring) {
Expand All @@ -387,7 +382,6 @@ private function check_answer($data, &$errors) {

if (isset($data['answer'])) {
$i = 0;
// TODO rework to base64 instead of xml
$xml = simplexml_load_string($data['ggbxml']);
foreach ($data['answer'] as $label) {
if (!empty($label)) {
Expand Down Expand Up @@ -466,6 +460,11 @@ private function add_hidden_inputs($mform) {

$mform->addElement('hidden', 'ggbcodebaseversion');
$mform->setType('ggbcodebaseversion', PARAM_RAW);

// We need the XML injected by the GGB API function to be able to access GGB objects and values from PHP side. However,
// we do not use the XML representation to store the GGB applet itself, but rely on base64.
$mform->addElement('hidden', 'ggbxml');
$mform->setType('ggbxml', PARAM_RAW);
}

/**
Expand Down
29 changes: 26 additions & 3 deletions question.php
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ public function get_expected_data() {
$expected = array();
$expected['answer'] = PARAM_RAW;
$expected['ggbbase64'] = PARAM_RAW;
$expected['ggbxml'] = PARAM_RAW;
return $expected;
}

Expand All @@ -156,6 +157,7 @@ public function get_correct_response() {
*/
public function is_complete_response(array $response) {
$ret = array_key_exists('ggbbase64', $response) && ($response['ggbbase64']);
$ret = $ret && array_key_exists('ggbxml', $response) && ($response['ggbxml']);
if (!empty($this->answers)) {
$ret = $ret && array_key_exists('answer', $response) && ($response['answer'] || $response['answer'] === '0');
$ret = $ret && (preg_replace("/[^0,1]/", "", $response['answer']) == $response['answer']);
Expand All @@ -175,9 +177,27 @@ public function is_complete_response(array $response) {
* whether the new set of responses can safely be discarded.
*/
public function is_same_response(array $prevresponse, array $newresponse) {
// TODO We have to find a way to determine if a response of a user has changed based on base64.
// For now we just say: Whenever it's loaded und saved, it has changed (no matter if there actually have been changes).
return false;
$ret = question_utils::arrays_same_at_key_missing_is_blank($prevresponse, $newresponse, 'answer');
// The base64 string does not seem to be equal every time you load the applet so using xml
// Some values in euclidianview -> coordsystem are also not equal on each submit
// I think we should only use construction from the xml.
$prevxml = '';
if (isset($prevresponse['ggbxml'])) {
$prevxml = simplexml_load_string($prevresponse['ggbxml']);
}
$newxml = '';
if (isset($newresponse['ggbxml'])) {
$newxml = simplexml_load_string($newresponse['ggbxml']);
}
if (!empty($newxml) && empty($prevxml)) {
$ret = false;
} else {
if (!empty($newxml)) {
$ret = $ret && ($prevxml->construction->asXML() == $newxml->construction->asXML());
}
}

return $ret;
}

/**
Expand Down Expand Up @@ -245,6 +265,9 @@ public function get_validation_error(array $response) {
if (!(array_key_exists('ggbbase64', $response) && ($response['ggbbase64']))) {
return get_string('ggbfilemissing', 'qtype_geogebra');
}
if (!(array_key_exists('ggbxml', $response) && ($response['ggbxml']))) {
return get_string('ggbxmlmissing', 'qtype_geogebra');
}
if (!empty($this->answers) &&
!(array_key_exists('answer', $response) && ($response['answer'] || $response['answer'] === '0'))
) {
Expand Down
13 changes: 13 additions & 0 deletions renderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,18 @@ public function formulation_and_controls(question_attempt $qa, question_display_

$result .= html_writer::empty_tag('input', $b64inputattributes);

$xmlinputname = $qa->get_qt_field_name('ggbxml');

$xmlinputattributes = array(
'type' => 'hidden',
'name' => $xmlinputname,
'value' => '', // Value is being extracted from base64 by JS module.
'id' => $xmlinputname,
'size' => 80,
);

$result .= html_writer::empty_tag('input', $xmlinputattributes);

$answercurrent = $qa->get_last_qt_var('answer');
$answerinputname = $qa->get_qt_field_name('answer');

Expand Down Expand Up @@ -118,6 +130,7 @@ public function formulation_and_controls(question_attempt $qa, question_display_
data-div=$ggbdivname
data-vars=$currentvals
data-b64input=$b64inputname
data-xmlinput=$xmlinputname
data-answerinput=$answerinputname
data-responsevars=$responsevarsjson
data-slot=$slot
Expand Down

0 comments on commit 93da533

Please sign in to comment.