Ejemplo n.º 1
0
/**
 * Process clinic rules.
 *
 * Test the clinic rules of entire clinic and create a report or patient reminders (can also test
 * on one patient or patients of one provider). The structure of the returned results is dependent on the
 * $organize_mode and $mode parameters.
 * <pre>The results are dependent on the $organize_mode parameter settings
 *   'default' organize_mode:
 *     Returns a two-dimensional array of results organized by rules (dependent on the following $mode settings):
 *       'reminders-due' mode - returns an array of reminders (action array elements plus a 'pid' and 'due_status')
 *       'reminders-all' mode - returns an array of reminders (action array elements plus a 'pid' and 'due_status')
 *       'report' mode        - returns an array of rows for the Clinical Quality Measures (CQM) report
 *   'plans' organize_mode:
 *     Returns similar to default, but organizes by the active plans
 * </pre>
 *
 * @param  integer      $provider      id of a selected provider. If blank, then will test entire clinic. If 'collate_outer' or 'collate_inner', then will test each provider in entire clinic; outer will nest plans  inside collated providers, while inner will nest the providers inside the plans (note inner and outer are only different if organize_mode is set to plans).
 * @param  string       $type          rule filter (active_alert,passive_alert,cqm,amc,patient_reminder). If blank then will test all rules. 
 * @param  string/array $dateTarget    target date (format Y-m-d H:i:s). If blank then will test with current date as target. If an array, then is holding two dates ('dateBegin' and 'dateTarget').
 * @param  string       $mode          choose either 'report' or 'reminders-all' or 'reminders-due' (required)
 * @param  integer      $patient_id    pid of patient. If blank then will check all patients.
 * @param  string       $plan          test for specific plan only
 * @param  string       $organize_mode Way to organize the results (default, plans). See above for organization structure of the results.
 * @param  array        $options       can hold various option (for now, used to hold the manual number of labs for the AMC report)
 * @param  string       $pat_prov_rel  How to choose patients that are related to a chosen provider. 'primary' selects patients that the provider is set as primary provider. 'encounter' selectes patients that the provider has seen. This parameter is only applicable if the $provider parameter is set to a provider or collation setting.
 * @param  integer      $start         applicable patient to start at (when batching process)
 * @param  integer      $batchSize     number of patients to batch (when batching process)
 * @return array                       See above for organization structure of the results.
 */
