From fe98ea90f9a0163eaa25cbbe2ea296fc38a45a14 Mon Sep 17 00:00:00 2001 From: kaipe Date: Sun, 2 Nov 2003 21:36:25 +0000 Subject: [PATCH] The new quizfile.php makes it possible to view associated file material on quizes, such as images, even for questions belonging to a category on a different course. lib.php is updated accordingly... --- mod/quiz/lib.php | 61 +++++++++++----------- mod/quiz/quizfile.php | 117 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 147 insertions(+), 31 deletions(-) create mode 100644 mod/quiz/quizfile.php diff --git a/mod/quiz/lib.php b/mod/quiz/lib.php index 4f5fc6c0f5..c5a2ed686e 100644 --- a/mod/quiz/lib.php +++ b/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 "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 '

'; echo text_to_html($question->questiontext); - if ($question->image) { - print_file_picture($question->image, $question->course); - } + quiz_print_possible_question_image($quizid, $question); echo '

'; 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 ""; // Notify the user of this recently added question echo ''; @@ -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 ""; echo "
$stranswer:  "; echo ""; @@ -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(); diff --git a/mod/quiz/quizfile.php b/mod/quiz/quizfile.php new file mode 100644 index 0000000000..830b00e27e --- /dev/null +++ b/mod/quiz/quizfile.php @@ -0,0 +1,117 @@ +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; +?> -- 2.39.5