Skip to content

Commit

Permalink
Merge pull request #1620 from fey/add-points
Browse files Browse the repository at this point in the history
Add points
  • Loading branch information
fey authored Mar 28, 2024
2 parents aed673a + d623b2d commit 324c03d
Show file tree
Hide file tree
Showing 23 changed files with 91 additions and 237 deletions.
46 changes: 0 additions & 46 deletions app/Helpers/RatingHelper.php

This file was deleted.

7 changes: 0 additions & 7 deletions app/Helpers/helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,3 @@ function getExerciseOriginLink(Exercise $exercise): ?string
return ExerciseHelper::getExerciseOriginLink($exercise);
}
}

if (!function_exists('getCalculatedRating')) {
function getCalculatedRating(): Collection
{
return RatingHelper::getCalculatedRating();
}
}
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
2 changes: 0 additions & 2 deletions app/Http/Controllers/HomeController.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
use App\Models\Exercise;
use App\Models\ChapterMember;
use App\Models\User;
use App\Services\PointsCalculator;
use Illuminate\Support\Carbon;
use Illuminate\View\View;
use Request;
Expand Down Expand Up @@ -67,7 +66,6 @@ private function getStatisticTable(string $filter): array
return [
'countChapterMember' => $countChapterMember,
'countCompletedExercise' => $countCompletedExercise,
'countPoints' => PointsCalculator::calculate($countChapterMember, $countCompletedExercise),
'filterForQuery' => $filter,
];
}
Expand Down
28 changes: 0 additions & 28 deletions app/Http/Controllers/Rating/ProgressController.php

This file was deleted.

17 changes: 17 additions & 0 deletions app/Http/Controllers/Rating/TopController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace App\Http\Controllers\Rating;

use App\Http\Controllers\Controller;
use App\Services\RatingCalculator;
use Illuminate\View\View;

class TopController extends Controller
{
public function index(RatingCalculator $ratingCalculator): View
{
$rating = $ratingCalculator->calculate();

return view('rating.top', compact('rating'));
}
}
16 changes: 0 additions & 16 deletions app/Http/Controllers/Rating/UserController.php

This file was deleted.

21 changes: 9 additions & 12 deletions app/Http/Controllers/UserController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,27 @@

use App\Helpers\ChartHelper;
use App\Models\User;
use App\Services\RatingCalculator;
use Illuminate\View\View;

class UserController extends Controller
{
public function show(User $user): View
public function show(User $user, RatingCalculator $ratingCalculator): View
{
$rating = getCalculatedRating();
$userRatingPosition = $rating
->search(function (array $ratingPosition) use ($user) {
['user' => $ratingUser] = $ratingPosition;
return $ratingUser->id === $user->id;
});

if ($userRatingPosition) {
['points' => $points] = $rating->get($userRatingPosition);
$rating = $ratingCalculator->calculate();
$userInRating = $rating->get($user->id);
if ($userInRating) {
$points = $userInRating->points;
$position = $userInRating->position;
} else {
$points = 0;
$userRatingPosition = 'N/A';
$position = 'N/A';
}
$user->load('chapterMembers', 'exerciseMembers');
$chart = ChartHelper::getChart($user->id);
return view('user.show', compact(
'user',
'userRatingPosition',
'position',
'points',
'chart'
));
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
11 changes: 0 additions & 11 deletions app/Services/PointsCalculator.php

This file was deleted.

20 changes: 20 additions & 0 deletions app/Services/RatingCalculator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace App\Services;

use App\Models\User;
use Illuminate\Support\Collection;

class RatingCalculator
{
public static function calculate(): Collection
{
$ratings = User::select('users.*')
->selectRaw('DENSE_RANK() OVER (ORDER BY points desc, created_at) AS position')
->where('points', '>', 0)
->limit(100)
->get()
->keyBy('id');
return $ratings;
}
}
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');
});
}
};
4 changes: 0 additions & 4 deletions resources/lang/en/rating.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,6 @@
'title' => 'Top commenters',
'description' => 'The sicp.hexlet.ru top commenters ranking',
],
'progress' => [
'title' => 'Top students',
'description' => 'The sicp.hexlet.ru top students ranking',
],
'positions' => 'Rank',
'user' => 'Username',
'number_of_points' => 'Points',
Expand Down
4 changes: 0 additions & 4 deletions resources/lang/ru/rating.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,6 @@
'title' => 'Вклад в сообщество',
'description' => 'Рейтиг участников платформы sicp.hexlet.ru по вкладу в сообщество',
],
'progress' => [
'title' => 'Прогресс',
'description' => 'Рейтиг участников платформы sicp.hexlet.ru в соответсвии с прогрессом изучения материалов книги СИКП',
],
'positions' => 'Позиция',
'user' => 'Пользователь',
'number_of_points' => 'Количество баллов',
Expand Down
4 changes: 0 additions & 4 deletions resources/views/home/index.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,6 @@
</ul>
<div class="container m-0 p-0 mt-2">
<div class="row m-0 p-0 text-center">
<div class="col mt-4">
<p>{{ __('welcome.statistic.table.count_points') }}:</p>
<h3>{{ $statisticTable['countPoints'] }}</h3>
</div>
<div class="col mt-4">
<p>{{ __('welcome.statistic.table.count_read_chapter') }}:</p>
<h3>{{ $statisticTable['countChapterMember'] }}</h3>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@
<a class="btn btn-light" href="{{ route('top.index') }}">
{{ __('rating.index.title') }}
</a>
<a class="btn btn-light" href="{{ route('progress_top.index') }}">
{{ __('rating.progress.title') }}
</a>
<a class="btn btn-light" href="{{ route('comments_top.index') }}">
{{ __('rating.comments.title') }}
</a>
Expand Down
2 changes: 1 addition & 1 deletion resources/views/rating/comments.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
@endsection
@section('content')
<div class="my-4">
@include('rating.menu')
@include('rating._menu')

<h1 class="h3">{{ __('rating.comments.title') }}</h1>

Expand Down
54 changes: 0 additions & 54 deletions resources/views/rating/progress.blade.php

This file was deleted.

Loading

0 comments on commit 324c03d

Please sign in to comment.