function test_rules_clinic($provider = '', $type = '', $dateTarget = '', $mode = '', $patient_id = '', $plan = '', $organize_mode = 'default', $options = array(), $pat_prov_rel = 'primary', $start = NULL, $batchSize = NULL)
{
    // If dateTarget is an array, then organize them.
    if (is_array($dateTarget)) {
        $dateArray = $dateTarget;
        $dateTarget = $dateTarget['dateTarget'];
    }
    // Set date to current if not set
    $dateTarget = $dateTarget ? $dateTarget : date('Y-m-d H:i:s');
    // Prepare the results array
    $results = array();
    // If set the $provider to collate_outer (or collate_inner without plans organize mode),
    // then run through this function recursively and return results.
    if ($provider == "collate_outer" || $provider == "collate_inner" && $organize_mode != 'plans') {
        // First, collect an array of all providers
        $query = "SELECT id, lname, fname, npi, federaltaxid FROM users WHERE authorized = 1 ORDER BY lname, fname";
        $ures = sqlStatementCdrEngine($query);
        // Second, run through each provider recursively
        while ($urow = sqlFetchArray($ures)) {
            $newResults = test_rules_clinic($urow['id'], $type, $dateTarget, $mode, $patient_id, $plan, $organize_mode, $options, $pat_prov_rel, $start, $batchSize);
            if (!empty($newResults)) {
                $provider_item['is_provider'] = TRUE;
                $provider_item['prov_lname'] = $urow['lname'];
                $provider_item['prov_fname'] = $urow['fname'];
                $provider_item['npi'] = $urow['npi'];
                $provider_item['federaltaxid'] = $urow['federaltaxid'];
                array_push($results, $provider_item);
                $results = array_merge($results, $newResults);
            }
        }
        // done, so now can return results
        return $results;
    }
    // If set organize-mode to plans, then collects active plans and run through this
    // function recursively and return results.
    if ($organize_mode == "plans") {
        // First, collect active plans
        $plans_resolve = resolve_plans_sql($plan, $patient_id);
        // Second, run through function recursively
        foreach ($plans_resolve as $plan_item) {
            //  (if collate_inner, then nest a collation of providers within each plan)
            if ($provider == "collate_inner") {
                // First, collect an array of all providers
                $query = "SELECT id, lname, fname, npi, federaltaxid FROM users WHERE authorized = 1 ORDER BY lname, fname";
                $ures = sqlStatementCdrEngine($query);
                // Second, run through each provider recursively
                $provider_results = array();
                while ($urow = sqlFetchArray($ures)) {
                    $newResults = test_rules_clinic($urow['id'], $type, $dateTarget, $mode, $patient_id, $plan_item['id'], 'default', $options, $pat_prov_rel, $start, $batchSize);
                    if (!empty($newResults)) {
                        $provider_item['is_provider'] = TRUE;
                        $provider_item['prov_lname'] = $urow['lname'];
                        $provider_item['prov_fname'] = $urow['fname'];
                        $provider_item['npi'] = $urow['npi'];
                        $provider_item['federaltaxid'] = $urow['federaltaxid'];
                        array_push($provider_results, $provider_item);
                        $provider_results = array_merge($provider_results, $newResults);
                    }
                }
                if (!empty($provider_results)) {
                    $plan_item['is_plan'] = TRUE;
                    array_push($results, $plan_item);
                    $results = array_merge($results, $provider_results);
                }
            } else {
                // (not collate_inner, so do not nest providers within each plan)
                $newResults = test_rules_clinic($provider, $type, $dateTarget, $mode, $patient_id, $plan_item['id'], 'default', $options, $pat_prov_rel, $start, $batchSize);
                if (!empty($newResults)) {
                    $plan_item['is_plan'] = TRUE;
                    array_push($results, $plan_item);
                    $results = array_merge($results, $newResults);
                }
            }
        }
        // done, so now can return results
        return $results;
    }
    // Collect applicable patient pids
    $patientData = array();
    $patientData = buildPatientArray($patient_id, $provider, $pat_prov_rel, $start, $batchSize);
    // Go through each patient(s)
    //
    //  If in report mode, then tabulate for each rule:
    //    Total Patients
    //    Patients that pass the filter
    //    Patients that pass the target
    //  If in reminders mode, then create reminders for each rule:
    //    Reminder that action is due soon
    //    Reminder that action is due
    //    Reminder that action is post-due
    //Collect applicable rules
    // Note that due to a limitation in the this function, the patient_id is explicitly
    //  for grouping items when not being done in real-time or for official reporting.
    //  So for cases such as patient reminders on a clinic scale, the calling function
    //  will actually need rather than pass in a explicit patient_id for each patient in
    //  a separate call to this function.
    if ($mode != "report") {
        // Use per patient custom rules (if exist)
        // Note as discussed above, this only works for single patient instances.
        $rules = resolve_rules_sql($type, $patient_id, FALSE, $plan);
    } else {
        // $mode = "report"
        // Only use default rules (do not use patient custom rules)
        $rules = resolve_rules_sql($type, $patient_id, FALSE, $plan);
    }
    foreach ($rules as $rowRule) {
        // If using cqm or amc type, then use the hard-coded rules set.
        // Note these rules are only used in report mode.
        if ($rowRule['cqm_flag'] || $rowRule['amc_flag']) {
            require_once dirname(__FILE__) . "/classes/rulesets/ReportManager.php";
            $manager = new ReportManager();
            if ($rowRule['amc_flag']) {
                // Send array of dates ('dateBegin' and 'dateTarget')
                $tempResults = $manager->runReport($rowRule, $patientData, $dateArray, $options);
            } else {
                // Send target date
                $tempResults = $manager->runReport($rowRule, $patientData, $dateTarget);
            }
            if (!empty($tempResults)) {
                foreach ($tempResults as $tempResult) {
                    array_push($results, $tempResult);
                }
            }
            // Go on to the next rule
            continue;
        }
        // If in reminder mode then need to collect the measurement dates
        //  from rule_reminder table
        $target_dates = array();
        if ($mode != "report") {
            // Calculate the dates to check for
            if ($type == "patient_reminder") {
                $reminder_interval_type = "patient_reminder";
            } else {
                // $type == "passive_alert" or $type == "active_alert"
                $reminder_interval_type = "clinical_reminder";
            }
            $target_dates = calculate_reminder_dates($rowRule['id'], $dateTarget, $reminder_interval_type);
        } else {
            // $mode == "report"
            // Only use the target date in the report
            $target_dates[0] = $dateTarget;
        }
        //Reset the counters
        $total_patients = 0;
        $pass_filter = 0;
        $exclude_filter = 0;
        $pass_target = 0;
        // Find the number of target groups
        $targetGroups = returnTargetGroups($rowRule['id']);
        if (count($targetGroups) == 1 || $mode == "report") {
            //skip this section if not report and more than one target group
            foreach ($patientData as $rowPatient) {
                // Count the total patients
                $total_patients++;
                $dateCounter = 1;
                // for reminder mode to keep track of which date checking
                foreach ($target_dates as $dateFocus) {
                    //Skip if date is set to SKIP
                    if ($dateFocus == "SKIP") {
                        $dateCounter++;
                        continue;
                    }
                    //Set date counter and reminder token (applicable for reminders only)
                    if ($dateCounter == 1) {
                        $reminder_due = "soon_due";
                    } else {
                        if ($dateCounter == 2) {
                            $reminder_due = "due";
                        } else {
                            // $dateCounter == 3
                            $reminder_due = "past_due";
                        }
                    }
                    // First, deal with deceased patients
                    //  (for now will simply not pass the filter, but can add a database item
                    //   if ever want to create rules for dead people)
                    // Could also place this function at the total_patients level if wanted.
                    //  (But then would lose the option of making rules for dead people)
                    // Note using the dateTarget rather than dateFocus
                    if (is_patient_deceased($rowPatient['pid'], $dateTarget)) {
                        continue;
                    }
                    // Check if pass filter
                    $passFilter = test_filter($rowPatient['pid'], $rowRule['id'], $dateFocus);
                    if ($passFilter === "EXCLUDED") {
                        // increment EXCLUDED and pass_filter counters
                        //  and set as FALSE for reminder functionality.
                        $pass_filter++;
                        $exclude_filter++;
                        $passFilter = FALSE;
                    }
                    if ($passFilter) {
                        // increment pass filter counter
                        $pass_filter++;
                    } else {
                        $dateCounter++;
                        continue;
                    }
                    // Check if pass target
                    $passTarget = test_targets($rowPatient['pid'], $rowRule['id'], '', $dateFocus);
                    if ($passTarget) {
                        // increment pass target counter
                        $pass_target++;
                        // send to reminder results
                        if ($mode == "reminders-all") {
                            // place the completed actions into the reminder return array
                            $actionArray = resolve_action_sql($rowRule['id'], '1');
                            foreach ($actionArray as $action) {
                                $action_plus = $action;
                                $action_plus['due_status'] = "not_due";
                                $action_plus['pid'] = $rowPatient['pid'];
                                $results = reminder_results_integrate($results, $action_plus);
                            }
                        }
                        break;
                    } else {
                        // send to reminder results
                        if ($mode != "report") {
                            // place the uncompleted actions into the reminder return array
                            $actionArray = resolve_action_sql($rowRule['id'], '1');
                            foreach ($actionArray as $action) {
                                $action_plus = $action;
                                $action_plus['due_status'] = $reminder_due;
                                $action_plus['pid'] = $rowPatient['pid'];
                                $results = reminder_results_integrate($results, $action_plus);
                            }
                        }
                    }
                    $dateCounter++;
                }
            }
        }
        // Calculate and save the data for the rule
        $percentage = calculate_percentage($pass_filter, $exclude_filter, $pass_target);
        if ($mode == "report") {
            $newRow = array('is_main' => TRUE, 'total_patients' => $total_patients, 'excluded' => $exclude_filter, 'pass_filter' => $pass_filter, 'pass_target' => $pass_target, 'percentage' => $percentage);
            $newRow = array_merge($newRow, $rowRule);
            array_push($results, $newRow);
        }
        // Now run through the target groups if more than one
        if (count($targetGroups) > 1) {
            foreach ($targetGroups as $i) {
                //Reset the target counter
                $pass_target = 0;
                foreach ($patientData as $rowPatient) {
                    $dateCounter = 1;
                    // for reminder mode to keep track of which date checking
                    foreach ($target_dates as $dateFocus) {
                        //Skip if date is set to SKIP
                        if ($dateFocus == "SKIP") {
                            $dateCounter++;
                            continue;
                        }
                        //Set date counter and reminder token (applicable for reminders only)
                        if ($dateCounter == 1) {
                            $reminder_due = "soon_due";
                        } else {
                            if ($dateCounter == 2) {
                                $reminder_due = "due";
                            } else {
                                // $dateCounter == 3
                                $reminder_due = "past_due";
                            }
                        }
                        // First, deal with deceased patients
                        //  (for now will simply not pass the filter, but can add a database item
                        //   if ever want to create rules for dead people)
                        // Could also place this function at the total_patients level if wanted.
                        //  (But then would lose the option of making rules for dead people)
                        // Note using the dateTarget rather than dateFocus
                        if (is_patient_deceased($rowPatient['pid'], $dateTarget)) {
                            continue;
                        }
                        // Check if pass filter
                        $passFilter = test_filter($rowPatient['pid'], $rowRule['id'], $dateFocus);
                        if ($passFilter === "EXCLUDED") {
                            $passFilter = FALSE;
                        }
                        if (!$passFilter) {
                            // increment pass filter counter
                            $dateCounter++;
                            continue;
                        }
                        //Check if pass target
                        $passTarget = test_targets($rowPatient['pid'], $rowRule['id'], $i, $dateFocus);
                        if ($passTarget) {
                            // increment pass target counter
                            $pass_target++;
                            // send to reminder results
                            if ($mode == "reminders-all") {
                                // place the completed actions into the reminder return array
                                $actionArray = resolve_action_sql($rowRule['id'], $i);
                                foreach ($actionArray as $action) {
                                    $action_plus = $action;
                                    $action_plus['due_status'] = "not_due";
                                    $action_plus['pid'] = $rowPatient['pid'];
                                    $results = reminder_results_integrate($results, $action_plus);
                                }
                            }
                            break;
                        } else {
                            // send to reminder results
                            if ($mode != "report") {
                                // place the actions into the reminder return array
                                $actionArray = resolve_action_sql($rowRule['id'], $i);
                                foreach ($actionArray as $action) {
                                    $action_plus = $action;
                                    $action_plus['due_status'] = $reminder_due;
                                    $action_plus['pid'] = $rowPatient['pid'];
                                    $results = reminder_results_integrate($results, $action_plus);
                                }
                            }
                        }
                        $dateCounter++;
                    }
                }
                // Calculate and save the data for the rule
                $percentage = calculate_percentage($pass_filter, $exclude_filter, $pass_target);
                // Collect action for title (just use the first one, if more than one)
                $actionArray = resolve_action_sql($rowRule['id'], $i);
                $action = $actionArray[0];
                if ($mode == "report") {
                    $newRow = array('is_sub' => TRUE, 'action_category' => $action['category'], 'action_item' => $action['item'], 'total_patients' => '', 'excluded' => '', 'pass_filter' => '', 'pass_target' => $pass_target, 'percentage' => $percentage);
                    array_push($results, $newRow);
                }
            }
        }
    }
    // Return the data
    return $results;
}
Ejemplo n.º 2
0
function test_rules_clinic($provider = '', $type = '', $dateTarget = '', $mode = '', $patient_id = '', $plan = '', $organize_mode = 'default')
{
    // Set date to current if not set
    $dateTarget = $dateTarget ? $dateTarget : date('Y-m-d H:i:s');
    // Prepare the results array
    $results = array();
    // If set the $provider to collate_outer (or collate_inner without plans organize mode),
    // then run through this function recursively and return results.
    if ($provider == "collate_outer" || $provider == "collate_inner" && $organize_mode != 'plans') {
        // First, collect an array of all providers
        $query = "SELECT id, lname, fname, npi, federaltaxid FROM users WHERE authorized = 1 ORDER BY lname, fname";
        $ures = sqlStatement($query);
        // Second, run through each provider recursively
        while ($urow = sqlFetchArray($ures)) {
            $newResults = test_rules_clinic($urow['id'], $type, $dateTarget, $mode, $patient_id, $plan, $organize_mode);
            if (!empty($newResults)) {
                $provider_item['is_provider'] = TRUE;
                $provider_item['prov_lname'] = $urow['lname'];
                $provider_item['prov_fname'] = $urow['fname'];
                $provider_item['npi'] = $urow['npi'];
                $provider_item['federaltaxid'] = $urow['federaltaxid'];
                array_push($results, $provider_item);
                $results = array_merge($results, $newResults);
            }
        }
        // done, so now can return results
        return $results;
    }
    // If set organize-mode to plans, then collects active plans and run through this
    // function recursively and return results.
    if ($organize_mode == "plans") {
        // First, collect active plans
        $plans_resolve = resolve_plans_sql($plan, $patient_id);
        // Second, run through function recursively
        foreach ($plans_resolve as $plan_item) {
            //  (if collate_inner, then nest a collation of providers within each plan)
            if ($provider == "collate_inner") {
                // First, collect an array of all providers
                $query = "SELECT id, lname, fname, npi, federaltaxid FROM users WHERE authorized = 1 ORDER BY lname, fname";
                $ures = sqlStatement($query);
                // Second, run through each provider recursively
                $provider_results = array();
                while ($urow = sqlFetchArray($ures)) {
                    $newResults = test_rules_clinic($urow['id'], $type, $dateTarget, $mode, $patient_id, $plan_item['id']);
                    if (!empty($newResults)) {
                        $provider_item['is_provider'] = TRUE;
                        $provider_item['prov_lname'] = $urow['lname'];
                        $provider_item['prov_fname'] = $urow['fname'];
                        $provider_item['npi'] = $urow['npi'];
                        $provider_item['federaltaxid'] = $urow['federaltaxid'];
                        array_push($provider_results, $provider_item);
                        $provider_results = array_merge($provider_results, $newResults);
                    }
                }
                if (!empty($provider_results)) {
                    $plan_item['is_plan'] = TRUE;
                    array_push($results, $plan_item);
                    $results = array_merge($results, $provider_results);
                }
            } else {
                // (not collate_inner, so do not nest providers within each plan)
                $newResults = test_rules_clinic($provider, $type, $dateTarget, $mode, $patient_id, $plan_item['id']);
                if (!empty($newResults)) {
                    $plan_item['is_plan'] = TRUE;
                    array_push($results, $plan_item);
                    $results = array_merge($results, $newResults);
                }
            }
        }
        // done, so now can return results
        return $results;
    }
    // Collect all patient ids
    $patientData = array();
    if (!empty($patient_id)) {
        // only look at the selected patient
        array_push($patientData, $patient_id);
    } else {
        if (empty($provider)) {
            // Look at entire practice
            $rez = sqlStatement("SELECT `pid` FROM `patient_data`");
            for ($iter = 0; $row = sqlFetchArray($rez); $iter++) {
                $patientData[$iter] = $row;
            }
        } else {
            // Look at one provider
            $rez = sqlStatement("SELECT `pid` FROM `patient_data` " . "WHERE providerID=?", array($provider));
            for ($iter = 0; $row = sqlFetchArray($rez); $iter++) {
                $patientData[$iter] = $row;
            }
        }
    }
    // Go through each patient(s)
    //
    //  If in report mode, then tabulate for each rule:
    //    Total Patients
    //    Patients that pass the filter
    //    Patients that pass the target
    //  If in reminders mode, then create reminders for each rule:
    //    Reminder that action is due soon
    //    Reminder that action is due
    //    Reminder that action is post-due
    //Collect applicable rules
    if ($mode != "report") {
        // Use per patient custom rules (if exist)
        $rules = resolve_rules_sql($type, $patient_id, FALSE, $plan);
    } else {
        // $mode = "report"
        // Only use default rules (do not use patient custom rules)
        $rules = resolve_rules_sql($type, $patient_id, FALSE, $plan);
    }
    foreach ($rules as $rowRule) {
        // If using cqm or amc type, then use the hard-coded rules set.
        // Note these rules are only used in report mode.
        if ($rowRule['cqm_flag'] || $rowRule['amc_flag']) {
            // Ensure the ruleSet class file has been included
            // (will only require if needed, since it's gonna be large)
            require_once dirname(__FILE__) . "/classes/rulesets/ruleSet.class.php";
            // Run the class rule set
            $rule_results = new ruleSet($rowRule, $dateTarget, $patientData);
            // Collect/add the results to the results array
            $tempResults = $rule_results->return_results();
            if (!empty($tempResults)) {
                foreach ($tempResults as $tempResult) {
                    array_push($results, $tempResult);
                }
            }
            // Go on to the next rule
            continue;
        }
        // If in reminder mode then need to collect the measurement dates
        //  from rule_reminder table
        $target_dates = array();
        if ($mode != "report") {
            // Calculate the dates to check for
            if ($type == "patient_reminder") {
                $reminder_interval_type = "patient_reminder";
            } else {
                // $type == "passive_alert" or $type == "active_alert"
                $reminder_interval_type = "clinical_reminder";
            }
            $target_dates = calculate_reminder_dates($rowRule['id'], $dateTarget, $reminder_interval_type);
        } else {
            // $mode == "report"
            // Only use the target date in the report
            $target_dates[0] = $dateTarget;
        }
        //Reset the counters
        $total_patients = 0;
        $pass_filter = 0;
        $exclude_filter = 0;
        $pass_target = 0;
        foreach ($patientData as $rowPatient) {
            // Count the total patients
            $total_patients++;
            $dateCounter = 1;
            // for reminder mode to keep track of which date checking
            foreach ($target_dates as $dateFocus) {
                //Skip if date is set to SKIP
                if ($dateFocus == "SKIP") {
                    $dateCounter++;
                    continue;
                }
                //Set date counter and reminder token (applicable for reminders only)
                if ($dateCounter == 1) {
                    $reminder_due = "soon_due";
                } else {
                    if ($dateCounter == 2) {
                        $reminder_due = "due";
                    } else {
                        // $dateCounter == 3
                        $reminder_due = "past_due";
                    }
                }
                // Check if pass filter
                $passFilter = test_filter($rowPatient['pid'], $rowRule['id'], $dateFocus);
                if ($passFilter === "EXCLUDED") {
                    // increment EXCLUDED and pass_filter counters
                    //  and set as FALSE for reminder functionality.
                    $pass_filter++;
                    $exclude_filter++;
                    $passFilter = FALSE;
                }
                if ($passFilter) {
                    // increment pass filter counter
                    $pass_filter++;
                } else {
                    $dateCounter++;
                    continue;
                }
                // Check if pass target
                $passTarget = test_targets($rowPatient['pid'], $rowRule['id'], '', $dateFocus);
                if ($passTarget) {
                    // increment pass target counter
                    $pass_target++;
                    // send to reminder results
                    if ($mode == "reminders-all") {
                        // place the completed actions into the reminder return array
                        $actionArray = resolve_action_sql($rowRule['id'], '1');
                        foreach ($actionArray as $action) {
                            $action_plus = $action;
                            $action_plus['due_status'] = "not_due";
                            $action_plus['pid'] = $rowPatient['pid'];
                            $results = reminder_results_integrate($results, $action_plus);
                        }
                    }
                    break;
                } else {
                    // send to reminder results
                    if ($mode != "report") {
                        // place the uncompleted actions into the reminder return array
                        $actionArray = resolve_action_sql($rowRule['id'], '1');
                        foreach ($actionArray as $action) {
                            $action_plus = $action;
                            $action_plus['due_status'] = $reminder_due;
                            $action_plus['pid'] = $rowPatient['pid'];
                            $results = reminder_results_integrate($results, $action_plus);
                        }
                    }
                }
                $dateCounter++;
            }
        }
        // Calculate and save the data for the rule
        $percentage = calculate_percentage($pass_filter, $exclude_filter, $pass_target);
        if ($mode == "report") {
            $newRow = array('is_main' => TRUE, 'total_patients' => $total_patients, 'excluded' => $exclude_filter, 'pass_filter' => $pass_filter, 'pass_target' => $pass_target, 'percentage' => $percentage);
            $newRow = array_merge($newRow, $rowRule);
            array_push($results, $newRow);
        }
        // Find the number of target groups, and go through each one if more than one
        $targetGroups = returnTargetGroups($rowRule['id']);
        if (count($targetGroups) > 1) {
            $firstGroup = true;
            foreach ($targetGroups as $i) {
                // skip first group if not in report mode
                //  (this is because first group was already queried above)
                if ($mode != "report" && $firstGroup) {
                    $firstGroup = false;
                    continue;
                }
                //Reset the target counter
                $pass_target = 0;
                foreach ($patientData as $rowPatient) {
                    $dateCounter = 1;
                    // for reminder mode to keep track of which date checking
                    foreach ($target_dates as $dateFocus) {
                        //Skip if date is set to SKIP
                        if ($dateFocus == "SKIP") {
                            $dateCounter++;
                            continue;
                        }
                        //Set date counter and reminder token (applicable for reminders only)
                        if ($dateCounter == 1) {
                            $reminder_due = "soon_due";
                        } else {
                            if ($dateCounter == 2) {
                                $reminder_due = "due";
                            } else {
                                // $dateCounter == 3
                                $reminder_due = "past_due";
                            }
                        }
                        //Check if pass target
                        $passTarget = test_targets($rowPatient['pid'], $rowRule['id'], $i, $dateFocus);
                        if ($passTarget) {
                            // increment pass target counter
                            $pass_target++;
                            // send to reminder results
                            if ($mode == "reminders-all") {
                                // place the completed actions into the reminder return array
                                $actionArray = resolve_action_sql($rowRule['id'], '1');
                                foreach ($actionArray as $action) {
                                    $action_plus = $action;
                                    $action_plus['due_status'] = "not_due";
                                    $action_plus['pid'] = $rowPatient['pid'];
                                    $results = reminder_results_integrate($results, $action_plus);
                                }
                            }
                            break;
                        } else {
                            // send to reminder results
                            if ($mode != "report") {
                                // place the actions into the reminder return array
                                $actionArray = resolve_action_sql($rowRule['id'], $i);
                                foreach ($actionArray as $action) {
                                    $action_plus = $action;
                                    $action_plus['due_status'] = $reminder_due;
                                    $action_plus['pid'] = $rowPatient['pid'];
                                    $results = reminder_results_integrate($results, $action_plus);
                                }
                            }
                        }
                        $dateCounter++;
                    }
                }
                // Calculate and save the data for the rule
                $percentage = calculate_percentage($pass_filter, $exclude_filter, $pass_target);
                // Collect action for title (just use the first one, if more than one)
                $actionArray = resolve_action_sql($rowRule['id'], $i);
                $action = $actionArray[0];
                if ($mode == "report") {
                    $newRow = array('is_sub' => TRUE, 'action_category' => $action['category'], 'action_item' => $action['item'], 'total_patients' => '', 'excluded' => '', 'pass_filter' => '', 'pass_target' => $pass_target, 'percentage' => $percentage);
                    array_push($results, $newRow);
                }
            }
        }
    }
    // Return the data
    return $results;
}