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

Add filtering quiz attempts by course_id and course author #28

Merged
merged 1 commit into from
Sep 6, 2023
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
35 changes: 35 additions & 0 deletions src/Dtos/Criteria/QuizAttemptCriteriaDto.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
use EscolaLms\Core\Dtos\CriteriaDto as BaseCriteriaDto;
use EscolaLms\Core\Repositories\Criteria\Primitives\DateCriterion;
use EscolaLms\Core\Repositories\Criteria\Primitives\EqualCriterion;
use EscolaLms\TopicTypeGift\Enum\TopicTypeGiftPermissionEnum;
use EscolaLms\TopicTypeGift\Models\GiftQuiz;
use EscolaLms\TopicTypeGift\Repositories\Criterion\RawCriterion;
use Illuminate\Http\Request;
use Illuminate\Support\Collection;

Expand All @@ -32,6 +35,38 @@ public static function instantiateFromRequest(Request $request): self
$criteria->push(new DateCriterion('end_at', $request->get('date_to'), '<='));
}

if ($request->get('course_id')) {
$criteria->push(
new RawCriterion('EXISTS (SELECT tgg.id
FROM topic_gift_quizzes tgg
JOIN topics t ON tgg.id = t.topicable_id
JOIN lessons l ON t.lesson_id = l.id
WHERE t.topicable_type = ?
AND l.course_id = ?
AND topic_gift_quiz_attempts.topic_gift_quiz_id = tgg.id)',
[GiftQuiz::class, $request->get('course_id')]
)
);
}

if (
!$request->user()->can(TopicTypeGiftPermissionEnum::LIST_QUIZ_ATTEMPT) &&
$request->user()->can(TopicTypeGiftPermissionEnum::LIST_SELF_QUIZ_ATTEMPT)
) {
$criteria->push(
new RawCriterion('EXISTS (SELECT tgg.id
FROM topic_gift_quizzes tgg
JOIN topics t ON tgg.id = t.topicable_id
JOIN lessons l ON t.lesson_id = l.id
JOIN course_author ca ON l.course_id = ca.course_id
WHERE t.topicable_type = ?
AND ca.author_id = ?
AND topic_gift_quiz_attempts.topic_gift_quiz_id = tgg.id)',
[GiftQuiz::class, $request->user()->getKey()]
)
);
}

return new static($criteria);
}
}
3 changes: 3 additions & 0 deletions src/Enum/TopicTypeGiftPermissionEnum.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ class TopicTypeGiftPermissionEnum extends BasicEnum

public const UPDATE_QUIZ_ATTEMPT = 'quiz-attempt_update';

public const LIST_SELF_QUIZ_ATTEMPT = 'quiz-attempt_list-self';

