Skip to content

Commit

Permalink
[Quiz] fixes papers delete
Browse files Browse the repository at this point in the history
  • Loading branch information
Elorfin committed Oct 11, 2023
1 parent 0c5445d commit 6d491a1
Show file tree
Hide file tree
Showing 17 changed files with 72 additions and 213 deletions.
14 changes: 14 additions & 0 deletions src/main/evaluation/Subscriber/ResourceEvaluationSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@

use Claroline\AppBundle\API\Crud;
use Claroline\AppBundle\Event\Crud\CreateEvent;
use Claroline\AppBundle\Event\Crud\DeleteEvent;
use Claroline\AppBundle\Event\Crud\UpdateEvent;
use Claroline\AppBundle\Persistence\ObjectManager;
use Claroline\CommunityBundle\Repository\UserRepository;
use Claroline\CoreBundle\Entity\Resource\ResourceEvaluation;
use Claroline\CoreBundle\Entity\Resource\ResourceNode;
use Claroline\CoreBundle\Entity\User;
use Claroline\CoreBundle\Event\CatalogEvents\ResourceEvents;
Expand Down Expand Up @@ -44,6 +46,7 @@ public static function getSubscribedEvents(): array
Crud::getEventName('create', 'post', ResourceNode::class) => 'createEvaluations',
Crud::getEventName('update', 'post', ResourceNode::class) => 'updateEvaluations',
ResourceEvents::RESOURCE_OPEN => ['open', 10],
Crud::getEventName('delete', 'pre', ResourceEvaluation::class) => 'updateNbAttempts',
];
}

Expand Down Expand Up @@ -103,4 +106,15 @@ public function open(LoadResourceEvent $event)
);
}
}

public function updateNbAttempts(DeleteEvent $event): void
{
/** @var ResourceEvaluation $resourceAttempt */
$resourceAttempt = $event->getObject();

$evaluation = $resourceAttempt->getResourceUserEvaluation();
if ($evaluation) {
$evaluation->setNbAttempts($evaluation->getNbAttempts() - 1);
}
}
}
24 changes: 9 additions & 15 deletions src/plugin/exo/Entity/Exercise.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

/**
* @ORM\Entity(repositoryClass="UJM\ExoBundle\Repository\ExerciseRepository")
*
* @ORM\Table(name="ujm_exercise")
*/
class Exercise extends AbstractResource
Expand Down Expand Up @@ -236,8 +237,10 @@ class Exercise extends AbstractResource
* @ORM\Column(name="max_papers", type="integer")
*
* @var int
*
* @todo to remove. not implemented.
*/
private $maxPapers = 0;
public $maxPapers = 0;

/**
* Use all papers to compute stats.
Expand Down Expand Up @@ -289,6 +292,7 @@ class Exercise extends AbstractResource

/**
* @ORM\OneToMany(targetEntity="Step", mappedBy="exercise", cascade={"all"}, orphanRemoval=true)
*
* @ORM\OrderBy({"order" = "ASC"})
*
* @var ArrayCollection|Step[]
Expand All @@ -313,7 +317,7 @@ public function getCorrectionMode(): string
return $this->correctionMode;
}

public function setDateCorrection(?\DateTimeInterface $dateCorrection = null): void
public function setDateCorrection(\DateTimeInterface $dateCorrection = null): void
{
$this->dateCorrection = $dateCorrection;
}
Expand Down Expand Up @@ -378,12 +382,12 @@ public function getIntermediateScores(): ?string
return $this->intermediateScores;
}

public function setIntermediateScores(?string $intermediateScores = null): void
public function setIntermediateScores(string $intermediateScores = null): void
{
$this->intermediateScores = $intermediateScores;
}

public function setAttemptsReachedMessage(?string $attemptsReachedMessage = null): void
public function setAttemptsReachedMessage(string $attemptsReachedMessage = null): void
{
$this->attemptsReachedMessage = $attemptsReachedMessage;
}
Expand Down Expand Up @@ -543,7 +547,7 @@ public function setScoreRule(?string $scoreRule): void
$this->scoreRule = $scoreRule;
}

public function setSuccessScore(?float $successScore = null): void
public function setSuccessScore(float $successScore = null): void
{
$this->successScore = $successScore;
}
Expand Down Expand Up @@ -603,16 +607,6 @@ public function getPicking(): string
return $this->picking;
}

public function setMaxPapers(?int $maxPapers = null): void
{
$this->maxPapers = $maxPapers;
}

public function getMaxPapers(): ?int
{
return $this->maxPapers;
}

public function isAllPapersStatistics(): bool
{
return $this->allPapersStatistics;
Expand Down
29 changes: 3 additions & 26 deletions src/plugin/exo/Library/Model/AttemptParametersTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,10 @@ trait AttemptParametersTrait
* @ORM\Column(name="max_day_attempts", type="integer")
*
* @var int
*
* @todo not implemented, to remove
*/
private $maxAttemptsPerDay = 0;
public $maxAttemptsPerDay = 0;

