-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathRedCapEtlModule.php
executable file
·2053 lines (1765 loc) · 79.7 KB
/
RedCapEtlModule.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
#-------------------------------------------------------
namespace IU\RedCapEtlModule;
# This is required for cron jobs
// phpcs:disable
require_once(__DIR__.'/vendor/autoload.php');
// phpcs:enable
define('REDCAP_ETL_MODULE', 1);
/**
* Main REDCap-ETL module class.
*/
class RedCapEtlModule extends \ExternalModules\AbstractExternalModule
{
public const ADMIN_HOME_PAGE = 'web/admin/config.php';
public const CRON_DETAIL_PAGE = 'web/admin/cron_detail.php';
public const USERS_PAGE = 'web/admin/users.php';
public const USER_CONFIG_PAGE = 'web/admin/user_config.php';
public const SERVERS_PAGE = 'web/admin/servers.php';
public const SERVER_CONFIG_PAGE = 'web/admin/server_config.php';
public const ADMIN_INFO_PAGE = 'web/admin/info.php';
public const ADMIN_WORKFLOWS_PAGE = 'web/admin/admin_workflows.php';
public const HELP_LIST_PAGE = 'web/admin/help_list.php';
public const HELP_EDIT_PAGE = 'web/admin/help_edit.php';
public const LOG_PAGE = 'web/admin/log.php';
public const RUN_PAGE = 'web/run.php';
public const USER_ETL_CONFIG_PAGE = 'web/configure.php';
public const WORKFLOWS_PAGE = 'web/workflows.php';
# REDCap event log constants
public const RUN_LOG_ACTION = 'REDCap-ETL Export';
public const CHANGE_LOG_ACTION = 'REDCap-ETL Change';
public const LOG_EVENT = -1;
# REDCap external module log constants
public const ETL_CRON = 'ETL cron';
public const ETL_CRON_JOB = 'ETL cron job';
public const ETL_RUN = 'ETL run';
public const ETL_RUN_DETAILS = 'ETL run details';
public const WORKFLOW_RUN = 'workflow run';
# Access/permission errors
public const CSRF_ERROR = 1; # (Possible) Cross-Site Request Forgery Error
public const USER_RIGHTS_ERROR = 2;
public const NO_ETL_PROJECT_PERMISSION = 3;
public const NO_CONFIGURATION_PERMISSION = 4;
# Workflow constants
public const WORKFLOW_REMOVED = 'Removed';
public const WORKFLOW_NAME = 'workflow_name';
public const JSON_WORKFLOW_KEY = 'workflow';
public const JSON_GLOBAL_PROPERTIES_KEY = 'global_properties';
public const JSON_TASKS_KEY = 'tasks';
public const TASK_CONFIG = 'task_config';
private $db;
private $settings;
private $moduleLog;
private $changeLogAction;
/**
* Returns REDCap user rights for the current project.
*
* @return array a map from username to an map of rights for the current project.
*/
public function getUserRights()
{
$userId = USERID;
$rights = \REDCap::getUserRights($userId)[$userId];
return $rights;
}
public function getUserInfo($username = USERID)
{
$userInfo = $this->db->getUserInfo($username);
return $userInfo;
}
/**
* Gets the data export right for the current user. If the current
* user is an admin 'Full data set' export permission will be
* returned, otherwise, the REDCap user data exports user right
* value for the current user will be returned.
*/
public function getDataExportRight()
{
$dataExportRight = 0; // No data access
if ($this->isSuperUser()) {
$dataExportRight = 1; // Full data access
} else {
$rights = $this->getUserRights();
// OLD: $dataExportRight = $rights['data_export_tool'];
$canExport = Authorization::canExportAllInstruments($rights);
if ($canExport) {
$dataExportRight = "1";
} else {
$dataExportRight = "";
}
}
return $dataExportRight;
}
/**
* Cron method that is called by REDCap as configured in the
* config.json file for this module.
*/
public function cron()
{
#\REDCap::logEvent('REDCap-ETL cron() start');
$now = new \DateTime();
$day = $now->format('w'); // 0-6 (day of week; Sunday = 0)
$hour = $now->format('G'); // 0-23 (24-hour format without leading zeroes)
$minutes = $now->format('i');
$date = $now->format('Y-m-d');
if ($this->isLastRunTime($date, $hour)) {
; # Cron jobs for this time were already processed
#\REDCap::logEvent('REDCap-ETL cron - cron jobs already processed.');
} else {
# Set the last run time processed to this time, so that it won't be processed again
$this->setLastRunTime($date, $hour, $minutes);
$this->runCronJobs($day, $hour);
}
}
/**
* Runs the cron jobs (if any, for the specified day and hour).
*/
public function runCronJobs($day, $hour)
{
$taskCronJobs = $this->getTaskCronJobs($day, $hour);
$workflowCronJobs = $this->getWorkflowCronJobs($day, $hour);
$cronJobsCount = count($taskCronJobs) + count($workflowCronJobs);
$cronJobsCount = count($taskCronJobs) + count($workflowCronJobs);
$cronJobsRunLogId = $this->getModuleLog()->logCronJobsRun($cronJobsCount, $day, $hour);
$this->runTaskCronJobs($taskCronJobs, $day, $hour, $cronJobsRunLogId);
$this->runWorkflowCronJobs($workflowCronJobs, $day, $hour, $cronJobsRunLogId);
}
/**
* Runs an ETL job. Top-level method for running an ETL job that all code should call,
* so that there is one place to do REDCap authorization checks and logging.
*
* @param string $configName the name of the REDCap-ETL configuration to run
* @param string $serverName name of server to run on
* @param boolean $isCronJon indicates whether this is being called from a cron job or not.
* @param int $cronJobLogId the log ID of the cron job log entry
*
* @return string the status of the run.
*/
public function run(
$configName,
$serverName,
$isCronJob = false,
$projectId = PROJECT_ID,
$cronJobLogId = null,
$cronDay = null,
$cronHour = null,
$dataTarget = DataTarget::DB
) {
try {
$username = '';
if (defined('USERID')) {
$username = USERID;
}
#-------------------------------------------------
# Log run information to external module log
#-------------------------------------------------
$this->getModuleLog()->logEtlRun(
$projectId,
$username,
$isCronJob,
$configName,
$serverName,
$cronJobLogId,
$cronDay,
$cronHour
);
$adminConfig = $this->getAdminConfig();
#---------------------------------------------
# Process configuration name
#---------------------------------------------
if (empty($configName)) {
throw new \Exception('No configuration specified.');
} else {
$etlConfig = $this->getConfiguration($configName, $projectId);
if (!isset($etlConfig)) {
$message = 'Configuration "' . $configName
. '" not found for project ID ' . $projectId;
throw new \Exception($message);
}
}
#---------------------------------------------
# Process server name
#---------------------------------------------
if (empty($serverName)) {
throw new \Exception('No server specified.');
} else {
$serverConfig = $this->getServerConfig($serverName); # Method throws exception if server not found
if (!$serverConfig->getIsActive()) {
throw new \Exception('Server "' . $serverName . '" has been deactivated and cannot be used.');
}
}
#-----------------------------------------------------
# Set logging information
#-----------------------------------------------------
$cron = 'no';
if ($isCronJob) {
$cron = 'yes';
}
$details = '';
# If being run on remote REDCap
if (strcasecmp($configUrl, $this->getRedCapApiUrl()) !== 0) {
$details .= "REDCap API URL: {$configUrl}\n";
} else {
# If local REDCap is being used, set SSL verify to the global value
$sslVerify = $adminConfig->getSslVerify();
$etlConfig->setProperty(Configuration::SSL_VERIFY, $sslVerify);
}
$details .= "project ID: {$projectId}\n";
if (!$isCronJob) {
$details .= "user: " . USERID . "\n";
}
$details .= "configuration: {$configName}\n";
$details .= "server: {$serverName}\n";
if ($isCronJob) {
$details .= "cron: yes\n";
} else {
$details .= "cron: no\n";
}
$status = '';
$sql = null;
$record = null;
$event = self::LOG_EVENT;
if ($isCronJob) {
$projectId = $etlConfig->getProjectId();
} else {
$projectId = null; // Handled automatically in logging if not cron job
}
#---------------------------------------------
# Authorization checks
#---------------------------------------------
if ($isCronJob) {
if (!$serverConfig->getAllowCronRun()) {
# Cron jobs not allowed
$message = "Cron job failed - cron jobs not allowed\n" . $details;
throw new \Exception($message);
}
# Note: the following check is no longer valid (an admin could set up a cron job):
#elseif (!$this->hasEtlUser($projectId)) {
# # No user for this project has ETL permission
# $message = "Cron job failed - no project user has ETL permission\n".$details;
# throw new \Exception($message);
#}
} else {
# If NOT a cron job
if (!Authorization::hasEtlProjectPagePermission($this)) {
$message = 'User "' . USERID . '" does not have permission to run ETL for this project.';
throw new \Exception($message);
}
}
$details = "ETL job submitted \n" . $details;
\REDCap::logEvent(self::RUN_LOG_ACTION, $details, $sql, $record, $event, $projectId);
#------------------------------------------------------------------------
# If no server configuration was specified, run on the embedded server
#------------------------------------------------------------------------
#if (empty($serverConfig)) {
# $status = $this->runEmbedded($etlConfig, $isCronJob);
#} else {
# # Run on the specified server
# $status = $serverConfig->run($etlConfig, $isCronJob);
#}
#$status = $serverConfig->run($etlConfig, $isCronJob, $this->getModuleLog());
$status = $serverConfig->run($etlConfig, $isCronJob, $this->getModuleLog(), false, $dataTarget);
} catch (\Exception $exception) {
$status = "ETL job failed: " . $exception->getMessage();
$details = "ETL job failed\n" . $details
. 'error: ' . $exception->getMessage();
\REDCap::logEvent(self::RUN_LOG_ACTION, $details, $sql, $record, $event, $projectId);
}
return $status;
}
/**
* Runs an ETL configuration on the embedded server.
*/
#private function runEmbedded($etlConfiguration, $isCronJob = false)
#{
# $properties = $etlConfiguration->getPropertiesArray();
#
# $adminConfig = $this->getAdminConfig();
#
# $logger = new \IU\REDCapETL\Logger('REDCap-ETL');
#
# try {
# # Set the from e-mail address from the admin. configuration
# $properties[Configuration::EMAIL_FROM_ADDRESS] = $adminConfig->getEmbeddedServerEmailFromAddress();
#
# # Set the log file, and set print logging off
# $properties[Configuration::LOG_FILE] = $adminConfig->getEmbeddedServerLogFile();
# $properties[Configuration::PRINT_LOGGING] = false;
#
# # Set process identifting properties
# $properties[Configuration::PROJECT_ID] = $etlConfiguration->getProjectId();
# $properties[Configuration::CONFIG_NAME] = $etlConfiguration->getName();
# $properties[Configuration::CONFIG_OWNER] = $etlConfiguration->getUsername();
# $properties[Configuration::CRON_JOB] = $isCronJob;
#
# $redCapEtl = new \IU\REDCapETL\RedCapEtl($logger, $properties);
# $redCapEtl->run();
# } catch (\Exception $exception) {
# $logger->logException($exception);
# $logger->log('Processing failed.');
# }
#
# $status = implode("\n", $logger->getLogArray());
# return $status;
#}
/**
* Gets the REDCap API URL.
*
* @return string the REDCap API URL.
*/
public static function getRedCapApiUrl()
{
return APP_PATH_WEBROOT_FULL . 'api/';
}
/**
* Gets the external module's version number.
*/
public function getVersion()
{
return $this->getSettings()->getVersion();
}
/**
* Gets the settings for this module and initializes settings if they are unset.
* This method should always be called (outside of this method) instead of
* accessing settings directly.
*
* @return Settings the settings for this module.
*/
public function getSettings()
{
if (!isset($this->settings)) {
$this->db = new RedCapDb();
$this->settings = new Settings($this, $this->db);
}
return $this->settings;
}
public function getModuleLog()
{
if (!isset($this->moduleLog)) {
$this->moduleLog = new ModuleLog($this);
}
return $this->moduleLog;
}
#-------------------------------------------------------------------
# UserList methods
#-------------------------------------------------------------------
public function getUsers()
{
return $this->getSettings()->getUsers();
}
public function addUser($username)
{
$this->getSettings()->addUser($username);
$details = 'User ' . $username . ' added to ETL users.';
\REDCap::logEvent(self::CHANGE_LOG_ACTION, $details, null, null, self::LOG_EVENT);
}
public function deleteUser($username)
{
$this->getSettings()->deleteUser($username);
$details = 'User ' . $username . ' deleted from ETL users.';
\REDCap::logEvent(self::CHANGE_LOG_ACTION, $details, null, null, self::LOG_EVENT);
}
/**
* Returns servers with the specified specific access level. If 'none' is
* specified for the specific access level, then all servera are returned.
*/
public function getServersViaAccessLevels($specificLevel = 'none')
{
#get server names
$servers = array();
$allServers = $this->getSettings()->getServers();
#loop through the server names to get their access levels
foreach ($allServers as $serverName) {
#get the server configurations for the server name
$serverConfig = $this->getSettings()->getServerConfig($serverName);
#if the server has the access level specified,
#add it to the array of servers
$accessLevel = $serverConfig->getAccessLevel();
if ($specificLevel === 'none') {
$servers[] = $serverName;
} else {
if ($accessLevel === $specificLevel) {
$servers[] = $serverName;
}
}
}
#print "<br /><br />specificLevel is: $specificLevel<br />";
#print "<br /><br />servers object is: <br />";
#print '<pre>'; print_r($servers); print '</pre>'."<br />";
return $servers;
}
public function hasServerForUser($username, $isScheduled = null, $isFileDownload = null)
{
$hasServer = $this->getSettings()->hasServerForUser($username, $isScheduled, $isFileDownload);
return $hasServer;
}
public function getServersForUser($username = USERID, $isScheduled = null, $isFileDownload = null)
{
$servers = $this->getSettings()->getServersForUser($username, $isScheduled, $isFileDownload);
return $servers;
}
/**
* Gets the list of servers the user is allowed to use.
*
* @param string $username the REDCap username of the user.
* @param boolean $isInteractive indicates if the check is for an interactive run (as opposed to a scheduled run).
*/
/*
public function getUserAllowedServersBasedOnAccessLevel($username = USERID, $isFileDownload = null)
{
$servers = array();
#-------------------------------------------------------------
# If this is a REDCap admin, then return all servers
#-------------------------------------------------------------
if ($this->isSuperUser($username)) {
$servers = $this->getServersViaAccessLevels('none');
} else {
# Add servers with public access
$servers = $this->getServersViaAccessLevels('public');
# Add the private servers that the user is allowed to access
$userPrivateServers = array();
$userPrivateServers = $this->getSettings()->getUserPrivateServerNames($username);
if (!empty($userPrivateServers)) {
$servers = array_merge($servers, $userPrivateServers);
}
# Remove duplicate server names. There are cases where a server can be listed twice, for example, if
# a private server is changed to being public, and the list of users for the private version is saved.
$servers = array_unique($servers);
}
#---------------------------------------------------------------------------------
# Check to see if the embedded server should be removed, because it is not set
# to allow the type of download (database or file) being done.
#---------------------------------------------------------------------------------
$embeddedServerIndex = array_search(ServerConfig::EMBEDDED_SERVER_NAME, $servers);
if ($embbdedServerIndex !== false && isset($isFileDownload)) {
$embeddedServerConfig = $this->getServerConfig(ServerConfig::EMBEDDED_SERVER_NAME);
$dataLoadOptions = $embeddedServerConfig->getDataLoadOptions();
if ($isFileDownLoad) {
# If a file download is being done, and the embedded server only has database download enabled,
# then remove the embedded server from the list of servers.
if ($dataLoadOptions === ServerConfig::DATA_LOAD_DB_ONLY) {
unset($servers[$embeddedServerIndex]);
}
} else {
# If a databasee download is being done, and the embedded server only has file download enabled,
# then remove the embedded server from the list of servers.
if ($dataLoadOptions === ServerConfig::DATA_LOAD_FILE_ONLY) {
unset($servers[$embeddedServerIndex]);
}
}
}
return $servers;
}
*/
public function setUserPrivateServerNames($username, $userServerNames)
{
$this->getSettings()->setUserPrivateServerNames($username, $userServerNames);
$details = 'Allowable private servers modified for user ' . $username;
\REDCap::logEvent(self::CHANGE_LOG_ACTION, $details, null, null, self::LOG_EVENT);
}
public function processUserPrivateServers($username, $userServerNames, $privateServers)
{
$this->getSettings()->processUserPrivateServers($username, $userServerNames, $privateServers);
}
public function getUserPrivateServerNames($username, $privateServers)
{
$userServerNames = array();
# $userServers = $this->getSettings()->getUserPrivateServerNames($username);
$userServers = $this->getSettings()->getPrivateServersForUser($username);
foreach ($userServers as $serverName) {
#if the user-assigned server stills have private access, then keep it
if (in_array($serverName, $privateServers)) {
$userServerNames[] = $serverName;
}
}
if ($userServerNames != $userServers) {
$this->getSettings()->setUserPrivateServerNames($username, $userServerNames);
}
if ($userServerNames == null) {
$userServerNames = [];
}
return $userServerNames;
}
public function getPrivateServerUsers($serverName)
{
return $this->getSettings()->getPrivateServerUsers($serverName);
}
public function setPrivateServerUsers($serverName, $usernames)
{
$this->getSettings()->setPrivateServerUsers($serverName, $usernames);
$details = 'Allowable users for private server modified for server ' . $serverName;
\REDCap::logEvent(self::CHANGE_LOG_ACTION, $details, null, null, self::LOG_EVENT);
}
public function processPrivateServerUsers($serverName, $removeUsernames)
{
$this->getSettings()->processPrivateServerUsers($serverName, $removeUsernames);
}
#-------------------------------------------------------------------
# User ETL project methods
#-------------------------------------------------------------------
/**
* Gets the ETL projects for the specified user, or for the current
* user if no user is specified.
*
* @param string the username for which to get the ETL projects.
*
* @return array project IDs of projects that the user has permission to use REDCap-ETL.
*/
public function getUserEtlProjects($username = USERID)
{
return $this->getSettings()->getUserEtlProjects($username);
}
#/**
# * Checks if the specified project has at least one user that has
# * permission to user REDCap-ETL for the project.
# *
# * @return boolean true if there is at least one user who has permission to
# * run REDCap-ETL for the project, and false otherwise.
# */
#public function hasEtlUser($projectId = PROJECT_ID)
#{
# return $this->getSettings()->hasEtlUser($projectId);
#}
#/**
# * Gets the current username.
# *
# * @return string the username of the current user.
# */
#public function getUsername()
#{
# return USERID;
#}
public function isSuperUser($username = USERID)
{
$isSuperUser = false;
$user = $this->getUser($username);
if ($user === null) {
$isSuperUser = false;
} else {
$isSuperUser = $user->isSuperUser();
}
return $isSuperUser;
}
public static function getProjectIdConstant()
{
$projectId = null;
if (defined('PROJECT_ID')) {
$projectId = PROJECT_ID;
}
return $projectId;
}
/**
* Get a REDCap "from e-mail" address.
*/
public static function getFromEmail()
{
// phpcs:disable
global $from_email;
global $homepage_contact_email;
$fromEmail = '';
# Need to diable phpcs here, because the REDCap e-mail variables
# ($from_email and $homepage_contact_email) don't use camel-case.
if (!empty($from_email)) {
$fromEmail = $from_email;
} else {
$fromEmail = $homepage_contact_email;
}
// phpcs:enable
return $fromEmail;
}
/**
* Set the projects for which the specified user is allowed to use
* REDCap-ETL.
*
* @param string username the username for the user whose projects
* are being modified.
* @param array $projects an array of REDCap project IDS
* for which the user has ETL permission.
*/
public function setUserEtlProjects($username, $projects)
{
$this->getSettings()->setUserEtlProjects($username, $projects);
$details = 'ETL projects modified for user ' . $username;
\REDCap::logEvent(self::CHANGE_LOG_ACTION, $details, null, null, self::LOG_EVENT);
}
#-------------------------------------------------------------------
# (ETL) Configuration methods
#-------------------------------------------------------------------
/**
* Gets the specified configuration for the current user.
*
* @param string $name the name of the configuration to get.
* @return Configuration the specified configuration.
*/
public function getConfiguration($name, $projectId = PROJECT_ID)
{
return $this->getSettings()->getConfiguration($name, $projectId);
}
/**
* @param Configuration $configuration
* @param string $username
* @param string $projectId
*/
public function setConfiguration($configuration, $username = USERID, $projectId = PROJECT_ID)
{
$this->getSettings()->setConfiguration($configuration, $username, $projectId);
$details = 'REDCap-ETL configuration "' . $configuration->getName()
. '" updated (pid=' . $configuration->getProjectId() . ') ';
\REDCap::logEvent(self::CHANGE_LOG_ACTION, $details, null, null, self::LOG_EVENT);
}
public function setConfigSchedule($configName, $server, $schedule, $username = USERID, $projectId = PROJECT_ID)
{
$this->getSettings()->setConfigSchedule($configName, $server, $schedule, $username, $projectId);
$details = 'REDCap-ETL configuration "' . $configName
. '" schedule modified for user ' . $username . ' and project ID ' . $projectId . '.';
\REDCap::logEvent(self::CHANGE_LOG_ACTION, $details, null, null, self::LOG_EVENT);
}
public function addConfiguration($name, $username = USERID, $projectId = PROJECT_ID)
{
$dataExportRight = $this->getDataExportRight();
$this->getSettings()->addConfiguration($name, $username, $projectId, $dataExportRight);
$details = 'REDCap-ETL configuration "' . $name . '" created.';
\REDCap::logEvent(self::CHANGE_LOG_ACTION, $details, null, null, self::LOG_EVENT);
}
/**
* Copy configuration (only supports copying from/to same
* user and project).
*/
public function copyConfiguration($fromConfigName, $toConfigName, $username = USERID)
{
if (Authorization::hasEtlConfigNamePermission($this, $fromConfigName, PROJECT_ID)) {
$toExportRight = $this->getDataExportRight();
$this->getSettings()->copyConfiguration($fromConfigName, $toConfigName, $toExportRight);
$details = 'REDCap-ETL configuration "' . $fromConfigName . '" copied to "'
. $toConfigName . '" for user ' . USERID . ', project ID ' . PROJECT_ID . '.';
\REDCap::logEvent(self::CHANGE_LOG_ACTION, $details, null, null, self::LOG_EVENT);
} else {
throw new \Exception('You do not have permission to copy configuration "' . $configName . '".');
}
}
/**
* Renames configuration (only supports rename from/to same project).
*
* @param string $configName the name of the configuration to be renamed.
* @param string $newConfigName the new name for the configuration being renamed.
*/
public function renameConfiguration($configName, $newConfigName)
{
if (Authorization::hasEtlConfigNamePermission($this, $configName, PROJECT_ID)) {
$this->getSettings()->renameConfiguration($configName, $newConfigName);
$details = 'REDCap-ETL configuration "' . $configName . '" renamed to "'
. $newConfigName . '" for user ' . USERID . ', project ID ' . PROJECT_ID . '.';
\REDCap::logEvent(self::CHANGE_LOG_ACTION, $details, null, null, self::LOG_EVENT);
} else {
throw new \Exception('You do not have permission to remname configuration "' . $configName . '".');
}
}
public function removeConfiguration($configName)
{
if (Authorization::hasEtlConfigNamePermission($this, $configName, PROJECT_ID)) {
$this->getSettings()->removeConfiguration($configName);
$details = 'REDCap-ETL configuration "' . $configName . '" deleted.';
\REDCap::logEvent(self::CHANGE_LOG_ACTION, $details, null, null, self::LOG_EVENT);
} else {
throw new \Exception('You do not have permission to remove configuration "' . $configName . '".');
}
}
public function getConfigurationExportRight($configuration, $projectId = PROJECT_ID)
{
$exportRight = $configuration->getProperty(Configuration::DATA_EXPORT_RIGHT);
return $exportRight;
}
/**
* Gets all the configuration names for the specified project.
*/
public function getConfigurationNames($projectId = PROJECT_ID)
{
return $this->getSettings()->getConfigurationNames($projectId);
}
/**
* Gets the configuration names for the specified project that the specified user
* has permission to access.
*/
public function getAccessibleConfigurationNames($projectId = PROJECT_ID, $username = USERID)
{
$configNames = array();
$allConfigNames = $this->getConfigurationNames($projectId);
if ($this->getDataExportRight() == 1) {
# If user has "full data set" export permissions, improve performance by
# avoiding individual configuration permission checks below
$configNames = $allConfigNames;
#} else {
# foreach ($allConfigNames as $configName) {
# $config = $this->getConfiguration($configName, $projectId);
# if (Authorization::hasEtlConfigurationPermission($this, $config)) {
# array_push($configNames, $configName);
# }
# }
}
return $configNames;
}
/**
* Gets the configuration from the request (if any), and
* updates the user's session appropriately.
*
* @throws \Exception if the configuration name is invalid.
*/
public function getConfigurationFromRequest()
{
$configuration = null;
$configName = Filter::stripTags($_POST['configName']);
if (empty($configName)) {
$configName = Filter::stripTags($_GET['configName']);
if (empty($configName)) {
$configName = Filter::stripTags($_SESSION['configName']);
}
}
if (!empty($configName)) {
Configuration::validateName($configName); # throw exception if invalid
$configuration = $this->getConfiguration($configName, PROJECT_ID);
if (empty($configuration)) {
$configName = '';
}
} else {
$configName = '';
}
$_SESSION['configName'] = $configName;
return $configuration;
}
#-------------------------------------------------------------------
# Cron job methods
#-------------------------------------------------------------------
/**
* Gets all the cron jobs (for all users and all projects).
*/
public function getAllCronJobs()
{
return $this->getSettings()->getAllCronJobs();
}
/**
* Gets the cron jobs for the specified day (0 = Sunday, 1 = Monday, ...)
* and time (0 = 12am - 1am, 1 = 1am - 2am, ..., 23 = 11pm - 12am).
*/
public function getTaskCronJobs($day, $time)
{
return $this->getSettings()->getTaskCronJobs($day, $time);
}
#==================================================================================
# Admin Config methods
#==================================================================================
public function getAdminConfig()
{
return $this->getSettings()->getAdminConfig();
}
public function setAdminConfig($adminConfig)
{
$this->getSettings()->setAdminConfig($adminConfig);
$details = 'REDCap-ETL admin configuration "' . $configName . '" modified.';
\REDCap::logEvent(self::CHANGE_LOG_ACTION, $details, null, null, self::LOG_EVENT);
}
#==================================================================================
# Server methods
#==================================================================================
public function getServers()
{
return $this->getSettings()->getServers();
}
/**
* Adds a new server with the specified server name.
*
* @param string $serverName the name of the server to add.
*/
public function addServer($serverName)
{
$this->getSettings()->addServer($serverName);
$details = 'Server "' . $serverName . '" created.';
\REDCap::logEvent(self::CHANGE_LOG_ACTION, $details, null, null, self::LOG_EVENT);
}
public function copyServer($fromServerName, $toServerName)
{
if (strcasecmp($fromServerName, ServerConfig::EMBEDDED_SERVER_NAME) === 0) {
throw new \Exception('The embedded server "' . ServerConfig::EMBEDDED_SERVER_NAME . '" cannot be copied.');
}
$this->getSettings()->copyServer($fromServerName, $toServerName);
$details = 'Server "' . $fromServerName . '" copied to "' . $toServerName . '".';
\REDCap::logEvent(self::CHANGE_LOG_ACTION, $details, null, null, self::LOG_EVENT);
}
public function renameServer($serverName, $newServerName)
{
if (strcasecmp($serverName, ServerConfig::EMBEDDED_SERVER_NAME) === 0) {
throw new \Exception('The embedded server "' . ServerConfig::EMBEDDED_SERVER_NAME . '" cannot be renamed.');
}
$this->getSettings()->renameServer($serverName, $newServerName);
$details = 'Server "' . $serverName . '" renamed to "' . $newServerName . '".';
\REDCap::logEvent(self::CHANGE_LOG_ACTION, $details, null, null, self::LOG_EVENT);
}
public function removeServer($serverName)
{
if (strcasecmp($serverName, ServerConfig::EMBEDDED_SERVER_NAME) === 0) {
throw new \Exception('The embedded server "' . ServerConfig::EMBEDDED_SERVER_NAME . '" cannot be deleted.');
}
$this->getSettings()->removeServer($serverName);
$details = 'Server "' . $serverName . '" deleted.';
\REDCap::logEvent(self::CHANGE_LOG_ACTION, $details, null, null, self::LOG_EVENT);
}
#==================================================================================
# Server Config methods
#==================================================================================
public function getServerConfig($serverName)
{
return $this->getSettings()->getServerConfig($serverName);
}
public function setServerConfig($serverConfig)
{
$this->getSettings()->setServerConfig($serverConfig);
$details = 'Server "' . $serverName . '" modified.';
\REDCap::logEvent(self::CHANGE_LOG_ACTION, $details, null, null, self::LOG_EVENT);
}
#==================================================================================
# Last run time methods
#==================================================================================
public function getLastRunTime()
{
return $this->getSettings()->getLastRunTime();
}
public function setLastRunTime($date, $hour, $minutes)
{
$this->getSettings()->setLastRunTime($date, $hour, $minutes);
# Don't log this because it is an internal event
}
public function isLastRunTime($date, $hour)
{
return $this->getSettings()->isLastRunTime($date, $hour);
}
#=============================================================
# Help methods
#=============================================================
public function getHelpSetting($topic)
{
return $this->getSettings()->getHelpSetting($topic);
}
public function setHelpSetting($topic, $setting)
{
$this->getSettings()->setHelpSetting($topic, $setting);
}
public function getCustomHelp($topic)
{
return $this->getSettings()->getCustomHelp($topic);
}
public function setCustomHelp($topic, $help)
{
$this->getSettings()->setCustomHelp($topic, $help);
}
/**