/**
 * @param $record
 * @param $event_id
 * @param $group_id
 * @param $debug
 */
function schedule_surveys($record, $event_id, $group_id, $debug)
{
	global $Proj, $project_id, $user_rights, $table_pk;
	/**
	 * if the user is in a DAG
	 */
	if ($user_rights['group_id'] != "") {
		/**
		 * does this record exist?
		 */
		$q = db_query("SELECT 1 from redcap_data WHERE project_id = $project_id AND record = '$record' LIMIT 1");
		if (db_num_rows($q) > 0) {
			/**
			 * is the record in this users DAG?
			 */
			$q = db_query("SELECT 1 from redcap_data WHERE project_id = $project_id AND record = '$record' AND field_name = '__GROUPID__' AND value = '{$user_rights['group_id']}' LIMIT 1");
			if (db_num_rows($q) < 1) {
				/**
				 * record is not in Users DAG!
				 */
				REDCap::logEvent('Scheduled record is not in users DAG', '', '', $record, $event_id, $project_id);
				exit;
			}
		}
	}
	/**
	 * check to see if the subject has an existing schedule on an existing arm
	 */
	$sub = "SELECT DISTINCT e.arm_id from redcap_events_calendar c, redcap_events_metadata e WHERE c.project_id = $project_id AND c.record = '$record' AND c.event_id = e.event_id";
	$sched_arm_result = db_query("SELECT arm_num FROM redcap_events_arms WHERE project_id = $project_id AND arm_id IN (" . pre_query($sub) . ")");
	if ($sched_arm_result) {
		$trt = Treatment::getTrtInfo($record);
		if ($debug) {
			error_log(print_r($trt, true));
		}
		$tx_start_date = $trt['rfxstdtc'];
		$rand_date = $trt['rand_date'];
		$dates = array();
		$arm_num = db_result($sched_arm_result, 0, 'arm_num');
		if (isset($arm_num) && $arm_num != '') { // subject has an existing schedule. keep existing event_id > arm structure
			if ($arm_num != '1') { // make sure we don't put anything in the first arm
				$q = db_query("SELECT * from redcap_events_metadata m, redcap_events_arms a WHERE a.project_id = $project_id AND a.arm_id = m.arm_id AND a.arm_num = $arm_num order by m.day_offset, m.descrip");
				if ($q) {
					while ($row = db_fetch_assoc($q)) { // if we have no $arm_num, this will be empty
						/**
						 * get the event date ($rand_date for baseline and $tx_start_date + day_offset)
						 */
						$row['day_offset'] = $arm_num != $trt['timing_arm_num'] ? $trt['timing_offsets'][$row['descrip']] : $row['day_offset'];
						if (in_array($row['descrip'], array('Baseline', 'EOT+1Year', 'EOT+3Year'))) {
							$this_event_date = isset($rand_date) && $rand_date != '' ? add_date($rand_date, $row['day_offset']) : null;
						} else {
							$this_event_date = isset($tx_start_date) && $tx_start_date != '' ? add_date($tx_start_date, $row['day_offset']) : null;
						}
						$dates[$row['event_id']] = $this_event_date;
					}
					db_free_result($q);
				}
			} else {
				REDCap::logEvent('Scheduling attempted in invalid arm', '', '', $record, $event_id, $project_id);
			}
		} else { // subject's schedule is new. put dates into event_ids for this arm
			$arm_result = db_query("SELECT arm_num FROM redcap_events_arms WHERE project_id = '$project_id' AND arm_name = '{$trt['arm']}'");
			if ($arm_result) {
				$arm_num = db_result($arm_result, 0, 'arm_num');
				if ($arm_num != '1') {
					$q = db_query("SELECT * from redcap_events_metadata m, redcap_events_arms a WHERE a.project_id = $project_id AND a.arm_id = m.arm_id AND a.arm_num = $arm_num order by m.day_offset, m.descrip");
					if ($q) {
						while ($row = db_fetch_assoc($q)) { // if we have no $arm_num, this will be empty
							/**
							 * get the event date ($rand_date for baseline and $tx_start_date + day_offset)
							 */
							if (in_array($row['descrip'], array('Baseline', 'EOT+1Year', 'EOT+3Year'))) {
								$this_event_date = isset($rand_date) && $rand_date != '' ? add_date($rand_date, $row['day_offset']) : null;
							} else {
								$this_event_date = isset($tx_start_date) && $tx_start_date != '' ? add_date($tx_start_date, $row['day_offset']) : null;
							}
							$dates[$row['event_id']] = $this_event_date;
						}
						db_free_result($q);
					}
				} else {
					REDCap::logEvent('Scheduling attempted in invalid arm', '', '', $record, $event_id, $project_id);
				}
				db_free_result($arm_result);
			}
		}
		if ($debug) {
			error_log(print_r($dates, true));
		}
		if (!empty($dates)) {
			/**
			 * do we have an existing schedule?
			 */
			$sql = "SELECT c.event_date, c.baseline_date, e.* FROM redcap_events_calendar c, redcap_events_metadata e WHERE c.project_id = $project_id AND c.record = '$record' AND c.event_id = e.event_id AND e.arm_id IN (" . pre_query($sub) . ")";
			$sched_result = db_query($sql);
			if ($sched_result) {
				$sql_all = array();
				$sql_errors = array();
				if (db_num_rows($sched_result) > 0) {
					while ($sched_row = db_fetch_assoc($sched_result)) {
						$base_date = in_array($sched_row['descrip'], array('Baseline', 'EOT+1Year', 'EOT+3Year')) ? $trt['rand_date'] : $trt['rfxstdtc'];
						/**
						 * if the scheduled date is in the $dates array, we don't care about it, so ignore it and remove from $dates
						 * if we have an existing schedule and the dates have changed, update the schedule and remove from $dates
						 * if the base date has changed, update it and the schedule
						 * whatever is left will be new dates, insert into schedule
						 */
						if ($dates[$sched_row['event_id']] == $sched_row['event_date']) {
							unset($dates[$sched_row['event_id']]);
						}
						if (isset($dates[$sched_row['event_id']]) && $dates[$sched_row['event_id']] != '' && $sched_row['event_date'] != $dates[$sched_row['event_id']]) { // the date has changed. update the date.
							$sql = "UPDATE redcap_events_calendar SET event_date = '{$dates[$sched_row['event_id']]}' WHERE record = '$record' AND project_id = '$project_id' AND group_id = '$group_id' AND event_id = '{$sched_row['event_id']}' AND event_date = '{$sched_row['event_date']}'";
							if (!$debug) {
								if (db_query($sql)) {
									$sql_all[] = $sql;
									log_event($sql, "redcap_events_calendar", "MANAGE", $record, $sched_row['event_id'], "Update calendar event");
								} else {
									$sql_errors[] = $sql;
								}
							} else {
								error_log($sql);
							}
							unset($dates[$sched_row['event_id']]);
						}
						if ($base_date != $sched_row['baseline_date']) { // the base_date has changed. this will only occur if the treatment start date or randomization date are changed in the study.
							$sql = "UPDATE redcap_events_calendar SET baseline_date = '" . prep($base_date) . "' WHERE record = '$record' AND project_id = '$project_id' AND group_id = '$group_id' AND event_id = '{$sched_row['event_id']}' AND baseline_date = '{$sched_row['baseline_date']}'";
							if (!$debug) {
								if (db_query($sql)) {
									$sql_all[] = $sql;
									log_event($sql, "redcap_events_calendar", "MANAGE", $record, $sched_row['event_id'], "Update calendar event");
								} else {
									$sql_errors[] = $sql;
								}
							} else {
								error_log($sql);
							}
							unset($dates[$sched_row['event_id']]);
						}
					}
					foreach ($dates AS $date_event_id => $date) { //Loop through dates and add them to the schedule
						$base_date = in_array($Proj->eventInfo[$date_event_id]['name'], array('Baseline', 'EOT+1Year', 'EOT+3Year')) ? $trt['rand_date'] : $trt['rfxstdtc'];
						if (isset($date) && $date != "") { //Add to table
							$sql = "INSERT INTO redcap_events_calendar (record, project_id, group_id, event_id, event_date, event_time, event_status, baseline_date) VALUES ('$record', $project_id, " . checkNull($group_id) . ", '" . prep($date_event_id) . "', '" . prep($date) . "', '" . null . "', 0, '$base_date')";
							if (!$debug) {
								if (db_query($sql)) {
									$sql_all[] = $sql;
								} else {
									$sql_errors[] = $sql;
								}
							} else {
								error_log($sql);
							}
						} else {
							REDCap::logEvent('Schedule start date is not a valid date', '', '', $record, $event_id, $project_id);
						}
					}
					log_event(implode(";\n", $sql_all), "redcap_events_calendar", "MANAGE", $_GET['idnumber'], "$table_pk = '$record'", "Perform scheduling");
				} else {
					foreach ($dates AS $date_event_id => $date) { //Loop through dates and add them to the schedule
						$base_date = in_array($Proj->eventInfo[$date_event_id]['name'], array('Baseline', 'EOT+1Year', 'EOT+3Year')) ? $trt['rand_date'] : $trt['rfxstdtc'];
						if (isset($date) && $date != "") { //Add to table
							$sql = "INSERT INTO redcap_events_calendar (record, project_id, group_id, event_id, event_date, event_time, event_status, baseline_date) VALUES ('$record', $project_id, " . checkNull($group_id) . ", '" . prep($date_event_id) . "', '" . prep($date) . "', '" . null . "', 0, '$base_date')";
							if (!$debug) {
								if (db_query($sql)) {
									$sql_all[] = $sql;
								} else {
									$sql_errors[] = $sql;
								}
							} else {
								error_log($sql);
							}
						} else {
							REDCap::logEvent('Schedule start date is not a valid date', '', '', $record, $event_id, $project_id);
						}
					}
					log_event(implode(";\n", $sql_all), "redcap_events_calendar", "MANAGE", $_GET['idnumber'], "$table_pk = '$record'", "Perform scheduling");
				}
			}
			db_free_result($sched_result);
		}
		db_free_result($sched_arm_result);
	}
}