Automatic installer.php lang files by installer_builder (20070726)
[moodle-linuxchix.git] / mod / quiz / quizfile.php
blob44d3b0f9348a74bcf81804613d3693004071ee48
1 <?PHP // $Id$
2 // This function fetches files from the data directory
3 // Syntax: quizfile.php/quiz id/question id/dir/.../dir/filename.ext
4 // It is supposed to be used by the quiz module only
5 // I believe this is obsolete, everything should be using moodle/file.php GWD
7 require_once('../../config.php');
8 require_once($CFG->libdir.'/filelib.php');
9 require_once('locallib.php');
11 if (empty($CFG->filelifetime)) {
12 $lifetime = 86400; // Seconds for files to remain in caches
13 } else {
14 $lifetime = $CFG->filelifetime;
17 // disable moodle specific debug messages
18 disable_debugging();
20 $relativepath = get_file_argument('quizfile.php');
22 if (!$relativepath) {
23 error('No valid arguments supplied or incorrect server configuration');
26 // extract relative path components
27 $args = explode('/', trim($relativepath, '/'));
28 if (count($args) < 3) { // always at least category, question and path
29 error('No valid arguments supplied');
32 $quizid = (int)array_shift($args);
33 $questionid = (int)array_shift($args);
34 $relativepath = implode ('/', $args);
36 if (!($question = get_record('question', 'id', $questionid))) {
37 error('No valid arguments supplied');
40 if (!($questioncategory = get_record('question_categories', 'id', $question->category))) {
41 error('No valid arguments supplied');
44 /////////////////////////////////////
45 // Check access
46 /////////////////////////////////////
47 if ($quizid == 0) { // teacher doing preview during quiz creation
48 if ($questioncategory->publish) {
49 require_login();
50 if (!isteacherinanycourse()) {
51 error('No valid arguments supplied');
53 } else {
54 require_login($questioncategory->course);
55 $cm = get_coursemodule_from_instance('quiz', $quizid);
56 require_capability('mod/quiz:preview', get_context_instance(CONTEXT_MODULE, $cm->id));
58 } else {
59 if (!($quiz = get_record('quiz', 'id', $quizid))) {
60 error('No valid arguments supplied');
62 if (!($course = get_record('course', 'id', $quiz->course))) {
63 error('No valid arguments supplied');
65 require_login($course->id);
67 // For now, let's not worry about this. The following check causes
68 // problems sometimes when reviewing a quiz
69 //if (!isteacher($course->id)
70 // and !quiz_get_user_attempt_unfinished($quiz->id, $USER->id)
71 // and ! ($quiz->review && time() > $quiz->timeclose)
72 // || !quiz_get_user_attempts($quiz->id, $USER->id) )
73 //{
74 // error("Logged-in user is not allowed to view this quiz");
75 //}
77 ///////////////////////////////////////////////////
78 // The logged-in user has the right to view material on this quiz!
79 // Now verify the consistency between $quiz, $question, its category and $relativepathname
80 ///////////////////////////////////////////////////
82 // For now, let's not worry about this. The following check doesn't
83 // work for randomly selected questions and it gets complicated
84 //if (!in_array($question->id, explode(',', $quiz->questions), FALSE)) {
85 // error("Specified question is not on the specified quiz");
86 //}
89 // Have the question check whether it uses this file or not
90 if (!$QTYPES[$question->qtype]->uses_quizfile($question,
91 $relativepath)) {
92 error("The specified file path is not on the specified question");
96 ///////////////////////////////////////////
97 // All security stuff is now taken care of.
98 // Specified file can now be returned...
99 //////////////////////////////////////////
101 $pathname = "$CFG->dataroot/$questioncategory->course/$relativepath";
102 $filename = $args[count($args)-1];
105 if (file_exists($pathname)) {
106 send_file($pathname, $filename, $lifetime);
107 } else {
108 header('HTTP/1.0 404 not found');
109 error(get_string('filenotfound', 'error')); //this is not displayed on IIS??