Skip to content

Instantly share code, notes, and snippets.

@tertek
Last active August 19, 2022 10:39
Show Gist options
  • Save tertek/bb20803cc1bad0da24a3771e6417caf4 to your computer and use it in GitHub Desktop.
Save tertek/bb20803cc1bad0da24a3771e6417caf4 to your computer and use it in GitHub Desktop.
REDCap Testing Helpers
<?php
function addRepeatingForm($projectId=null, $eventId=null, $form=null, $customLabel=null) {
// Prepare parameters
if($projectId === null) {
$projectId = PROJECT_ID;
}
if($eventId === null){
$eventId = $this->getEventId($projectId);
}
if($form === null){
$form = "Form " . rand();
}
// Create new instance of Project with projectId
$Proj = new \Project($projectId, true);
// Create new isntance of ProjectDesigner with Project
$projectDesigner = new \Vanderbilt\REDCap\Classes\ProjectDesigner($Proj);
// Create new Form with ProjectDesigner
$created = $projectDesigner->createForm($form);
$formName = $this->getFormNameByForm($form);
// Set newly created Form as repeating Form
if($created) {
$sql_all[] = $sql = "insert into redcap_events_repeat (event_id, form_name, custom_repeat_form_label)
values ({$eventId}, '".db_escape($formName)."', ".checkNull($customLabel).")";
db_query($sql);
}
if (!empty($sql_all)) {
\Logging::logEvent(implode(";\n", $sql_all),"redcap_events_repeat","MANAGE",PROJECT_ID,"","Set up repeating instrument for ".$form);
}
return $formName;
}
function addTextFieldToForm($projectId=null, $formName) {
// Prepare parameters
if($projectId === null) {
$projectId = PROJECT_ID;
}
$field = "Text Field " . rand();
$fieldParams = [
"field_label" => $field,
"field_name" => str_replace(' ', '_', strtolower($field)),
"field_type" => "text",
];
// Create new instance of Project with projectId
$Proj = new \Project($projectId, true);
// Create new isntance of ProjectDesigner with Project
$projectDesigner = new \Vanderbilt\REDCap\Classes\ProjectDesigner($Proj);
$projectDesigner->createField(
$formName,
$fieldParams
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment