admin_handle_edit_degree_submit

4.x admin.degrees.inc admin_handle_edit_degree_submit()

File

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

Code

function admin_handle_edit_degree_submit() {

  $de_catalog_year = $_REQUEST["de_catalog_year"];
  $db = get_global_database_handler();

  // This will UPDATE a degree in the system with the courses
  // and groups that the user selected.
  $perform_action2 = trim($_POST["perform_action2"]);

  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];

  }

  $major_code = trim($_POST["major_code"]);
  if ($major_code == "") {
    die("Fatal error:  major_code not found.");
  }

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


  $degree_id = "";
  // First things first, if this degree already exists in this
  // catalog year, then we need to delete it first.
  if ($degree_id = $db->get_degree_id($major_code, $de_catalog_year, true)) {
    $degree = new DegreePlan($degree_id, null, false, false, true);
    $degree->load_descriptive_data();

    // Delete from degree_requirements WHERE this degree_id exists.
    $res = db_query("DELETE FROM draft_degree_requirements
                  WHERE degree_id = '?' ", $degree_id);

    // Are we trying to DELETE this degree?  If so, keep deleting!
    if ($perform_action2 == "delete_degree") {
      $res = db_query("DELETE FROM draft_degrees
                  WHERE  degree_id = '?' ", $degree_id);

      // Also need to get rid of the track, if there is one for this
      // degree.
      $res = db_query("DELETE FROM draft_degree_tracks
                  WHERE major_code = '$degree->major_code' 
                  AND track_code = '$degree->track_code'
                  AND catalog_year = '?' LIMIT 1", $de_catalog_year);

      // Okay, we have deleted everything.  We need to go back to
      // just the list of degrees.
      //display_edit_degrees("<font color='green'><b>The degree $major_code ($de_catalog_year) has been deleted.</b></font>");
      //die;

      fp_add_message("The degree $major_code ($de_catalog_year) has been deleted.");
      fp_goto("admin/degrees", "de_catalog_year=$de_catalog_year");
      return;

    }


  }
  else {
    // We need to generate a new degreeID for this major_code and catalog_year,
    // because one does not already exist!
    if (!$degree_id = $db->request_new_degree_id()) {
      die("Error.  Could not create new degreeID.");
    }
  }

  // Save the entire post to the log.

  $errors = "";
  $semester_titles_c_s_v = "";
  $highest_semester_num = 0; // What is the largest semester_num in the system?
  // Okay, now get the various courses...
  for ($semester_num = 0; $semester_num < 50; $semester_num++) {
    // Assuming no more than 50 semesters.
    $courses = trim($_POST["courses_$semester_num"]);
    if ($courses == "") {
      continue;
    }

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

    $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";
      }

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

      /////////////////////////////////////////////
      // Okay, we now have enough information to insert the course.
      // Find out what the course_id is.
      if ($course_id = $db->get_course_id($subject_id, $course_num, "", true)) // don't care about catalog year.
       {
        $query = "INSERT INTO draft_degree_requirements
                    (degree_id, semester_num, course_id, course_min_grade, course_requirement_type, data_entry_value)
                    values ('?','?','?','?','?','?~?') ";
        $res = db_query($query, $degree_id, $semester_num, $course_id, $min_grade, $requirement_type, $subject_id, $course_num);
        //debug_c_t($query);
      }
      else {
        // The course_id could not be found!
        $errors .= "<br><font color='red'><b>Course Not Found!</b>
              In Block " . ($semester_num + 1) . ", you specified the course
              <b>$subject_id $course_num</b> as a requirement, but this course
              could not be found.
              It was removed from that block.
              Are you sure you typed it correctly?  Please go to this
              semester, check your spelling, and add the course again.</font>";

      }

    }


  }

  // Get the groups....
  foreach ($_POST as $key => $value) {
    if (!strstr($key, "group_")) {
      continue;
    }
    // Only look at the groups...
    $temp = explode("_", $value);
    $group_id = $temp[0];
    $semester_num = $temp[1];
    $hours = $temp[2];
    $type = $temp[3];
    $min_grade = trim($temp[4]);

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


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

    // We now have enough information to insert this group.
    //debugCT("group: $group_id $semester_num $hours $type");
    $query = "INSERT INTO draft_degree_requirements
                    (`degree_id`,`semester_num`,`group_id`,
                    `group_requirement_type`,`group_hours_required`,`group_min_grade`)
                    values ('?','?','?',
                    '?','?','?') ";
    $res = db_query($query, $degree_id, $semester_num, $group_id, $type, $hours, $min_grade);


  }


  // Was there a group added or deleted?
  if (strstr($perform_action2, "addGroup")) {
    $temp = explode("_", $perform_action2);
    $group_id = $temp[1];
    $semester_num = $temp[2];
    $hours = trim($temp[3]);
    $type = $temp[4];
    $min_grade = trim($temp[5]);
    $query = "INSERT INTO draft_degree_requirements
                    (`degree_id`,`semester_num`,`group_id`,
                    `group_requirement_type`,`group_hours_required`,`group_min_grade`)
                    VALUES ('?','?','?','?','?','?') ";
    $res = db_query($query, $degree_id, $semester_num, $group_id, $type, $hours, $min_grade);

  }


  // Make the semesterTitlesCSV...
  for ($semester_num = 0; $semester_num <= $highest_semester_num; $semester_num++) {
    $semester_titles_csv .= trim($_POST["semester_title_$semester_num"]) . ",";
  }

  // Before we UPDATE, also grab the degree title, degree_type,
  // and exclude value, etc....
  $degree_title = trim($_POST["title"]);
  $degree_type = trim($_POST["degree_type"]);
  $degree_class = strtoupper(trim($_POST["degree_class"]));
  $exclude = trim($_POST["exclude"]);
  $public_note = trim($_POST["public_note"]);
  $res = db_query("UPDATE draft_degrees
              SET `semester_titles_csv`='?',
              `title`='?',
              `degree_type`='?',
              `degree_class`='?',
              `exclude`='?',
              `public_note`='?'
              WHERE `degree_id`='?' ", $semester_titles_csv, $degree_title, $degree_type, $degree_class, $exclude, $public_note, $degree_id);

  ////  Was there a track title/description?  If so, UPDATE that in the tracks
  // table...
  if (strstr($major_code, "_")) {
    // There was a track. Update track description.
    $temp = explode("_", $major_code);
    $major = trim($temp[0]);
    // major might now have a | at the end.  If so, take it out.
    if (substr($major, strlen($major) -1, 1) == "|") {
      $major = str_replace("|", "", $major);
    }


    $track = trim($temp[1]);
    $track_description = trim($_POST["track_description"]);
    $track_title = trim($_POST["track_title"]);
    //debugCT($track_description);
    $res = db_query("UPDATE draft_degree_tracks
                SET `track_description`='?',
                `track_title`='?'
                WHERE `track_code`='?'
                AND `major_code`='?' 
                AND `catalog_year`='?' ", $track_description, $track_title, $track, $major, $de_catalog_year);
  }




  //$msg = "<font color='green' size='4'>Degree updated successfully at " . get_current_time() . ".</font>";
  fp_add_message("Degree updated succesfully.");
  $bool_scroll = $bool_button_msg = true;
  if ($errors != "") {
    fp_add_message("ERROR(s): $errors");
    $bool_scroll = $bool_button_msg = false;
  }


  fp_goto("admin/degrees/edit-degree", "de_catalog_year=$de_catalog_year&major_code=$major_code&scroll_top=" . $_REQUEST["scroll_top"] . "&button_msg=" . urlencode("Degree updated successfully at " . date("H:i:s")));
  return;

}