ExtraFields: Add ExtraFieldItemInterface

pull/4032/head
Julio 3 years ago
parent ce0ad53824
commit 266df43d31
  1. 2
      src/CoreBundle/Entity/Course.php
  2. 12
      src/CoreBundle/Entity/ExtraFieldItemInterface.php
  3. 2
      src/CoreBundle/Entity/User.php
  4. 17
      src/CoreBundle/Repository/ExtraFieldValuesRepository.php
  5. 20
      tests/CoreBundle/Repository/ExtraFieldValuesRepositoryTest.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.

@ -0,0 +1,12 @@
<?php
declare(strict_types=1);
/* For licensing terms, see /license.txt */
namespace Chamilo\CoreBundle\Entity;
interface ExtraFieldItemInterface
{
public function getResourceIdentifier(): int;
}

@ -85,7 +85,7 @@ use UserManager;
'lastname' => '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;

@ -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 <angel.quiroz@beeznest.com>
* @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)
;

@ -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);
}
}

Loading…
Cancel
Save