-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDataAnywhere.php
69 lines (61 loc) · 2.65 KB
/
DataAnywhere.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
<?php
namespace UWMadison\DataAnywhere;
use ExternalModules\AbstractExternalModule;
use REDCap;
class DataAnywhere extends AbstractExternalModule
{
public function redcap_every_page_top($project_id)
{
// Custom Config page
if ($this->isPage('ExternalModules/manager/project.php') && $project_id != NULL) {
$url = $this->getUrl('config.js');
echo "<script src={$url}></script>";
}
}
public function redcap_data_entry_form_top($project_id, $record, $instrument, $event_id)
{
if (empty($record)) return; // New Record, no data
$settings = $this->getProjectSettings();
$data = [];
// Loop over all of the config, destination-all is arbitrary
foreach ($settings['destination-all'] as $index => $destAll) {
$destAll = $destAll == "1";
$destEvent = $settings['destination-event'][$index];
$destInst = $settings['destination-instrument'][$index];
if (
$destAll || (($destEvent == $event_id) && ($destInst == $instrument)) ||
(empty($destEvent) && ($destInst == $instrument)) || (($destEvent == $event_id) && empty($destInst))
) {
$srcAll = $settings['source-all'][$index] == "1";
$srcEvent = $settings['source-event'][$index];
$srcInst = $settings['source-instrument'][$index];
$srcEvent = $srcAll ? null : $srcEvent;
$fields = $srcAll ? null : REDCap::getFieldNames($srcInst);
$tmp = REDCap::getData($project_id, 'array', $record, $fields, $srcEvent);
$data = array_replace_recursive($data, $tmp);
}
}
// Pass data down to JS if we have any
if (!empty($data)) {
echo "<script> var DataAnywhere = " . json_encode([
"data" => $this->escape($data[$record]),
"eventNames" => REDCap::getEventNames(true),
"usernames" => $this->getUsers()
]) . ";</script>";
}
}
private function getUsers()
{
// Framework doesn't return user's real name
$users = "\"" . implode("\",\"", REDCap::getUsers()) . "\"";
$sql = "SELECT username, CONCAT(user_firstname, ' ' , user_lastname) as name FROM redcap_user_information WHERE username in ($users)";
$data = db_query($sql);
$results = [];
while ($row = db_fetch_assoc($data)) {
$user = htmlspecialchars($row["username"], ENT_QUOTES);
$name = htmlspecialchars($row["name"], ENT_QUOTES);
$results[$user] = $name;
}
return $results;
}
}