Skip to content

Commit

Permalink
Merge pull request #22 from alkrauss48/issue-16-analytics
Browse files Browse the repository at this point in the history
Issue 16 analytics
  • Loading branch information
alkrauss48 authored May 5, 2024
2 parents 3b6ec48 + 4c7ec46 commit ffe9694
Show file tree
Hide file tree
Showing 6 changed files with 215 additions and 102 deletions.
6 changes: 6 additions & 0 deletions app/Filament/Pages/Dashboard.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Filament\Forms\Get;
use Filament\Pages\Dashboard as BaseDashboard;
use Filament\Pages\Dashboard\Concerns\HasFiltersForm;
use Illuminate\Contracts\View\View;

class Dashboard extends BaseDashboard
{
Expand All @@ -22,6 +23,11 @@ public function getColumns(): int|string|array
return 2;
}

public function getFooter(): ?View
{
return view('filament.dashboard.footer');
}

protected function getHeaderActions(): array
{
return [
Expand Down
2 changes: 1 addition & 1 deletion app/Filament/Widgets/TopViews.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public function table(Table $table): Table
->emptyStateDescription(null)
->emptyStateActions([
Action::make('create')
->label('Create Presentation')
->label('New Presentation')
->url(route('filament.admin.resources.presentations.create'))
->icon('heroicon-m-plus')
->button(),
Expand Down
28 changes: 6 additions & 22 deletions app/Http/Controllers/PresentationController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace App\Http\Controllers;

use App\Models\Presentation;
use App\Models\User;
use Inertia\Inertia;
use Inertia\Response;
Expand All @@ -16,13 +15,15 @@ public function show(User $user, string $slug): Response
->where('slug', $slug)
->firstOrFail();

if (! $this->canViewPresentation($presentation)) {
if (! $presentation->canBeViewed) {
abort(403);
}

dispatch(function () use ($presentation) {
$presentation->addDailyView();
})->afterResponse();
if ($presentation->shouldTrackView) {
dispatch(function () use ($presentation) {
$presentation->addDailyView();
})->afterResponse();
}

return Inertia::render('Slides', [
'content' => $presentation->content,
Expand All @@ -33,21 +34,4 @@ public function show(User $user, string $slug): Response
],
]);
}

private function canViewPresentation(Presentation $presentation): bool
{
// If the presentation is published, then anyone can see it.
if ($presentation->is_published) {
return true;
}

// If the user is not logged in, then they can't see any draft
// presentations.
if (! auth()->check()) {
return false;
}

// Default to the normal view policy function
return auth()->user()->can('view', $presentation);
}
}
68 changes: 68 additions & 0 deletions app/Models/Presentation.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace App\Models;

use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
Expand Down Expand Up @@ -30,6 +31,73 @@ class Presentation extends Model implements HasMedia
'deleted_at',
];

/**
* Determine if this presentation be viewed, based on published status and
* the authenticated user.
*
* @return Attribute<bool, null>
*/
protected function canBeViewed(): Attribute
{
return Attribute::make(
get: function (mixed $value, array $attributes): bool {
// If the presentation is published, then anyone can see it.
if ($this->is_published) {
return true;
}

// If the user is not logged in, then they can't see any draft
// presentations.
if (! auth()->check()) {
return false;
}

// Default to the normal view policy function
return auth()->user()->can('view', $this);
},
);
}

/**
* Determine if this presentation should track a daily view, based on
* published status and the authenticated user.
*
* @return Attribute<bool, null>
*/
protected function shouldTrackView(): Attribute
{
return Attribute::make(
get: function (mixed $value, array $attributes): bool {
// If the presentation is not published, then a view should not
// be tracked.
if (! $this->is_published) {
return false;
}

// If the user is not logged in, then a view should be tracked.
if (! auth()->check()) {
return true;
}

// If the user is an admin, then a view should not be tracked.
if (auth()->user()->isAdministrator()) {
return false;
}

// If the user is the creator of the presentation, then a view
// should not be tracked.
if (auth()->id() === $this->user_id) {
return false;
}

// Otherwise, a user would be logged in, but not as an admin or
// the creator of the presentation, and thus should track a
// daily view. This would be a pretty rare case.
return true;
},
);
}

/**
* Scope a query to only include presentations for the authenticated user.
*
Expand Down
7 changes: 7 additions & 0 deletions resources/views/filament/dashboard/footer.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<x-filament::card>
<p>
<strong>Note:</strong> Views are only tracked on published presentations.
Additionally, if you are logged in and view one of your own
presentations, a view will <strong>not</strong> be tracked.
</p>
</x-filament::card>
Loading

0 comments on commit ffe9694

Please # to comment.