Skip to content

Commit

Permalink
[Quiz] fixes papers search by user
Browse files Browse the repository at this point in the history
  • Loading branch information
Elorfin committed Jan 20, 2021
1 parent 9aabab2 commit 48fdeec
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 59 deletions.
67 changes: 10 additions & 57 deletions src/plugin/exo/Controller/PaperController.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
use Claroline\AppBundle\Persistence\ObjectManager;
use Claroline\CoreBundle\Entity\User;
use Claroline\CoreBundle\Security\Collection\ResourceCollection;
use Claroline\CoreBundle\Validator\Exception\InvalidDataException;
use Sensio\Bundle\FrameworkExtraBundle\Configuration as EXT;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
Expand Down Expand Up @@ -47,15 +46,6 @@ class PaperController
/** @var ExerciseManager */
private $exerciseManager;

/**
* PaperController constructor.
*
* @param AuthorizationCheckerInterface $authorization
* @param ObjectManager $om
* @param FinderProvider $finder
* @param PaperManager $paperManager
* @param ExerciseManager $exerciseManager
*/
public function __construct(
AuthorizationCheckerInterface $authorization,
ObjectManager $om,
Expand All @@ -76,14 +66,8 @@ public function __construct(
*
* @Route("", name="exercise_paper_list", methods={"GET"})
* @EXT\ParamConverter("user", converter="current_user")
*
* @param Exercise $exercise
* @param User $user
* @param Request $request
*
* @return JsonResponse
*/
public function listAction(Exercise $exercise, User $user, Request $request)
public function listAction(Exercise $exercise, User $user, Request $request): JsonResponse
{
$this->assertHasPermission('OPEN', $exercise);

Expand All @@ -96,7 +80,7 @@ public function listAction(Exercise $exercise, User $user, Request $request)
if (!$this->authorization->isGranted('ADMINISTRATE', $collection) &&
!$this->authorization->isGranted('MANAGE_PAPERS', $collection)
) {
$params['hiddenFilters']['user'] = $user->getId();
$params['hiddenFilters']['user'] = $user->getUuid();
}

$results = $this->finder->searchEntities(Paper::class, $params);
Expand All @@ -118,14 +102,8 @@ public function listAction(Exercise $exercise, User $user, Request $request)
* @Route("/{id}", name="exercise_paper_get", methods={"GET"})
* @EXT\ParamConverter("paper", class="UJMExoBundle:Attempt\Paper", options={"mapping": {"id": "uuid"}})
* @EXT\ParamConverter("user", converter="current_user")
*
* @param Exercise $exercise
* @param Paper $paper
* @param User $user
*
* @return JsonResponse
*/
public function getAction(Exercise $exercise, Paper $paper, User $user)
public function getAction(Exercise $exercise, Paper $paper, User $user): JsonResponse
{
$this->assertHasPermission('OPEN', $exercise);

Expand All @@ -141,22 +119,13 @@ public function getAction(Exercise $exercise, Paper $paper, User $user)
* Deletes some papers associated with an exercise.
*
* @Route("", name="ujm_exercise_delete_papers", methods={"DELETE"})
*
* @param Exercise $exercise
* @param Request $request
*
* @return JsonResponse
*/
public function deleteAction(Exercise $exercise, Request $request)
public function deleteAction(Exercise $exercise, Request $request): JsonResponse
{
$this->assertHasPermission('MANAGE_PAPERS', $exercise);

try {
$papers = $this->decodeIdsString($request, Paper::class);
$this->paperManager->delete($papers);
} catch (InvalidDataException $e) {
return new JsonResponse($e->getErrors(), 422);
}
$papers = $this->decodeIdsString($request, Paper::class);
$this->paperManager->delete($papers);

return new JsonResponse(null, 204);
}
Expand All @@ -165,12 +134,8 @@ public function deleteAction(Exercise $exercise, Request $request)
* Exports papers into a CSV file.
*
* @Route("/export/csv", name="exercise_papers_export", methods={"GET"})
*
* @param Exercise $exercise
*
* @return StreamedResponse
*/
public function exportCsvAction(Exercise $exercise)
public function exportCsvAction(Exercise $exercise): StreamedResponse
{
$this->assertHasPermission('MANAGE_PAPERS', $exercise);

Expand All @@ -186,12 +151,8 @@ public function exportCsvAction(Exercise $exercise)
* Exports papers into a json file.
*
* @Route("/export/json", name="exercise_papers_export_json", methods={"GET"})
*
* @param Exercise $exercise
*
* @return StreamedResponse
*/
public function exportJsonAction(Exercise $exercise)
public function exportJsonAction(Exercise $exercise): StreamedResponse
{
if (!$this->isAdmin($exercise)) {
// Only administrator or Paper Managers can export Papers
Expand All @@ -215,12 +176,8 @@ public function exportJsonAction(Exercise $exercise)
* Exports papers into a csv file.
*
* @Route("/export/papers/csv", name="exercise_papers_export_csv", methods={"GET"})
*
* @param Exercise $exercise
*
* @return StreamedResponse
*/
public function exportCsvAnswersAction(Exercise $exercise)
public function exportCsvAnswersAction(Exercise $exercise): StreamedResponse
{
if (!$this->isAdmin($exercise)) {
// Only administrator or Paper Managers can export Papers
Expand All @@ -237,12 +194,8 @@ public function exportCsvAnswersAction(Exercise $exercise)

/**
* Checks whether the current User has the administration rights on the Exercise.
*
* @param Exercise $exercise
*
* @return bool
*/
private function isAdmin(Exercise $exercise)
private function isAdmin(Exercise $exercise): bool
{
$collection = new ResourceCollection([$exercise->getResourceNode()]);

Expand Down
6 changes: 4 additions & 2 deletions src/plugin/exo/Finder/PaperFinder.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

use Claroline\AppBundle\API\Finder\AbstractFinder;
use Doctrine\ORM\QueryBuilder;
use UJM\ExoBundle\Entity\Attempt\Paper;

/**
* Quiz papers finder.
Expand All @@ -21,7 +22,7 @@ class PaperFinder extends AbstractFinder
{
public function getClass()
{
return 'UJM\ExoBundle\Entity\Attempt\Paper';
return Paper::class;
}

public function configureQueryBuilder(QueryBuilder $qb, array $searches = [], array $sortBy = null, array $options = ['count' => false, 'page' => 0, 'limit' => -1])
Expand All @@ -35,7 +36,7 @@ public function configureQueryBuilder(QueryBuilder $qb, array $searches = [], ar
switch ($filterName) {
case 'user':
$qb->join('obj.user', 'u');
$qb->andWhere('u.id = :userId');
$qb->andWhere('u.uuid = :userId');
$qb->setParameter('userId', $filterValue);
break;

Expand All @@ -48,6 +49,7 @@ public function configureQueryBuilder(QueryBuilder $qb, array $searches = [], ar
$this->setDefaults($qb, $filterName, $filterValue);
}
}

if (!is_null($sortBy) && isset($sortBy['property']) && isset($sortBy['direction'])) {
$sortByProperty = $sortBy['property'];
$sortByDirection = 1 === $sortBy['direction'] ? 'ASC' : 'DESC';
Expand Down

0 comments on commit 48fdeec

Please # to comment.