function FlightPath::load_advising_session_from_database

7.x FlightPath.php FlightPath::load_advising_session_from_database($faculty_id = 0, $term_id = "", $bool_what_if = false, $bool_draft = true, $advising_session_id = 0, $duplicate_for_faculty_id = 0)
6.x FlightPath.php FlightPath::load_advising_session_from_database($faculty_id = 0, $term_id = "", $bool_what_if = false, $bool_draft = true, $advising_session_id = 0, $duplicate_for_faculty_id = 0)

File

classes/FlightPath.php, line 1856

Class

FlightPath

Code

function load_advising_session_from_database($faculty_id = 0, $term_id = "", $bool_what_if = false, $bool_draft = true, $advising_session_id = 0, $duplicate_for_faculty_id = 0) 
 {
  global $user;
  // This method will load an advising session for a particular
  // student, and modify the degree plan object to reflect
  // the advisings.

  $db = get_global_database_handler();
  $is_what_if = "0";
  $is_draft = "0";


  if ($bool_what_if == true) {
    $is_what_if = "1";
  }
  if ($bool_draft == true) {
    $is_draft = "1";
  }

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


  // If we are pulling up an active student record, then let's
  // delete any draft sessions for this faculty user and student, so as not to cause
  // a bug later on when changing tabs.  We don't care if $faculty_id is set or not, just
  // the current user's CWID.
  if ($bool_draft == FALSE && $user->cwid != 0) {
    $res = db_query("DELETE FROM advising_sessions
                        WHERE student_id = ?
                        AND faculty_id = ?
                        AND is_draft = 1
                         ", $student_id, $user->cwid);
  }







  $advising_session_line = " `advising_session_id`='$advising_session_id' ";
  // First, find the advising session id...
  if ($advising_session_id < 1 && $available_terms == "") 
   {
    $advising_session_id = $this->db->get_advising_session_id($faculty_id, $student_id, $term_id, $degree_id, $bool_what_if, $bool_draft, TRUE);
    $advising_session_line = " `advising_session_id`='$advising_session_id' ";

    // Create a duplicate of this session as a draft...
    if ($bool_draft == FALSE) {
      $db->duplicate_advising_session($advising_session_id, $duplicate_for_faculty_id, "", "", "", "", 1);
    }


  }
  else if ($advising_session_id < 1 && $available_terms != "") 
   {
    // Meaning, we are looking for more than one term.
    $advising_session_line = "(";
    $temp = explode(",", $available_terms);
    for ($t = 0; $t < count($temp); $t++) 
     {
      $t_id = trim($temp [$t]);

      $asid = $this->db->get_advising_session_id($faculty_id, $student_id, $t_id, $degree_id, $bool_what_if, $bool_draft, TRUE);
      if ($asid != 0) 
       {
        $advising_session_line .= " advising_session_id='$asid' || ";

        // Create a duplicate of this session as a draft...
        if ($bool_draft == FALSE) {
          $db->duplicate_advising_session($asid, $duplicate_for_faculty_id, "", "", "", "", 1);
        }


      }
    }
    // Take off the last 3 chars...
    $advising_session_line = substr($advising_session_line, 0, -3);
    $advising_session_line .= ")";
    if ($advising_session_line == ")") 
     { // Found NO previously advised semesters, so just
      // use a dummy value which guarantees it pulls up nothing.
      $advising_session_line = " advising_session_id='-99999'";
    }

  }

  // Now, look up the courses they were advised to take.
  $query = "SELECT * FROM advised_courses
                WHERE 
                 $advising_session_line
                ORDER BY `id` ";
  $result = $db->db_query($query);
  while ($cur = $db->db_fetch_array($result)) 
   {
    $course_id = trim($cur ["course_id"]);
    $semester_num = trim($cur ["semester_num"]);
    $group_id = trim($cur ["group_id"]);
    $degree_id = $cur ["degree_id"];
    $var_hours = trim($cur ["var_hours"]);
    $advised_term_id = trim($cur ["term_id"]);
    $id = trim($cur ["id"]);

    // Add this course to the generic list of advised courses.  Useful
    // if we are using this to pull up an advising summary.
    $temp_course = new Course($course_id);
    $temp_course->advised_hours = $var_hours;
    $this->course_list_advised_courses->add($temp_course);

    if ($semester_num == DegreePlan::SEMESTER_NUM_FOR_COURSES_ADDED) 
     {
      // This was a courses added by the advisor.
      $this->assign_course_to_courses_added_list($course_id, $var_hours, $id, $advised_term_id);
      continue;
    }

    // Now, we need to modify the degree_plan object to
    // show these advisings.
    if ($course_list = $this->degree_plan->find_courses($course_id, $group_id, $semester_num, $degree_id)) 
     {

      // This course may exist in several different branches of a group, so we need
      // to mark all the branches as having been advised to take.  Usually, this CourseList
      // will probably only have 1 course object in it.  But, better safe than sorry.
      $course_list->reset_counter();
      if ($course = $course_list->get_next()) 
       {
        // make sure the hour count has been loaded correctly.
        if ($course->get_catalog_hours() < 1) 
         {
          $course->load_descriptive_data();
        }

        // Let's start by looking at the first course.  Is it
        // supposed to be repeated?
        if ($course->bool_specified_repeat == true
         && $course->specified_repeats >= 0) 
         {
          // This is a course which is supposed to be repeated.
          // We need to cycle through and find an instance
          // of this course which has not been advised yet.


          $course_list->reset_counter();
          while ($course_list->has_more()) 
           {
            $course = $course_list->get_next();

            // make sure the hour count has been loaded correctly.
            if ($course->get_catalog_hours() < 1) 
             {
              $course->load_descriptive_data();
            }


            ////////////////////
            // Logan's change: https://bytetask.com/node/2455

            // If we have the setting which says we should skip if it's already been completed/enrolled for this term,
            // then we should do that.
            if (variable_get_for_school("remove_advised_when_course_taken", "no", $school_id) == "yes") {
              // First, see if this advised course has been attempted already.
              if ($this->student->list_courses_taken->find_specific_course($course->course_id, $advised_term_id)) {
                // Yep, found it!  So, skip this one.
                continue;
              }
            }

            //////////////////////////



            //if ($course->bool_advised_to_take != true && !is_object($course->courseFulfilledBy))
            if ($course->bool_advised_to_take != true && $course->course_list_fulfilled_by->is_empty == true) 
             {
              // Okay, this course is supposed to be taken/advised
              // more than once.  So, I will mark this one as
              // advised, and then break out of the loop, since
              // I don't want to mark all occurances as advised.
              $course->bool_advised_to_take = true;
              $course->assigned_to_semester_num = $semester_num;
              //$course->assigned_to_group_id = $group_id;                
              $course->assigned_to_group_ids_array [$group_id] = $group_id;

              // Make sure we assign the hours to the group, so this
              // advised courses takes up a spot in the group.  Otherwise
              // it may be missed in later logic.
              if ($g = $this->degree_plan->find_group($group_id)) {
                $h = $var_hours;
                if ($h == 0) {
                  $h = $course->get_catalog_hours();
                  if ($h == 0) {
                    $h = 1; // some problem occured. Just give it a token hour so it doesn't
                    // horribly break.
                  }
                }
                $g->hours_assigned += $h;
              }


              $course->advised_hours = $var_hours;
              $course->advised_term_id = $advised_term_id;
              $course->db_advised_courses_id = $id;
              $course_list->dec_specified_repeats($course);
              break;
            }
          }
          continue; // Go to the next advised course.
        }

      } // if $course = $course_list->get_next();

      //////////////////////////////
      // We're here, because it was not a repeatable course.
      // ** We should only go through THIS loop once!  So,
      // we will break after we make our assignment.
      $course_list->reset_counter();
      while ($course_list->has_more()) 
       {

        $course = $course_list->get_next();
        // make sure the hour count has been loaded correctly.
        if ($course->get_catalog_hours() < 1) 
         {
          $course->load_descriptive_data();
        }

        // make sure it has not already been advised to take.
        // Would occur if the same course is specified more
        // than once in a semester.
        if ($course->bool_advised_to_take == true) 
         {
          continue;
        }

        // If we have the setting which says we should skip if it's already been completed/enrolled for this term,
        // then we should do that.
        if (variable_get_for_school("remove_advised_when_course_taken", "no", $school_id) == "yes") {
          // First, see if this advised course has been attempted already.            
          if ($taken_course = $this->student->list_courses_taken->find_specific_course($course->course_id, $advised_term_id)) {
            // Yep, found it!  So, skip this one.              
            continue;
          }
        }


        // Has this course already been fulfilled by something?
        // If so, we cannot attempt to say it's been advised!
        if (!$course->course_list_fulfilled_by->is_empty) 
         {
          // meaning, this course HAS been fulfilled.
          // So, let's move this advising to the "added by advisor"
          // spot.

          /*  This is the original bit of code here.  It is causing a problem when there are courses which are supposed to be
             * repeated.  Example: MUSC courses which are advised one term at a time.
             * 
             * 
            $this->assign_course_to_courses_added_list($course_id, $var_hours, $id, $advised_term_id);            
            break;
             * 
             * Strategy:  Find out how many courses are in this course_list by looking at the size of the list.  If it's > 1, then just continue.  If it == 1, then
             * do the original logic.
             * 
            */

          if ($course_list->get_size() > 1) {
            continue;
          }
          else {
            $this->assign_course_to_courses_added_list($course_id, $var_hours, $id, $advised_term_id);
            break;
          }


        }


        $course->bool_advised_to_take = true;
        $course->assigned_to_semester_num = $semester_num;
        //$course->assigned_to_group_id = $group_id;
        $course->assigned_to_group_ids_array [$group_id] = $group_id;

        // Make sure we assign the hours to the group, so this
        // advised courses takes up a spot in the group.  Otherwise
        // it may be missed in later logic.
        if ($g = $this->degree_plan->find_group($group_id)) {
          $h = $var_hours;
          if ($h == 0) {
            $h = $course->get_catalog_hours();
            if ($h == 0) {
              $h = 1; // some problem occured. Just give it a token hour so it doesn't
              // horribly break.
            }
          }
          $g->hours_assigned += $h;

        }

        $course->advised_hours = $var_hours;
        $course->advised_term_id = $advised_term_id;
        $course->db_advised_courses_id = $id;
        if ($course->required_on_branch_id > 0) 
         {
          // In other words, this course was found on a branch, so we need
          // to increment that branch's count_of_matches.
          if ($branch = $this->degree_plan->find_group($course->required_on_branch_id)) 
           {
            $branch->count_of_matches++;
          }
          else {
            fpm("Error: Could not find branch.");
          }

        }

        // We should only be in this loop once, so let's
        // break after we make our assignment.
        break;

      }

    }

  }

  // Now, what we need to do is tell the DegreePlan to re-sort its
  // group's course lists, so that the advised courses are lower
  // than the fulfilled courses.

  //$this->degree_plan->sortGroupsFulfilledFirst();
  //print_pre($this->degree_plan->list_groups->toString());


}