Example #1
0
	/**
	 * @param $record
	 * @param $instrument
	 * @param $debug
	 */
	public static function set_dag($record, $instrument, $debug)
	{
		global $project_id;
		/**
		 * SET Data Access Group based upon dm_usubjid prefix
		 */
		$fields = array('dm_usubjid');
		$data = REDCap::getData('array', $record, $fields);
		foreach ($data AS $subject) {
			foreach ($subject AS $event_id => $event) {
				if ($event['dm_usubjid'] != '') {
					/**
					 * find which DAG this subject belongs to
					 */
					$site_prefix = substr($event['dm_usubjid'], 0, 3) . '%';
					$dag_query = "SELECT group_id, group_name FROM redcap_data_access_groups WHERE project_id = '$project_id' AND group_name LIKE '$site_prefix'";
					$dag_result = db_query($dag_query);
					if ($dag_result) {
						$dag = db_fetch_assoc($dag_result);
						if (isset($dag['group_id'])) {
							/**
							 * For each event in project for this subject, determine if this subject_id has been added to its appropriate DAG. If it hasn't, make it so.
							 * First, we need a list of events for which this subject has data
							 */
							$subject_events_query = "SELECT DISTINCT event_id FROM redcap_data WHERE project_id = '$project_id' AND record = '$record' AND field_name = '" . $instrument . "_complete'";
							$subject_events_result = db_query($subject_events_query);
							if ($subject_events_result) {
								while ($subject_events_row = db_fetch_assoc($subject_events_result)) {
									if (isset($subject_events_row['event_id'])) {
										$_GET['event_id'] = $subject_events_row['event_id']; // for logging
										/**
										 * The subject has data in this event_id
										 * does the subject have corresponding DAG assignment?
										 */
										$has_event_data_query = "SELECT DISTINCT event_id FROM redcap_data WHERE project_id = '$project_id' AND record = '$record' AND event_id = '" . $subject_events_row['event_id'] . "' AND field_name = '__GROUPID__'";
										$has_event_data_result = db_query($has_event_data_query);
										if ($has_event_data_result) {
											$has_event_data = db_fetch_assoc($has_event_data_result);
											if (!isset($has_event_data['event_id'])) {
												/**
												 * Subject does not have a matching DAG assignment for this data
												 * construct proper matching __GROUPID__ record and insert
												 */
												$insert_dag_query = "INSERT INTO redcap_data SET record = '$record', event_id = '" . $subject_events_row['event_id'] . "', value = '" . $dag['group_id'] . "', project_id = '$project_id', field_name = '__GROUPID__'";
												if (!$debug) {
													if (db_query($insert_dag_query)) {
														target_log_event($insert_dag_query, 'redcap_data', 'insert', $record, $dag['group_name'], 'Assign record to Data Access Group (' . $dag['group_name'] . ')');
													} else {
														error_log("SQL INSERT FAILED: " . db_error() . "\n");
														echo db_error() . "\n";
													}
												} else {
													show_var($insert_dag_query);
													error_log('(TESTING) NOTICE: ' . $insert_dag_query);
												}
											}
											db_free_result($has_event_data_result);
										}
									}
								}
								db_free_result($subject_events_result);
							}
						}
						db_free_result($dag_result);
					}
				}
			}
		}
	}
					echo "<div class='red'>ALL values for this field are hidden. DELETE value: {$empty_row['value']}.</div>";
					show_var($history, "{$empty_row['record']}, {$empty_row['event_id']}, {$empty_row['field_name']}", 'red');
					show_var($delete_query);
				}
			} else {
				/**
				 * All are not hidden. If the most recent value shown is not THIS VALUE, then delete the row
				 */
				$mutable_history = $history;
				$most_recent_event = array_pop($mutable_history);
				if (($most_recent_event['value'] != $empty_row['value']) || $most_recent_event['value'] == '') {
					$rows_deleted++;
					$delete_query = "DELETE FROM redcap_data WHERE record = '{$empty_row['record']}' AND project_id = '$project_id' AND event_id = '{$empty_row['event_id']}' AND field_name = '{$empty_row['field_name']}' AND value = '" . prep($empty_row['value']) . "' LIMIT 1";
					if (!$debug) {
						if (db_query($delete_query)) {
							target_log_event($delete_query, 'redcap_data', 'delete', $empty_row['record'], "{$empty_row['field_name']} = '{$empty_row['value']}'", 'Delete hidden blank value', '', $project_id, $empty_row['event_id']);
						} else {
							error_log("SQL DELETE FAILED: " . db_error() . ': ' . $delete_query);
							echo db_error() . "<br />" . $delete_query;
						}
					} else {
						echo "<div class='red'>NON-MATCHING value is shown. THIS value ({$empty_row['value']}) is hidden. DELETE row.</div>";
						show_var($history, "{$empty_row['record']}, {$empty_row['event_id']}, {$empty_row['field_name']}", 'red');
						show_var($most_recent_event['value'], 'MOST RECENT', 'red');
						show_var($delete_query);
					}
				} else {
					/**
					 * do nothing
					 */
					if ($debug) {
Example #3
0
								 * The subject has data in this event_id
								 * does the subject have corresponding DAG assignment?
								 */
								$has_event_data_query = "SELECT DISTINCT event_id FROM redcap_data WHERE project_id = '$project_id' AND record = '{$event['dm_subjid']}' AND event_id = '" . $subject_events_row['event_id'] . "' AND field_name = '__GROUPID__'";
								$has_event_data_result = db_query($has_event_data_query);
								if ($has_event_data_result) {
									$has_event_data = db_fetch_assoc($has_event_data_result);
									if (!isset($has_event_data['event_id'])) {
										/**
										 * Subject does not have a matching DAG assignment for this data
										 * construct proper matching __GROUPID__ record and insert
										 */
										$insert_dag_query = "INSERT INTO redcap_data SET record = '{$event['dm_subjid']}', event_id = '" . $subject_events_row['event_id'] . "', value = '" . $dag['group_id'] . "', project_id = '$project_id', field_name = '__GROUPID__'";
										if (!$debug) {
											if (db_query($insert_dag_query)) {
												target_log_event($insert_dag_query, 'redcap_data', 'insert', $event['dm_subjid'], $dag['group_name'], 'Assign record to Data Access Group (' . $dag['group_name'] . ')');
												show_var($insert_dag_query, '', 'green');
											} else {
												error_log("SQL INSERT FAILED: " . db_error() . "\n");
												echo db_error() . "\n";
											}
										} else {
											show_var($insert_dag_query);
										}
									}
									db_free_result($has_event_data_result);
								}
							}
						}
						db_free_result($subject_events_result);
					}
 */
