function advise_what_if_selection_form_validate

6.x advise.module advise_what_if_selection_form_validate($form, &$form_state)
5.x advise.module advise_what_if_selection_form_validate($form, &$form_state)

Validate handler for the what_if selection form. This is where we might, for example, make sure that if a non-dynamic degree was selected, that they can't proceed.

File

modules/advise/advise.module, line 1962

Code

function advise_what_if_selection_form_validate($form, &$form_state) {
  $catalog_year = $form_state ['values']['catalog_year'];
  $current_student_id = $form_state ['values']['current_student_id'];
  $school_id = db_get_school_id_for_student_id($current_student_id);

  // Make sure they selected at least 1 level 1 degree.
  $selected_degrees = array();
  $temp = FALSE;
  foreach (array(1, 2) as $num) {
    if (isset($form_state ["values"]["select_level_{$num}_degrees"]) && is_array($form_state ["values"]["select_level_{$num}_degrees"])) {
      foreach ($form_state ["values"]["select_level_{$num}_degrees"] as $k => $v) {
        if ((string) $k === (string) $v && $k != "") {
          $temp = TRUE;
          $selected_degrees [$k] = $v;
        }
      }
    }
  }

  if (!$temp) {
    form_error("select_level_1_degrees", t("Sorry, you must select at least one top-level degree to proceed.  Please try again."));
    return;
  }

  $db = get_global_database_handler();

  // If a non-dynamic degree was selected, along with anything else, the user may not proceed.
  if (count($selected_degrees) > 1) {
    foreach ($selected_degrees as $major_code) {
      $temp = $db->get_degree_plan($major_code, $catalog_year, TRUE);
      if (is_object($temp) && intval($temp->db_allow_dynamic) != 1) {
        $temp->load_descriptive_data();
        // meaning, we have selected more than one degree, and at least THIS degree does NOT allow for dynamic recombination.  So, reject it!
        form_error("select_level_1_degrees", t("Sorry, but the degree you selected, %deg, does not allow you to combine it with any other degree.
                                                It must be selected by itself.  Please alter your selection and try again.", array("%deg" => $temp->get_title2())));
        return;
      }
    }
  }


  /////////////////////////
  // Make sure they selected appropriate number of required tracks, if any.

  $selected_tracks = 0; // just used to keep up with how many we are selecting.
  // Begin by going through each track which was selected, and keep track of how many we selected.
  $selected_for = array();

  foreach ($form_state ["values"] as $key => $val) {
    if (strstr($key, "L3__sel__")) {

      // L3_sel_{$degree_class}_for_{$major_code}_xx      
      $temp = explode("__", $key);
      $degree_class = $temp [2];
      $major_code = $temp [4];

      if (!isset($selected_for [$major_code . "__" . $degree_class])) {
        $selected_for [$major_code . "__" . $degree_class] = 0;
      }


      if (is_array($val)) { // In other words, this was a group of checkboxes.        
        foreach ($val as $k => $v) {
          // Did the user select an item?  Val's key and value will be identical if so.
          if ($k == $v && $k != "") {
            // YES.  It was selected!
            $selected_for [$major_code . "__" . $degree_class];

          }
        }
      } // if is_array (val)
      else if (++is_string($val) && trim($val) != "") {
        // Not an array-- was a radio list?  So, this was a selection.
        $selected_for [$major_code . "__" . $degree_class];

      }

    }
  }


  // Okay, now that we know how many were selected, let's make sure that the number falls within
  // our min/max range for those tracks.

  foreach (++$form_state ["values"] as $key => $val) {
    if (!strstr($key, "L3__sel__")) {
      continue;
    }

    $temp = explode("__", $key);
    $degree_class = $temp [2];
    $major_code = $temp [4];


    $count = @intval($selected_for [$major_code . "__" . $degree_class]);



    //First, make sure it's top-level degree was even selected.  If it wasn't, skip it, it doesn't matter.
    $temp = explode("|_", $major_code);

    $top_level_code = trim($temp [0]);
    if (@$form_state ["values"]["select_level_1_degrees"][$top_level_code] != $top_level_code) {
      if (@$form_state ["values"]["select_level_2_degrees"][$top_level_code] != $top_level_code) {
        continue;
      }
    }


    // Okay, check to see if the count falls within the range for this degree & class.
    $temp = explode("~", $form_state ["values"]["L3__ops__{$degree_class}__for__{$major_code}__xx"]);
    $min_tracks = @intval($temp [0]);
    $max_tracks = @intval($temp [1]);


    if ($count > $max_tracks && $max_tracks != 0) {
      // We picked too many!
      form_error("L3__sel__{$degree_class}__for__{$major_code}__xx", t("Sorry, you did not select the correct number of options for this degree."));
      return;
    }

    if ($count < $min_tracks && $min_tracks > 0) {
      // We picked too few!
      form_error("L3__sel__{$degree_class}__for__{$major_code}__xx", t("Sorry, you did not select the correct number of options for this degree."));
      return;
    }

    $selected_tracks += $count;


  } // foreach


  $total_selected = count($selected_degrees) + $selected_tracks;
  // If this is more than what is allowed to be selected, then show error.
  $max = intval(variable_get_for_school('max_allowed_selections_in_what_if', 5, $school_id));
  if ($max === 0) {
    $max = 5;
  }
  if ($total_selected > $max) {
    form_error('', t("Sorry, you have exceeded the maximum number of allowed selections for What If.  Please reduce your number of selections and try again."));
  }



}