-
Notifications
You must be signed in to change notification settings - Fork 4
/
config.php
663 lines (615 loc) · 30 KB
/
config.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
<?php
namespace Vanderbilt\FlightTrackerExternalModule;
use \Vanderbilt\CareerDevLibrary\Download;
use Vanderbilt\CareerDevLibrary\Portal;
use \Vanderbilt\CareerDevLibrary\Upload;
use \Vanderbilt\CareerDevLibrary\Scholar;
use \Vanderbilt\CareerDevLibrary\Links;
use \Vanderbilt\CareerDevLibrary\REDCapManagement;
use \Vanderbilt\CareerDevLibrary\Application;
use \Vanderbilt\CareerDevLibrary\Sanitizer;
use \Vanderbilt\CareerDevLibrary\DataDictionaryManagement;
use \Vanderbilt\CareerDevLibrary\Wrangler;
require_once(dirname(__FILE__)."/classes/Autoload.php");
require_once(dirname(__FILE__)."/FlightTrackerExternalModule.php");
define('MAX_DEGREE_SOURCES', 5);
if (isset($_GET['uploadOrder'])) {
require_once(dirname(__FILE__)."/small_base.php");
$html = uploadOrderToMetadata($token, $server, $_POST);
echo $html;
exit();
} else {
require_once(dirname(__FILE__)."/charts/baseWeb.php");
?>
<style>
td { padding: 8px; }
</style>
<div id='overlay'></div>
<?php
}
echo "<h1>Configure ".CareerDev::getProgramName()."</h1>\n";
if (count($_POST) > 0) {
if (isset($_GET['order'])) {
if ($_POST['text'] && $_POST['code'] && ($_POST['type'] != "")) {
$exampleField = getExampleField();
$text = Sanitizer::sanitizeWithoutChangingQuotes($_POST['text']);
$code = Sanitizer::sanitize($_POST['code']);
$type = Sanitizer::sanitize(['type']);
$metadata = Download::metadata($token, $server);
if (isset($_GET['test'])) {
echo "In config 1, metadata has ".count($metadata)." rows<br>";
}
$choices = Scholar::getChoices($metadata);
if (!isset($choices[$exampleField][$code])) {
$i = 0;
$rowsAffected = 0;
foreach ($metadata as $row) {
if (preg_match("/_source$/", $row['field_name'])) {
if ($row['select_choices_or_calculations']) {
$row['select_choices_or_calculations'] .= " | ";
}
$row['select_choices_or_calculations'] .= "$code, $text";
$metadata[$i] = $row;
$rowsAffected++;
}
$i++;
}
$res = Scholar::addSourceType(CareerDev::getModule(), $code, $type, $pid);
if ($res) {
Upload::metadata($metadata, $token, $server);
echo "<p class='green centered'>$rowsAffected fields affected.</p>";
} else {
echo "<p class='red centered'>Could not add ".Sanitizer::sanitize($code)." to data sources!</p>";
}
} else {
echo "<p class='red centered'>Could not add because code ('".Sanitizer::sanitize($code)."') already exists.</p>";
}
} else {
echo "<p class='red centered'>You must specify the text, a value for its code, and a type.</p>";
}
} else {
$lists = [];
$optionSettings = REDCapManagement::getOptionalSettings();
$associativeArrays = [];
Application::saveSetting(Wrangler::PILOT_GRANT_SETTING, [], $pid); // clear out associative arrays
foreach ($_POST as $key => $value) {
$key = Sanitizer::sanitizeWithoutChangingQuotes($key);
$value = Sanitizer::sanitizeWithoutChangingQuotes($value);
while (preg_match("/&/", $value)) {
# to correct for but where repetitive ampersands were being produced on every save
# e.g., &amp;amp;...
$value = str_replace("&", "&", $value);
}
$value = REDCapManagement::changeSlantedQuotes($value);
if (preg_match("/^(\w+)___(\d+)___(code|text)$/", $key, $matches)) {
$var = $matches[1];
$i = $matches[2];
$type = $matches[3];
if (($value !== "") && ($type == "code") && isset($_POST[$var."___".$i."___text"])) {
$code = $value;
$textLabel = Sanitizer::sanitizeWithoutChangingQuotes($_POST[$var."___".$i."___text"]);
$textLabel = REDCapManagement::changeSlantedQuotes($textLabel);
if (!isset($associativeArrays[$var])) {
$associativeArrays[$var] = [];
}
$associativeArrays[$var][$code] = $textLabel;
}
}
if (isset($optionSettings[$key])) {
$lists[$key] = $value;
Application::saveSetting($key, $value, $pid);
} else if (in_array($key, ["departments", "resources"])) {
$lists[$key] = $value;
} else if (in_array($key, ["pid", "event_id"])) {
$module = Application::getModule();
if ($key == "pid") {
$value = $module->getProjectId();
} else if ($key == "event_id") {
$value = $module->getEventId();
}
Application::saveSetting($key, $value, $pid);
} else if ($key == "bulletin_board_monitor") {
Application::saveSystemSetting($key, $value);
} else {
Application::saveSetting($key, $value, $pid);
}
}
foreach ($associativeArrays as $var => $array) {
Application::saveSetting($var, $array, $pid);
}
$lists["institutions"] = implode("\n", CareerDev::getInstitutions());
$metadata = Download::metadata($token, $server);
if (isset($_GET['test'])) {
echo "In config 2, metadata has ".count($metadata)." rows<br>";
}
$token = Application::getSetting("token", $pid);
$feedback = DataDictionaryManagement::addLists($pid, $lists, CareerDev::getSetting("hasCoeus"), $metadata);
if (is_array($feedback)) {
$feedback = json_encode($feedback);
}
echo "<p class='centered green'>Saved ".count($_POST)." settings into pid $pid ($feedback)</p>\n";
}
}
$configJSLink = Application::link("js/config.js");
echo "<script src='$configJSLink'></script>";
if (isset($_GET['order'])) {
if (!isset($metadata)) {
$metadata = Download::metadata($token, $server);
}
echo makeOrder($token, $server, $pid, $metadata);
} else {
echo makeSettings(CareerDev::getModule(), $pid);
}
function getFieldNames($metadata) {
$fields = array();
foreach ($metadata as $row) {
$fields[] = $row['field_name'];
}
return $fields;
}
function getExampleField() {
return Scholar::getExampleField();
}
function getExistingChoices($existingChoices, $scholar, $allFields) {
$choices = array();
$orders = Scholar::getDefaultOrder("all");
foreach ($existingChoices as $key => $text) {
foreach ($orders as $fieldForOrder => $order) {
if (!isset($choices[$key])) {
$newOrder = $scholar->getOrder($order, $fieldForOrder);
foreach ($newOrder as $field => $source) {
if (($source == $key) && in_array($field, $allFields) && !isset($choices[$key])) {
$choices[$key] = $text;
}
}
}
}
}
return $choices;
}
function getExistingChoicesTexts($existingChoices, $scholar, $allFields) {
$choices = getExistingChoices($existingChoices, $scholar, $allFields);
$texts = array();
foreach ($choices as $key => $value) {
$texts[] = "$key = $value";
}
return $texts;
}
function makeOrder($token, $server, $pid, $metadata = []) {
$exampleField = getExampleField();
$delim = getUploadDelim();
if (empty($metadata)) {
$metadata = Download::metadata($token, $server);
if (isset($_GET['test'])) {
echo "In config 3, metadata has ".count($metadata)." rows<br>";
echo "$token, $server, $pid<br>";
}
}
$scholar = new Scholar($token, $server, $metadata, $pid);
$orders = Scholar::getDefaultOrder("all");
$choices = Scholar::getChoices($metadata);
$allFields = REDCapManagement::getFieldsFromMetadata($metadata);
$fieldLabels = array();
foreach ($orders as $fieldForOrder => $order) {
$fieldLabels[$fieldForOrder] = findFieldLabel($fieldForOrder, $metadata);
}
list($sources, $sourceTypes) = produceSourcesAndTypes($scholar, $metadata);
$existingChoicesTexts = getExistingChoicesTexts($choices[$exampleField], $scholar, $allFields);
$button = "<p class='centered'><button onclick='commitOrder(); return false;'>Commit All Changes</button></p>\n";
$html = "";
$html .= "<script src='".CareerDev::link("/js/config.js")."'></script>\n";
$html .= "<script>\n";
$html .= "var maxDegrees = ".MAX_DEGREE_SOURCES.";\n";
$html .= "$(document).ready(function() { $('.sortable').sortable({ revert: true }); $('ul.sortable, li').disableSelection(); });\n";
$html .= "</script>\n";
$html .= "<h2>Add New Data Source</h2>\n";
$html .= "<table style='width: 800px;' class='centered'>\n";
$html .= "<tr><td colspan='2'>\n";
$html .= "<p class='centered'>To add a new data source, you must create a code for it (no spaces [like <code>initial_survey</code>], then name it, and then select its type (computer-generated, self-reported, or manually entered). It will appear in existing data sources only when it is assigned to a field in the Source-of-Truth configuration below.</p>\n";
$html .= "</td></tr>\n";
$html .= "<tr>\n";
$html .= "<td style='vertical-align: top; width: 50%;'>\n";
$html .= "<h3>Default Data Sources</h3>\n";
$html .= "<p class='centered'>These are included by Flight Tracker by default. Custom data sources are shown in the dropdowns for a <b>New Source</b> below.</p>\n";
$html .= implode("<br>\n", $existingChoicesTexts);
$html .= "</td>\n";
$html .= "<td style='vertical-align: top; width: 50%;'><form method='POST' action='".CareerDev::link("config.php")."&order'>\n";
$html .= Application::generateCSRFTokenHTML();
$html .= "<h3>Add a Custom Data Source</h3>\n";
$html .= "<p class='centered'>Code: <input type='text' name='code' value=''><br>\n";
$html .= "Name: <input type='text' name='text' value=''><br>\n";
$html .= "Type: <select name='type'>\n";
$html .= "<option value=''>---SELECT---</option>\n";
foreach ($choices[$exampleField."type"] as $key => $text) {
$html .= "<option value='$key'>$text</option>\n";
}
$html .= "</select><br>\n";
$html .= "<button>Add Data Source</button></p>\n";
$html .= "</form></td>\n";
$html .= "</tr>\n";
$html .= "</table>\n";
$html .= "<hr>\n";
$html .= "<h2>Configure Source of Truth</h2>\n";
$html .= "<p class='centered' style='width: 800px; margin: 0 auto;'>The \"Source of Truth\" defines which field provides the chosen value. These values are re-calculated every night. They are defined in an order, with the top being given priority. Starting with the top value, if a data value for a field exists, the field is chosen; if a data value for the field does not exist, we move down one rung in the order until no more rungs exist. You may sort the order and add new fields here. New fields must be added for new data sources to be hooked up.</p>\n";
$html .= $button;
foreach ($sources as $fieldForOrder => $sourceList) {
$fieldLabel = $fieldLabels[$fieldForOrder];
if ($fieldLabel) {
$html .= "<div style='margin: 14px auto; max-width: 600px;'>\n";
$html .= "<h3>$fieldLabel</h3>\n";
if ($fieldForOrder == "summary_race_ethnicity") {
$numEntries = 2;
} else {
$numEntries = 1;
}
if ($numEntries == 1) {
$html .= "<ul class='sortable nobullets' id='$fieldForOrder'>\n";
}
foreach ($sourceList as $field => $source) {
if (is_array($source)) {
$sourceRow = $source;
$type = $field;
$html .= "<div>";
$html .= "<h4>".ucfirst($type)."</h4>\n";
$html .= "<ul class='sortable nobullets' id='$fieldForOrder$delim$type'>\n";
foreach ($sourceRow as $field => $source) {
if ($choices[$exampleField][$source]) {
$sourceName = $choices[$exampleField][$source];
} else {
$sourceName = $source;
}
$sourceTypeForField = $sourceTypes[$fieldForOrder][$type][$field];
$html .= makeLI($field, $sourceTypeForField, $sourceName, $field);
}
$html .= "</ul>\n";
$html .= "</div>";
$html .= makeNewSourceHTML($allFields, $choices[$exampleField], 1);
} else {
$sourceTypeForField = $sourceTypes[$fieldForOrder][$field];
if ($choices[$exampleField][$source]) {
$sourceName = $choices[$exampleField][$source];
} else {
$sourceName = $source;
}
$fields = explode($delim, $field);
if (count($fields) > 1) {
# summary_degrees
$fieldText = implode(", ", $fields);
$fieldID = implode($delim, $fields);
} else {
$fieldText = $fields[0];
$fieldID = $fields[0];
}
$html .= makeLI($fieldID, $sourceTypeForField, $sourceName, $fieldText);
}
}
if ($numEntries == 1) {
$html .= "</ul>\n";
if ($fieldForOrder == "summary_degrees") {
$numAdditionalSources = MAX_DEGREE_SOURCES;
} else {
$numAdditionalSources = 1;
}
$html .= makeNewSourceHTML($allFields, $choices[$exampleField], $numAdditionalSources);
}
$html .= "</div>\n";
}
}
$html .= $button;
return $html;
}
function makeLI($fieldID, $sourceTypeForField, $sourceName, $fieldText) {
# the following line is also replicated in config.js; please change it in both places
return "<li class='ui-state-default centered nobullets' id='$fieldID' type='$sourceTypeForField'>$sourceName [$fieldText]</li>\n";
}
function makeNewSourceHTML($allFields, $choices, $numSources) {
$html = "";
$html .= "<p class='centered'>";
$html .= "New Source: <select onchange='checkButtonVisibility(this);' class='newSortableSource'>\n";
$html .= "<option value=''>---SELECT---</option>\n";
foreach ($choices as $code => $text) {
$html .= "<option value='$code'>$text</option>\n";
}
$html .= "</select><br>\n";
for ($i = 1; $i <= $numSources; $i++) {
if ($numSources == 1) {
$index = "";
} else {
$index = "index='$i'";
$html .= "<br>\n";
}
$optional = "";
if ($i > 1) {
$optional = " (optional)";
}
$html .= "New Field$optional: <select onchange='checkButtonVisibility(this);' $index class='newSortableField combobox'>\n";
$html .= "<option value=''>---SELECT---</option>\n";
foreach ($allFields as $field) {
$html .= "<option value='$field'>$field</option>\n";
}
$html .= "</select><br>\n";
}
$html .= "<button style='display: none;' onclick='addCustomField(this); return false;'>Add</button></p>\n";
return $html;
}
function findFieldLabel($fieldName, $metadata) {
foreach ($metadata as $row) {
if ($row['field_name'] == $fieldName) {
return $row['field_label'];
}
}
return "";
}
function makeSettings($module, $pid) {
$ary = [];
$ary["Length of K Grants"] = [];
$ary["Length of K Grants"][] = makeSetting("internal_k_length", "number", "Internal K Length in Years", "3");
$ary["Length of K Grants"][] = makeSetting("k12_kl2_length", "number", "K12/KL2 Length in Years", "3");
$ary["Length of K Grants"][] = makeSetting("individual_k_length", "number", "Length of NIH K Grants in Years", "5");
$ary["Installation Variables"] = [];
$ary["Installation Variables"][] = makeSetting("institution", "text", "Full Name of Institution");
$ary["Installation Variables"][] = makeSetting("short_institution", "text", "Short Name of Institution");
$ary["Installation Variables"][] = makeSetting("other_institutions", "text", "Other Institutions (if any); comma-separated");
$ary["Installation Variables"][] = makeSetting("display_institutions", "text", "'Home' Institutions that Your Scholars Belong To; comma-separated");
$ary["Installation Variables"][] = makeSetting("omit_va", "yesno", "Omit searching for pubs/grants with 'Veterans Health Administration'? (Default: searches for VHA with all scholars for matches.)", 0);
$ary["Installation Variables"][] = makeSetting("token", "text", "API Token");
$ary["Installation Variables"][] = makeSetting("supertoken", "text", "REDCap Supertoken (optional, from REDCap Administrator, for turning on Cohort Portals)");
$ary["Installation Variables"][] = makeSetting("event_id", "text", "Event ID (read-only)", "", [], TRUE);
$ary["Installation Variables"][] = makeSetting("pid", "text", "Project ID (read-only)", "", [], TRUE);
$ary["Installation Variables"][] = makeSetting("server", "text", "Server API Address");
$ary["Installation Variables"][] = makeSetting("tokenName", "text", "Project Name");
$ary["Installation Variables"][] = makeSetting("timezone", "text", "Timezone");
$ary["Installation Variables"][] = makeSetting("grant_class", "radio", "Grant Class", "", CareerDev::getGrantClasses());
$ary["Installation Variables"][] = makeSetting("grant_number", "text", "Grant Number");
$ary["Installation Variables"][] = makeSetting("server_class", "radio", "Server Class", "prod", CareerDev::getServerClasses());
$ary["Installation Variables"][] = makeSetting("send_error_logs", "yesno", "Report Fatal Errors to Development Team?");
$ary["Installation Variables"][] = makeSetting("auto_recalculate", "yesno", "Automatically Re-summarize After Data Saves? (No waits until overnight.)", 0);
$ary["Installation Variables"][] = makeSetting("security_test_mode", "yesno", "Place in Security-Test mode (disabling unauthorized APIs)?", 0);
$ary["Administrative Setup"][] = makeSetting("departments", "textarea", "Department Names (One per line.)");
$ary["Administrative Setup"][] = makeSetting("resources", "short_textarea", "Resources (One per line.)");
$optionSettings = REDCapManagement::getOptionalSettings();
$fileMetadata = DataDictionaryManagement::getFileMetadata();
foreach(REDCapManagement::getOptionalFields() as $field) {
$setting = REDCapManagement::turnOptionalFieldIntoSetting($field);
$numSettings = REDCapManagement::getOptionalFieldsNumber($field);
$label = $optionSettings[$setting] ?? $field;
$row = DataDictionaryManagement::getRowForFieldFromMetadata($field, $fileMetadata);
$form = $row['form_name'] ?? "";
$fieldType = $row['field_type'];
$note = (isset($row['field_note']) && $row['field_note']) ? "<div class='smaller'>".$row['field_note']."</div>" : "";
$message = "<div>Options for <strong>$label</strong> on the <strong>".ucfirst($form)."</strong> form. These will be made into a $fieldType.</div>$note<div class='smaller'>(One per line. Optional. When filled in, it will create an extra field in your project once you update your Data Dictionary on Flight Tracker's Home page.)</div>";
$ary["Administrative Setup"][] = makeOptionalSetting($setting, "short_textarea", $message, $numSettings);
}
$ary["Administrative Setup"][] = makeCheckboxes("shared_forms", FlightTrackerExternalModule::getConfigurableForms(), "In Flight Tracker's data-sharing on the same server, in addition to surveys, what data should be shared among the following forms?");
$ary["Administrative Setup"][] = makeAssociativeArray(Wrangler::PILOT_GRANT_SETTING, "Options for <strong>Pilot Grants</strong><br/>Enter a unique code (left) and a label (right) to set up pilot grants to be associated during data wrangling.");
$ary["Administrative Setup"][] = makeSetting(DataDictionaryManagement::CUSTOM_SECTION_HEADER_SETTING, "short_textarea", "Custom Instructions to Put in Certain Section Headers on the Initial Survey and the Followup Survey. HTML welcomed.");
$ary["Administrative Setup"][] = makeSetting(DataDictionaryManagement::FUNDING_SECTION_HEADER_SETTING, "short_textarea", "Custom Instructions to Put in the Funding Section Headers on the Initial Survey and the Followup Survey. HTML welcomed.");
$ary["Emails"] = [];
$ary["Emails"][] = makeSetting("admin_email", "text", "Administrative Email(s) for Flight Tracker Project; comma-separated");
$ary["Emails"][] = makeSetting("default_from", "text", "Default From Address");
$ary["Emails"][] = makeSetting("warning_minutes", "number", "Number of Minutes Before An Email to Send a Warning Email", Application::getWarningEmailMinutes($pid));
if (Portal::isLive()) {
$ary["Emails"][] = makeSystemSetting("bulletin_board_monitor", "text", "Bulletin Board Monitor Email(s) for entire server; comma-separated");
}
$ary["Publications"] = [];
$ary["Publications"][] = makeSetting("pubmed_api_key", "text", "PubMed API Key for faster queries (".Links::makeLink("https://www.ncbi.nlm.nih.gov/books/NBK25497/#:~:text=API%20Keys", "Acquire an API Key for PubMed").")");
$ary["Publications"][] = makeSetting("wos_userid", "text", Links::makeLink("https://www.webofknowledge.com/", "Web of Science (for H Index)") . " User ID");
$ary["Publications"][] = makeSetting("wos_password", "text", Links::makeLink("https://www.webofknowledge.com/", "Web of Science (for H Index)") . " Password");
$ary["Publications"][] = makeSetting("scopus_api_key", "text", Links::makeLink("https://www.scopus.com/", "Scopus") . " API Key (for H Index)");
$ary["Additional Institutional Resources"] = [];
$ary["Additional Institutional Resources"][] = makeSetting("mentee_agreement_link", "text", "Additional Institutional Resources for scholars to use. This will appear on the Mentee-Mentor Agreements and the Scholar Portal.");
$ary["Proxy Server (Only if Applicable)"] = [];
$ary["Proxy Server (Only if Applicable)"][] = makeHelperText("If your REDCap server has a proxy server, please fill out the following information. (If you don't know about this, you probably don't have one, so no worries then.)");
$ary["Proxy Server (Only if Applicable)"][] = makeSetting("proxy-ip", "text", "Proxy IP Address");
$ary["Proxy Server (Only if Applicable)"][] = makeSetting("proxy-port", "text", "Proxy Port Number");
$ary["Proxy Server (Only if Applicable)"][] = makeSetting("proxy-user", "text", "Proxy Username");
$ary["Proxy Server (Only if Applicable)"][] = makeSetting("proxy-pass", "text", "Proxy Password");
$ary["REDCap Configuration"] = [];
$ary["REDCap Configuration"][] = makeSetting("safe_servers", "text", "Comma-separated list of domain names (e.g., redcap.vumc.org) of other <strong>external</strong> servers that (e.g., a separate REDCap Survey Server) that can access Flight Tracker");
$html = "";
if ($module) {
$html .= "<form method='POST' action='".Application::link("config.php")."'>\n";
$html .= Application::generateCSRFTokenHTML();
foreach ($ary as $header => $htmlAry) {
$id = REDCapManagement::makeHTMLId($header);
$html .= "<a id='$id'></a><h2>$header</h2>";
$html .= "<table class='centered' style='max-width: 600px;'>";
$html .= implode("\n", $htmlAry);
$html .= "<tr><td colspan='2' class='centered'><button onclick='return checkForRequiredFields();'>Save Settings</button></td></tr>";
$html .= "</table>";
}
$html .= "</form>\n";
} else {
throw new \Exception("Could not find module!");
}
return $html;
}
function makeHelperText($str) {
return "<tr><td colspan='2' class='centered'>".$str."</td></tr>";
}
function makeCheckboxes($var, $fieldChoices, $label, $defaultChecked = []) {
if (empty($fieldChoices)) {
return "";
}
$sharedForms = CareerDev::getSetting($var);
if (!$sharedForms) {
$sharedForms = $defaultChecked;
}
$html = "";
$html .= "<tr><td style='text-align: right;'>$label</td>\n";
$html .= "<td style='text-align: left;'>";
$first = TRUE;
foreach ($fieldChoices as $idx => $fieldLabel) {
if (in_array($idx, $sharedForms)) {
$selected = " checked";
} else {
$selected = "";
}
if (!$first) {
for ($i = 0 ; $i < 5; $i++) {
$html .= " ";
}
} else {
$first = FALSE;
}
$html .= "<span class='sameLine'><input type='checkbox' name='$var"."[]' id='$var"."___$idx' value='$idx'$selected><label for='$var"."___$idx'> $fieldLabel</label></span>";
}
$html .= "</td></tr>\n";
return $html;
}
function makeSystemSetting($var, $type, $label, $default = "", $fieldChoices = [], $readonly = FALSE) {
$value = Application::getSystemSetting($var);
if ($value === "") {
$value = $default;
}
return constructSetting($value, $var, $type, $label, $default, $fieldChoices, $readonly, "", "");
}
function makeOptionalSetting($var, $type, $message, $numSettings) {
$divPrefix = "div_";
$i = 1;
$baseField = preg_replace("/_\d+$/", "", $var);
if (preg_match("/_(\d+)$/", $var, $matches)) {
$i = (int) $matches[1];
}
if ($i < $numSettings) {
$nextField = REDCapManagement::getOptionalFieldSetting($baseField, $i + 1);
} else {
$nextField = "";
}
if ($i == 1) {
$lastValue = FALSE;
} else {
$lastI = $i - 1;
$lastField = REDCapManagement::getOptionalFieldSetting($baseField, $lastI);
$lastValue = Application::getSetting($lastField);
}
$value = Application::getSetting($var);
$trAttributes = "id='$divPrefix$var'";
if (($i > 1) && !$value && !$lastValue) {
$trAttributes = "id='$divPrefix$var' style='display: none;'";
}
$js = $nextField ? "if ($(this).val() || $('#$divPrefix$nextField').val()) { $('#$divPrefix$nextField').show(); } else { $('#$divPrefix$nextField').hide(); }" : "";
return constructSetting($value, $var, $type, $message, "", [], FALSE, $js, $trAttributes);
}
function makeSetting($var, $type, $label, $default = "", $fieldChoices = [], $readonly = FALSE)
{
$value = Application::getSetting($var);
if ($value === "") {
$value = $default;
}
return constructSetting($value, $var, $type, $label, $default, $fieldChoices, $readonly, "", "");
}
function constructSetting($value, $var, $type, $label, $default, $fieldChoices, $readonly, $js, $trAttributes) {
# only one of these is used
$onblur = $js ? " onblur=\"$js\" " : "";
$onchange = $js ? " onchange=\"$js\" " : "";
if (in_array($var, ["event_id", "pid", "token"])) {
$module = Application::getModule();
$realValue = "";
if ($var == "event_id") {
$realValue = $module->getEventId();
} else if ($var == "pid") {
$realValue = $module->getProjectId();
} else if ($var == "token") {
# check for copied project
$myPid = $module->getProjectId();
$myUsername = Application::getUsername();
$realValue = REDCapManagement::getToken($myPid, $myUsername);
if ($realValue === NULL) {
$realValue = "";
}
if (!$realValue) {
$allTokens = REDCapManagement::getToken($myPid);
if (in_array($value, $allTokens)) {
$realValue = $value;
}
}
}
if ($realValue != $value) {
# recently copied project
Application::saveSetting($var, $realValue);
$value = $realValue;
}
}
$html = "";
$spacing = "";
for ($i = 0 ; $i < 5; $i++) {
$spacing .= " ";
}
if (($type == "text") || ($type == "number")) {
$html .= "<tr $trAttributes>";
$html .= "<td style='text-align: right;'>";
$html .= $label;
if ($default) {
$html .= " (default: ".$default.")";
if (!$value) {
$value = $default;
}
}
$html .= "</td><td style='text-align: left;'>";
$html .= "<input type='$type' name='$var' value=\"$value\" $onblur";
if ($readonly) {
$html .= " readonly";
}
$html .= ">\n";
$html .= "</td>";
$html .= "</tr>";
} else if (($type == "radio") || ($type == "yesno")) {
if ($type == "yesno") {
$fieldChoices = ["0" => "No", "1" => "Yes"];
}
$html .= "<tr $trAttributes>";
$html .= "<td style='text-align: right;'>";
$html .= $label;
$html .= "</td><td style='text-align: left;'>";
$options = [];
foreach ($fieldChoices as $idx => $fieldLabel) {
if ($idx == $value) {
$selected = " checked";
} else {
$selected = "";
}
$html .= "<div><input type='radio' name='$var' id='$var"."___$idx' value='$idx'$selected$onchange /><label for='$var"."___$idx'> $fieldLabel</label></div>";
}
$html .= implode($spacing, $options);
$html .= "</td>";
$html .= "</tr>";
} else if (in_array($type, ["textarea", "short_textarea"])) {
$html .= "<tr $trAttributes>";
$html .= "<td colspan='2'>";
if (preg_match("/^<div/", $label)) {
$html .= $label;
} else {
$html .= "<div>$label</div>";
}
if ($type == "short_textarea") {
$html .= "<textarea class='config' style='height: 250px;' name='$var' $onblur>$value</textarea>";
} else {
$html .= "<textarea class='config' name='$var' $onblur>$value</textarea>";
}
$html .= "</td>";
$html .= "</tr>";
}
return $html;
}
function makeAssociativeArray($var, $label) {
$priorSettings = Application::getSetting($var);
$html = "<tr><td colspan='2'>$label</td></tr>";
$i = 1;
$html .= "<tr><th>Code for REDCap<div class='smaller unbolded'>This text will be stored in REDCap. An example is \"field_name\". It should remain constant once it is assigned.</div></th><th>Label Text<br/><div class='unbolded smaller'>This text will be displayed during data wrangling. It can be changed and refined over time if desired. An example is \"Option 1\".</div></th></tr>";
foreach ($priorSettings as $code => $text) {
$html .= makeAssociativeRow($var, $i, $code, $text);
$i++;
}
$html .= makeAssociativeRow($var, $i);
return $html;
}
# also in js/config.js checkForNextField
function makeAssociativeRow($var, $i, $code = "", $text = "") {
$id = $var."___".$i."___";
$html = "<tr id='$id"."tr' class='$var"."___row'>";
$html .= "<td><input type='text' id='$id"."code' name='$id"."code' value='$code' onblur='checkForNextField(\"$var\", \"$i\");' /></td>";
$html .= "<td><input type='text' id='$id"."text' name='$id"."text' value='$text' onblur='checkForNextField(\"$var\", \"$i\");' /></td>";
$html .= "</tr>";
return $html;
}