-
Notifications
You must be signed in to change notification settings - Fork 42
Expand file tree
/
Copy pathProfileController.php
More file actions
205 lines (179 loc) · 6.4 KB
/
ProfileController.php
File metadata and controls
205 lines (179 loc) · 6.4 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
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
<?php
namespace App\Http\Controllers\User;
use App\Exports\CreateCSVExport;
use App\Http\Controllers\Controller;
use App\Jobs\EmailUserExportCompleted;
use App\Level;
use App\Models\CustomTag;
use App\Models\Photo;
use App\Models\User\User;
use Illuminate\Http\Request;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\Auth;
class ProfileController extends Controller
{
/**
* Apply middleware to all of these routes
*/
public function __construct ()
{
$this->middleware('auth');
}
/**
* Dispatch a request to download the users data
*
* @return array
*/
public function download (Request $request)
{
$user = Auth::user();
$dateFilter = $this->getDownloadDateFilter($request);
$x = new \DateTime();
$date = $x->format('Y-m-d');
$date = explode('-', $date);
$year = $date[0];
$month = $date[1];
$day = $date[2];
$unix = now()->timestamp;
$path = $year.'/'.$month.'/'.$day.'/'.$unix; // 2020/10/25/unix/
if (!empty($dateFilter)) {
$path .= "_from_{$dateFilter['fromDate']}_to_{$dateFilter['toDate']}";
}
$path .= '_MyData_OpenLitterMap.csv';
/* Dispatch job to create CSV file for export */
(new CreateCSVExport(null, null, null, $user->id, $dateFilter))
->queue($path, 's3', null, ['visibility' => 'public'])
->chain([
// These jobs are executed when above is finished.
new EmailUserExportCompleted($user->email, $path)
// new ....job
]);
return ['success' => true];
}
/**
* Get the users data for the given time period
*
* Period created_at || datetime
*
* start null? yyyy-mm-dd
* end null? yyyy-mm-dd
*
* @return array
*/
public function geojson ()
{
$photos = Photo::query()
->where([
['user_id', auth()->user()->id],
'verified' => 2
])
->with([
'user:id,name,username,show_username_maps,show_name_maps,settings',
'user.team:is_trusted',
'team:id,name',
'customTags:photo_id,tag',
])
->whereDate(request()->period, '>=', request()->start)
->whereDate(request()->period, '<=', request()->end)
->orderBy(request()->period, 'asc')
->get();
// Populate geojson object
$features = [];
foreach ($photos as $photo) {
$name = $photo->user->show_name_maps ? $photo->user->name : null;
$username = $photo->user->show_username_maps ? $photo->user->username : null;
$team = $photo->team ? $photo->team->name : null;
$filename = ($photo->user->is_trusted || $photo->verified >= 2) ? $photo->filename : '/assets/images/waiting.png';
$resultString = $photo->verified >= 2 ? $photo->result_string : null;
$features[] = [
'type' => 'Feature',
'geometry' => [
'type' => 'Point',
'coordinates' => [$photo->lat, $photo->lon]
],
'properties' => [
'photo_id' => $photo->id,
'result_string' => $resultString,
'filename' => $filename,
'datetime' => $photo->datetime,
'time' => $photo->datetime,
'cluster' => false,
'verified' => $photo->verified,
'name' => $name,
'username' => $username,
'team' => $team,
'picked_up' => $photo->picked_up,
'social' => $photo->user->social_links,
'custom_tags' => $photo->customTags->pluck('tag')
]
];
}
return [
'geojson' => [
'type' => 'FeatureCollection',
'features' => $features
]
];
}
/**
* Get the total number of users, and the current users position
* To get the current position, we need to count how many users have more XP than current users
*
* @return array
*/
public function index ()
{
/** @var User $user */
$user = Auth::user()->append('xp_redis');
// Todo - Store this metadata in another table
$totalUsers = User::count();
$usersPosition = $user->position;
// Todo - Store this metadata in Redis
$totalPhotosAllUsers = Photo::count();
// Todo - Store this metadata in Redis
$totalTagsAllUsers = Photo::sum('total_litter') + CustomTag::count(); // this doesn't include brands
$usersTotalTags = $user->total_tags;
$photoPercent = ($user->total_images && $totalPhotosAllUsers) ? ($user->total_images / $totalPhotosAllUsers) : 0;
$tagPercent = ($usersTotalTags && $totalTagsAllUsers) ? ($usersTotalTags / $totalTagsAllUsers) : 0;
// XP needed to reach the next level
$nextLevel = Level::where('xp', '>', $user->xp_redis)->first();
$requiredXp = $nextLevel->xp - $user->xp_redis;
$currentLevel = $nextLevel->level - 1;
// Update the user's current level if needed
if ($user->level != $currentLevel) {
$user->level = $currentLevel;
$user->save();
}
return [
'totalUsers' => $totalUsers,
'usersPosition' => $usersPosition,
'tagPercent' => $tagPercent,
'photoPercent' => $photoPercent,
'requiredXp' => $requiredXp
];
}
/**
* Returns an array of values
* so that users can filter their own data
*
* @param Request $request
* @return array
*/
private function getDownloadDateFilter(Request $request): array
{
if (!$request->dateField || !($request->fromDate || $request->toDate)) {
return [];
}
$fromDate = $request->fromDate
? Carbon::parse($request->fromDate)
: Carbon::create(2017);
$toDate = $request->toDate
? Carbon::parse($request->toDate)
: now();
return [
'column' => $request->dateField,
'fromDate' => $fromDate->toDateString(),
'toDate' => $toDate->toDateString()
];
}
}