Skip to content

Feature/lar 188 mettre en place le systeme de badge #345

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
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
24 changes: 24 additions & 0 deletions app/Gamify/Badges/NewComerBadge.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

declare(strict_types=1);

namespace App\Gamify\Badges;

use QCod\Gamify\BadgeType;

final class NewComerBadge extends BadgeType
{
protected string $name = 'badges.new_comer.title';

protected string $description = 'badges.new_comer.description';

protected string $icon = 'new_comer.svg';

/**
* Check is user qualifies for badge
*/
public function qualifier($user): bool

Check failure on line 20 in app/Gamify/Badges/NewComerBadge.php

View workflow job for this annotation

GitHub Actions / phpstan

Method App\Gamify\Badges\NewComerBadge::qualifier() has parameter $user with no type specified.
{
return $user->created_at->diffInMonths(now()) < 6;
}
}
22 changes: 22 additions & 0 deletions app/Gamify/Badges/OneYearMemberBadge.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace App\Gamify\Badges;

use QCod\Gamify\BadgeType;

final class OneYearMemberBadge extends BadgeType
{
protected string $name = 'badges.one_year.title';
protected string $description = 'badges.one_year.description';

/**
* Check is user qualifies for badge
*
* @param $user
* @return bool
*/
public function qualifier($user): bool

Check failure on line 18 in app/Gamify/Badges/OneYearMemberBadge.php

View workflow job for this annotation

GitHub Actions / phpstan

Method App\Gamify\Badges\OneYearMemberBadge::qualifier() has parameter $user with no type specified.
{
return $user->created_at->diffInYears(now()) >= 1;
}
}
22 changes: 22 additions & 0 deletions app/Gamify/Badges/ThreeYearMemberBadge.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

declare(strict_types=1);

namespace App\Gamify\Badges;

use QCod\Gamify\BadgeType;

final class ThreeYearMemberBadge extends BadgeType
{
protected string $name = 'badges.three_year.title';

protected string $description = 'badges.three_year.description';

/**
* Check is user qualifies for badge
*/
public function qualifier($user): bool

Check failure on line 18 in app/Gamify/Badges/ThreeYearMemberBadge.php

View workflow job for this annotation

GitHub Actions / phpstan

Method App\Gamify\Badges\ThreeYearMemberBadge::qualifier() has parameter $user with no type specified.
{
return $user->created_at->diffInYears(now()) >= 3;
}
}
21 changes: 21 additions & 0 deletions app/Gamify/Badges/TwoYearMemberBadge.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace App\Gamify\Badges;

use QCod\Gamify\BadgeType;

final class TwoYearMemberBadge extends BadgeType
{
protected string $name = 'badges.two_year.title';
protected string $description = 'badges.two_year.description';

/**
* Check is user qualifies for badge
* @param $user
* @return bool
*/
public function qualifier($user): bool

Check failure on line 17 in app/Gamify/Badges/TwoYearMemberBadge.php

View workflow job for this annotation

GitHub Actions / phpstan

Method App\Gamify\Badges\TwoYearMemberBadge::qualifier() has parameter $user with no type specified.
{
return $user->created_at->diffInYears(now()) >= 2;
}
}
22 changes: 22 additions & 0 deletions app/Gamify/Badges/VeteranBadge.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace App\Gamify\Badges;

use QCod\Gamify\BadgeType;

final class VeteranBadge extends BadgeType
{
protected string $name = 'badges.veteran.title';
protected string $description = 'badges.veteran.description';

/**
* Check is user qualifies for badge
*
* @param $user
* @return bool
*/
public function qualifier($user): bool

Check failure on line 18 in app/Gamify/Badges/VeteranBadge.php

View workflow job for this annotation

GitHub Actions / phpstan

Method App\Gamify\Badges\VeteranBadge::qualifier() has parameter $user with no type specified.
{
return $user->created_at->diffInYears(now()) > 4;
}
}
32 changes: 32 additions & 0 deletions app/Livewire/Components/User/Badges.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

declare(strict_types=1);

namespace App\Livewire\Components\User;

use App\Models\User;
use Illuminate\Contracts\View\View;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Cache;
use Livewire\Attributes\Computed;
use Livewire\Component;

final class Badges extends Component
{
public User $user;

#[Computed]
public function badges(): Collection
{
return Cache::remember(
key: 'badges.'.$this->user->id,
ttl: now()->addDays(3),
callback: fn (): Collection => $this->user->badges
);
}

public function render(): View
{
return view('livewire.components.user.badges');
}
}
1 change: 1 addition & 0 deletions app/Livewire/Pages/Account/Profile.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace App\Livewire\Pages\Account;

