Skip to content

Commit

Permalink
MDL-30592 quiz statistics, ensure images in the question text appear.
Browse files Browse the repository at this point in the history
  • Loading branch information
timhunt committed Dec 9, 2011
1 parent 8c03be8 commit fdb5bc0
Show file tree
Hide file tree
Showing 3 changed files with 125 additions and 27 deletions.
76 changes: 55 additions & 21 deletions lib/questionlib.php
Expand Up @@ -1636,10 +1636,7 @@ public function require_one_edit_tab_cap($tabname) {
}

/**
* Rewrite question url, file_rewrite_pluginfile_urls always build url by
* $file/$contextid/$component/$filearea/$itemid/$pathname_in_text, so we cannot add
* extra questionid and attempted in url by it, so we create quiz_rewrite_question_urls
* to build url here
* Helps call file_rewrite_pluginfile_urls with the right parameters.
*
* @param string $text text being processed
* @param string $file the php script used to serve files
Expand All @@ -1653,32 +1650,59 @@ public function require_one_edit_tab_cap($tabname) {
*/
function question_rewrite_question_urls($text, $file, $contextid, $component,
$filearea, array $ids, $itemid, array $options=null) {
global $CFG;

$options = (array)$options;
if (!isset($options['forcehttps'])) {
$options['forcehttps'] = false;
$idsstr = '';
if (!empty($ids)) {
$idsstr .= implode('/', $ids);
}

if (!$CFG->slasharguments) {
$file = $file . '?file=';
if ($itemid !== null) {
$idsstr .= '/' . $itemid;
}
return file_rewrite_pluginfile_urls($text, $file, $contextid, $component,
$filearea, $idsstr, $options);
}

$baseurl = "$CFG->wwwroot/$file/$contextid/$component/$filearea/";
/**
* Rewrite the PLUGINFILE urls in the questiontext, when viewing the question
* text outside and attempt (for example, in the question bank listing or in the
* quiz statistics report).
*
* @param string $questiontext the question text.
* @param int $contextid the context the text is being displayed in.
* @param string $component component
* @param array $ids other IDs will be used to check file permission
* @param array $options
* @return string $questiontext with URLs rewritten.
*/
function question_rewrite_questiontext_preview_urls($questiontext, $contextid,
$component, $questionid, $options=null) {

if (!empty($ids)) {
$baseurl .= (implode('/', $ids) . '/');
}
return file_rewrite_pluginfile_urls($questiontext, 'pluginfile.php', $contextid,
'question', 'questiontext_preview', "$component/$questionid", $options);
}

if ($itemid !== null) {
$baseurl .= "$itemid/";
}
/**
* Send a file from the question text of a question.
* @param int $questionid the question id
* @param array $args the remaining file arguments (file path).
* @param bool $forcedownload whether the user must be forced to download the file.
*/
function question_send_questiontext_file($questionid, $args, $forcedownload) {
global $DB;

$question = $DB->get_record_sql('
SELECT q.id, qc.contextid
FROM {question} q
JOIN {question_categories} qc ON qc.id = q.category
WHERE q.id = :id', array('id' => $questionid), MUST_EXIST);

if ($options['forcehttps']) {
$baseurl = str_replace('http://', 'https://', $baseurl);
$fs = get_file_storage();
$fullpath = "/$question->contextid/question/questiontext/$question->id/" . implode('/', $args);
if (!$file = $fs->get_file_by_hash(sha1($fullpath)) or $file->is_directory()) {
send_file_not_found();
}

return str_replace('@@PLUGINFILE@@/', $baseurl, $text);
send_stored_file($file, 0, 0, $forcedownload);
}

/**
Expand All @@ -1704,6 +1728,16 @@ function question_rewrite_question_urls($text, $file, $contextid, $component,
function question_pluginfile($course, $context, $component, $filearea, $args, $forcedownload) {
global $DB, $CFG;

if ($filearea === 'questiontext_preview') {
$component = array_shift($args);
$questionid = array_shift($args);

component_callback($component, 'questiontext_preview_pluginfile', array(
$context, $questionid, $args, $forcedownload));

send_file_not_found();
}

list($context, $course, $cm) = get_context_info_array($context->id);
require_login($course, false, $cm);

Expand Down
46 changes: 46 additions & 0 deletions mod/quiz/report/statistics/lib.php
@@ -0,0 +1,46 @@
<?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 quiz statistics report.
*
* @package quiz
* @subpackage statistics
* @copyright 2011 The Open University
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/


/**
* Serve questiontext files in the question text when they are displayed in this report.
* @param context $context the context
* @param int $questionid the question id
* @param array $args remaining file args
* @param bool $forcedownload
*/
function quiz_statistics_questiontext_preview_pluginfile($context, $questionid, $args, $forcedownload) {
global $CFG;
require_once($CFG->dirroot . '/mod/quiz/locallib.php');

list($context, $course, $cm) = get_context_info_array($context->id);
require_login($course, false, $cm);

// Assume only trusted people can see this report. There is no real way to
// validate questionid, becuase of the complexity of random quetsions.
require_capability('quiz/statistics:view', $context);

question_send_questiontext_file($questionid, $args, $forcedownload);
}
30 changes: 24 additions & 6 deletions mod/quiz/report/statistics/report.php
Expand Up @@ -54,7 +54,7 @@ class quiz_statistics_report extends quiz_default_report {
public function display($quiz, $cm, $course) {
global $CFG, $DB, $OUTPUT, $PAGE;

$context = get_context_instance(CONTEXT_MODULE, $cm->id);
$this->context = get_context_instance(CONTEXT_MODULE, $cm->id);

// Work out the display options.
$download = optional_param('download', '', PARAM_ALPHA);
Expand Down Expand Up @@ -94,7 +94,7 @@ public function display($quiz, $cm, $course) {

} else {
// All users who can attempt quizzes and who are in the currently selected group
$groupstudents = get_users_by_capability($context,
$groupstudents = get_users_by_capability($this->context,
array('mod/quiz:reviewmyattempts', 'mod/quiz:attempt'),
'', '', '', '', $currentgroup, '', false);
if (!$groupstudents) {
Expand Down Expand Up @@ -160,7 +160,7 @@ public function display($quiz, $cm, $course) {
}

if (!quiz_questions_in_quiz($quiz->questions)) {
echo quiz_no_questions_message($quiz, $cm, $context);
echo quiz_no_questions_message($quiz, $cm, $this->context);
} else if (!$this->table->is_downloading() && $s == 0) {
echo $OUTPUT->notification(get_string('noattempts', 'quiz'));
}
Expand Down Expand Up @@ -317,15 +317,33 @@ protected function output_individual_question_data($quiz, $question) {
echo $OUTPUT->heading(get_string('questionstatistics', 'quiz_statistics'));
echo html_writer::table($questionstatstable);
}
public function format_text($text, $format, $qa, $component, $filearea, $itemid,
$clean = false) {
$formatoptions = new stdClass();
$formatoptions->noclean = !$clean;
$formatoptions->para = false;
$text = $qa->rewrite_pluginfile_urls($text, $component, $filearea, $itemid);
return format_text($text, $format, $formatoptions);
}

/** @return the result of applying {@link format_text()} to the question text. */
public function format_questiontext($qa) {
return $this->format_text($this->questiontext, $this->questiontextformat,
$qa, 'question', 'questiontext', $this->id);
}

/**
* @param object $question question data.
* @return string HTML of question text, ready for display.
*/
protected function render_question_text($question){
protected function render_question_text($question) {
global $OUTPUT;
return $OUTPUT->box(format_text($question->questiontext, $question->questiontextformat,
array('overflowdiv' => true)),

$text = question_rewrite_questiontext_preview_urls($question->questiontext,
$this->context->id, 'quiz_statistics', $question->id);

return $OUTPUT->box(format_text($text, $question->questiontextformat,
array('noclean' => true, 'para' => false, 'overflowdiv' => true)),
'questiontext boxaligncenter generalbox boxwidthnormal mdl-align');
}

Expand Down

0 comments on commit fdb5bc0

Please sign in to comment.