Skip to content
Browse files

MDL-28564 question type importing, improve Jamie's fix.

  • Loading branch information...
1 parent 80fa621 commit 6310adb3f48f8a41fa9984cc4e1b44ce61e61013 @timhunt timhunt committed Aug 22, 2011
Showing with 19 additions and 16 deletions.
  1. +16 −6 question/format/xml/format.php
  2. +3 −10 question/type/questiontypebase.php
View
22 question/format/xml/format.php
@@ -1012,6 +1012,20 @@ public function get_single($id) {
}
/**
+ * Take a string, and wrap it in a CDATA secion, if that is required to make
+ * the output XML valid.
+ * @param string $string a string
+ * @return string the string, wrapped in CDATA if necessary.
+ */
+ public function xml_escape($string) {
+ if (!empty($string) && htmlspecialchars($string) != $string) {
+ return "<![CDATA[{$string}]]>";
+ } else {
+ return $string;
+ }
+ }
+
+ /**
* Generates <text></text> tags, processing raw text therein
* @param string $raw the content to output.
* @param int $indent the current indent level.
@@ -1020,11 +1034,7 @@ public function get_single($id) {
*/
public function writetext($raw, $indent = 0, $short = true) {
$indent = str_repeat(' ', $indent);
-
- // if required add CDATA tags
- if (!empty($raw) && htmlspecialchars($raw) != $raw) {
- $raw = "<![CDATA[$raw]]>";
- }
+ $raw = $this->xml_escape($raw);
if ($short) {
$xml = "$indent<text>$raw</text>\n";
@@ -1467,7 +1477,7 @@ public function write_hint($hint) {
$output .= " <clearwrong/>\n";
}
if (!empty($hint->options)) {
- $output .= ' <options>' . htmlspecialchars($hint->options) . "</options>\n";
+ $output .= ' <options>' . $this->xml_escape($hint->options) . "</options>\n";
}
$output .= " </hint>\n";
return $output;
View
13 question/type/questiontypebase.php
@@ -986,7 +986,7 @@ public function export_to_xml($question, $format, $extra=null) {
array_shift($extraquestionfields);
$expout='';
foreach ($extraquestionfields as $field) {
- $exportedvalue = self::wrap_html_special_chars($question->options->$field);
+ $exportedvalue = $format->xml_escape($question->options->$field);
$expout .= " <$field>{$exportedvalue}</$field>\n";
}
@@ -1003,22 +1003,15 @@ public function export_to_xml($question, $format, $extra=null) {
$expout .= " </feedback>\n";
if (is_array($extraanswersfields)) {
foreach ($extraanswersfields as $field) {
- $exportedvalue = self::wrap_html_special_chars($answer->$field);
- $expout .= " <$field>{$exportedvalue}</$field>\n";
+ $exportedvalue = $format->xml_escape($answer->$field);
+ $expout .= " <{$field}>{$exportedvalue}</{$field}>\n";
}
}
-
$expout .= " </answer>\n";
}
return $expout;
}
- protected static function wrap_html_special_chars($text) {
- if (!empty($text) && htmlspecialchars($text) != $text) {
- $text = '<![CDATA[' . $text . ']]>';
- }
- return $text;
- }
/**
* Abstract function implemented by each question type. It runs all the code

0 comments on commit 6310adb

Please sign in to comment.
Something went wrong with that request. Please try again.