MDL-16094 File storage conversion Quiz and Questions
[moodle.git] / question / previewlib.php
CommitLineData
fe6ce234
DC
1<?php
2
3// This file is part of Moodle - http://moodle.org/
4//
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.
9//
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.
14//
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/>.
17
18/**
19 * Library functions used by question/preview.php.
20 *
21 * @package core
22 * @subpackage questionengine
23 * @copyright 2010 The Open University
24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 */
26
27/**
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.
30 *
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
38 */
39function 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');
43
44 if (!$question = $DB->get_record('question', array('id' => $questionid))) {
45 return send_file_not_found();
46 }
47
48 if (!question_has_capability_on($question, 'use', $question->category)) {
49 send_file_not_found();
50 }
51
52 if (!isset($SESSION->quizpreview->states) || $SESSION->quizpreview->questionid != $questionid) {
53 send_file_not_found();
54 }
55
56 $states = end($SESSION->quizpreview->states);
57 if (!array_key_exists($question->id, $states)) {
58 send_file_not_found();
59 }
60 $state = $states[$question->id];
61
62 // Build fake cmoptions
63 $quiz = new cmoptions;
64 $quiz->id = 0;
65 $quiz->review = get_config('quiz', 'review');
66 if (empty($course->id)) {
67 $quiz->course = SITEID;
68 } else {
69 $quiz->course = $course->id;
70 }
71 $quiz->decimalpoints = get_config('quiz', 'decimalpoints');
72
73 $questions[$question->id] = $question;
74 get_question_options($questions);
75
76 // Build fake attempt
77 $timenow = time();
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;
87 $attempt->id = 0;
88 $attempt->layout = $question->id;
89
90 $options = quiz_get_renderoptions($quiz, $attempt, $context, $state);
91 $options->noeditlink = true;
92 // XXX: mulitichoice type needs quiz id to get maxgrade
93 $options->quizid = 0;
94
95 if (!question_check_file_access($question, $state, $options, $context->id, $component,
96 $filearea, $args, $forcedownload)) {
97 send_file_not_found();
98 }
99
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();
105 }
106
107 send_stored_file($file, 0, 0, $forcedownload);
108}