redcap_save_record
(REDCap >= 5.11.0)
redcap_save_record
— Allows custom actions to be performed immediately after a record has been saved on a data entry form or survey page
Description
void redcap_save_record ( int $project_id, string $record = NULL, string $instrument, int $event_id, int $group_id = NULL, string $survey_hash = NULL, int $response_id = NULL, int $repeat_instance = 1 )
Allows custom actions to be performed immediately after a record has been saved (either created or modified) on a data entry form or survey page whenever the user/participant clicks the Save/Submit/Next Page button.
NOTE: This hook function differs from the redcap_survey_page(), redcap_survey_complete(), and redcap_data_entry_form() functions in that those are only executed once the page has been rendered, while the redcap_save_record() function is executed during post-processing prior to the page being rendered.
Location of Execution
The function is executed immediately after a record has been saved on a data entry form or survey page whenever the user/participant clicks the Save/Submit/Next Page button.
Parameters
project_id
The project ID number of the REDCap project to which the survey belongs.
record
The name of the record/response that was just created or modified.
instrument
The name of the current data collection instrument (i.e., the unique name, not the instrument label). This corresponds to the value of Column B in the Data Dictionary.
event_id
The event ID number of the current data entry form or survey, in which the event_id corresponds to a defined event in a longitudinal project. For classic projects, there will only ever be one event_id for the project.
group_id
The group ID number corresponding to the data access group to which this record has been assigned. If no DAGs exist or if the record has not been assigned to a DAG, its value will be NULL.
survey_hash
(only for survey pages) The hashed string of alphanumeric text in the survey link (i.e., the "s" parameter in the query string of the survey URL). NOTE: If this is a public survey, the survey hash will always be the same for every participant. If not currently on a survey page, this value will be NULL.
response_id
(only for survey pages) The response ID number of the current survey response, in which the response_id originates from the redcap_surveys_response database table. The response_id is particular to a specific record-event_id pair for the given survey. If the record does not exist yet (e.g., if participant is just beginning the first survey), the response_id value will be NULL. If not currently on a survey page, this value will be NULL.
repeat_instance
(only used for projects with repeating instruments/events) The repeat instance number of the repeating event (if longitudinal) or the repeating instrument (if classic or longitudinal). Default value = 1. NOTE: This parameter was added in REDCap 7.3.4.
Return Values
Nothing. Your function does not need to return anything.
Examples
Example #1:
This example shows a simple way of performing project-specific actions using the $project_id parameter. NOTE: This particular method is not as clean or as easy to maintain for many projects at once. See the next example for a better way to implement project-specific actions for many projects.
function redcap_save_record($project_id, $record, $instrument, $event_id, $group_id, $survey_hash, $response_id, $repeat_instance)
{
// Perform certain actions for specific projects (using "switch" or "if" statements).
switch ($project_id) {
// Perform operations for project_id 212
case 212:
// do something specific for this project ...
break;
// Perform operations for project_id 4775
case 4775:
// do something specific for this project ...
break;
// ...
}
}
Example #2:
A much more manageable way to perform project-specific operations for many projects at once is to create a directory structure where each project has its own subdirectory under the main Hooks directory (e.g., named "redcap/hooks/pid{$project_id}/redcap_save_record.php"). This allows the code for each project to be sandboxed and separated and also makes it more manageable to utilize other files (e.g., PHP, HTML, CSS, and/or JavaScript files) that you can keep in the project's subdirectory (i.e., "pid{$project_id}") in the Hooks folder. Then the designated project handler PHP script can utilize any of the parameters passed in the function to perform actions specific to each project. NOTE: This example assumes that the "hooks" sub-directory is located in the same directory as the PHP file containing the Hook functions.
function redcap_save_record($project_id, $record, $instrument, $event_id, $group_id, $survey_hash, $response_id, $repeat_instance)
{
// Set the full path of the project handler PHP script located inside the
// project-specific sub-folder, which itself exists in the main Hooks folder.
$project_handler_script = dirname(__FILE__) . "/hooks/pid{$project_id}/redcap_save_record.php";
// Check if the project handler PHP script exists for this project, and if so,
// then "include" the script to execute it. If not, do nothing.
if (file_exists($project_handler_script)) include $project_handler_script;
}
Example #3:
This example provides a simple illustration of how one might email a survey administrator based upon a specific response by a participant.
<?php
// This is a project handler script with filename ...redcap/hooks/pid998/redcap_save_record.php
// This script only gets executed by surveys for project_id 998
// Only perform this action for the survey corresponding to the instrument 'mental_health_survey'
if ($instrument == 'mental_health_survey' && $_POST['suicidal'] == '1' && $response_id != null)
{
// If the participant just answered "Yes" (1) to the question "Are you suicidal?" (variable "suicidal"),
// then immediately send an email to the survey administrator so the proper actions may be taken.
$email_text = "A participant (record '$record') noted on the survey that they are suicidal. "
. "Please take appropriate actions immediately to contact them.";
REDCap::email('surveyadmin@mystudy.com', 'redcap@yoursite.edu', 'Suicide alert', $email_text);
}