diff --git a/admin/quotarow.php b/admin/quotarow.php index 0be68b4c..7da99056 100755 --- a/admin/quotarow.php +++ b/admin/quotarow.php @@ -85,9 +85,16 @@ if (isset($_GET['questionnaire_id']) && isset($_GET['sgqa']) && isset($_GET['va $sample_import_id = bigintval($_GET['sample_import_id']); if ($_GET['sgqa'] != -1) { - $sgqa = $db->quote($_GET['sgqa']); - $comparison = $db->quote($_GET['comparison']); - $value = $db->quote($_GET['value']); + if ($_GET['sgqa'] != -2) + { + $comparison = $db->quote($_GET['comparison']); + $value = $db->quote($_GET['value']); + $sgqa = $db->quote($_GET['sgqa']); + } + else + { + $sgqa = -2; + } $completions = $db->quote($_GET['completions']); } $exclude_val = $db->quote($_GET['exclude_val']); @@ -173,6 +180,9 @@ if ($questionnaire_id != false) { if ($v['lime_sgqa'] == -1) print "
" . T_("Replicate: Where") . " " . $v['exclude_var'] . " " . T_("like") . " " . $v['exclude_val'] . " - "; + else if ($v['lime_sgqa'] == -2) + print "
" . T_("Sample only. Stop calling where") . " " . $v['exclude_var'] . " " . T_("like") . " " . $v['exclude_val'] . " " . T_("rows from this sample when:") . " {$v['completions']} " . T_("completions") . " - "; + else print "
" . T_("Stop calling") . " " . $v['exclude_var'] . " " . T_("like") . " " . $v['exclude_val'] . " " . T_("rows from this sample when:") . " {$v['lime_sgqa']} {$v['comparison']} {$v['value']} " . T_("for") . ": {$v['completions']} " . T_("completions") ." - "; @@ -236,6 +246,10 @@ if ($questionnaire_id != false) $selected = ""; if ($sgqa == -1) $selected = "selected='selected'"; array_unshift($rs,array("value" => -1, "description" => T_("No question (Replicate)"), "selected" => $selected)); + + $selected = ""; + if ($sgqa == -2) $selected = "selected='selected'"; + array_unshift($rs,array("value" => -2, "description" => T_("Sample only quota"), "selected" => $selected)); display_chooser($rs,"sgqa","sgqa",true,"questionnaire_id=$questionnaire_id&sample_import_id=$sample_import_id"); @@ -281,9 +295,13 @@ if ($questionnaire_id != false)


- +

+ + + +
diff --git a/functions/functions.limesurvey.php b/functions/functions.limesurvey.php index ee308370..54a2b76f 100644 --- a/functions/functions.limesurvey.php +++ b/functions/functions.limesurvey.php @@ -106,6 +106,38 @@ function limesurvey_quota_match($lime_sgqa,$lime_sid,$case_id,$value,$comparison return false; } +/** + * Return whether the given case matches the replicate sample only quota + * + * @param int $lime_sid The Limesurvey survey id + * @param int $case_id The case id + * @param string $val The sample value to compare + * @param string $var The sample variable to compare + * + * @return bool|int False if failed, otherwise 1 if matched, 0 if doesn't + * @author Adam Zammit + * @since 2012-04-30 + */ +function limesurvey_quota_replicate_match($lime_sid,$case_id,$val,$var) +{ + global $db; + + $sql = "SELECT count(*) as c + FROM " . LIME_PREFIX . "survey_$lime_sid as s + JOIN `case` as c ON (c.case_id = '$case_id') + JOIN `sample` as sam ON (c.sample_id = sam.sample_id) + JOIN `sample_var` as sv ON (sv.sample_id = sam.sample_id AND sv.var LIKE '$var' AND sv.val LIKE '$val') + WHERE s.token = c.case_id"; + + $rs = $db->GetRow($sql); + + if (isset($rs) && !empty($rs)) + return $rs['c']; + + return false; + +} + /** * Return the number of completions for a given * questionnaire, where the given question has diff --git a/functions/functions.operator.php b/functions/functions.operator.php index d53312b7..8f7c10de 100644 --- a/functions/functions.operator.php +++ b/functions/functions.operator.php @@ -1509,7 +1509,10 @@ function update_row_quota($questionnaire_id,$case_id = false) //the quota criteria, and if so, increment the quota completions counter if ($case_id != false) { - $match = limesurvey_quota_match($r['lime_sgqa'],$r['lime_sid'],$case_id,$r['value'],$r['comparison']); + if ($r['lime_sgqa'] == -2) + $match = limesurvey_quota_replicate_match($r['lime_sid'],$case_id,$r['exclude_val'],$r['exclude_var']); + else + $match = limesurvey_quota_match($r['lime_sgqa'],$r['lime_sid'],$case_id,$r['value'],$r['comparison']); if ($match == 1) { @@ -1527,7 +1530,10 @@ function update_row_quota($questionnaire_id,$case_id = false) } else { - $completions = limesurvey_quota_completions($r['lime_sgqa'],$r['lime_sid'],$r['questionnaire_id'],$r['sample_import_id'],$r['value'],$r['comparison']); + if ($r['lime_sgqa'] == -2) + $completions = limesurvey_quota_replicate_completions($r['lime_sid'],$r['questionnaire_id'],$r['sample_import_id'],$r['exclude_val'],$r['exclude_var']); + else + $completions = limesurvey_quota_completions($r['lime_sgqa'],$r['lime_sid'],$r['questionnaire_id'],$r['sample_import_id'],$r['value'],$r['comparison']); $updatequota = true; }