moodle/mod/quiz/quizfile.php

110 lines
4.1 KiB
PHP
Raw Normal View History

<?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
Towards removing reference to quiz module from the question code Renaming tables: quiz_questions -> question quiz_states -> question_states Renaming functions: quiz_delete_question -> delete_question quiz_get_question_options -> get_question_options quiz_get_states -> get_question_states quiz_restore_state -> restore_question_state quiz_save_question_session -> save_question_session quiz_state_is_graded -> question_state_is_graded quiz_extract_responses -> question_extract_responses quiz_regrade_question_in_attempt -> regrade_question_in_attempt quiz_process_responses -> question_process_responses quiz_isgradingevent -> question_isgradingevent($event) quiz_search_for_duplicate_responses -> question_search_for_duplicate_responses quiz_apply_penalty_and_timelimit -> question_apply_penalty_and_timelimit quiz_print_question_icon -> print_question_icon quiz_get_image -> get_question_image quiz_make_name_prefix -> question_make_name_prefix quiz_get_id_from_name_prefix -> question_get_id_from_name_prefix quiz_new_attempt_uniqueid -> question_new_attempt_uniqueid quiz_get_renderoptions -> question_get_renderoptions quiz_print_quiz_question -> print_question quiz_get_question_responses -> get_question_responses quiz_get_question_actual_response -> get_question_actual_response quiz_get_question_fraction_grade -> get_question_fraction_grade quiz_get_default_category -> get_default_question_category Renaming constants: QUIZ_EVENT.... -> QUESTION_EVENT.... QUIZ_MAX_NUMBER_ANSWERS -> QUESTION_NUMANS
2006-02-28 09:26:00 +00:00
// I believe this is obsolete, everything should be using moodle/file.php GWD
require_once('../../config.php');
require_once($CFG->libdir.'/filelib.php');
require_once('locallib.php');
if (empty($CFG->filelifetime)) {
$lifetime = 86400; // Seconds for files to remain in caches
} else {
$lifetime = $CFG->filelifetime;
}
$relativepath = get_file_argument('quizfile.php');
if (!$relativepath) {
error('No valid arguments supplied or incorrect server configuration');
}
// extract relative path components
$args = explode('/', trim($relativepath, '/'));
if (count($args) < 3) { // always at least category, question and path
error('No valid arguments supplied');
}
$quizid = (int)array_shift($args);
$questionid = (int)array_shift($args);
$relativepath = implode ('/', $args);
Towards removing reference to quiz module from the question code Renaming tables: quiz_questions -> question quiz_states -> question_states Renaming functions: quiz_delete_question -> delete_question quiz_get_question_options -> get_question_options quiz_get_states -> get_question_states quiz_restore_state -> restore_question_state quiz_save_question_session -> save_question_session quiz_state_is_graded -> question_state_is_graded quiz_extract_responses -> question_extract_responses quiz_regrade_question_in_attempt -> regrade_question_in_attempt quiz_process_responses -> question_process_responses quiz_isgradingevent -> question_isgradingevent($event) quiz_search_for_duplicate_responses -> question_search_for_duplicate_responses quiz_apply_penalty_and_timelimit -> question_apply_penalty_and_timelimit quiz_print_question_icon -> print_question_icon quiz_get_image -> get_question_image quiz_make_name_prefix -> question_make_name_prefix quiz_get_id_from_name_prefix -> question_get_id_from_name_prefix quiz_new_attempt_uniqueid -> question_new_attempt_uniqueid quiz_get_renderoptions -> question_get_renderoptions quiz_print_quiz_question -> print_question quiz_get_question_responses -> get_question_responses quiz_get_question_actual_response -> get_question_actual_response quiz_get_question_fraction_grade -> get_question_fraction_grade quiz_get_default_category -> get_default_question_category Renaming constants: QUIZ_EVENT.... -> QUESTION_EVENT.... QUIZ_MAX_NUMBER_ANSWERS -> QUESTION_NUMANS
2006-02-28 09:26:00 +00:00
if (!($question = get_record('question', 'id', $questionid))) {
error('No valid arguments supplied');
}
if (!($questioncategory = get_record('question_categories', 'id', $question->category))) {
error('No valid arguments supplied');
}
/////////////////////////////////////
// Check access
/////////////////////////////////////
if ($quizid == 0) { // teacher doing preview during quiz creation
if ($questioncategory->publish) {
require_login();
if (!isteacherinanycourse()) {
error('No valid arguments supplied');
}
} else {
require_login($questioncategory->course);
if (!isteacher($questioncategory->course)) {
error('Access not allowed');
}
}
} else {
if (!($quiz = get_record('quiz', 'id', $quizid))) {
error('No valid arguments supplied');
}
if (!($course = get_record('course', 'id', $quiz->course))) {
error('No valid arguments supplied');
}
require_login($course->id);
// For now, let's not worry about this. The following check causes
// problems sometimes when reviewing a quiz
//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
///////////////////////////////////////////////////
// For now, let's not worry about this. The following check doesn't
// work for randomly selected questions and it gets complicated
//if (!in_array($question->id, explode(',', $quiz->questions), FALSE)) {
// error("Specified question is not on the specified quiz");
//}
}
// Have the question check whether it uses this file or not
2006-02-24 13:48:43 +00:00
if (!$QTYPES[$question->qtype]->uses_quizfile($question,
$relativepath)) {
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/$relativepath";
$filename = $args[count($args)-1];
if (file_exists($pathname)) {
send_file($pathname, $filename, $lifetime);
} else {
header('HTTP/1.0 404 not found');
error(get_string('filenotfound', 'error')); //this is not displayed on IIS??
}
?>