Skip to content

Commit

Permalink
add calculating for points
Browse files Browse the repository at this point in the history
  • Loading branch information
fey committed Mar 27, 2024
1 parent aed673a commit 3e8356b
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 2 deletions.
4 changes: 2 additions & 2 deletions app/Http/Controllers/Chapter/ChapterMemberController.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use App\Models\ChapterMember;
use App\Models\User;
use App\Services\ActivityService;
use Flash;
use DB;
use Illuminate\Http\RedirectResponse;

class ChapterMemberController extends Controller
Expand All @@ -18,8 +18,8 @@ public function finish(Chapter $chapter, ActivityService $activityService): Redi
$currentChapterMember = $this->getMember($user, $chapter);
$currentChapterMember->finish();
$currentChapterMember->save();
$user->increment('points');

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

Expand Down
3 changes: 3 additions & 0 deletions app/Services/ExerciseService.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@

class ExerciseService
{
private const POINTS_PER_EXERCISE = 3;

public function __construct(private SolutionChecker $checker, private ActivityService $activityService)
{
}
Expand Down Expand Up @@ -46,6 +48,7 @@ public function check(User $user, Exercise $exercise, string $solutionCode): Che
if ($checkResult->isSuccess() && $user->isRegistered()) {
if ($exerciseMember->mayFinish()) {
$exerciseMember->finish();
$user->increment('points', self::POINTS_PER_EXERCISE);
$exerciseMember->save();

$this->activityService->logCompletedExercise($user, $exercise);
Expand Down
32 changes: 32 additions & 0 deletions database/migrations/2024_03_27_174229_add_points_to_user.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

use App\Models\User;
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Query\JoinClause;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::table('users', function (Blueprint $table) {
$table->addColumn('integer', 'points')->default(0);
});
DB::statement("UPDATE users SET points = ((SELECT count(user_id) FROM exercise_members em WHERE em.user_id = users.id) * 3);");
DB::statement("UPDATE users SET points = points + (SELECT count(user_id) FROM chapter_members em WHERE em.user_id = users.id);");
}

/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::table('users', function (Blueprint $table) {
$table->dropColumn('points');
});
}
};

0 comments on commit 3e8356b

Please sign in to comment.