function 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 224
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...

  $c = trim(mysql_real_escape_string($_REQUEST ["c"]));
  $id = trim(mysql_real_escape_string($_REQUEST ["id"]));

  $db = get_global_database_handler();

  $temp = explode("_", $c);
  $subject_id = $temp [0];
  $course_num = $temp [1];

  // Get the filename in question...
  $query = "select * from course_syllabi
            where course_perm_id = '$subject_id" . "_$course_num' ";
  if ($id != "") {
    $query = "select * from course_syllabi
            where course_id = '$id' ";
  }
  $res = db_query($query);
  $cur = db_fetch_array($res);
  $filename = $cur ["filename"];

  $course_id = $cur ["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->catalog_year = variable_get("current_catalog_year", 2006);
  $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,$c", 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,$c", array(), WATCHDOG_DEBUG);

  $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;
}