if ($debug) {
	$timer['main_start'] = microtime(true);
}
$delete_message = 'Delete record';
$clean_result = db_query("SELECT result.* FROM
(SELECT *, count(value) as cnt FROM redcap_data WHERE project_id = $project_id GROUP BY record, event_id, field_name, value) result
WHERE result.cnt > 1 ORDER BY abs(result.record) ASC");
if ($clean_result) {
	while ($row = db_fetch_assoc($clean_result)) {
		$row_count = $row['cnt'] - 1;
		$row_text = $row_count == 1 ? 'row' : 'rows';
		$change_message = "Remove $row_count spurious $row_text from redcap_data proliferated due to a sofware bug, repaired as of 2014-12-08.";
		$delete_query = "DELETE FROM redcap_data WHERE record = '{$row['record']}' AND field_name = '{$row['field_name']}' AND event_id = '{$row['event_id']}' AND value = '{$row['value']}' LIMIT {$row_count}";
		d($delete_query);
		if (!$debug) {
			if (db_query($delete_query)) {
				target_log_event($delete_query, 'redcap_data', 'delete', $row['record'], "{$row['field_name']} = '{$row['value']}'", $delete_message, $change_message, $project_id, $row['event_id']);
				d("SUCCESS", $delete_query);
			} else {
				error_log(db_error() . "\n");
				d(db_error());
			}
		}
	}
}
if ($debug) {
	$timer['main_end'] = microtime(true);
	$init_time = benchmark_timing($timer);
	echo $init_time;
}
}
$atc_reset_val = '';
$delete_message = 'Flushing derived ATC data prior to refresh';
$flush_fields = array('cm_suppcm_atcname', 'cm_suppcm_atc2name');
$flush_data = REDCap::getData('array', $subjects, $flush_fields);
$update_message = 'Refresh ATC, Indication and Bodsys coding';
$fields = array('cm_cmoccur', 'cm_cmdecod', 'cm_cmindc', 'cm_oth_cmindc', 'cm_suppcm_indcmodf', 'cm_suppcm_indcod', 'cm_suppcm_indcsys', 'cm_suppcm_atcname', 'cm_suppcm_atc2name');
$data = REDCap::getData('array', $subjects, $fields);
foreach ($flush_data AS $subject_id => $flush_subject) {
	foreach ($flush_subject AS $event_id => $event) {
		foreach ($flush_fields as $flush_field) {
			if ($event[$flush_field] != '') {
				$flush_sql = "DELETE FROM redcap_data WHERE project_id = '$project_id' AND record = '$subject_id' AND event_id = '$event_id' AND field_name = '$flush_field'";
				if (!$debug) {
					if (db_query($flush_sql)) {
						target_log_event($flush_sql, 'redcap_data', 'delete', $subject_id, "$flush_field = ''", 'Delete record', $delete_message, $project_id, $event_id);
					} else {
						error_log(db_error() . ': ' . $flush_sql);
						echo db_error() . "<br />" . $flush_sql;
					}
				} else {
					show_var($flush_sql);
					error_log("DEBUG: " . $flush_sql);
				}

			}
		}
 	}
	foreach ($data[$subject_id] AS $subject) {
		foreach ($subject AS $event_id => $event) {
			if ($event['cm_cmoccur'] == 'Y') {