From e2b388c1a4ef76c83dcb0370b6cbf6e7aeb0d703 Mon Sep 17 00:00:00 2001 From: Tim Hunt Date: Fri, 2 Aug 2013 13:30:20 +0100 Subject: [PATCH] MDL-39507 questions: fix pluginfile URLs before format_text. This commit build's on Jean-Michel's work, tidying up a few lose ends. --- lib/questionlib.php | 5 -- mod/quiz/editlib.php | 8 +-- question/engine/lib.php | 16 ++++++ question/format.php | 7 +-- question/format/xhtml/format.php | 8 +-- question/format/xhtml/lib.php | 58 ++++++++++++++++++++++ question/type/essay/question.php | 5 +- question/type/multichoice/questiontype.php | 9 ++-- question/type/questionbase.php | 2 +- 9 files changed, 89 insertions(+), 29 deletions(-) create mode 100644 question/format/xhtml/lib.php diff --git a/lib/questionlib.php b/lib/questionlib.php index 07cb05afb6b..9984b71cd59 100644 --- a/lib/questionlib.php +++ b/lib/questionlib.php @@ -1219,11 +1219,6 @@ function question_categorylist($categoryid) { return $categorylist; } -function to_plain_text($text, $format, $options) { - $text = str_replace('@@PLUGINFILE@@/', 'http://example.com/', $text); - return html_to_text(format_text($text, $format, $options), 0, false); -} - //=========================== // Import/Export Functions //=========================== diff --git a/mod/quiz/editlib.php b/mod/quiz/editlib.php index c7e814bea52..6cebed780e2 100644 --- a/mod/quiz/editlib.php +++ b/mod/quiz/editlib.php @@ -1013,12 +1013,8 @@ function quiz_question_tostring($question, $showicon = false, } $result .= shorten_text(format_string($question->name), 200) . ''; if ($showquestiontext) { - $formatoptions = new stdClass(); - $formatoptions->noclean = true; - $formatoptions->para = false; - $questiontext = strip_tags(format_text($question->questiontext, - $question->questiontextformat, - $formatoptions, $COURSE->id)); + $questiontext = question_utils::to_plain_text($question->questiontext, + $question->questiontextformat, array('noclean' => true, 'para' => false)); $questiontext = shorten_text($questiontext, 200); $result .= ''; if (!empty($questiontext)) { diff --git a/question/engine/lib.php b/question/engine/lib.php index 80db5b4b46d..8b29eb8073d 100644 --- a/question/engine/lib.php +++ b/question/engine/lib.php @@ -818,6 +818,22 @@ abstract class question_utils { return self::clean_param_mark( optional_param($parname, null, PARAM_RAW_TRIMMED)); } + + /** + * Convert part of some question content to plain text. + * @param string $text the text. + * @param int $format the text format. + * @param array $options formatting options. Passed to {@link format_text}. + * @return float|string|null cleaned mark as a float if possible. Otherwise '' or null. + */ + public static function to_plain_text($text, $format, $options = array('noclean' => 'true')) { + // The following call to html_to_text uses the option that strips out + // all URLs, but format_text complains if it finds @@PLUGINFILE@@ tokens. + // So, we need to replace @@PLUGINFILE@@ with a real URL, but it doesn't + // matter what. We use http://example.com/. + $text = str_replace('@@PLUGINFILE@@/', 'http://example.com/', $text); + return html_to_text(format_text($text, $format, $options), 0, false); + } } diff --git a/question/format.php b/question/format.php index 377ac2c2fef..7dfe33056a9 100644 --- a/question/format.php +++ b/question/format.php @@ -933,11 +933,8 @@ class qformat_default { * during import to let the user see roughly what is going on. */ protected function format_question_text($question) { - global $DB; - $formatoptions = new stdClass(); - $formatoptions->noclean = true; - return to_plain_text($question->questiontext, - $question->questiontextformat, $formatoptions); + return question_utils::to_plain_text($question->questiontext, + $question->questiontextformat); } } diff --git a/question/format/xhtml/format.php b/question/format/xhtml/format.php index 28011874039..0c625881990 100644 --- a/question/format/xhtml/format.php +++ b/question/format/xhtml/format.php @@ -66,11 +66,11 @@ class qformat_xhtml extends qformat_default { $expout .= "

$question->name

