Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

The new quizfile.php makes it possible to view associated file materi…

…al on quizes, such as images, even for questions belonging to a category on a different course. lib.php is updated accordingly...
  • Loading branch information...
commit fe98ea90f9a0163eaa25cbbe2ea296fc38a45a14 1 parent ecb1b5e
kaipe authored
Showing with 147 additions and 31 deletions.
  1. +30 −31 mod/quiz/lib.php
  2. +117 −0 mod/quiz/quizfile.php
View
61 mod/quiz/lib.php
@@ -461,29 +461,40 @@ function quiz_print_question_icon($question, $editlink=true) {
}
}
+function quiz_print_possible_question_image($quizid, $question) {
+// Includes the question image is there is one
+ global $CFG;
+
+ if ($question->image) {
+ echo "<img border=\"0\" src=\"";
+
+ if (substr(strtolower($question->image), 0, 7) == "http://") {
+ echo $question->image;
+
+ } else if ($CFG->slasharguments) { // Use this method if possible for better caching
+ echo "$CFG->wwwroot/mod/quiz/quizfile.php/$quizid/$question->id/$question->image";
+
+ } else {
+ echo "$CFG->wwwroot/mod/quiz/quizfile.php?file=/$quizid/$question->id/$question->image";
+ }
+ echo "\" />";
+
+ }
+}
-function quiz_print_question($number, $question, $grade, $courseid,
+function quiz_print_question($number, $question, $grade, $quizid,
$feedback=NULL, $response=NULL, $actualgrade=NULL, $correct=NULL,
$realquestion=NULL, $shuffleanswers=false, $showgrades=true) {
/// Prints a quiz question, any format
/// $question is provided as an object
- if ($question->image) {
- if ($quizcategory = get_record("quiz_categories", "id", $question->category)) {
- $question->course = $quizcategory->course;
- } else {
- $question->course = $courseid;
- }
- }
if ($question->qtype == DESCRIPTION) { // Special case question - has no answers etc
echo '<p align="center">';
echo text_to_html($question->questiontext);
- if ($question->image) {
- print_file_picture($question->image, $question->course);
- }
+ quiz_print_possible_question_image($quizid, $question);
echo '</p>';
return true;
}
@@ -507,7 +518,7 @@ function quiz_print_question($number, $question, $grade, $courseid,
}
print_spacer(1,100);
- if ($question->recentlyadded) {
+ if (isset($question->recentlyadded) and $question->recentlyadded) {
echo "</td><td valign=top align=right>";
// Notify the user of this recently added question
echo '<font color="red">';
@@ -531,9 +542,7 @@ function quiz_print_question($number, $question, $grade, $courseid,
case SHORTANSWER:
case NUMERICAL:
echo text_to_html($question->questiontext);
- if ($question->image) {
- print_file_picture($question->image, $question->course);
- }
+ quiz_print_possible_question_image($quizid, $question);
if ($response) {
$value = "VALUE=\"$response[0]\"";
} else {
@@ -566,9 +575,7 @@ function quiz_print_question($number, $question, $grade, $courseid,
$false->answer = get_string("false", "quiz");
}
echo text_to_html($question->questiontext);
- if ($question->image) {
- print_file_picture($question->image, $question->course);
- }
+ quiz_print_possible_question_image($quizid, $question);
$truechecked = "";
$falsechecked = "";
@@ -611,9 +618,7 @@ function quiz_print_question($number, $question, $grade, $courseid,
notify("Error: Missing question answers!");
}
echo text_to_html($question->questiontext);
- if ($question->image) {
- print_file_picture($question->image, $question->course);
- }
+ quiz_print_possible_question_image($quizid, $question);
echo "<TABLE ALIGN=right>";
echo "<TR><TD valign=top>$stranswer:&nbsp;&nbsp;</TD><TD>";
echo "<TABLE>";
@@ -667,9 +672,7 @@ function quiz_print_question($number, $question, $grade, $courseid,
if (!empty($question->questiontext)) {
echo text_to_html($question->questiontext);
}
- if (!empty($question->image)) {
- print_file_picture($question->image, $question->course);
- }
+ quiz_print_possible_question_image($quizid, $question);
if ($shuffleanswers) {
$subquestions = draw_rand_array($subquestions, count($subquestions));
@@ -717,9 +720,7 @@ function quiz_print_question($number, $question, $grade, $courseid,
notify("Error: Missing question options!");
}
echo text_to_html($question->questiontext);
- if ($question->image) {
- print_file_picture($question->image, $question->course);
- }
+ quiz_print_possible_question_image($quizid, $question);
/// First, get all the questions available
@@ -801,9 +802,7 @@ function quiz_print_question($number, $question, $grade, $courseid,
case MULTIANSWER:
// For this question type, we better print the image on top:
- if ($question->image) {
- print_file_picture($question->image, $question->course);
- }
+ quiz_print_possible_question_image($quizid, $question);
$qtextremaining = text_to_html($question->questiontext);
// The regex will recognize text snippets of type {#X} where the X can be any text not containg } or white-space characters.
@@ -1012,7 +1011,7 @@ function quiz_print_quiz_questions($quiz, $results=NULL, $questions=NULL, $shuff
print_simple_box_start("CENTER", "90%");
- quiz_print_question($count, $question, $grades[$question->id]->grade, $quiz->course,
+ quiz_print_question($count, $question, $grades[$question->id]->grade, $quiz->id,
$feedback, $response, $actualgrades, $correct,
$randomquestion, $quiz->shuffleanswers, $quiz->grade);
print_simple_box_end();
View
117 mod/quiz/quizfile.php
@@ -0,0 +1,117 @@
+<?PHP // $Id$
+ // This function fetches files from the data directory
+ // Syntax: quizfile.php/quiz id/question id/dir/.../dir/filename.ext
+ // It is supposed to be used by the quiz module only
+
+ require_once("../../config.php");
+ require_once("../../files/mimetypes.php");
+ require_once("lib.php");
+
+ $lifetime = 86400;
+
+ if (isset($file)) { // workaround for situations where / syntax doesn't work
+ $pathinfo = $file;
+ } else {
+ $pathinfo = get_slash_arguments("file.php");
+ }
+
+ if (!$pathinfo) {
+ error("No file parameters!");
+ }
+
+ /////////////////////////////////////
+ // Extract info from $pathinfo
+ /////////////////////////////////////
+
+ $idreg = '[0-9]+';
+ if (!ereg("^/?($idreg)/($idreg)/((.+/)?([^/]+))$",
+ $pathinfo,
+ $regs) )
+ {
+ error("File parameters are badly formated");
+ }
+ if (! ($quiz = get_record('quiz', 'id', $regs[1]))) {
+ error("No valid quiz supplied");
+ }
+ if (! ($question = get_record('quiz_questions', 'id', $regs[2]))) {
+ error("No valid question supplied");
+ }
+ if (! ($relativefilepath = $regs[3])) {
+ error("No valid file path supplied");
+ }
+ if (! ($filename = $regs[5])) {
+ error("No valid file name supplied");
+ }
+
+ //////////////////////////////////////////
+ // Info from $pathinfo is now extracted!
+ // Now check the user's persmissions on this quiz...
+ //////////////////////////////////////////
+
+ if (! ($course = get_record("course", "id", $quiz->course))) {
+ error("Supplied quiz $quiz->name does not belong to a valid course");
+ }
+ require_login($course->id);
+ if (!isteacher($course->id)
+ and !quiz_get_user_attempt_unfinished($quiz->id, $USER->id)
+ and ! ($quiz->review && time() > $quiz->timeclose)
+ || !quiz_get_user_attempts($quiz->id, $USER->id) )
+ {
+ error("Logged-in user is not allowed to view this quiz");
+ }
+
+ ///////////////////////////////////////////////////
+ // The logged-in user has the right to view material on this quiz!
+ // Now verify the consistency between $quiz, $question, its category and $relativepathname
+ ///////////////////////////////////////////////////
+
+ if (!in_array($question->id, explode(',', $quiz->questions), FALSE)) {
+ error("Specified question is not on the specified quiz");
+ }
+ if (! ($questioncategory = get_record('quiz_categories', 'id',
+ $question->category)))
+ {
+ error("Question category is not valid");
+ }
+ // For the moment - questions can reference datafiles through image only
+ if (! ($question->image == $relativefilepath)) {
+ error("The specified file path is not on the specified question");
+ }
+
+
+ ///////////////////////////////////////////
+ // All security stuff is now taken care of.
+ // Specified file can now be returned...
+ //////////////////////////////////////////
+
+ $pathname = "$CFG->dataroot/$questioncategory->course/$relativefilepath";
+ // $filename has already been extracted
+
+
+ /////////////////////////////////////////////////////////////////
+ // The remaining code is identical to the final lines of file.php
+ // If you ask me - this stuff should be separated into a separate
+ // function for conviency.
+ // That function would find itself very in comfortable in the
+ // file mimetypes.php
+ //////////////////////////////////
+
+ $mimetype = mimeinfo("type", $filename);
+
+ if (file_exists($pathname)) {
+ $lastmodified = filemtime($pathname);
+
+ header("Last-Modified: " . gmdate("D, d M Y H:i:s", $lastmodified) . " GMT");
+ header("Expires: " . gmdate("D, d M Y H:i:s", time() + $lifetime) . " GMT");
+ header("Cache-control: max_age = $lifetime"); // a day
+ header("Pragma: ");
+ header("Content-disposition: inline; filename=$filename");
+ header("Content-length: ".filesize($pathname));
+ header("Content-type: $mimetype");
+ readfile("$pathname");
+ } else {
+ error("Sorry, but the file you are looking for was not found ($pathname)", "course/view.php?id=$courseid");
+ }
+
+ exit;
+?>
Please sign in to comment.
Something went wrong with that request. Please try again.