function _FlightPath::save_advising_session_from_post

4.x _FlightPath.php _FlightPath::save_advising_session_from_post($faculty_id = 0, $bool_draft = true)
5.x _FlightPath.php _FlightPath::save_advising_session_from_post($faculty_id = 0, $bool_draft = true)
1 call to _FlightPath::save_advising_session_from_post()
_FlightPath::process_request_save_draft in classes/_FlightPath.php
This function will check to see if we are trying to save the draft from a tab change. It should be near the top of all of FP's "tab" pages, like Main, Comments, etc.

File

classes/_FlightPath.php, line 831

Class

_FlightPath

Code

function save_advising_session_from_post($faculty_id = 0, $bool_draft = true) 
 {
  global $user;


  // This method will, only by looking at variables in the
  // POST, save an advising session into the database.
  $db = get_global_database_handler();
  if ($faculty_id == 0) {
    // if none supplied, use the one from the session of
    // whomever is currently logged in.
    $faculty_id = $user->cwid;
  }

  $bool_found_update_match = false;
  $student_id = $this->student->student_id;
  $degree_id = $this->degree_plan->degree_id;
  $major_code = $this->degree_plan->major_code;
  $available_terms = variable_get("available_advising_term_ids", "0");


  // Do we need to update the student's settings?
  if (trim($_POST ["advising_update_student_settings_flag"]) != "") 
   {
    // We are to assume that the student's array_settings
    // have already been updated by this point, so we will
    // simply convert them to XML and store in the database.			
    $result = $db->db_query("REPLACE INTO student_settings
									(student_id, settings, posted)
									VALUES ('?','?', '?' )	", $student_id, serialize($this->student->array_settings), time());

    watchdog("update_student_settings", "Settings updated for this student.");


  }


  // Is there anything in "log_addition" which we should write to the log?
  if ($_POST ["log_addition"] != "") 
   {
    $temp = explode("~", $_POST ["log_addition"]);
    if ($temp [0] == "change_term") {
      watchdog("change_term", "$student_id," . $temp [1]);
    }

    if ($temp [0] == "change_track") {
      watchdog("change_track", "$student_id," . $temp [1]);
    }


  }




  // If this user cannot advise, then just return right now.
  if (!user_has_permission("can_advise_students")) {
    return;
  }


  // First, create a new entry in the advising_sessions table,
  // so we can get the advisingSessionID.

  // But before we can do that, we look for an existing entry
  // which matches this.  If we find it, we delete it so the
  // new one will display instead.
  // Only delete if its a draft copy!
  $is_draft = intval($bool_draft);
  $is_what_if = intval($this->bool_what_if);

  // Since we only want one draft copy per term/per student,
  // let's delete
  // any draft copies already in existence, if we are saving a draft.
  $result = $db->db_query("DELETE FROM advising_sessions
									WHERE `student_id`='?'
									AND `is_draft`='1'
									AND `degree_id`='?'
									AND `is_whatif`='?' ", $student_id, $degree_id, $is_what_if);


  // The first thing we need to do is go through the availableTerms,
  // create new entries for them in the table, and store what their
  // session ID's are in an array.
  $advising_session_id_array = array();
  $advising_session_id_array_count = array();

  $temp = explode(",", $available_terms);
  foreach ($temp as $term_id) 
   {
    $term_id = trim($term_id);

    if ($term_id == "") {
      continue;
    }

    // Okay, now create a new entry in the system for that term.
    // We create entries for all available terms, whether we
    // are going to use them later or not.
    $result = $db->db_query("INSERT INTO advising_sessions
								(student_id, faculty_id, term_id, degree_id,
								major_code,
								catalog_year, posted, is_whatif, is_draft)
								VALUES
								('?', '?','?','?','?','?','?','?','?') 
								", $student_id, $faculty_id, $term_id, $degree_id, $major_code, $catalog_year, time(), $is_what_if, $is_draft);
    $advising_session_id = mysql_insert_id();
    $advising_session_id_array [$term_id] = $advising_session_id;
    $advising_session_id_array_count [$term_id] = 0;
  }


  $wi = "";
  if ($is_what_if == "1") {
    $wi = "_whatif";
  }

  if ($bool_draft) {
    watchdog("save_adv_draft$wi", "$student_id,major_code:$major_code");
  }
  else {
    watchdog("save_adv_active$wi", "$student_id,major_code:$major_code");
  }

  // Go through the POST, looking for the
  // phrase "advisecourse_" in the name of the variables.
  // There should be one of these for every course that was
  // on the page.  It looks like this:
  // advisecourse_course_id_semesterNum_group_id_varHours_randomID
  //fpm($_POST);
  foreach ($_POST as $key => $value) 
   {
    if (!strstr($key, "advisecourse_")) 
     { // Skip vars which don't have this as part of the name.
      continue;
    }
    if ($value != "true") 
     { // This means the course was *not* advised to be taken,
      // so, skip it.
      continue;
    }

    $temp = explode("_", $key);
    $course_id = trim($temp [1]);
    $semester_num = trim($temp [2]);
    $group_id = trim($temp [3]);
    $var_hours = trim($temp [4]);
    $random_id = trim($temp [5]);
    $advised_term_id = trim($temp [6]);
    $db_group_requirement_id = trim($temp [7]);

    $advising_session_id = $advising_session_id_array [$advised_term_id];

    $new_course = new Course($course_id);
    $new_course->load_descriptive_data();
    $entry_value = "$new_course->subject_id~$new_course->course_num";



    // Some particular course should be updated.  Possibly this one.
    // Updates happen because of a student changing the
    // variable hours, for example.
    if (trim($_POST ["updatecourse"]) != "") 
     {
      $temp2 = explode("~", trim($_POST ["updatecourse"]));

      $tcourse_id = $temp2 [0];
      $tgroup_id = $temp2 [1] * 1;
      $tsemester_num = $temp2 [2] * 1;
      $tvar_hours = $temp2 [3];
      $trandom_id = $temp2 [4];
      $tadvised_term_id = $temp2 [5];

      // Do we have a match?
      if ($course_id == $tcourse_id && $random_id == $trandom_id) 
       {
        // We have a match, so update with the new information.
        $var_hours = $tvar_hours;
        $bool_found_update_match = true;
      }


    }


    if ($group_id != 0) 
     {
      $this->replace_missing_course_in_group($course_id, $group_id);
    }


    // Okay, write it to the table...
    $result = $db->db_query("INSERT INTO advised_courses
									(`advising_session_id`,`course_id`,
									`entry_value`,`semester_num`,
										`group_id`,`var_hours`,`term_id`)
									VALUES
									('?','?','?','?','?','?','?')
									", $advising_session_id, $course_id, $entry_value, $semester_num, $group_id, $var_hours, $advised_term_id);

    $advising_session_id_array_count [$advised_term_id];

  }

  // Did we have to perform an update-- but no course was found?
  if (++trim($_POST ["updatecourse"]) != "" && $bool_found_update_match == false) 
   {
    // This means that the course was probably on the bare
    // degree program, and not already checked for advising.  So,
    // let's add it to the advised_courses table, so it DOES
    // get checked for advising.
    $temp2 = explode("~", trim($_POST ["updatecourse"]));
    $course_id = $temp2 [0];
    $group_id = $temp2 [1] * 1;
    $semester_num = $temp2 [2] * 1;
    $var_hours = $temp2 [3];
    $advised_term_id = $temp2 [5];

    $advising_session_id = $advising_session_id_array [$advised_term_id];

    $result = $db->db_query("INSERT INTO advised_courses
									(`advising_session_id`,`course_id`,`semester_num`,
										`group_id`,`var_hours`,`term_id`)
									VALUES
									('?','?','?','?','?','?')
									", $advising_session_id, $course_id, $semester_num, $group_id, $var_hours, $advised_term_id);

    $advising_session_id_array_count [$advised_term_id];

    if (++$group_id != 0) 
     {
      $this->replace_missing_course_in_group($course_id, $group_id);
    }


  }



  //------------------------------------------------------
  //
  //             Substitutions...
  //
  //-------------------------------------------------------
  // check permissions for substitutions before saving
  if (trim($_POST ["savesubstitution"]) != "" && user_has_permission("can_substitute")) {
    $temp = explode("~", trim($_POST ["savesubstitution"]));
    $course_id = $temp [0]; // required course
    $group_id = $temp [1] * 1;
    $semester_num = $temp [2] * 1;

    $sub_course_id = $temp [3];
    $sub_term_id = $temp [4];
    $sub_transfer_flag = $temp [5];
    $sub_hours = $temp [6] * 1;
    $sub_addition = $temp [7];
    $sub_remarks = urldecode($temp [8]);

    if ($sub_addition == "true") 
     {
      $course_id = 0;
    }

    // Figure out the entry values for the required course & sub course...
    $required_entry_value = $sub_entry_value = "";
    if ($course_id > 0) 
     {
      $new_course = new Course($course_id);
      $new_course->load_descriptive_data();
      $required_entry_value = "$new_course->subject_id~$new_course->course_num";
    }

    if ($sub_transfer_flag != 1) 
     {
      $new_course = new Course($sub_course_id);
      $new_course->load_descriptive_data();
      $sub_entry_value = "$new_course->subject_id~$new_course->course_num";

    }

    if ($group_id != 0 && $course_id != 0) 
     {
      $this->replace_missing_course_in_group($course_id, $group_id);
    }


    // Make sure the sub_hours aren't larger than the sub_course_id's awarded hours.
    // This is to stop a bug from happening where sometimes, some people are able to substitute
    // a course for larger than the awarded hours.  I believe it is a javascript bug.      

    if ($test_c = $this->student->list_courses_taken->find_specific_course($sub_course_id, $sub_term_id, (bool) $sub_transfer_flag, true)) {
      // Are the hours out of whack?
      if (floatval($sub_hours) > floatval($test_c->hours_awarded)) {
        // Yes!  Set it to the value of the hours_awarded.
        $sub_hours = floatval($test_c->hours_awarded);
      }
    }


    $result = $db->db_query("INSERT INTO student_substitutions
									(`student_id`,`faculty_id`,`required_course_id`,`required_entry_value`,
									`required_group_id`,`required_semester_num`,`sub_course_id`,`sub_entry_value`,
									`sub_term_id`,`sub_transfer_flag`,`sub_hours`,`sub_remarks`,`posted`)
									VALUES
									('?','?','?','?','?','?','?','?','?','?','?','?','?')
									", $student_id, $faculty_id, $course_id, $required_entry_value, $group_id, $semester_num, $sub_course_id, $sub_entry_value, $sub_term_id, $sub_transfer_flag, $sub_hours, $sub_remarks, time());

    watchdog("save_substitution", "$student_id,group_id:$group_id,insert_id:" . mysql_insert_id());

  }


  if (trim($_POST ["removesubstitution"]) != "") 
   {
    $temp = explode("~", trim($_POST ["removesubstitution"]));
    $sub_id = trim($temp [0]) * 1;

    $result = $db->db_query("UPDATE student_substitutions
									SET `delete_flag`='1'
									WHERE `id`='?'	", $sub_id);

    watchdog("remove_substitution", "$student_id,sub_id:$sub_id");

  }



  //------------------------------------------------------
  //
  //             Group Unassignments
  //
  //-------------------------------------------------------
  if (trim($_POST ["unassign_group"]) != "") 
   {
    $temp = explode("~", trim($_POST ["unassign_group"]));
    $course_id = $temp [0];
    $term_id = $temp [1];
    $transfer_flag = $temp [2];
    $group_id = $temp [3];

    $result = $db->db_query("INSERT INTO student_unassign_group
									(`student_id`,`faculty_id`,`course_id`,
									`term_id`,`transfer_flag`,`group_id`,
									`posted`)
									VALUES
									('?','?','?','?','?','?','?')
									", $student_id, $faculty_id, $course_id, $term_id, $transfer_flag, $group_id, time());

    watchdog("save_unassign_group", "$student_id,group_id:$group_id");

  }

  if (trim($_POST ["restore_unassign_group"]) != "") 
   {
    $temp = explode("~", trim($_POST ["restore_unassign_group"]));
    $unassign_id = trim($temp [0]) * 1;


    $result = $db->db_query("UPDATE student_unassign_group
									SET `delete_flag`='1'
									WHERE `id`='?' ", $unassign_id);

    watchdog("restore_unassign_group", "$student_id,unassign_id:$unassign_id");

  }


  //------------------------------------------------------
  //
  //             Transfer EQV Unassignments
  //
  //-------------------------------------------------------
  if (trim($_POST ["unassign_transfer_eqv"]) != "") 
   {
    $temp = explode("~", trim($_POST ["unassign_transfer_eqv"]));
    $course_id = $temp [0];

    $result = $db->db_query("INSERT INTO student_unassign_transfer_eqv
									(`student_id`,`faculty_id`,`transfer_course_id`,
									`posted`)
									VALUES
									('?','?','?','?')
									", $student_id, $faculty_id, $course_id, time());

    watchdog("save_unassign_transfer", "$student_id,course_id:$course_id");

  }

  if (trim($_POST ["restore_transfer_eqv"]) != "") 
   {
    $temp = explode("~", trim($_POST ["restore_transfer_eqv"]));
    $unassign_id = trim($temp [0]) * 1;

    $result = $db->db_query("UPDATE student_unassign_transfer_eqv
									SET `delete_flag`='1'
									WHERE `id`='?' ", $unassign_id);

    watchdog("restore_unassign_transfer", "$student_id,unassign_id:$unassign_id");

  }



  ////////////////////////////////////////////////////
  ///////  Cleanup !////////////////////////////////
  ////////////////////////////////////////////////////
  // If any of the advisingSessions we created earlier
  // are blank, we should FLAG them, so they will not
  // show up under the student's history.
  // Only flag non-draft empty ones.  If they are draft,
  // let them be.
  // We just look at $advising_session_id_array_count[] to see
  // if any of the counts are still 0.  If they are, delete
  // that advisingSessionID from the table.
  if ($is_draft == 0) 
   {
    foreach ($advising_session_id_array as $term_id => $advising_session_id) 
     {
      if ($advising_session_id_array_count [$term_id] == 0) 
       {

        // This one is blank!  Delete it!
        $res = $db->db_query("UPDATE advising_sessions
								SET `is_empty`='1'	
								WHERE `advising_session_id`='?' ", $advising_session_id);
        $advising_session_id_array [$term_id] = "";
      }
    }
  }


  watchdog("advising", "Student has been advised: @student", array("@student" => $student_id));

  return $advising_session_id_array;


}