diff --git a/Randapi.php b/Randapi.php index 75d5a62..84be576 100644 --- a/Randapi.php +++ b/Randapi.php @@ -102,26 +102,48 @@ function randomizeRecord($recordId,$projectId,$fields=array(),$resultFieldName,$ } /** - * @param int $rid + * @param int $projectId The project id * @param int $project_status (0 = development, 1 = production) * @param RandomizationAllocation[] $allocations * @throws Exception */ - public function addRecordsToAllocationTable(int $rid,int $project_status,array $allocations){ - foreach($allocations as $allocation){ - $sourceFieldNames = array(); - for($i = 0; $i < sizeof($allocation->getSourceFields()); $i++){ - $sourceFieldNames[$i] = "source_field".($i+1); + public function addRecordsToAllocationTable(int $projectId,int $project_status,array $allocations){ + + $ridQuery = "select rid from redcap.redcap_randomization where project_id = $projectId"; + $rid = false; + try{ + if($ridQueryResult = $this->query($ridQuery)){ + if($row = $ridQueryResult->fetch_assoc()){ + $rid = $row["rid"]; + } + $ridQueryResult->close(); + }else{ + $msg = "Could not execute ridQuery $ridQuery. "; + error_log($msg); } + }catch(\Exception $e){ + $msg = "Could not execute ridQuery $ridQuery. ".$e->getMessage()." ".$e->getTraceAsString(); + error_log($msg); + } + if($rid) { - $query = " - insert into redcap_randomization_allocation(rid,project_status,target_field,".implode(',',$sourceFieldNames).") - values($rid,$project_status,'".$allocation->getTargetField()."','".implode("','",$allocation->getSourceFields())."');"; + foreach ($allocations as $allocation) { + $sourceFieldNames = array(); + for ($i = 0; $i < sizeof($allocation->getSourceFields()); $i++) { + $sourceFieldNames[$i] = "source_field" . ($i + 1); + } - error_log("Executing query: $query"); - if(!$this->query($query)){ - throw new Exception("Could not add allocation values to table for query: $query. Exception: ".mysqli_error($this->conn)); + $query = " + insert into redcap_randomization_allocation(rid,project_status,target_field," . implode(',', $sourceFieldNames) . ") + values($rid,$project_status,'" . $allocation->getTargetField() . "','" . implode("','", $allocation->getSourceFields()) . "');"; + + error_log("Executing query: $query"); + if (!$this->query($query)) { + throw new Exception("Could not add allocation values to table for query: $query. Exception: " . mysqli_error($this->conn)); + } } + }else{ + throw new Exception("Could not find rid"); } } } \ No newline at end of file diff --git a/api.php b/api.php index f199164..74bb043 100644 --- a/api.php +++ b/api.php @@ -15,11 +15,11 @@ function handleAddAllocation(\redcapuzgent\Randapi\Randapi $randapi, $jsonObject if(!property_exists($jsonObject,"parameters")){ throw new RandapiException("parameters property not found."); } - if(!property_exists($jsonObject->parameters, "rid")){ - throw new RandapiException("parameters->rid property not found."); + if(!property_exists($jsonObject->parameters, "projectId")){ + throw new RandapiException("parameters->projectId property not found."); } - if(!is_numeric($jsonObject->parameters->rid)){ - throw new RandapiException("parameters->rid is not numeric."); + if(!is_numeric($jsonObject->parameters->projectId)){ + throw new RandapiException("parameters->projectId is not numeric."); } if(!property_exists($jsonObject->parameters, "project_status")){ throw new RandapiException("parameters->project_status property not found."); @@ -42,7 +42,7 @@ function handleAddAllocation(\redcapuzgent\Randapi\Randapi $randapi, $jsonObject array_push($allocations,\redcapuzgent\Randapi\RandomizationAllocation::fromstdClass($allocation)); } - $randapi->addRecordsToAllocationTable($jsonObject->parameters->rid, + $randapi->addRecordsToAllocationTable($jsonObject->parameters->projectId, $jsonObject->parameters->project_status, $allocations); } diff --git a/help.php b/help.php index 6aacd04..3753a52 100644 --- a/help.php +++ b/help.php @@ -13,21 +13,22 @@
RandAPI is a rest service that exposes some methods to work with the build in randomization support of REDCap.
+RandAPI is a rest service that exposes some methods to work with the build in Randomization support of REDCap.
Basically the randapi accepts a json object that defines an action and some parameters
+The JSON object can be send to an url (e.g. https://localhost/api/?type=module&prefix=Randapi&page=api&NOAUTH)
-Adds new records to the allocation table
Send the following json object to https://localhost/api/?type=module&prefix=Randapi&page=api&NOAUTH
.
This example adds
{
@@ -45,6 +46,36 @@
}
+
+ Randomizes a record
+This example randomizes a record with id 1 (from project with id 20). The randomization is performed using a field called `randgroup` with value 1. The result should be saved in a field called `assignedto`
+ +
+
+ "action":"randomizeRecord",
+ "parameters":{
+ "recordId":1,
+ "projectId":20,
+ "fields":[
+ {"key":"randgroup","value":"1"}
+ ],
+ "resultFieldName"=>"assignedto"
+ }
+ }
+
+