Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
lsgs authored Aug 7, 2018
1 parent 73da6e4 commit d04ba8b
Show file tree
Hide file tree
Showing 3 changed files with 222 additions and 0 deletions.
134 changes: 134 additions & 0 deletions CodebookConcertina.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
<?php
/**
* REDCap External Module: Codebook Concertina
* Expand and collapse the codebook table rows for the fields of each instrument
* @author Luke Stevens, Murdoch Children's Research Institute
*/
namespace MCRI\CodebookConcertina;

use ExternalModules\AbstractExternalModule;
use REDCap;

class CodebookConcertina extends AbstractExternalModule
{
const DEFAULT_TEXT_SHOW = 'Expand';
const DEFAULT_TEXT_SHOW_ALL = 'Expand all instruments';
const DEFAULT_TEXT_HIDE = 'Collapse';
const DEFAULT_TEXT_HIDE_ALL = 'Collapse all instruments';
const DEFAULT_VISIBILITY = '1';

public function redcap_every_page_top($project_id) {
if (isset($project_id) && intval($project_id)>0 && PAGE==='Design/data_dictionary_codebook.php') {
$btnTextShow = $this->getShowButtonText();
$btnTextShowAll = $this->getShowAllButtonText();
$btnTextHide = $this->getHideButtonText();
$btnTextHideAll = $this->getHideAllButtonText();
$defaultVisibility = $this->getDefaultVisibility();
// add js to page to add toggle buttons and hide/show
?>
<script type='text/javascript'>
(function(window, document, $) {
$(document).ready(function() {
var defaultVisibility = <?php echo $defaultVisibility;?>;
var icons = ['down', 'up'];
var btnLbl = ['<?php echo $btnTextShow;?>', '<?php echo $btnTextHide;?>'];
var btnLblAll = ['<?php echo $btnTextShowAll;?>', '<?php echo $btnTextHideAll;?>'];
var currentForm = '';

function btnLblText(visibility) {
return '<span class="glyphicon glyphicon-chevron-'+icons[visibility]+'"></span>&nbsp;'+btnLbl[visibility];
}

function btnLblAllText(visibility) {
return '<span class="glyphicon glyphicon-chevron-'+icons[visibility]+'"></span>&nbsp;'+btnLblAll[visibility];
}

var toggleRows = function() {
var $this = $(this);
var toggleForm = this.id;
var visible = btnLbl.indexOf($this.text().trim()); // visible when button says "Collapse"
if (visible) {
// collapse and switch button lbl to "Expand" when expanded
$this.html(btnLblText(0));
$('table.ReportTableWithBorder tr.'+toggleForm).hide();
} else {
// expand and switch button lbl to "Collapse" when collapsed
$this.html(btnLblText(1));
$('table.ReportTableWithBorder tr.'+toggleForm).show();
}
};

var toggleAllRows = function() {
var $this = $(this);
var toggleType = btnLblAll.indexOf($this.text().trim());
// trigger click on all buttons with text corresponding to the visibility e.g. if Collapse all, all the Collapse buttons
$('table.ReportTableWithBorder button.toggle-rows:contains("'+btnLbl[toggleType]+'")').trigger('click');
$this.html(btnLblAllText((toggleType)?0:1));
};

$('table.ReportTableWithBorder:first > tbody > tr').each(function() { // main table.ReportTableWithBorder trs only - ignore table.ReportTableWithBorder subtables for sql fields
var rowTDs = $(this).find('td');
if (rowTDs.length===0) {
// this is the th row - do nothing
} else if (rowTDs.length===1) {
// this is a form header row
// - extract the form ref from the final span (<span style="margin-left:10px;color:#444;">(instrument_name)</span>
// - add toggle button
currentForm = $(rowTDs[0]).find('span').last().html().replace('(','').replace(')','');
$('<button type="button" id="toggle-'+currentForm+'" class="btn btn-xs btn-primary toggle-rows" style="float:right;" data-toggle="button">'+btnLblText(defaultVisibility)+'</button>')
.on('click', toggleRows)
.appendTo(rowTDs[0]);
} else {
// this is a variable's tr
// - add a class to target with the toggle
// - hide if default is hidden
$(this).addClass("toggle-"+currentForm);
if (!defaultVisibility) { $(this).hide(); }
}
});

$('<button type="button" id="toggle-all-forms" class="btn btn-xs btn-primary" style="float:right;margin:5px;" data-toggle="button">'+btnLblAllText(defaultVisibility)+'</button>')
.on('click', toggleAllRows)
.insertBefore('table.ReportTableWithBorder:first');
});
})(window, document, jQuery);
</script>
<?php
}
}

protected function getDefaultVisibility() {
$defaultVisibility = $this->getProjectSetting('default-visibility');
if ($defaultVisibility!='0' && $defaultVisibility!='1') {
$defaultVisibility = self::DEFAULT_VISIBILITY;
$this->setProjectSetting('default-visibility', $defaultVisibility);
}
return $defaultVisibility;
}

protected function getShowButtonText() {
return $this->getButtonText('button-text-show', self::DEFAULT_TEXT_SHOW);
}

protected function getShowAllButtonText() {
return $this->getButtonText('button-text-show-all', self::DEFAULT_TEXT_SHOW_ALL);
}

protected function getHideButtonText() {
return $this->getButtonText('button-text-hide', self::DEFAULT_TEXT_HIDE);
}

protected function getHideAllButtonText() {
return $this->getButtonText('button-text-hide-all', self::DEFAULT_TEXT_HIDE_ALL);
}

protected function getButtonText($setting, $default) {
$btnText = $this->getProjectSetting($setting);

if ($btnText=='') {
$this->setProjectSetting($setting, $default);
$btnText = $default;
}
return REDCap::escapeHtml($btnText);
}
}
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
********************************************************************************
# Codebook Concertina

Luke Stevens, Murdoch Children's Research Institute https://www.mcri.edu.au

********************************************************************************
## Summary

The Codebook page displays a table of all project variables in sections
corresponding to the instrument in which they appear.

This module provides a button in each instrument header that the user may click
to collapse or expand the fields for that instrument. A "Collapse all"/"Expand
all" button is also added above the table.

********************************************************************************
72 changes: 72 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
{
"name": "Codebook Concertina",

"description": "On the Codebook page, expand and collapse the table rows for the fields of each instrument.",

"namespace": "MCRI\\CodebookConcertina",

"authors": [
{
"name": "Luke Stevens",
"email": "[email protected]",
"institution": "Murdoch Children's Research Institute"
}
],

"permissions": [
"redcap_every_page_top"
],

"enable-every-page-hooks-on-system-pages": false,

"links": {
},

"no-auth-pages": [
],

"system-settings": [
],

"project-settings": [
{
"key": "default-visibility",
"name": "Default visibility of codebook variable rows",
"required": true,
"type": "radio",
"choices": [
{ "value": "0", "name": "Collapsed" },
{ "value": "1", "name": "Expanded" }
],
"default": "1"
},
{
"key": "button-text-show",
"name": "Button text 'Expand instrument'",
"required": true,
"type": "text",
"default": "Expand"
},
{
"key": "button-text-show-all",
"name": "Button text 'Expand all'",
"required": true,
"type": "text",
"default": "Expand all instruments"
},
{
"key": "button-text-hide",
"name": "Button text 'Collapse instrument'",
"required": true,
"type": "text",
"default": "Collapse"
},
{
"key": "button-text-hide-all",
"name": "Button text 'Collapse all'",
"required": true,
"type": "text",
"default": "Collapse all instruments"
}
]
}

0 comments on commit d04ba8b

Please sign in to comment.