Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
# Change Log
All notable changes to the Auto-Populate Fields project will be documented in this file.

## [2.3.0] - 2018-07-13
### Added
- Added option to chronologically detect previous event (tbembersimeao)

## [2.2] - 2017-11-22
### Changed
- Update descriptions for this module (Philip Chase)
Expand Down
53 changes: 48 additions & 5 deletions ExternalModule.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class ExternalModule extends AbstractExternalModule {
/**
* @inheritdoc
*/
function hook_every_page_top($project_id) {
function redcap_every_page_top($project_id) {
if (!$project_id) {
return;
}
Expand All @@ -41,6 +41,26 @@ function hook_every_page_top($project_id) {
}
}

/**
* @inheritdoc
*/
function redcap_module_system_enable($version) {
$sql = 'SELECT 1 FROM INFORMATION_SCHEMA.STATISTICS WHERE `table_schema` = DATABASE() AND `table_name` = "redcap_log_event" AND `index_name` = "project_id"';

// Indexing redcap_log_event's project_id column for performance
// reasons.
if (($q = $this->query($sql)) && !db_num_rows($q)) {
$this->query('ALTER TABLE `redcap_log_event` ADD INDEX `project_id` (`project_id`)');
}
}

/**
* @inheritdoc
*/
function redcap_module_system_version_change($version, $old_version) {
$this->redcap_module_system_enable($version);
}

/**
* Extends @DEFAULT action tag.
*
Expand Down Expand Up @@ -85,11 +105,31 @@ function setDefaultValues() {
// Only consider @DEFAULT-FROM-PREVIOUS-EVENT action tag when the
// record is already exists.
array_unshift($action_tags_to_look, '@DEFAULT-FROM-PREVIOUS-EVENT');

if ($this->getProjectSetting('chronological_previous_event')) {
// Getting chronological sequence of events.
$events = array();

$sql = '
SELECT MIN(log_event_id), event_id FROM redcap_log_event
WHERE pk = "' . db_real_escape_string($_GET['id']) . '" AND
project_id = "' . db_real_escape_string($Proj->project_id) . '"
GROUP BY event_id
ORDER BY log_event_id';

if (($q = $this->query($sql)) && db_num_rows($q)) {
while ($result = db_fetch_assoc($q)) {
$events[] = $result['event_id'];
}
}
}
else {
$arm = $Proj->eventInfo[$_GET['event_id']]['arm_num'];
$events = array_keys($Proj->events[$arm]['events']);
}
}

$fields = empty($_GET['page']) ? $Proj->metadata : $Proj->forms[$_GET['page']]['fields'];
$arm = $Proj->eventInfo[$_GET['event_id']]['arm_num'];
$events = array_keys($Proj->events[$arm]['events']);
$entry_num = ($double_data_entry && $user_rights['double_data'] != 0) ? '--' . $user_rights['double_data'] : '';

foreach (array_keys($fields) as $field_name) {
Expand Down Expand Up @@ -118,18 +158,21 @@ function setDefaultValues() {
continue;
}

$prev_event = false;
$source_form = $Proj->metadata[$source_field]['form_name'];

// Getting previous event ID.
foreach ($events as $event) {
if ($event == $_GET['event_id']) {
break;
}

if (in_array($field_info['form_name'], $Proj->eventsForms[$event])) {
if (in_array($source_form, $Proj->eventsForms[$event])) {
$prev_event = $event;
}
}

if (!isset($prev_event)) {
if (!$prev_event) {
continue;
}

Expand Down
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ Provides the possibility to define secondary, tertiary, etc default values. If `
#### @DEFAULT-FROM-PREVIOUS-EVENT
Sets a field's default value based on its own value in a previous event. To map the default value from another field, you may specify the source field name as a parameter to the action tag, e.g `@DEFAULT-FROM-PREVIOUS-EVENT="source_field"`. Analogously to `@DEFAULT_<N>`, `@DEFAULT-FROM-PREVIOUS-EVENT_<N>` is also provided.

If your events are not necessarily arranged in a chronological order, you can enable an option to auto-detect the last chronological event. To do that, go to your project page, then access External Modules (at the left sidebar), and then click on Auto Populate Fields configuration button:

![Default from previous event configuration](img/default_from_previous_event_config.png)

### Mixing @DEFAULT_\<N\> and @DEFAULT-FROM-PREVIOUS-EVENT_\<N\>

When using `@DEFAULT_<N>` and `@DEFAULT-FROM-PREVIOUS-EVENT_<N>` together, using unique numbers on each action tag to ensure the desired precendence. E.g.
Expand Down
11 changes: 10 additions & 1 deletion config.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
"namespace": "AutoPopulateFields\\ExternalModule",
"description": "This module provides rich control of default values for data entry fields via a set of action tags. These action tags allow fields to be populated based on values from an ordered list of fields and static values. The fields can be read from the current event or the previous event in longitudinal projects. <strong><a href=\"https://github.com/ctsit/auto_populate_fields\">See full documentation here</a></strong>.",
"permissions": [
"hook_every_page_top"
"redcap_every_page_top",
"redcap_module_system_enable",
"redcap_module_system_change_version"
],
"authors": [
{
Expand Down Expand Up @@ -51,5 +53,12 @@
"email": "",
"institution": "University of Florida"
}
],
"project-settings": [
{
"key": "chronological_previous_event",
"name": "Enable chronological previous event detection (useful when the events are not necessarily arranged in a chronological order)",
"type": "checkbox"
}
]
}
Binary file added img/default_from_previous_event_config.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.