Skip to content

Commit

Permalink
Merge pull request #12 from aarenson/master
Browse files Browse the repository at this point in the history
Remove empty strings/nulls from data; Enable proper numbers/floats; Mark booleans as ints
  • Loading branch information
lsgs authored Aug 9, 2019
2 parents 20cf34b + c46ee78 commit c40c13b
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 29 deletions.
63 changes: 35 additions & 28 deletions TableauConnector.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* Connector Documentation:
* https://github.com/tableau/webdataconnector/blob/master/docs/wdc_tutorial.md
* https://blog.clairvoyantsoft.com/setting-up-a-tableau-web-data-connector-62147e4bc4bf
* TODO:
* TODO:
* - Options for different export types: metadata, report, field/record filters
* (may require additional content in Project ODM XML e.g. report list, indication
* of which instruments are surveys.
Expand All @@ -26,7 +26,7 @@ public function redcap_module_link_check_display()
}

/**
* Print the page of instructions
* Print the page of instructions
*/
public function printInstructionsPageContent() {
$pid=PROJECT_ID;
Expand All @@ -51,17 +51,17 @@ public function getSystemSettingOrDefault($key) {
$config = $this->getSettingConfig($key);
if (array_key_exists('default', $config)) {
$value = $config['default'];
}
}
}
return $value;
}

