Skip to content

Commit

Permalink
Merge pull request #95 from Tschucki/dev-v-0.0.3
Browse files Browse the repository at this point in the history
0.0.3
  • Loading branch information
Tschucki authored Jul 22, 2024
2 parents b1617d3 + 2089efa commit c7f4e01
Show file tree
Hide file tree
Showing 31 changed files with 2,053 additions and 1,196 deletions.
2 changes: 0 additions & 2 deletions app/Filament/Resources/MyPollResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
use Filament\Tables\Table;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\Log;
use Yepsua\Filament\Tables\Components\RatingColumn;

class MyPollResource extends Resource
{
Expand Down Expand Up @@ -175,7 +174,6 @@ public static function table(Table $table): Table
Tables\Columns\TextColumn::make('title')->label('Titel')->sortable()->searchable(),
Tables\Columns\TextColumn::make('answers_count')->counts('answers')->label('Anzahl Antworten')->sortable()->toggleable(),
Tables\Columns\TextColumn::make('participants_count')->counts('participants')->label('Anzahl Teilnehmer')->sortable()->toggleable(),
RatingColumn::make('rating')->state(fn (MyPoll $myPoll) => $myPoll->participants()->avg('rating'))->label('Bewertung')->toggleable(),
Tables\Columns\TextColumn::make('updated_at')->dateTime('d.m.Y H:i')->suffix(' Uhr')->label('Änderungsdatum')->sortable()->toggleable()->toggledHiddenByDefault(),
Tables\Columns\TextColumn::make('created_at')->dateTime('d.m.Y H:i')->suffix(' Uhr')->label('Erstelldatum')->sortable()->toggleable()->toggledHiddenByDefault(),
])
Expand Down
32 changes: 17 additions & 15 deletions app/Filament/Resources/PublicPollsResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
use Filament\Tables\Table;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\SoftDeletingScope;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Number;

