-
Notifications
You must be signed in to change notification settings - Fork 7
/
RedCapProject.php
2741 lines (2446 loc) · 112 KB
/
RedCapProject.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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
#-------------------------------------------------------
# Copyright (C) 2019 The Trustees of Indiana University
# SPDX-License-Identifier: BSD-3-Clause
#-------------------------------------------------------
/**
* This file contains the REDCap project class for PHPCap.
*/
namespace IU\PHPCap;
/**
* REDCap project class used to retrieve data from, and modify, REDCap projects.
*/
class RedCapProject
{
const JSON_RESULT_ERROR_PATTERN = '/^[\s]*{"error":[\s]*"(.*)"}[\s]*$/';
/** string REDCap API token for the project */
protected $apiToken;
/** RedCapApiConnection connection to the REDCap API at the $apiURL. */
protected $connection;
/** Error handler for the project. */
protected $errorHandler;
/**
* Creates a REDCapProject object for the specifed project.
*
* Example Usage:
* <code>
* $apiUrl = 'https://redcap.someplace.edu/api/'; # replace with your API URL
* $apiToken = '11111111112222222222333333333344'; # replace with your API token
* $sslVerify = true;
*
* # See the PHPCap documentation for information on how to set this file up
* $caCertificateFile = 'USERTrustRSACertificationAuthority.crt';
*
* $project = new RedCapProject($apiUrl, $apiToken, $sslVerify, $caCertificateFile);
* </code>
*
* @param string $apiUrl the URL for the API for the REDCap site that has the project.
* @param string $apiToken the API token for this project.
* @param boolean $sslVerify indicates if SSL connection to REDCap web site should be verified.
* @param string $caCertificateFile the full path name of the CA (Certificate Authority)
* certificate file.
* @param ErrorHandlerInterface $errorHandler the error handler used by the project.
* This would normally only be set if you want to override the PHPCap's default
* error handler.
* @param RedCapApiConnectionInterface $connection the connection used by the project.
* This would normally only be set if you want to override the PHPCap's default
* connection. If this argument is specified, the $apiUrl, $sslVerify, and
* $caCertificateFile arguments will be ignored, and the values for these
* set in the connection will be used.
* @throws PhpCapException if any of the arguments are invalid
*/
public function __construct(
$apiUrl,
$apiToken,
$sslVerify = false,
$caCertificateFile = null,
$errorHandler = null,
$connection = null
) {
# Need to set errorHandler to default to start in case there is an
# error with the errorHandler passed as an argument
# (to be able to handle that error!)
$this->errorHandler = new ErrorHandler();
if (isset($errorHandler)) {
$this->errorHandler = $this->processErrorHandlerArgument($errorHandler);
}
$this->apiToken = $this->processApiTokenArgument($apiToken);
if (isset($connection)) {
$this->connection = $this->processConnectionArgument($connection);
} else {
$this->apiUrl = $this->processApiUrlArgument($apiUrl);
$sslVerify = $this->processSslVerifyArgument($sslVerify);
$caCertificateFile = $this->processCaCertificateFileArgument($caCertificateFile);
$this->connection = new RedCapApiConnection($apiUrl, $sslVerify, $caCertificateFile);
}
}
/**
* Exports the numbers and names of the arms in the project.
*
* @param $format string the format used to export the arm data.
* <ul>
* <li> 'php' - [default] array of maps of values</li>
* <li> 'csv' - string of CSV (comma-separated values)</li>
* <li> 'json' - string of JSON encoded values</li>
* <li> 'xml' - string of XML encoded data</li>
* </ul>
* @param array $arms array of integers or numeric strings that are the numbers of the arms to export.
* If no arms are specified, then information for all arms will be returned.
*
* @return mixed For 'php' format, array of arrays that have the following keys:
* <ul>
* <li>'arm_num'</li>
* <li>'name'</li>
* </ul>
*/
public function exportArms($format = 'php', $arms = [])
{
$data = array(
'token' => $this->apiToken,
'content' => 'arm',
'returnFormat' => 'json'
);
$legalFormats = array('csv', 'json', 'php', 'xml');
$data['format'] = $this->processFormatArgument($format, $legalFormats);
$data['arms'] = $this->processArmsArgument($arms);
$arms = $this->connection->callWithArray($data);
$arms = $this->processExportResult($arms, $format);
return $arms;
}
/**
* Imports the specified arms into the project.
*
* @param mixed $arms the arms to import. This will
* be a PHP array of associative arrays if no format, or 'php' format was specified,
* and a string otherwise. The field names (keys) used in both cases
* are: arm_num, name
* @param string $format the format for the export.
* <ul>
* <li> 'php' - [default] array of maps of values</li>
* <li> 'csv' - string of CSV (comma-separated values)</li>
* <li> 'json' - string of JSON encoded values</li>
* <li> 'xml' - string of XML encoded data</li>
* </ul>
* @param boolean $override
* <ul>
* <li> false - [default] don't delete existing arms; only add new
* arms or renames existing arms.
* </li>
* <li> true - delete all existing arms before importing.</li>
* </ul>
* @throws PhpCapException if an error occurs.
*
* @return integer the number of arms imported.
*/
public function importArms($arms, $format = 'php', $override = false)
{
$data = array(
'token' => $this->apiToken,
'content' => 'arm',
'action' => 'import',
'returnFormat' => 'json'
);
#---------------------------------------
# Process arguments
#---------------------------------------
$legalFormats = array('csv', 'json', 'php', 'xml');
$data['format'] = $this->processFormatArgument($format, $legalFormats);
$data['data'] = $this->processImportDataArgument($arms, 'arms', $format);
$data['override'] = $this->processOverrideArgument($override);
$result = $this->connection->callWithArray($data);
$this->processNonExportResult($result);
return (integer) $result;
}
/**
* Deletes the specified arms from the project.
*
* @param array $arms array of arm numbers to delete.
*
* @throws PhpCapException if an error occurs, including if the arms array is null or empty
*
* @return integer the number of arms deleted.
*/
public function deleteArms($arms)
{
$data = array (
'token' => $this->apiToken,
'content' => 'arm',
'action' => 'delete',
'returnFormat' => 'json',
);
$data['arms'] = $this->processArmsArgument($arms, $required = true);
$result = $this->connection->callWithArray($data);
$this->processNonExportResult($result);
return (integer) $result;
}
/**
* Exports information about the specified events.
*
* Example usage:
* <code>
* #export information about all events in CSV (Comma-Separated Values) format.
* $eventInfo = $project->exportEvents('csv');
*
* # export events in XML format for arms 1 and 2.
* $eventInfo = $project->exportEvents('xml', [1, 2]);
* </code>
*
* @param string $format the format for the export.
* <ul>
* <li> 'php' - [default] array of maps of values</li>
* <li> 'csv' - string of CSV (comma-separated values)</li>
* <li> 'json' - string of JSON encoded values</li>
* <li> 'xml' - string of XML encoded data</li>
* </ul>
* @param array $arms array of integers or numeric strings that are the arm numbers for
* which events should be exported.
* If no arms are specified, then all events will be returned.
*
* @return array information about the specified events. Each element of the
* array is an associative array with the following keys: 'event_name', 'arm_num',
* 'day_offset', 'offset_min', 'offset_max', 'unique_event_name', 'custom_event_label'
*/
public function exportEvents($format = 'php', $arms = [])
{
$data = array(
'token' => $this->apiToken,
'content' => 'event',
'returnFormat' => 'json'
);
#---------------------------------------
# Process arguments
#---------------------------------------
$legalFormats = array('csv', 'json', 'php', 'xml');
$data['format'] = $this->processFormatArgument($format, $legalFormats);
$data['arms'] = $this->processArmsArgument($arms);
#------------------------------------------------------
# Get and process events
#------------------------------------------------------
$events = $this->connection->callWithArray($data);
$events = $this->processExportResult($events, $format);
return $events;
}
/**
* Imports the specified events into the project.
*
* @param mixed $events the events to import. This will
* be a PHP array of associative arrays if no format, or 'php' format is specified,
* and a string otherwise. The field names (keys) used in both cases
* are: event_name, arm_num
* @param string $format the format for the export.
* <ul>
* <li> 'php' - [default] array of maps of values</li>
* <li> 'csv' - string of CSV (comma-separated values)</li>
* <li> 'json' - string of JSON encoded values</li>
* <li> 'xml' - string of XML encoded data</li>
* </ul>
* @param boolean $override
* <ul>
* <li> false - [default] don't delete existing arms; only add new
* arms or renames existing arms.
* </li>
* <li> true - delete all existing arms before importing.</li>
* </ul>
*
* @throws PhpCapException if an error occurs.
*
* @return integer the number of events imported.
*/
public function importEvents($events, $format = 'php', $override = false)
{
$data = array(
'token' => $this->apiToken,
'content' => 'event',
'action' => 'import',
'returnFormat' => 'json'
);
#---------------------------------------
# Process arguments
#---------------------------------------
$data['data'] = $this->processImportDataArgument($events, 'arms', $format);
$legalFormats = array('csv', 'json', 'php', 'xml');
$data['format'] = $this->processFormatArgument($format, $legalFormats);
$data['override'] = $this->processOverrideArgument($override);
$result = $this->connection->callWithArray($data);
$this->processNonExportResult($result);
return (integer) $result;
}
/**
* Deletes the specified events from the project.
*
* @param array $events array of event names of events to delete.
*
* @throws PhpCapException if an error occurs, including if the events array is null or empty.
*
* @return integer the number of events deleted.
*/
public function deleteEvents($events)
{
$data = array (
'token' => $this->apiToken,
'content' => 'event',
'action' => 'delete',
'returnFormat' => 'json',
);
$data['events'] = $this->processEventsArgument($events, $required = true);
$result = $this->connection->callWithArray($data);
$this->processNonExportResult($result);
return (integer) $result;
}
/**
* Exports the fields names for a project.
*
* @param string $format the format for the export.
* <ul>
* <li> 'php' - [default] array of maps of values</li>
* <li> 'csv' - string of CSV (comma-separated values)</li>
* <li> 'json' - string of JSON encoded values</li>
* <li> 'xml' - string of XML encoded data</li>
* </ul>
* @param string $field the name of the field for which to export field
* name information. If no field is specified, information for all
* fields is exported.
*
* @return mixed information on the field specified, or all fields if no
* field was specified. If 'php' or no format was specified, results
* will be returned as a PHP array of maps (associative arrays), where the
* keys for the maps:
* <ul>
* <li>original_field_name</li>
* <li>choice_value</li>
* <li>export_field_name</li>
* </ul>
*/
public function exportFieldNames($format = 'php', $field = null)
{
$data = array(
'token' => $this->apiToken,
'content' => 'exportFieldNames',
'returnFormat' => 'json'
);
#---------------------------------------
# Process arguments
#---------------------------------------
$legalFormats = array('csv', 'json', 'php', 'xml');
$data['format'] = $this->processFormatArgument($format, $legalFormats);
$data['field'] = $this->processFieldArgument($field, $required = false);
$fieldNames = $this->connection->callWithArray($data);
$fieldNames = $this->processExportResult($fieldNames, $format);
return $fieldNames;
}
/**
* Exports the specified file.
*
* @param string $recordId the record ID for the file to be exported.
* @param string $field the name of the field containing the file to export.
* @param string $event name of event for file export (for longitudinal studies).
* @param integer $repeatInstance
*
* @throws PhpCapException if an error occurs.
*
* @return string the contents of the file that was exported.
*/
public function exportFile($recordId, $field, $event = null, $repeatInstance = null)
{
$data = array(
'token' => $this->apiToken,
'content' => 'file',
'action' => 'export',
'returnFormat' => 'json'
);
#--------------------------------------------
# Process arguments
#--------------------------------------------
$data['record'] = $this->processRecordIdArgument($recordId);
$data['field'] = $this->processFieldArgument($field);
$data['event'] = $this->processEventArgument($event);
$data['repeat_instance'] = $this->processRepeatInstanceArgument($repeatInstance);
#-------------------------------
# Get and process file
#-------------------------------
$file = $this->connection->callWithArray($data);
$file = $this->processExportResult($file, $format = 'file');
return $file;
}
/**
* Imports the file into the field of the record
* with the specified event and/or repeat istance, if any.
*
* Example usage:
* <code>
* ...
* $file = '../data/consent1001.txt';
* $recordId = '1001';
* $field = 'patient_document';
* $event = 'enrollment_arm_1';
* $project->importFile($file, $recordId, $field, $event);
* ...
* </code>
*
* @param string $filename the name of the file to import.
* @param string $recordId the record ID of the record to import the file into.
* @param string $field the field of the record to import the file into.
* @param string $event the event of the record to import the file into
* (only for longitudinal studies).
* @param integer $repeatInstance the repeat instance of the record to import
* the file into (only for studies that have repeating events
* and/or instruments).
*
* @throws PhpCapException
*/
public function importFile($filename, $recordId, $field, $event = null, $repeatInstance = null)
{
$data = array (
'token' => $this->apiToken,
'content' => 'file',
'action' => 'import',
'returnFormat' => 'json'
);
#----------------------------------------
# Process non-file arguments
#----------------------------------------
$data['file'] = $this->processFilenameArgument($filename);
$data['record'] = $this->processRecordIdArgument($recordId);
$data['field'] = $this->processFieldArgument($field);
$data['event'] = $this->processEventArgument($event);
$data['repeat_instance'] = $this->processRepeatInstanceArgument($repeatInstance);
#---------------------------------------------------------------------
# For unknown reasons, "call" (instead of "callWithArray") needs to
# be used here (probably something to do with the 'file' data).
# REDCap's "API Playground" (also) makes no data conversion for this
# method.
#---------------------------------------------------------------------
$result = $this->connection->call($data);
$this->processNonExportResult($result);
}
/**
* Deletes the specified file.
*
* @param string $recordId the record ID of the file to delete.
* @param string $field the field name of the file to delete.
* @param string $event the event of the file to delete
* (only for longitudinal studies).
* @param integer $repeatInstance repeat instance of the file to delete
* (only for studies that have repeating events
* and/or instruments).
*/
public function deleteFile($recordId, $field, $event = null, $repeatInstance = null)
{
$data = array (
'token' => $this->apiToken,
'content' => 'file',
'action' => 'delete',
'returnFormat' => 'json'
);
#----------------------------------------
# Process arguments
#----------------------------------------
$data['record'] = $this->processRecordIdArgument($recordId);
$data['field'] = $this->processFieldArgument($field);
$data['event'] = $this->processEventArgument($event);
$data['repeat_instance'] = $this->processRepeatInstanceArgument($repeatInstance);
$result = $this->connection->callWithArray($data);
$this->processNonExportResult($result);
return $result;
}
/**
* Exports information about the instruments (data entry forms) for the project.
*
* Example usage:
* <code>
* $instruments = $project->getInstruments();
* foreach ($instruments as $instrumentName => $instrumentLabel) {
* print "{$instrumentName} : {$instrumentLabel}\n";
* }
* </code>
*
* @param $format string format instruments are exported in:
* <ul>
* <li>'php' - [default] returns data as a PHP array</li>
* <li>'csv' - string of CSV (comma-separated values)</li>
* <li>'json' - string of JSON encoded data</li>
* <li>'xml' - string of XML encoded data</li>
* </ul>
*
* @return mixed For the 'php' format, and array map of instrument names to instrument labels is returned.
* For all other formats a string is returned.
*/
public function exportInstruments($format = 'php')
{
$data = array(
'token' => $this->apiToken,
'content' => 'instrument',
'returnFormat' => 'json'
);
$legalFormats = array('csv', 'json', 'php', 'xml');
$data['format'] = $this->processFormatArgument($format, $legalFormats);
$instrumentsData = $this->connection->callWithArray($data);
$instrumentsData = $this->processExportResult($instrumentsData, $format);
#------------------------------------------------------
# If format is 'php', reformat the data as
# a map from "instrument name" to "instrument label"
#------------------------------------------------------
if ($format == 'php') {
$instruments = array ();
foreach ($instrumentsData as $instr) {
$instruments [$instr ['instrument_name']] = $instr ['instrument_label'];
}
} else {
$instruments = $instrumentsData;
}
return $instruments;
}
/**
* Exports a PDF version of the requested instruments (forms).
*
* @param string $file the name of the file (possibly with a path specified also)
* to store the PDF instruments in.
* @param string $recordId if record ID is specified, the forms retrieved will
* be filled with values for that record. Otherwise, they will be blank.
* @param string $event (only for longitudinal projects) a unique event name
* that is used when a record ID has been specified to return only
* forms that are in that event (for the specified records).
* @param string $form if this is specified, only this form will be
* returned.
* @param boolean $allRecords if this is set to true, all forms for all
* records will be retrieved (the $recordId, $event, and $form arguments
* will be ignored).
*
* @throws PhpCapException if an error occurs.
*
* @return string PDF content of requested instruments (forms).
*/
public function exportPdfFileOfInstruments(
$file = null,
$recordId = null,
$event = null,
$form = null,
$allRecords = null
) {
$data = array(
'token' => $this->apiToken,
'content' => 'pdf',
'returnFormat' => 'json'
);
$file = $this->processFileArgument($file);
$data['record'] = $this->processRecordIdArgument($recordId, $required = false);
$data['event'] = $this->processEventArgument($event);
$data['instrument'] = $this->processFormArgument($form);
$data['allRecords'] = $this->processAllRecordsArgument($allRecords);
$result = $this->connection->callWithArray($data);
if (isset($file)) {
FileUtil::writeStringToFile($result, $file);
}
return $result;
}
/**
* Gets the instrument to event mapping for the project.
*
* For example, the following code:
* <code>
* $map = $project->exportInstrumentEventMappings();
* print_r($map[0]); # print first element of map
* </code>
* might generate the following output:
* <pre>
* Array
* (
* [arm_num] => 1
* [unique_event_name] => enrollment_arm_1
* [form] => demographics
* )
* </pre>
*
* @param string $format the format in which to export the records:
* <ul>
* <li> 'php' - [default] array of maps of values</li>
* <li> 'csv' - string of CSV (comma-separated values)</li>
* <li> 'json' - string of JSON encoded values</li>
* <li> 'xml' - string of XML encoded data</li>
* </ul>
* @param array $arms array of integers or numeric strings that are the numbers of the arms
* for which instrument/event mapping infomation should be exported.
* If no arms are specified, then information for all arms will be exported.
*
* @return arrray an array of arrays that have the following keys:
* <ul>
* <li>'arm_num'</li>
* <li>'unique_event_name'</li>
* <li>'form'</li>
* </ul>
*/
public function exportInstrumentEventMappings($format = 'php', $arms = [])
{
$data = array(
'token' => $this->apiToken,
'content' => 'formEventMapping',
'format' => 'json',
'returnFormat' => 'json'
);
#------------------------------------------
# Process arguments
#------------------------------------------
$legalFormats = array('csv', 'json', 'php', 'xml');
$data['format'] = $this->processFormatArgument($format, $legalFormats);
$data['arms'] = $this->processArmsArgument($arms);
#---------------------------------------------
# Get and process instrument-event mappings
#---------------------------------------------
$instrumentEventMappings = $this->connection->callWithArray($data);
$instrumentEventMappings = $this->processExportResult($instrumentEventMappings, $format);
return $instrumentEventMappings;
}
/**
* Imports the specified instrument-event mappings into the project.
*
* @param mixed $mappings the mappings to import. This will
* be a PHP array of associative arrays if no format, or
* 'php' format, was specified,
* and a string otherwise. In all cases, the field names that
* are used in the mappings are:
* arm_num, unique_event_name, form
* @param string $format the format for the export.
* <ul>
* <li> 'php' - [default] array of maps of values</li>
* <li> 'csv' - string of CSV (comma-separated values)</li>
* <li> 'json' - string of JSON encoded values</li>
* <li> 'xml' - string of XML encoded data</li>
* </ul>
*
* @throws PhpCapException if an error occurs.
*
* @return integer the number of mappings imported.
*/
public function importInstrumentEventMappings($mappings, $format = 'php')
{
$data = array(
'token' => $this->apiToken,
'content' => 'formEventMapping',
'returnFormat' => 'json'
);
#---------------------------------------
# Process arguments
#---------------------------------------
$data['data'] = $this->processImportDataArgument($mappings, 'mappings', $format);
$legalFormats = array('csv', 'json', 'php', 'xml');
$data['format'] = $this->processFormatArgument($format, $legalFormats);
$result = $this->connection->callWithArray($data);
$this->processNonExportResult($result);
return (integer) $result;
}
/**
* Exports metadata about the project, i.e., information about the fields in the project.
*
* @param string $format the format for the export.
* <ul>
* <li> 'php' - [default] array of maps of values</li>
* <li> 'csv' - string of CSV (comma-separated values)</li>
* <li> 'json' - string of JSON encoded values</li>
* <li> 'xml' - string of XML encoded data</li>
* </ul>
* @param array $fields array of field names for which metadata should be exported
* @param array $forms array of form names. Metadata will be exported for all fields in the
* specified forms.
*
* @return array associative array (map) of metatdata for the project, which consists of
* information about each field. Some examples of the information
* provided are: 'field_name', 'form_name', 'field_type', 'field_label'.
* See REDCap API documentation
* for more information, or use the print_r function on the results of this method.
*/
public function exportMetadata($format = 'php', $fields = [], $forms = [])
{
$data = array(
'token' => $this->apiToken,
'content' => 'metadata',
'returnFormat' => 'json'
);
#---------------------------------------
# Process format
#---------------------------------------
$legalFormats = array('csv', 'json', 'php', 'xml');
$data['format'] = $this->processFormatArgument($format, $legalFormats);
$data['forms'] = $this->processFormsArgument($forms);
$data['fields'] = $this->processFieldsArgument($fields);
#-------------------------------------------
# Get and process metadata
#-------------------------------------------
$metadata = $this->connection->callWithArray($data);
$metadata = $this->processExportResult($metadata, $format);
return $metadata;
}
/**
* Imports the specified metadata (field information) into the project.
*
* @param mixed $metadata the metadata to import. This will
* be a PHP associative array if no format, or 'php' format was specified,
* and a string otherwise.
* @param string $format the format for the export.
* <ul>
* <li> 'php' - [default] array of maps of values</li>
* <li> 'csv' - string of CSV (comma-separated values)</li>
* <li> 'json' - string of JSON encoded values</li>
* <li> 'xml' - string of XML encoded data</li>
* </ul>
*
* @throws PhpCapException if an error occurs.
*
* @return integer the number of fields imported.
*/
public function importMetadata($metadata, $format = 'php')
{
$data = array(
'token' => $this->apiToken,
'content' => 'metadata',
'returnFormat' => 'json'
);
#---------------------------------------
# Process arguments
#---------------------------------------
$data['data'] = $this->processImportDataArgument($metadata, 'metadata', $format);
$legalFormats = array('csv', 'json', 'php', 'xml');
$data['format'] = $this->processFormatArgument($format, $legalFormats);
$result = $this->connection->callWithArray($data);
$this->processNonExportResult($result);
return (integer) $result;
}
/**
* Exports information about the project, e.g., project ID, project title, creation time.
*
* @param string $format the format for the export.
* <ul>
* <li> 'php' - [default] array of maps of values</li>
* <li> 'csv' - string of CSV (comma-separated values)</li>
* <li> 'json' - string of JSON encoded values</li>
* <li> 'xml' - string of XML encoded data</li>
* </ul>
*
* @return array associative array (map) of project information. See REDCap API documentation
* for a list of the fields, or use the print_r function on the results of this method.
*/
public function exportProjectInfo($format = 'php')
{
$data = array(
'token' => $this->apiToken,
'content' => 'project',
'returnFormat' => 'json'
);
#---------------------------------------
# Process format
#---------------------------------------
$legalFormats = array('csv', 'json', 'php', 'xml');
$data['format'] = $this->processFormatArgument($format, $legalFormats);
#---------------------------------------
# Get and process project information
#---------------------------------------
$projectInfo = $this->connection->callWithArray($data);
$projectInfo = $this->processExportResult($projectInfo, $format);
return $projectInfo;
}
/**
* Imports the specified project information into the project.
* The valid fields that can be imported are:
*
* project_title, project_language, purpose, purpose_other, project_notes,
* custom_record_label, secondary_unique_field, is_longitudinal,
* surveys_enabled, scheduling_enabled, record_autonumbering_enabled,
* randomization_enabled, project_irb_number, project_grant_number,
* project_pi_firstname, project_pi_lastname, display_today_now_button
*
* You do not need to specify all of these fields when doing an import,
* only the ones that you actually want to change. For example:
* <code>
* ...
* # Set the project to be longitudinal and enable surveys
* $projectInfo = ['is_longitudinal' => 1, 'surveys_enabled' => 1];
* $project->importProjectInfo($projectInfo);
* ...
* </code>
*
* @param mixed $projectInfo the project information to import. This will
* be a PHP associative array if no format, or 'php' format was specified,
* and a string otherwise.
* @param string $format the format for the export.
* <ul>
* <li> 'php' - [default] array of maps of values</li>
* <li> 'csv' - string of CSV (comma-separated values)</li>
* <li> 'json' - string of JSON encoded values</li>
* <li> 'xml' - string of XML encoded data</li>
* </ul>
*
* @throws PhpCapException if an error occurs.
*
* @return integer the number of project info values specified that were valid,
* whether or not each valid value actually caused an update (i.e., was different
* from the existing value before the method call).
*/
public function importProjectInfo($projectInfo, $format = 'php')
{
$data = array(
'token' => $this->apiToken,
'content' => 'project_settings',
'returnFormat' => 'json'
);
#---------------------------------------
# Process arguments
#---------------------------------------
$data['data'] = $this->processImportDataArgument($projectInfo, 'projectInfo', $format);
$legalFormats = array('csv', 'json', 'php', 'xml');
$data['format'] = $this->processFormatArgument($format, $legalFormats);
$result = $this->connection->callWithArray($data);
$this->processNonExportResult($result);
return (integer) $result;
}
/**
* Exports the specified information of project in XML format.
*
* @param boolean $returnMetadataOnly if this is set to true, only the metadata for
* the project is returned. If it is not set to true, the metadata and data for the project
* is returned.
* @param array $recordIds array of strings with record id's that are to be retrieved.
* @param array $fields array of field names to export
* @param array $events array of event names for which fields should be exported
* @param array $filterLogic logic used to restrict the records retrieved, e.g.,
* "[last_name] = 'Smith'".
* @param boolean $exportSurveyFields specifies whether survey fields should be exported.
* <ul>
* <li> true - export the following survey fields:
* <ul>
* <li> survey identifier field ('redcap_survey_identifier') </li>
* <li> survey timestamp fields (instrument+'_timestamp') </li>
* </ul>
* </li>
* <li> false - [default] survey fields are not exported.</li>
* </ul>
* @param boolean $exportDataAccessGroups specifies whether the data access group field
* ('redcap_data_access_group') should be exported.
* <ul>
* <li> true - export the data access group field if there is at least one data access group, and
* the user calling the method (as identified by the API token) is not
* in a data access group.</li>
* <li> false - [default] don't export the data access group field.</li>
* </ul>
* @param boolean $exportFiles If this is set to true, files will be exported in the XML.
* If it is not set to true, files will not be exported.
* @return string the specified information for the project in XML format.
*/
public function exportProjectXml(
$returnMetadataOnly = false,
$recordIds = null,
$fields = null,
$events = null,
$filterLogic = null,
$exportSurveyFields = false,
$exportDataAccessGroups = false,
$exportFiles = false
) {
$data = array(
'token' => $this->apiToken,
'content' => 'project_xml',
'returnFormat' => 'json'
);
#---------------------------------------------
# Process the arguments
#---------------------------------------------
$data['returnMetadataOnly'] = $this->processReturnMetadataOnlyArgument($returnMetadataOnly);
$data['records'] = $this->processRecordIdsArgument($recordIds);
$data['fields'] = $this->processFieldsArgument($fields);
$data['events'] = $this->processEventsArgument($events);
$data['filterLogic'] = $this->processFilterLogicArgument($filterLogic);
$data['exportSurveyFields'] = $this->processExportSurveyFieldsArgument($exportSurveyFields);
$data['exportDataAccessGroups'] = $this->processExportDataAccessGroupsArgument($exportDataAccessGroups);
$data['exportFiles'] = $this->processExportFilesArgument($exportFiles);
#---------------------------------------
# Get the Project XML and process it
#---------------------------------------
$projectXml = $this->connection->callWithArray($data);
$projectXml = $this->processExportResult($projectXml, $format = 'xml');
return $projectXml;
}
/**
* This method returns the next potential record ID for a project, but it does NOT
* actually create a new record. The record ID returned will generally be the current maximum
* record ID number incremented by one (but see the REDCap documentation for the case
* where Data Access Groups are being used).
* This method is intended for use with projects that have record-autonumbering enabled.
*
* @return string the next record name.
*/
public function generateNextRecordName()
{
$data = array(
'token' => $this->apiToken,
'content' => 'generateNextRecordName',
'returnFormat' => 'json'
);
$nextRecordName = $this->connection->callWithArray($data);
$nextRecordName = $this->processExportResult($nextRecordName, $format = 'number');
return $nextRecordName;
}
/**
* Exports the specified records.
*