This repository was archived by the owner on Oct 1, 2025. It is now read-only.
forked from smartinkc/Multilingual
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMultilingual.php
More file actions
951 lines (819 loc) · 36.6 KB
/
Multilingual.php
File metadata and controls
951 lines (819 loc) · 36.6 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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
<?php
namespace CMH\Multilingual;
use ExternalModules\AbstractExternalModule;
use ExternalModules\ExternalModules;
use \Piping as Piping;
use REDCap;
class Multilingual extends AbstractExternalModule
{
public $translate_answer_field_types = [
"select",
"radio",
"checkbox",
"yesno",
"truefalse",
"slider"
];
function redcap_survey_page($project_id, $record, $instrument, $event_id, $group_id, $survey_hash, $response_id, $repeat_instance){
$api_endpoint = $this->getProjectSetting('use-api-endpoint', $project_id);
// add multilingual_survey.js after making text replacements
$redcap_survey_javascript = file_get_contents($this->getModulePath() . 'js/multilingual_survey.js');
$redcap_survey_javascript = str_replace('REDCAP_PDF_URL', ($this->getProjectSetting('multilingual-econsent', $project_id) ? $this->getUrl("multilingualPDF.php", true, ($api_endpoint == true ? true : false)) : 'false') . '&id=' . $record . '&form=' . $instrument . '&event_id=' . $event_id . '&instance=' . $repeat_instance, $redcap_survey_javascript);
$redcap_survey_javascript = str_replace('APP_PATH_IMAGES', APP_PATH_IMAGES, $redcap_survey_javascript);
$redcap_survey_javascript = str_replace('REDCAP_INSTRUMENT_NAME', $instrument, $redcap_survey_javascript);
$redcap_survey_javascript = str_replace('MULTILINGUAL_RECORD_ID', $record, $redcap_survey_javascript);
$redcap_survey_javascript = str_replace('REDCAP_LANGUAGE_VARIABLE', $this->languageVariable($project_id), $redcap_survey_javascript);
$redcap_survey_javascript = str_replace('REDCAP_AJAX_URL', $this->getUrl("index.php", true, ($api_endpoint == true ? true : false)), $redcap_survey_javascript);
$redcap_survey_javascript = str_replace('MULTILINGUAL_LANGUAGE_SELECTED_URL', $this->getUrl('languageSelected.php'), $redcap_survey_javascript);
$redcap_survey_javascript = str_replace('MULTILINGUAL_SURVEY_EVENT', $event_id, $redcap_survey_javascript);
// see if pdf translation is configured or not -- set variable in multilingual_survey.js
if ($this->getProjectSetting("translate_pdfs_instruments")) {
$redcap_survey_javascript = str_replace('MULTILINGUAL_PDF_TRANSLATION_ENABLED', 'true', $redcap_survey_javascript);
} else {
$redcap_survey_javascript = str_replace('MULTILINGUAL_PDF_TRANSLATION_ENABLED', 'false', $redcap_survey_javascript);
}
echo "<script type='text/javascript'>$redcap_survey_javascript</script>";
echo '<link rel="stylesheet" type="text/css" href="' . $this->getUrl('css/multilingual.css', true, $api_endpoint == true) . '">';
}
function redcap_survey_complete($project_id, $record, $instrument, $event_id, $group_id, $survey_hash, $response_id, $repeat_instance){
$api_endpoint = $this->getProjectSetting('use-api-endpoint', $project_id);
// Update and add multilingual_survey_complete
echo '<script type="text/javascript">' . str_replace('REDCAP_PDF_URL', ($this->getProjectSetting('multilingual-econsent', $project_id) ? $this->getUrl("multilingualPDF.php", true, ($api_endpoint == true ? true : false)) : 'false') . '&id=' . $record . '&form=' . $instrument . '&event_id=' . $event_id . '&instance=' . $repeat_instance , str_replace('REDCAP_LANGUAGE_VARIABLE', $this->languageVariable($project_id), str_replace('REDCAP_AJAX_URL', $this->getUrl("index.php", true, ($api_endpoint == true ? true : false)), file_get_contents($this->getModulePath() . 'js/multilingual_survey_complete.js')))) . '</script>';
}
function redcap_data_entry_form($project_id, $record, $instrument){
echo '<script type="text/javascript">' . str_replace('APP_PATH_IMAGES', APP_PATH_IMAGES, str_replace('REDCAP_LANGUAGE_VARIABLE', $this->languageVariable($project_id), str_replace('REDCAP_AJAX_URL', $this->getUrl("index.php", true), file_get_contents($this->getModulePath() . 'js/multilingual.js')))) . '</script>';
echo '<link rel="stylesheet" type="text/css" href="' . $this->getUrl('css/multilingual.css') . '">';
}
function redcap_pdf($project_id, $metadata, $data, $instrument, $record, $event_id, $instance) {
// delay execution of this module to allow multi-consent-signature module to do it's thing
if ($this->delayModuleExecution()) {
return;
}
// get which languages were selected for which instruments by the user as they were taking surveys
$user_langs = $this->getUserSelectedLanguages($record);
if (empty($user_langs)) {
// user never selected a lang, do no translations
return array('metadata'=>$metadata, 'data'=>$data);
}
// log to project event table
$action_description = "Translating Generated PDF";
$changes_made = "The Multilingual module will translate field labels (for each instrument) based on language selections the user made in survey(s).";
\REDCap::logEvent($action_description, $changes_made, null, $record_id, $event_id);
// translate metadata using user selected languages
global $Proj;
$translated_metadata = $this->translatePDF($metadata, $user_langs);
return array('metadata'=>$translated_metadata, 'data'=>$data);
}
function redcap_every_page_top($project_id){
$api_endpoint = $this->getProjectSetting('use-api-endpoint', $project_id);
$at_survey_settings = strpos($_SERVER['REQUEST_URI'], 'Surveys/edit_info.php') !== false || strpos($_SERVER['REQUEST_URI'], 'Surveys/create_survey.php') !== false;
//$user_rights = REDCap::getUserRights();
//echo json_encode($user_rights);
if(strpos($_SERVER['REQUEST_URI'], 'online_designer.php') !== false && isset($_GET['page'])){
echo '<link rel="stylesheet" type="text/css" href="' . $this->getUrl('css/multilingual.css') . '">';
echo '<script type="text/javascript">' . str_replace('REDCAP_LANGUAGE_VARIABLE', $this->languageVariable($project_id), str_replace('REDCAP_AJAX_URL', $this->getUrl("index.php", true), file_get_contents($this->getModulePath() . 'js/multilingual_setup.js'))) . '</script>';
}
elseif(strpos($_SERVER['REQUEST_URI'], 'DataExport/index.php') !== false){
echo '<script type="text/javascript">' . str_replace('REDCAP_LANGUAGE_VARIABLE', $this->languageVariable($project_id), str_replace('REDCAP_AJAX_URL', $this->getUrl("index.php", true), file_get_contents($this->getModulePath() . 'js/multilingual_export.js'))) . '</script>';
}
elseif((isset($_GET['__return']) && $_GET['__return'] == 1) or (isset($_GET['s']) && !isset($_GET['__page__']))){
$instrument = $_GET['page'];
echo '<script type="text/javascript">' .
str_replace('APP_PATH_IMAGES', APP_PATH_IMAGES,
str_replace('REDCAP_LANGUAGE_VARIABLE', $this->languageVariable($project_id),
str_replace('REDCAP_INSTRUMENT_NAME', $instrument,
str_replace('REDCAP_AJAX_URL', $this->getUrl("index.php", true, ($api_endpoint == true ? true : false)),
file_get_contents($this->getModulePath() . 'js/multilingual_survey_return.js'))))) . '</script>';
echo '<link rel="stylesheet" type="text/css" href="' . $this->getUrl('css/multilingual.css', true, ($api_endpoint == true ? true : false)) . '">';
}
elseif(strpos($_SERVER['REQUEST_URI'], 'DataEntry/index.php') !== false || strpos($_SERVER['REQUEST_URI'], 'DataEntry/record_home.php') !== false){
echo '<link rel="stylesheet" type="text/css" href="' . $this->getUrl('css/multilingual.css') . '">';
echo '<script type="text/javascript">' . str_replace('PDF_URL', $this->getUrl("multilingualPDF.php", true), str_replace('APP_PATH_IMAGES', APP_PATH_IMAGES, str_replace('REDCAP_LANGUAGE_VARIABLE', $this->languageVariable($project_id), str_replace('REDCAP_AJAX_URL', $this->getUrl("index.php", true), file_get_contents($this->getModulePath() . 'js/multilingual_pdf.js'))))) . '</script>';
}
elseif($at_survey_settings && isset($_GET['page'])){
$index_url = $this->getUrl("index.php", true, ($api_endpoint == true ? true : false));
$language_var = $this->languageVariable($project_id);
$stylesheet = '<link rel="stylesheet" type="text/css" href="' . $this->getUrl('css/multilingual.css') . '">';
$ml_survey_settings_js = '<script type="text/javascript">' . file_get_contents($this->getUrl('js/multilingual_survey_settings.js')) . '</script>';
$ml_survey_settings_js = str_replace('REDCAP_AJAX_URL', $index_url, $ml_survey_settings_js);
$ml_survey_settings_js = str_replace('REDCAP_LANGUAGE_VARIABLE', $language_var, $ml_survey_settings_js);
echo $stylesheet;
echo $ml_survey_settings_js;
}
}
/**
* @param $projectId
* @return string
*/
private function getMetaDataTableName($projectId){
global $conn;
if($this->getProjectSetting('use-drafted-changes', $projectId)){
$query = "select draft_mode from redcap_projects where project_id = " . mysqli_real_escape_string($conn, $data['project_id']);
if($result = mysqli_query($conn, $query)){
if($row = mysqli_fetch_array($result)){
$draftMode = $row["draft_mode"];
return "redcap_metadata".($draftMode == 1?"_temp":"");
}else{
error_log("Multilingual: no row to determine draft_mode");
}
mysqli_free_result($result);
}else{
error_log("Multilingual: no result to determine draft_mode");
}
}
return "redcap_metadata";
}
public function languageVariable($project_id){
$langVar = $this->getProjectSetting('languages_variable', $project_id);
if($langVar == ''){
$langVar = 'languages';
}
return $langVar;
}
public function getSurveySettings($data) {
$instruments = $this->getProjectSetting('instruments');
if (!empty($instruments)) {
$instruments = json_decode($instruments);
$name = htmlspecialchars($data['instrument']);
$instrument = $instruments->$name;
} else {
$instruments = new \stdClass();
}
header('Content-Type: application/json');
echo json_encode($instrument);
}
public function saveSurveySettings($all_data) {
$instruments = $this->getProjectSetting('instruments');
if (empty($instruments)) {
$instruments = new \stdClass();
} else {
$instruments = json_decode($instruments);
}
foreach ($all_data as $data) {
$instrument = htmlspecialchars($data['instrument']);
$lang = htmlspecialchars($data['language']);
if (empty($instruments->$instrument)) {
$instruments->$instrument = new \stdClass();
}
if (empty($instruments->$instrument->$lang)) {
$instruments->$instrument->$lang = new \stdClass();
}
foreach ($data['collections'] as $coll_name => $coll) {
$instruments->$instrument->$lang->$coll_name = new \stdClass();
// add each setting to collection after encoding HTML
foreach ($coll as $sname => $setting) {
$instruments->$instrument->$lang->$coll_name->$sname = htmlspecialchars($setting);
}
}
}
$this->setProjectSetting('instruments', json_encode($instruments));
}
public function getSettings($data){
$response = $this->getProjectSettings($data['project_id']);
foreach($response AS $key => $values){
if($key == 'button-width'){
if(substr($response[$key], -2) != 'px'){
$response[$key] = '100px';
}
elseif(intval(str_replace('px', '', $response[$key])) < 1){
$response[$key] = '100px';
}
}
elseif($key == 'languages_variable' && $response[$key] == null){
$response[$key] = 'languages';
}
}
header('Content-Type: application/json');
echo json_encode($response);
}
public function getAnswers($data){
global $conn;
$data['project_id'] = mysqli_real_escape_string($conn, $data['project_id']);
$data['field_name'] = mysqli_real_escape_string($conn, $data['field_name']);
$metaDataTableName = $this->getMetaDataTableName($data['project_id']);
if($data['matrix'] == 1){
$query = "SELECT element_enum, element_type, element_validation_type, element_validation_min, element_validation_max FROM $metaDataTableName
WHERE project_id = " . intval($data['project_id']) . "
AND grid_name LIKE '" . $data['field_name'] . "'
LIMIT 1";
}
else{
$query = "SELECT element_enum, element_type, element_validation_type FROM $metaDataTableName
WHERE project_id = " . intval($data['project_id']) . "
AND field_name LIKE '" . $data['field_name'] . "'";
}
$result = mysqli_query($conn, $query);
$row = mysqli_fetch_array($result);
if(strpos(' \n ', $row['element_enum']) !== false){
$tmp = explode(' \n ', $row['element_enum']);
}
else{
$tmp = explode('\n', $row['element_enum']);
}
foreach($tmp AS $key => $value){
$tmp2 = explode(',', $value);
$response[trim($tmp2[0])] = trim($tmp2[1]);
}
if($row['element_type'] == 'text' && (strpos($row['element_validation_type'], 'date') !== false || strpos($row['element_validation_type'], 'time') !== false)){
$response = null;
$response['0'] = 'Answer';
}
elseif($row['element_type'] == 'file' && strpos($row['element_validation_type'], 'signature') !== false){
$response = null;
$response['0'] = 'Answer';
}
elseif($row['element_type'] == 'file' && $row['element_validation_type'] == null){
$response = null;
$response['0'] = 'Answer';
}
elseif($row['element_type'] == 'calc'){
$response = null;
$response[""] = "";
}
elseif($row['element_type'] == 'yesno'){
$response = null;
$response['0'] = "No";
$response['1'] = "Yes";
}
elseif($row['element_type'] == 'truefalse'){
$response = null;
$response['0'] = "False";
$response['1'] = "True";
}
elseif($row['element_type'] == 'slider'){
$response = explode('|', array_keys($response)[0]);
$response = array_map('trim',$response);
if (count($response) == 3){$keys = array('0', '50', '100');}
elseif(count($response) == 2){$keys = array('0', '100');}
elseif(count($response) == 1){$keys = array('0');}
else{$keys = array();}
$response = array_combine($keys, $response);
}
header('Content-Type: application/json');
echo json_encode($response);
}
public function getTranslations($data, $projectSettings){
global $conn;
$layout_set = 0;
$data['project_id'] = mysqli_real_escape_string($conn, $data['project_id']);
$data['page'] = mysqli_real_escape_string($conn, $data['page']);
$metaDataTableName = $this->getMetaDataTableName($data['project_id']);
$query = "SELECT field_name, element_type, misc, grid_name, element_validation_type, element_validation_min, element_validation_max, element_label FROM $metaDataTableName
WHERE project_id = " . intval($data['project_id'])
. ($data['page'] !='' ? " AND (form_name LIKE '" . $data['page'] . "' OR field_name LIKE 'survey_text_" . $data['page'] . "')" : '');
$result = mysqli_query($conn, $query);
while($row = mysqli_fetch_array($result)){
//default questions
$response['defaults'][$row['field_name']] = strip_tags($row['element_label']. '<br>');
//$misc = explode("@", $row['misc']);
$misc = str_getcsv($row['misc'], '@');
//$response['test'] = $misc;
$response['all'][$row['field_name']] = $misc;
foreach($misc AS $key => $value){
//replace ___ with @
$value = str_replace('___', '@', $value);
//questions
if(strpos($value, 'p1000lang') !== false){
$value = trim(str_replace('p1000lang', '', $value));
$value = json_decode($value, true);
foreach($value AS $key2 => $trans){
if($key2 == $data['lang']){
$response['questions'][$row['field_name']]['text'] = Piping::replaceVariablesInLabel($trans, ($data['record_id'] ? $data['record_id'] : '0'), $data['event_id']);
if(strpos($row['element_validation_type'], 'date') !== false){
$response['questions'][$row['field_name']]['type'] = 'date';
}
elseif(strpos($row['element_validation_type'], 'time') !== false){
$response['questions'][$row['field_name']]['type'] = 'time';
}
else{
$response['questions'][$row['field_name']]['type'] = $row['element_type'];
}
$response['questions'][$row['field_name']]['matrix'] = $row['grid_name'];
//layout
if($layout_set == 0){
if(\CMH\Multilingual\Multilingual::is_arabic($trans) === true){
$response['layout'] = 'rtl';
}
else{
$response['layout'] = 'ltr';
}
$layout_set = 1;
}
}
}
}
//answers
elseif(strpos($value, 'p1000answers') !== false){
$value = trim(str_replace('p1000answers', '', $value));
$value = json_decode($value, true);
foreach($value AS $key2 => $trans){
if($key2 == $data['lang']){
foreach($trans AS $key3 => $newTrans){
if($row['element_type'] == 'select'){
$trans[$key3] = strip_tags(Piping::replaceVariablesInLabel($newTrans, ($data['record_id'] ? $data['record_id'] : '0'), $data['event_id']), '<br>');
}
else{
$trans[$key3] = Piping::replaceVariablesInLabel($newTrans, ($data['record_id'] ? $data['record_id'] : '0'), $data['event_id']);
}
}
$response['answers'][$row['field_name']]['text'] = $trans;
if(strpos($row['element_validation_type'], 'date') !== false){
$response['answers'][$row['field_name']]['type'] = 'date';
}
elseif(strpos($row['element_validation_type'], 'time') !== false){
$response['answers'][$row['field_name']]['type'] = 'time';
}
elseif(strpos($row['element_validation_type'], 'signature') !== false){
$response['answers'][$row['field_name']]['type'] = 'signature';
}
else{
$response['answers'][$row['field_name']]['type'] = $row['element_type'];
}
$response['answers'][$row['field_name']]['matrix'] = $row['grid_name'];
}
}
}
//errors
elseif(strpos($value, 'p1000errors') !== false){
$value = trim(str_replace('p1000errors', '', $value));
$value = json_decode($value, true);
foreach($value AS $key2 => $trans){
if($key2 == $data['lang']){
$response['errors'][$row['field_name']]['text'] = $trans;
if(strpos($row['element_validation_type'], 'date') !== false){
$response['errors'][$row['field_name']]['type'] = 'date';
}
else{
$response['errors'][$row['field_name']]['type'] = $row['element_type'];
}
$response['errors'][$row['field_name']]['matrix'] = $row['grid_name'];
}
}
}
//field notes
elseif(strpos($value, 'p1000notes') !== false){
$value = str_replace('p1000notes', '', $value);
$value = json_decode($value, true);
foreach($value AS $key2 => $trans){
if($key2 == $data['lang']){
$response['notes'][$row['field_name']]['text'] = Piping::replaceVariablesInLabel($trans, ($data['record_id'] ? $data['record_id'] : '0'), $data['event_id']);
}
}
}
//survey tranlations
elseif(strpos($value, 'p1000surveytext') !== false){
$value = trim(str_replace('p1000surveytext', '', $value));
$value = json_decode($value, true);
foreach($value AS $key2 => $trans){
if($key2 == $data['lang']){
foreach($trans AS $survey_id => $survey_text){
$response['surveytext'][$survey_id] = $survey_text;
}
}
}
}
}
// if error message is not set
if(!isset($response['errors'][$row['field_name']])){
// if it is a text field with validation
if($row['element_type'] == 'text' && !empty($row['element_validation_type'])){
// Make array of error messages from project settings if not already made
if (!isset($defaultError)){
// make array of default error prompts
$defaultError = array();
$defaultError = array_fill_keys($projectSettings['validation'], NULL);
foreach($projectSettings['validation'] AS $key => $valid_type){
$defaultError[$valid_type] = array_combine($projectSettings['lang'][$key], $projectSettings['error'][$key]);
}
}
// If the text field's validation matches with any defined default error messages, use the default error messages
if (array_key_exists($row['element_validation_type'], $defaultError)){
if (array_key_exists($data['lang'], $defaultError[$row['element_validation_type']])){
// Check if the variable contains "[validation_range]", if so, pipe it with the actual range.
if (strpos($defaultError[$row['element_validation_type']][$data['lang']], "[validation_range]") !== false){
if (empty($row['element_validation_min']) && empty($row['element_validation_max'])){
$validationRange = '';
} else {
$validationRange = '('.$row['element_validation_min'].' - '.$row['element_validation_max'].')';
}
$response['errors'][$row['field_name']]['text'] = str_replace("[validation_range]", $validationRange, $defaultError[$row['element_validation_type']][$data['lang']]);
} else {
$response['errors'][$row['field_name']]['text'] = $defaultError[$row['element_validation_type']][$data['lang']];
}
}
}
}
}
}
// override
$instruments = $this->getProjectSetting('instruments');
if (!empty($instruments)) {
$instruments = json_decode($instruments);
$form_name = $data['page'];
$this_lang = $data['lang'];
$simple_settings = $instruments->$form_name->$this_lang;
if (!empty($simple_settings)) {
$general_settings = $simple_settings->survey_settings;
if (!empty($general_settings)) {
if (!empty($general_settings->title) || $general_settings->title == "")
$response['surveytext']['surveytitle'] = html_entity_decode($general_settings->title);
if (!empty($general_settings->instructions) || $general_settings->instructions == "")
$response['surveytext']['surveyinstructions'] = html_entity_decode($general_settings->instructions);
if (!empty($general_settings->acknowledgement) || $general_settings->acknowledgement == "")
$response['surveytext']['surveyacknowledgment'] = html_entity_decode($general_settings->acknowledgement);
}
}
}
//update language field
$response['exist'] = $this->updateLangVar($data);
$response['table'] = $metaDataTableName;
header('Content-Type: application/json');
echo json_encode($response);
}
public function getRecordVar($data){
global $conn;
$data['project_id'] = mysqli_real_escape_string($conn, $data['project_id']);
$metaDataTableName = $this->getMetaDataTableName($data['project_id']);
$query = "SELECT field_name FROM $metaDataTableName where project_id = " . intval($data['project_id']) . " ORDER BY field_order LIMIT 1";
$result = mysqli_query($conn, $query);
$row = mysqli_fetch_array($result);
return $row['field_name'];
}
public function getSavedLang($data){
if($data['record_id'] != ''){
$langVar = $this->getProjectSetting('languages_variable', $data['project_id']);
$tmp = json_decode(REDCap::getData($data['project_id'], 'json', array($data['record_id']), array($langVar)),true);
if(!empty($tmp)){
header('Content-Type: application/json');
echo json_encode($tmp[0][$langVar]);
}
else{
return null;
}
}
else{
return null;
}
}
public function updateLangVar($data){
if($data['record_id'] != ''){
$exist = json_decode(\REDCap::getData($data['project_id'], 'json', $data['record_id']), true);
if(!empty($exist)){
$recordVar = $this->getRecordVar($data);
$langVar = $this->getProjectSetting('languages_variable', $data['project_id']);
$t = array($recordVar => $data['record_id'], ($langVar != null ? $langVar : 'languages') => $data['lang_id'], 'redcap_event_name' => $this->getEventName($data['event_id']));
$json_data = json_encode(array($t));
$tmp = \REDCap::saveData($data['project_id'], 'json', $json_data, 'normal');
return $tmp;
}
}
}
public function getEventName($event_id){
global $conn;
$query = "SELECT descrip, arm_id FROM redcap_events_metadata WHERE event_id = " . intval($event_id);
$result = mysqli_query($conn, $query);
$row = mysqli_fetch_array($result);
$event_name = strtolower(str_replace(" ", "_", $row['descrip']));
$query = "SELECT arm_name FROM redcap_events_arms WHERE arm_id = " . intval($row['arm_id']);
$result = mysqli_query($conn, $query);
$row = mysqli_fetch_array($result);
$event_name .= '_' . strtolower(str_replace(" ", "_", $row['arm_name']));
return $event_name;
}
//copied from php.net
public function uniord($u) {
// i just copied this function fron the php.net comments, but it should work fine!
$k = mb_convert_encoding($u, 'UCS-2LE', 'UTF-8');
$k1 = ord(substr($k, 0, 1));
$k2 = ord(substr($k, 1, 1));
return $k2 * 256 + $k1;
}
//copied from stackoverflow
public function is_arabic($str) {
if(mb_detect_encoding($str) !== 'UTF-8') {
$str = mb_convert_encoding($str,mb_detect_encoding($str),'UTF-8');
}
/*
$str = str_split($str); <- this function is not mb safe, it splits by bytes, not characters. we cannot use it
$str = preg_split('//u',$str); <- this function woulrd probably work fine but there was a bug reported in some php version so it pslits by bytes and not chars as well
*/
preg_match_all('/.|\n/u', $str, $matches);
$chars = $matches[0];
$arabic_count = 0;
$latin_count = 0;
$total_count = 0;
foreach($chars as $char) {
//$pos = ord($char); we cant use that, its not binary safe
$pos = \CMH\Multilingual\Multilingual::uniord($char);
//echo $char ." --> ".$pos.PHP_EOL;
if($pos >= 1536 && $pos <= 1791) {
$arabic_count++;
} else if($pos > 123 && $pos < 123) {
$latin_count++;
}
$total_count++;
}
if(($arabic_count/$total_count) > 0.6) {
// 60% arabic chars, its probably arabic
return true;
}
return false;
}
public function getLanguages($project_id){
$langVar = $this->languageVariable($project_id);
if(!$langVar){
$langVar = 'languages';
}
$q = "SELECT element_enum FROM redcap_metadata WHERE project_id = " . intval($project_id) . " AND field_name = '" . $langVar . "'";
$query = db_query($q);
$row = db_fetch_assoc($query);
$tmp = explode(' \n ', $row['element_enum']);
foreach($tmp AS $key => $value){
$tmp2 = explode(',', $value);
$response[trim($tmp2[0])] = trim($tmp2[1]);
}
return $response;
}
public function getData($project_id = NULL, $record = NULL){
if($project_id == NULL || $record == NULL)
return;
$q = "SELECT record, event_id, field_name, `value` FROM redcap_data
WHERE project_id = " . intval($project_id) .
" AND record = '" . $record . "'";
$query = db_query($q);
while($row = db_fetch_assoc($query)){
$response[$row['record']][$row['event_id']][$row['field_name']] = $row['value'];
}
return $response;
}
public function getMetaData($project_id = NULL, $form = NULL){
if($project_id == NULL)
return;
$q = "SELECT * FROM redcap_metadata
WHERE project_id = " . intval($project_id)
. ($form ? " AND form_name = '" . $form . "'" : "") .
" ORDER BY field_order";
$query = db_query($q);
while($row = db_fetch_assoc($query)){
$response[] = $row;
}
return $response;
}
public function exportData($pid, $lang){
global $conn;
$langVar = $this->getProjectSetting('languages_variable', $pid);
if($langVar == ''){
$langVar = 'languages';
}
ini_set('memory_limit','100M');
set_time_limit(0);
$lang = mysqli_real_escape_string($conn, $lang);
$pid = mysqli_real_escape_string($conn, $pid);
$metaDataTableName = $this->getMetaDataTableName($pid);
//language
$query = "SELECT element_enum, element_type, element_validation_type FROM $metaDataTableName
WHERE project_id = " . intval($pid) . "
AND field_name LIKE '" . $langVar . "'";
$result = mysqli_query($conn, $query);
$row = mysqli_fetch_array($result);
$tmp = explode(' \n ', $row['element_enum']);
foreach($tmp AS $key => $value){
$tmp2 = explode(',', $value);
if($tmp2[0] == $lang){
$response['language'] = trim($tmp2[1]);
break;
}
}
//translations
$query = "SELECT field_name, element_type, misc, grid_name, element_validation_type, element_label FROM $metaDataTableName
WHERE project_id = " . intval($pid) . " AND field_name NOT LIKE 'survey_text%' ORDER BY field_order";
$result = mysqli_query($conn, $query);
while($row = mysqli_fetch_array($result)){
$misc = explode('@', $row['misc']);
foreach($misc AS $key => $value){
//replace ___ with @
$value = str_replace('___', '@', $value);
//questions
if(strpos($value, 'p1000lang') !== false){
$value = trim(str_replace('p1000lang', '', $value));
$value = json_decode($value, true);
foreach($value AS $key2 => $trans){
if($key2 == $response['language']){
$response['questions'][$row['field_name']]['text'] = $trans;
if(strpos($row['element_validation_type'], 'date') !== false){
$response['questions'][$row['field_name']]['type'] = 'date';
}
else{
$response['questions'][$row['field_name']]['type'] = $row['element_type'];
}
$response['questions'][$row['field_name']]['matrix'] = $row['grid_name'];
}
}
}
//answers
elseif(strpos($value, 'p1000answers') !== false){
$value = trim(str_replace('p1000answers', '', $value));
$value = json_decode($value, true);
foreach($value AS $key2 => $trans){
if($key2 == $response['language']){
$response['answers'][$row['field_name']]['text'] = $trans;
if(strpos($row['element_validation_type'], 'date') !== false){
$response['answers'][$row['field_name']]['type'] = 'date';
}
elseif(strpos($row['element_validation_type'], 'signature') !== false){
$response['answers'][$row['field_name']]['type'] = 'signature';
}
else{
$response['answers'][$row['field_name']]['type'] = $row['element_type'];
}
$response['answers'][$row['field_name']]['matrix'] = $row['grid_name'];
}
}
}
}
//non translated fields
if(!isset($response['questions'][$row['field_name']])){
$response['questions'][$row['field_name']]['text'] = $row['element_label'];
$response['questions'][$row['field_name']]['type'] = 'text';
}
}
//header
//$data = '"record_id",';
foreach($response['questions'] AS $field_name => $values){
if($values['type'] == 'checkbox'){
foreach($response['answers'][$field_name]['text'] AS $key => $text){
$data .= '"' . strip_tags($values['text']) . ': ' . $text . '",';
}
}
else{
$data .= '"' . strip_tags($values['text']) . '",';
}
}
$data .= "\r\n";
//data
$query = "SELECT record, field_name, instance, value FROM redcap_data WHERE project_id = " . $pid . " ORDER BY record";
$result = mysqli_query($conn, $query);
while($row = mysqli_fetch_array($result)){
if($response['questions'][$row['field_name']]['type'] == 'checkbox'){
$myData[$row['record']][($row['instance'] == null ? 1 : $row['instance'])][$row['field_name'] . '___' . $row['value']] = 1;
}
else{
$myData[$row['record']][($row['instance'] == null ? 1 : $row['instance'])][$row['field_name']] = $row['value'];
}
}
//format
foreach($myData AS $record => $values){
foreach($values AS $instance => $vals){
//$data .= '"' . $record . '",';
foreach($response['questions'] AS $field_name => $vars){
if($vars['type'] == 'checkbox'){
foreach($response['answers'][$field_name]['text'] AS $key => $text){
$data .= '"' . $myData[$record][$instance][$field_name . '___' . $key] . '",';
}
}
elseif(in_array($response['answers'][$field_name]['type'], array('radio','select','yesno','truefalse'))){
$data .= '"' . $response['answers'][$field_name]['text'][$myData[$record][$instance][$field_name]] . '",';
}
else{
$data .= '"' . $myData[$record][$instance][$field_name] . '",';
}
}
$data .= "\r\n";
}
}
//export
header("Content-type: text/csv");
header("Content-Disposition: attachment; filename=\"Multilingual" . ''/*\REDCap::getProjectTitle($pid)*/ . " DATA (" . $response['language'] . ") " . date('Y-m-d Hi') . ".csv\"");
header("Pragma: no-cache");
header("Expires: 0");
echo $data;
}
public function getUserSelectedLanguages($record) {
$user_languages = [];
$result = $this->queryLogs("SELECT timestamp, message, record_id, language_value, instrument, event_id
WHERE message = ? AND record_id = ?", [
"user_selected_language",
$record
]);
while($row = db_fetch_assoc($result)) {
$instrument = $row['instrument'];
$language_value = $row['language_value'];
$event_id = $row['event_id'];
if (!empty($instrument) && !empty($language_value) && !empty($event_id)) {
if (empty($user_languages[$event_id])) {
$user_languages[$event_id] = [];
}
if (empty($user_languages[$event_id][$instrument])) {
$user_languages[$event_id][$instrument] = $language_value;
}
}
}
return $user_languages;
}
public function translatePDF(&$metadata, $user_languages) {
// translate survey instructions/titles
global $Proj;
$instruments = json_decode($this->getProjectSetting('instruments'));
if ($instruments && !empty($instruments)) {
foreach ($Proj->surveys as $id => &$survey) {
// determine which language to use for this form
$lang = '';
$form_name = $survey['form_name'];
foreach($user_languages as $event_id => $event) {
if (!empty($event[$form_name])) {
$lang = $event[$form_name];
break;
}
}
if ($lang == '') {
continue;
}
if (!empty($instruments->$form_name->$lang->survey_settings->title)) {
$survey['title'] = $instruments->$form_name->$lang->survey_settings->title;
}
if (!empty($instruments->$form_name->$lang->survey_settings->acknowledgement)) {
$survey['instructions'] = $instruments->$form_name->$lang->survey_settings->acknowledgement;
}
}
}
// translate field question/answer labels
foreach($metadata as &$field) {
// see which instrument this field belongs to
$parent_form = $field['form_name'];
$field_name = $field['field_name'];
// get question and answer translations for this field
$translations = $this->getFieldTranslations($field);
// determine which language to use for that field
$lang = '';
foreach($user_languages as $event_id => $event) {
if (!empty($event[$parent_form])) {
$lang = $event[$parent_form];
break;
}
}
if ($lang == '') {
continue;
}
// determine the translated field label for this field/lang combo
if ($translations['lang'] && $translations['lang'][$lang]) {
$field['element_label'] = $translations['lang'][$lang];
}
// determine the translated answer labels for this field/lang combo
if ($translations['answers'] && $translations['answers'][$lang]) {
foreach($translations['answers'][$lang] as $raw => $translation) {
$translations['answers'][$lang][$raw] = "$raw, $translation";
}
$field['element_enum'] = implode(" \\n ", $translations['answers'][$lang]);
}
}
return $metadata;
}
public function getFieldTranslations($field_array) {
// this function returns an array like:
/*
translations = [
'lang' => [
'English' => 'My Field Label',
'Espanol' => 'Mi etiqueta de campo'
],
'answers' => [
'English' => [
'0' => 'Zero',
'1' => 'One',
'2' => 'Two'
],
'Espanol' => [
'0' => 'Cero',
'1' => 'Uno',
'2' => 'Dos'
],
]
]
assuming $field_array passed has 'misc' field with relevant @p1000lang and @p1000answers information
*/
$translations = [];
$field_misc = $field_array['misc'];
// determine indexes for p1000lang and p1000answers
$regex_capture_p1000 = "/p1000([^{]*)/m";
preg_match_all($regex_capture_p1000, $field_misc, $indexes);
$indexes = $indexes[1];
// capture pieces of field['misc'] that are contained in balanced curly braces (inclusive)
$regex_capture_balanced_braces = "/\{(?:[^}{]+|(?R))*+\}/m";
preg_match_all($regex_capture_balanced_braces, $field_misc, $matches);
if (gettype($matches) == 'array') {
$matches = $matches[0];
// decode lang json (if applicable)
$lang_index = array_search('lang', $indexes, true);
if ($lang_index !== false) {
$translations['lang'] = json_decode($matches[$lang_index], true);
}
// decode answers json (if applicable)
$answers_index = array_search('answers', $indexes, true);
if ($answers_index !== false) {
$translations['answers'] = json_decode($matches[$answers_index], true);
}
}
return $translations;
}
}
?>