-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathscript.php
More file actions
325 lines (289 loc) · 10.3 KB
/
script.php
File metadata and controls
325 lines (289 loc) · 10.3 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
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
<?php
$metadata = $data_entry_trigger_builder->retrieveProjectMetadata($module->getProjectId());
// $metadata = json_decode($data_entry_trigger_builder->retrieveProjectMetadata($module->getProjectId()), true);
//print "<!-- in script.php metadata is an array? " . is_array($metadata) . "-->";
//print "<!-- in script.php metadata is: " . print_r($metadata, true) . "-->";
$instrument_names = REDCap::getInstrumentNames();
?>
<script>
window.redcap_csrf_token = <?= json_encode($module->getCSRFToken()) ?>;
var sourceIsLongitudinal = <?= REDCap::isLongitudinal() ? 'true' : 'false' ?>;
/**
* Code to populate the populate
* the autocomplete fields for the
* source project
*/
<?php if (REDCap::isLongitudinal()): ?>
var sourceEvents = [
<?php
foreach ($metadata["events"] as $event)
{
print "'$event',";
}
?>
];
$("#linkSourceEvent").autocomplete({
source: sourceEvents,
minLength: 0
}).off("focus.detOpen").on("focus.detOpen", function () {
$(this).autocomplete("search", $(this).val());
});
$("#event-select").autocomplete({
source: sourceEvents,
minLength: 0,
appendTo: "#add-field-modal"
}).off("focus.detOpen").on("focus.detOpen", function () {
$(this).autocomplete("search", $(this).val());
});
$("#instr-event-select").autocomplete({
source: sourceEvents,
minLength: 0,
appendTo: "#add-instr-modal"
}).off("focus.detOpen").on("focus.detOpen", function () {
$(this).autocomplete("search", $(this).val());
});
<?php else: ?>
var sourceEvents = [];
<?php endif;?>
var sourceFields = [
<?php
foreach ($metadata["fields"] as $field)
{
print "'$field',";
}
?>
];
$("#linkSource").autocomplete({
source: sourceFields,
minLength: 0
}).off("focus.detOpen").on("focus.detOpen", function () {
$(this).autocomplete("search", $(this).val());
});
$("#field-select").autocomplete({
source: sourceFields,
minLength: 0,
appendTo: "#add-field-modal"
}).off("focus.detOpen").on("focus.detOpen", function () {
$(this).autocomplete("search", $(this).val());
});
var sourceInstr = [
<?php
foreach ($instrument_names as $unique_name => $label)
{
print "'$unique_name',";
}
?>
];
$("#instr-select").autocomplete({
source: sourceInstr,
minLength: 0,
appendTo: "#add-instr-modal"
}).off("focus.detOpen").on("focus.detOpen", function () {
$(this).autocomplete("search", $(this).val());
});
/**
* When user goes to add a field or instrument
* update .table-id item, hidden in each modal,
* to tell them which trigger's table to update.
*
*/
$("body").on("click", ".add-field-btn, .add-instr-btn", function () {
var id = $(this).closest(".trigger-and-data-wrapper").find("table").attr("id");
$(".table-id").val(id);
});
$("body").on("click", ".fa-pencil-alt", function () {
var id = $(this).closest("table").attr("id");
$(".table-id").val(id);
});
/**
Code to delete a trigger
*/
$('body').on('click', '.delete-trigger-btn', function () {
$(this).closest('.trigger-and-data-wrapper').remove();
});
/**
Code to delete a trigger's field/instrument
*/
$("body").on("click", ".delete-trigger-field", function () {
$(this).closest('.trigger-field-row').remove();
});
/**
* Ajax calls to retrieve destination project's fields and instruments
*/
/**
Call to retrieve destination project's fields and instruments when page loads.
Only relevent when DET aleady exists.
*/
$(document).ready(function() {
if ($("#destination-project-select").val() != "")
{
$.ajax({
url: "<?php print $module->getUrl("getDestinationFields.php") ?>",
type: "POST",
dataType: "json",
data: {
pid: $("#destination-project-select").val(),
redcap_csrf_token: window.redcap_csrf_token
},
success: function (data) {
updateAutocompleteItems(data);
$("#dest-field-select").autocomplete({source: destFields, appendTo: "#add-field-modal"});
},
error: function (data, status, error) {
console.log("Returned with status " + status + " - " + error);
}
});
}
});
/**
Call to retrieve destination project's fields and instruments when
destination project changes, and update autcomplete items
*/
$("#destination-project-select").change(function () {
// Reset form by removing all triggers.
$('.trigger-and-data-wrapper').remove();
$.ajax({
url: "<?php print $module->getUrl("getDestinationFields.php") ?>",
type: "POST",
dataType: "json",
data: {
pid: $(this).val(),
},
success: function (data) {
updateAutocompleteItems(data);
$(".dest-fields-autocomplete").val("");
},
error: function (data, status, error) {
console.log("Returned with status " + status + " - " + error);
}
});
// Show the main form are if it's not already visible
$('#main-form').show();
});
/**
* Ajax call to submit form, and validate
*/
$("#det-form").submit(function (event) {
event.preventDefault();
if ($('input[name="redcap_csrf_token"]').length === 0) {
$('<input type="hidden" name="redcap_csrf_token">')
.val(window.redcap_csrf_token)
.appendTo('#det-form');
}
$.ajax({
url: "<?php print $module->getUrl("SubmitForm.php");?>",
type: "POST",
dataType: "json",
data: $("form").serialize(),
success: function (data) {
// var errors = JSON.parse(data); // JORDAN LOOK HERE
var errors = data;
$(document).find('.error-msg').remove();
$(document).find(".error").removeClass("error");
if (errors.success != true)
{
if (errors.linkSourceEvent)
{
addError('linkSourceEvent', errors.linkSourceEvent);
}
if (errors.linkDestEvent)
{
addError('linkDestEvent', errors.linkDestEvent);
}
if (errors.linkSource)
{
addError('linkSource', errors.linkSource);
}
if (errors.linkDest)
{
addError('linkDest', errors.linkDest);
}
if (errors.pipingSourceEvents)
{
addTableErrors(errors.pipingSourceEvents, "pipingSourceEvents");
}
if (errors.pipingSourceFields)
{
addTableErrors(errors.pipingSourceFields, "pipingSourceFields");
}
if (errors.pipingDestEvents)
{
addTableErrors(errors.pipingDestEvents, "pipingDestEvents");
}
if (errors.pipingDestFields)
{
addTableErrors(errors.pipingDestFields, "pipingDestFields");
}
if (errors.setDestEvents)
{
addTableErrors(errors.setDestEvents, "setDestEvents");
}
if (errors.setDestFields)
{
addTableErrors(errors.setDestFields, "setDestFields");
}
if (errors.sourceInstrEvents)
{
addTableErrors(errors.sourceInstrEvents, "sourceInstrEvents");
}
if (errors.sourceInstr)
{
addTableErrors(errors.sourceInstr, "sourceInstr");
}
if (errors.trigger_errors)
{
var triggers = $('.det-trigger');
for (var index in errors.trigger_errors)
{
var item = errors.trigger_errors[index];
var msg = "";
item.forEach(function(m) {
msg += m + "<br/>";
});
$(triggers[index]).find("input").addClass("error")
$(triggers[index]).after("<p class='error-msg'><i>" + msg + "</i></p>")
}
}
}
else
{
alert("Your DET has successfully been created");
}
},
error: function (data, status, error) {
console.log("Returned with status " + status + " - " + error);
}
});
})
/**
* Clear add field modal items, whenever
* user switches back and forth between
* entering a custom value or selecting a field
* for the source.
*/
$('.fa-exchange-alt').click(function () {
$('#source-select').toggle();
$('#field-value').val("");
// $('#source-input').toggle();
$('#source-select').toggleClass('d-none');
$('#source-input').toggleClass('d-none');
$('#field-select').val("");
$('#event-select').val("");
});
/**
* When a user closes a modal, clear the
* form.
*/
$('.close-modal-btn').click(function() {
clearInstrForm();
clearFieldForm();
$('#add-field-btn').text("Add");
$('#add-instr-btn').text("Add");
})
$('#add-trigger-btn').click(function() {
addTrigger();
})
$('#add-field-btn, #add-instr-btn').click(function () {
updateTable(this);
})
</script>