function admin_duplicate_year_perform_batch_operation

7.x admin.module admin_duplicate_year_perform_batch_operation(&$batch, $ops, $old_year, $new_year)

The is the batch operation to duplicate from $old_year to $new_year

File

modules/admin/admin.module, line 739
The administrative configurations for FlightPath.

Code

function admin_duplicate_year_perform_batch_operation(&$batch, $ops, $old_year, $new_year) {

  set_time_limit(300); // Extend time limit since this may take a little while.

  // if this is our first time through, let's init our values.
  if (!isset($batch ["results"]["total"])) {
    // Our first time through.  Let's start up.
    $batch ["results"]["total"] = count($ops);
    $batch ["results"]["current"] = 0;
    $batch ["results"]["finished"] = FALSE;

    unset($_SESSION ['admin_duplicate_year']);
    $_SESSION ['admin_duplicate_year'] = array();

    $_SESSION ['admin_duplicate_year']['courses'] = array();
    $_SESSION ['admin_duplicate_year']['groups'] = array();
    $_SESSION ['admin_duplicate_year']['degrees'] = array();


  }


  $op_command = key($ops [$batch ['results']['current']]);
  $op_val = $ops [$batch ['results']['current']][$op_command];
  $db = get_global_database_handler();

  watchdog('debug', "$op_command", array(), WATCHDOG_DEBUG);

  if ($op_command == 'delete_new_year') {
    /////////////////////  DELETION  ///////////////////////////////////////////
    // We must first begin by deleting any entries for the new_year
    // from our tables.  This is because we may have to run this
    // more than once while debugging and such.
    $res = db_query("DELETE FROM draft_courses WHERE catalog_year = ? ", $new_year);


    $res = db_query("DELETE FROM draft_degree_tracks WHERE catalog_year = ? ", $new_year);
    // For degrees, we first need to select all of the new_year degrees.
    $res = db_query("SELECT * FROM draft_degrees WHERE catalog_year = ? ", $new_year);
    while ($cur = db_fetch_array($res)) {
      $res2 = db_query("DELETE FROM draft_degree_requirements WHERE degree_id = ? ", $cur ["degree_id"]);
    }

    $res = db_query("DELETE FROM draft_degrees WHERE catalog_year = ? ", $new_year);
    // For groups, begin by selecting all the groups in that year...
    $res = db_query("SELECT * FROM draft_groups WHERE catalog_year = ? ", $new_year);
    while ($cur = db_fetch_array($res)) 
     {
      // Now, select all the requirements and see if there are any sub groups...
      $res2 = db_query("SELECT * FROM draft_group_requirements WHERE group_id = ? ", $cur ["group_id"]);
      while ($cur2 = db_fetch_array($res2)) 
       {
        if ($cur2 ["child_group_id"] > 0) 
         {
          // Delete the child group.
          $res3 = db_query("DELETE FROM draft_group_requirements WHERE group_id = ? ", $cur2 ["child_group_id"]);
        }
      }
      // Now, delete the original requirement.
      $res3 = db_query("DELETE FROM draft_group_requirements WHERE group_id = ? ", $cur ["group_id"]);
    }
    $res = db_query("DELETE FROM draft_groups WHERE catalog_year = ? ", $new_year);

  } // delete_new_year


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

  if ($op_command == 'copy_courses') {

    $start_position = intval($op_val);

    /////////////////////// COPY COURSES //////////////////////////////////////////////////
    // Copy all of the courses from the old_year to the new_year.
    $res = db_query("SELECT * FROM draft_courses WHERE catalog_year = ?
                      LIMIT $start_position, 150 ", array($old_year));
    while ($cur = db_fetch_array($res)) 
     {


      $course = new stdClass();
      $course->course_id = $cur ['course_id'];
      $course->subject_id = $cur ['subject_id'];
      $course->course_num = $cur ['course_num'];
      $course->db_exclude = $cur ['exclude'];
      $course->min_hours = $cur ['min_hours'];
      $course->max_hours = $cur ['max_hours'];
      $course->repeat_hours = $cur ['repeat_hours'];
      $course->school_id = $cur ['school_id'];
      $course->title = $cur ['title'];
      $course->description = $cur ['description'];


      //$course_id, $c->subject_id,$c->course_num,$catalog_year,$c->title,$c->description,$min_hours,$max_hours,
      //             $c->repeat_hours,$c->db_exclude,$c->school_id)

      // Now, duplicate it for the new_year.
      // The FALSE at the end means don't bother deleting any existing course, since we have already done that.
      $db->duplicate_course_for_year($course, $new_year, FALSE);

      // Store what courses we are working on, for other modules.
      $_SESSION ['admin_duplicate_year']['courses'][] = $cur ['course_id'];

    }

  } // copy_courses


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


  if ($op_command == 'copy_groups') {
    //////////////////////// COPY GROUPS  ///////////////////////////////////////////////
    //  Now, let's copy over the groups.
    $group_id_array = array();
    $subgroup_id_array = array();


    $start_position = intval($op_val);


    $res = db_query("SELECT * FROM draft_groups WHERE catalog_year = ?
                        LIMIT $start_position, 3 ", $old_year);
    while ($cur = db_fetch_array($res)) 
     {

      $db_group_id = $cur ['group_id'];
      $db_definition = $cur ['definition'];
      $db_title = $cur ['title'];
      $db_group_name = $cur ['group_name'];
      $db_priority = $cur ['priority'];
      $db_icon_filename = $cur ['icon_filename'];
      $db_public_note = $cur ['public_note'];
      $db_delete_flag = $cur ['delete_flag'];
      $db_data_entry_comment = $cur ['data_entry_comment'];
      $db_catalog_repeat = $cur ['catalog_repeat'];
      $db_school_id = $cur ['school_id'];



      // First, let's request a new group ID for this new group.
      $new_group_id = $db->request_new_group_id();
      $group_id_array [$db_group_id] = $new_group_id;

      // Now, let's insert this top-level group back into the table
      // as the new_year, with the new_group_id.
      $query = "INSERT INTO draft_groups(`group_id`,`group_name`,
                `title`,`public_note`,`definition`,`icon_filename`,`catalog_year`,
                `priority`,`delete_flag`,`data_entry_comment`, `catalog_repeat`,school_id)
                VALUES (?,?,?,?,?,?,?,?,?,?,?,?) ";
      $res2 = db_query($query, array($new_group_id, $db_group_name, $db_title, $db_public_note,
        $db_definition, $db_icon_filename, $new_year,
        $db_priority, $db_delete_flag, $db_data_entry_comment,
        $db_catalog_repeat, $db_school_id));

      // Okay, now we need to go through the requirements for the group, and copy
      // those over to the new_year.
      $res3 = db_query("SELECT * FROM draft_group_requirements
                WHERE group_id = ? ", $db_group_id);
      while ($cur3 = db_fetch_array($res3)) 
       {

        $child_group_id = 0;
        // Was there a child_group (a branch)?  If so, we need to copy that
        // over too, with a new child_group_id.
        if ($cur3 ['child_group_id'] > 0) {
          // First, create the child group...
          $new_sub_group_id = $db->request_new_group_id();
          $subgroup_id_array [$cur3 ['child_group_id']] = $new_sub_group_id;
          $res4 = db_query("SELECT * FROM draft_group_requirements
                WHERE group_id = ? ", $cur3 ['child_group_id']);
          while ($cur4 = db_fetch_array($res4)) 
           {


            $res5 = db_query("INSERT INTO draft_group_requirements
                      (`group_id`,`course_id`,`course_min_grade`,
                      `course_repeats`,`attributes`,`data_entry_value`)
                      VALUES (?,?,?,?,?,?) ", array($new_sub_group_id, $cur4 ['course_id'],
              $cur4 ['course_min_grade'], $cur4 ['course_repeats'], $cur4 ['attributes'],
              $cur4 ['data_entry_value']));
          }

          // Now, add the replace the db3_child_group_id with this new id
          // so it will get added as a requirement.
          $child_group_id = $new_sub_group_id;
        }

        // Add the row into the table...
        $res5 = db_query("INSERT INTO draft_group_requirements
                  (`group_id`,`course_id`,`course_min_grade`,
                  `course_repeats`,`attributes`,`data_entry_value`,`child_group_id`)
                  VALUES (?,?,?,?,?,?,?) ", array($new_group_id, $cur3 ['course_id'],
          $cur3 ['course_min_grade'], $cur3 ['course_repeats'], $cur3 ['attributes'],
          $cur3 ['data_entry_value'], $child_group_id));


      }
    }

    // Store what groups we are working on, for other modules.
    $_SESSION ['admin_duplicate_year']['groups'] += $group_id_array; // Since we are going in multiple rounds, add to the array.

  } // copy_groups

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


  if ($op_command == 'copy_degree_tracks') {

    $res = db_query("SELECT * FROM draft_degree_tracks WHERE catalog_year = ? ", $old_year);
    while ($cur = db_fetch_array($res)) 
     {

      $res2 = db_query("INSERT INTO draft_degree_tracks
              (`catalog_year`,`major_code`,`track_code`,`track_title`,
              `track_short_title`,`track_description`,school_id)
              VALUES
              (?,?,?,?,?,?,?) ", array($new_year, $cur ['major_code'], $cur ['track_code'], $cur ['track_title'],
        $cur ['track_short_title'], $cur ['track_description'], $cur ['school_id']));

    }

  }


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


  if ($op_command == 'copy_degrees') {

    //////////////////////// COPY DEGREES  ///////////////////////////////////////////
    //  Now, on to transfering the degrees.
    //  We will have to use the groupIDArray we constructed earlier, which
    //  looks like $arr[old_id] = new_id.
    // Do the tracks first, since they are easier and straight forward...

    $start_position = intval($op_val);

    $group_id_array = $_SESSION ['admin_duplicate_year']['groups']; // Load from last time, when we copied groups.


    // Now, let's do the degrees themselves.
    $res = db_query("SELECT * FROM draft_degrees WHERE catalog_year = ?
                        LIMIT $start_position, 15 ", $old_year);
    while ($cur = db_fetch_array($res)) {
      //extract($cur, 3, "db");

      $db_degree_id = $cur ['degree_id'];
      $db_school_id = $cur ['school_id'];
      $db_major_code = $cur ['major_code'];
      $db_degree_type = $cur ['degree_type'];
      $db_degree_level = $cur ['degree_level'];
      $db_degree_class = $cur ['degree_class'];
      $db_title = $cur ['title'];
      $db_semester_titles_csv = $cur ['semester_titles_csv'];
      $db_exclude = $cur ['exclude'];
      $db_public_note = $cur ['public_note'];
      $db_allow_dynamic = $cur ['allow_dynamic'];
      $db_advising_weight = $cur ['advising_weight'];
      $db_override_degree_hours = $cur ['override_degree_hours'];
      $db_min_tracks = $cur ['min_tracks'];
      $db_max_tracks = $cur ['max_tracks'];
      $db_default_tracks = $cur ['default_tracks'];
      $db_track_selection_config = $cur ['track_selection_config'];


      $new_degree_id = $db->request_new_degree_id();

      // Store what degrees we are working on, for other modules.
      $_SESSION ['admin_duplicate_year']['degrees'][] = array(
        'src' => $db_degree_id,
        'src_catalog_year' => $old_year,
        'school_id' => $db_school_id,
        'dest' => $new_degree_id,
        'dest_major_code' => $db_major_code,
        'dest_catalog_year' => $new_year,
      );

      // add in the top-level degree to the table.
      $res2 = db_query("INSERT INTO draft_degrees
              (degree_id, major_code, degree_type, degree_level, degree_class, title,
               semester_titles_csv, catalog_year, exclude, public_note,
               allow_dynamic, advising_weight, override_degree_hours, min_tracks, max_tracks, default_tracks, track_selection_config, school_id)
               VALUES
              (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
              ", array($new_degree_id, $db_major_code, $db_degree_type, $db_degree_level, $db_degree_class, $db_title,
        $db_semester_titles_csv, $new_year, $db_exclude, $db_public_note,
        $db_allow_dynamic, $db_advising_weight, $db_override_degree_hours, $db_min_tracks, $db_max_tracks,
        $db_default_tracks, $db_track_selection_config, $db_school_id));

      // now, pull out all of the degree_requirements.
      $res3 = db_query("SELECT * FROM draft_degree_requirements
                WHERE degree_id = ? ", array($db_degree_id));
      while ($cur3 = db_fetch_array($res3)) 
       {
        //extract($cur3, 3, "db3");
        // If there is a required group, we must convert it to the
        // new groupID !
        $required_group_id = intval($cur3 ['group_id']);
        if ($cur3 ['group_id'] > 0 && trim($cur3 ['group_id']) != "") 
         {
          $o_group_id = $cur3 ['group_id'];
          $required_group_id = $group_id_array [$cur3 ['group_id']];
          if ($required_group_id < 1 || $required_group_id == "") 
           {
            //die("could not find new group for $old_year $db3_group_id!  Group id is $required_group_id");
            fp_add_message(t("Couldn't find the specified group with ID {$cur3 ['group_id']} in catalog year $old_year, required by degree with ID $db_degree_id ($db_major_code).  The group was possibly deleted?
                            Be aware that any degrees in the new year will be missing this group requirement."), 'error', TRUE);
          }
        }


        $res4 = db_query("INSERT INTO draft_degree_requirements
                (degree_id, semester_num, group_id, group_requirement_type,
                group_hours_required, group_min_hours_allowed, group_min_grade, course_id,
                course_min_grade, course_requirement_type, data_entry_value)
                VALUES
                (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
                ", array($new_degree_id, $cur3 ['semester_num'], $required_group_id, $cur3 ['group_requirement_type'],
          $cur3 ['group_hours_required'], $cur3 ['group_min_hours_allowed'], $cur3 ['group_min_grade'], $cur3 ['course_id'],
          $cur3 ['course_min_grade'], $cur3 ['course_requirement_type'], $cur3 ['data_entry_value']));


      }

    } // while draft_degrees

  } // copy_degrees



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


  // Allows other modules to act.
  if ($op_command == 'hook_admin_duplicate_year') {
    invoke_hook('admin_duplicate_year', array($old_year, $new_year));
  }




  $batch ['results']['current'];


  // Have we finished?
  if (++$batch ["results"]["current"] >= $batch ["results"]["total"]) {
    $batch ["results"]["finished"] = TRUE;


    watchdog("admin", "Duplicated all degree/group/courses data from $old_year to $new_year");


    fp_add_message(t("The copy operation is now completed.  You may view the new year
                    by using the Degree & Course Management links on the admin main menu.  Remember, you must
                    apply draft changes before they will be visible in to other users in the system."));


  }



}