|
|
@ -198,30 +198,176 @@ function entry_date($entry, $user) { |
|
|
|
return $date; |
|
|
|
} |
|
|
|
|
|
|
|
function caffeine_options() { |
|
|
|
return array( |
|
|
|
function default_drink_options() { |
|
|
|
return [ |
|
|
|
'Coffee', |
|
|
|
'Americano', |
|
|
|
'Latte', |
|
|
|
'Cappuccino', |
|
|
|
'Espresso', |
|
|
|
'Iced Coffee', |
|
|
|
'Iced Americano', |
|
|
|
'Iced Latte', |
|
|
|
'Black Tea', |
|
|
|
'Tea' |
|
|
|
); |
|
|
|
} |
|
|
|
|
|
|
|
function alcohol_options() { |
|
|
|
return array( |
|
|
|
'Beer', |
|
|
|
'Cocktail', |
|
|
|
'Tea', |
|
|
|
'Mimosa', |
|
|
|
'Champagne', |
|
|
|
'Wine', |
|
|
|
'Sake', |
|
|
|
'Cider' |
|
|
|
); |
|
|
|
'Latte', |
|
|
|
'Champagne' |
|
|
|
]; |
|
|
|
} |
|
|
|
|
|
|
|
function default_food_options() { |
|
|
|
return [ |
|
|
|
'Burrito', |
|
|
|
'Banana', |
|
|
|
'Pizza', |
|
|
|
'Soup', |
|
|
|
'Tacos', |
|
|
|
'Mac and Cheese' |
|
|
|
]; |
|
|
|
} |
|
|
|
|
|
|
|
function query_user_nearby_options($type, $user_id, $latitude, $longitude) { |
|
|
|
$published = date('Y-m-d H:i:s', strtotime('-4 months')); |
|
|
|
$options = []; |
|
|
|
$optionsQ = ORM::for_table('entries')->raw_query(' |
|
|
|
SELECT *, SUM(num) AS num FROM |
|
|
|
( |
|
|
|
SELECT id, published, content, type, |
|
|
|
round(gc_distance(latitude, longitude, :latitude, :longitude) / 1000) AS dist, |
|
|
|
COUNT(1) AS num |
|
|
|
FROM entries |
|
|
|
WHERE user_id = :user_id |
|
|
|
AND type = :type |
|
|
|
AND gc_distance(latitude, longitude, :latitude, :longitude) IS NOT NULL |
|
|
|
AND published > :published /* only look at the last 4 months of posts */ |
|
|
|
GROUP BY content, round(gc_distance(latitude, longitude, :latitude, :longitude) / 1000) /* group by 1km buckets */ |
|
|
|
ORDER BY round(gc_distance(latitude, longitude, :latitude, :longitude) / 1000), COUNT(1) DESC /* order by distance and frequency */ |
|
|
|
) AS tmp |
|
|
|
WHERE num > 2 /* only include things that have been used more than 2 times in this 1km bucket */ |
|
|
|
GROUP BY content /* group by name again */ |
|
|
|
ORDER BY SUM(num) DESC /* order by overall frequency */ |
|
|
|
LIMIT 4 |
|
|
|
', ['user_id'=>$user_id, 'type'=>$type, 'latitude'=>$latitude, 'longitude'=>$longitude, 'published'=>$published])->find_many(); |
|
|
|
foreach($optionsQ as $o) { |
|
|
|
$options[] = [ |
|
|
|
'title' => $o->content, |
|
|
|
'type' => $o->type |
|
|
|
]; |
|
|
|
} |
|
|
|
return $options; |
|
|
|
} |
|
|
|
|
|
|
|
function query_user_frequent_options($type, $user_id) { |
|
|
|
$published = date('Y-m-d H:i:s', strtotime('-4 months')); |
|
|
|
$options = []; |
|
|
|
$optionsQ = ORM::for_table('entries')->raw_query(' |
|
|
|
SELECT type, content |
|
|
|
FROM entries |
|
|
|
WHERE user_id = :user_id |
|
|
|
AND type = :type |
|
|
|
AND published > :published |
|
|
|
GROUP BY content |
|
|
|
ORDER BY COUNT(1) DESC |
|
|
|
LIMIT 2 |
|
|
|
', ['user_id'=>$user_id, 'type'=>$type, 'published'=>$published])->find_many(); |
|
|
|
foreach($optionsQ as $o) { |
|
|
|
$options[] = [ |
|
|
|
'title' => $o->content, |
|
|
|
'type' => $o->type |
|
|
|
]; |
|
|
|
} |
|
|
|
return $options; |
|
|
|
} |
|
|
|
|
|
|
|
function get_entry_options($user_id, $latitude=null, $longitude=null) { |
|
|
|
/* |
|
|
|
Sections: |
|
|
|
* Recent 2 posts (food + drink combined) |
|
|
|
* Drinks (based on location) |
|
|
|
* custom box below |
|
|
|
* Food (based on location) |
|
|
|
* custom box below |
|
|
|
|
|
|
|
If no recent entries, remove that section. |
|
|
|
If no nearby food/drinks, use a default list |
|
|
|
*/ |
|
|
|
|
|
|
|
$recent = []; |
|
|
|
$drinks = []; |
|
|
|
$food = []; |
|
|
|
|
|
|
|
$recentQ = ORM::for_table('entries')->raw_query(' |
|
|
|
SELECT type, content FROM |
|
|
|
(SELECT * |
|
|
|
FROM entries |
|
|
|
WHERE user_id = :user_id |
|
|
|
ORDER BY published DESC) AS tmp |
|
|
|
GROUP BY content |
|
|
|
ORDER BY MAX(published) DESC |
|
|
|
LIMIT 4', ['user_id'=>$user_id])->find_many(); |
|
|
|
foreach($recentQ as $r) { |
|
|
|
$recent[] = [ |
|
|
|
'title' => $r->content, |
|
|
|
'type' => $r->type |
|
|
|
]; |
|
|
|
} |
|
|
|
|
|
|
|
if($latitude) { |
|
|
|
$drinks = query_user_nearby_options('drink', $user_id, $latitude, $longitude); |
|
|
|
} |
|
|
|
// If there's no nearby data (like if the user isn't including location) then return the most frequently used ones instead
|
|
|
|
if(count($drinks) == 0) { |
|
|
|
$drinks = query_user_frequent_options('drink', $user_id); |
|
|
|
} |
|
|
|
// If there's less than 4 options available, fill the list with the default options
|
|
|
|
if(count($drinks) < 4) { |
|
|
|
$default = default_drink_options(); |
|
|
|
while(count($drinks) < 4) { |
|
|
|
$next = array_shift($default); |
|
|
|
if(!in_array(['title'=>$next,'type'=>'drink'], $drinks)) { |
|
|
|
$drinks[] = [ |
|
|
|
'title' => $next, |
|
|
|
'type' => 'drink' |
|
|
|
]; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
if($latitude) { |
|
|
|
$food = query_user_nearby_options('eat', $user_id, $latitude, $longitude); |
|
|
|
} |
|
|
|
if(count($food) == 0) { |
|
|
|
$food = query_user_frequent_options('eat', $user_id); |
|
|
|
} |
|
|
|
if(count($food) < 4) { |
|
|
|
$default = default_food_options(); |
|
|
|
while(count($food) < 4) { |
|
|
|
$next = array_shift($default); |
|
|
|
if(!in_array(['title'=>$next,'type'=>'eat'], $food)) { |
|
|
|
$food[] = [ |
|
|
|
'title' => $next, |
|
|
|
'type' => 'eat' |
|
|
|
]; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
$options = [ |
|
|
|
'sections' => [ |
|
|
|
[ |
|
|
|
'title' => 'Recent', |
|
|
|
'items' => $recent |
|
|
|
], |
|
|
|
[ |
|
|
|
'title' => 'Drinks', |
|
|
|
'items' => $drinks |
|
|
|
], |
|
|
|
[ |
|
|
|
'title' => 'Food', |
|
|
|
'items' => $food |
|
|
|
] |
|
|
|
] |
|
|
|
]; |
|
|
|
|
|
|
|
if(count($options['sections'][0]['items']) == 0) |
|
|
|
array_shift($options['sections']); |
|
|
|
|
|
|
|
return $options; |
|
|
|
} |
|
|
|
|