Skip to content

Commit

Permalink
added an algorithm to prioritize unseen and failed cards
Browse files Browse the repository at this point in the history
  • Loading branch information
WolfyWin committed Sep 7, 2023
1 parent 9cce7b8 commit fabe715
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 4 deletions.
3 changes: 3 additions & 0 deletions src/plugin/flashcard/Resources/config/services/serializer.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
services:
Claroline\FlashcardBundle\Serializer\FlashcardDeckSerializer:
tags: ['claroline.serializer']
arguments:
- '@Claroline\AppBundle\Persistence\ObjectManager'
- '@security.token_storage'

Claroline\FlashcardBundle\Serializer\UserProgressionSerializer:
tags: [ 'claroline.serializer' ]
Expand Down
38 changes: 34 additions & 4 deletions src/plugin/flashcard/Serializer/FlashcardDeckSerializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,27 @@
namespace Claroline\FlashcardBundle\Serializer;

use Claroline\AppBundle\API\Serializer\SerializerTrait;
use Claroline\AppBundle\Persistence\ObjectManager;
use Claroline\FlashcardBundle\Entity\Flashcard;
use Claroline\FlashcardBundle\Entity\FlashcardDeck;
use Claroline\FlashcardBundle\Entity\UserProgression;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;

class FlashcardDeckSerializer
{
use SerializerTrait;

private ObjectManager $om;
private TokenStorageInterface $tokenStorage;

public function __construct(
ObjectManager $om,
TokenStorageInterface $tokenStorage
) {
$this->om = $om;
$this->tokenStorage = $tokenStorage;
}

public function getClass(): string
{
return FlashcardDeck::class;
Expand Down Expand Up @@ -46,16 +60,32 @@ public function serialize(FlashcardDeck $flashcardDeck): array

private function serializeCards(FlashcardDeck $flashcardDeck): array
{
$user = $this->tokenStorage->getToken()->getUser();
$cards = $flashcardDeck->getCards();
$cardsData = [];
$unseenCards = [];
$failedCards = [];
$passedCards = [];

foreach ($cards as $card) {
$cardsData[] = $this->serializeCard($card);
$userProgression = $this->om->getRepository(UserProgression::class)->findOneBy([
'user' => $user,
'flashcard' => $card,
]);

if (!$userProgression) {
$unseenCards[] = $this->serializeCard($card);
} elseif (!$userProgression->isSuccessful()) {
$failedCards[] = $this->serializeCard($card);
} else {
$passedCards[] = $this->serializeCard($card);
}
}

shuffle($cardsData);
shuffle($unseenCards);
shuffle($failedCards);
shuffle($passedCards);

return $cardsData;
return array_merge($unseenCards, $failedCards, $passedCards);
}

public function serializeCard(Flashcard $flashcard): array
Expand Down

0 comments on commit fabe715

Please sign in to comment.