function admin_edit_course_form_submit

6.x admin.courses.inc admin_edit_course_form_submit(&$form, &$form_submit)
4.x admin.courses.inc admin_edit_course_form_submit(&$form, $form_submit)
5.x admin.courses.inc admin_edit_course_form_submit(&$form, $form_submit)

File

modules/admin/admin.courses.inc, line 385

Code

function admin_edit_course_form_submit(&$form, $form_submit) {
  $values = $form_submit ["values"];


  $de_catalog_year = $values ["de_catalog_year"];
  $course_id = trim($values ["course_id"]);
  $course_names = trim($values ["course_names"]);

  $db = get_global_database_handler();

  if ($course_names == "") {
    $course_names = $values ["subject_id"] . " " . $values ["course_num"];
  }

  $title = trim($values ["title"]);
  $min_hours = trim($values ["min_hours"]);
  $max_hours = trim($values ["max_hours"]);
  $repeat_hours = trim($values ["repeat_hours"]);
  //$exclude = trim($_POST["exclude"]);
  $description = trim($values ["description"]);
  $data_entry_comment = trim($values ["data_entry_comment"]);

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



  // Unlike the degrees and the groups, course_ids are not
  // unique.  Only a course_id + catalog_year pair are unique.  This
  // is so we can handle equivalent courses more gracefully.

  // So, the first thing we need to do is delete *all* courses with the
  // course_id and catalog_year listed above.  For most courses, this will
  // only be one row.  For eqv courses, this will delete more than one row.
  if ($course_id != "new") {
    // Don't delete!  Temporarily transfer to a temporary course_id.
    // Will possibly delete later.

    $res = db_query("UPDATE draft_courses
          SET course_id = '-12345'
          WHERE course_id = '?'
        AND catalog_year = '?' ", $course_id, $de_catalog_year);
  }


  if ($values ["perform_action2"] == "delete_course") {
    // That's it.  All we wanted to do was delete the course.
    $query = "DELETE FROM draft_courses
          WHERE course_id = '-12345'
          ";
    $res = db_query($query);

    fp_add_message(t("Course %course successfully deleted for %year", array("%course" => $course_names, "%year" => $de_catalog_year)));
    // Redirect us to the list of courses.
    $form ["#redirect"] = array(
      "path" => "admin/courses",
      "query" => "de_catalog_year=$de_catalog_year",
    );
    return;

  }

  // If the $course_id == new then create a new one.
  if ($course_id == "new") {
    $course_id = $db->request_new_course_id();

    $values ["course_id"] = $course_id;
    // change the $form redirect so we get sent to the new course on save. 
    $form ["#redirect"] = array(
      "path" => "admin/courses/edit-course",
      "query" => "de_catalog_year=$de_catalog_year&course_id=$course_id",
    );

  }




  // Now, we will split the courseNames on commas, and for each
  // token, we will insert a row into the database.
  $courses = explode(",", $course_names);
  foreach ($courses as $course) {
    $course = str_replace("  ", " ", $course);
    $course = str_replace("  ", " ", $course);
    $course = str_replace("  ", " ", $course);
    $course = trim($course);
    if ($course == "") {
      continue;
    }

    $temp = explode(" ", $course);
    $subject_id = trim($temp [0]);
    $course_num = trim($temp [1]);

    ////////////
    ///  Error conditions...
    if (strtolower($course_num) == "exclude" || $course_num == "") {
      form_error("course_names", t("It appears you specified an excluded course
            without a course number.  You entered %course.
            Notice there is no course number. Please re-enter.", array("%course" => "$subject_id $course_num")));
      continue;
    }

    ////////////////

    $exclude = 0;
    // Do we exclude?
    if (strtolower(trim($temp [2])) == "exclude") {
      $exclude = 1;
      // Set ALL courses with this subject_id and course_num to exclude!
      $res = db_query("UPDATE draft_courses
                SET exclude = '1'
                WHERE subject_id = '?'
                AND course_num = '?' 
                ", $subject_id, $course_num);


    }
    else {
      // Aet all courses with this subject_id and course_num to NOT exclude!
      $res = db_query("UPDATE draft_courses
                SET exclude = '0'
                WHERE subject_id = '?'
                AND course_num = '?' 
                ", $subject_id, $course_num);

    }

    // Did the user specify a course which already exists?  If so,
    // mark that course's ID as -12345...
    $res = db_query("UPDATE draft_courses
                SET course_id = '-12345'
                WHERE subject_id = '?'
                AND course_num = '?' 
                AND catalog_year = '?' ", $subject_id, $course_num, $de_catalog_year);



    // We now have enough information to make an insertion into
    // the table.
    $query = "INSERT INTO draft_courses
          (`course_id`,`subject_id`,`course_num`,`catalog_year`,
            `title`,`description`,`min_hours`,`max_hours`,`repeat_hours`,
            `exclude`,`data_entry_comment`)
            values ('?','?','?','?','?','?','?','?','?','?','?') ";
    //debug_c_t($query);
    $res = db_query($query, $course_id, $subject_id, $course_num, $de_catalog_year, 
    $title, $description, $min_hours, $max_hours, $repeat_hours, 
    $exclude, $data_entry_comment);

    // Now, this part is tricky.  Are there any courses which already
    // existed with this subject_id and course_num, but not this course_id?
    // This would happen if we add an eqv for a course that already existed
    // elsewhere.  We want to change that existing course's ID to match the
    // new one, but we also need to update EVERY table that used the old
    // course_id with the new course_id, including degree plans, groups,
    // substitutions, etc.

    // query for subject_id and course_num but != course_id.
    // get oldCourseID.
    // call function update_course_id(oldCourseID, newCourseID)
    $res2 = db_query("SELECT * FROM draft_courses WHERE
                subject_id = '?'
                AND course_num = '?'
                AND course_id != '?' 
                AND course_id != '-12345' ", $subject_id, $course_num, $course_id);
    while ($cur2 = db_fetch_array($res2)) {
      $old_course_id = $cur2 ["course_id"];
      // Now, update all the existing references to $old_course_id
      // with the new course_id.
      $db2 = new DatabaseHandler();
      $db2->update_course_id($old_course_id, $course_id, true);
      // Now, add it to our list of things to update when we apply
      // the draft changes...
      $db2->add_draft_instruction("update_course_id,$old_course_id,$course_id");
    }


  }

  // We have to accomodate the situation that there used to be an
  // eqv set up (multiple course names were set) but now there is not.
  // In other words, someone wanted to undo an eqv.
  // We used to have:  ACCT 101, MATH 101
  // But they took out the comma.  So, only ACCT 101 just got written
  // to the database, while MATH 101 has been marked as -12345 and is
  // destined to be deleted.
  // -- we need to give MATH 101 a new course_id and update that course_id
  // for all years.
  // Then, we need to go through all our tables and update where it was
  // actually spelled out that "MATH 101" be used with the new course_id.
  // -- This process will ensure that no previous existing courses
  // will get deleted.  That they will remain as their own unique
  // courses.

  // First thing's first.  Go through all the courses with the course_id
  // of -12345.  If we find one that does not have the same subject_id
  // and course_num with the new ID, then this is a removed eqv, and
  // that is our cue that it should be it's own course.
  $res = db_query("SELECT * FROM draft_courses
              WHERE course_id = '-12345' ");
  while ($cur = db_fetch_array($res)) {
    $found_s_i = $cur ["subject_id"];
    $found_c_n = $cur ["course_num"];
    $db2 = new DatabaseHandler();
    $res2 = $db2->db_query("SELECT * FROM draft_courses
              WHERE course_id = '?'
              AND subject_id = '?'
              AND course_num = '?' 
              AND catalog_year = '?' ", $course_id, $found_s_i, $found_c_n, $de_catalog_year);
    if ($db2->db_num_rows($res2) == 0) {
      // Meaning, this course name is not listed with the course_id,
      // so this is a broken eqv.
      // We should make a new course_id and all that for this course,
      // for all available years.

      $new_course_id = $db2->request_new_course_id();
      $db3 = new DatabaseHandler();
      $res3 = $db3->db_query("UPDATE draft_courses
                  SET course_id = '?'
                  WHERE subject_id = '?'
                  AND course_num = '?' ", $new_course_id, $found_s_i, $found_c_n);
      // removed WHERE `course_id`='-12345' from query.  We want to UPDATE
      // this across all years for this course.
      // And also UPDATE every other table that specified foundSI &CN
      // as a requirement.
      $db3->update_course_requirement_from_name($found_s_i, $found_c_n, $new_course_id);
      $db3->add_draft_instruction("update_course_requirement_from_name,$found_s_i,$found_c_n,$new_course_id");
    }
  }





  // Was the "all_years" box checked?  If it was, then update all instances
  // of this course, across all available catalog years.
  if (is_array($values ["all_years"]) && $values ["all_years"]["yes"] == "yes") {
    $res = db_query("UPDATE draft_courses
                  SET title = '?',
                    description = '?',
                    min_hours = '?',
                    max_hours = '?',
                    repeat_hours = '?'
                  WHERE course_id = '?' ", $title, $description, $min_hours, $max_hours, $repeat_hours, $course_id);

  }



  // Clean up.  Delete the temporary course_id...
  $res = db_query("DELETE FROM draft_courses WHERE course_id = '-12345'  ");

  fp_add_message("Course updated successfully.");


}