-
Notifications
You must be signed in to change notification settings - Fork 0
/
RequestedAndRequiredFields.php
309 lines (293 loc) · 13.9 KB
/
RequestedAndRequiredFields.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
<?php namespace INTERSECT\RequestedAndRequiredFields;
use \REDCap as REDCap;
use \Project as Project;
use ExternalModules\AbstractExternalModule;
class RequestedAndRequiredFields extends \ExternalModules\AbstractExternalModule {
function getTags($tags, $fields, $instruments) {
// This is straight out of Andy Martin's example post on this:
// https://community.projectredcap.org/questions/32001/custom-action-tags-or-module-parameters.html
if (!class_exists('INTERSECT\RequestedAndRequiredFields\ActionTagHelper')) include_once('classes/ActionTagHelper.php');
$action_tag_results = ActionTagHelper::getActionTags($tags, $fields, $instruments);
return $action_tag_results;
}
function redcap_survey_page($project_id, $record, $instrument) {
// Collect project settings
$settings = $this->getProjectSettings();
// Get all annotated fields
$requestedTag = "@REQUESTED";
$requiredTag = "@REQUIRED";
$tags = array($requestedTag, $requiredTag);
$annotatedFields = $this->getTags($tags, $fields=NULL, $instruments=$instrument);
// Extract field names and build the new structure, with fieldType and description (or label if not set in the tag)
$requestedFields = [];
if (!empty($annotatedFields[$requestedTag]) && is_array($annotatedFields[$requestedTag])) {
foreach (array_keys($annotatedFields[$requestedTag]) as $fieldName) {
$fieldType = REDCap::getFieldType($fieldName);
$description = trim($annotatedFields[$requestedTag][$fieldName][0], '"');
if (strlen($description) == 0) $description = $this->getFieldLabel($fieldName);
$requestedFields[$fieldName] = [
'type' => $fieldType,
'description' => $this->escape($description)
];
};
};
$requiredFields = [];
if (!empty($annotatedFields[$requiredTag]) && is_array($annotatedFields[$requiredTag])) {
foreach (array_keys($annotatedFields[$requiredTag]) as $fieldName) {
$fieldType = REDCap::getFieldType($fieldName);
$description = trim($annotatedFields[$requiredTag][$fieldName][0], '"');
if (strlen($description) == 0) $description = $this->getFieldLabel($fieldName);
$requiredFields[$fieldName] = [
'type' => $fieldType,
'description' => strip_tags($description)
];
};
};
// Add fields marked as required in the metadata to the requiredFields array
if ($settings['designer-required'] == '1'){
$Proj = new Project($project_id);
// Loop through all fields in the current instrument
$instrumentFields = REDCap::getFieldNames($instrument=$instrument);
foreach ($instrumentFields as $this_field) {
// If the field is required AND it is not already in thr requiredFields array
if ($Proj->metadata[$this_field]['field_req'] == '1' && !array_key_exists($this_field, $requiredFields)){
$fieldType = REDCap::getFieldType($this_field); // Get the type
$description = $this->getFieldLabel($this_field); // Get the label
$requiredFields[$this_field] = [
'type' => $fieldType,
'description' => strip_tags($description)
];
};
};
// Sort requiredFields by the order of the fields in the instrument, using the instrumentFields array from earlier
// This is not necessary unless we are only using fields marked @REQUIRED as they are retrieved in instrument order
$requiredFieldsSorted = [];
foreach ($instrumentFields as $field) {
if (isset($requiredFields[$field])) {
$requiredFieldsSorted[$field] = $requiredFields[$field];
};
};
$requiredFields = $requiredFieldsSorted;
};
// Let's stop if both requestedFields and requiredFields are empty
if (empty($requestedFields) && empty($requiredFields)){
return;
};
// Language defaults
$settings['modal-title'] = $settings['modal-title'] ?? $this->tt('modal-title-text');
$settings['modal-requested-header'] = $settings['modal-requested-header'] ?? $this->tt('modal-requested-header-text');
$settings['modal-required-header'] = $settings['modal-required-header'] ?? $this->tt('modal-required-header-text');
$settings['modal-footer-norequired'] = $settings['modal-footer-norequired'] ?? $this->tt('modal-footer-norequired-text');
$settings['modal-footer-required'] = $settings['modal-footer-required'] ?? $this->tt('modal-footer-required-text');
$settings['requested-label'] = $settings['requested-label'] ?? $this->tt('requested-label-text');
$settings['modal-cancel'] = $settings['modal-cancel'] ?? $this->tt('modal-cancel-text');
$settings['modal-submit'] = $settings['modal-submit'] ?? $this->tt('modal-submit-text');
// Colour defaults
$settings['requested-hlcolour'] = $settings['requested-hlcolour'] ?? $this->tt('requested-hlcolour-hex');
$settings['required-hlcolour'] = $settings['required-hlcolour'] ?? $this->tt('required-hlcolour-hex');
$settings['requested-label-colour'] = $settings['requested-label-colour'] ?? $this->tt('requested-label-colour-hex');
echo "<script>
$('button[name=\"submit-btn-saverecord\"]').attr('onclick','$(this).button(\"disable\");checkReqdFields();return false;');
var requestedFields = " . json_encode($requestedFields) . ";
var requiredFields = " . json_encode($requiredFields) . ";
$.each(requestedFields, function(fieldName, fieldInfo) {
var fieldRow = $('tr#'+fieldName+'-tr');
var fieldType = fieldInfo.type;
var desc = fieldInfo.description;
if (!fieldRow.length) {
delete requestedFields[fieldName];
}
var fieldClass = fieldRow.attr('class');
if (fieldClass && fieldClass.indexOf('@HIDDEN') !== -1) {
delete requestedFields[fieldName];
}
});
$.each(requiredFields, function(fieldName, fieldInfo) {
var fieldRow = $('tr#'+fieldName+'-tr');
var fieldType = fieldInfo.type;
var desc = fieldInfo.description;
if (!fieldRow.length) {
delete requiredFields[fieldName];
}
var fieldClass = fieldRow.attr('class');
if (fieldClass && fieldClass.indexOf('@HIDDEN') !== -1) {
delete requiredFields[fieldName];
}
});
function fieldIsEmpty(fieldName, fieldType){
var fieldIsEmpty = false;
var fieldRow = $('tr#'+fieldName+'-tr');
if (!fieldRow.is(':visible')) {
return fieldIsEmpty;
}
switch (fieldType) {
case 'text':
case 'radio':
case 'truefalse':
case 'yesno':
case 'slider':
case 'file':
case 'signature':
fieldIsEmpty = ($('input[name=\"'+fieldName+'\"]').val()==='');
break;
case 'notes':
fieldIsEmpty = ($('textarea[name=\"'+fieldName+'\"]').val()==='');
break;
case 'dropdown':
case 'sql':
fieldIsEmpty = ($('select[name=\"'+fieldName+'\"]').val()==='');
break;
case 'checkbox':
fieldIsEmpty = (!$('tr#' + fieldName+ '-tr').find('input[type=\"checkbox\"]').is(':checked'));
break;
}
return fieldIsEmpty;
}
function createEmptyReport(fieldArray){
var empties = [];
$.each(fieldArray, function(fieldName, fieldInfo) {
var fieldType = fieldInfo.type;
var fieldDesc = fieldInfo.description;
if (fieldIsEmpty(fieldName, fieldType)) {
empties.push(fieldDesc);
}
});
return empties;
};
function addClassToField(fieldName, className){
var fieldRow = $('tr#'+fieldName+'-tr');
fieldRow.addClass(className);
};
function checkReqdFields(){
$('#requested-list').empty();
$('#required-list').empty();
var emptyRequested = createEmptyReport(requestedFields);
var emptyRequired = createEmptyReport(requiredFields);
if (emptyRequested.length + emptyRequired.length == 0){
dataEntrySubmit();
} else {
if (emptyRequested.length > 0) {
$('#modal-requested-header').show();
// Re-enable the modal's submit button
$('button#confirmSubmit').prop('disabled', false);
// Create a <ul> element
var requestedUl = document.createElement('ul');
// Loop through the descriptions and create <li> elements
emptyRequested.forEach(function(description) {
var li = document.createElement('li');
li.textContent = description;
requestedUl.appendChild(li);
});
document.getElementById('requested-list').appendChild(requestedUl);
} else {
$('#modal-requested-header').hide();
}
if (emptyRequired.length > 0) {
$('#modal-required-header').show();
// Disable modal's submit button
$('button#confirmSubmit').prop('disabled', true);
$('#modal-action').text('". $settings['modal-footer-required'] ."');
// Create a <ul> element
var requiredUl = document.createElement('ul');
// Loop through the descriptions and create <li> elements
emptyRequired.forEach(function(description) {
var li = document.createElement('li');
li.textContent = description;
requiredUl.appendChild(li);
});
document.getElementById('required-list').appendChild(requiredUl);
} else {
$('#modal-required-header').hide();
}
// Show modal
$('#confirmationModal').modal('show');
// Handle the OK button click in the modal
$('button#confirmSubmit').on('click', function() {
// Hide the modal
$('#confirmationModal').modal('hide');
// Call the original function
dataEntrySubmit();
});
// Handle the Cancel button click in the modal
$('#cancelSubmit, .close').on('click', function() {
// Re-enable the submit button
$('#confirmationModal').modal('hide');
$('button[name=\"submit-btn-saverecord\"]').button('enable')
$.each(requestedFields, function(fieldName) {
addClassToField(fieldName, 'em-requested');
});
$.each(requiredFields, function(fieldName) {
addClassToField(fieldName, 'em-required');
});
});
// Re-enable the submit button if the modal is closed without confirmation
$('#confirmationModal').on('hidden.bs.modal', function () {
$('button[name=\"submit-btn-saverecord\"]').button('enable')
$.each(requestedFields, function(fieldName) {
addClassToField(fieldName, 'em-requested');
});
$.each(requiredFields, function(fieldName) {
addClassToField(fieldName, 'em-required');
});
});
}
};
</script>";
if ($settings['highlight']) {
echo "<style>
tr.em-requested .labelrc, tr.em-requested .data {
background: " . $settings['requested-hlcolour'] . ";
}
tr.em-required .labelrc, tr.em-required .data {
background: " . $settings['required-hlcolour'] . ";
}
</style>";
}
echo "<div class='modal fade' id='confirmationModal' tabindex='-1' role='dialog' aria-labelledby='confirmationModalLabel' aria-hidden='true'>
<div class='modal-dialog' role='document'>
<div class='modal-content'>
<div class='modal-header'>
<h5 class='modal-title' id='confirmationModalLabel'>".$settings['modal-title']."</h5>
<button type='button' class='close' data-dismiss='modal' aria-label='Close'>
<span aria-hidden='true'>×</span>
</button>
</div>
<div class='modal-body'>
<p id='modal-requested-header'>".$settings['modal-requested-header']."</p>
<div " . ($settings['highlight'] ? "style='background: " . $settings['requested-hlcolour'] . ";'" : "") . " id='requested-list'></div>
<p id='modal-required-header'>".$settings['modal-required-header']."</p>
<div " . ($settings['highlight'] ? "style='background: " . $settings['required-hlcolour'] . ";'" : "") . " id='required-list'></div>
<p id='modal-action'>".$settings['modal-footer-norequired']."</p>
</div>
<div class='modal-footer'>
<button type='button' class='btn btn-secondary' data-dismiss='modal' id='cancelSubmit'>".$settings['modal-cancel']."</button>
<button type='button' class='btn btn-primary' id='confirmSubmit'>".$settings['modal-submit']."</button>
</div>
</div>
</div>
</div>";
if ($settings['show-requested']) {
echo "<script>
var requestedLabelDiv = '<div class=\"requestedlabel\" aria-label=\"response requested\">" . $settings['requested-label'] . "</div>';
document.addEventListener('DOMContentLoaded', function() {
$.each(requestedFields, function(fieldName) {
var fieldRow = $('tr#'+fieldName+'-tr');
fieldRow.find('label#label-' + fieldName + ' div[data-mlm-field=\"' + fieldName + '\"]').append(requestedLabelDiv);
});
});
</script>
<style>
.requestedlabel {
font-size: 12px;
color: " . $settings['requested-label-colour'] . ";
font-weight: normal;
}
</style>";
}
if ($settings['disable-greenhl']) {
echo "<script>
doGreenHighlight = function() {};
</script>";
}
}
}