Add CToolIntro.php as resource

pull/4006/head
Julio 3 years ago
parent 58f069fbf9
commit a73c9918e5
  1. 17
      src/CoreBundle/Migrations/Schema/V200/Version20191101132000.php
  2. 2
      src/CoreBundle/Tool/CourseTool.php
  3. 39
      src/CourseBundle/Entity/CTool.php
  4. 102
      src/CourseBundle/Entity/CToolIntro.php
  5. 27
      src/CourseBundle/Repository/CToolIntroRepository.php
  6. 166
      tests/CourseBundle/Repository/CToolIntroRepositoryTest.php

@ -169,6 +169,23 @@ class Version20191101132000 extends AbstractMigrationChamilo
$this->addSql(
'ALTER TABLE course_category CHANGE auth_course_child auth_course_child VARCHAR(40) DEFAULT NULL'
);
$table = $schema->getTable('c_tool');
if ($table->hasIndex('course')) {
$this->addSql('DROP INDEX course ON c_tool_intro;');
}
$this->addSql('ALTER TABLE c_tool_intro CHANGE id id VARCHAR(255) DEFAULT NULL');
if (!$table->hasColumn('c_tool_id')) {
$this->addSql('ALTER TABLE c_tool_intro ADD c_tool_id INT NOT NULL, ADD resource_node_id BIGINT DEFAULT NULL;');
$this->addSql('ALTER TABLE c_tool_intro ADD CONSTRAINT FK_D705267B1DF6B517 FOREIGN KEY (c_tool_id) REFERENCES c_tool (iid);');
$this->addSql('ALTER TABLE c_tool_intro ADD CONSTRAINT FK_D705267B1BAD783F FOREIGN KEY (resource_node_id) REFERENCES resource_node (id) ON DELETE CASCADE;');
$this->addSql('CREATE INDEX IDX_D705267B1DF6B517 ON c_tool_intro (c_tool_id);');
$this->addSql('CREATE UNIQUE INDEX UNIQ_D705267B1BAD783F ON c_tool_intro (resource_node_id);');
}
}
public function down(Schema $schema): void

@ -7,6 +7,7 @@ declare(strict_types=1);
namespace Chamilo\CoreBundle\Tool;
use Chamilo\CourseBundle\Entity\CTool;
use Chamilo\CourseBundle\Entity\CToolIntro;
class CourseTool extends AbstractTool implements ToolInterface
{
@ -34,6 +35,7 @@ class CourseTool extends AbstractTool implements ToolInterface
{
return [
'links' => CTool::class,
'introductions' => CToolIntro::class,
];
}
}

