diff --git a/admin/quotareport.php b/admin/quotareport.php index 3ff63856..15b40070 100644 --- a/admin/quotareport.php +++ b/admin/quotareport.php @@ -159,7 +159,7 @@ if ($questionnaire_id) else { $completions = limesurvey_quota_completions($v['lime_sgqa'],$v['lime_sid'],$questionnaire_id,$sample_import_id,$v['value'],$v['comparison']); - $perc = ROUND(($completions / ($v['completions'])) * 100,2); + $perc = ($v['completions'] <= 0 ? 0 : ROUND(($completions / ($v['completions'])) * 100,2)); } //We need to calc Sample size, Sample drawn, Sample remain diff --git a/admin/quotarow.php b/admin/quotarow.php index c04c519a..55d9d314 100755 --- a/admin/quotarow.php +++ b/admin/quotarow.php @@ -141,6 +141,11 @@ if ($questionnaire_id != false) copy_row_quota($questionnaire_id,$sample_import_id,bigintval($_POST['copy_sample_import_id'])); print "

" . T_("Copied quotas") . "

"; } + if (isset($_POST['copy_sample_import_id_with_adjustment'])) + { + copy_row_quota_with_adjusting($questionnaire_id,$sample_import_id,bigintval($_POST['copy_sample_import_id_with_adjustment'])); + print "

" . T_("Copied quotas") . "

"; + } print "

" . T_("Current row quotas (click to delete)") . "

"; @@ -191,6 +196,10 @@ if ($questionnaire_id != false) print "

" . T_("Copy quotas for this sample to (No error/duplicate checking): "); display_chooser($ss,"copy_sample_import_id","copy_sample_import_id",false,false,false,false); print "

"; + + print "

" . T_("Copy quotas for this sample to (No error/duplicate checking) with adjusting: "); + display_chooser($ss,"copy_sample_import_id_with_adjustment","copy_sample_import_id_with_adjustment",false,false,false,false); + print "

"; } } diff --git a/functions/functions.operator.php b/functions/functions.operator.php index 42fce1ff..7738aab8 100644 --- a/functions/functions.operator.php +++ b/functions/functions.operator.php @@ -1142,12 +1142,100 @@ function copy_row_quota($questionnaire_id,$sample_import_id,$copy_sample_import_ AND sample_import_id = '$sample_import_id'"; $db->Execute($sql); + update_quotas($questionnaire_id); $db->CompleteTrans(); } +/** + * Copy row quotas from one sample to another with blocking + * Set quota_reached to 0 by default + * + * @param int $questionnaire_id + * @param int $sample_import_id + * @param int $copy_sample_import_id The sample_import_id to copy to + */ +function copy_row_quota_with_blocking($questionnaire_id,$sample_import_id,$copy_sample_import_id) +{ + global $db; + + $db->StartTrans(); + + //Set quota_reached to 0 always + + $sql = "INSERT INTO questionnaire_sample_quota_row (questionnaire_id,sample_import_id,lime_sgqa,value,comparison,completions,exclude_var,exclude_val,quota_reached,description) + SELECT questionnaire_id, $copy_sample_import_id, lime_sgqa,value,comparison,completions,exclude_var,exclude_val,quota_reached,description + FROM questionnaire_sample_quota_row + WHERE questionnaire_id = '$questionnaire_id' + AND sample_import_id = '$sample_import_id'"; + + $db->Execute($sql); + + + update_quotas($questionnaire_id); + + $db->CompleteTrans(); +} + +/** + * Copy row quotas from one sample to another and adjust completion number appropriately to completed in the sample. + * + * @param int $questionnaire_id + * @param int $sample_import_id + * @param int $copy_sample_import_id The sample_import_id to copy to + */ +function copy_row_quota_with_adjusting($questionnaire_id,$sample_import_id,$copy_sample_import_id) +{ + global $db; + + // Copy quotas (defalt Quexs function) + copy_row_quota_with_blocking($questionnaire_id,$sample_import_id,$copy_sample_import_id); + + $db->StartTrans(); + + // Select quotas from the old sample rows and calculate + $sql = "SELECT questionnaire_sample_quota_row_id,q.questionnaire_id,sample_import_id,lime_sgqa,value,comparison,completions,quota_reached,q.lime_sid,qsq.exclude_var,qsq.exclude_val + FROM questionnaire_sample_quota_row as qsq, questionnaire as q + WHERE qsq.questionnaire_id = '$questionnaire_id' + AND q.questionnaire_id = '$questionnaire_id' + #AND qsq.quota_reached != '1' + AND qsq.lime_sgqa != -1 + AND sample_import_id='".$sample_import_id."' + "; + + $rs = $db->GetAll($sql); + + if (isset($rs) && !empty($rs)) + { + //include limesurvey functions + include_once(dirname(__FILE__).'/functions.limesurvey.php'); + + //update all row quotas for this questionnaire + foreach($rs as $r) + { + $completions = limesurvey_quota_completions($r['lime_sgqa'],$r['lime_sid'],$r['questionnaire_id'],$r['sample_import_id'],$r['value'],$r['comparison']); + if ($completions > 0) + { + //Update adjusting the completion number + $sql = "UPDATE questionnaire_sample_quota_row + SET completions = IF(completions>".$completions.",(completions-".$completions."),0), quota_reached = IF(quota_reached = 0,IF(completions=0,1,0),1) + WHERE questionnaire_id = '".$questionnaire_id."' + AND sample_import_id='".$copy_sample_import_id."' + AND lime_sgqa='".$r['lime_sgqa']."' + AND value='".$r['value']."' + AND comparison='".$r['comparison']."'"; + + $db->Execute($sql); + } + + } + } + + $db->CompleteTrans(); +} + /** * Update the row quota table * @@ -1253,7 +1341,7 @@ function end_case($operator_id) WHERE c.case_id = '$case_id' AND c.outcome_id = o.outcome_id AND o.tryanother = 0 - AND (o.outcome_type_id = 4) + AND o.outcome_type_id = 4 ORDER BY c.call_id DESC LIMIT 1"; @@ -1309,20 +1397,17 @@ function end_case($operator_id) ORDER BY o.contacted DESC,c.call_id DESC LIMIT 1"; - $t = $db->GetRow($sql); + $t = $db->GetRow($sql); - if (!empty($t)) - { - $outcome = $t['outcome_id']; - } + if (!empty($t)) + $outcome = $t['outcome_id']; } else if ($count >= 1) //one or more numbers to be tried again - first code as eligible if ever eligible... { //$r[0]['contact_phone_id']; - //code as eligible if ever eligible, or if referred to the supervisor, code as that if last call $sql = "SELECT c.outcome_id as outcome_id FROM `call` as c - JOIN outcome AS o ON ( c.outcome_id = o.outcome_id AND (o.eligible = 1 OR o.outcome_type_id = 2) ) + JOIN outcome AS o ON ( c.outcome_id = o.outcome_id AND o.eligible = 1) WHERE c.case_id = '$case_id' ORDER BY c.call_id DESC";