function course_search_download_syllabus
Search API
7.x course_search.module | course_search_download_syllabus() |
6.x course_search.module | course_search_download_syllabus() |
4.x course_search.module | course_search_download_syllabus() |
5.x course_search.module | course_search_download_syllabus() |
This function will actually deliver a syllabus to the user's browser for download.
File
- modules/
course_search/ course_search.module, line 255 - This module allows users to search for courses, descriptions, and, if supported, rotation schedules and sample syllabi.
Code
function course_search_download_syllabus() {
// Get our global variables...
$id = trim($_REQUEST ["id"]);
$db = get_global_database_handler();
$query = "select * from course_syllabi
where course_id = ? ";
$params = array($id);
$res = db_query($query, $params);
$cur = db_fetch_array($res);
$filename = $cur ["filename"];
$course_id = $cur ["course_id"];
$school_id = $db->get_school_id_for_course_id($course_id);
// Get the latest subject_id and course_num for this course!
$new_course = new Course();
$new_course->course_id = $course_id;
$new_course->school_id = $school_id;
$new_course->catalog_year = variable_get_for_school("current_catalog_year", 2006, $school_id);
$new_course->load_descriptive_data(false, false, true);
$subject_id = $new_course->subject_id;
$course_num = $new_course->course_num;
$files_path = $GLOBALS ["fp_system_settings"]["file_system_path"];
if ($filename == "" || !file_exists("$files_path/custom/files/syllabi/$filename"))
{ // Check to make sure the src file actually exists.
// Display a message, letting the user know it does not
// exist.
watchdog("syllabus", "fail,$course_id", array(), WATCHDOG_ERROR);
fp_add_message(t("Sorry, the syllabus for @course could not be found.", array("@course" => "$subject_id $course_num")));
// Just so admins can see:
fpm("Admin: file path attempted: $files_path/custom/files/syllabi/$filename");
return;
}
watchdog("syllabus", "get,$course_id");
$content_type = "application/plain"; // default, save as generic binary file.
$temp = explode("\.", $filename);
$ext = $temp [count($temp) - 1]; // get the original file extension.
// attempt to match to the correct content_type...
if ($ext == "pdf") {
$content_type = "application/pdf";
}
if ($ext == "doc") {
$content_type = "application/msword";
}
if ($ext == "docx") {
$content_type = "application/vnd.openxmlformats-officedocument.wordprocessingml.document";
}
if ($ext == "txt") {
$content_type = "text/plain";
}
if ($ext == "pot") {
$content_type = "application/mspowerpoint";
}
if ($ext == "ppt") {
$content_type = "application/powerpoint";
}
if (strstr($ext, "xl")) {
$content_type = "application/excel";
}
$fn = urlencode($subject_id . "_" . $course_num . "_SAMPLE_SYLLABUS") . ".$ext"; // make it a safe filename.
$fn = str_replace("+", " ", $fn); // restore spaces from + symbols...
// Give it to the browser!
header('Content-type: ' . $content_type . '');
header('Content-Disposition: attachment; filename="' . $fn . '"');
readfile("$files_path/custom/files/syllabi/" . $filename . "");
die;
}