This repository has been archived by the owner on Feb 19, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStringUtils.php
137 lines (122 loc) · 5.36 KB
/
StringUtils.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
<?php
namespace uzgent\StringUtils;
// Declare your module class, which must extend AbstractExternalModule
use REDCap;
class StringUtils extends \ExternalModules\AbstractExternalModule {
const annotation = ["@TOLOWER", "@TOUPPER", "@SUBSTR", "@LTRIM", "@RTRIM", "@TRIM", "@STRLEN", "@INDEXOF"];
public function redcap_data_entry_form($project_id, $record, $instrument, $event_id, $group_id, $repeat_instance)
{
if ($record === null) return;
$listeners = [];
$warnings = [];
$fieldNames = $this->getFieldNames($project_id);
foreach ($this->getMetadata($project_id) as $field) {
$field_annotation = $field['field_annotation'];
foreach (self::annotation as $annotation)
{
if (strpos($field_annotation, $annotation) !== false) {
list($warnings, $listeners) = $this->getListenersFromAnot($field_annotation, $field, $annotation, $listeners, $warnings, $fieldNames);
}
}
}
$this->showWarnings($listeners, $fieldNames, $warnings);
$this->writeListeners($listeners);
}
public function redcap_survey_page($project_id, $record, $instrument, $event_id, $group_id, $repeat_instance)
{
$this->redcap_data_entry_form($project_id, $record, $instrument, $event_id, $group_id, $repeat_instance);
}
/**
* @param $project_id
* @param array $fieldNames
* @return array
*/
public function getFieldNames($project_id)
{
$fieldNames = [];
foreach ($this->getMetadata($project_id) as $field) {
$fieldNames [] = $field["field_name"];
}
return $fieldNames;
}
/**
* @param array $listeners
* @param array $fieldNames
* @param array $warnings
*/
public function showWarnings(array $listeners, array $fieldNames, array $warnings)
{
foreach ($listeners as $sourceField => $code) {
if (!in_array($sourceField, $fieldNames)) {
echo '<div class="alert">The following field is used by cannot be found: ' . $sourceField . '</div>';
}
}
foreach ($warnings as $warning) {
echo '<div class="alert">' . $warning . '</div>';
}
}
/**
* @param array $listeners
*/
public function writeListeners(array $listeners)
{
echo '<script>';
foreach ($listeners as $sourceField => $code) {
echo '$("input[name=\'' . $sourceField . '\']").keyup(function(){';
echo $code;
echo '} );';
}
echo '</script>';
}
/**
* @param $field_annotation
* @param $field
* @param $annotation
* @param array $listeners
* @param array $warnings
* @param array $fieldNames
* @return array
*/
public function getListenersFromAnot($field_annotation, $field, $annotation, array $listeners, array $warnings, array $fieldNames)
{
$selectedPieces = explode("=", $field_annotation);
$sourceField = explode(" ", $selectedPieces[1])[0];
$destinationField = $field["field_name"];
switch ($annotation) {
case "@TOLOWER":
$listeners[$sourceField] .= '$("input[name=\'' . $destinationField . '\']").val($("input[name=\'' . $sourceField . '\']").val().toLowerCase());';
break;
case "@TOUPPER":
$listeners[$sourceField] .= '$("input[name=\'' . $destinationField . '\']").val($("input[name=\'' . $sourceField . '\']").val().toUpperCase());';
break;
case "@LTRIM":
$listeners[$sourceField] .= '$("input[name=\'' . $destinationField . '\']").val($("input[name=\'' . $sourceField . '\']").val().trimLeft());';
break;
case "@RTRIM":
$listeners[$sourceField] .= '$("input[name=\'' . $destinationField . '\']").val($("input[name=\'' . $sourceField . '\']").val().trimRight());';
break;
case "@TRIM":
$listeners[$sourceField] .= '$("input[name=\'' . $destinationField . '\']").val($("input[name=\'' . $sourceField . '\']").val().trim());';
break;
case "@STRLEN":
$listeners[$sourceField] .= '$("input[name=\'' . $destinationField . '\']").val($("input[name=\'' . $sourceField . '\']").val().length);';
break;
case "@SUBSTR":
$selectedFields = explode(",", $sourceField);
if (!is_numeric($selectedFields[1])) {
$warnings [] = 'For field: ' . $destinationField . ' ' . $selectedFields[1] . ' is not numeric';
}
if (!is_numeric($selectedFields[2])) {
$warnings [] = 'For field: ' . $destinationField . ' ' . $selectedFields[2] . ' is not numeric';
}
if (!in_array($selectedFields[0], $fieldNames)) {
$warnings [] = 'For field: ' . $destinationField . ' ' . $selectedFields[0] . ' is not fields.';
}
$listeners[$selectedFields[0]] .= '$("input[name=\'' . $destinationField . '\']").val($("input[name=\'' . $selectedFields[0] . '\']").val().substr(' . $selectedFields[1] . ', ' . $selectedFields[2] . '));';
break;
default:
break;
}
return [$warnings, $listeners];
}
}