Skip to content

Commit

Permalink
don't ask for rid but use projectid instead
Browse files Browse the repository at this point in the history
  • Loading branch information
gadeynebram committed Feb 27, 2019
1 parent e689e47 commit f58abe3
Show file tree
Hide file tree
Showing 5 changed files with 132 additions and 102 deletions.
46 changes: 34 additions & 12 deletions Randapi.php
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
}
}
10 changes: 5 additions & 5 deletions api.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.");
Expand All @@ -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);
}
Expand Down
39 changes: 35 additions & 4 deletions help.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,22 @@
<body>
<div class="container">
<h1>RandAPI Usage</h1>
<p>RandAPI is a rest service that exposes some methods to work with the build in randomization support of <a href="https://projectredcap.org/">REDCap</a>.</p>
<p>RandAPI is a rest service that exposes some methods to work with the build in Randomization support of <a href="https://projectredcap.org/">REDCap</a>.</p>
<p>Basically the randapi accepts a json object that defines an action and some parameters</p>
<p>The JSON object can be send to an url (e.g. https://localhost/api/?type=module&prefix=Randapi&page=api&NOAUTH)</p>

<h2>Methods</h2>
<h2>Actions</h2>

<h3>addRecordsToAllocationTable</h3>
<p>Adds new records to the allocation table</p>
<h4>parameters:</h4>
<ul>
<li><b>rid</b>: The record to randomize (integer)</li>
<li><b>rid</b>: Randomization table id (integer)</li>
<li><b>project_status</b>: 0 = development, 1 = production (integer)</li>
<li><b>allocations</b>: array of new allocation values (see RandomizationAllocation.ts)</li>
</ul>
<h4>Example:</h4>
<p>Send the following json object to <code>https://localhost/api/?type=module&prefix=Randapi&page=api&NOAUTH</code>.</p>
<p>This example adds </p>
<pre>
<code>
{
Expand All @@ -45,6 +46,36 @@
}
</code>
</pre>

<h3>randomizeRecord</h3>
<p>Randomizes a record</p>
<h4>parameters:</h4>
<ul>
<li><b>recordId</b>: The record that we want to randomize</li>
<li><b>projectId</b>: The projectId where the record belongs to</li>
<li><b>fields</b>: An array of RandomizationFields</li>
<li><b>resultFieldName</b>: The field where the randomization result can be stored.</li>
<li><b>groupId</b>: (optional) The DAG identifier. default = '' (none)</li>
<li><b>armName</b>: (optional) The name of the arm. default = 'Arm 1'</li>
<li><b>eventName</b>: (optional) The name of the event. default = 'Event 1'</li>
</ul>
<h4>Example:</h4>
<p>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`</p>

<pre>
<code>
"action":"randomizeRecord",
"parameters":{
"recordId":1,
"projectId":20,
"fields":[
{"key":"randgroup","value":"1"}
],
"resultFieldName"=>"assignedto"
}
}
</code>
</pre>
</div>
</body>
</html>
Expand Down
137 changes: 57 additions & 80 deletions testallocation.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
2 changes: 1 addition & 1 deletion typescript/AddRecordsToAllocationTableParameters.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {RandomizationAllocation} from "./RandomizationAllocation";

export interface AddRecordsToAllocationTableParameters{
rid: number;
projectId: number;
project_status: number;
allocations: RandomizationAllocation[];
}

0 comments on commit f58abe3

Please sign in to comment.