MDL-39507 questions: fix pluginfile URLs before format_text.

This commit build's on Jean-Michel's work, tidying up a few lose ends.
This commit is contained in:
Tim Hunt 2013-08-02 13:30:20 +01:00
parent cda91161df
commit 06a42cff90
9 changed files with 89 additions and 29 deletions

View file

@ -1214,11 +1214,6 @@ function question_categorylist($categoryid) {
return $categorylist; 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 // Import/Export Functions
//=========================== //===========================

View file

@ -1013,12 +1013,8 @@ function quiz_question_tostring($question, $showicon = false,
} }
$result .= shorten_text(format_string($question->name), 200) . '</span>'; $result .= shorten_text(format_string($question->name), 200) . '</span>';
if ($showquestiontext) { if ($showquestiontext) {
$formatoptions = new stdClass(); $questiontext = question_utils::to_plain_text($question->questiontext,
$formatoptions->noclean = true; $question->questiontextformat, array('noclean' => true, 'para' => false));
$formatoptions->para = false;
$questiontext = strip_tags(format_text($question->questiontext,
$question->questiontextformat,
$formatoptions, $COURSE->id));
$questiontext = shorten_text($questiontext, 200); $questiontext = shorten_text($questiontext, 200);
$result .= '<span class="questiontext">'; $result .= '<span class="questiontext">';
if (!empty($questiontext)) { if (!empty($questiontext)) {

View file

@ -816,6 +816,22 @@ abstract class question_utils {
return self::clean_param_mark( return self::clean_param_mark(
optional_param($parname, null, PARAM_RAW_TRIMMED)); 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);
}
} }

View file

@ -933,11 +933,8 @@ class qformat_default {
* during import to let the user see roughly what is going on. * during import to let the user see roughly what is going on.
*/ */
protected function format_question_text($question) { protected function format_question_text($question) {
global $DB; return question_utils::to_plain_text($question->questiontext,
$formatoptions = new stdClass(); $question->questiontextformat);
$formatoptions->noclean = true;
return to_plain_text($question->questiontext,
$question->questiontextformat, $formatoptions);
} }
} }

View file

@ -67,11 +67,11 @@ class qformat_xhtml extends qformat_default {
$expout .= "<h3>$question->name</h3>\n"; $expout .= "<h3>$question->name</h3>\n";
// Format and add the question text. // Format and add the question text.
$text = question_rewrite_question_urls($question->questiontext, 'pluginfile.php', $text = question_rewrite_question_preview_urls($question->questiontext, $question->id,
$question->contextid, 'question', $filearea, $question->contextid, 'question', 'questiontext', $question->id,
'', $question->id); $question->contextid, 'qformat_xhtml');
$expout .= '<p class="questiontext">' . format_text($text, $expout .= '<p class="questiontext">' . format_text($text,
$question->questiontextformat) . "</p>\n"; $question->questiontextformat, array('noclean' => true)) . "</p>\n";
// selection depends on question type // selection depends on question type
switch($question->qtype) { switch($question->qtype) {

View file

@ -0,0 +1,58 @@
<?php
// This file is part of Moodle - http://moodle.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/>.
/**
* 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);
}

View file

@ -68,9 +68,8 @@ class qtype_essay_question extends question_with_responses {
public function summarise_response(array $response) { public function summarise_response(array $response) {
if (isset($response['answer'])) { if (isset($response['answer'])) {
$formatoptions = new stdClass(); return question_utils::to_plain_text($response['answer'],
$formatoptions->para = false; $response['answerformat'], array('para' => false));
return html_to_text($response['answer'], FORMAT_HTML, $formatoptions);
} else { } else {
return null; return null;
} }

View file

@ -203,8 +203,8 @@ class qtype_multichoice extends question_type {
$responses = array(); $responses = array();
foreach ($questiondata->options->answers as $aid => $answer) { foreach ($questiondata->options->answers as $aid => $answer) {
$responses[$aid] = new question_possible_response(to_plain_text( $responses[$aid] = new question_possible_response(
$answer->answer, $answer->answerformat, array('noclean' => true)), question_utils::to_plain_text($answer->answer, $answer->answerformat),
$answer->fraction); $answer->fraction);
} }
@ -214,9 +214,8 @@ class qtype_multichoice extends question_type {
$parts = array(); $parts = array();
foreach ($questiondata->options->answers as $aid => $answer) { foreach ($questiondata->options->answers as $aid => $answer) {
$parts[$aid] = array($aid => $parts[$aid] = array($aid => new question_possible_response(
new question_possible_response(to_plain_text( question_utils::to_plain_text($answer->answer, $answer->answerformat),
$answer->answer, $answer->answerformat, array('noclean' => true)),
$answer->fraction)); $answer->fraction));
} }

View file

@ -317,7 +317,7 @@ abstract class question_definition {
* @return string the equivalent plain text. * @return string the equivalent plain text.
*/ */
public function html_to_text($text, $format) { 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. */ /** @return the result of applying {@link format_text()} to the question text. */