-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdisplay_algorithm.php
More file actions
141 lines (125 loc) · 4.98 KB
/
display_algorithm.php
File metadata and controls
141 lines (125 loc) · 4.98 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
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
<?php
/* Set dummy session information */
/*
$_SESSION['dishes'] = array();
$_SESSION['dishes'][] = 4;
$_SESSION['dishes'][] = 6;
$_SESSION['dishes'][] = 3;
$_SESSION['dishes'][] = 5;
*/
$_SESSION['order'] = 'sequentialZ';
/* Retrieve recipe ids */
$array_recipe_ids = array();
$recipe_count = 0;
foreach ($_SESSION['dishes'] as $id) {
$array_recipe_ids[] = $id;
++$recipe_count;
}
/* Retrieve recipe names */
$array_recipe_names = array();
foreach ($array_recipe_ids as $recipe_id) {
$query_recipe_names = "SELECT recipe_name FROM recipe WHERE id = $recipe_id";
$resource_recipe_names = mysql_query($query_recipe_names) or die(mysql_error());
$recipe_name = mysql_fetch_array($resource_recipe_names);
$array_recipe_names[] = $recipe_name['recipe_name'];
}
/* Determine task order */
/* If recipe order is sequential, display recipes in order. */
if ($_SESSION['order'] == 'sequential') {
/* Retrieve task ids */
$array_task_ids = array();
$array_task_origins = array();
$index = 0;
foreach ($array_recipe_ids as $recipe_id) {
$query_task_ids = "SELECT task_id FROM recipe_task WHERE recipe_id = $recipe_id";
$resource_task_ids = mysql_query($query_task_ids) or die(mysql_error());
while ($task_id = mysql_fetch_array($resource_task_ids)) {
$array_task_ids[] = $task_id['task_id'];
$array_task_origins[] = $index;
}
++$index;
}
}
/* Else recipes should be finished around the same time. */
else {
/* Compute the total duration of each recipe */
$array_recipe_durations = array();
$array_recipe_tasks = array();
$index = 0;
/* Find all tasks for each recipe */
foreach ($array_recipe_ids as $recipe_id) {
$query_task_ids = "SELECT task_id FROM recipe_task WHERE recipe_id = $recipe_id";
$resource_task_ids = mysql_query($query_task_ids) or die(mysql_error());
$array_recipe_durations[] = 0;
$array_recipe_tasks[] = array();
/* For each task, find its duration */
while ($task_id = mysql_fetch_array($resource_task_ids)) {
$found_task_id = $task_id['task_id'];
/* Maintain tasks to be ordered later */
$array_recipe_tasks[$index][] = $found_task_id;
/* Query databse for task duration */
$query_task_durations = "SELECT duration FROM task WHERE id = $found_task_id";
$resource_task_durations = mysql_query($query_task_durations) or die(mysql_error());
$task_duration = mysql_fetch_array($resource_task_durations);
$found_task_duration = $task_duration['duration'];
/* Convert from MySQL HH:MM:SS time format to minutes */
$duration = substr($found_task_duration, 3, 2);
/* Add to accumulated recipe duration */
$array_recipe_durations[$index] += $duration;
}
++$index;
}
/* Continuously retrieve the next task from the recipe with the longest remaining duration */
/* Finish once all tasks have been removed (all recipe durations = 0) */
$array_task_ids = array();
$array_task_origins = array();
$total_remaining_duration = 0;
foreach ($array_recipe_durations as $recipe_duration) {
$total_remaining_duration += $recipe_duration;
}
while ($total_remaining_duration > 0) {
/* Find recipe with the longest remaining duration */
$index = 0;
$longest_recipe_id = 0;
$longest_duration = 0;
foreach ($array_recipe_durations as $recipe_duration) {
if ($recipe_duration > $longest_duration) {
$longest_duration = $recipe_duration;
$longest_recipe_id = $index;
}
++$index;
}
/* Retrieve the next task and remove it from its recipe's task list */
$next_task_id = array_shift($array_recipe_tasks[$longest_recipe_id]);
$array_task_ids[] = $next_task_id;
/* Record the recipe this task belongs to */
$array_task_origins[] = $array_recipe_ids[$longest_recipe_id];
/* Find this task's duration */
$query_task_durations = "SELECT duration FROM task WHERE id = $next_task_id";
$resource_task_durations = mysql_query($query_task_durations) or die(mysql_error());
$task_duration = mysql_fetch_array($resource_task_durations);
$found_task_duration = $task_duration['duration'];
/* Convert from MySQL HH:MM:SS time format to minutes */
$duration = substr($found_task_duration, 3, 2);
/* Subtract duration from recipe and total remaining durations */
$array_recipe_durations[$longest_recipe_id] -= $duration;
$total_remaining_duration -= $duration;
}
}
/* Retrieve task names */
$array_task_names = array();
foreach ($array_task_ids as $task_id) {
$query_task_names = "SELECT task_name FROM task WHERE id = $task_id";
$resource_task_names = mysql_query($query_task_names) or die(mysql_error());
$task_name = mysql_fetch_array($resource_task_names);
$array_task_names[] = $task_name['task_name'];
}
/* Retreieve task descriptions */
$array_task_descriptions = array();
foreach ($array_task_ids as $task_id) {
$query_task_descriptions = "SELECT description FROM task WHERE id = $task_id";
$resource_task_descriptions = mysql_query($query_task_descriptions) or die(mysql_error());
$task_description = mysql_fetch_array($resource_task_descriptions);
$array_task_descriptions[] = $task_description['description'];
}
?>