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

New feature: Add records to an existing sample

This commit is contained in:
Adam Zammit
2017-08-07 12:07:36 +10:00
parent ed7ef0deef
commit bed64ed0cb
4 changed files with 237 additions and 6 deletions

View File

@@ -130,6 +130,7 @@ include ("../functions/functions.operator.php");
<li class="has_sub"><a href="" class=""><i class="fa fa-book fa-lg"></i><span><?php print T_("Samples") ;?></span></a>
<ul style="<?php if ($g == 2) echo "display:block";?>">
<li><a href="?g=2&amp;page=import.php"><i class="fa fa-upload fa-fw"></i><?php print T_("Import a sample file") ;?></a></li>
<li><a href="?g=2&amp;page=updatesample.php"><i class="fa fa-plus fa-fw"></i><?php print T_("Add to a sample file") ;?></a></li>
<li><a href="?g=2&amp;page=samplelist.php"><i class="fa fa-list fa-fw"></i><?php print T_("Sample management") ;?></a></li>
<li><a href="?g=2&amp;page=samplesearch.php"><i class="fa fa-search fa-fw"></i><?php print T_("Search the sample") ;?></a></li>
<li><a href="?g=2&amp;page=assignsample.php"><i class="fa fa-link fa-fw"></i><?php print T_("Assign samples to questionnaires") ;?></a></li>

View File

