function admin_edit_degree_form_validate

6.x admin.degrees.inc admin_edit_degree_form_validate($form, &$form_state)

File

modules/admin/admin.degrees.inc, line 1670

Code

function admin_edit_degree_form_validate($form, &$form_state) {
  $values = $form_state ['values'];
  $perform_action2 = trim($values ["perform_action2"]);

  $semester_nums_in_use = array();

  $school_id = 0;
  if (isset($values ['schools_school'])) { // A school was set from the schools module.
    $school_id = intval($values ['schools_school']); // Use our NEW school_id for validating.
  }

  // What courses should have their type overwritten as "x"?
  $ignore_courses = csv_to_array(variable_get_for_school("ignore_courses_from_hour_counts", "", $school_id));


  // One of the things we want to do is make sure that the courses & groups specified actually belong to the school_id for this degree.
  $de_catalog_year = $values ["de_catalog_year"];
  $db = get_global_database_handler();

  $degree_id = @intval($form_state ['values']['degree_id']);

  // Each degree_id is unique to the catalog year, so we should be good with just the id here.  
  if ($degree_id) {
    $degree = new DegreePlan($degree_id, null, false, false, TRUE);
    $degree->load_descriptive_data();
  }
  else {
    // Couldn't find degree_id!
    form_error("", t("There was a problem trying to save this degree.  The degree_id could not be found.  Contact FlightPath admin if
                      this problem persists."));
    return;
  } // else from if(degree_id)   


  // At this point, we have a school_id and degree_id.

  if (module_enabled("schools")) {
    $defs = schools_get_school_definitions();
    $school_name = $defs [$school_id];
  }


  // Let's check that all of the courses also belong to this degree and/or can be found at all (no typos).  
  foreach ($values as $key => $value) {
    if (!strstr($key, "courses_")) {
      continue;
    }
    // Only look at the groups...
    $temp = explode("_", $key);
    $semester_num = trim($temp [1]);
    $semester_nums_in_use [] = $semester_num;

    if ($semester_num > $highest_semester_num) {
      $highest_semester_num = $semester_num;
    }


    $courses = trim(@$values ["courses_$semester_num"]);

    // Does it contain a single #?  Or, is blank?  If so, insert and move on.
    // We do this, so we can have blank semesters until someone deletes them.
    if ($courses == "#" || $courses == "") {
      continue;
    }

    $course_rows = explode("\n", $courses);
    for ($t = 0; $t < count($course_rows); $t++) {
      $line = trim($course_rows [$t]);
      if ($line == "") {
        continue;
      }

      // Take out extra whitespace between tokens.
      $line = str_replace("   ", " ", $line);
      $line = str_replace("  ", " ", $line);
      $line = str_replace("  ", " ", $line);
      $line = str_replace("  ", " ", $line);

      $tokens = explode(" ", $line);
      $subject_id = $tokens [0];
      $course_num = $tokens [1];
      $requirement_type = strtolower(@$tokens [2]);

      if ($requirement_type == "") {
        // major type by default.
        $requirement_type = "m";
      }

      // If this course is in our ignore list, override it's type
      // to be 'x'
      if (in_array("$subject_id $course_num", $ignore_courses)) {
        $requirement_type = "x";
      }


      $min_grade = strtoupper(@$tokens [3]);

      if (strstr($requirement_type, "(")) {
        // This means there was no requirement_type specified, so it's "m",
        // and a min_grade was found in its place.
        $min_grade = strtoupper($requirement_type);
        $requirement_type = "m";
      }

      $min_grade = str_replace("(", "", $min_grade);
      $min_grade = str_replace(")", "", $min_grade);

      /////////////////////////////////////////////
      // Find out what the course_id is.
      if ($course_id = $db->get_course_id($subject_id, $course_num, "", TRUE, $school_id, TRUE)) 
       {
        // This was found correctly, do nothing.
      }
      else {
        // The course_id could not be found!

        // Different message if it's a school_id problem.
        if (module_enabled("schools")) {
          form_error("courses_" . $semester_num, t("<strong>Your work has NOT been saved.</strong> Course not found.  In block %blocknum, you specified the course %course as a requirement,
                          but this course could not be found.  This could be due to a typo, or due to the course not belonging to the same school
                          as the degree (%schoolname).  Please check the course and try again.", array("%blocknum" => ($semester_num + 1), "%course" => "$subject_id $course_num", "%schoolname" => $school_name)));
        }
        else {
          // Not related to school (possibly).
          form_error("courses_" . $semester_num, t("<strong>Your work has NOT been saved.</strong> Course not found.  In block %blocknum, you specified the course %course as a requirement,
                          but this course could not be found.  This could be due to a typo.  
                          Please check the course and try again.", array("%blocknum" => ($semester_num + 1), "%course" => "$subject_id $course_num")));

        }
      }

    } // for course_rows

  } // foreach courses






  // Are we trying to ADD a group?  
  if (strstr($perform_action2, "addGroup")) {
    $temp = explode("_", $perform_action2);
    $group_id = $temp [1];
    $semester_num = $temp [2];
    $semester_nums_in_use [] = $semester_num;
    $hours = trim($temp [3]);
    $min_hours = $hours;
    if (strstr($hours, "-")) {
      // Hours has a min hour value specified.  let's grab it.
      $tt = explode("-", $hours);
      $min_hours = $tt [0];
      $hours = $tt [1];
    }


    $type = $temp [4];
    $min_grade = trim($temp [5]);


    $adding_group = new Group($group_id);

    // If this group is already saved in this degree elsewhere, and it has a different type or min_grade,
    // then we must display a warning message to the user.
    $res = db_query("SELECT * FROM draft_degree_requirements
                     WHERE degree_id = '?'
                     AND group_id = '?'
                     AND (group_min_grade <> '?' OR group_requirement_type <> '?')", $degree_id, $group_id, $min_grade, $type);
    while ($cur = db_fetch_array($res)) {
      form_error("semester_groups_" . $semester_num . "_markup", t("<strong>Your work has NOT been saved.</strong> In block %blocknum, you specified the group %group with a min grade of %mingrade and a requirement type of %req.  
                                However, the group was previously specified in this degree plan in block %otherblocknum wih a different requirement type or min grade.
                                FlightPath will not perform calculations correctly for courses assigned to this group.  The duplicate occurance of the group should have the same
                                type/min grade, or, you need to create a new group to handle these alternate requirements.</strong>"
      , array("%blocknum" => ($semester_num + 1), "%group" => $adding_group->title, "%mingrade" => $min_grade, "%req" => $type, "%otherblocknum" => ($cur ['semester_num'] + 1))));


    } // while
  } // addGroup





  // Now, let's check the groups.

  // Are we trying to DELETE a group, but there might be an error with it?  If so, we won't worry about any errors later.
  $del_group = NULL;
  if (strstr($perform_action2, "delGroup")) {
    $temp = explode("_", $perform_action2);
    $del_group = new Group();
    $del_group->bool_use_draft = TRUE;
    $del_group->group_id = $temp [1];
    $del_group->assigned_to_semester_num = $temp [2];
  }

  // Get the groups....
  foreach ($values as $key => $value) {
    if (!strstr($key, "group_")) {
      continue;
    }
    // Only look at the groups...
    $temp = explode("_", $value);

    $group_id = $temp [0];

    $semester_num = $temp [1];
    $semester_nums_in_use [] = $semester_num;

    if ($semester_num > $highest_semester_num) {
      $highest_semester_num = $semester_num;
    }

    $hours = $temp [2];
    $min_hours = $hours;

    if (strstr($hours, "-")) {
      // Hours has a min hour value specified.  let's grab it.
      $tt = explode("-", $hours);
      $min_hours = $tt [0];
      $hours = $tt [1];
    }

    $type = $temp [3];
    $min_grade = trim($temp [4]);

    // Do not process any further, if we are supposed to be deleting this group!
    if (isset($del_group) && $del_group != NULL && is_object($del_group)) {
      if ($del_group->group_id == $group_id && $del_group->assigned_to_semester_num == $semester_num) {
        continue;
      }
    }

    $adding_group = new Group($group_id);

    // Is this in the right school?
    if ($adding_group->school_id != $school_id && $adding_group->school_id !== 0) {

      form_error("semester_groups_" . $semester_num . "_markup", t("<strong>Your work has NOT been saved.</strong> In block %blocknum, you specified the group %group as a requirement,
                          but this group could not be found in the degree's school (%schoolname).<br>You may need to change the degree's school back to its previous value, then remove this group,
                          in order to continue.", array("%blocknum" => ($semester_num + 1), "%group" => $adding_group->title, "%schoolname" => $school_name)));

    }


    // If this group is already saved in this degree elsewhere, and it has a different type or min_grade,
    // then we must display a warning message to the user.
    $res = db_query("SELECT * FROM draft_degree_requirements
                     WHERE degree_id = ?
                     AND group_id = ?
                     AND (group_min_grade <> ? OR group_requirement_type <> ?)", $degree_id, $group_id, $min_grade, $type);
    while ($cur = db_fetch_array($res)) {

      // Is the group we found the group we are trying to delete?  If so, skip it; we're going to delete it later.
      if (isset($del_group) && is_object($del_group)) {
        if (intval($del_group->group_id) == intval($cur ['group_id']) && intval($del_group->assigned_to_semester_num) == intval($cur ['semester_num'])) {
          continue;
        }
      }

      form_error("semester_groups_" . $semester_num . "_markup", t("<strong>Your work has NOT been saved.</strong> In block %blocknum, you specified the group %group with a min grade of %mingrade and a requirement type of %req.  
                                However, the group was previously specified in this degree plan in block %otherblocknum wih a different requirement type or min grade.
                                FlightPath will not perform calculations correctly for courses assigned to this group.  The duplicate occurance of the group should have the same
                                type/min grade, or, you need to create a new group to handle these alternate requirements.</strong>"
      , array("%blocknum" => ($semester_num + 1), "%group" => $adding_group->title, "%mingrade" => $min_grade, "%req" => $type, "%otherblocknum" => ($cur ['semester_num'] + 1))));


    }

  } // foreach groups



  // Are we trying to change a semester block number?
  if (strstr($perform_action2, "editSemesterBlockNum")) {
    // Make sure we don't already have this number in existence elsewhere in the degree.
    $temp = explode("_", $perform_action2);
    $old_semester_num = $temp [1];
    $new_semester_num = $temp [2];

    if (in_array($new_semester_num, $semester_nums_in_use)) {
      form_error('', t("<strong>Your work has NOT been saved.</strong> Sorry, but you requested to change block number %old to %new, but block number %new
                        is already in use.  Please make the appropriate changes and try again.", array("%old" => ($old_semester_num + 1), "%new" => ($new_semester_num + 1))));
    }

  } // editSemesterBlockNum







}