public const READ_GIFT_QUIZ = 'gift-quiz_read';
public const UPDATE_GIFT_QUIZ = 'gift-quiz_update';
public const CREATE_GIFT_QUIZ_QUESTION = 'gift-quiz-question_create';
Expand All @@ -40,6 +42,7 @@ public static function tutorPermissions(): array
TopicTypeGiftPermissionEnum::UPDATE_GIFT_QUIZ_QUESTION,
TopicTypeGiftPermissionEnum::DELETE_GIFT_QUIZ_QUESTION,
TopicTypeGiftPermissionEnum::DELETE_GIFT_QUIZ_QUESTION,
TopicTypeGiftPermissionEnum::LIST_SELF_QUIZ_ATTEMPT,
];
}
}
10 changes: 9 additions & 1 deletion src/Http/Controllers/Swagger/QuizAttemptApiAdminSwagger.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,14 @@ interface QuizAttemptApiAdminSwagger
* ),
* ),
* @OA\Parameter(
* name="course_id",
* required=false,
* in="query",
* @OA\Schema(
* type="number",
* ),
* ),
* @OA\Parameter(
* name="user_id",
* required=false,
* in="query",
Expand All @@ -62,7 +70,7 @@ interface QuizAttemptApiAdminSwagger
* @OA\Property(
* property="data",
* type="array",
* @OA\Items(@OA\Schema(ref="#/components/schemas/QuizAttemptSimpleResource"))
* @OA\Items(ref="#/components/schemas/QuizAttemptSimpleResource")
* ),
* @OA\Property(
* property="message",
Expand Down
30 changes: 30 additions & 0 deletions src/Http/Resources/CourseSimpleResource.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace EscolaLms\TopicTypeGift\Http\Resources;

use Illuminate\Http\Resources\Json\JsonResource;

/**
* @OA\Schema(
* schema="GiftCourseSimpleResource",
* @OA\Property(
* property="id",
* type="number"
* ),
* @OA\Property(
* property="title",
* type="string"
* ),
* )
*
*/
class CourseSimpleResource extends JsonResource
{
public function toArray($request): array
{
return [
'id' => $this->id,
'title' => $this->title,
];
}
}
13 changes: 13 additions & 0 deletions src/Http/Resources/QuizAttemptSimpleResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,16 @@
* description="min pass score",
* type="number"
* ),
* @OA\Property(
* property="user",
* description="user",
* type="object"
* ),
* @OA\Property(
* property="course",
* description="course",
* type="object"
* ),
* )
*
*/
Expand All @@ -58,6 +68,7 @@ public function toArray($request): array
{
$maxScore = $this->giftQuiz->questions->sum('score');
$resultScore = $this->answers->sum('score');
$course = $this->giftQuiz?->topic?->lesson?->course;

return [
'id' => $this->id,
Expand All @@ -69,6 +80,8 @@ public function toArray($request): array
'min_pass_score' => $this->giftQuiz->min_pass_score,
'result_score' => $this->isEnded() ? $resultScore : null,
'is_ended' => $this->isEnded(),
'user' => UserSimpleResource::make($this->user),
'course' => $course ? CourseSimpleResource::make($course) : null,
];
}
}
42 changes: 42 additions & 0 deletions src/Http/Resources/UserSimpleResource.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

namespace EscolaLms\TopicTypeGift\Http\Resources;

use Illuminate\Http\Resources\Json\JsonResource;

/**
* @OA\Schema(
* schema="GiftUserSimpleResource",
* @OA\Property(
* property="id",
* description="id",
* type="number"
* ),
* @OA\Property(
* property="first_name",
* type="string"
* ),
* @OA\Property(
* property="last_name",
* type="string"
* ),
* @OA\Property(
* property="email",
* description="email",
* type="string"
* ),
* )
*
*/
class UserSimpleResource extends JsonResource
{
public function toArray($request): array
{
return [
'id' => $this->id,
'first_name' => $this->first_name,
'last_name' => $this->last_name,
'email' => $this->email,
];
}
}
5 changes: 4 additions & 1 deletion src/Policies/QuizAttemptPolicy.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ public function update(User $user, QuizAttempt $attempt): bool

public function list(User $user): bool
{
return $user->can(TopicTypeGiftPermissionEnum::LIST_QUIZ_ATTEMPT);
return $user->canAny([
TopicTypeGiftPermissionEnum::LIST_QUIZ_ATTEMPT,
TopicTypeGiftPermissionEnum::LIST_SELF_QUIZ_ATTEMPT,
]);
}
}
25 changes: 25 additions & 0 deletions src/Repositories/Criterion/RawCriterion.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace EscolaLms\TopicTypeGift\Repositories\Criterion;

use EscolaLms\Core\Repositories\Criteria\Criterion;
use Illuminate\Database\Eloquent\Builder;

class RawCriterion extends Criterion
{
private string $sql;
private array $bindings;

public function __construct(string $sql, array $bindings)
{
parent::__construct();

$this->sql = $sql;
$this->bindings = $bindings;
}

public function apply(Builder $query): Builder
{
return $query->whereRaw($this->sql, $this->bindings);
}
}
91 changes: 91 additions & 0 deletions tests/Api/Admin/AdminListQuizAttemptApiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
namespace EscolaLms\TopicTypeGift\Tests\Api\Admin;

