14
14
class Randapi extends AbstractExternalModule
15
15
{
16
16
17
+ private $ contentType = "json " ;
18
+
17
19
public function __construct (){
18
20
parent ::__construct ();
19
21
}
@@ -649,6 +651,10 @@ public function handleRequest(stdClass $jsonObject, string $jsonText):void{
649
651
$ newAid = $ this ->handleChangeTarget ($ jsonObject );
650
652
echo json_encode ($ newAid );
651
653
break ;
654
+ case "readConfiguration " : $ this ->readConfiguration ($ jsonObject );
655
+ break ;
656
+ case "readAllocations " : $ this ->readAllocations ($ jsonObject );
657
+ break ;
652
658
default :
653
659
throw new RandapiException ("Invalid Action was specified " );
654
660
}
@@ -663,6 +669,30 @@ public function handleRequest(stdClass $jsonObject, string $jsonText):void{
663
669
}
664
670
}
665
671
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
+
666
696
/**
667
697
* @param stdClass $jsonObject
668
698
* @return bool
@@ -908,4 +938,81 @@ private function handleChangeTarget(stdClass $jsonObject){
908
938
$ eventName );
909
939
}
910
940
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
+
911
1018
}
0 commit comments