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

#628 progress default page changed #1590

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
16 changes: 16 additions & 0 deletions app/Helpers/ChapterHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,20 @@ public static function getChapterOriginLinkForNumber(string $chapter): ?string

return $links[$chapter] ?? null;
}

public static function getChapterDescendants(Chapter $chapter): array
{
$descendants = [];
$children = $chapter->children;

foreach ($children as $child) {
if (!$child->children->isEmpty()) {
$descendants = [...$descendants, ...self::getChapterDescendants($child)];
} else {
$descendants[] = $child;
}
}

return $descendants;
}
}
17 changes: 15 additions & 2 deletions app/Http/Controllers/MyController.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,19 @@ public function __invoke(): View
$user->load('readChapters', 'exerciseMembers');

$chapters = Chapter::with('children', 'exercises')->get();
$mainChapters = $chapters->where('parent_id', null);
$mainChapters = $chapters->where('parent_id', null)->sortBy('id');
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

давайте добавить scope для получения главных глав.
По сортировке- надежнее использовать сортировку по path. Тогда это будет по смыслу ближе к нашей предметной области. ведь у наших глав в книге нет айдишников, есть ее номера.


$firstUnfinishedChapter = 1;
foreach ($mainChapters as $mainChapter) {
if (!$user->haveReadMainChapter($mainChapter)) {
break;
}
$firstUnfinishedChapter += 1;
}
if ($firstUnfinishedChapter > $mainChapters->count()) {
$firstUnfinishedChapter = 1;
}

$exerciseMembers = $user->exerciseMembers->keyBy('exercise_id');
$savedSolutionsExercises = $user->solutions()
->versioned()
Expand All @@ -36,7 +48,8 @@ public function __invoke(): View
'chapters',
'mainChapters',
'exerciseMembers',
'savedSolutionsExercises'
'savedSolutionsExercises',
'firstUnfinishedChapter',
));
}
}
14 changes: 14 additions & 0 deletions app/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Models;

use App\Helpers\ChapterHelper;
use App\Presenters\UserPresenter;
use Database\Factories\UserFactory;
use Hemp\Presenter\Presentable;
Expand Down Expand Up @@ -127,4 +128,17 @@ public function haveRead(Chapter $chapter): bool
{
return $this->chapters->contains($chapter);
}

public function haveReadMainChapter(Chapter $chapter): bool
{
$subChapters = ChapterHelper::getChapterDescendants($chapter);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

На мой взгляд неправильно прокидывать в модель хелпер.
Хелперы служат для удобного доступа к некоторым данным, чтобы не дулировать код. Чаще всего они используются во вьюхах, контроллерах. Но на самом деле некоторые из них уже содержат код, который можно сказать принадлежит нашей предметной области и пора это добро закинуть в в какой-то сервис что ли.


foreach ($subChapters as $subChapter) {
if (!$this->haveRead($subChapter)) {
return false;
}
}

return true;
}
}
4 changes: 2 additions & 2 deletions resources/views/my/progresses/_my_chapters.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<div class="col-12 col-md-4 border-end x-z-index-0">
<div class="nav nav-pills flex-column sticky-top m-2 pt-2" role="tablist" aria-orientation="vertical">
@foreach ($mainChapters as $mainChapter)
<button class="nav-link text-start {{ $mainChapter->path === '1' ? 'active' : '' }}"
<button class="nav-link text-start {{ $mainChapter->path === (string) $firstUnfinishedChapter ? 'active' : '' }}"
id="subChapters{{ $mainChapter->id }}-tab" data-bs-target="#subChapters{{ $mainChapter->id }}"
data-bs-toggle="tab" type="button" role="tab" aria-controls="subChapters{{ $mainChapter->id }}"
aria-selected="{{ $mainChapter->path === '1' ? 'true' : 'false' }}">
Expand All @@ -15,7 +15,7 @@
{{ BsForm::open(route('users.chapters.store', [$user])) }}
<div class="tab-content m-2 m-lg-4">
@foreach ($mainChapters as $mainChapter)
<div class="tab-pane fade {{ $mainChapter->path === '1' ? 'show active' : '' }}"
<div class="tab-pane fade {{ $mainChapter->path === (string) $firstUnfinishedChapter ? 'show active' : '' }}"
id="subChapters{{ $mainChapter->id }}" role="tabpanel"
aria-labelledby="subChapters{{ $mainChapter->id }}-tab">
@include('partials.chapter_form_element', ['chapter' => $mainChapter])
Expand Down