2
0
mirror of https://github.com/ACSPRI/queXS synced 2024-04-02 12:12:16 +00:00

New feature: Choose Limesurvey attributes to assign sample records to on case creation

This commit is contained in:
Adam Zammit
2017-05-24 12:51:31 +10:00
parent b71c133c20
commit b2ef33e98a
4 changed files with 196 additions and 50 deletions

View File

@@ -1,3 +1,22 @@
queXS 2.1.0 - Changes since 2.0.1
Database changes required:
CREATE TABLE IF NOT EXISTS `remote_sample_var` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`var_id` bigint(20) NOT NULL,
`questionnaire_id` bigint(20) NOT NULL,
`field` text COLLATE utf8_unicode_ci NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `var_id` (`var_id`,`questionnaire_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=1 ;
INSERT INTO remote_sample_var (var_id,questionnaire_id,field)
SELECT sv.var_id,qs.questionnaire_id,CASE WHEN sv.type = 6 THEN 'firstname' WHEN sv.type = 7 THEN 'lastname' WHEN sv.type = 8 THEN 'email' WHEN sv.type = 9 THEN 'token' END
FROM questionnaire_sample as qs
JOIN sample_import_var_restrict as sv ON (sv.sample_import_id = qs.sample_import_id AND sv.type IN (6,7,8,9));
queXS 2.0.1 - Changes since 2.0.0
This release includes fixes ported from the 1.x branch

View File

@@ -103,6 +103,22 @@ if (isset($_GET['questionnaire_id']) && isset($_GET['sample']) && isset($_GET['
$db->Execute($sql);
foreach($_GET as $key => $val) {
if (substr($key,0,2) == "sv") {
$varid = bigintval(substr($key,2));
if (empty($val)) {
$sql = "DELETE FROM remote_sample_var
WHERE questionnaire_id = $questionnaire_id
AND var_id = $varid";
} else {
$sql = "INSERT INTO remote_sample_var (questionnaire_id,var_id,field)
VALUES ($questionnaire_id,$varid," . $db->qstr($val) . ")
ON DUPLICATE KEY UPDATE field = " . $db->qstr($val);
}
$db->Execute($sql);
}
}
if (isset($_GET['generatecases']))
{
//find the number of sample variables required
@@ -211,6 +227,22 @@ if (isset($_POST['edit']))
AND sample_import_id = '$sid'";
$db->Execute($sql);
foreach($_POST as $key => $val) {
if (substr($key,0,2) == "sv") {
$varid = bigintval(substr($key,2));
if (empty($val)) {
$sql = "DELETE FROM remote_sample_var
WHERE questionnaire_id = $questionnaire_id
AND var_id = $varid";
} else {
$sql = "INSERT INTO remote_sample_var (questionnaire_id,var_id,field)
VALUES ($questionnaire_id,$varid," . $db->qstr($val) . ")
ON DUPLICATE KEY UPDATE field = " . $db->qstr($val);
}
$db->Execute($sql);
}
}
}
@@ -245,6 +277,22 @@ if (isset($_GET['questionnaire_id']) && isset($_GET['rsid']))
<h2 class='col-lg-6'>" . T_("Sample") . ": <span class='text-primary'>" . $qs['description'] . "</span></h2>
<div class='clearfix'></div><div class='panel-body form-group'>";
//Get list of attributes in Limesurvey and how they link to this sample
//
$sql = "SELECT sv.var_id,sv.var,sv.type,rv.field
FROM sample_import_var_restrict as sv
LEFT JOIN remote_sample_var as rv ON (rv.var_id = sv.var_id AND rv.questionnaire_id = $questionnaire_id)
WHERE sv.sample_import_id = $sid";
$svars = $db->GetAll($sql);
include_once("../functions/functions.limesurvey.php");
$attributes = lime_get_token_attributes($questionnaire_id);
$allownew = $selected ="";
if ($qs['random_select'] == 1)
$selected = "checked=\"checked\"";
@@ -269,6 +317,32 @@ if (isset($_GET['questionnaire_id']) && isset($_GET['rsid']))
<div class="col-sm-1"><input type="checkbox" id = "allownew" name="allownew" <?php echo $allownew;?> class="col-sm-1" data-toggle="toggle" data-size="small" data-on="<?php echo T_("Yes");?>" data-off="<?php echo T_("No");?>" data-width="85"/></div><br/><br/><br/>
<input type="hidden" name="questionnaire_id" value="<?php print($questionnaire_id); ?>"/>
<input type="hidden" name="sample_import_id" value="<?php print($sid); ?>"/>
<?php
print "<h4>" . T_("Sample variables to copy to Limesurvey participant table") . "</h4>";
print "<table class='table table-condensed'><tr><th>" . T_("Sample variable") . "</th><th>" . T_("Limesurvey participant variable") . "</th><tr>";
foreach($svars as $sv) {
print "<tr><td><label for='sv{$sv['var_id']}'>{$sv['var']}</label></td>";
print "<td><select name='sv{$sv['var_id']}' id='sv{$sv['var_id']}'>";
print "<option value=''>" . T_("Do not copy to Limesurvey") . "</option>";
foreach($attributes as $key => $val) {
$sel = "";
if ($sv['field'] == $val ||
($sv['type'] == 6 && $val == 'firstname') ||
($sv['type'] == 7 && $val == 'lastname') ||
($sv['type'] == 8 && $val == 'email') ||
($sv['type'] == 9 && $val == 'token')
) {
$sel = "selected='selected'";
}
print "<option $sel value='$val'>$val</option>";
}
print "</select></td></tr>";
}
print "</table>"
?>
<div class="col-sm-offset-4 col-sm-3"><button type="submit" name="edit" class="btn btn-primary"><i class="fa fa-floppy-o fa-lg"></i>&emsp;<?php echo T_("Save changes");?></button></div>
</form></div>
@@ -418,11 +492,34 @@ if ($questionnaire_id != false)
{
print "<div class='clearfix '></div>";
print "<div class='panel-body form-group'><h3 class='text-primary col-lg-offset-4'>" . T_("Add a sample to this questionnaire:") . "</h3>";
$sample_import_id = $qs[0]['sample_import_id'];
if (isset($_GET['sample'])) {
$sample_import_id = bigintval($_GET['sample']);
}
//Get list of attributes in Limesurvey and how they link to this sample
//
$sql = "SELECT sv.var_id,sv.var,sv.type,rv.field
FROM sample_import_var_restrict as sv
LEFT JOIN remote_sample_var as rv ON (rv.var_id = sv.var_id AND rv.questionnaire_id = $questionnaire_id)
WHERE sv.sample_import_id = $sample_import_id";
$svars = $db->GetAll($sql);
include_once("../functions/functions.limesurvey.php");
$attributes = lime_get_token_attributes($questionnaire_id);
if ($attributes == false) {
print "<div>" . T_("Unable to access token/participants table in Limesurvey. Please confirm that this survey has a participant table created.") . "</div>";
} else {
?>
<form action="" method="get" class="form-horizontal">
<label for="sample" class="control-label col-lg-4"><?php echo T_("Select sample:");?></label>
<div class="col-lg-4"><select name="sample" id="sample" class="form-control " >
<?php foreach($qs as $q) { print "<option value=\"{$q['sample_import_id']}\">{$q['description']}</option>"; } ?> </select></div><br/><br/>
<div class="col-lg-4"><select name="sample" id="sample" class="form-control" onchange="location.href = 'assignsample.php?questionnaire_id=<?= $questionnaire_id;?>&sample=' + this.value;">
<?php foreach($qs as $q) { $sel = ""; if ($q['sample_import_id'] == $sample_import_id) $sel = "selected=\"selected\""; print "<option $sel value=\"{$q['sample_import_id']}\">{$q['description']}</option>"; } ?> </select></div><br/><br/>
<label for="call_max" class="control-label col-lg-4"><?php echo T_("Max calls");?></label>
<div class="col-sm-1"><input type="number" min="0" max="20" style="width:6em;" name="call_max" id="call_max" value="0" class="form-control"/></div>
@@ -446,19 +543,47 @@ if ($questionnaire_id != false)
<label for="generatecases" class="control-label col-lg-4"><?php echo T_("Generate cases for all sample records with a valid email address and set outcome to 'Self completion email invitation sent'?");?></label>
<div class="col-sm-1"><input type="checkbox" onchange="if(this.checked==true) {$('#ve').show();} else {$('#ve').hide();}" id = "generatecases" name="generatecases" class="col-sm-1" data-toggle="toggle" data-size="small" data-on="<?php echo T_("Yes");?>" data-off="<?php echo T_("No");?>" data-width="85"/></div>
<em class="control-label"> * <?php echo T_("Ideal if you intend to send an email invitation to sample members before attempting to call using queXS") . " " . T_("Please ensure there are sufficient additional attribute fields in your Limesurvey questionnaire so that sample records can be inserted."); ?></em>
<em class="control-label"> * <?php echo T_("Ideal if you intend to send an email invitation to sample members before attempting to call using queXS"); ?></em>
<div class='clearfix '></div></br>
<div id='ve' style='display:none'><label for="validemail" class="control-label col-lg-4"><?php echo T_("Only generate cases where there is a valid email attached?");?></label>
<div class="col-sm-1"><input type="checkbox" checked="checked" id = "validemail" name="validemail" class="col-sm-1" data-toggle="toggle" data-size="small" data-on="<?php echo T_("Yes");?>" data-off="<?php echo T_("No");?>" data-width="85"/></div>
<div class='clearfix '></div></br></div>
<?php
print "<h4>" . T_("Sample variables to copy to Limesurvey participant table") . "</h4>";
print "<table class='table table-condensed'><tr><th>" . T_("Sample variable") . "</th><th>" . T_("Limesurvey participant variable") . "</th><tr>";
foreach($svars as $sv) {
print "<tr><td><label for='sv{$sv['var_id']}'>{$sv['var']}</label></td>";
print "<td><select name='sv{$sv['var_id']}' id='sv{$sv['var_id']}'>";
print "<option value=''>" . T_("Do not copy to Limesurvey") . "</option>";
foreach($attributes as $key => $val) {
$sel = "";
if ($sv['field'] == $val ||
($sv['type'] == 6 && $val == 'firstname') ||
($sv['type'] == 7 && $val == 'lastname') ||
($sv['type'] == 8 && $val == 'email') ||
($sv['type'] == 9 && $val == 'token')
) {
$sel = "selected='selected'";
}
print "<option $sel value='$val'>$val</option>";
}
print "</select></td></tr>";
}
print "</table>"
?>
<input type="hidden" name="questionnaire_id" value="<?php print($questionnaire_id);?>"/>
<div class="col-lg-offset-4 col-lg-3"><button type="submit" name="add_sample" class="btn btn-primary"><i class="fa fa-plus-circle fa-lg"></i>&emsp;<?php echo T_("Add sample");?></button></div>
</form></div>
<?php
<?php
}
}
}

View File

@@ -67,7 +67,6 @@ function limerpc_init ($url,$user,$pass)
if (is_array($limeKey) && isset($limeKey['status'])) {
die($limeKey['status']);
}
return true;
}
@@ -276,7 +275,37 @@ function lime_get_responses_by_questionnaire($qid,$fields = null)
return $ret;
}
function lime_get_token_attributes($qid)
{
global $limeKey;
global $limeRPC;
$ret = false;
$lime_id = limerpc_init_qid($qid);
if ($lime_id !== false) {
//attribute array (test for all attributes)
for ($i = 1; $i < 256; $i++) {
$aa[] = "attribute_$i";
}
//add a dummy participant
$dtoken = 'QUEXSTESTTOKEN';
try {
$np = $limeRPC->add_participants($limeKey,$lime_id,array(array('firstname'=>$dtoken,'lastname'=>$dtoken)));
} catch (Exception $e) {
limerpc_close();
return false;
}
if (isset($np[0]['tid'])) {
$ret = array_keys($np[0]); //array of data
$limeRPC->delete_participants($limeKey,$lime_id,array($np[0]['tid']));
}
}
limerpc_close();
return $ret;
}
function lime_add_token($qid,$params)
{

View File

@@ -447,55 +447,28 @@ function add_case($sample_id,$questionnaire_id,$operator_id = "NULL",$testing =
if ($lime_sid)
{
$lfirstname = "";
$llastname = "";
$lemail = "";
$params = array('token' => $token);
$params = array('firstname' => "",
'lastname' => "",
'email' => "",
'token' => $token);
//get remote sample fields for this questionaire/sample only
$sql = "SELECT rv.var_id,rv.field
FROM remote_sample_var as rv
JOIN sample as s ON (s.sample_id = $sample_id)
JOIN sample_import_var_restrict as sv ON (s.import_id = sv.sample_import_id AND rv.var_id = sv.var_id)
WHERE questionnaire_id = $questionnaire_id";
$params['firstname'] = ($db->GetOne("SELECT sv.val
FROM sample_var as sv, sample_import_var_restrict as s
WHERE sv.var_id = s.var_id
AND sv.sample_id = '$sample_id'
AND s.type = '6'"));
$ps = $db->GetAll($sql);
$params['lastname'] = ($db->GetOne("SELECT sv.val
FROM sample_var as sv, sample_import_var_restrict as s
WHERE sv.var_id = s.var_id
AND sv.sample_id = '$sample_id'
AND s.type = '7'"));
$params['email'] = ($db->GetOne("SELECT sv.val
FROM sample_var as sv, sample_import_var_restrict as s
WHERE sv.var_id = s.var_id
AND sv.sample_id = '$sample_id'
AND s.type = '8'"));
//copy across all selected variables to matching Limesurvey fields
foreach($ps as $p) {
$params[$p['field']] = $db->GetOne("SELECT val
FROM sample_var
WHERE var_id = {$p['var_id']}
AND sample_id = '$sample_id'");
}
//include limesurvey functions
include_once(dirname(__FILE__).'/functions.limesurvey.php');
if ($addlimeattributes)
{
$sql = "SELECT sv.val
FROM sample_var as sv, sample_import_var_restrict as s
WHERE sv.var_id = s.var_id
AND sv.sample_id = '$sample_id'
AND s.type = '1'
ORDER BY s.var_id ASC";
$vars = $db->GetAll($sql);
$att = 1;
foreach($vars as $v) {
$params['attribute_' . $att] = $v['val'];
$att++;
}
}
$ret = lime_add_token($questionnaire_id,$params);
//fail to create case if can't add remote token