use App\Gamify\Badges\NewComerBadge;
use App\Models\Article;
use App\Models\Discussion;
use App\Models\Thread;
Expand Down
2 changes: 1 addition & 1 deletion config/gamify.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
'channel_name' => 'user.reputation.',

// Badge model
'badge_model' => '\QCod\Gamify\Badge',
'badge_model' => \QCod\Gamify\Badge::class,

// Where all badges icon stored
'badge_icon_folder' => 'images/badges/',
Expand Down
34 changes: 34 additions & 0 deletions lang/en/pages/badge.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

declare(strict_types=1);

return [

'title' => 'My badges',

'new_comer' => [
'title' => 'New comer',
'description' => 'Member for less than 6 months',
],

'one_year' => [
'title' => 'One Year of Community',
'description' => 'Member for 1 year',
],

'two_year' => [
'title' => 'Two Years of Community',
'description' => 'Member for 2 years',
],

'three_year' => [
'title' => 'Three Years of Community',
'description' => 'Member for 3 years',
],

'veteran' => [
'title' => 'Veteran',
'description' => 'Community Veteran',
],

];
34 changes: 34 additions & 0 deletions lang/fr/pages/badge.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

declare(strict_types=1);

return [

'title' => 'Mes badges',

'new_comer' => [
'title' => 'Nouveau membre',
'description' => 'Membre depuis moins de 6 mois',
],

'one_year' => [
'title' => 'Un an de communauté',
'description' => 'Membre depuis 1 an',
],

'two_year' => [
'title' => 'deux ans de communauté',
'description' => 'Membre depuis 2 ans',
],

'three_year' => [
'title' => 'Trois ans de communauté',
'description' => 'Membre depuis 3 ans',
],

'veteran' => [
'title' => 'Veteran',
'description' => 'Vétéran de la Communauté',
]

];
34 changes: 34 additions & 0 deletions resources/views/livewire/components/user/badges.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<div>
<div class="mt-5 space-y-4">
@foreach ($this->badges as $badge)
<div wire:key="{{ $badge->id }}" class="rounded-xl p-5 bg-white transition duration-200 ease-in-out ring-1 ring-gray-200/50 dark:bg-gray-800 dark:ring-white/10 dark:hover:bg-white/10">
<div class="flex justify-between gap-3">
<div class="flex items-center gap-2 flex-1">
<div class="w-12 h-12 bg-gray-200 rounded-full flex items-center justify-center">
@if ($this->user->hasBadge())
<svg class="w-8 h-8 text-green-500" fill="currentColor" viewBox="0 0 20 20">
<path fill-rule="evenodd" d="M16.707 5.293a1 1 0 010 1.414l-8 8a1 1 0 01-1.414 0l-4-4a1 1 0 011.414-1.414L8 12.586l7.293-7.293a1 1 0 011.414 0z" clip-rule="evenodd"/>
</svg>
@else
<span class="text-gray-400 opacity-50">?</span>
@endif
</div>
<div>
<h3 class="font-semibold {{ $this->user->hasBadge() ? 'text-gray-900' : 'text-gray-400' }}">
{{ (new $badge)->name }}
</h3>
<p class="text-sm text-gray-500">
{{ (new $badge)->description }}
</p>
</div>
</div>
<div class="flex items-center gap-2">
@if (!$this->user->hasBadge())
<span class="text-xs text-gray-400">Not unlocked</span>
@endif
</div>
</div>
</div>
@endforeach
</div>
</div>
16 changes: 16 additions & 0 deletions resources/views/livewire/pages/account/dashboard.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,19 @@ class="relative"
{{ number_format($this->user->threads->count()) }}
</span>
</x-filament::tabs.item>

<x-filament::tabs.item
alpine-active="activeTab === 'badges'"
class="relative"
x-on:click="activeTab = 'badges'"
icon="untitledui-award-05"
data-slot="tab"
>
{{ __('pages/badge.title') }}
<span class="lg:absolute lg:right-5">
{{ number_format($this->user->badges->count()) }}
</span>
</x-filament::tabs.item>
</x-filament::tabs>
</div>
</div>
Expand All @@ -61,6 +74,9 @@ class="relative"
<div x-cloak x-show="activeTab === 'sujets'">
<livewire:components.user.threads />
</div>
<div x-cloak x-show="activeTab === 'badges'">
<livewire:components.user.badges :user="$this->user" />
</div>
</div>
</section>
</x-container>
Loading