aboutsummaryrefslogtreecommitdiffstats
path: root/functions/overlapping_events.php
blob: 659a0cb981391c2a4300f2ae0a286f9a5dad8bef (plain) (blame)
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
<?php
// function to determine maximum necessary columns per day
// actually an algorithm to get the smallest multiple for two numbers
function kgv($a, $b) {
	$x = $a;
	$y = $b;
	while ($x != $y) {
		if ($x < $y) $x += $a;
		else $y += $b;
	}
	return $x;
}


// drei 20020921: function for checking and counting overlapping events
// drei 20020212: added parameter uid to function call
function checkOverlap($ol_start_date, $ol_start_time, $ol_end_time, $ol_uid) {
	global $master_array, $overlap_array;

		$drawTimes = drawEventTimes($ol_start_time, $ol_end_time);
		$newMasterTime = $drawTimes["draw_start"];
		// if (isset($master_array[($ol_start_date)][($newMasterTime)])) $newMasterEventKey = sizeof($master_array[($ol_start_date)][($newMasterTime)]);
		// else $newMasterEventKey = 0;
		
		$maxOverlaps = 0;
		$newEventAdded = FALSE;
		if (isset($overlap_array[($ol_start_date)])) {
			foreach ($overlap_array[($ol_start_date)] as $loopBlockKey => $loopBlock) {
				// check for overlap with existing overlap block
				if (($loopBlock["blockStart"] < $drawTimes["draw_end"]) and ($loopBlock["blockEnd"] > $drawTimes["draw_start"])) {
					$newOverlapBlock = FALSE;
					// if necessary adjust start and end times of overlap block
					if ($loopBlock["blockStart"] > $drawTimes["draw_start"]) $overlap_array[($ol_start_date)][($loopBlockKey)]["blockStart"] = $drawTimes["draw_start"];
					if ($loopBlock["blockEnd"] < $drawTimes["draw_end"]) $overlap_array[($ol_start_date)][($loopBlockKey)]["blockEnd"] = $drawTimes["draw_end"];
					// add the new event to the array of events
					// $overlap_array[($ol_start_date)][($loopBlockKey)]["events"][] = array ("time" => $newMasterTime, "key" => $newMasterEventKey);
					$overlap_array[($ol_start_date)][($loopBlockKey)]["events"][] = array ("time" => $newMasterTime, "key" => $ol_uid);
					// check if the adjusted overlap block must be merged with an existing overlap block
					reset($overlap_array[($ol_start_date)]);
					do {
						$compBlockKey = key(current($overlap_array[($ol_start_date)]));
						// only compare with other blocks
						if ($compBlockKey != $loopBlockKey) {
							// check if blocks overlap
							if (($overlap_array[($ol_start_date)][($compBlockKey)]["blockStart"] < $overlap_array[($ol_start_date)][($loopBlockKey)]["blockEnd"]) and ($overlap_array[($ol_start_date)][($compBlockKey)]["blockEnd"] > $overlap_array[($ol_start_date)][($loopBlockKey)]["blockStart"])) {
								// define start and end of merged overlap block
								if ($loopBlock["blockStart"] > $overlap_array[($ol_start_date)][($compBlockKey)]["blockStart"]) $overlap_array[($ol_start_date)][($loopBlockKey)]["blockStart"] = $overlap_array[($ol_start_date)][($compBlockKey)]["blockStart"];
								if ($loopBlock["blockEnd"] < $overlap_array[($ol_start_date)][($compBlockKey)]["blockEnd"]) $overlap_array[($ol_start_date)][($loopBlockKey)]["blockEnd"] = $overlap_array[($ol_start_date)][($compBlockKey)]["blockEnd"];
								$overlap_array[($ol_start_date)][($loopBlockKey)]["events"] = array_merge($overlap_array[($ol_start_date)][($loopBlockKey)]["events"],$overlap_array[($ol_start_date)][($compBlockKey)]["events"]);
								$overlap_array[($ol_start_date)][($loopBlockKey)]["overlapRanges"] = array_merge($overlap_array[($ol_start_date)][($loopBlockKey)]["overlapRanges"],$overlap_array[($ol_start_date)][($compBlockKey)]["overlapRanges"]);
								unset($overlap_array[($ol_start_date)][($compBlockKey)]);
								reset($overlap_array[($ol_start_date)]);
							}
						} 
					} while (next($overlap_array[($ol_start_date)]));
					// insert new event to appropriate overlap range

					$newOverlapRange = TRUE;
					foreach ($overlap_array[($ol_start_date)][($loopBlockKey)]["overlapRanges"] as $keyOverlap => $overlapRange) {
						if (($overlapRange["start"] < $drawTimes["draw_end"]) and ($overlapRange["end"] > $drawTimes["draw_start"])) {
							$overlap_array[($ol_start_date)][($loopBlockKey)]["overlapRanges"][($keyOverlap)]["count"]++;
							if ($overlapRange["start"] < $drawTimes["draw_start"]) $overlap_array[($ol_start_date)][($loopBlockKey)]["overlapRanges"][($keyOverlap)]["start"] = $drawTimes["draw_start"];
							if ($overlapRange["end"] > $drawTimes["draw_end"]) $overlap_array[($ol_start_date)][($loopBlockKey)]["overlapRanges"][($keyOverlap)]["end"] = $drawTimes["draw_end"];
							$newOverlapRange = FALSE;
		//					break;
						}
					}
					if ($newOverlapRange) {
						foreach ($loopBlock["events"] as $blockEvents) {
							$eventDrawTimes = drawEventTimes($master_array[($ol_start_date)][($blockEvents["time"])][($blockEvents["key"])]["event_start"], $master_array[($ol_start_date)][($blockEvents["time"])][($blockEvents["key"])]["event_end"]);
							if ((isset($eventDrawTimes["draw_start"]) && isset($eventDrawTimes["draw_end"]) && isset($drawTimes["draw_end"]) && isset($drawTimes["draw_start"])) && ($eventDrawTimes["draw_start"] < $drawTimes["draw_end"]) and ($eventDrawTimes["draw_end"] > $drawTimes["draw_start"])) {
								// define start time of overlap range and overlap block
								if ($eventDrawTimes["draw_start"] < $drawTimes["draw_start"]) $overlap_start = $drawTimes["draw_start"];
								else $overlap_start = $eventDrawTimes["draw_start"];
								// define end time of overlap range and overlap block
								if ($eventDrawTimes["draw_end"] > $drawTimes["draw_end"]) $overlap_end = $drawTimes["draw_end"];
								else $overlap_end = $eventDrawTimes["draw_end"];
								
								$newOverlapRange2 = TRUE;
								foreach ($overlap_array[($ol_start_date)][($loopBlockKey)]["overlapRanges"] as $keyOverlap => $overlapRange) {
									if (($overlapRange["start"] < $overlap_end) and ($overlapRange["end"] > $overlap_start)) {
										$overlap_array[($ol_start_date)][($loopBlockKey)]["overlapRanges"][($keyOverlap)]["count"]++;
										if ($overlapRange["start"] < $overlap_start) $overlap_array[($ol_start_date)][($loopBlockKey)]["overlapRanges"][($keyOverlap)]["start"] = $overlap_start;
										if ($overlapRange["end"] > $overlap_end) $overlap_array[($ol_start_date)][($loopBlockKey)]["overlapRanges"][($keyOverlap)]["end"] = $overlap_end;
										$newOverlapRange2 = FALSE;
			//							break;
									}
								}
								if ($newOverlapRange2) {
									array_push($overlap_array[($ol_start_date)][($loopBlockKey)]["overlapRanges"], array ("count" => 1,"start" => $overlap_start, "end" => $overlap_end));
								}
							}
						}
					}
					// determine the maximum overlaps for the current overlap block
					foreach ($overlap_array[($ol_start_date)][($loopBlockKey)]["overlapRanges"] as $overlapRange) {
						if ($overlapRange["count"] > $maxOverlaps) $maxOverlaps = $overlapRange["count"];
					}
					$overlap_array[($ol_start_date)][($loopBlockKey)]["maxOverlaps"] = $maxOverlaps;
					foreach ($overlap_array[($ol_start_date)][($loopBlockKey)]["events"] as $updMasterEvent) {
						//if (($updMasterEvent["time"] != $newMasterTime) or ($updMasterEvent["key"] != $newMasterEventKey)) {
						if (($updMasterEvent["time"] != $newMasterTime) or ($updMasterEvent["key"] != $ol_uid)) {
							$master_array[($ol_start_date)][($updMasterEvent["time"])][($updMasterEvent["key"])]["event_overlap"] = $maxOverlaps;
						}
					}
					$newEventAdded = TRUE;
					break;
				}
			}
		}
		if (!$newEventAdded) {
			if (isset($master_array[($ol_start_date)])) {
				foreach ($master_array[($ol_start_date)] as $keyTime => $eventTime) {
					foreach ($eventTime as $keyEvent => $event) {
						if ($keyTime != '-1') $entryDrawTimes =  drawEventTimes($event["event_start"], $event["event_end"]);
						if ((isset($entryDrawTimes["draw_start"]) && isset($entryDrawTimes["draw_end"]) && isset($drawTimes["draw_end"]) && isset($drawTimes["draw_start"])) && ($entryDrawTimes["draw_start"] < $drawTimes["draw_end"]) and ($entryDrawTimes["draw_end"] > $drawTimes["draw_start"])) {
							// define start time of overlap range and overlap block
							if ($entryDrawTimes["draw_start"] < $drawTimes["draw_start"]) {
								$overlap_start = $drawTimes["draw_start"];
								$overlapBlock_start = $entryDrawTimes["draw_start"];
							} else {
								$overlap_start = $entryDrawTimes["draw_start"];
								$overlapBlock_start = $drawTimes["draw_start"];
							}
							// define end time of overlap range and overlap block
							if ($entryDrawTimes["draw_end"] > $drawTimes["draw_end"]) {
								$overlap_end = $drawTimes["draw_end"];
								$overlapBlock_end = $entryDrawTimes["draw_end"];
							} else {
								$overlap_end = $entryDrawTimes["draw_end"];
								$overlapBlock_end = $drawTimes["draw_end"];
							}
							if (!isset($newBlockKey)) {
								// $overlap_array[($ol_start_date)][] = array ("blockStart" => $overlapBlock_start, "blockEnd" => $overlapBlock_end, "maxOverlaps" => 1, "events" => array (array ("time" => $keyTime, "key" => $keyEvent), array ("time" => $newMasterTime, "key" => $newMasterEventKey)), "overlapRanges" => array (array ("count" => 1, "start" => $overlap_start, "end" => $overlap_end)));
								$overlap_array[($ol_start_date)][] = array ("blockStart" => $overlapBlock_start, "blockEnd" => $overlapBlock_end, "maxOverlaps" => 1, "events" => array (array ("time" => $keyTime, "key" => $keyEvent), array ("time" => $newMasterTime, "key" => $ol_uid)), "overlapRanges" => array (array ("count" => 1, "start" => $overlap_start, "end" => $overlap_end)));
								$maxOverlaps = 1;
								end($overlap_array[($ol_start_date)]);
								$newBlockKey = key($overlap_array[($ol_start_date)]);
							} else {
								if ($overlap_array[($ol_start_date)][($newBlockKey)]["blockStart"] > $overlapBlock_start) $overlap_array[($ol_start_date)][($newBlockKey)]["blockStart"] = $overlapBlock_start;
								if ($overlap_array[($ol_start_date)][($newBlockKey)]["blockEnd"] < $overlapBlock_end) $overlap_array[($ol_start_date)][($newBlockKey)]["blockEnd"] = $overlapBlock_end;
								$overlap_array[($ol_start_date)][($newBlockKey)]["events"][] = array ("time" => $keyTime, "key" => $keyEvent);
								$overlap_array[($ol_start_date)][($newBlockKey)]["overlapRanges"][] = array ("count" => 1, "start" => $overlap_start, "end" => $overlap_end);
							}
							// update master_array
							$master_array[($ol_start_date)][($keyTime)][($keyEvent)]["event_overlap"] = $maxOverlaps;
						}
					}
				}
			}
		}

// for debugging the checkOverlap function
//print 'Date: ' . $ol_start_date . ' / New Time: ' . $newMasterTime . ' / New Key: ' . $newMasterEventKey . '<br />';
//print '<pre>';
//print_r($overlap_array);
//print '</pre>';

	return $maxOverlaps;
}

