Skip to content

Commit

Permalink
MDL-39507 questions: fix pluginfile URLs before format_text.
Browse files Browse the repository at this point in the history
This commit build's on Jean-Michel's work, tidying up a few lose ends.
  • Loading branch information
timhunt committed Aug 12, 2013
1 parent 6c23d0d commit e2b388c
Show file tree
Hide file tree
Showing 9 changed files with 89 additions and 29 deletions.
5 changes: 0 additions & 5 deletions lib/questionlib.php
Expand Up @@ -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
//===========================
Expand Down
8 changes: 2 additions & 6 deletions mod/quiz/editlib.php
Expand Up @@ -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)) {
Expand Down
16 changes: 16 additions & 0 deletions question/engine/lib.php
Expand Up @@ -818,6 +818,22 @@ public static function optional_param_mark($parname) {
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);
}
}


Expand Down
7 changes: 2 additions & 5 deletions question/format.php
Expand Up @@ -933,11 +933,8 @@ protected function writequestion($question) {
* 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);
}
}

Expand Down
8 changes: 4 additions & 4 deletions question/format/xhtml/format.php
Expand Up @@ -66,11 +66,11 @@ protected function writequestion($question) {
$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) {
Expand Down
58 changes: 58 additions & 0 deletions question/format/xhtml/lib.php
@@ -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);
}
5 changes: 2 additions & 3 deletions question/type/essay/question.php
Expand Up @@ -70,9 +70,8 @@ public function get_expected_data() {

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;
}
Expand Down
9 changes: 4 additions & 5 deletions question/type/multichoice/questiontype.php
Expand Up @@ -203,8 +203,8 @@ public function get_possible_responses($questiondata) {
$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);
}

Expand All @@ -214,9 +214,8 @@ public function get_possible_responses($questiondata) {
$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));
}

Expand Down
2 changes: 1 addition & 1 deletion question/type/questionbase.php
Expand Up @@ -333,7 +333,7 @@ public function format_text($text, $format, $qa, $component, $filearea, $itemid,
* @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. */
Expand Down

0 comments on commit e2b388c

Please sign in to comment.