class PublicPollsResource extends Resource
{
Expand Down Expand Up @@ -58,15 +60,15 @@ public static function table(Table $table): Table
Tables\Columns\TextColumn::make('user.name')->label('Ersteller')->prefix('Von: ')->state(fn (PublicPoll $publicPoll) => $publicPoll->not_anonymous ? $publicPoll->user->name : ''),
Tables\Columns\TextColumn::make('participants_count')->counts('participants')->prefix('Teilnehmer: ')->label('Teilnehmerzahl'),
Tables\Columns\TextColumn::make('within_target_group')->label('Innerhalb deiner Zielgruppe')->icon(function (PublicPoll $poll) {
return $poll->userIsWithinTargetGroup(\Auth::user()) ? 'heroicon-o-check-circle' : 'heroicon-o-x-circle';
return $poll->userIsWithinTargetGroup(Auth::user()) ? 'heroicon-o-check-circle' : 'heroicon-o-x-circle';
})->iconColor(function (PublicPoll $poll) {
return $poll->userIsWithinTargetGroup(\Auth::user()) ? 'success' : 'danger';
})->prefix('In Zielgruppe: ')->state(fn (PublicPoll $publicPoll) => $publicPoll->userIsWithinTargetGroup(\Auth::user()) ? 'Ja' : 'Nein'),
return $poll->userIsWithinTargetGroup(Auth::user()) ? 'success' : 'danger';
})->prefix('In Zielgruppe: ')->state(fn (PublicPoll $publicPoll) => $publicPoll->userIsWithinTargetGroup(Auth::user()) ? 'Ja' : 'Nein'),
Tables\Columns\TextColumn::make('participated')->label('Teilgenommen')->icon(function (PublicPoll $poll) {
return $poll->userParticipated(\Auth::user()) ? 'heroicon-o-check-circle' : 'heroicon-o-x-circle';
return $poll->userParticipated(Auth::user()) ? 'heroicon-o-check-circle' : 'heroicon-o-x-circle';
})->iconColor(function (PublicPoll $poll) {
return $poll->userParticipated(\Auth::user()) ? 'success' : 'danger';
})->state(fn (PublicPoll $poll) => $poll->userParticipated(\Auth::user()) ? 'Ja' : 'Nein')->prefix('Teilgenommen: '),
return $poll->userParticipated(Auth::user()) ? 'success' : 'danger';
})->state(fn (PublicPoll $poll) => $poll->userParticipated(Auth::user()) ? 'Ja' : 'Nein')->prefix('Teilgenommen: '),
]),
])
->filters([])
Expand All @@ -85,13 +87,13 @@ public static function table(Table $table): Table
]),
];
})->modalSubmitAction(false)->button()->label('Zielgruppe')->modalHeading('Zielgruppe'),
Tables\Actions\Action::make('results')->button()->label('Ergebnisse ansehen')->url(fn (PublicPoll $poll) => route('filament.pr0p0ll.resources.umfragen.results', ['record' => $poll]))->visible(fn (PublicPoll $poll) => $poll->resultsArePublic() || \Auth::user()?->isAdmin()),
Tables\Actions\Action::make('results')->button()->label('Ergebnisse ansehen')->url(fn (PublicPoll $poll) => route('filament.pr0p0ll.resources.umfragen.results', ['record' => $poll]))->visible(fn (PublicPoll $poll) => $poll->resultsArePublic() || Auth::user()?->isAdmin()),
Tables\Actions\Action::make('participate')
->icon('heroicon-o-plus-circle')
->button()
->label('Teilnehmen')
->url(fn (PublicPoll $publicPoll): string => route('filament.pr0p0ll.resources.umfragen.teilnehmen', ['record' => $publicPoll]))
->disabled(fn (PublicPoll $publicPoll) => $publicPoll->userParticipated(\Auth::user()) || ! $publicPoll->userIsWithinTargetGroup(\Auth::user()) || $publicPoll->hasEnded()),
->disabled(fn (PublicPoll $publicPoll) => $publicPoll->userParticipated(Auth::user()) || ! $publicPoll->userIsWithinTargetGroup(Auth::user()) || $publicPoll->hasEnded()),
])
->filters([
Tables\Filters\SelectFilter::make('in_target_group')
Expand All @@ -103,11 +105,11 @@ public static function table(Table $table): Table
->default(fn (Page $livewire) => $livewire->activeTab === 'offene-umfragen' ? '1' : null)
->query(function (Builder $query, $data) {
if ($data['value'] === '1') {
$pollIdsWithinTargetGroup = PublicPoll::all()->filter(fn ($poll) => $poll->userIsWithinTargetGroup(\Auth::user()) === true)->pluck('id');
$pollIdsWithinTargetGroup = PublicPoll::all()->filter(fn ($poll) => $poll->userIsWithinTargetGroup(Auth::user()) === true)->pluck('id');
$query->whereIn('id', $pollIdsWithinTargetGroup);
}
if ($data['value'] === '0') {
$pollIdsWithinTargetGroup = PublicPoll::all()->filter(fn ($poll) => $poll->userIsWithinTargetGroup(\Auth::user()) === false)->pluck('id');
$pollIdsWithinTargetGroup = PublicPoll::all()->filter(fn ($poll) => $poll->userIsWithinTargetGroup(Auth::user()) === false)->pluck('id');
$query->whereIn('id', $pollIdsWithinTargetGroup);
}
}),
Expand All @@ -134,7 +136,7 @@ public static function getNavigationBadge(): ?string
{
// TODO: Add Cache
$notParticipatedPolls = PublicPoll::whereDoesntHave('participants', static function (Builder $query) {
$query->where('participant_id', \Auth::id());
$query->where('participant_id', Auth::id());
})
->where('original_content_link', null)
->where('visible_to_public', true)
Expand All @@ -144,22 +146,22 @@ public static function getNavigationBadge(): ?string
->withoutGlobalScope(SoftDeletingScope::class)
->get();

$inTargetGroup = $notParticipatedPolls->filter(fn (PublicPoll $publicPoll) => $publicPoll->userIsWithinTargetGroup(\Auth::user()));
$inTargetGroup = $notParticipatedPolls->filter(fn (PublicPoll $publicPoll) => $publicPoll->userIsWithinTargetGroup(Auth::user()));

return \Number::abbreviate($inTargetGroup->count());
return Number::abbreviate($inTargetGroup->count());
}

public static function canViewResults(PublicPoll $poll): bool
{
if (\Auth::user()?->isAdmin()) {
if (Auth::user()?->isAdmin()) {
return true;
}

if ($poll->resultsArePublic()) {
return true;
}

if (\Auth::user()?->getKey() === $poll->user->getKey()) {
if (Auth::user()?->getKey() === $poll->user->getKey()) {
return true;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,17 @@ protected function getFormActions(): array
];
}

protected function getHeaderActions(): array
{
return [
Action::make('participate_header')
->label('Antworten abgeben')
->action(fn () => $this->participate()),

$this->getCancelFormAction(),
];
}

protected function getCancelFormAction(): Action
{
return $this->backAction();
Expand Down
31 changes: 16 additions & 15 deletions app/Filament/Widgets/StatsOverview.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,35 +13,36 @@
use Filament\Widgets\StatsOverviewWidget as BaseWidget;
use Filament\Widgets\StatsOverviewWidget\Stat;
use Flowframe\Trend\Trend;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Number;
use NjoguAmos\Plausible\Facades\Plausible;

class StatsOverview extends BaseWidget
{
protected function getStats(): array
{
if (\Cache::has('answers_count')) {
$answers = \Cache::get('answers_count');
if (Cache::has('answers_count')) {
$answers = Cache::get('answers_count');
} else {
$answers = Answer::count();
\Cache::put('answers_count', $answers, now()->addHours(12));
Cache::put('answers_count', $answers, now()->addHours(12));
}

if (\Cache::has('questions_count')) {
$questions = \Cache::get('questions_count');
if (Cache::has('questions_count')) {
$questions = Cache::get('questions_count');
} else {
$questions = Question::count();
\Cache::put('questions_count', $questions, now()->addHours(12));
Cache::put('questions_count', $questions, now()->addHours(12));
}

$answerTypeCounts = QuestionType::where('disabled', false)->get()->map(function (QuestionType $type) {

$cacheKey = $type->title.'answers_count';
if (\Cache::has($cacheKey)) {
$count = \Cache::get($cacheKey);
if (Cache::has($cacheKey)) {
$count = Cache::get($cacheKey);
} else {
$count = Answer::where('answerable_type', $type->answerType()->getMorphClass())->count();
\Cache::put($cacheKey, $count, now()->addHours(12));
Cache::put($cacheKey, $count, now()->addHours(12));
}

return Stat::make('Antworten '.$type->title, Number::format(number: $count, precision: 0, locale: 'de'));
Expand All @@ -64,8 +65,8 @@ protected function getStats(): array
$trends = [];
$trends['polls'] = Trend::query(Poll::where('approved', true))
->between(
start: now()->startOfYear(),
end: now()->endOfYear(),
start: now()->startOfMonth(),
end: now()->endOfMonth(),
)
->perMonth()
->count()
Expand All @@ -74,8 +75,8 @@ protected function getStats(): array

$trends['users'] = Trend::model(User::class)
->between(
start: now()->startOfYear(),
end: now()->endOfYear(),
start: now()->startOfMonth(),
end: now()->endOfMonth(),
)
->perMonth()
->count()
Expand All @@ -84,8 +85,8 @@ protected function getStats(): array

$trends['answers'] = Trend::model(Answer::class)
->between(
start: now()->startOfYear(),
end: now()->endOfYear(),
start: now()->startOfMonth(),
end: now()->endOfMonth(),
)
->perMonth()
->count()
Expand Down
13 changes: 12 additions & 1 deletion app/Jobs/SendPollAcceptedTelegramNotification.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@
use App\Models\Polls\Poll;
use App\Models\User;
use App\Notifications\Telegram\NewPollAvailableTelegramNotification;
use DragonCode\Contracts\Queue\ShouldBeUnique;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Notification;

class SendPollAcceptedTelegramNotification implements ShouldQueue
class SendPollAcceptedTelegramNotification implements ShouldQueue, ShouldBeUnique
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

Expand All @@ -41,4 +42,14 @@ public function handle(): void
{
Notification::route('telegram', config('services.telegram-bot-api.channel'))->notify(new NewPollAvailableTelegramNotification($this->poll));
}

public function uniqueId()
{
return $this->poll->getKey();
}

public function uniqueFor(): int
{
return 3600;
}
}
2 changes: 1 addition & 1 deletion app/Services/TargetGroupService.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ private static function baseQuery(array $aTargetGroupData): Builder
->when($aTargetGroupData['gender'], fn (Builder $query) => $query->where('gender', $aTargetGroupData['gender']))
->when($aTargetGroupData['nationality'], fn (Builder $query) => $query->whereIn('nationality', $aTargetGroupData['nationality']))
->when($aTargetGroupData['minAge'], fn (Builder $query) => $query->whereDate('birthday', '<=', Carbon::now()->subYears($aTargetGroupData['minAge'])))
->when($aTargetGroupData['maxAge'], fn (Builder $query) => $query->whereDate('birthday', '>=', Carbon::now()->subYears($aTargetGroupData['maxAge'])))
->when($aTargetGroupData['maxAge'], fn (Builder $query) => $query->whereDate('birthday', '>=', Carbon::now()->subYears($aTargetGroupData['maxAge'] + 1)))
->when($aTargetGroupData['region'], fn (Builder $query) => $query->whereIn('region', $aTargetGroupData['region']));

return $query;
Expand Down
6 changes: 3 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "laravel/laravel",
"type": "project",
"version": "0.0.2",
"version": "0.0.3",
"description": "The skeleton application for the Laravel framework.",
"keywords": [
"laravel",
Expand All @@ -16,7 +16,7 @@
"ext-sodium": "*",
"ext-zip": "*",
"cybercog/laravel-ban": "^4.9",
"filament/filament": "3.2.15",
"filament/filament": "^3.2",
"flowframe/laravel-trend": "^0.2.0",
"guzzlehttp/guzzle": "^7.2",
"inertiajs/inertia-laravel": "^0.6.11",
Expand All @@ -33,7 +33,7 @@
"tightenco/ziggy": "^1.8",
"tschucki/laravel-notification-channel-pr0gramm": "^0.0.1",
"widiu7omo/filament-bandel": "^2.0",
"yepsua/filament-rating-field": "^0.3.0"
"yepsua/filament-rating-field": "^0.4"
},
"require-dev": {
"barryvdh/laravel-debugbar": "^3.9",
Expand Down
Loading

0 comments on commit c7f4e01

Please sign in to comment.