Skip to content

Commit

Permalink
add activity for finish chapter member
Browse files Browse the repository at this point in the history
  • Loading branch information
fey committed Mar 26, 2024
1 parent 8380d07 commit 64a4638
Show file tree
Hide file tree
Showing 10 changed files with 88 additions and 206 deletions.
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
4 changes: 3 additions & 1 deletion app/Http/Controllers/Chapter/ChapterMemberController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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();
}
Expand Down
46 changes: 0 additions & 46 deletions app/Http/Controllers/User/UserChapterController.php

This file was deleted.

65 changes: 7 additions & 58 deletions app/Services/ActivityService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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 ['', []];
}
}
13 changes: 7 additions & 6 deletions database/factories/ReadChapterFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
// );
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

use App\Models\Activity;
use App\Services\ActivityService;
use Illuminate\Database\Migrations\Migration;

return new class extends Migration
{
public function up(): void
{
Activity::whereDescription(ActivityService::ACTIVITY_CHAPTER_ADDED)->update([
'description' => ActivityService::ACTIVITY_MULTIPLE_CHAPTERS_ADDED,
]);
}
};
2 changes: 1 addition & 1 deletion resources/views/home/index.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@
@break

@case('removed')
@case('added')
@case('multiple_chapters_added')
<div class="d-block">
<a data-bs-toggle="collapse" href="#collapseExp{{ $logItem->id }}" aria-expanded="false"
aria-controls="collapseExp{{ $logItem->id }}">
Expand Down
34 changes: 3 additions & 31 deletions tests/Feature/Http/Controllers/ActivityControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,38 +19,10 @@ protected function setUp(): void
$this->actingAs($this->user);
}

public function testStoreAddChapters(): void
public function testIndex(): void
{
$chapters = Chapter::limit(3)->get();
$response = $this->get(route('log.index'));

$response = $this->post(route('users.chapters.store', [$this->user]), [
'chapters_id' => $chapters->pluck('id')->toArray(),
]);

$response->assertSessionDoesntHaveErrors();
$response->assertRedirect();

$this->assertDatabaseHas('activity_log', [
'description' => ActivityService::ACTIVITY_CHAPTER_ADDED,
'causer_id' => $this->user->id,
]);
}

public function testStoreRemovedChapters(): void
{
$chapters = Chapter::limit(3)->get();
$this->user->chapters()->saveMany($chapters);

$response = $this->post(route('users.chapters.store', [$this->user->id]), [
'chapters_id' => [],
]);

$response->assertSessionDoesntHaveErrors();
$response->assertRedirect();

$this->assertDatabaseHas('activity_log', [
'description' => ActivityService::ACTIVITY_CHAPTER_REMOVED,
'causer_id' => $this->user->id,
]);
$response->assertOk();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

namespace Tests\Feature\Feature\Http\Controllers\Chapter;

use App\Models\Chapter;
use App\Models\ChapterMember;
use App\Models\User;
use App\Services\ActivityService;
use Database\Seeders\ChaptersTableSeeder;
use Tests\TestCase;

class ChapterMemberControllerTest extends TestCase
{
protected function setUp(): void
{
parent::setUp();
$this->seed([
ChaptersTableSeeder::class,
]);
}

public function testFinish(): void
{
/** @var User $user */
$user = User::factory()->create();
$chapter = Chapter::wherePath('1.1.1')->first();

$this->actingAs($user);

$response = $this->put(route('chapters.members.finish', $chapter));

$response->assertRedirect();
$response->assertSessionDoesntHaveErrors();

$user->refresh();

$chapterMember = ChapterMember::where([
'chapter_id' => $chapter->id,
'user_id' => $user->id,
])->firstOrFail();

$this->assertTrue($chapterMember->isFinished());
$this->assertDatabaseHas('activity_log', [
'description' => ActivityService::ACTIVITY_CHAPTER_MEMBER_FINISHED,
'subject_type' => $chapterMember::class,
'subject_id' => $chapterMember->id,
'causer_id' => $user->id,
'causer_type' => $user::class,
]);
}
}
62 changes: 0 additions & 62 deletions tests/Feature/Http/Controllers/User/UserChapterControllerTest.php

This file was deleted.

0 comments on commit 64a4638

Please sign in to comment.