@ -6,21 +6,19 @@ declare(strict_types=1);
namespace Chamilo\CourseBundle\Entity;
use ApiPlatform\Core\Annotation\ApiResource;
use Chamilo\CoreBundle\Entity\AbstractResource;
use Chamilo\CoreBundle\Entity\Course;
use Chamilo\CoreBundle\Entity\ResourceInterface;
use Chamilo\CoreBundle\Entity\ResourceShowCourseResourcesInSessionInterface;
use Chamilo\CoreBundle\Entity\Session;
use Chamilo\CoreBundle\Entity\Tool;
use Doctrine\ORM\Event\LifecycleEventArgs;
use Doctrine\ORM\Mapping as ORM;
use Gedmo\Mapping\Annotation as Gedmo;
use Symfony\Component\Serializer\Annotation\Groups;
use Symfony\Component\Validator\Constraints as Assert;
/**
* CTool.
*
* @ORM\HasLifecycleCallbacks
* @ORM\Table(
* name="c_tool",
@ -31,30 +29,38 @@ use Symfony\Component\Validator\Constraints as Assert;
* )
* @ORM\Entity(repositoryClass="Chamilo\CourseBundle\Repository\CToolRepository")
*/
#[ApiResource(
attributes: [
'security' => "is_granted('ROLE_ADMIN') or is_granted('ROLE_CURRENT_COURSE_TEACHER')",
],
denormalizationContext: [
'groups' => ['ctool:write'],
],
normalizationContext: [
'groups' => ['ctool:read'],
],
)]
class CTool extends AbstractResource implements ResourceInterface, ResourceShowCourseResourcesInSessionInterface
{
/**
* @Groups({"ctool:read"})
*
* @ORM\Column(name="iid", type="integer")
* @ORM\Id
* @ORM\GeneratedValue
*/
#[Groups(['ctool:read'])]
protected int $iid;
/**
* @Groups({"ctool:read"})
*
* @ORM\Column(name="name", type="text", nullable=false)
*/
#[Assert\NotBlank]
#[Groups(['ctool:read'])]
protected string $name;
/**
* @Groups({"ctool:read"})
*
* @ORM\Column(name="visibility", type="boolean", nullable=true)
*/
#[Groups(['ctool:read'])]
protected ?bool $visibility = null;
/**
@ -81,9 +87,7 @@ class CTool extends AbstractResource implements ResourceInterface, ResourceShowC
*/
protected int $position;
/**
* @Groups({"ctool:read"})
*/
#[Groups(['ctool:read'])]
protected string $nameToTranslate;
public function __construct()
@ -169,17 +173,6 @@ class CTool extends AbstractResource implements ResourceInterface, ResourceShowC
return $this;
}
/**
* @ORM\PostPersist()
*/
public function postPersist(LifecycleEventArgs $args): void
{
// Update id with iid value
/*$em = $args->getEntityManager();
$em->persist($this);
$em->flush();*/
}
public function getPosition(): int
{
return $this->position;

@ -6,20 +6,34 @@ declare(strict_types=1);
namespace Chamilo\CourseBundle\Entity;
use ApiPlatform\Core\Annotation\ApiResource;
use Chamilo\CoreBundle\Entity\AbstractResource;
use Chamilo\CoreBundle\Entity\ResourceInterface;
use Chamilo\CoreBundle\Entity\ResourceShowCourseResourcesInSessionInterface;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Serializer\Annotation\Groups;
use Symfony\Component\Validator\Constraints as Assert;
/**
* CToolIntro.
*
* @ORM\Table(
* name="c_tool_intro",
* indexes={
* @ORM\Index(name="course", columns={"c_id"})
* }
* )
* @ORM\Entity
*/
class CToolIntro
#[ApiResource(
attributes: [
'security' => "is_granted('ROLE_ADMIN') or is_granted('ROLE_CURRENT_COURSE_TEACHER')",
],
denormalizationContext: [
'groups' => ['c_tool_intro:write'],
],
normalizationContext: [
'groups' => ['c_tool_intro:read'],
],
)]
class CToolIntro extends AbstractResource implements ResourceInterface, ResourceShowCourseResourcesInSessionInterface
{
/**
* @ORM\Column(name="iid", type="integer")
@ -28,79 +42,67 @@ class CToolIntro
*/
protected int $iid;
/**
* @ORM\Column(name="c_id", type="integer")
*/
protected int $cId;
/**
* @ORM\Column(name="intro_text", type="text", nullable=false)
*/
#[Assert\NotNull]
#[Groups(['c_tool_intro:read', 'c_tool_intro:write'])]
protected string $introText;
/**
* @ORM\Column(name="session_id", type="integer")
* @ORM\ManyToOne(targetEntity="Chamilo\CourseBundle\Entity\CTool")
* @ORM\JoinColumn(name="c_tool_id", referencedColumnName="iid", nullable=false)
*/
protected int $sessionId;
#[Assert\NotNull]
#[Groups(['c_tool_intro:read', 'c_tool_intro:write'])]
protected CTool $courseTool;
public function setIntroText(string $introText): self
public function __toString(): string
{
$this->introText = $introText;
return $this->getIntroText();
}
return $this;
public function getIid(): int
{
return $this->iid;
}
/**
* Get introText.
*
* @return string
*/
public function getIntroText()
public function getCourseTool(): CTool
{
return $this->introText;
return $this->courseTool;
}
/**
* Set cId.
*
* @return CToolIntro
*/
public function setCId(int $cId)
public function setCourseTool(CTool $courseTool): self
{
$this->cId = $cId;
$this->courseTool = $courseTool;
return $this;
}
/**
* Get cId.
*
* @return int
*/
public function getCId()
public function setIntroText(string $introText): self
{
return $this->cId;
$this->introText = $introText;
return $this;
}
/**
* Set sessionId.
*
* @return CToolIntro
*/
public function setSessionId(int $sessionId)
public function getIntroText(): string
{
$this->sessionId = $sessionId;
return $this->introText;
}
return $this;
public function getResourceIdentifier(): int
{
return $this->getIid();
}
/**
* Get sessionId.
*
* @return int
*/
public function getSessionId()
public function getResourceName(): string
{
return $this->getIntroText();
}
public function setResourceName(string $name): self
{
return $this->sessionId;
return $this->setIntroText($name);
}
}

@ -0,0 +1,27 @@
<?php
declare(strict_types=1);
/* For licensing terms, see /license.txt */
namespace Chamilo\CourseBundle\Repository;
use Chamilo\CoreBundle\Entity\Course;
use Chamilo\CoreBundle\Entity\ResourceLink;
use Chamilo\CoreBundle\Entity\ResourceNode;
use Chamilo\CoreBundle\Entity\Session;
use Chamilo\CoreBundle\Entity\User;
use Chamilo\CoreBundle\Repository\ResourceRepository;
use Chamilo\CourseBundle\Entity\CDocument;
use Chamilo\CourseBundle\Entity\CGroup;
use Chamilo\CourseBundle\Entity\CToolIntro;
use Doctrine\ORM\QueryBuilder;
use Doctrine\Persistence\ManagerRegistry;
final class CToolIntroRepository extends ResourceRepository
{
public function __construct(ManagerRegistry $registry)
{
parent::__construct($registry, CToolIntro::class);
}
}

