function admin_apply_draft_changes_form_submit

6.x admin.module admin_apply_draft_changes_form_submit($form, $form_submit)
4.x admin.module admin_apply_draft_changes_form_submit($form, $form_submit)
5.x admin.module admin_apply_draft_changes_form_submit($form, $form_submit)

Handles the actual moving of draft courses into production.

File

modules/admin/admin.module, line 1110
The administrative configurations for FlightPath.

Code

function admin_apply_draft_changes_form_submit($form, $form_submit) {
  $values = $form_submit ["values"];

  $db = get_global_database_handler();

  $de_catalog_year = admin_get_de_catalog_year();


  // Keep the script from timing out prematurely...
  set_time_limit(99999); // around 27 hours (so it runs a really long time).


  // First, set maintenance mode...
  variable_set("maintenance_mode", TRUE);

  // Okay, so what we gotta do is truncate the production tables,
  // then copy the draft tables in.
  $table_array = array(
    "courses",
    "degree_requirements",
    "degree_tracks",
    "degrees",
    "group_requirements",
    "groups",
  );


  foreach ($table_array as $table_name) {
    $draft_table_name = "draft_$table_name";
    // First, truncate existing...
    $query = "truncate table `$table_name`";
    $res = db_query($query);
    // Now, copy in draft changes...
    $query = "INSERT INTO `$table_name`
            SELECT * FROM `$draft_table_name` ";
    $res = db_query($query);
  }


  $db2 = new DatabaseHandler();
  // Now, we need to go through the draft_instructions table,
  // and perform each instruction one at a time.
  $res = db_query("SELECT * FROM draft_instructions
            ORDER BY `id` ");
  while ($cur = db_fetch_array($res)) 
   {
    $instruction = trim($cur ["instruction"]);

    $temp = explode(",", $instruction);

    if (trim($temp [0]) == "update_course_id") {
      $db2->update_course_id(trim($temp [1]), trim($temp [2]), trim($temp [3]));
    }

    if (trim($temp [0]) == "update_course_requirement_from_name") {
      $db2->update_course_requirement_from_name(trim($temp [1]), trim($temp [2]), trim($temp [3]), trim($temp [4]));
    }

    // TODO:  Maybe invoke another hook here, to let other modules act on the instruction?

  }

  // Once this is done, truncate the draft_instructions table.
  $res = db_query("TRUNCATE TABLE draft_instructions");


  // Invoke a hook to allow other modules to apply_draft_changes as well
  invoke_hook("apply_draft_changes");


  // Rebuild our course cache file.
  unset($_SESSION ['fp_cache_course_inventory_last_generated']);
  if (file_exists(fp_get_files_path() . "/cache_data/courses_serialized.info")) {
    $x = unlink(fp_get_files_path() . "/cache_data/courses_serialized.info");
    if (!$x) {
      fpm("Cannot delete cache_data/courses_serialized.info under custom/files.  Permission error or file does not exist?");
      watchdog("system", "Cannot delete cache_data/courses_serialized.info under custom/files.  Permission error or file does not exist?", array(), WATCHDOG_ERROR);
    }
  }
  variable_set('cache_course_inventory_last_generated', 0); // reset this so it will be regenerated.

  // Builds new courses cache file on the hard drive.
  system_reload_and_cache_course_inventory();

  // And we are done!  Set maintenance mode back to none
  variable_set("maintenance_mode", FALSE);

  // Send emails to notify programmers...
  $notify = variable_get("notify_apply_draft_changes_email_address", "");
  if ($notify != "") {
    mail($notify, "FlightPath Apply Draft Changes", "Someone has applied draft changes to FlightPath, which updated degree plans, groups, and courses.");
  }

  fp_add_message(t("Successfully updated the production database with draft changes.  Your changes are now live and visible on production for all users."));

  watchdog("admin", "Draft changes applied.");


}