From 0019a9b7aa9afae3053d1b0ef61eb80a44acfffe Mon Sep 17 00:00:00 2001
From: Tim Hunt <T.J.Hunt@open.ac.uk>
Date: Mon, 5 Dec 2011 19:07:12 +0000
Subject: [PATCH] MDL-30592 question_bank: make files in qtext work in the
 question list.

---
 lib/questionlib.php  | 27 +++++++++++++++++++++++++++
 question/editlib.php | 16 +++++++++++-----
 2 files changed, 38 insertions(+), 5 deletions(-)

diff --git a/lib/questionlib.php b/lib/questionlib.php
index 06c68712041..2b3b6aca5f7 100644
--- a/lib/questionlib.php
+++ b/lib/questionlib.php
@@ -1827,6 +1827,33 @@ function question_pluginfile($course, $context, $component, $filearea, $args, $f
     }
 }
 
+/**
+ * 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 core_question_questiontext_preview_pluginfile($context, $questionid, $args, $forcedownload) {
+    global $DB;
+
+    // Verify that contextid matches the question.
+    $question = $DB->get_record_sql('
+            SELECT q.*, qc.contextid
+              FROM {question} q
+              JOIN {question_categories} qc ON qc.id = q.category
+             WHERE q.id = :id AND qc.contextid = :contextid',
+            array('id' => $questionid, 'contextid' => $context->id), MUST_EXIST);
+
+    // Check the capability.
+    list($context, $course, $cm) = get_context_info_array($context->id);
+    require_login($course, false, $cm);
+
+    question_require_capability_on($question, 'use');
+
+    question_send_questiontext_file($questionid, $args, $forcedownload);
+}
+
 /**
  * Create url for question export
  *
diff --git a/question/editlib.php b/question/editlib.php
index 2b541a6306c..5f2487e1e09 100644
--- a/question/editlib.php
+++ b/question/editlib.php
@@ -791,16 +791,22 @@ class question_bank_question_text_row extends question_bank_row_base {
     }
 
     protected function display_content($question, $rowclasses) {
-        $text = format_text($question->questiontext, $question->questiontextformat,
-                $this->formatoptions, $this->qbank->get_courseid());
+        $text = question_rewrite_questiontext_preview_urls($question->questiontext,
+                $question->contextid, 'question', $question->id);
+        $text = format_text($text, $question->questiontextformat,
+                $this->formatoptions);
         if ($text == '') {
             $text = '&#160;';
         }
         echo $text;
     }
 
+    public function get_extra_joins() {
+        return array('qc' => 'JOIN {question_categories} qc ON qc.id = q.category');
+    }
+
     public function get_required_fields() {
-        return array('q.questiontext', 'q.questiontextformat');
+        return array('q.id', 'q.questiontext', 'q.questiontextformat', 'qc.contextid');
     }
 }
 
@@ -1100,10 +1106,10 @@ class question_bank_view {
         }
 
     /// Build the where clause.
-        $tests = array('parent = 0');
+        $tests = array('q.parent = 0');
 
         if (!$showhidden) {
-            $tests[] = 'hidden = 0';
+            $tests[] = 'q.hidden = 0';
         }
 
         if ($recurse) {