@ -0,0 +1,166 @@
<?php
declare(strict_types=1);
/* For licensing terms, see /license.txt */
namespace Chamilo\Tests\CourseBundle\Repository;
use Chamilo\CoreBundle\Entity\ResourceType;
use Chamilo\CoreBundle\Entity\Tool;
use Chamilo\CoreBundle\Repository\Node\CourseRepository;
use Chamilo\CoreBundle\Tool\HandlerCollection;
use Chamilo\CoreBundle\Tool\ToolChain;
use Chamilo\CourseBundle\Entity\CAnnouncement;
use Chamilo\CourseBundle\Entity\CAnnouncementAttachment;
use Chamilo\CourseBundle\Entity\CCalendarEvent;
use Chamilo\CourseBundle\Entity\CTool;
use Chamilo\CourseBundle\Entity\CToolIntro;
use Chamilo\CourseBundle\Repository\CAnnouncementAttachmentRepository;
use Chamilo\CourseBundle\Repository\CAnnouncementRepository;
use Chamilo\CourseBundle\Repository\CToolIntroRepository;
use Chamilo\Tests\AbstractApiTest;
use Chamilo\Tests\ChamiloTestTrait;
use Doctrine\Common\Collections\ArrayCollection;
class CToolIntroRepositoryTest extends AbstractApiTest
{
use ChamiloTestTrait;
public function testCreate(): void
{
self::bootKernel();
$em = $this->getEntityManager();
$repo = self::getContainer()->get(CToolIntroRepository::class);
$this->assertSame(0, $repo->count([]));
$course = $this->createCourse('new');
$teacher = $this->createUser('teacher');
/** @var CTool $courseTool */
$courseTool = $course->getTools()->first();
$intro = (new CToolIntro())
->setIntroText('test')
->setCourseTool($courseTool)
->setParent($course)
->setCreator($teacher)
->addCourseLink($course)
;
$this->assertHasNoEntityViolations($intro);
$em->persist($intro);
$em->flush();
$this->assertNotEmpty($intro->getIntroText());
$this->assertSame(1, $repo->count([]));
$repo->delete($intro);
$this->assertSame(0, $repo->count([]));
}
public function testGetToolIntros(): void
{
$token = $this->getUserToken([]);
$response = $this->createClientWithCredentials($token)->request('GET', '/api/c_tool_intros');
$this->assertResponseIsSuccessful();
// Asserts that the returned content type is JSON-LD (the default)
$this->assertResponseHeaderSame('content-type', 'application/ld+json; charset=utf-8');
// Asserts that the returned JSON is a superset of this one
$this->assertJsonContains([
'@context' => '/api/contexts/CToolIntro',
'@id' => '/api/c_tool_intros',
'@type' => 'hydra:Collection',
'hydra:totalItems' => 0,
]);
$this->assertCount(0, $response->toArray()['hydra:member']);
$this->assertMatchesResourceCollectionJsonSchema(CToolIntro::class);
}
public function testCreateIntroApi(): void
{
$course = $this->createCourse('new');
$token = $this->getUserToken();
$resourceNodeId = $course->getResourceNode()->getId();
/** @var CTool $courseTool */
$courseTool = $course->getTools()->first();
$iri = '/api/c_tools/'.$courseTool->getIid();
$this->createClientWithCredentials($token)->request(
'POST',
'/api/c_tool_intros',
[
'json' => [
'introText' => 'introduction here',
'courseTool' => $iri,
'parentResourceNodeId' => $resourceNodeId,
],
]
);
$this->assertResponseIsSuccessful();
$this->assertResponseStatusCodeSame(201);
$this->assertResponseHeaderSame('content-type', 'application/ld+json; charset=utf-8');
$this->assertJsonContains([
'@context' => '/api/contexts/CToolIntro',
'@type' => 'CToolIntro',
'introText' => 'introduction here',
]);
}
public function testUpdateIntroApi(): void
{
$course = $this->createCourse('new');
$token = $this->getUserToken();
$resourceNodeId = $course->getResourceNode()->getId();
/** @var CTool $courseTool */
$courseTool = $course->getTools()->first();
$iri = '/api/c_tools/'.$courseTool->getIid();
$response = $this->createClientWithCredentials($token)->request(
'POST',
'/api/c_tool_intros',
[
'json' => [
'introText' => 'introduction here',
'courseTool' => $iri,
'parentResourceNodeId' => $resourceNodeId,
],
]
);
$this->assertResponseIsSuccessful();
$iri = $response->toArray()['@id'];
$this->createClientWithCredentials($token)->request(
'PUT',
$iri,
[
'json' => [
'introText' => 'MODIFIED',
],
]
);
$this->assertResponseIsSuccessful();
$this->assertResponseHeaderSame('content-type', 'application/ld+json; charset=utf-8');
$this->assertJsonContains([
'@context' => '/api/contexts/CToolIntro',
'@type' => 'CToolIntro',
'introText' => 'MODIFIED',
]);
}
}
Loading…
Cancel
Save