function FlightPath::save_advising_session_from_post

7.x FlightPath.php FlightPath::save_advising_session_from_post($faculty_id = 0, $bool_draft = true)
6.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 1293

Class

FlightPath

Code

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

  $catalog_year = 0;


  $bool_fp_goto_at_end = FALSE;


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

  $advising_session_token = $db->request_new_advising_session_token();


  // It's possible the user has simply pressed "refresh" after submitting the form.  If so,
  // there is no reason to re-submit everything, creating duplicate data in some situations.
  $post_md5 = md5(serialize($_POST));
  if (@$_SESSION ["fp_previous_advising_post_md5"] == $post_md5) {
    return array();
  }
  // We may proceed, but save the POST md5 for next time.
  $_SESSION ["fp_previous_advising_post_md5"] = $post_md5;


  $bool_found_update_match = false;
  $student_id = $this->student->student_id;
  $school_id = db_get_school_id_for_student_id($student_id);
  $degree_id = $this->degree_plan->degree_id;
  $major_code_csv = $this->degree_plan->get_major_code_csv();
  $catalog_year = $this->degree_plan->catalog_year;
  $available_terms = variable_get_for_school("available_advising_term_ids", "0", $school_id);

  // 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.");


  }

  //var_dump($_POST);
  //    die;
  // We have changed tracks, so we are to edit the student degrees table.
  if ($_POST ["advising_update_student_degrees_flag"] == "true") {
    // Begin by deleting all the "editable" rows for this student
    // in student_degrees.
    db_query("DELETE FROM student_degrees 
                WHERE student_id = '?'                
                AND is_editable = '1' ", $student_id);
    // Now, go through our list of degree tracks ids we're adding back in, and add to the table.
    $temp = explode(",", $_POST ["advising_track_degree_ids"]);

    foreach ($temp as $tdegree_id) {
      if (trim($tdegree_id) == "") {
        continue;
      }
      if (!is_numeric($tdegree_id)) {
        continue;
      }

      $tdegree_plan = fp_load_degree($tdegree_id, NULL, TRUE);

      $tmajor_code = $tdegree_plan->major_code;

      /*  // shouldn't need this anymore, as the major_code will contain both the major and track in one.
        $tmajor_code = $tdegree_plan->major_code;
        if (!strstr($tmajor_code, "|")) {
          $tmajor_code .= "|";
        }        
        $tmajor_code .= "_" . $tdegree_plan->track_code;
        */

      db_query("INSERT INTO student_degrees
                  (student_id, major_code, is_editable)
                  VALUES ('?', '?', '1')", $student_id, $tmajor_code);

    }

    // Reset the SESSION variables and re-init, so we get the correct
    // major codes and track codes for this student.      
    $_SESSION ["advising_track_degree_ids$student_id"] = "";
    $_SESSION ["advising_major_code$student_id"] = "";
    $_REQUEST ["advising_major_code"] = "";
    $_REQUEST ["advising_track_degree_ids"] = "";

    $this->student->load_student_data();

    // force re-build of cache
    //$_REQUEST["load_from_cache"] = "no"; 
    //$_SESSION["cache_fp$student_id"] = "";
    //$_SESSION["cache_what_if$student_id"] = "";
    $GLOBALS ["fp_advising"]["load_from_cache"] = "no";

    $this->init(TRUE);
    $bool_fp_goto_at_end = TRUE;
  } // editing degrees?




  // 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 advising_session_id.

  // 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 and faculty,
  // 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 faculty_id = ?
                  AND degree_id = ?
                  AND is_whatif = ? ", $student_id, $faculty_id, $degree_id, $is_what_if);


  // Set all other advising sessions' "most_recent_session" flag to zero (0) for this student (but only if this is NOT an "is_draft" save!)
  if ($is_draft === 0) {
    db_query("UPDATE advising_sessions SET most_recent_session = 0 WHERE student_id = ?", array($student_id));
  }


  // The first thing we need to do is go through the available_terms,
  // 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();
  $posted = time();
  $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_csv,
                catalog_year, posted, is_whatif, is_draft, advising_session_token, delete_flag, most_recent_session)
                VALUES
                (?,?,?,?,?,?,?,?,?,?, 0, 1) 
                ", $student_id, $faculty_id, $term_id, $degree_id, $major_code_csv, $catalog_year, $posted, $is_what_if, $is_draft, $advising_session_token);
    $advising_session_id = db_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_csv:$major_code_csv~adv_session_token:$advising_session_token");
  }
  else {
    watchdog("save_adv_active$wi", "$student_id,major_code_csv:$major_code_csv~adv_session_token:$advising_session_token");
  }

  // 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
  foreach ($_POST as $key => $value) 
   {

    if (!strstr($key, "advisecourse_") && !(strstr($key, "advcr_"))) 
     { // Skip vars which don't have this as part of the name.
      // We accept either advisecourse_ or advcr_ for short.  advisecourse_ is the old way.
      // I changed to use advcr_ to save space, because some browsers will not allow long input names.
      continue;
    }
    if ($value != "true") 
     { // This means the course was *not* advised to be taken,
      // so, skip it.
      continue;
    }

    // The key might contain a DoT (dot placeholder) instead of a period.  If so, let's
    // add the period back in.  This was to correct a bug where courses with dots couldn't
    // be advised.
    if (strstr($key, "DoT")) {
      $key = str_replace("DoT", ".", $key);
    }

    $temp = explode("_", $key);
    $course_id = trim($temp [1]);
    $semester_num = trim($temp [2]);
    $group_id = str_replace("U", "_", trim($temp [3])); // replace U with _, which was required for the submission to work (couldn't use the _ in group_id.)
    $var_hours = trim($temp [4]) * 1;
    $random_id = trim($temp [5]);
    $advised_term_id = trim($temp [6]);
    //$db_group_requirement_id = trim($temp[7]);
    $degree_id = trim($temp [7]);
    if ($degree_id == "group") {
      // Get degree_id from the group_id.
      $tt = explode("_", $group_id);
      $degree_id = $tt [1];
    }

    $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];
      $tsemester_num = $temp2 [2];
      $tvar_hours = $temp2 [3];
      $trandom_id = $temp2 [4];
      $tadvised_term_id = $temp2 [5];
      $tdegree_id = $temp2 [6];

      // 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;
        $degree_id = $tdegree_id;
        $new_course = new Course($tcourse_id);
        $new_course->load_descriptive_data();
        $entry_value = "$new_course->subject_id~$new_course->course_num";
        $bool_found_update_match = true;
      }


    }


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


    // Make sure degree_id is a valid number for the database.
    if (!is_numeric($degree_id) || intval($degree_id) < 0) {
      $degree_id = 0;
    }

    // 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`, `degree_id`)
                  VALUES
                  ('?','?','?','?','?','?','?','?')
                  ", $advising_session_id, $course_id, $entry_value, $semester_num, $group_id, $var_hours, $advised_term_id, $degree_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];
    $semester_num = $temp2 [2];
    $var_hours = $temp2 [3];
    $advised_term_id = $temp2 [5];
    $degree_id = $temp2 [6];

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

    $advising_session_id = $advising_session_id_array [$advised_term_id];

    $result = $db->db_query("INSERT INTO advised_courses
                  (`advising_session_id`,`course_id`,`entry_value`,`semester_num`,
                    `group_id`,`var_hours`,`term_id`,`degree_id`)
                  VALUES
                  ('?','?','?','?','?','?','?','?')
                  ", $advising_session_id, $course_id, $entry_value, $semester_num, $group_id, $var_hours, $advised_term_id, $degree_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 = trim($temp [1]);
    $req_by_degree_id = $temp [2];
    $semester_num = $temp [3] * 1;

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

    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->get_hours_awarded($req_by_degree_id))) {
        // Yes!  Set it to the value of the hours_awarded.
        $sub_hours = floatval($test_c->get_hours_awarded($req_by_degree_id));
      }
    }


    $result = $db->db_query("INSERT INTO student_substitutions
                  (`student_id`,`faculty_id`,`required_course_id`,`required_entry_value`,
                  `required_group_id`,`required_degree_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, $req_by_degree_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:" . db_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];
    $degree_id = $temp [4];

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

    watchdog("save_unassign_group", "$student_id,group_id:$group_id,degree_id:$degree_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));


  // Call a hook so other modules can act when the advising session gets saved.
  invoke_hook('save_advising_session_from_post', array($student_id, $is_draft, $advising_session_id_array));

  // Instead of executing the page, we will issue a redirect using fp_goto.
  // This makes it so if the user hits "refresh", it will not re-submit a POST request.
  if ($bool_fp_goto_at_end) {

    // Goto the same page we are already on.  This mimics Drupal's Form API behavior.  
    $q = $_REQUEST ['q'];
    fp_goto($q, "advising_major_code=&load_from_cache=no&advising_student_id=$student_id");

  }

  return $advising_session_id_array;


}