function admin_edit_group_form_submit

7.x admin.groups.inc admin_edit_group_form_submit(&$form, &$form_state)
6.x admin.groups.inc admin_edit_group_form_submit(&$form, &$form_state)
4.x admin.groups.inc admin_edit_group_form_submit(&$form, $form_submit)
5.x admin.groups.inc admin_edit_group_form_submit(&$form, $form_state)

File

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

Code

function admin_edit_group_form_submit(&$form, $form_state) {
  $values = $form_state ["values"];
  $de_catalog_year = $values ["de_catalog_year"];

  $db = get_global_database_handler();
  $db2 = new DatabaseHandler();

  $group_id = $values ["group_id"];

  // Okay, we are trying to save the details of this group.
  // First thing we need to do is UPDATE the title, group_name,
  // priority, icon and comment.
  $group_name = trim($values ["group_name"]);
  $title = trim($values ["title"]);
  $priority = intval(trim($values ["priority"]));
  $icon_filename = trim($values ["icon_filename"]);
  $data_entry_comment = trim($values ["data_entry_comment"]);
  $catalog_repeat = intval(trim($values ["catalog_repeat"]));

  // Save the entire post to the log.

  // Since we are making a change to the draft table(s), let's add a row
  // to draft instructions.
  $db->add_draft_instruction("-");


  // Are we trying to delete this group?
  if ($_POST ["perform_action2"] == "delete_group" && user_has_permission("can_delete_data_entry")) {
    $res = db_query("UPDATE draft_groups
                SET delete_flag = '1'
                WHERE group_id = '?' 
                AND catalog_year = '?'
                ", $group_id, $de_catalog_year);
    fp_add_message(t("The group @title (%name) has been deleted successfully for @year", array("@title" => $title, "%name" => $group_name, "@year" => $de_catalog_year)));
    fp_goto("admin/groups", "de_catalog_year=$de_catalog_year");
    return;
  }

  // If the $group_id == new then create a new one.
  if ($group_id == "new") {
    $group_id = $db->request_new_group_id();
    $res = db_query("INSERT INTO draft_groups(group_id, catalog_year)
                values ('?','?') ", $group_id, $de_catalog_year);
    $values ["group_id"] = $group_id;
    // Let's reset where we should redirect to after the form submits.
    $form ["#redirect"] = array(
      "path" => "admin/groups/edit-group",
      "query" => "group_id=$group_id&de_catalog_year=$de_catalog_year",
    );
  }

  $res = db_query("UPDATE draft_groups
              SET group_name = '?',
              title = '?',
              priority = '?',
              icon_filename = '?',
              catalog_repeat = '?',
              data_entry_comment = '?'
              WHERE
                group_id = '?' ", 
  $group_name, $title, $priority, $icon_filename, $catalog_repeat, $data_entry_comment, $group_id);

  // We need to delete all the existing course & subgroup requirements from this group.
  // That entails first seeing what subgroups were required and deleting them,
  // then deleting the parent group's requirements.
  // First, find and delete the branches (child groups):
  $res = db_query("SELECT * FROM draft_group_requirements
              WHERE group_id = '?'
              AND child_group_id != '0' ", $group_id);
  while ($cur = db_fetch_array($res)) {
    $cg_id = $cur ["child_group_id"];
    $res2 = db_query("DELETE FROM draft_group_requirements
                WHERE group_id = '?' ", $cg_id);
  }
  // Now delete the course requirements...
  $res = db_query("DELETE FROM draft_group_requirements
                WHERE group_id = '?' ", $group_id);

  $courses = trim($values ["courses"]);
  // If a definition was set, then we will ignore what is in the POST
  // for the course requrements, and instead use the definition.
  if (trim($values ["set_definition"] != "")) {
    $def = urldecode(trim($values ["set_definition"]));
    //$cc = trim(get_courses_from_definition($def, $de_catalog_year));
    $temp2 = admin_get_courses_from_definition($def);
    $cc = trim($temp2 ["text"]);
    if ($cc != "") {
      $courses = $cc;
      // UPDATE this group's definition!
      $res = db_query("UPDATE draft_groups
              SET definition = '?'
              WHERE
                group_id = '?' ", $def, $group_id);

    }
    //print_pre($cc);
  }
  else {
    // In other words, the setDefinition WAS blank.
    // Let's update the table.  This is to fix a bug where they were unable
    // to clear definitions.
    $res = db_query("UPDATE draft_groups
              SET definition = ''
              WHERE
                group_id = '?' ", $group_id);
  }



  // If catalog_repeat == 1, then we need to perform that operation on the $courses text.
  if ($catalog_repeat == 1) {
    $courses = admin_process_catalog_repeats_for_group_courses_text($courses, $de_catalog_year);
  }

  // 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);

    // 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.
      // First, we need to request a new branchID for this new group.
      if (!$branch_id = $db->request_new_group_id()) {
        die("Error.  Could not create new group (branch) ID.");
      }
      else {
        // Add this branch to the list of requirements for this group.
        $query = "INSERT INTO draft_group_requirements
                    (group_id, child_group_id)
                    values ('?','?') ";
        $res = db_query($query, $group_id, $branch_id);

      }
      $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]);
        $min_grade = @trim($tokens [2]);
        $course_repeats = @trim($tokens [3]);

        if (strstr($min_grade, "[")) {
          // This is actually a specified repeat, not a min grade.
          $course_repeats = $min_grade;
          $min_grade = "";
        }

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

        $course_repeats = str_replace("[", "", $course_repeats);
        $course_repeats = str_replace("]", "", $course_repeats);
        $course_repeats--;
        if ($course_repeats < 0) {
          $course_repeats = 0;
        }

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

        // Commenting out, because we do not care about catalog_year
        // when specifying courses...
        if ($course_id = $db->get_course_id($subject_id, $course_num, "", true)) {

          $query = "INSERT INTO draft_group_requirements
                    (group_id, course_id,
                    course_min_grade, course_repeats, data_entry_value)
                    values ('?','?','?','?', ?) ";
          $res = db_query($query, $branch_id, $course_id, $min_grade, $course_repeats, "$subject_id~$course_num");
        }
        else {
          // The course_id could not be found!

          form_error("courses", t("Course Not Found!
              You specified the course
              <b>%course</b> as a requirement, but this course
              could not be found in the catalog.
              It was removed from the list of requirements.
              Are you sure you typed it correctly?  Please check 
              your spelling, and add the course again.", array("%course" => "$subject_id $course_num")));

        }

      }


    }
    else {
      // Did NOT contain an ampersand (&), so this goes in the
      // regular course requirements.
      $tokens = explode(" ", $line);
      $subject_id = @trim($tokens [0]);
      $course_num = @trim($tokens [1]);
      $min_grade = @trim($tokens [2]);
      $course_repeats = @trim($tokens [3]);

      if (strstr($min_grade, "[")) {
        // This is actually a specified repeat, not a min grade.
        $course_repeats = $min_grade;
        $min_grade = "";
      }

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

      $course_repeats = str_replace("[", "", $course_repeats);
      $course_repeats = str_replace("]", "", $course_repeats);
      $course_repeats--;
      if ($course_repeats < 0) {
        $course_repeats = 0;
      }

      // 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)) {
        $query = "INSERT INTO draft_group_requirements
                    (group_id, course_id,
                    course_min_grade, course_repeats, data_entry_value)
                    values (?,?,?,?,?) ";
        $res = db_query($query, $group_id, $course_id, $min_grade, $course_repeats, "$subject_id~$course_num");

      }
      else {
        // The course_id could not be found!
        form_error("courses", t("Course Not Found!
              You specified the course
              <b>%course</b> as a requirement, but this course
              could not be found in the catalog.
              It was removed from the list of requirements.
              Are you sure you typed it correctly?  Please check 
              your spelling, and add the course again.", array("%course" => "$subject_id $course_num")));

      }



    }

  }


  fp_add_message("Group successfully updated.");

  // Clear previous values to make sure the Required Courses box gets updated,
  // even if its set to readonly.
  clear_session_form_values("admin_edit_group_form");

}