-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmetadata.php
More file actions
141 lines (127 loc) · 4.89 KB
/
Copy pathmetadata.php
File metadata and controls
141 lines (127 loc) · 4.89 KB
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
<?php
use \Vanderbilt\FlightTrackerExternalModule\CareerDev;
use \Vanderbilt\CareerDevLibrary\Download;
use \Vanderbilt\CareerDevLibrary\Upload;
use \Vanderbilt\CareerDevLibrary\REDCapManagement;
require_once(dirname(__FILE__)."/small_base.php");
require_once(dirname(__FILE__)."/CareerDev.php");
require_once(dirname(__FILE__)."/classes/Download.php");
require_once(dirname(__FILE__)."/classes/Upload.php");
require_once(dirname(__FILE__)."/classes/REDCapManagement.php");
$filename = dirname(__FILE__)."/metadata.json";
$lastCheckField = "prior_metadata_ts";
$deletionRegEx = "/___delete$/";
if ($_POST['process'] == "check") {
$ts = $_POST['timestamp'];
$lastCheckTs = CareerDev::getSetting($lastCheckField);
if (!$lastCheckTs) {
$lastCheckTs = 0;
}
# check a maximum of once every 30 seconds
if ($ts > $lastCheckTs + 30) {
$fp = fopen($filename, "r");
$json = "";
while ($line = fgets($fp)) {
$json .= $line;
}
fclose($fp);
$metadata = array();
$metadata['file'] = json_decode($json, TRUE);
$metadata['REDCap'] = Download::metadata($token, $server);
$choices = array();
foreach ($metadata as $type => $md) {
$choices[$type] = REDCapManagement::getChoices($md);
}
$fieldList = array();
$indexedMetadata = array();
foreach ($metadata as $type => $metadataRows) {
$fieldList[$type] = array();
$indexedMetadata[$type] = array();
foreach ($metadataRows as $row) {
$fieldList[$type][$row['field_name']] = $row['select_choices_or_calculations'];
$indexedMetadata[$type][$row['field_name']] = $row;
}
}
$missing = array();
$additions = array();
$changed = array();
$metadataFields = REDCapManagement::getMetadataFieldsToScreen();
$specialFields = REDCapManagement::getSpecialFields("all");
foreach ($fieldList["file"] as $field => $choiceStr) {
if (!in_array($field, $specialFields)) {
if (!isset($fieldList["REDCap"][$field])) {
if (!preg_match("/^coeus_/", $field)) {
array_push($missing, $field);
if (!preg_match($deletionRegEx, $field)) {
array_push($additions, $field);
}
}
} else if ($choices["file"][$field] && $choices["REDCap"][$field] && !REDCapManagement::arraysEqual($choices["file"][$field], $choices["REDCap"][$field])) {
array_push($missing, $field);
array_push($changed, $field);
} else {
foreach ($metadataFields as $metadataField) {
if (REDCapManagement::hasMetadataChanged($indexedMetadata["REDCap"][$field][$metadataField], $indexedMetadata["file"][$field][$metadataField], $metadataField)) {
array_push($missing, $field);
array_push($changed, $field);
break; // metadataFields loop
}
}
}
}
}
CareerDev::setSetting($lastCheckField, time());
if (count($additions) + count($changed) > 0) {
echo "<script>var missing = ".json_encode($missing).";</script>\n";
echo "An upgrade in your Data Dictionary exists. <a href='javascript:;' onclick='installMetadata(missing);'>Click here to install.</a><br>The following fields will be added: ".(empty($additions) ? "none" : implode(", ", $additions))."<br>The following fields will be changed: ".(empty($changed) ? "none" : implode(", ", $changed));
}
}
} else if ($_POST['process'] == "install") {
$postedFields = $_POST['fields'];
$fp = fopen($filename, "r");
$json = "";
while ($line = fgets($fp)) {
$json .= $line;
}
fclose($fp);
$metadata = array();
$metadata['file'] = json_decode($json, TRUE);
$metadata['REDCap'] = Download::metadata($token, $server);
if ($metadata['file']) {
if ($grantClass == "K") {
$mentorLabel = "Primary mentor during your K/K12 training period";
} else if ($grantClass == "T") {
$mentorLabel = "Primary mentor during your pre-doc/post-doc training period";
} else {
$mentorLabel = "Primary mentor (current)";
}
$fieldLabels = array();
foreach ($metadata as $type => $md) {
$fieldLabels[$type] = REDCapManagement::getLabels($md);
}
$fieldsForMentorLabel = array("check_primary_mentor", "followup_primary_mentor", );
foreach ($fieldsForMentorLabel as $field) {
$metadata['file'] = changeFieldLabel($field, $mentorLabel, $metadata['file']);
if ($fieldLabels['file'][$field] != $fieldLabels['REDCap'][$field]) {
array_push($postedFields, $field);
}
}
try {
$feedback = REDCapManagement::mergeMetadataAndUpload($metadata['REDCap'], $metadata['file'], $token, $server, $postedFields, $deletionRegEx);
echo json_encode($feedback);
} catch (\Exception $e) {
$feedback = array("Exception" => $e->getMessage());
echo json_encode($feedback);
}
}
}
function changeFieldLabel($field, $label, $metadata) {
$i = 0;
foreach ($metadata as $row) {
if ($row['field_name'] == $field) {
$metadata[$i]['field_label'] = $label;
}
$i++;
}
return $metadata;
}