-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsave_randomization_date.php
More file actions
61 lines (45 loc) · 1.85 KB
/
Copy pathsave_randomization_date.php
File metadata and controls
61 lines (45 loc) · 1.85 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
<?php
namespace NorwichCTU\save_randomization_date ;
use \REDCap;
class save_randomization_date extends \ExternalModules\AbstractExternalModule {
public function __construct() {
parent::__construct();
// Other code to run when object is instantiated
}
public function redcap_save_record($project_id, $record, $instrument, $event_id, $group_id, $survey_hash, $response_id, $repeat_instance) {
// Define where the randomisation date is recorded (project specific)
$dateField = $this->getProjectSetting("date-field");
$dateEvent = $this->getProjectSetting("date-event");
$dateFormat = $this->getProjectSetting("date-format");
//set the format of the date/time
$dateFormats=array();
$dateFormats[0]="%Y-%m-%d";
$dateFormats[1]="%Y-%m-%d %H:%i";
$dateFormats[2]="%Y-%m-%d %T";
$dateFormatString=$dateFormats[$dateFormat];
// Load existing value in the randomisation date field
$loadData = REDCap::getData('array', $record, $dateField, $dateEvent);
$randoDate = $loadData[$record][$dateEvent][$dateField];
// If it doesn't already contain data
if($randoDate =="") {
// Get the randomisation date from the log
$log_event_table = method_exists('\REDCap', 'getLogEventTable') ? \REDCap::getLogEventTable($project_id) : "redcap_log_event";
$sql = "SELECT DATE_FORMAT(ts, '$dateFormatString') AS 'logged_date'
FROM $log_event_table
WHERE description = 'randomize record' AND pk = '" . db_escape($record) . "' AND project_id = " . $project_id;
$q = db_query($sql);
$row = db_fetch_assoc($q);
// Check the returned row isn't empty
if (!empty($row)) {
// Build an array to save
$saveData = array(
$record => array(
$dateEvent => array($dateField => $row['logged_date'])
)
);
// Save the date
$response = REDCap::saveData('array', $saveData, 'normal');
}
}
}
}