Skip to content
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

Rename read chapter to chapter member #1618

Merged
merged 8 commits into from
Mar 27, 2024
Merged
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
4 changes: 2 additions & 2 deletions app/Helpers/RatingHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@ public static function getCalculatedRating(): Collection
->defaultSort('name')
->allowedSorts($sort ?? 'name')
->withCount([
'readChapters',
'chapterMembers',
'exerciseMembers' => fn($query) => $query->finished(),
])
->get();
$calculatedRating = $users->map(fn(User $user) => [
'user' => $user,
'points' => PointsCalculator::calculate($user->read_chapters_count, $user->exercise_members_count),
'points' => PointsCalculator::calculate($user->chapter_members_count, $user->exercise_members_count),
])
->when(empty($sort), function ($collection) {
return $collection->sortByDesc('points');
Expand Down
2 changes: 1 addition & 1 deletion app/Http/Controllers/ActivityController.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class ActivityController extends Controller
{
public function index(): View
{
$logItems = Activity::with('causer')
$logItems = Activity::with('causer', 'subject')
->orderBy('created_at', 'DESC')
->paginate(15);

Expand Down
45 changes: 45 additions & 0 deletions app/Http/Controllers/Chapter/ChapterMemberController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

namespace App\Http\Controllers\Chapter;

use App\Http\Controllers\Controller;
use App\Models\Chapter;
use App\Models\ChapterMember;
use App\Models\User;
use App\Services\ActivityService;
use Flash;
use Illuminate\Http\RedirectResponse;

class ChapterMemberController extends Controller
{
public function finish(Chapter $chapter, ActivityService $activityService): RedirectResponse
{
$user = auth()->user();
$currentChapterMember = $this->getMember($user, $chapter);
$currentChapterMember->finish();
$currentChapterMember->save();

// TODO: add points for finishing
flash()->info(__('layout.flash.success'))->success();
$activityService->logChapterMemberFinished($currentChapterMember);

return back();
}

private function getMember(User $user, Chapter $chapter): ChapterMember
{
$currentChapterMember = $chapter
->members()
->whereUserId($user->id)
->firstOr(function () use ($chapter, $user): ChapterMember {
$chapterMember = new ChapterMember([]);

$chapterMember->user()->associate($user);
$chapterMember->chapter()->associate($chapter);

return $chapterMember;
});

return $currentChapterMember;
}
}
15 changes: 12 additions & 3 deletions app/Http/Controllers/ChapterController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace App\Http\Controllers;

use App\Models\Chapter;
use App\Models\ChapterMember;
use App\Models\User;
use Illuminate\View\View;

Expand All @@ -20,7 +21,6 @@ public function show(Chapter $chapter): View
$chapter->load([
'parent',
'children',
'users',
'exercises',
'comments',
]);
Expand All @@ -31,11 +31,20 @@ public function show(Chapter $chapter): View
$authUser = auth()->user() ?? new User();
$previousChapter = Chapter::whereId($chapter->id - 1)->firstOrNew([]);
$nextChapter = Chapter::whereId($chapter->id + 1)->firstOrNew([]);
$isCompletedChapter = $authUser->readChapters()->where('chapter_id', $chapter->id)->exists();
$currentChapterMember = $chapter
->members()
->whereUserId($authUser->id)->firstOr(function () use ($chapter, $authUser): ChapterMember {
$chapterMember = new ChapterMember([]);

$chapterMember->user()->associate($authUser);
$chapterMember->chapter()->associate($chapter);

return $chapterMember;
});

return view('chapter.show', compact(
'currentChapterMember',
'chapter',
'isCompletedChapter',
'authUser',
'previousChapter',
'nextChapter',
Expand Down
10 changes: 5 additions & 5 deletions app/Http/Controllers/HomeController.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
use App\Models\Comment;
use App\Models\ExerciseMember;
use App\Models\Exercise;
use App\Models\ReadChapter;
use App\Models\ChapterMember;
use App\Models\User;
use App\Services\PointsCalculator;
use Illuminate\Support\Carbon;
Expand Down Expand Up @@ -53,21 +53,21 @@ private function getStatisticTable(string $filter): array
$periodsForFilter = ['week', 'month'];

if (!in_array($filter, $periodsForFilter, true)) {
$countReadChapter = ReadChapter::count();
$countChapterMember = ChapterMember::count();
$countCompletedExercise = ExerciseMember::count();
$filter = 'all';
} else {
$subPeriod = "sub{$filter}";
$period = Carbon::today()->$subPeriod(1);

$countReadChapter = ReadChapter::where('created_at', '>', $period)->count();
$countChapterMember = ChapterMember::where('created_at', '>', $period)->count();
$countCompletedExercise = ExerciseMember::where('created_at', '>', $period)->count();
}

return [
'countReadChapter' => $countReadChapter,
'countChapterMember' => $countChapterMember,
'countCompletedExercise' => $countCompletedExercise,
'countPoints' => PointsCalculator::calculate($countReadChapter, $countCompletedExercise),
'countPoints' => PointsCalculator::calculate($countChapterMember, $countCompletedExercise),
'filterForQuery' => $filter,
];
}
Expand Down
4 changes: 3 additions & 1 deletion app/Http/Controllers/MyController.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,11 @@ public function __invoke(): View
{
/** @var User $user */
$user = Auth::user();
$user->load('readChapters', 'exerciseMembers');
$user->load('chapterMembers', 'exerciseMembers');

$chapters = Chapter::with('children', 'exercises')->get();
$mainChapters = $chapters->where('parent_id', null);
$chapterMembers = $user->chapterMembers->keyBy('chapter_id');
$exerciseMembers = $user->exerciseMembers->keyBy('exercise_id');
$savedSolutionsExercises = $user->solutions()
->versioned()
Expand All @@ -35,6 +36,7 @@ public function __invoke(): View
'user',
'chapters',
'mainChapters',
'chapterMembers',
'exerciseMembers',
'savedSolutionsExercises'
));
Expand Down
2 changes: 1 addition & 1 deletion app/Http/Controllers/Rating/ProgressController.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class ProgressController extends Controller
public function index(Request $request): View
{
$sortParams = $request->get('sort');
$nextChaptersParameterFromSort = RatingHelper::getParameterFromSorting('read_chapters_count', $sortParams);
$nextChaptersParameterFromSort = RatingHelper::getParameterFromSorting('chapter_members_count', $sortParams);
$nextExercisesParameterFromSort = RatingHelper::getParameterFromSorting('exercise_members_count', $sortParams);
$rating = getCalculatedRating();
$exercisesCount = Exercise::count();
Expand Down
48 changes: 0 additions & 48 deletions app/Http/Controllers/User/UserChapterController.php

This file was deleted.

2 changes: 1 addition & 1 deletion app/Http/Controllers/UserController.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public function show(User $user): View
$points = 0;
$userRatingPosition = 'N/A';
}
$user->load('readChapters', 'exerciseMembers');
$user->load('chapterMembers', 'exerciseMembers');
$chart = ChartHelper::getChart($user->id);
return view('user.show', compact(
'user',
Expand Down
4 changes: 2 additions & 2 deletions app/Models/Chapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ class Chapter extends Model
{
public const MARKABLE_COUNT = 101;

public function users(): BelongsToMany
public function members(): HasMany
{
return $this->belongsToMany(User::class, 'read_chapters');
return $this->hasMany(ChapterMember::class);
}

public function comments(): MorphMany
Expand Down
81 changes: 81 additions & 0 deletions app/Models/ChapterMember.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
<?php

namespace App\Models;

use AllowDynamicProperties;
use Database\Factories\ChapterMemberFactory;
use Iben\Statable\Statable;
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;

/**
* @method static ChapterMemberFactory factory(...$parameters)
* @property string $state
*/
class ChapterMember extends Model
{
use HasFactory;
use Statable;

public const INITIAL_STATE = 'started';

protected function getGraph(): string
{
return 'chapter_member';
}

protected function saveBeforeTransition(): bool
{
return true;
}

public function mayFinish(): bool
{
return $this->canApply('finish');
}

public function isFinished(): bool
{
return $this->state === 'finished';
}

public function isNotFinished(): bool
{
return !$this->isFinished();
}

public function finish(): void
{
$this->apply('finish');
}

public function isStarted(): bool
{
return $this->state === 'started';
}

public function scopeFinished(Builder $builder): Builder
{
return $builder->where('state', '=', 'finished');
}

public function user(): BelongsTo
{
return $this->belongsTo(User::class);
}

public function chapter(): BelongsTo
{
return $this->belongsTo(Chapter::class);
}

protected function state(): Attribute
{
return Attribute::make(
get:fn($state) => $state ?? self::INITIAL_STATE
);
}
}
26 changes: 0 additions & 26 deletions app/Models/ReadChapter.php

This file was deleted.

10 changes: 5 additions & 5 deletions app/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@
* @property-read int|null $exercise_members_count
* @property-read Collection|Exercise[] $exercises
* @property-read int|null $exercises_count
* @property-read Collection|ReadChapter[] $readChapters
* @property-read int|null $read_chapters_count
* @property-read Collection|ChapterMember[] $chapterMembers
* @property-read int|null $chapter_members_count
* @property-read Collection|Solution[] $solutions
* @property-read int|null $solutions_count
* @method static UserFactory factory(...$parameters)
Expand Down Expand Up @@ -80,12 +80,12 @@ class User extends Authenticatable implements MustVerifyEmail

public function chapters(): BelongsToMany
{
return $this->belongsToMany(Chapter::class, 'read_chapters')->withTimestamps();
return $this->belongsToMany(Chapter::class, 'chapter_members')->withTimestamps();
}

public function readChapters(): HasMany
public function chapterMembers(): HasMany
{
return $this->hasMany(ReadChapter::class);
return $this->hasMany(ChapterMember::class);
}

public function exerciseMembers(): HasMany
Expand Down
Loading