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 6c23d0d403
commit e2b388c1a4
9 changed files with 89 additions and 29 deletions

View File

@ -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
//===========================

View File

@ -1013,12 +1013,8 @@ function quiz_question_tostring($question, $showicon = false,
}
$result .= shorten_text(format_string($question->name), 200) . '</span>';
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 .= '<span class="questiontext">';
if (!empty($questiontext)) {

View File

@ -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);
}
}

View File

@ -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);
}
}

View File

@ -66,11 +66,11 @@ class qformat_xhtml extends qformat_default {
$expout .= "<h3>$question->name</h3>\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 .= '<p class="questiontext">' . format_text($text,
$question->questiontextformat) . "</p>\n";
$question->questiontextformat, array('noclean' => true)) . "</p>\n";
// Selection depends on question type.
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

@ -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;
}

View File

@ -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));
}

View File

@ -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. */