-
Notifications
You must be signed in to change notification settings - Fork 0
/
testrandomization.php
324 lines (275 loc) · 11.6 KB
/
testrandomization.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
<?php
require_once(__DIR__.DIRECTORY_SEPARATOR."vendor/autoload.php");
require_once(__DIR__. "/test_utils/count_allocations.php");
use IU\PHPCap\RedCapProject;
use redcapuzgent\Randapi\model\RandomizationField;
use redcapuzgent\Randapi\model\RandomizationAllocation;
/**
* @param int $projectid
* @param string $token
* @param string $record_id
* @param RandomizationField[] $fields
* @return mixed
*/
function randomizeRecord(int $projectid, string $token, string $record_id,array $fields){
$url = APP_PATH_WEBROOT_FULL."api/?type=module&prefix=Randapi&page=api&NOAUTH&pid=$projectid";
$postfields = [
"action"=>"randomizeRecord",
"token"=>$token,
"parameters"=> [
"recordId" => $record_id,
"fields" => $fields,
"resultFieldName"=>"assignedto"
]
];
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($postfields));
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);
return json_decode($output);
}
/**
* @param $projectid int
* @param $token string
* @param $record_id string
* @return bool
*/
function undoRandomization(int $projectid, string $token, string $record_id){
$url = APP_PATH_WEBROOT_FULL."api/?type=module&prefix=Randapi&page=api&NOAUTH&pid=$projectid";
$postfields = [
"action"=>"undoRandomization",
"token"=>$token,
"parameters"=> $record_id
];
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($postfields));
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);
return json_decode($output);
}
/**
* @param int $projectid
* @param string $token
* @param string $record_id
* @param RandomizationField[] $fields
* @param RandomizationAllocation[] $allocations
* @return int
*/
function changeSourceFields(int $projectid, string $token, string $record_id,array $fields, array $allocations){
$url = APP_PATH_WEBROOT_FULL."api/?type=module&prefix=Randapi&page=api&NOAUTH&pid=$projectid";
$postfields = [
"action"=>"changeSources",
"token"=>$token,
"parameters"=> [
"recordId" => $record_id,
"fields" => $fields,
//"groupId"=>"" // in this test there are nog DAGS defined
"allocations" => $allocations
]
];
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($postfields));
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);
return json_decode($output);
}
/**
* @param int $projectid
* @param string $token
* @param string $record_id
* @return mixed
*/
function findAid(int $projectid, string $token, string $record_id){
$url = APP_PATH_WEBROOT_FULL."api/?type=module&prefix=Randapi&page=api&NOAUTH&pid=$projectid";
$postfields = [
"action"=>"findAID",
"token"=>$token,
"parameters"=> $record_id
];
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($postfields));
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);
return json_decode($output);
}
try {
$token = $_GET["token"];
/* @var $module \redcapuzgent\Randapi\Randapi*/
$apiUrl = APP_PATH_WEBROOT_FULL."api/";
error_log("retrieved token $token and url $apiUrl and projectId ".$module->getProjectId());
$project = new RedCapProject($apiUrl,$token);
$existingRecords = REDCap::getData(array(
'return_format'=>'array',
'fields'=>"record_id"
));
//$existingRecords = $project->exportRecords('php','flat',null,array("record_id"));
error_log("retrieved existing records: " . print_r($existingRecords, true));
if (sizeof($existingRecords) > 0) {
$recordsToDelete = array();
foreach ($existingRecords as $recordId => $record) {
error_log("removing record: " . $recordId);
$recordsToDelete[sizeof($recordsToDelete)] = $recordId;
}
error_log("deleting records with ids: " .print_r($recordsToDelete,true));
$deleteResult = $project->deleteRecords($recordsToDelete);
if ($deleteResult != 3) {
error_log("response after deleting: " . print_r($deleteResult, true));
error_log("Could not import records!!!");
} else {
error_log("deleted successfully");
}
}
$nrOfAvailableSlots = countAllocations($token, $module->getProjectId(),array("1"));
if($nrOfAvailableSlots != 10){
echo "The number of available slots before randomization was not 10 but was $nrOfAvailableSlots";
}
// import test dataset
$testSet = array();
$testSet[0] = new stdClass();
$testSet[0]->record_id = 1;
$testSet[0]->randgroup = 1; //A
$testSet[0]->general_complete = 2; //Complete
$testSet[1] = new stdClass();
$testSet[1]->record_id = 2;
$testSet[1]->randgroup = 1; //A
$testSet[1]->general_complete = 2; //Complete
$testSet[2] = new stdClass();
$testSet[2]->record_id = 3;
$testSet[2]->randgroup = 2; //B
$testSet[2]->general_complete = 2; //Complete
$importResponse = $project->importRecords($testSet);
if ($importResponse != 3) {
error_log("response after importing: " . print_r($importResponse, true));
error_log("Could not import records!!!");
} else {
error_log("imported successfully");
}
// randomize
$assignedAids = array();
foreach ($testSet as $test) {
$fields = array(new RandomizationField("randgroup", $test->randgroup));
try {
$assignedAid = randomizeRecord($module->getProjectId(), $token, $test->record_id,$fields);
error_log("assigned for records $test->record_id aid: $assignedAid");
$assignedAids[$test->record_id - 1] = $assignedAid;
$foundAid = findAid($module->getProjectId(), $token, $test->record_id);
error_log("found aid $foundAid for ".$test->record_id.".");
echo "found aid $foundAid for ".$test->record_id."<br />";
} catch (Exception $e) {
error_log("error while randomizing: " . $e->getMessage() . " " . $e->getTraceAsString());
}
}
$nrOfAvailableSlots = countAllocations($token, $module->getProjectId(),array("1"));
// 2 were assigned to randgroup 1. We expect 8 more to be available.
if($nrOfAvailableSlots != 8){
echo "The number of available slots after randomization was not 8 but was $nrOfAvailableSlots";
}
$testSet[0]->expected = '1'; //X
$testSet[1]->expected = '2'; //Y
$testSet[2]->expected = '1'; //X
$addaptedRecords = $project->exportRecords('php','flat',null,array("record_id","assignedto"));
$i = 0;
foreach($addaptedRecords as $record){
if($record["assignedto"] == $testSet[$i]->expected){
$msg = "record $i randomized ok";
error_log($msg);
echo $msg."<br />";
}else{
$msg = "record $i expected was '".$testSet[$i]->expected."' ".gettype($testSet[$i]->expected)." but found '".$record["assignedto"]."' ".gettype($record["assignedto"]);
error_log($msg);
echo $msg."<br />";
}
$i++;
}
// test undo randomization for record 1
if(undoRandomization($module->getProjectId(), $token, $testSet[0]->record_id)){
echo "Method reports successfull undo of randomization for record 1";
$testSet[0]->expected = ''; //empty
$addaptedRecords = $project->exportRecords('php','flat',null,array("record_id","assignedto"),null, null, "[record_id] = '1'");
if($addaptedRecords[0]["addignedto"] == ""){
echo "assignedto is indeed empty <br/>";
}else{
echo "assignedto is not empty but was '".$addaptedRecords[0]["addignedto"]."' <br/>";
}
}
// test change of the source field for record 2. Changing randgroup from 1 to 2. Expect the target value to be still 2
$testSet[1]->randgroup = 2;
$fields = array(new RandomizationField("randgroup", $testSet[1]->randgroup));
$allocations = array();
$allocations[0] = new RandomizationAllocation(array("2"),"1");
$allocations[1] = new RandomizationAllocation(array("2"),"2");
$newAid = changeSourceFields($module->getProjectId(), $token, "2", $fields, $allocations);
echo "received new aid $newAid</br>";
$addaptedRecords = $project->exportRecords('php','flat',null,array("record_id","assignedto","randgroup"),null, null, "[record_id] = '2'");
if($addaptedRecords[0]["assignedto"] != "2" || $addaptedRecords[0]["randgroup"] != "2"){
echo "Incorrect data after changeSourceFields. assignedto is '".$addaptedRecords[0]["assignedto"]."' expected 2, randgroup is '".$addaptedRecords[0]["randgroup"]."' expected 2</br>";
}else{
echo "Correct data after changeSourceFields";
}
}catch(RandException $e){
error_log("error while executing testrandomization: " . $e->getMessage() . " " . $e->getTraceAsString());
echo $e->getMessage();
}catch(Exception $e){
error_log("error while executing testrandomization: " . $e->getMessage() . " " . $e->getTraceAsString());
echo $e->getMessage();
}