Skip to content

Draft: fix for xml export of items with html - #86

Draft
a-kempka wants to merge 2 commits into
ndunand:masterfrom
a-kempka:#65-xml-html-export
Draft

Draft: fix for xml export of items with html#86
a-kempka wants to merge 2 commits into
ndunand:masterfrom
a-kempka:#65-xml-html-export

Conversation

@a-kempka

@a-kempka a-kempka commented Sep 4, 2025

Copy link
Copy Markdown
Contributor

fixes #65 as question items with html are escaped

fixes ndunand#65 as question items with html are escaped

@danowar2k danowar2k left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is an internal review before removing draft status, a-kempka and myself are colleagues.

There are changes to be done or to be discussed....

Comment thread questiontype.php
* @param mixed $value
* @return string
*/
protected static function text_from_editor($value): string {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This assumes another change (having an editor form field for row shorttext) to already have happened. As this isn't the case, this should not be in this PR.

Comment thread questiontype.php
* @param object $question
* @return void
*/
protected static function normalise_rows_shorttext($question): void {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See above, this isn't necessary as long as there is no editor form field for row shorttext.

Comment thread questiontype.php
global $DB;
$store = new question_matrix_store();

// Ensure rows_shorttext are plain strings (if coming from an HTML editor).

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Part three, this is not part of the solution for this issue.

Comment thread questiontype.php
'id' => $rowid,
'matrixid' => $matrixid,
'shorttext' => $question->rows_shorttext[$i],
'shorttext' => $question->rows_shorttext[$i], // already normalised to string

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This comment only makes sense if the above changes are in this PR, which they shouldn't be.

Comment thread questiontype.php
$question->rows_shorttext[$index] = $format->getpath($rowxml, ['#', 'shorttext', 0, '#'], '');
// Support both legacy: <shorttext>text</shorttext>
// and new: <shorttext format="..."><text>html</text></shorttext>
$shorttexthtml = $format->getpath($rowxml, ['#', 'shorttext', 0, '#', 'text', 0, '#'], null);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There exists a function called import_text() that does some small post-processing. You should probably use that, so:

$shorttexthtml = $format->getpath($rowxml, ['#', 'shorttext', 0, '#', 'text'], null);
if ($shorttexthtml !== null) {
   // Postprocess text node
   $shorttexthtml = import_text($shorttexthtml);
} else {
   // An older exported matrix question
   $shorttexthtml = $format->getpath($rowxml, ['#', 'shorttext', 0, '#'], '');
}

Comment thread questiontype.php
// Legacy path (no <text> child).
$shorttexthtml = $format->getpath($rowxml, ['#', 'shorttext', 0, '#'], '');
}
$question->rows_shorttext[$index] = (string)$shorttexthtml;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think you need the additional casting to string.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

BUG: Import / Export as XML fails due to lack of HTML escaping in the items' text

2 participants