Skip to content

Commit

Permalink
Merge pull request #11 from RTIInternational/feature/DateConversion
Browse files Browse the repository at this point in the history
- Added a descriptive label in the configuration so you can keep up w…
  • Loading branch information
dcleblond authored Jan 21, 2025
2 parents f30d9ab + 25ad22e commit 9b8bd8b
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 2 deletions.
8 changes: 7 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,10 @@
## [1.2.6] - 2024-10-04
## Changed
- Added JSON Parsing.
- Added fix for piping into filterLogic being messed up. See README.
- Added fix for piping into filterLogic being messed up. See README.

## [1.3.0] - 2025-01-21
## Changed
- Added a descriptive label in the configuration so you can keep up with your triggers better.
- Added the ability to convert MDY days to YMD for REDCap importing.
- Added the ability to insert escaping for quotes.
41 changes: 41 additions & 0 deletions ConditionalAPITriggerModule.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ public function redcap_save_record($project_id, $record, $instrument, $event_id,

// create the url
$url = Piping::replaceVariablesInLabel($apiUrls[$i], $record, $event_id, $repeat_instance, $recordData, false, $project_id, false);
// not sure why we'd ever want to have dates in the url but I'm CMA here
$url = $this->replaceYMD($url);

$method = $apiMethods[$i];

Expand All @@ -63,6 +65,8 @@ public function redcap_save_record($project_id, $record, $instrument, $event_id,
curl_setopt($conn, CURLOPT_RETURNTRANSFER, true);
if ($apiData[$i] != "") {
$formData = Piping::replaceVariablesInLabel($apiData[$i], $record, $event_id, $repeat_instance, $recordData, false, $project_id, false);
$formData = $this->replaceYMD($formData);
$formData = $this->replaceSlashes($formData);
if ($sanitizeBrackets[$i] == '1') {
// replace {{ and }} with [ and ]
$formData = str_replace('{{', '[', $formData);
Expand All @@ -78,6 +82,8 @@ public function redcap_save_record($project_id, $record, $instrument, $event_id,
$headerArr = array();
if ($apiHeaders[$i] != "") {
$headers = Piping::replaceVariablesInLabel($apiHeaders[$i], $record, $event_id, $repeat_instance, $recordData, false, $project_id, false);
$headers = $this->replaceYMD($headers);
$headers = $this->replaceSlashes($headers);
if ($sanitizeBrackets[$i] == '1') {
// replace {{ and }} with [ and ]
$headers = str_replace('{{', '[', $headers);
Expand Down Expand Up @@ -132,6 +138,41 @@ public function redcap_save_record($project_id, $record, $instrument, $event_id,
}
}

// looks for convertMDYtoYMD() and converts the containing data to that date
private function replaceYMD($formData)
{
$begin = strpos($formData, "convertMDYtoYMD(");
if ($begin === false)
{
return $formData;
}
else
{
$end = strpos($formData, ")", $begin);
$toconvert = substr($formData, $begin + 16, $end - ($begin+16));
$date = date_create_from_format("m-d-Y", $toconvert);
$date = date_format($date, "Y-m-d");
return $this->replaceYMD(substr_replace($formData, $date, $begin, ($end - $begin + 1)));
}
}

// looks for addSlashes<<<>>> and adds slashes to the containing data. Not using () to keep from mucking things up when parenthesis appears in the code
private function replaceSlashes($formData)
{
$begin = strpos($formData, "addSlashes<<<");
if ($begin === false)
{
return $formData;
}
else
{
$end = strpos($formData, ">>>", $begin);
$toconvert = substr($formData, $begin + 13, $end - ($begin+13));
$toconvert = addslashes($toconvert);
return $this->replaceSlashes(substr_replace($formData, $toconvert, $begin, ($end - $begin + 3)));
}
}

private function parseResponse($response, $jsonParsing, $jsonKey, $jsonIsArray, $jsonArrayIndex)
{
if ($jsonParsing == '1') {
Expand Down
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ A REDCap module that executes an API call (to any API) when a certain condition

## How to use
You can define multiple triggers in **Project Settings**. For each trigger, define:
- **Label** - This is a label so you can describe what the trigger does.
- **Instrument** (required)- This is the instrument that the trigger will fire upon saving.
- **Condition** (required)- This is the condition (in addition to the above instrument being saved) that will cause the trigger to fire. This field should evaluate to true/false.
- **API URL** (required)- This is full API URL of the API request. You can pipe variables and smart tags in here.
Expand Down Expand Up @@ -46,4 +47,10 @@ This example imports a value into another REDCap project.
**API Data** `token=ABC123FAKETOKENFAKETOKEN;content=record;action=import;format=json;type=flat;overwriteBehavior=normal;forceAutoNumber=false;data=[{"record_id":"[record_id]","redcap_event_name":"event_1_arm_1","otherconsentvariable":"[consent_yn]"}];returnContent=ids;returnFormat=json`
**Separate Post Data** `Checked`
**Data Item Separator** `;`
**Data Value Separator** `=`
**Data Value Separator** `=`

## Date Conversion
In order to convert anything in the **API Data** or **API Header** from MM-DD-YYYY to YYYY-MM-DDDD, wrap the piped in variable in `convertMDYtoYMD()`. This function call will be removed when the data is actually sent to the API.

## Add Slashes
If you have quotes in your data, this can mess up the API call if the data is being placed into JSON. If this is the case, wrap the piped in variable in `addSlashes<<< >>>` (I use triple angle brackets because it's possible that the data will have parenthesis.) This will escape the quotation marks so they shouldn't cause issues.
5 changes: 5 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@
"type": "sub_settings",
"repeatable": true,
"sub_settings" : [
{
"key": "label",
"name": "Label",
"type": "text"
},
{
"key": "instrument",
"name": "Instrument",
Expand Down

0 comments on commit 9b8bd8b

Please sign in to comment.