function prereqs_get_prereq_warnings_for_course

7.x prereqs.module prereqs_get_prereq_warnings_for_course(Course $course, Student $student = null)
6.x prereqs.module prereqs_get_prereq_warnings_for_course(Course $course, Student $student = null)

This is meant to be a general function to find all relavent prereq warnings for the supplied course.

It will also call a hook to allow other modules to add to it.

Returns an array of warning messages, or an empty array if the course is good to go.

2 calls to prereqs_get_prereq_warnings_for_course()
prereqs_content_alter in modules/prereqs/prereqs.module
Implememnt hook_content_alter
prereqs_theme_advise_course_row in modules/prereqs/prereqs.module
Implements hook_theme_advise_course_row. Lets us re-write how our course rows are drawn, if desired.

File

modules/prereqs/prereqs.module, line 466
This is the module file for the prereqs module.

Code

function prereqs_get_prereq_warnings_for_course(Course $course, Student $student = null) {

  $rtn = array();



  $school_id = $course->school_id;

  $bool_use_term_abbreviation = (variable_get("prereqs_use_term_desc_abbr_in_popup", "yes") == "yes");

  ///////////////////////////////////////////////////
  ///////////////////////////////////////////////////
  // Advising Term Based Locks
  // Should we lock this course if its unavailable for this advising term?
  if (variable_get("prereqs_lock_course_advise_based_on_avail", "no") == "yes") {

    $warning_msg = variable_get("prereqs_lock_msg_avail", "This course is not listed as being available for the selected advising term (@term).  Are you sure you wish to advise the student to enroll in it?");

    // Find out what the rotation schedule is for this course.
    $advising_term_id = @$GLOBALS ["fp_advising"]["advising_term_id"];
    // Get the advising year from the term.
    $year = substr($advising_term_id, 0, 4);
    $rotation_schedule = course_search_get_course_rotation_schedule($course->course_id, $year, 5);

    // Is the "not anticipated" flag set?
    $bool_not_anticipated = course_search_get_course_rotation_schedule_not_anticipated($course->course_id);
    if ($bool_not_anticipated) {
      // Clear out the rotation_schedule, since it isn't anticipated.
      $rotation_schedule = array();

    }


    if (!in_array($advising_term_id, $rotation_schedule)) {
      // The course is NOT listed for this advising term!  Therefor, it SHOULD have a warning!

      $rtn ["avail"]["plain_text"] = t($warning_msg, array("@term" => htmlentities(get_term_description($advising_term_id, $bool_use_term_abbreviation, $school_id), ENT_QUOTES)));

    } // !in_array advising_term_id in rotation_schedule






  } // advising availbility / term based locks

  /////////////////////////////////////////
  /////////////////////////////////////////
  //  Previous course requirement prereq locks

  if (variable_get("prereqs_lock_course_advise_based_on_courses", "no") == "yes" && $student != null && is_object($student)) {

    // Let's get the prereqs array for this course.
    $prereqs = prereqs_get_prereq_array_for_course($course->course_id);

    $retake_grades = csv_to_array(variable_get_for_school("retake_grades", "W,F", $course->school_id));

    if (count($prereqs) > 0) {

      $bool_fulfilled_prereqs = TRUE;
      $completed_courses = array();
      $completed_branches = array();

      foreach ($prereqs as $branch_num => $reqs) {

        $bool_fulfilled_branch = FALSE;
        // Loop through and see if the student HAS fulfilled the specified prereq.  
        foreach ($reqs as $details) {
          $bool_found_course = FALSE;
          $test_course = new Course();
          $test_course->course_id = $details ["course_id"];
          $found = $student->list_courses_taken->find_best_match($test_course, $details ["min_grade"], FALSE, 0, FALSE);

          if ($found && is_object($found) && !in_array($found->grade, $retake_grades)) {
            // YEP!  They have this requirement, so that means the entire branch is good.
            $bool_found_course = TRUE;

          } // was found.
          else {
            // NOT FOUND!

            // Perhaps this course ($test_course) is being fulfilled by a substitution?
            $found_sub = $student->list_substitutions->find_requirement($test_course);
            if ($found_sub) {
              $found = $found_sub->course_requirement;
              // Yes, there is at least ONE substitution for this course.  We will count it as completed the prereq, even if it does
              // not complete all of the hours.

              // TODO:  Make that also check completed hours to make sure the sub is "complete" and not split up?
              $bool_found_course = TRUE;
            }
            else {
              // Nope, this was not a substitution.  But perhaps the student has an automatic transfer eqv for this course?

            }



          } // if/else

          if ($bool_found_course) {
            $bool_fulfilled_branch = TRUE;
            $completed_branches [$branch_num] = $branch_num;
            $completed_courses [$found->course_id . "~" . $details ["min_grade"]] = $found->course_id . "~" . $details ["min_grade"];
          }


        } // foreach reqs 

        // If we didn't fulfill a branch, then we have failed.  We should quit.
        if ($bool_fulfilled_branch == FALSE) {
          $bool_fulfilled_prereqs = FALSE;
        }

      }
      // foreach prereqs


      if ($bool_fulfilled_prereqs == FALSE) {
        // We did NOT fulfill the prereqs for this course!  Let's add a message!

        // Prepare our prereq requirements for display...
        $prereq_data = prereqs_get_prereq_string_for_course($course->course_id);

        // Fix trouble characters, and reformat so it looks pretty.
        $prereq_data = "  - " . str_replace("\n", "\\n  - ", $prereq_data);



        $warning_msg = variable_get("prereqs_lock_msg_courses", "This course is not listed as being available because the student has not fulfilled the following prerequisite course(s) and possible min grades:\\n@need_courses.\\nAre you sure you wish to advise the student to enroll in this course anyway?");

        $rtn ["prereq"]["plain_text"] = t($warning_msg, array("@need_courses" => $prereq_data));

        // Let's also get the full-html version, where we indicate what we've completed or not.
        $html = "";
        foreach ($prereqs as $branch_num => $reqs) {
          $branch_class = "prereqs-incomplete-branch";
          if (in_array($branch_num, $completed_branches)) {
            $branch_class = "prereqs-completed-branch";
          }
          $html .= "<div class='prereqs-req-branch $branch_class'>";
          $c = 0;
          foreach ($reqs as $details) {

            if ($c > 0) {
              $html .= " <span class='prereq-req-or-token'>or</span> ";
            }


            $course_id = $details ["course_id"];
            $course_class = "prereqs-incomplete-course";
            if (in_array($course_id . "~" . $details ["min_grade"], $completed_courses)) {
              $course_class = "prereqs-completed-course";
            }

            $html .= "<span class='prereqs-req-course $course_class'>";
            $html .= $details ["subject_id"] . " " . $details ["course_num"];
            if ($details ["min_grade"] != "") {
              $html .= " (" . $details ["min_grade"] . ") ";
            }
            $html .= "</span>";

            $c++;
          }
          $html .= "</div>"; // prereqs-req-branch
        }

        $rtn ["prereq"]["full_html"] = t($warning_msg, array("@need_courses" => $html));



      } // bool_fulfilled_prereqs = false

    } // if count > 0
  } // previous course requirement prereq locks    


  //////////////////////////////////////
  // Okay, now we invoke a hook, to see if any other modules
  // would like to add or alter our prereq warnings.  
  invoke_hook("prereqs_get_prereq_warnings_for_course", array(&$rtn, $course, $student));



  return $rtn;

}