use EscolaLms\Core\Tests\CreatesUsers;
use EscolaLms\Courses\Models\Course;
use EscolaLms\Courses\Models\Lesson;
use EscolaLms\Courses\Models\Topic;
use EscolaLms\Courses\Models\User;
use EscolaLms\TopicTypeGift\Database\Seeders\TopicTypeGiftPermissionSeeder;
use EscolaLms\TopicTypeGift\Models\AttemptAnswer;
use EscolaLms\TopicTypeGift\Models\GiftQuestion;
Expand Down Expand Up @@ -54,6 +58,54 @@ public function testAdminQuizAttemptListFiltering(): void
->assertJsonCount(3, 'data');
}

public function testAdminQuizAttemptListFilteringByCourse(): void
{
$course1 = Course::factory()->create();
$course2 = Course::factory()->create();

$lesson1 = Lesson::factory()->state(['course_id' => $course1->getKey()])->create();
$lesson2 = Lesson::factory()->state(['course_id' => $course2->getKey()])->create();

$quiz1 = GiftQuiz::factory()->create();
$quiz2 = GiftQuiz::factory()->create();
$quiz3 = GiftQuiz::factory()->create();

$topic1 = Topic::factory()->state(['lesson_id' => $lesson1->getKey()])->create();
$topic2 = Topic::factory()->state(['lesson_id' => $lesson2->getKey()])->create();
$topic3 = Topic::factory()->state(['lesson_id' => $lesson2->getKey()])->create();

$topic1->topicable()->associate($quiz1)->save();
$topic2->topicable()->associate($quiz2)->save();
$topic3->topicable()->associate($quiz3)->save();

QuizAttempt::factory()
->state(new Sequence(
['topic_gift_quiz_id' => $quiz1->getKey()], // course1
['topic_gift_quiz_id' => $quiz1->getKey()], // course1
['topic_gift_quiz_id' => $quiz2->getKey()], // course2
['topic_gift_quiz_id' => $quiz3->getKey()], // course2
['topic_gift_quiz_id' => $quiz3->getKey()], // course2
))
->count(5)
->create();

$this->actingAs($this->makeAdmin(), 'api')
->getJson('api/admin/quiz-attempts')
->assertOk()
->assertJsonCount(5, 'data');

$this->actingAs($this->makeAdmin(), 'api')
->getJson('api/admin/quiz-attempts?course_id=' . $course1->getKey())
->assertOk()
->assertJsonCount(2, 'data');


$this->actingAs($this->makeAdmin(), 'api')
->getJson('api/admin/quiz-attempts?course_id=' . $course2->getKey())
->assertOk()
->assertJsonCount(3, 'data');
}

public function testAdminQuizAttemptListSorting(): void
{
$student = $this->makeStudent();
Expand Down Expand Up @@ -166,4 +218,43 @@ public function testAdminQuizAttemptListSorting(): void
$this->assertTrue($response->json('data.0.id') === $attempt2->getKey());
$this->assertTrue($response->json('data.1.id') === $attempt1->getKey());
}

public function testTutorQuizAttemptList(): void
{
// tutor
$tutor = $this->makeInstructor();
$course = Course::factory()->create();
$course->authors()->sync($tutor);
$lesson = Lesson::factory()->state(['course_id' => $course->getKey()])->create();
$quiz = GiftQuiz::factory()->create();
$topic = Topic::factory()->state(['lesson_id' => $lesson->getKey()])->create();
$topic->topicable()->associate($quiz)->save();

// other
$otherTopic = Topic::factory()
->for(Lesson::factory()
->for(Course::factory()))
->create();

$otherTopic->lesson->course->authors()->sync($this->makeInstructor());
$otherQuiz = GiftQuiz::factory()->create();
$otherTopic->topicable()->associate($otherQuiz)->save();


QuizAttempt::factory()
->state(new Sequence(
['topic_gift_quiz_id' => $quiz->getKey()],
['topic_gift_quiz_id' => $quiz->getKey()],
['topic_gift_quiz_id' => $quiz->getKey()],
['topic_gift_quiz_id' => $otherQuiz->getKey()],
['topic_gift_quiz_id' => $otherQuiz->getKey()],
))
->count(5)
->create();

$this->actingAs($tutor, 'api')
->getJson('api/admin/quiz-attempts')
->assertOk()
->assertJsonCount(3, 'data');
}
}
Loading