-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunctions.php
More file actions
298 lines (278 loc) · 13.2 KB
/
functions.php
File metadata and controls
298 lines (278 loc) · 13.2 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
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
<?php
namespace Vanderbilt\DataModelBrowserExternalModule;
use Vanderbilt\DataModelBrowserExternalModule\JsonPDF;
use Vanderbilt\DataModelBrowserExternalModule\ProjectData;
function array_filter_empty($array)
{
foreach ($array as $key => &$value) {
if (is_array($value)) {
$value = array_filter_empty($value);
}
if (is_array($value) && empty($value)) {
unset($array[$key]);
}
}
return $array;
}
function multi_array_diff($arr1, $arr2){
$arrDiff = array();
foreach($arr1 as $key => $val) {
if(isset($arr2[$key])){
if(is_array($val)){
$arrDiff[$key] = multi_array_diff($val, $arr2[$key]);
}else{
if(is_array($arr2) && in_array($val, $arr2)!= 1){
$arrDiff[$key] = $val;
}
}
}else if(isset($val)){
$arrDiff[$key] = $val;
}
}
return $arrDiff;
}
function getCrypt($string, $action = 'e',$secret_key="",$secret_iv="" ) {
$output = false;
$encrypt_method = "AES-256-CBC";
$key = hash( 'sha256', $secret_key );
$iv = substr( hash( 'sha256', $secret_iv ), 0, 16 );
if( $action == 'e' ) {
$output = base64_encode( openssl_encrypt( $string, $encrypt_method, $key, 0, $iv ) );
}
else if( $action == 'd' ){
$output = openssl_decrypt( base64_decode( $string ), $encrypt_method, $key, 0, $iv );
}
return $output;
}
function printFile($module,$edoc, $type){
$file = "#";
if($edoc != ""){
$q = $module->query("SELECT stored_name,doc_name,doc_size,mime_type FROM redcap_edocs_metadata WHERE doc_id=?",[$edoc]);
while ($row = $q->fetch_assoc()) {
$url = 'downloadFile.php?sname=' . $row['stored_name'] . '&file=' . urlencode($row['doc_name'])."&NOAUTH";
$base64 = base64_encode(file_get_contents($module->framework->getSafePath($row['stored_name'], EDOC_PATH)));
if($type == "img"){
$file = '<br/><div class="inside-panel-content"><img src="data:'.$row['mime_type'].';base64,' . $base64. '" style="display: block; margin: 0 auto;"></div>';
}else if($type == "logo"){
$file = '<img src="data:'.$row['mime_type'].';base64,' . $base64. '" class="wiki_logo_img" style="height:40px;">';
}else if($type == "imgpdf"){
$file = '<div style="max-width: 450px;height: 500px;"><img src="data:'.$row['mime_type'].';base64,' . $base64. '" style="display: block; margin: 0 auto;width:450px;height: 450px;"></div>';
}else if($type == "url"){
$file = $module->getUrl($url);
}else{
$file = '<br/><div class="inside-panel-content"><a href="'.$module->getUrl($url,true).'" target="_blank"><span class="fa fa-file-o"></span> ' . $row['doc_name'] . '</a></div>';
}
}
}
return $file;
}
function getDataRepeatingInstrumentsGroupByField($module,$project_id,$vars=""){
$data_ditionary = $module->framework->dataDictionaryCSVToMetadataArray($module->framework->getModulePath()."csv/DATAMODEL_data_dictionary.csv");
$array = array();
$records = \REDCap::getData($project_id,'array',$vars);
$index=0;
foreach ($records as $record=>$record_array) {
foreach ($record_array as $event=>$data) {
if($event == 'repeat_instances'){
foreach ($data as $eventarray){
foreach ($eventarray as $instrument=>$instrumentdata){
$count = 0;
foreach ($instrumentdata as $instance=>$instancedata){
foreach ($data_ditionary['B'] as $key=>$value){
if($instrument == $value && array_key_exists($data_ditionary['A'][$key],$instancedata)){
if(!array_key_exists($data_ditionary['A'][$key],$array[$index]) || (array_key_exists($data_ditionary['A'][$key],$array[$index]) && !is_array($array[$index][$data_ditionary['A'][$key]]))){
$array[$index][$data_ditionary['A'][$key]] = array();
}
else if(is_array($array[$index][$data_ditionary['A'][$key]]) && !empty($array[$index][$data_ditionary['A'][$key]]) && is_array($instancedata[$data_ditionary['A'][$key]]) && count($instancedata[$data_ditionary['A'][$key]]) == 1 && $count==0){
$array[$index][$data_ditionary['A'][$key]] = array();
}
array_push($array[$index][$data_ditionary['A'][$key]],$instancedata[$data_ditionary['A'][$key]]);
}
}
$count++;
}
}
}
}else{
$array[$index] = $data;
}
}
$index++;
}
return $array;
}
/**
* Function that generates an array with the table name and event information
* @param $projectID, the project we want to search in
* @param $dataTable, the array we are going to fill up
* @return mixed, the array $dataTable we are going to fill up
*/
function generateTableArray($module, $project_id, $tableID=null, $tableOrderParam="table_order"){
if(empty($tableID)){
$recordsTable = getDataRepeatingInstrumentsGroupByField($module,$project_id);
}else{
$recordsTable = getDataRepeatingInstrumentsGroupByField($module,$project_id, array('record_id' => $tableID));
}
$dataFormat = $module->getChoiceLabels('data_format', $project_id);
$dataTable = [];
$dataTable['data_format_label'] = $dataFormat;
foreach($recordsTable as $record ){
#we sort the variables by value and keep key
if(is_array($record['variable_order'])) {
asort($record['variable_order']);
}
if(!empty($record['record_id'])){//Variables
$dataTable[$record['record_id']] = $record;
}
}
#We order the tables
array_sort_by_column($dataTable, $tableOrderParam);
return $dataTable;
}
function array_sort_by_column(&$arr, $col, $dir = SORT_ASC) {
$sort_col = array();
foreach ($arr as $key=> $row) {
$sort_col[$key] = $row[$col];
}
array_multisort($sort_col, $dir, $arr);
}
function getHtmlTableCodesTableArrayExcel($module,$dataTable,$pidsArray){
$data_array = array();
$dataFormat = $module->getChoiceLabels('data_format', $pidsArray['DATAMODEL']);
foreach ($dataTable as $data) {
if (!empty($data['record_id']) && ($data['table_status'] == "1" || !array_key_exists('table_status',$data) || $data['table_status'] != 3)) {
$data_code_array = array();
foreach ($data['variable_order'] as $id=>$value) {
if($data['variable_status'][$id] == "1" && $data['has_codes'][$id] == "1") {
$data_code_array[0] = $data["table_name"];
$data_code_array[1] = !array_key_exists($id, $data['variable_name']) ? $data['variable_name'][''] : $data['variable_name'][$id];
$description = empty($data["description"][$id]) ? $data["description"][''] : $data["description"][$id];
if (!empty($data['description_extra'][$id])) {
$description .= "\n" . $data['description_extra'][$id];
}
if ($data['has_codes'][$id] == '1') {
if (!empty($data['code_list_ref'][$id])) {
$RecordSetCodeList = \REDCap::getData($pidsArray['CODELIST'], 'array', array('record_id' => $data['code_list_ref'][$id]));
$codeformat = ProjectData::getProjectInfoArrayRepeatingInstruments($RecordSetCodeList,$pidsArray['CODELIST'])[0];
if ($codeformat['code_format'] == '1') {
$codeOptions = empty($codeformat['code_list']) ? $data['code_text'][$id] : explode(" | ", $codeformat['code_list']);
foreach ($codeOptions as $option) {
$var_codes = preg_split("/((?<!['\"])=(?!['\"]))/", $option);
$data_code_array[2] = htmlentities(trim($var_codes[0]));
$data_code_array[3] = htmlentities(trim($var_codes[1]));
array_push($data_array, $data_code_array);
}
} else {
if ($codeformat['code_format'] == '3') {
if (array_key_exists('code_file', $codeformat) && $data['codes_print'][$id] == '1') {
$data_array = \Vanderbilt\DataModelBrowserExternalModule\getHtmlCodesTableArrayExcel($module, $data_array, $data_code_array, $codeformat['code_file']);
}
} else if ($codeformat['code_format'] == '4') {
$data_code_array[2] = 'https://bioportal.bioontology.org/ontologies/' . $codeformat['code_ontology'];
array_push($data_array, $data_code_array);
}
}
}
} else if (!empty($data['code_text'][$id])) {
$data_code_array[2] = htmlentities($dataFormat[$data['data_format'][$id]]);
$data_code_array[3] = htmlentities($description);
array_push($data_array, $data_code_array);
}
}
}
}
}
return $data_array;
}
function getHtmlCodesTableArrayExcel($module, $data_array,$data_code_array,$code_file)
{
$csv = \Vanderbilt\DataModelBrowserExternalModule\parseCSVtoArray($module,$code_file);
if (!empty($csv)) {
foreach ($csv as $header => $content) {
if ($header != 0) {
$index = 2;
foreach ($content as $col => $value) {
#Convert to UTF-8 to avoid weird characters
$value = mb_convert_encoding($value, 'UTF-8', 'HTML-ENTITIES');
$data_code_array[$index] = $value;
$index++;
}
array_push($data_array,$data_code_array);
}
}
}
return $data_array;
}
/**
* Table list with anchor links for the JsonPDF
* @param $dataTable
* @return string
*/
function generateRequestedTablesList_pdf($dataTable,$draft,$deprecated){
$requested_tables = "<ol>";
foreach ($dataTable as $data) {
if (!empty($data['record_id']) && ($data['table_status'] == "1" || !array_key_exists("table_status",$data) || ($data['table_status'] == "2" && $deprecated == "true") || ($data['table_status'] == "0" && $draft == "true"))) {
$requested_tables .= "<li><a href='#anchor_" . $data['record_id'] . "' style='text-decoration:none'>" . $data["table_name"] . "</a></li>";
}
}
$requested_tables .= "</ol>";
return $requested_tables;
}
function isUserExpiredOrSuspended($module,$username,$field){
$result = $module->query("SELECT ".$field." FROM redcap_user_information WHERE username = ?",[$username]);
while($row = db_fetch_assoc($result)){
if($row[$field] == null || $row[$field] == "" || strtotime($row[$field]) > strtotime(date("Y-m-d"))) {
#Not Expired
return false;
}
}
#User Expired
return true;
}
/**
* Function that searches the file name in the database, parses it and returns an array with the content
* @param $DocID, the id of the document
* @return array, the generated array with the data
*/
function parseCSVtoArray($module,$DocID){
$q = $module->query("SELECT * FROM `redcap_edocs_metadata` WHERE doc_id = ?",[$DocID]);
$csv = array();
while ($rowTableCSV = $q->fetch_assoc()) {
$csv = \Vanderbilt\DataModelBrowserExternalModule\createArrayFromCSV($module->framework->getSafePath($rowTableCSV['stored_name'], EDOC_PATH));
}
return $csv;
}
/**
* Function that parses de CSV file to an Array
* @param $filepath, the path of the file
* @param $filename, the file name
* @return array, the generated array with the CSV data
*/
function createArrayFromCSV($filepath, $addHeader = false){
$csv = array_map('str_getcsv', file($filepath));
array_walk($csv, function(&$a) use ($csv) {
$a = array_combine($csv[0], $a);
});
if($addHeader){
# remove column header
array_shift($csv);
}
return $csv;
}
/**
* Function that searches the file name in the database and returns a string with the link info to download the file
* @param $DocID, the id of the document
* @return string, the parameters needed to create a link and download the file
*/
function parseCSVtoLink($module,$DocID){
$q = $module->query("SELECT * FROM `redcap_edocs_metadata` WHERE doc_id = ?",[$DocID]);
$link = "";
while ($rowTableCSV = $q->fetch_assoc()) {
$link = "sname=" . $rowTableCSV['stored_name'] . "&file=" . $rowTableCSV['doc_name'];
}
return $link;
}
function getDataTable($project_id){
return method_exists('\REDCap', 'getDataTable') ? \REDCap::getDataTable($project_id) : "redcap_data";
}
?>