";
if ($id)
print "
";
else
diff --git a/functions/functions.limesurvey.php b/functions/functions.limesurvey.php
index c301a521..95b7f933 100644
--- a/functions/functions.limesurvey.php
+++ b/functions/functions.limesurvey.php
@@ -152,7 +152,7 @@ function lime_list_answeroptions($qid,$qcode)
/** Get completed responses as an array based on the case_id
*/
-function lime_get_responses_by_case($case_id,$fields)
+function lime_get_responses_by_case($case_id,$fields = null)
{
global $db;
global $limeRPC;
@@ -165,7 +165,7 @@ function lime_get_responses_by_case($case_id,$fields)
$rs = $db->GetRow($sql);
$token = $rs['token'];
- $qid = $rs['qid'];
+ $qid = $rs['questionnaire_id'];
$lime_id = limerpc_init_qid($qid);
@@ -175,12 +175,10 @@ function lime_get_responses_by_case($case_id,$fields)
$q = $limeRPC->export_responses_by_token($limeKey,$lime_id,'json',$token,null,'complete','code','short',$fields);
if (!isset($q['status'])) {
$ret = json_decode(base64_decode($q));
- //TODO: check how this returns
- var_dump($ret); die();
}
}
- limerpc_close();
+ limerpc_close();
return $ret;
@@ -190,7 +188,7 @@ function lime_get_responses_by_case($case_id,$fields)
/** Get completd responses as an array based on the questionnaire
* indexed by token
*/
-function lime_get_responses_by_questionnaire($qid,$fields)
+function lime_get_responses_by_questionnaire($qid,$fields = null)
{
global $limeRPC;
global $limeKey;
@@ -203,8 +201,6 @@ function lime_get_responses_by_questionnaire($qid,$fields)
$q = $limeRPC->export_responses($limeKey,$lime_id,'json',null,'complete','code','short',null,null,$fields);
if (!isset($q['status'])) {
$ret = json_decode(base64_decode($q));
- //TODO: check how this returns
- var_dump($ret); die();
}
}
@@ -300,7 +296,6 @@ function get_survey_list ()
* @param mixed $email
* @param resource $replace Optional, defaults to ''.
*
- * @return TODO
* @author Adam Zammit
* @since 2013-02-26
*/
@@ -524,6 +519,25 @@ function validate_email($email){
return TRUE;
}
+function lime_compare($val1,$operator,$val2)
+{
+ $val1 = trim($val1);
+ $val2 = trim($val2);
+ if ($operator == "<")
+ return ($val1 < $val2);
+ else if ($operator == ">")
+ return ($val1 > $val2);
+ else if ($operator == "<=")
+ return ($val1 <= $val2);
+ else if ($operator == ">=")
+ return ($val1 >= $val2);
+ else if ($operator == "NOT LIKE" || $operator == "!=")
+ return ($val1 != $val2);
+ else
+ return ($val1 == $val2);
+}
+
+
/**
* Return the number of completions for a given
* questionnaire, where the given question has
@@ -542,25 +556,32 @@ function limesurvey_quota_completions($lime_sgqa,$lime_sid,$questionnaire_id,$sa
{
global $db;
- $resp = lime_get_responses_by_questionnaire($questionnaire_id,array($lime_sgqa));
+ $resp = lime_get_responses_by_questionnaire($questionnaire_id);
- $completions = false;
+ $completions = false;
- if ($resp !== false) {
- $sql = "SELECT c.token
- FROM `case` as c
- JOIN `sample` as sam ON (c.sample_id = sam.sample_id AND sam.import_id = '$sample_import_id')
- WHERE c.questionnaire_id = '$questionnaire_id'";
-
- $rs = $db->GetAssoc($sql);
+ if ($resp !== false) {
+ $sql = "SELECT c.token,c.token as tok
+ FROM `case` as c
+ JOIN `sample` as sam ON (c.sample_id = sam.sample_id AND sam.import_id = '$sample_import_id')
+ WHERE c.questionnaire_id = '$questionnaire_id'";
+
+ $rs = $db->GetAssoc($sql);
- $completions = 0;
-
- foreach($resp as $r) {
-
+ $completions = 0;
+ foreach($resp as $r) {
+ foreach($r as $r1) {
+ foreach($r1 as $rl) {
+ if (isset($rl->token) && isset($rs[$rl->token])) {
+ //match to a case in the sample
+ if (isset($rl->$lime_sgqa)) {
+ $completions += lime_compare($rl->$lime_sgqa, $comparison, $value);
+ }
}
-
+ }
+ }
}
+ }
return $completions;
}
@@ -709,7 +730,7 @@ function limesurvey_percent_complete($case_id)
global $db;
- //TODO: use export_responses_by_token and check the lastpage variable
+ //TODO: use export_responses_by_token and check the lastpage variable?
//
return false;
diff --git a/functions/functions.operator.php b/functions/functions.operator.php
index c3d673df..9ff5a6c2 100644
--- a/functions/functions.operator.php
+++ b/functions/functions.operator.php
@@ -1794,13 +1794,16 @@ function update_single_row_quota($qsqri,$case_id = false)
//get response data from Limesurvey
$resp = lime_get_responses_by_case($case_id);
+ //get the result from the current response
+ $resp = current(current(current($resp)));
foreach($qev as $ev)
{
- $sql2 .= " AND '" . $resp[$ev['lime_sgqa']] . " ' {$ev['comparison']} '{$ev['value']}' ";
+ $sql2 .= " AND '" . trim($resp->$ev['lime_sgqa']) . "' {$ev['comparison']} '{$ev['value']}' ";
}
$match = $db->GetOne($sql2);
+
}
if ($match == 1)
@@ -1841,24 +1844,40 @@ function update_single_row_quota($qsqri,$case_id = false)
else
{
//find all completions from cases with matching limesurvey records
- $sql2 = "SELECT token
- FROM `case` as c
- WHERE c.questionnaire_id = '$questionnaire_id'";
- //get all completed responses from limesurvey, indexed by token
-
include_once(dirname(__FILE__).'/functions.limesurvey.php');
$resp = lime_get_responses_by_questionnaire($questionnaire_id);
- foreach($qev as $ev)
- {
- //TODO: Exclude responses from the $resp array where there isn't a match
- //based on the comparisons
- $sql2 .= " AND s.`{$ev['lime_sgqa']}` {$ev['comparison']} '{$ev['value']}' ";
+ if ($resp !== false) {
+
+ $sql2 = "SELECT c.token,c.token as tok
+ FROM `case` as c
+ JOIN `sample` as sam ON (c.sample_id = sam.sample_id AND sam.import_id = '$sample_import_id')
+ WHERE c.questionnaire_id = '$questionnaire_id'";
+
+ $rs = $db->GetAssoc($sql2);
+
+ foreach($resp as $r) {
+ foreach($r as $r1) {
+ foreach($r1 as $rl) {
+ if (isset($rl->token) && isset($rs[$rl->token])) {
+ //match to a case in the sample
+ $tmp = 0;
+ foreach($qev as $ev) {
+ if (isset($rl->$ev['lime_sgqa'])) {
+ $tmp = lime_compare($rl->$ev['lime_sgqa'], $ev['comparison'], $ev['value']);
+ if ($tmp != 1) {
+ break;
+ }
+ }
+ }
+ $completions += $tmp;
+ }
+ }
+ }
+ }
}
-
- $completions = count($resp);
}
$updatequota = true;
@@ -2483,11 +2502,8 @@ function get_limesurvey_id($operator_id,$rs = false)
else
$sql = "SELECT q.lime_sid as sid";
- $sql .= " FROM `case` as c, questionnaire_sample as qs, sample as s, questionnaire as q
+ $sql .= " FROM `case` as c, questionnaire as q
WHERE c.current_operator_id = '$operator_id'
- AND c.sample_id = s.sample_id
- AND s.import_id = qs.sample_import_id
- AND q.questionnaire_id = qs.questionnaire_id
AND c.questionnaire_id = q.questionnaire_id";
$rs = $db->GetRow($sql);