-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathIndiciaConversions.php
More file actions
263 lines (245 loc) · 6.73 KB
/
Copy pathIndiciaConversions.php
File metadata and controls
263 lines (245 loc) · 6.73 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
<?php
namespace IForm;
/**
* Class containing useful conversion functions.
*/
class IndiciaConversions {
/**
* Track if status labels have been translated so we only do it once.
*
* @var bool
*/
private static $statusTermsTranslated = FALSE;
/**
* Record status term mappings.
*
* @var array
*/
private static $statusTerms = [
'V' => 'Accepted',
'R' => 'Not accepted',
'C' => 'Not reviewed',
'I' => 'In progress',
// Deprecated.
'D' => 'Query',
'T' => 'Test record',
];
/**
* Record substatus term mappings.
*
* @var array
*/
private static $substatusTerms = [
'1' => 'correct',
'2' => 'considered correct',
'3' => 'plausible',
'4' => 'unable to verify',
'5' => 'incorrect',
];
/**
* Returns an array of all translated status terms, keyed by code.
*
* @return array
* List of status terms keyed by code.
*/
public static function getTranslatedStatusTerms() {
self::translateStatusTerms();
return array_merge(
self::$statusTerms,
[
'V1' => self::statusToLabel('V', '1', FALSE),
'V2' => self::statusToLabel('V', '2', FALSE),
'C3' => self::statusToLabel('C', '3', FALSE),
'R4' => self::statusToLabel('R', '4', FALSE),
'R5' => self::statusToLabel('R', '5', FALSE),
]
);
}
/**
* Returns the icon HTML for a given status/substatus.
*
* @param string $status
* Record status code.
* @param string $substatus
* Record substatus number.
* @param string $imgPath
* Path to the media/images folder.
*
* @return string
* Icon HTML.
*/
public static function statusToIcons($status, $substatus, $imgPath) {
$r = '';
if (!empty($status)) {
$hint = self::statusToLabel($status, $substatus, NULL);
$images = [];
if ($status === 'V') {
$images[] = 'ok-16px';
}
elseif ($status === 'R') {
$images[] = 'cancel-16px';
}
switch ($substatus) {
case '1':
$images[] = 'ok-16px';
break;
case '2':
break;
case '3':
$images[] = 'quiz-22px';
break;
case '4':
break;
case '5':
$images[] = 'cancel-16px';
break;
}
if ($images) {
foreach ($images as $image) {
$r .= " :: <img width=\"12\" height=\"12\" src=\"{$imgPath}nuvola/$image.png\" title=\"$hint\" alt=\"$hint\"/>";
}
}
}
return $r;
}
/**
* Converts a status and substatus into a readable label.
*
* E.g. "accepted", or "accepted:considered correct".
*
* @param string $status
* Status code from database (e.g. 'C').
* @param int $substatus
* Substatus value from database.
* @param string $query
* Query valid for the record (null, Q or A).
*
* @return string
* Status label text.
*/
public static function statusToLabel($status, $substatus, $query) {
$labels = [];
self::translateStatusTerms();
// Grab the term for the status. We don't need to bother with not reviewed status if
// substatus is plausible.
if (!empty(self::$statusTerms[$status]) && ($status !== 'C' || (int) $substatus !== 3)) {
$labels[] = \lang::get(self::$statusTerms[$status]);
}
elseif ((int) $substatus !== 3) {
$labels[] = \lang::get('Unknown');
}
if ($substatus && !empty(self::$substatusTerms[$substatus])) {
$labels[] = \lang::get(self::$substatusTerms[$substatus]);
}
switch ($query) {
case 'Q':
$labels[] = \lang::get('Queried');
break;
case 'A':
$labels[] = \lang::get('Query answered');
break;
}
return implode('::', $labels);
}
/**
* Ensure a provided value is boolean, not a string representation.
*
* Use when you are uncertain of the type or representation of a value that
* should be boolean (e.g. a query string parameter from $_GET). Empty
* strings are interpreted as NULL.
*
* @param mixed $value
* Provided value, e.g. 't', 'true', TRUE, 'f', 'false', FALSE.
*
* @return null|bool
* Boolean equivalent.
*/
public static function toBool($value) {
if (in_array($value, ['', 'null', 'NULL', NULL], TRUE)) {
return NULL;
}
elseif (in_array($value, ['t', 'f'], TRUE)) {
// Filter_var() doesn't handle 't' & 'f'.
return $value === 't';
}
else {
return filter_var($value, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE);
}
}
/**
* Convert a timestamp into readable format (... ago).
*
* @param int $timestamp
* The date time to convert.
*
* @return string
* The output string.
*/
public static function timestampToTimeAgoString($timestamp) {
$difference = time() - $timestamp;
// Having the full phrase means that it is fully localisable if the
// phrasing is different.
$periods = [
\lang::get("{1} second ago"),
\lang::get("{1} minute ago"),
\lang::get("{1} hour ago"),
\lang::get("Yesterday"),
\lang::get("{1} week ago"),
\lang::get("{1} month ago"),
\lang::get("{1} year ago"),
\lang::get("{1} decade ago")
];
$periodsPlural = [
\lang::get("{1} seconds ago"),
\lang::get("{1} minutes ago"),
\lang::get("{1} hours ago"),
\lang::get("{1} days ago"),
\lang::get("{1} weeks ago"),
\lang::get("{1} months ago"),
\lang::get("{1} years ago"),
\lang::get("{1} decades ago")
];
$lengths = ['60', '60', '24', '7', '4.35', '12', '10'];
for ($j = 0; (($j < 7) && ($difference >= $lengths[$j])); $j++) {
$difference /= $lengths[$j];
}
$difference = round($difference);
if ($difference == 1) {
$text = str_replace('{1}', $difference, $periods[$j]);
}
else {
$text = str_replace('{1}', $difference, $periodsPlural[$j]);
}
return $text;
}
/**
* Transform a size in bytes into a human readable format such as 2GB.
*
* @param int $size
* Size in bytes.
*
* @return string
* Size converted to human readable format.
*/
public static function bytesToReadable($size) {
$units = ['bytes', 'KB', 'MB', 'GB', 'TB', 'PB'];
for ($i = 0; $size >= 1024 && $i < count($units) - 1; $i++) {
$size /= 1024;
}
return round($size, 2) . ' ' . $units[$i];
}
/**
* Convert the list of status/substatus terms and into a translated version.
*/
private static function translateStatusTerms() {
if (!self::$statusTermsTranslated) {
foreach (self::$statusTerms as &$term) {
$term = \lang::get($term);
}
foreach (self::$substatusTerms as &$term) {
$term = \lang::get($term);
}
self::$statusTermsTranslated = TRUE;
}
}
}