@@ -322,6 +322,7 @@ if (isset($_GET['edit']) )
print "<div class='col-sm-8'><h3>" . T_("Sample") . ":&emsp;" . $sd['description'] . "&emsp;<small>ID <b> $sample_import_id</b> </small></h3></div>";
print "<div class='col-sm-2'><a href='samplesearch.php?sample_import_id=$sample_import_id' class='btn btn-default' ><i class='fa fa-search fa-lg fa-fw text-primary'></i>&emsp;" . T_("Search this sample") . "</a></div>";
print "<div class='col-sm-2'><a href='updatesample.php?sample_import_id=$sample_import_id' class='btn btn-default' ><i class='fa fa-plus fa-lg fa-fw text-primary'></i>&emsp;" . T_("Add to this sample") . "</a></div>";
print "<div class='clearfix'></div>";
if($sd['enabled'] == 0){
@@ -484,6 +485,7 @@ $sql = "SELECT
CONCAT('<a href=\'\' class=\'btn btn-default \' data-toggle=\'confirmation\' data-href=\'?delete_sample=',sample_import_id,'\' data-title=\'" . TQ_("ARE YOU SURE?") . "\' data-btnOkLabel=\'" . TQ_("Yes") . "\' data-btnCancelLabel=\'" . TQ_("Cancel") . "\' ><i class=\'fa fa-trash fa-lg fa-fw text-danger \' data-toggle=\'tooltip\' title=\'" . TQ_("DELETE SAMPLE") . "\'></i></a>')
END as delsample,
CONCAT('<a href=\'samplesearch.php?sample_import_id=',sample_import_id,'\' class=\'btn btn-default\' data-toggle=\'tooltip\' title=\'" . TQ_("Search the sample") . "',sample_import_id,'\'><i class=\'fa fa-search fa-lg fa-fw text-primary\'></i></a>') as ssearch,
CONCAT('<a href=\'updatesample.php?sample_import_id=',sample_import_id,'\' class=\'btn btn-default\' data-toggle=\'tooltip\' title=\'" . TQ_("Add to the sample") . "',sample_import_id,'\'><i class=\'fa fa-plus fa-lg fa-fw text-primary\'></i></a>') as sadd,
CONCAT('<a href=\'callhistory.php?sample_import_id=',sample_import_id,'\' class=\'btn btn-default\' data-toggle=\'tooltip\' title=\'" . TQ_("Call history"). "&ensp;\n" . TQ_("sample"). "&ensp;',sample_import_id,'\'><i class=\'fa fa-phone fa-lg text-primary\'></i></a>') as calls,
CONCAT('<h4>',description,'&emsp;</h4>') as description,
CONCAT('<h4 class=\'fa fa-lg text-primary pull-right\'>',(SELECT COUNT( DISTINCT`sample_var`.sample_id) FROM `sample_var`, `sample` WHERE `sample`.sample_id = `sample_var`.sample_id AND `sample`.import_id = sample_import_id ),'&emsp;</h4>') as cnt
@@ -497,7 +499,7 @@ echo "<div class='form-group'>
<a href='' onclick='history.back();return false;' class='btn btn-default'><i class='fa fa-chevron-left fa-lg text-primary'></i>&emsp;" . T_("Go back") . "</a>
<a href='import.php' class='btn btn-default col-sm-offset-4' ><i class='fa fa-upload fa-lg'></i>&emsp;" . T_("Import a sample file") . "</a>
</div>";
$columns = array("id","description","cnt","status","enabledisable","calls","did","ssearch","delsample"); //"vp","rname",
$columns = array("id","description","cnt","status","enabledisable","calls","did","ssearch","sadd","delsample"); //"vp","rname",
//$titles = array(T_("ID"),T_("Sample"),T_("Records"), T_("Call History"),T_("Enable/Disable"), T_("Status"), T_("Deidentify"), T_("View"), T_("Rename"), T_("Search"), T_("Delete sample"));
xhtml_table($rs,$columns, false, "table-hover table-condensed ");

140
admin/updatesample.php Normal file
View File

@@ -0,0 +1,140 @@
<?php
/**
* Update a sample from a Headered CSV file
*/
/**
* Configuration file
*/
include ("../config.inc.php");
/**
* Authentication file
*/
require ("auth-admin.php");
/**
* XHTML functions
*/
include ("../functions/functions.xhtml.php");
/**
* Sample import functions
*/
include("../functions/functions.import.php");
$css = array(
"../include/bootstrap/css/bootstrap.min.css",
"../include/bootstrap/css/bootstrap-theme.min.css",
"../include/font-awesome/css/font-awesome.css",
"../include/bootstrap-toggle/css/bootstrap-toggle.min.css",
"../css/custom.css"
);
$js_head = array(
"../include/jquery/jquery.min.js",
"../include/bootstrap/js/bootstrap.min.js",
"../include/bootstrap-toggle/js/bootstrap-toggle.min.js",
);
$js_foot = array(
"../js/bootstrap-filestyle.min.js",
"../js/custom.js"
);
if (isset($_POST['import_file']))
{
//file has been submitted
$subtitle = T_("Validating and uploading");
xhtml_head(T_("Import sample") . ":",true,$css,$js_head,false,false,false,$subtitle);
echo "<a href='?' class='btn btn-default pull-left' ><i class='fa fa-chevron-left fa-lg text-primary'></i>&emsp;" . T_("Go back") . "</a>";
$import_id = intval($_POST['import_id']);
$tmpfname = tempnam(TEMPORARY_DIRECTORY, "FOO");
move_uploaded_file($_FILES['file']['tmp_name'],$tmpfname);
$valid = verify_file($tmpfname,$import_id);
if ($valid)
{ //verified so upload
if (update_file($tmpfname,$import_id))
{
print "<div class='well text-primary col-md-offset-2'><p>" . T_("Successfully updated sample") . "&emsp;<h3>$description</h3></p></div>";
}
else
{
print "<div class='alert alert-danger col-md-offset-2'><p>" . T_("Error importing file. Please try again") . "</p></div>";
}
}
else
print "<div class='alert alert-danger col-md-offset-2'><p>" . T_("Error:") . " $error </p><p>" . T_("Please check imported file matches column count and names from original sample file, go back in your browser and fix the problem") . "</p></div>";
xhtml_foot($js_foot);
}
else
{
//need to supply file to upload
$subtitle = T_("Select file to upload");
xhtml_head(T_("Add to existing sample") .":",true,$css,$js_head,false,false,false,$subtitle);
echo "<a href='' onclick='history.back();return false;' class='btn btn-default'><i class='fa fa-chevron-left fa-lg text-primary'></i>&emsp;" . T_("Go back") . "</a>";
$ua = $_SERVER['HTTP_USER_AGENT'];
if (preg_match('/Firefox/i', $ua)) $csv= "text/csv"; else $csv= ".csv";
//print "ua=" . $_SERVER['HTTP_USER_AGENT'];
?>
<form class="form-horizontal col-sm-12 " enctype="multipart/form-data" action="" method="post">
<fieldset>
<input type="hidden" name="MAX_FILE_SIZE" value="1000000000" />
<!-- File Button -->
<div class="form-group">
<label class="col-md-4 control-label" for="file"><?php echo T_("Choose the CSV sample file to upload"); ?>:</label>
<div class="col-md-4">
<input id="file" name="file" class="filestyle" required data-buttonBefore="true" data-iconName="fa fa-folder-open fa-lg text-primary " data-buttonText="<?php echo T_("Select file"); ?>..." type="file" accept="<?php echo $csv; ?>" />
</div>
</div>
<!-- Text input-->
<div class="form-group">
<label class="col-md-4 control-label" for="description"><?php echo T_("Sample"); ?>:</label>
<div class="col-md-4">
<select id="import_id" name="import_id" class="form-inline">
<?php
$sql = "SELECT sample_import_id,description
FROM sample_import
WHERE enabled = 1";
$samples = $db->GetAll($sql);
$selected = "";
foreach ($samples as $s) {
if (isset($_GET['sample_import_id']) && $_GET['sample_import_id'] == $s['sample_import_id']) {
$selected = "selected='selected'";
}
print "<option value=\"{$s['sample_import_id']}\" $selected>{$s['description']}</option>";
}
?>
</select>
</div>
</div>
<!-- Button -->
<div class="form-group">
<label class="col-md-4 control-label" for="submit"></label>
<div class="col-md-4">
<button id="submit" type="submit" name="import_file" class="btn btn-primary"><i class="fa fa-plus-square-o fa-lg"></i>&emsp;<?php echo T_("Add to existing sample"); ?></button>
</div>
</div>
</fieldset>
</form>
<?php
xhtml_foot($js_foot);
}
?>

View File

@@ -53,6 +53,46 @@ function only_numbers($str)
return preg_replace("/[^0-9]/", "",$str);
}
/**
* Verify that a provided file matches an existing sample file
*/
function verify_file($file,$import_id)
{
$row = get_first_row($file);
global $db;
$sql = "SELECT var,type,var_id
FROM sample_import_var_restrict
WHERE sample_import_id = $import_id
ORDER BY var_id ASC";
$sivr = $db->GetAll($sql);
$count = 0;
$nrow = array();
foreach($row as $key => $val) {
$nrow[$key] = strtolower(str_replace(array(" ,",", ",","," "," "),"_", $val));
}
foreach($sivr as $r) {
$var = strtolower($r['var']);
if ($var != $nrow[$count]) {
return false;
}
$count++;
}
//column headings match database
if ($count == count($row)) {
return true;
}
}
/**
* Verify fields in a CSV file
@@ -188,6 +228,38 @@ function get_first_row($file)
return $data;
}
/**
* Add records to an existing sample
*/
function update_file($file,$import_id)
{
global $db;
$db->StartTrans();
$selected_type = array();
$selected_name = array();
$sirv_id = array();
$sql = "SELECT var_id,var,type
FROM sample_import_var_restrict
WHERE sample_import_id = $import_id
ORDER BY var_id ASC";
$sivr = $db->GetAll($sql);
$count = 1;
foreach($sivr as $s) {
$selected_type[$count] = $s['type'];
$selected_name[$count] = $s['var'];
$sirv_id[$count] = $s['var_id'];
$count++;
}
add_to_sample($file,$import_id,$selected_type,$selected_name,$sirv_id,2);
return $db->CompleteTrans();
}
/**
* Import a CSV file to the sample database
@@ -202,10 +274,6 @@ function get_first_row($file)
*/
function import_file($file, $description, $fields, $firstrow = 2)
{
$row = 1;
$handle = fopen($file, "r");
//import into database
global $db;
@@ -246,6 +314,23 @@ function import_file($file, $description, $fields, $firstrow = 2)
}
}
add_to_sample($file,$id,$selected_type,$selected_name,$sirv_id,$firstrow);
return $db->CompleteTrans();
}
/**
* add_to_sample
*
* Add records to the sample file
*
*/
function add_to_sample($file,$id,$selected_type,$selected_name,$sirv_id,$firstrow)
{
global $db;
$db->StartTrans();
/**
* create an ordered index of columns that contain data for obtaining the timezone
* type of 5,4,3,2 preferred
@@ -253,6 +338,9 @@ function import_file($file, $description, $fields, $firstrow = 2)
arsort($selected_type);
$row = 1;
$handle = fopen($file, "r");
$imported = 0;
while (($data = fgetcsv($handle)) !== FALSE)
@@ -346,9 +434,9 @@ function import_file($file, $description, $fields, $firstrow = 2)
unlink($file);
return $db->CompleteTrans();
}
/**
* Get the timezone given the sample value and type
*