// drei 20021126: function for checking and removing overlapping events
//function removeOverlap($ol_start_date, $ol_start_time, $ol_key = 0) {
function removeOverlap($ol_start_date, $ol_start_time, $ol_key) {
	global $master_array, $overlap_array;
	if (isset($overlap_array[$ol_start_date])) {
		if (sizeof($overlap_array[$ol_start_date]) > 0) {
			$ol_end_time = $master_array[$ol_start_date][$ol_start_time][$ol_key]["event_end"];
			foreach ($overlap_array[$ol_start_date] as $keyBlock => $blockId) {
//				if (($blockId["blockStart"] <= $ol_start_time) or ($blockId["blockEnd"] >= $ol_start_time)) {
				if (($blockId["blockStart"] <= $ol_start_time) and ($blockId["blockEnd"] > $ol_start_time)) {
					foreach ($blockId["events"] as $keyEvent => $ol_event) {
						$master_array[$ol_start_date][$ol_event["time"]][$ol_event["key"]]["event_overlap"] -= 1;
						if (($ol_event["time"] == $ol_start_time) and ($ol_event["key"] == $ol_key)) {
							unset ($overlap_array[$ol_start_date][$keyBlock]["events"][$keyEvent]);
						}
					}
					if ($blockId["maxOverlaps"] == 1) {
						unset ($overlap_array[$ol_start_date][$keyBlock]);
					} else {
						$overlap_array[$ol_start_date][$keyBlock]["maxOverlaps"] -= 1;
						//$blockId["maxOverlaps"] -= 1;
						// SJBO: Shouldn't something be done with [overlapRanges] as well?
					}
				}
			}
		}
	}
}
?>

© 2014-2024 Faster IT GmbH | imprint | privacy policy