/**
* Print the module plugin page html content
* Print the module plugin page html content
*/
public function printConnectorPageContent() {
global $login_logo, $institution;

$logoImg = (trim($login_logo)=="") ? "" : "<img src=\"$login_logo\" title=\"$institution\" alt=\"$institution\" style='max-width:850px;'>";
$institution = js_escape2(strip_tags(label_decode($institution)));
$pageTitle = $this->getSystemSettingOrDefault('connector-page-title');
Expand Down Expand Up @@ -152,7 +152,7 @@ public function printConnectorPageContent() {
// Define the schema
myConnector.getSchema = function(schemaCallback) {
var connectionData = JSON.parse(tableau.connectionData);

$.ajax({
url: connectionData.url,
type: "POST",
Expand Down Expand Up @@ -182,8 +182,8 @@ public function printConnectorPageContent() {
// Download the data
myConnector.getData = function(table, doneCallback) {
var connectionData = JSON.parse(tableau.connectionData);
// if subset of fields is specified, look for any checkbox columns

// if subset of fields is specified, look for any checkbox columns
// and swap out for the checkbox field name
var fieldList = [];
if (connectionData.fieldList.length>0) {
Expand All @@ -198,7 +198,7 @@ public function printConnectorPageContent() {
}
});
}

var tableData = [];
$.ajax({
url: connectionData.url,
Expand All @@ -222,7 +222,12 @@ public function printConnectorPageContent() {
dataType: "json",
success: function(resp){
resp.forEach(function(record){
tableData.push(record);
$.each(record, function(key, value) {
if (value === "" || value === null) {
delete record[key];
}
});
tableData.push(record);
});
table.appendRows(tableData);
doneCallback();
Expand Down Expand Up @@ -255,9 +260,9 @@ function buildDataSource(connectionData, response){
var rcFType = varNode.attr('redcap:FieldType');

if (rcFType !== 'descriptive') {
if (fields.length>0 && filterFields &&
if (fields.length>0 && filterFields &&
$.inArray(rcExportVarname, connectionData.fieldList)===-1) {
if (rcFType==='checkbox') {
if (rcFType==='checkbox') {
// if the user has specified the checkbox variable name rather than full export names (with ___) then still allow
var cbNameParts = rcExportVarname.split('___');
if ($.inArray(cbNameParts[0], connectionData.fieldList)===-1) {
Expand All @@ -266,7 +271,7 @@ function buildDataSource(connectionData, response){
} else {
return; // if field list specified, skip if current field is not listed
}
}
}

var f = {};
f.id = rcExportVarname;
Expand All @@ -275,24 +280,25 @@ function buildDataSource(connectionData, response){
f.alias = rcExportVarname;
} else {
f.alias = varNode.find( 'TranslatedText' ).text();
}
}
f.description = varNode.find( 'TranslatedText' ).text();

var dataType = 'string';

if (connectionData.raworlabel==='raw') {
if (connectionData.raworlabel==='raw') {
dataType = varNode.attr('DataType');
}

if (rcFType==='checkbox') {
if (connectionData.varorlabel==='label') {
f.alias = f.description+' (choice='+getCheckboxChoiceLabel($response, rcExportVarname)+')';
}
}

f.description = getCheckboxChoiceLabel($response, rcExportVarname)+' | '+f.description;
}

f.dataType = odmTypeToTableauType(dataType);

fields.push(f);

if (fields.length === 1){ // i.e. directly after record id field...
Expand Down Expand Up @@ -345,44 +351,45 @@ function buildDataSource(connectionData, response){
};

function odmTypeToTableauType(dtype) {

switch (dtype) {
case 'integer': return tableau.dataTypeEnum.string; break;
case 'integer': return tableau.dataTypeEnum.int; break;
case 'text': return tableau.dataTypeEnum.string; break;
case 'float': return tableau.dataTypeEnum.string; break;
case 'float': return tableau.dataTypeEnum.float; break;
case 'date': return tableau.dataTypeEnum.date; break;
case 'datetime': return tableau.dataTypeEnum.datetime; break;
case 'partialDatetime': return tableau.dataTypeEnum.datetime; break;
case 'boolean': return tableau.dataTypeEnum.string; break;
case 'boolean': return tableau.dataTypeEnum.int; break;
default: return tableau.dataTypeEnum.string;
}
};

function getCheckboxChoiceLabel($response, rcExportVarname) {
var choiceOptionString = $response.find( 'CodeList[OID="'+rcExportVarname+'.choices"]' ).attr('redcap:CheckboxChoices');
var choiceVarVal = rcExportVarname.split('___');
var choiceLabel = choiceVarVal;
choiceOptionString.split(' | ').forEach(function(c) {
if (c.lastIndexOf(choiceVarVal[1]+', ', 0)===0) { // if (c.startsWith(choiceVarVal[1]+', ')) { // do not use startsWith() !
choiceLabel = c.replace(choiceVarVal[1]+', ', '');
choiceLabel = c.replace(choiceVarVal[1]+', ', '');
}
});
return choiceLabel;
}

$(document).ready(function (){

$("#submitButton").click(function() {
var exportFormat = $("input[name=\"raworlabel\"]:checked").val();
exportFormat = (exportFormat==='label') ? exportFormat : 'raw';

var exportFieldFormat = $("input[name=\"varorlabel\"]:checked").val();
exportFieldFormat = (exportFieldFormat==='label') ? exportFieldFormat : 'var';

var includeDag = ("1" == $("input[name=\"incldag\"]:checked").val());

var fields = $("input#fieldList").val();
var fieldList = (fields.trim().length>0) ? fields.split(/[ ,\t]+/) : [];
/* Passing tableau.connectionData as an object works in the simulator but not in Tableau.
/* Passing tableau.connectionData as an object works in the simulator but not in Tableau.
* Debugging shows tableau.connectionData = "[object Object]" i.e. that string, not the object!
* (https://tableau.github.io/webdataconnector/docs/wdc_debugging)
* Passing tableau.connectionData as a string is a workaround, hence:
Expand Down Expand Up @@ -412,4 +419,4 @@ function getCheckboxChoiceLabel($response, rcExportVarname) {
</script>
<?php
}
}
}
1 change: 0 additions & 1 deletion wdc.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
* File wdc.php is for the Web Data Connector URL
* @author Luke Stevens, Murdoch Children's Research Institute
*/
use HtmlPage;
$page = new HtmlPage();
$page->PrintHeaderExt();

Expand Down

0 comments on commit c40c13b

Please sign in to comment.