function admin_edit_group_form_validate

6.x admin.groups.inc admin_edit_group_form_validate(&$form, &$form_state)
5.x admin.groups.inc admin_edit_group_form_validate(&$form, &$form_state)

Validate handler for edit group form.

File

modules/admin/admin.groups.inc, line 10

Code

function admin_edit_group_form_validate(&$form, &$form_state) {
  $values = $form_state ["values"];
  $group_name = trim($values ["group_name"]);
  $group_id = $values ["group_id"];
  $de_catalog_year = $values ["de_catalog_year"];

  $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.
  }

  $school_name = $andschool = "";
  if ($school_id > 0) {
    $school_name = "School-" . $school_id;
  }
  if (module_enabled("schools")) {
    $defs = schools_get_school_definitions();
    if (isset($defs [$school_id])) {
      $school_name = $defs [$school_id];
    }
    $andschool = " and school %school";
  }


  // We want to make sure the group_name is in a machine-name format,
  // and we need to make sure it is unique for this catalog year and school_id!
  $group_name = fp_get_machine_readable(strtolower($group_name));

  $form_state ['values']['group_name'] = $group_name;


  // Check that group_name is unique (but exclude the current group_id from the check)
  $mycount = db_query("SELECT count(*) as mycount FROM draft_groups 
                       WHERE group_name = ?
                       AND group_id != ?
                       AND delete_flag = 0
                       AND catalog_year = ?
                       AND school_id = ?", $group_name, $group_id, $de_catalog_year, $school_id)->fetchColumn();

  if ($mycount > 0) {
    form_error("group_name", t("The 'Internal group machine name' %gn is already in use in FlightPath by another group
                                in the catalog year %cy$andschool.  
                                Please change to a different name, and submit again. <b>Your work has NOT been saved.</b>
                                ", array("%gn" => $group_name, "%cy" => $de_catalog_year, "%school" => $school_name)));
    return;
  }


  $db = get_global_database_handler();



  // Make sure that courses are for the correct school for this group.
  $courses = $values ['courses'];
  // Okay, now we look at the actual "courses" box and assemble the group
  // in the database.
  $lines = explode("\n", $courses);
  for ($t = 0; $t < count($lines); $t++) {
    $line = trim($lines [$t]);
    if ($line == "") {
      continue;
    }
    // Get rid of extra whitespace.
    $line = str_replace("  ", " ", $line);
    $line = str_replace("  ", " ", $line);
    $line = str_replace("  ", " ", $line);
    $line = str_replace("  ", " ", $line);
    $line = str_replace("  ", " ", $line);
    $line = str_replace("  ", " ", $line);

    // Does this line contain at least one & symbol?  If it does,
    // then this is a subgroup (branch).  If not, then we can insert
    // the course as-is.
    if (strstr($line, "&")) {
      // This line DOES have an ampersand (&), so this is a sub group
      // within this group.
      $line = trim(str_replace("-", "", $line)); // get rid of attributes, if any.
      $line = trim(str_replace("*", "", $line)); // get rid of attributes, if any.

      $c_tokes = explode("&", $line);
      for ($cT = 0; $cT < count($c_tokes); $cT++) 
       {
        $tokens = explode(" ", trim($c_tokes [$cT]));
        $subject_id = trim($tokens [0]);
        $course_num = trim($tokens [1]);

        // If the subject_id had a _A_ in it, convert this back
        // to an ampersand.
        $subject_id = str_replace("_A_", "&", $subject_id);

        if ($course_id = $db->get_course_id($subject_id, $course_num, "", TRUE, $school_id, TRUE)) {
          // Course was found correctly, no need to do anything.                    
        }
        else {
          // The course_id could not be found!

          // Different message if it's a school_id problem.
          if (module_enabled("schools")) {

            form_error("courses", t("<strong>Your work has NOT been saved.</strong> Course not found.  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 group (%schoolname).  Please check the course (and possibly change the group's school back first), then try again.", array("%course" => "$subject_id $course_num", "%schoolname" => $school_name)));
          }
          else {
            // Not related to school (possibly).
            form_error("courses", t("<strong>Your work has NOT been saved.</strong> Course not found.  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("%course" => "$subject_id $course_num")));

          }
        }

      } // for cT (tokens)


    } // if line contains & 
    else {
      // Did NOT contain an ampersand (&), so this goes in the
      // regular course requirements.

      $line = trim(str_replace("-", "", $line)); // get rid of attributes, if any.
      $line = trim(str_replace("*", "", $line)); // get rid of attributes, if any.

      if ($line == "") {
        continue; // blank line, so skip
      }


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

      if ($subject_id == "") {
        continue; // blank line, or mal-formed, so skip.
      }

      // If the subject_id had a _A_ in it, convert this back
      // to an ampersand.
      $subject_id = str_replace("_A_", "&", $subject_id);

      // We don't care about catalog year anymore...
      if ($course_id = $db->get_course_id($subject_id, $course_num, "", TRUE, $school_id, TRUE)) {
        // Course found normally, do nothing.        
      }
      else {

        // Different message if it's a school_id problem.
        if (module_enabled("schools")) {

          form_error("courses", t("<strong>Your work has NOT been saved.</strong> Course not found.  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 group (%schoolname).  Please check the course (and possibly change the group's school back first), then try again.", array("%course" => "$subject_id $course_num", "%schoolname" => $school_name)));
        }
        else {
          // Not related to school (possibly).

          form_error("courses", t("<strong>Your work has NOT been saved.</strong> Course not found.  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("%course" => "$subject_id $course_num")));

        }


      }

    } // else (did not contain &)

  } // for t (lines of courses)  



}