Skip to content
This repository was archived by the owner on Oct 15, 2025. It is now read-only.

Commit 6b54190

Browse files
committed
refactor(questions): Move pass rate logic to Question
1 parent 8b3e469 commit 6b54190

File tree

3 files changed

+40
-42
lines changed

3 files changed

+40
-42
lines changed

src/Entity/Question.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,44 @@ public function setSolutionVideo(?string $solution_video): static
176176
return $this;
177177
}
178178

179+
/**
180+
* Get the pass rate of the question.
181+
*
182+
* @return float the pass rate of the question
183+
*/
184+
public function getPassRate(): float
185+
{
186+
$totalAttemptCount = $this->getTotalAttemptCount();
187+
if (0 === $totalAttemptCount) {
188+
return 0;
189+
}
190+
191+
return round($this->getTotalSolvedCount() / $totalAttemptCount * 100, 2);
192+
}
193+
194+
/**
195+
* Get the total number of attempts made on the question.
196+
*
197+
* @return int the total number of attempts made on the question
198+
*/
199+
public function getTotalAttemptCount(): int
200+
{
201+
return $this->getSolutionEvents()->count();
202+
}
203+
204+
/**
205+
* Get the total number of times the question has been solved.
206+
*
207+
* @return int the total number of times the question has been solved
208+
*/
209+
public function getTotalSolvedCount(): int
210+
{
211+
return $this->getSolutionEvents()
212+
->filter(
213+
fn (SolutionEvent $solutionEvent) => SolutionEventStatus::Passed === $solutionEvent->getStatus()
214+
)->count();
215+
}
216+
179217
/**
180218
* @return Collection<int, SolutionEvent>
181219
*/

src/Twig/Components/Questions/Card.php

Lines changed: 1 addition & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -5,30 +5,13 @@
55
namespace App\Twig\Components\Questions;
66

77
use App\Entity\Question;
8-
use App\Entity\SolutionEvent;
9-
use App\Entity\SolutionEventStatus;
108
use Symfony\UX\TwigComponent\Attribute\AsTwigComponent;
119

1210
#[AsTwigComponent]
1311
final class Card
1412
{
1513
public Question $question;
1614

17-
/**
18-
* Get the pass rate of the question.
19-
*
20-
* @return float the pass rate of the question
21-
*/
22-
public function getPassRate(): float
23-
{
24-
$totalAttemptCount = $this->getTotalAttemptCount();
25-
if (0 === $totalAttemptCount) {
26-
return 0;
27-
}
28-
29-
return round($this->getTotalSolvedCount() / $totalAttemptCount * 100, 2);
30-
}
31-
3215
/**
3316
* Get the pass rate level of the question.
3417
*
@@ -38,35 +21,12 @@ public function getPassRate(): float
3821
*/
3922
public function getPassRateLevel(): string
4023
{
41-
$passRate = $this->getPassRate();
24+
$passRate = $this->question->getPassRate();
4225

4326
return match (true) {
4427
$passRate <= 40 => 'low',
4528
$passRate <= 70 => 'medium',
4629
default => 'high',
4730
};
4831
}
49-
50-
/**
51-
* Get the total number of attempts made on the question.
52-
*
53-
* @return int the total number of attempts made on the question
54-
*/
55-
private function getTotalAttemptCount(): int
56-
{
57-
return $this->question->getSolutionEvents()->count();
58-
}
59-
60-
/**
61-
* Get the total number of times the question has been solved.
62-
*
63-
* @return int the total number of times the question has been solved
64-
*/
65-
private function getTotalSolvedCount(): int
66-
{
67-
return $this->question->getSolutionEvents()
68-
->filter(
69-
fn (SolutionEvent $solutionEvent) => SolutionEventStatus::Passed === $solutionEvent->getStatus()
70-
)->count();
71-
}
7232
}

templates/components/Questions/Card.html.twig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
<div class="question-card__operations">
1313
<a role="button" class="btn btn-primary" href="{{ path('app_challenge', {id: question.id}) }}">進行測驗</a>
14-
<div class="question-card__pass-rate" data-pass-rate="{{ this.passRateLevel }}">通過率 {{ this.passRate }}%</div>
14+
<div class="question-card__pass-rate" data-pass-rate="{{ this.passRateLevel }}">通過率 {{ question.passRate }}%</div>
1515
</div>
1616

1717
<div class="question-card__background">

0 commit comments

Comments
 (0)