Skip to content

Commit a309c0f

Browse files
committed
Add readConfiguration and readAllocations actions
1 parent 586cff0 commit a309c0f

File tree

4 files changed

+125
-3
lines changed

4 files changed

+125
-3
lines changed

Randapi.php

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
class Randapi extends AbstractExternalModule
1515
{
1616

17+
private $contentType = "json";
18+
1719
public function __construct(){
1820
parent::__construct();
1921
}
@@ -649,6 +651,10 @@ public function handleRequest(stdClass $jsonObject, string $jsonText):void{
649651
$newAid = $this->handleChangeTarget($jsonObject);
650652
echo json_encode($newAid);
651653
break;
654+
case "readConfiguration": $this->readConfiguration($jsonObject);
655+
break;
656+
case "readAllocations": $this->readAllocations($jsonObject);
657+
break;
652658
default:
653659
throw new RandapiException("Invalid Action was specified");
654660
}
@@ -663,6 +669,30 @@ public function handleRequest(stdClass $jsonObject, string $jsonText):void{
663669
}
664670
}
665671

672+
public function handleGetRequest(){
673+
$params = new stdClass();
674+
$params->token = $_GET["token"];
675+
if($this->checkToken($params)){
676+
if(isset($_GET["action"]) && isset($_GET["pid"])){
677+
switch($_GET["action"]){
678+
case "readConfiguration": $this->readConfiguration();
679+
break;
680+
case "readAllocations": $this->readAllocations();
681+
break;
682+
default:
683+
throw new RandapiException("Invalid Action was specified");
684+
}
685+
}else{
686+
http_response_code(500);
687+
$exception = new RandapiException("No action or pid was set");
688+
echo json_encode($exception);
689+
}
690+
}else{
691+
error_log("incorrect token");
692+
throw new RandapiException("You don't have sufficient privileges to access this api.",500);
693+
}
694+
}
695+
666696
/**
667697
* @param stdClass $jsonObject
668698
* @return bool
@@ -908,4 +938,81 @@ private function handleChangeTarget(stdClass $jsonObject){
908938
$eventName);
909939
}
910940

941+
/**
942+
*
943+
*/
944+
private function readConfiguration(){
945+
$configQuery = $this->query("select r.*
946+
from redcap_randomization r
947+
where r.project_id = ".$this->getProjectId().";");
948+
$ret = array();
949+
while($row = $configQuery->fetch_assoc()){
950+
foreach($row as $key=>$value){
951+
if(!key_exists($key,$ret)){
952+
$ret[$key]=array();
953+
}
954+
array_push($ret[$key],$value);
955+
}
956+
}
957+
$this->printCsv($ret);
958+
}
959+
960+
/**
961+
* @throws RandapiException
962+
*/
963+
private function readAllocations(){
964+
if(!isset($_GET["status"])){
965+
error_log("parameter status not found.");
966+
throw new RandapiException("parameter status not found.");
967+
}
968+
969+
$allocationsQuery = $this->query("select a.*
970+
from redcap_randomization r
971+
join redcap_randomization_allocation a on
972+
a.rid = r.rid and
973+
a.project_status = ".$_GET["status"]."
974+
where r.project_id = ".$this->getProjectId().";");
975+
$ret = array();
976+
while($row = $allocationsQuery->fetch_assoc()){
977+
foreach($row as $key=>$value){
978+
if(!key_exists($key,$ret)){
979+
$ret[$key]=array();
980+
}
981+
array_push($ret[$key],$value);
982+
}
983+
}
984+
$this->printCsv($ret);
985+
}
986+
987+
/**
988+
* @param array $ret a map with key, the column name and value an array with values for each line.
989+
*/
990+
private function printCsv(array $ret){
991+
$this->setHeaderCSV();
992+
// assume each array has an equal nr of rows
993+
$nrOfRow = sizeof($ret[array_keys($ret)[0]]);
994+
$keys = array_keys($ret);
995+
echo implode(";",$keys)."\r\n";
996+
for($i = 0; $i<$nrOfRow;$i++){
997+
$line = array();
998+
foreach($keys as $key){
999+
array_push($line, $ret[$key][$i]);
1000+
}
1001+
echo implode(";",$line)."\r\n";
1002+
}
1003+
}
1004+
1005+
private function setHeaderCSV(){
1006+
$this->contentType = "csv";
1007+
}
1008+
1009+
public function setHeaders(){
1010+
if($this->contentType == "json"){
1011+
header("Content-Type: application/json");
1012+
}else if($this->contentType == "csv"){
1013+
header("Content-Type: application/csv");
1014+
header("Content-Disposition: attachment;filename=export.csv");
1015+
}
1016+
}
1017+
9111018
}

api.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@
99

1010
if ($_SERVER['REQUEST_METHOD'] == 'POST'){
1111

12-
header('Content-Type: application/json');
13-
1412
$jsonText = file_get_contents("php://input");
1513
error_log("received text: $jsonText");
1614
$jsonObject = json_decode($jsonText, false);
@@ -19,6 +17,10 @@
1917
* @var $module \redcapuzgent\Randapi\Randapi
2018
*/
2119
$module->handleRequest($jsonObject,$jsonText);
20+
$module->setHeaders();
21+
}else if($_SERVER['REQUEST_METHOD'] == 'GET' && isset($_GET["action"])){
22+
$module->handleGetRequest();
23+
$module->setHeaders();
2224
}else{
2325
include('help.php');
2426
}

config.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@
1414
{"0.0.6": "Add unrandomize method"},
1515
{"0.0.7": "Bugfix release. Remove fixed database schema name"},
1616
{"0.0.8": "Add changeSources method"},
17-
{"0.0.9": "Add changeTarget method"}
17+
{"0.0.9": "Add changeTarget method"},
18+
{"0.0.10": "Add readAllocations en readConfiguration"}
1819
],
1920

2021
"authors": [

help.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,18 @@
203203
}
204204
</code>
205205
</pre>
206+
207+
<h2>readConfiguration</h2>
208+
<p>This method must be executed in a GET request and extracts the randomization configuration settings from REDCap for the given project</p>
209+
<p>The API exports this configuration in a CSV format.</p>
210+
<p>Example URL: https://localhost/api/?type=module&prefix=randapi&page=api&pid=19&NOAUTH&token=F33F6876ADC5EC63CE79EBFF88FF0092&action=readConfiguration</p>
211+
212+
<h2>readAllocations</h2>
213+
<p>This method must be executed in a GET request and extracts the allocation table for the given project (and status) from REDCap.</p>
214+
<p>The status parameter is required. The value 0 van be passed for projects in development status and 1 for projects with a production status.</p>
215+
<p>The API exports this allocations in a CSV format.</p>
216+
<p>Example URL: https://localhost/api/?type=module&prefix=randapi&page=api&pid=19&NOAUTH&token=F33F6876ADC5EC63CE79EBFF88FF0092&action=readAllocations&status=0</p>
217+
206218
</div>
207219
</body>
208220
</html>

0 commit comments

Comments
 (0)