\n"; // Format and add the question text. - $text = question_rewrite_question_urls($question->questiontext, 'pluginfile.php', - $question->contextid, 'question', $filearea, - '', $question->id); + $text = question_rewrite_question_preview_urls($question->questiontext, $question->id, + $question->contextid, 'question', 'questiontext', $question->id, + $question->contextid, 'qformat_xhtml'); $expout .= '

' . format_text($text, - $question->questiontextformat) . "

\n"; + $question->questiontextformat, array('noclean' => true)) . "

\n"; // Selection depends on question type. switch($question->qtype) { diff --git a/question/format/xhtml/lib.php b/question/format/xhtml/lib.php new file mode 100644 index 00000000000..e1d6c640b22 --- /dev/null +++ b/question/format/xhtml/lib.php @@ -0,0 +1,58 @@ +. + +/** + * Standard plugin entry points of the HTML question export format. + * + * @package qformat_xhtml + * @copyright 2013 The Open University + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + + +defined('MOODLE_INTERNAL') || die(); + + +/** + * Serve question files when they are displayed in this export format. + * + * @param context $previewcontext the quiz context + * @param int $questionid the question id. + * @param context $filecontext the file (question) context + * @param string $filecomponent the component the file belongs to. + * @param string $filearea the file area. + * @param array $args remaining file args. + * @param bool $forcedownload. + * @param array $options additional options affecting the file serving. + */ +function qformat_xhtml_question_preview_pluginfile($previewcontext, $questionid, + $filecontext, $filecomponent, $filearea, $args, $forcedownload, $options = array()) { + global $CFG; + + list($context, $course, $cm) = get_context_info_array($previewcontext->id); + require_login($course, false, $cm); + + question_require_capability_on($questionid, 'view'); + + $fs = get_file_storage(); + $relativepath = implode('/', $args); + $fullpath = "/{$filecontext->id}/{$filecomponent}/{$filearea}/{$relativepath}"; + if (!$file = $fs->get_file_by_hash(sha1($fullpath)) or $file->is_directory()) { + send_file_not_found(); + } + + send_stored_file($file, 0, 0, $forcedownload, $options); +} diff --git a/question/type/essay/question.php b/question/type/essay/question.php index 75d085d6e34..29a4fc742e4 100644 --- a/question/type/essay/question.php +++ b/question/type/essay/question.php @@ -70,9 +70,8 @@ class qtype_essay_question extends question_with_responses { public function summarise_response(array $response) { if (isset($response['answer'])) { - $formatoptions = new stdClass(); - $formatoptions->para = false; - return html_to_text($response['answer'], FORMAT_HTML, $formatoptions); + return question_utils::to_plain_text($response['answer'], + $response['answerformat'], array('para' => false)); } else { return null; } diff --git a/question/type/multichoice/questiontype.php b/question/type/multichoice/questiontype.php index ff29604ca06..352018ed4d6 100644 --- a/question/type/multichoice/questiontype.php +++ b/question/type/multichoice/questiontype.php @@ -203,8 +203,8 @@ class qtype_multichoice extends question_type { $responses = array(); foreach ($questiondata->options->answers as $aid => $answer) { - $responses[$aid] = new question_possible_response(to_plain_text( - $answer->answer, $answer->answerformat, array('noclean' => true)), + $responses[$aid] = new question_possible_response( + question_utils::to_plain_text($answer->answer, $answer->answerformat), $answer->fraction); } @@ -214,9 +214,8 @@ class qtype_multichoice extends question_type { $parts = array(); foreach ($questiondata->options->answers as $aid => $answer) { - $parts[$aid] = array($aid => - new question_possible_response(to_plain_text( - $answer->answer, $answer->answerformat, array('noclean' => true)), + $parts[$aid] = array($aid => new question_possible_response( + question_utils::to_plain_text($answer->answer, $answer->answerformat), $answer->fraction)); } diff --git a/question/type/questionbase.php b/question/type/questionbase.php index 8c49f6c35cf..448d200f5bc 100644 --- a/question/type/questionbase.php +++ b/question/type/questionbase.php @@ -333,7 +333,7 @@ abstract class question_definition { * @return string the equivalent plain text. */ public function html_to_text($text, $format) { - return to_plain_text($text, $format, array('noclean' => true)); + return question_utils::to_plain_text($text, $format); } /** @return the result of applying {@link format_text()} to the question text. */