From 64a463880b5f06a1d509e75f32160a6e3971f77b Mon Sep 17 00:00:00 2001 From: Nikolay Gagarinov Date: Tue, 26 Mar 2024 23:08:34 +0500 Subject: [PATCH] add activity for finish chapter member --- app/Http/Controllers/ActivityController.php | 2 +- .../Chapter/ChapterMemberController.php | 4 +- .../User/UserChapterController.php | 46 ------------- app/Services/ActivityService.php | 65 ++----------------- database/factories/ReadChapterFactory.php | 13 ++-- ...y_log_event_to_multiple_chapters_added.php | 15 +++++ resources/views/home/index.blade.php | 2 +- .../Controllers/ActivityControllerTest.php | 34 +--------- .../Chapter/ChapterMemberControllerTest.php | 51 +++++++++++++++ .../User/UserChapterControllerTest.php | 62 ------------------ 10 files changed, 88 insertions(+), 206 deletions(-) delete mode 100644 app/Http/Controllers/User/UserChapterController.php create mode 100644 database/migrations/2024_03_26_174007_rename_added_activity_log_event_to_multiple_chapters_added.php create mode 100644 tests/Feature/Http/Controllers/Chapter/ChapterMemberControllerTest.php delete mode 100644 tests/Feature/Http/Controllers/User/UserChapterControllerTest.php diff --git a/app/Http/Controllers/ActivityController.php b/app/Http/Controllers/ActivityController.php index d631fc570..3808a04fd 100644 --- a/app/Http/Controllers/ActivityController.php +++ b/app/Http/Controllers/ActivityController.php @@ -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); diff --git a/app/Http/Controllers/Chapter/ChapterMemberController.php b/app/Http/Controllers/Chapter/ChapterMemberController.php index 8e9df8728..23cb09164 100644 --- a/app/Http/Controllers/Chapter/ChapterMemberController.php +++ b/app/Http/Controllers/Chapter/ChapterMemberController.php @@ -6,11 +6,12 @@ use App\Models\Chapter; use App\Models\ChapterMember; use App\Models\User; +use App\Services\ActivityService; use Flash; class ChapterMemberController extends Controller { - public function finish(Chapter $chapter) + public function finish(Chapter $chapter, ActivityService $activityService) { $user = auth()->user(); $currentChapterMember = $this->getMember($user, $chapter); @@ -19,6 +20,7 @@ public function finish(Chapter $chapter) // TODO: add points for finishing flash()->info(__('layout.flash.success'))->success(); + $activityService->logChapterMemberFinished($currentChapterMember); return back(); } diff --git a/app/Http/Controllers/User/UserChapterController.php b/app/Http/Controllers/User/UserChapterController.php deleted file mode 100644 index 02106ae0a..000000000 --- a/app/Http/Controllers/User/UserChapterController.php +++ /dev/null @@ -1,46 +0,0 @@ -middleware('auth'); - $this->activityService = $activityService; - } - - public function store(SaveChapterRequest $request, User $user): RedirectResponse - { - $userChaptersOld = $user->chapters()->pluck('path'); - $user->chapters()->sync($request->get('chapters_id', [])); - $userChaptersNew = $user->chapters()->pluck('path'); - - $chaptersMembers = $user->chapterMembers()->whereIn('id', $user->chapters()->pluck('chapter_members.id')); - - $chaptersMembers->each(function (ChapterMember $chapterMember) { - $chapterMember->finish(); - $chapterMember->save(); - }); - - $this->activityService->logChangedUserChapters( - $user, - $userChaptersOld, - $userChaptersNew - ); - - flash()->success(__('layout.flash.success')); - - return redirect()->route('my'); - } -} diff --git a/app/Services/ActivityService.php b/app/Services/ActivityService.php index a1e3af065..e4873c139 100644 --- a/app/Services/ActivityService.php +++ b/app/Services/ActivityService.php @@ -4,11 +4,11 @@ use App\Models\Activity; use App\Models\Chapter; +use App\Models\ChapterMember; use App\Models\Comment; use App\Models\Exercise; use App\Models\Solution; use App\Models\User; -use Illuminate\Database\Eloquent\Model; use Illuminate\Support\Collection; class ActivityService @@ -18,6 +18,8 @@ class ActivityService public const ACTIVITY_EXERCISE_COMPLETED = 'completed_exercise'; public const ACTIVITY_SOLUTION_ADDED = 'add_solution'; public const ACTIVITY_CHAPTER_ADDED = 'added'; + public const ACTIVITY_MULTIPLE_CHAPTERS_ADDED = 'multiple_chapters_added'; + public const ACTIVITY_CHAPTER_MEMBER_FINISHED = 'chapter_member_finished'; public const COMMENTED = 'commented'; public function logAddedSolution(User $user, Solution $solution): void @@ -43,50 +45,12 @@ public function logCompletedExercise(User $user, Exercise $exercise): void ->log(self::ACTIVITY_EXERCISE_COMPLETED); } - public function logRemovedExercise(User $user, Exercise $exercise): void + public function logChapterMemberFinished(ChapterMember $chapterMember): void { activity() - ->performedOn($exercise) - ->causedBy($user) - ->withProperties(['exercise_id' => $exercise->id]) - ->log(self::ACTIVITY_EXERCISE_REMOVED); - } - - public function logChangedUserChapters( - User $user, - Collection $userChaptersOld, - Collection $userChaptersNew - ): void { - [$log, $chapters] = $this->calculateChaptersDiff($userChaptersOld, $userChaptersNew); - - if ($log) { - $properties = [ - 'chapters' => $chapters, - 'count' => count($chapters), - 'url' => route('users.show', $user), - ]; - activity() - ->performedOn($user) - ->causedBy($user) - ->withProperties($properties) - ->log($log); - } - } - - public function logRemovedUserChapter(User $user, Chapter $chapter): void - { - $properties = [ - 'chapters' => [$chapter->path], - 'count' => 1, - 'url' => route('users.show', $user), - ]; - - activity() - ->performedOn($user) - ->causedBy($user) - ->withProperties( - $properties - )->log(self::ACTIVITY_CHAPTER_REMOVED); + ->performedOn($chapterMember) + ->causedBy($chapterMember->user) + ->log(self::ACTIVITY_CHAPTER_MEMBER_FINISHED); } public function logCreatedComment(User $user, Comment $comment): void @@ -116,19 +80,4 @@ private function getCreatedCommentActivityProperties(Comment $comment): array 'url' => $comment->present()->getLink(), ]; } - - private function calculateChaptersDiff(Collection $chaptersOld, Collection $chaptersNew): array - { - $chapters = $chaptersNew->diff($chaptersOld); - if (count($chapters)) { - return [self::ACTIVITY_CHAPTER_ADDED, $chapters]; - } - - $chapters = $chaptersOld->diff($chaptersNew); - if (count($chapters)) { - return [self::ACTIVITY_CHAPTER_REMOVED, $chapters]; - } - - return ['', []]; - } } diff --git a/database/factories/ReadChapterFactory.php b/database/factories/ReadChapterFactory.php index 11642d402..a090f70f4 100644 --- a/database/factories/ReadChapterFactory.php +++ b/database/factories/ReadChapterFactory.php @@ -39,12 +39,13 @@ public function configure(): self { return $this->afterCreating(function (ChapterMember $chapterMember) { /** @var ActivityService $service */ - $service = app()->make(ActivityService::class); - $service->logChangedUserChapters( - $chapterMember->user, - collect($chapterMember->chapter->path), - collect() - ); + // TODO: add logging + // $service = app()->make(ActivityService::class); + // $service->logChangedUserChapters( + // $chapterMember->user, + // collect($chapterMember->chapter->path), + // collect() + // ); }); } } diff --git a/database/migrations/2024_03_26_174007_rename_added_activity_log_event_to_multiple_chapters_added.php b/database/migrations/2024_03_26_174007_rename_added_activity_log_event_to_multiple_chapters_added.php new file mode 100644 index 000000000..60b1912e9 --- /dev/null +++ b/database/migrations/2024_03_26_174007_rename_added_activity_log_event_to_multiple_chapters_added.php @@ -0,0 +1,15 @@ +update([ + 'description' => ActivityService::ACTIVITY_MULTIPLE_CHAPTERS_ADDED, + ]); + } +}; diff --git a/resources/views/home/index.blade.php b/resources/views/home/index.blade.php index 9fc9c8a15..59ca31f12 100644 --- a/resources/views/home/index.blade.php +++ b/resources/views/home/index.blade.php @@ -130,7 +130,7 @@ @break @case('removed') - @case('added') + @case('multiple_chapters_added')