3 // This file is part of Moodle - http://moodle.org/
5 // Moodle is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
10 // Moodle is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
19 * Library functions used by question/preview.php.
22 * @subpackage questionengine
23 * @copyright 2010 The Open University
24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
28 * Called via pluginfile.php -> question_pluginfile to serve files belonging to
29 * a question in a question_attempt when that attempt is a preview.
31 * @param object $course course settings object
32 * @param object $context context object
33 * @param string $component the name of the component we are serving files for.
34 * @param string $filearea the name of the file area.
35 * @param array $args the remaining bits of the file path.
36 * @param bool $forcedownload whether the user must be forced to download the file.
37 * @return bool false if file not found, does not return if found - justsend the file
39 function question_preview_question_pluginfile($course, $context, $component,
40 $filearea, $attemptid, $questionid, $args, $forcedownload) {
41 global $USER, $SESSION, $DB, $CFG;
42 require_once($CFG->dirroot
. '/mod/quiz/locallib.php');
44 if (!$question = $DB->get_record('question', array('id' => $questionid))) {
45 return send_file_not_found();
48 if (!question_has_capability_on($question, 'use', $question->category
)) {
49 send_file_not_found();
52 if (!isset($SESSION->quizpreview
->states
) ||
$SESSION->quizpreview
->questionid
!= $questionid) {
53 send_file_not_found();
56 $states = end($SESSION->quizpreview
->states
);
57 if (!array_key_exists($question->id
, $states)) {
58 send_file_not_found();
60 $state = $states[$question->id
];
62 // Build fake cmoptions
63 $quiz = new cmoptions
;
65 $quiz->review
= get_config('quiz', 'review');
66 if (empty($course->id
)) {
67 $quiz->course
= SITEID
;
69 $quiz->course
= $course->id
;
71 $quiz->decimalpoints
= get_config('quiz', 'decimalpoints');
73 $questions[$question->id
] = $question;
74 get_question_options($questions);
78 $attempt = new stdClass();
79 $attempt->quiz
= $quiz->id
;
80 $attempt->userid
= $USER->id
;
81 $attempt->attempt
= 0;
82 $attempt->sumgrades
= 0;
83 $attempt->timestart
= $timenow;
84 $attempt->timefinish
= 0;
85 $attempt->timemodified
= $timenow;
86 $attempt->uniqueid
= 0;
88 $attempt->layout
= $question->id
;
90 $options = quiz_get_renderoptions($quiz, $attempt, $context, $state);
91 $options->noeditlink
= true;
92 // XXX: mulitichoice type needs quiz id to get maxgrade
95 if (!question_check_file_access($question, $state, $options, $context->id
, $component,
96 $filearea, $args, $forcedownload)) {
97 send_file_not_found();
100 $fs = get_file_storage();
101 $relativepath = implode('/', $args);
102 $fullpath = "/$context->id/$component/$filearea/$relativepath";
103 if (!$file = $fs->get_file_by_hash(sha1($fullpath)) or $file->is_directory()) {
104 send_file_not_found();
107 send_stored_file($file, 0, 0, $forcedownload);