function admin_process_all_definitions_perform_batch_operation

7.x admin.groups.inc admin_process_all_definitions_perform_batch_operation(&$batch, $de_catalog_year)
6.x admin.groups.inc admin_process_all_definitions_perform_batch_operation(&$batch, $de_catalog_year)
5.x admin.groups.inc admin_process_all_definitions_perform_batch_operation(&$batch, $de_catalog_year)

This actually is the batch operation for processing all of our group definitions.

File

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

Code

function admin_process_all_definitions_perform_batch_operation(&$batch, $de_catalog_year) {


  ////////////////////////////  FIRST TIME ///////////////////////////////
  // 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"]["current"] = 0;
    $batch ["results"]["finished"] = FALSE;

    // Change to count how many groups with definitions OR "catalog_repeat" set.
    // We need a count of how many groups in this year have definitions to process.
    $res = db_query("SELECT count(*) as `count` FROM draft_groups
                       WHERE (definition != '' OR catalog_repeat = 1) 
                       AND catalog_year = ? 
                       AND delete_flag = 0 ", $de_catalog_year);
    $cur = db_fetch_array($res);

    $batch ["results"]["total"] = $cur ["count"];

  }
  ////////////////////////////////////////////////////////////////////////  


  // Okay, we can now begin the actual batch process.
  $current = $batch ["results"]["current"];
  $total = $batch ["results"]["total"];

  $limit = 1; // how many definitions to process per run of THIS function
  $c = 0; // count of records.

  $db = get_global_database_handler();

  // Change to count how many groups with definitions OR "catalog_repeat" set.
  // First, find every group which has a definition set.
  $res = db_query("SELECT * FROM draft_groups
                       WHERE (definition != '' OR catalog_repeat = 1) 
                       AND catalog_year = ? 
                       AND delete_flag = 0 
                       limit $current, $limit", $de_catalog_year);
  while ($cur = db_fetch_array($res)) {

    if ($c >= $limit) {
      break;
    }


    $def = trim($cur ["definition"]);
    $group_id = $cur ["group_id"];
    $group_name = $cur ["group_name"];
    $catalog_repeat = intval($cur ['catalog_repeat']);
    $school_id = intval($cur ['school_id']);

    // Only do definitions if there IS a definition.
    if ($def != "") {
      $temp = admin_get_courses_from_definition($def, "", $school_id);
      $courses = trim($temp ["text"]);
    }
    else {
      // If there are no definitions, assemble a $courses string. admin_get_group_courses(Group $group)

      $g = new Group($group_id, null, -1, false, true); // make sure we are loading from draft.
      $courses = admin_get_group_courses($g);

    }

    // If the catalog_repeat flag is set, process that.
    if ($catalog_repeat == 1) {
      $courses = admin_process_catalog_repeats_for_group_courses_text($courses, $de_catalog_year, $school_id);
    }

    $ccount = 0;

    fp_add_message(t("Working on %name", array("%name" => $group_name)));




    // 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):
    $res2 = db_query("SELECT * FROM draft_group_requirements
                WHERE group_id = ?
                AND child_group_id != '0' ", $group_id);
    while ($cur2 = db_fetch_array($res2)) {
      $cg_id = $cur2 ["child_group_id"];
      $res22 = db_query("DELETE FROM draft_group_requirements
                  WHERE group_id = ? ", $cg_id);
    }
    // Now delete the course requirements...
    $res2 = db_query("DELETE FROM draft_group_requirements
                  WHERE group_id = ? ", $group_id);



    $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, "&")) {
        // 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);


        if ($course_id = $db->get_course_id($subject_id, $course_num, "", true, $school_id, TRUE)) {
          $query = "INSERT INTO draft_group_requirements
                    (`group_id`,`course_id`,
                    `course_min_grade`,`course_repeats`,`data_entry_value`)
                    values (?, ?, ?, ?, ?) ";
          $res2 = db_query($query, $group_id, $course_id, $min_grade, $course_repeats, "$subject_id~$course_num");
          $ccount++;
        }
        else {
          // The course_id could not be found!

          fp_add_message(t("Course not found! You specified %course as a requirement in %gname,
                            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", "%gname" => $group_name)), "error");

        }
      } // if line does not contain &
      else {
        // ELSE, this was a branch!  Insert it as well.

        // 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 ('?','?') ";
          $res2 = 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);


          if ($course_id = $db->get_course_id($subject_id, $course_num, "", true, $school_id, TRUE)) {

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

            fp_add_message(t("Course not found! You specified %course as a requirement in %gname,
                            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", "%gname" => $group_name)), "error");

          }

        } // for $cT
      } // else it does contain &      


    } // for t < count lines

    fp_add_message(t("%name processed. %count courses added.", array("%name" => $group_name, "%count" => $ccount)) . "<br><br>");



    $c++;

  } // while cur = db_fetch_array($res)


  // Update our $batch results variables
  $batch ["results"]["current"] = $current + $c;


  if ($batch ["results"]["current"] >= $total) {
    // We are finished!
    $batch ["results"]["finished"] = TRUE;
    fp_add_message("<br><br><br>" . t("Group definitions have been processed for %year", array("%year" => $de_catalog_year)));
  }


}