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

Fix filtering of started courses #328

Merged
merged 3 commits into from
Jan 17, 2025
Merged
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
14 changes: 14 additions & 0 deletions .github/workflows/swagger.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,18 @@ jobs:
container:
image: escolalms/php:8.2

services:
mysql:
image: mysql:5.7
env:
MYSQL_ROOT_PASSWORD: root
MYSQL_DATABASE: database
MYSQL_PASSWORD: password
MYSQL_USER: username
ports:
- 33306:3306
options: --health-cmd="mysqladmin ping" --health-interval=10s --health-timeout=5s --health-retries=3

steps:
- name: Instantiate package
uses: actions/checkout@v2
Expand All @@ -35,6 +47,8 @@ jobs:

- name: Generate Swagger UI
uses: Legion2/swagger-ui-action@v1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
output: swagger-ui
spec-file: openapi.json
Expand Down
43 changes: 25 additions & 18 deletions src/Services/ProgressService.php
Original file line number Diff line number Diff line change
Expand Up @@ -225,24 +225,31 @@ private function filterForPlannedCourses(Builder $query, int $userId): Builder

private function filterForStartedCourses(Builder $query, int $userId): Builder
{
return $query
->whereExists(function (QueryBuilder $query) use ($userId) {
$query->select(DB::raw(1))
->from('topics')
->join('lessons', 'lessons.id', '=', 'topics.lesson_id')
->leftJoin('course_progress', function (JoinClause $join) use ($userId) {
$join->on('topics.id', '=', 'course_progress.topic_id')
->where('course_progress.user_id', $userId);
})
->whereColumn('lessons.course_id', 'courses.id')
->where('course_progress.status', ProgressStatus::IN_PROGRESS)
->orWhere(function (QueryBuilder $query) {
$query->groupBy('topic.id')
->selectRaw('COUNT(CASE WHEN status = ? THEN 1 END) as completed_count', [ProgressStatus::COMPLETE])
->selectRaw('COUNT(CASE WHEN status = ? THEN 1 END) as in_completed_count', [ProgressStatus::INCOMPLETE])
->havingRaw('completed_count > 0 AND in_completed_count > 0');
});
});
return $query->where(function (Builder $query) use ($userId) {
$query
->whereExists(function (QueryBuilder $query) use ($userId) {
$this->getCourseProgressQuery($query, $userId);
$query->where('course_progress.status', ProgressStatus::IN_PROGRESS);
})
->orWhereExists(function (QueryBuilder $query) use ($userId) {
$this->getCourseProgressQuery($query, $userId);
$query->whereIn('course_progress.status', [ProgressStatus::COMPLETE, ProgressStatus::INCOMPLETE])
->groupBy('lessons.course_id')
->havingRaw('COUNT(DISTINCT course_progress.status) = 2');
});
});
}

private function getCourseProgressQuery(QueryBuilder $query, int $userId): void
{
$query->select(DB::raw(1))
->from('topics')
->join('lessons', 'lessons.id', '=', 'topics.lesson_id')
->leftJoin('course_progress', function (JoinClause $join) use ($userId) {
$join->on('topics.id', '=', 'course_progress.topic_id')
->where('course_progress.user_id', $userId);
})
->whereColumn('lessons.course_id', 'courses.id');
}

private function filterForFinishedCourses(Builder $query, int $userId): Builder
Expand Down
30 changes: 29 additions & 1 deletion tests/APIs/CourseProgressApiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -377,9 +377,34 @@ public function test_show_progress_courses_paginated_filtered_started(): void
'status' => ProgressStatus::COMPLETE,
]);

$course3 = Course::factory()->create([
'status' => CourseStatusEnum::PUBLISHED,
'title' => 'B Course',
]);
$lesson3 = Lesson::factory()->create(['course_id' => $course2->getKey()]);
$topic4 = Topic::factory()->create(['lesson_id' => $lesson3->getKey(), 'active' => true]);
$topic5 = Topic::factory()->create(['lesson_id' => $lesson3->getKey(), 'active' => true]);

CourseProgress::factory()->create([
'user_id' => $user->getKey(),
'topic_id' => $topic4->getKey(),
'finished_at' => now(),
'seconds' => 50,
'status' => ProgressStatus::COMPLETE,
]);

CourseProgress::factory()->create([
'user_id' => $user->getKey(),
'topic_id' => $topic5->getKey(),
'finished_at' => now(),
'seconds' => 50,
'status' => ProgressStatus::INCOMPLETE,
]);


$user->courses()->save($course1); //Course A
$user->courses()->save($course2); //Course B
$user->courses()->save($course3); //Course C

$this->response = $this->actingAs($user, 'api')->json(
'GET',
Expand All @@ -389,9 +414,12 @@ public function test_show_progress_courses_paginated_filtered_started(): void
);

$this->response
->assertJsonCount(1, 'data')
->assertJsonCount(2, 'data')
->assertJsonFragment([
'title' => $course1->title,
])
->assertJsonFragment([
'title' => $course3->title,
]);
}

Expand Down
Loading