Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file added .idea/olm.iml
Empty file.
50 changes: 50 additions & 0 deletions app/Helpers/Get/User/GetUserDataHelper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php


namespace App\Helpers\Get\User;


use App\Level;
use App\Models\Photo;
use App\Models\User\User;

trait GetUserDataHelper
{
/**
* Get the users current position and other global metadata
*
* @param $userXp
* @param $totalTags
* @param $totalImages
* @return array
*/
public static function get ($userXp, $totalTags, $totalImages)
{
// Todo - Store this metadata in another table
$totalUsers = User::count();

$usersPosition = User::where('xp', '>', $userXp)->count() + 1;

// Todo - Store this metadata in Redis
$totalPhotosAllUsers = Photo::count();
// Todo - Store this metadata in Redis
$totalTagsAllUsers = Photo::sum('total_litter'); // this doesn't include brands

$usersTotalTags = $totalTags;

$photoPercent = ($totalImages && $totalPhotosAllUsers) ? number_format(($totalImages / $totalPhotosAllUsers), 2) : 0;
$tagPercent = ($usersTotalTags && $totalTagsAllUsers) ? number_format(($usersTotalTags / $totalTagsAllUsers), 2) : 0;

// XP needed to reach the next level
$nextLevelXp = Level::where('xp', '>=', $userXp)->first()->xp;
$requiredXp = $nextLevelXp - $userXp;

return [
'totalUsers' => $totalUsers,
'usersPosition' => $usersPosition,
'tagPercent' => $tagPercent,
'photoPercent' => $photoPercent,
'requiredXp' => $requiredXp
];
}
}
7 changes: 5 additions & 2 deletions app/Http/Controllers/HomeController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace App\Http\Controllers;

// use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

class HomeController extends Controller
Expand All @@ -18,15 +17,19 @@ public function index ()
$auth = Auth::check();

$user = null;

if ($auth)
{
$user = Auth::user();

// Load this data
$user->roles;
$user->settings;
}

// We set this to true when user verifies their email
$verified = false;
// or when a user unsubscribes from emails
// We set this to true when a user unsubscribes from communication
$unsub = false;

return view('root', compact('auth', 'user', 'verified', 'unsub'));
Expand Down
68 changes: 38 additions & 30 deletions app/Http/Controllers/User/ProfileController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,19 @@

namespace App\Http\Controllers\User;

use App\Exports\CreateCSVExport;
use App\Http\Controllers\Controller;
use App\Jobs\EmailUserExportCompleted;
use App\Helpers\Get\User\GetUserDataHelper;
use App\Level;
use App\Models\Photo;
use App\Models\User\User;

use App\Exports\CreateCSVExport;
use App\Jobs\EmailUserExportCompleted;

use Illuminate\Routing\Redirector;
use Illuminate\Support\Facades\Auth;

use App\Http\Controllers\Controller;

class ProfileController extends Controller
{
/**
Expand Down Expand Up @@ -91,6 +96,7 @@ public function geojson ()
'features' => []
];

// Might be big...
$photos = $query->get();

// Populate geojson object
Expand All @@ -100,7 +106,7 @@ public function geojson ()
'type' => 'Feature',
'geometry' => [
'type' => 'Point',
'coordinates' => [$photo->lon, $photo->lat]
'coordinates' => [$photo->lat, $photo->lon]
],

'properties' => [
Expand All @@ -118,45 +124,47 @@ public function geojson ()
}

return [
'success' => true,
'geojson' => $geojson
];
}

/**
* 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
* Load extra data for the Users Profile Page
*
* This is also used to get extra data for a PublicProfile if allowed.
*
* Gets:
* - total number of users,
* - users position
*
* @return array
*/
public function index ()
public function index () : array
{
$user = Auth::user();

// Todo - Store this metadata in another table
$totalUsers = User::count();

$usersPosition = $user->position;
$user = request()->has('username')
? User::where('username', request()->username)->first()
: Auth::user();

// Todo - Store this metadata in Redis
$totalPhotosAllUsers = Photo::count();
// Todo - Store this metadata in Redis
$totalTagsAllUsers = Photo::sum('total_litter'); // 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;
if (Auth::guest() && (!$user || !isset($user->settings) || !$user->settings->show_public_profile)) {
return [
'success' => false
];
}

// XP needed to reach the next level
$nextLevelXp = Level::where('xp', '>=', $user->xp)->first()->xp;
$requiredXp = $nextLevelXp - $user->xp;
$userData = GetUserDataHelper::get(
$user->xp,
$user->total_tags,
$user->total_images
);

return [
'totalUsers' => $totalUsers,
'usersPosition' => $usersPosition,
'tagPercent' => $tagPercent,
'photoPercent' => $photoPercent,
'requiredXp' => $requiredXp
'success' => true,
'totalUsers' => $userData->totalUsers,
'usersPosition' => $userData->usersPosition,
'tagPercent' => $userData->tagPercent,
'photoPercent' => $userData->photoPercent,
'requiredXp' => $userData->requiredXp
];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
<?php

namespace App\Http\Controllers\User\PublicProfile;

use App\Http\Controllers\Controller;
use App\Models\Photo;
use App\Models\User\User;
use Illuminate\Http\Request;

class PublicProfileMapController extends Controller
{
/**
* When the users settings allow it,
*
* Return their data for the given date range.
*
* Todo:
* - a lot of duplication here with PublicProfileController @ get user
* - a lot of duplication here with ProfileController @ geojson
*/
public function index ()
{
$user = User::select([
'id',
'username',
'level',
'xp',
'photos_per_month'
])
->with(['settings' => function ($q) {
$q->select('id', 'user_id', 'public_profile_show_map', 'show_public_profile');
}])
->where([
'username' => request()->username
])->first();

if (!$user || !isset($user->settings) || !$user->settings->show_public_profile || !$user->settings->public_profile_show_map) {
return redirect('/');
}

// Todo - Pre-cluster each users photos
$query = Photo::select('id', 'filename', 'datetime', 'lat', 'lon', 'model', 'result_string', 'created_at')
->where([
'user_id' => $user->id,
['verified', '>=', 2]
])
->whereDate(request()->period, '>=', request()->start)
->whereDate(request()->period, '<=', request()->end);

$geojson = [
'type' => 'FeatureCollection',
'features' => []
];

// Might be big...
$photos = $query->get();

// Populate geojson object
foreach ($photos as $photo)
{
$feature = [
'type' => 'Feature',
'geometry' => [
'type' => 'Point',
'coordinates' => [$photo->lat, $photo->lon]
],

'properties' => [
'img' => $photo->filename,
'model' => $photo->model,
'datetime' => $photo->datetime,
'latlng' => [$photo->lat, $photo->lon],
'text' => $photo->result_string
]
];

array_push($geojson["features"], $feature);
}

return [
'success' => true,
'geojson' => $geojson
];
}
}
Loading