/**
* Sets random order.
Expand Down Expand Up @@ -170,29 +172,4 @@ public function getMaxAttempts()
{
return $this->maxAttempts;
}

/**
* Sets max attempts.
*
* @param int $maxAttemptsPerDay
*/
public function setMaxAttemptsPerDay($maxAttemptsPerDay)
{
if ($maxAttemptsPerDay > $this->maxAttempts) {
//we can't try more times per day than the maximum allowed attempts defined
$this->maxAttemptsPerDay = $this->maxAttempts;
}

$this->maxAttemptsPerDay = $maxAttemptsPerDay;
}

/**
* Gets max attempts.
*
* @return int
*/
public function getMaxAttemptsPerDay()
{
return $this->maxAttemptsPerDay;
}
}
8 changes: 0 additions & 8 deletions src/plugin/exo/Listener/Resource/ExerciseListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,14 +88,10 @@ public function onLoad(LoadResourceEvent $event)

// fetch additional user data
$lastAttempt = null;
$nbUserPapers = 0;
//$nbUserPapersDayCount = 0;
$userEvaluation = null;
if ($currentUser instanceof User) {
$lastAttempt = $this->attemptManager->getLastPaper($exercise, $currentUser);

$nbUserPapers = (int) $this->paperManager->countUserFinishedPapers($exercise, $currentUser);
//$nbUserPapersDayCount = (int) $this->paperManager->countUserFinishedDayPapers($exercise, $currentUser);
$userEvaluation = $this->serializer->serialize(
$this->resourceEvalManager->getUserEvaluation($exercise->getResourceNode(), $currentUser),
[Options::SERIALIZE_MINIMAL]
Expand All @@ -104,12 +100,8 @@ public function onLoad(LoadResourceEvent $event)

$event->setData([
'quiz' => $this->serializer->serialize($exercise, $options),
//'paperCount' => (int) $this->paperManager->countExercisePapers($exercise),

// user data
'lastAttempt' => $lastAttempt ? $this->paperManager->serialize($lastAttempt) : null,
'userPaperCount' => $nbUserPapers,
//'userPaperDayCount' => $nbUserPapersDayCount,
'userEvaluation' => $userEvaluation,
]);
$event->stopPropagation();
Expand Down
23 changes: 13 additions & 10 deletions src/plugin/exo/Manager/Attempt/PaperManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace UJM\ExoBundle\Manager\Attempt;

use Claroline\AppBundle\API\Crud;
use Claroline\AppBundle\Persistence\ObjectManager;
use Claroline\CoreBundle\Entity\Resource\ResourceEvaluation;
use Claroline\CoreBundle\Entity\User;
Expand Down Expand Up @@ -36,6 +37,9 @@ class PaperManager
/** @var EventDispatcherInterface */
private $eventDispatcher;

/** @var Crud */
private $crud;

/** @var PaperSerializer */
private $serializer;

Expand All @@ -52,6 +56,7 @@ public function __construct(
AuthorizationCheckerInterface $authorization,
ObjectManager $om,
EventDispatcherInterface $eventDispatcher,
Crud $crud,
PaperSerializer $serializer,
ItemManager $itemManager,
ScoreManager $scoreManager,
Expand All @@ -61,6 +66,7 @@ public function __construct(
$this->om = $om;
$this->repository = $om->getRepository(Paper::class);
$this->eventDispatcher = $eventDispatcher;
$this->crud = $crud;
$this->serializer = $serializer;
$this->itemManager = $itemManager;
$this->scoreManager = $scoreManager;
Expand Down Expand Up @@ -201,7 +207,7 @@ public function calculateTotal(Paper $paper): ?float
/**
* Returns the papers for a given exercise, in a JSON format.
*/
public function serializeExercisePapers(Exercise $exercise, ?User $user = null): array
public function serializeExercisePapers(Exercise $exercise, User $user = null): array
{
if (!empty($user)) {
// Load papers for of a single user
Expand Down Expand Up @@ -229,7 +235,12 @@ public function serializeExercisePapers(Exercise $exercise, ?User $user = null):
public function delete(array $papers): void
{
foreach ($papers as $paper) {
$resourceAttempt = $this->repository->getPaperAttempt($paper);

$this->om->remove($paper);
if ($resourceAttempt) {
$this->crud->delete($resourceAttempt);
}
}

$this->om->flush();
Expand All @@ -243,14 +254,6 @@ public function countUserFinishedPapers(Exercise $exercise, User $user): int
return $this->repository->countUserFinishedPapers($exercise, $user);
}

/**
* Returns the number of finished papers already done by the user for a given exercise for the current day.
*/
public function countUserFinishedDayPapers(Exercise $exercise, User $user): int
{
return $this->repository->countUserFinishedDayPapers($exercise, $user);
}

/**
* Returns the number of papers already done for a given exercise.
*/
Expand Down Expand Up @@ -358,7 +361,7 @@ public function generateResourceEvaluation(Paper $paper, bool $finished): Resour
if (isset($structure['steps'])) {
foreach ($structure['steps'] as $step) {
$nbQuestions += count(array_filter($step['items'], function (array $item) {
/// only get answerable items
// / only get answerable items
return $this->itemManager->isQuestionType($item['type']);
}));
}
Expand Down
8 changes: 0 additions & 8 deletions src/plugin/exo/Manager/AttemptManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,14 +91,10 @@ public function __construct(
*
* Based on the maximum attempt allowed and the number of already done by the user.
*
* @param User $user
*
* @return bool
*/
public function canPass(Exercise $exercise, User $user = null)
{
// TODO : max attempts by day

$canPass = true;
if ($user) {
$max = $exercise->getMaxAttempts();
Expand All @@ -115,8 +111,6 @@ public function canPass(Exercise $exercise, User $user = null)

public function getErrors(Exercise $exercise, User $user = null)
{
// TODO : max attempts by day

$errors = [];
if ($user) {
$max = $exercise->getMaxAttempts();
Expand All @@ -140,8 +134,6 @@ public function getErrors(Exercise $exercise, User $user = null)
* A user can submit to a paper only if it is its own and the paper is not closed (= no end).
* ATTENTION : As is, anonymous have access to all the other anonymous Papers !!!
*
* @param User $user
*
* @return bool
*/
public function canUpdate(Paper $paper, User $user = null)
Expand Down
47 changes: 17 additions & 30 deletions src/plugin/exo/Repository/PaperRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,36 +54,6 @@ public function findUnfinishedPapers(Exercise $exercise, User $user)
->getResult();
}

/**
* Returns the unfinished papers of a user for a given exercise for the current day, if any.
*
* @return int
*/
public function countUserFinishedDayPapers(Exercise $exercise, User $user)
{
$today = new \DateTime();
$today->setTime(0, 0);
$tomorrow = clone $today;
$tomorrow->add(new \DateInterval('P1D'));

return (int) $this->getEntityManager()
->createQuery('
SELECT COUNT(p)
FROM UJM\ExoBundle\Entity\Attempt\Paper AS p
WHERE p.user = :user
AND p.exercise = :exercise
AND p.end >= :today
AND p.end <= :tomorrow
')
->setParameters([
'user' => $user,
'exercise' => $exercise,
'today' => $today,
'tomorrow' => $tomorrow,
])
->getSingleScalarResult();
}

/**
* Finds the score of a paper by summing the score of each answer.
*
Expand Down Expand Up @@ -124,6 +94,23 @@ public function isFullyEvaluated(Paper $paper)
->getSingleScalarResult();
}

/**
* Retrieve a Claroline attempt (ResourceEvaluation) from a paper.
*/
public function getPaperAttempt(Paper $paper)
{
return $this->getEntityManager()
->createQuery('
SELECT a
FROM Claroline\CoreBundle\Entity\Resource\ResourceEvaluation AS a
WHERE a.data LIKE :paperId
')
->setParameters([
'paperId' => '%'.$paper->getUuid().'%',
])
->getOneOrNullResult();
}

/**
* Returns the number of papers for an exercise.
*
Expand Down
1 change: 1 addition & 0 deletions src/plugin/exo/Resources/config/services/manager.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ services:
- '@security.authorization_checker'
- '@Claroline\AppBundle\Persistence\ObjectManager'
- '@event_dispatcher'
- '@Claroline\AppBundle\API\Crud'
- '@ujm_exo.serializer.paper'
- '@UJM\ExoBundle\Manager\Item\ItemManager'
- '@UJM\ExoBundle\Manager\Attempt\ScoreManager'
Expand Down
12 changes: 5 additions & 7 deletions src/plugin/exo/Resources/modules/quiz/player/selectors.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {createSelector} from 'reselect'
import get from 'lodash/get'
import uniq from 'lodash/uniq'

import {selectors as securitySelectors} from '#/main/app/security/store/selectors'
Expand Down Expand Up @@ -128,14 +129,12 @@ const maxAttempts = createSelector(
[paperParameters],
(parameters) => parameters.maxAttempts || 0
)
const maxAttemptsPerDay = createSelector(
[paperParameters],
(parameters) => parameters.maxAttemptsPerDay || 0
)

const hasMoreAttempts = createSelector(
[maxAttempts, maxAttemptsPerDay, playerSelectors.userPaperCount, playerSelectors.userPaperDayCount],
(maxAttempts, maxAttemptsPerDay, userPaperCount, userPaperDayCount) => (!maxAttempts || maxAttempts > userPaperCount) && (!maxAttemptsPerDay || maxAttemptsPerDay > userPaperDayCount)
[maxAttempts, quizSelectors.evaluation],
(maxAttempts, evaluation) => {
return !evaluation || !maxAttempts || maxAttempts > get(evaluation, 'nbAttempts')
}
)

const steps = createSelector(
Expand Down Expand Up @@ -311,7 +310,6 @@ export const select = {
progressionDisplayed,
hasMoreAttempts,
maxAttempts,
maxAttemptsPerDay,
attemptsReachedMessage,
items,
countItems,
Expand Down

0 comments on commit 6d491a1

Please sign in to comment.