From 266df43d314552a3160352ed657fcd2d3b496cb2 Mon Sep 17 00:00:00 2001 From: Julio Date: Thu, 14 Oct 2021 10:58:22 +0200 Subject: [PATCH] ExtraFields: Add ExtraFieldItemInterface --- src/CoreBundle/Entity/Course.php | 2 +- .../Entity/ExtraFieldItemInterface.php | 12 +++++++++++ src/CoreBundle/Entity/User.php | 2 +- .../Repository/ExtraFieldValuesRepository.php | 17 +++++++++------- .../ExtraFieldValuesRepositoryTest.php | 20 +++++++++++++++++++ 5 files changed, 44 insertions(+), 9 deletions(-) create mode 100644 src/CoreBundle/Entity/ExtraFieldItemInterface.php diff --git a/src/CoreBundle/Entity/Course.php b/src/CoreBundle/Entity/Course.php index 1fd55cc5a1..cf45802ae2 100644 --- a/src/CoreBundle/Entity/Course.php +++ b/src/CoreBundle/Entity/Course.php @@ -68,7 +68,7 @@ use Symfony\Component\Validator\Constraints as Assert; #[ApiFilter(PropertyFilter::class)] #[ApiFilter(OrderFilter::class, properties: ['id', 'title'])] -class Course extends AbstractResource implements ResourceInterface, ResourceWithAccessUrlInterface, ResourceIllustrationInterface +class Course extends AbstractResource implements ResourceInterface, ResourceWithAccessUrlInterface, ResourceIllustrationInterface, ExtraFieldItemInterface { public const CLOSED = 0; public const REGISTERED = 1; // Only registered users in the course. diff --git a/src/CoreBundle/Entity/ExtraFieldItemInterface.php b/src/CoreBundle/Entity/ExtraFieldItemInterface.php new file mode 100644 index 0000000000..e7562fac06 --- /dev/null +++ b/src/CoreBundle/Entity/ExtraFieldItemInterface.php @@ -0,0 +1,12 @@ + 'partial', ])] #[ApiFilter(BooleanFilter::class, properties: ['isActive'])] -class User implements UserInterface, EquatableInterface, ResourceInterface, ResourceIllustrationInterface, PasswordAuthenticatedUserInterface, LegacyPasswordAuthenticatedUserInterface +class User implements UserInterface, EquatableInterface, ResourceInterface, ResourceIllustrationInterface, PasswordAuthenticatedUserInterface, LegacyPasswordAuthenticatedUserInterface, ExtraFieldItemInterface { use TimestampableEntity; use UserCreatorTrait; diff --git a/src/CoreBundle/Repository/ExtraFieldValuesRepository.php b/src/CoreBundle/Repository/ExtraFieldValuesRepository.php index cc8b32f90b..6ebc0ddbd3 100644 --- a/src/CoreBundle/Repository/ExtraFieldValuesRepository.php +++ b/src/CoreBundle/Repository/ExtraFieldValuesRepository.php @@ -7,14 +7,15 @@ declare(strict_types=1); namespace Chamilo\CoreBundle\Repository; use Chamilo\CoreBundle\Entity\ExtraField; +use Chamilo\CoreBundle\Entity\ExtraFieldItemInterface; use Chamilo\CoreBundle\Entity\ExtraFieldValues; -use Chamilo\CoreBundle\Entity\User; use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository; use Doctrine\ORM\Query\Expr\Join; use Doctrine\Persistence\ManagerRegistry; /** * @author Angel Fernando Quiroz Campos + * @author Julio Montoya */ class ExtraFieldValuesRepository extends ServiceEntityRepository { @@ -55,9 +56,11 @@ class ExtraFieldValuesRepository extends ServiceEntityRepository } /** + * @param ExtraFieldItemInterface $item can be a User|Course|Any Entity that implements ExtraFieldItemInterface + * * @return ExtraFieldValues[] */ - public function getExtraFieldValuesFromItem(User $user, int $type) + public function getExtraFieldValuesFromItem(ExtraFieldItemInterface $item, int $type) { $qb = $this->createQueryBuilder('v'); $qb @@ -65,20 +68,20 @@ class ExtraFieldValuesRepository extends ServiceEntityRepository ->andWhere('v.itemId = :id') ->andWhere( $qb->expr()->eq('f.visibleToSelf', true), - $qb->expr()->eq('f.fieldType', $type) + $qb->expr()->eq('f.extraFieldType', $type) ) ->setParameter( 'id', - $user->getId() + $item->getResourceIdentifier() ) ; return $qb->getQuery()->getResult(); } - public function updateItemData(ExtraField $extraField, User $user, $data): ?ExtraFieldValues + public function updateItemData(ExtraField $extraField, ExtraFieldItemInterface $item, ?string $data): ?ExtraFieldValues { - $itemId = $user->getId(); + $itemId = $item->getResourceIdentifier(); $qb = $this->createQueryBuilder('v'); $qb ->innerJoin('v.field', 'f') @@ -93,7 +96,7 @@ class ExtraFieldValuesRepository extends ServiceEntityRepository if (null === $extraFieldValues) { $extraFieldValues = (new ExtraFieldValues()) - ->setItemId((int) $itemId) + ->setItemId($itemId) ->setField($extraField) ->setValue($data) ; diff --git a/tests/CoreBundle/Repository/ExtraFieldValuesRepositoryTest.php b/tests/CoreBundle/Repository/ExtraFieldValuesRepositoryTest.php index b812875c36..f96fdd85f3 100644 --- a/tests/CoreBundle/Repository/ExtraFieldValuesRepositoryTest.php +++ b/tests/CoreBundle/Repository/ExtraFieldValuesRepositoryTest.php @@ -61,6 +61,7 @@ class ExtraFieldValuesRepositoryTest extends AbstractApiTest $em = $this->getEntityManager(); + // User extra field. $field = (new ExtraField()) ->setDisplayText('test') ->setVariable('test') @@ -80,5 +81,24 @@ class ExtraFieldValuesRepositoryTest extends AbstractApiTest $this->assertNotNull($items); $this->assertNotNull($extraFieldValue); $this->assertCount(1, $items); + + // Course extra field. + + $field = (new ExtraField()) + ->setDisplayText('test2') + ->setVariable('test2') + ->setVisibleToSelf(true) + ->setExtraFieldType(ExtraField::COURSE_FIELD_TYPE) + ->setFieldType(\ExtraField::FIELD_TYPE_TEXT) + ; + $em->persist($field); + $em->flush(); + + $course = $this->createCourse('new'); + + $extraFieldValue = $repo->updateItemData($field, $course, 'hahaha'); + $items = $repo->getExtraFieldValuesFromItem($course, ExtraField::COURSE_FIELD_TYPE); + $this->assertNotNull($extraFieldValue); + $this->assertCount(1, $items); } }