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 Usage

-

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)

-

Methods

+

Actions

addRecordsToAllocationTable

Adds new records to the allocation table

parameters:

Example:

-

Send the following json object to https://localhost/api/?type=module&prefix=Randapi&page=api&NOAUTH.

+

This example adds

                 
                     {
@@ -45,6 +46,36 @@
                     }
                 
             
+ +

randomizeRecord

+

Randomizes a record

+

parameters:

+ +

Example:

+

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"
+                        }
+                    }
+                
+            
diff --git a/testallocation.php b/testallocation.php index 5366bc7..27742d4 100644 --- a/testallocation.php +++ b/testallocation.php @@ -23,100 +23,77 @@ $project_status = 0; /* @var $module \redcapuzgent\Randapi\Randapi*/ - $project_id = $module->getProjectId(); - $ridQuery = "select rid from redcap.redcap_randomization where project_id = $project_id"; - $rid = false; - if($ridQueryResult = $module->query($ridQuery)){ - - if($row = $ridQueryResult->fetch_assoc()){ - $rid = $row["rid"]; - } - $ridQueryResult->close(); + // call api method + $url = APP_PATH_WEBROOT_FULL."api/?type=module&prefix=Randapi&page=api&NOAUTH"; + $fields = [ + "action"=>"addRecordsToAllocationTable", + "parameters"=> [ + "projectId" => intval($module->getProjectId()), + "project_status" => $project_status, + "allocations" => $allocations + ] + ]; + + $ch = curl_init(); + curl_setopt($ch, CURLOPT_URL, $url); + curl_setopt($ch, CURLOPT_POST, 1); + curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields)); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); + curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // Set to TRUE for production use + curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); // Set to TRUE for production use + curl_setopt($ch, CURLOPT_VERBOSE, 0); + curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); + curl_setopt($ch, CURLOPT_AUTOREFERER, true); + curl_setopt($ch, CURLOPT_MAXREDIRS, 10); + curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST"); + curl_setopt($ch, CURLOPT_FRESH_CONNECT, 1); + + $output = curl_exec($ch); + if(!$output){ + echo 'Curl error: ' . curl_error($ch)."\n"; }else{ - $msg = "Could not execute ridQuery $ridQuery. ".mysqli_error($conn); - echo $msg; - error_log($msg); } + curl_close($ch); + $jsonDecoded = json_decode($output); + + echo "The service returned: $output\n"; + + if($jsonDecoded == "success"){ + + //$module->addRecordsToAllocationTable(intval($rid),$project_status,$allocations); + + // check if allocations where added + $checkQuery = " + select count(*) as aantal + from redcap.redcap_randomization_allocation rra + where rra.rid = $rid and + rra.project_status = $project_status and + rra.source_field1 = '1'"; + if($checkQueryResult = $module->query($checkQuery)) { + $aantal = false; + if ($row = $checkQueryResult->fetch_assoc()) { + $aantal = $row["aantal"]; + } + $checkQueryResult->close(); - if($rid){ - error_log("found rid $rid"); - // call api method - $url = APP_PATH_WEBROOT_FULL."api/?type=module&prefix=Randapi&page=api&NOAUTH"; - $fields = [ - "action"=>"addRecordsToAllocationTable", - "parameters"=> [ - "rid" => intval($rid), - "project_status" => $project_status, - "allocations" => $allocations - ] - ]; - - $ch = curl_init(); - curl_setopt($ch, CURLOPT_URL, $url); - curl_setopt($ch, CURLOPT_POST, 1); - curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields)); - curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); - curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // Set to TRUE for production use - curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); // Set to TRUE for production use - curl_setopt($ch, CURLOPT_VERBOSE, 0); - curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); - curl_setopt($ch, CURLOPT_AUTOREFERER, true); - curl_setopt($ch, CURLOPT_MAXREDIRS, 10); - curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST"); - curl_setopt($ch, CURLOPT_FRESH_CONNECT, 1); - - $output = curl_exec($ch); - if(!$output){ - echo 'Curl error: ' . curl_error($ch)."\n"; - }else{ - } - curl_close($ch); - $jsonDecoded = json_decode($output); - - echo "The service returned: $output\n"; - - if($jsonDecoded == "success"){ - - //$module->addRecordsToAllocationTable(intval($rid),$project_status,$allocations); - - // check if allocations where added - $checkQuery = " - select count(*) as aantal - from redcap.redcap_randomization_allocation rra - where rra.rid = $rid and - rra.project_status = $project_status and - rra.source_field1 = '1'"; - if($checkQueryResult = $module->query($checkQuery)) { - $aantal = false; - if ($row = $checkQueryResult->fetch_assoc()) { - $aantal = $row["aantal"]; - } - $checkQueryResult->close(); - - if($aantal){ - $msg = ($aantal == 16?"Added correctly":"Error, aantal: $aantal"); - echo "Test result: $msg"; - error_log($msg); - }else{ - $msg = "Could not get new allocation count"; - echo $msg; - error_log($msg); - } + if($aantal){ + $msg = ($aantal == 16?"Added correctly":"Error, aantal: $aantal"); + echo "Test result: $msg"; + error_log($msg); }else{ - $msg = "Could not execute checkQuery $checkQuery. ".mysqli_error($conn); + $msg = "Could not get new allocation count"; echo $msg; error_log($msg); } - }else{ - $msg = "api call failed: "; + $msg = "Could not execute checkQuery $checkQuery. ".mysqli_error($conn); echo $msg; error_log($msg); } }else{ - $msg = "Could not get rid"; + $msg = "api call failed: "; echo $msg; error_log($msg); } diff --git a/typescript/AddRecordsToAllocationTableParameters.ts b/typescript/AddRecordsToAllocationTableParameters.ts index 775b544..4ad90f1 100644 --- a/typescript/AddRecordsToAllocationTableParameters.ts +++ b/typescript/AddRecordsToAllocationTableParameters.ts @@ -1,7 +1,7 @@ import {RandomizationAllocation} from "./RandomizationAllocation"; export interface AddRecordsToAllocationTableParameters{ - rid: number; + projectId: number; project_status: number; allocations: RandomizationAllocation[]; } \ No newline at end of file