Migrations: Add CToolIntro migration

pull/4004/head
Julio 3 years ago
parent 794ed4f631
commit 729b030940
  1. 5
      src/CoreBundle/Migrations/Schema/V200/Version20191101132000.php
  2. 115
      src/CoreBundle/Migrations/Schema/V200/Version20210930130343.php
  3. 10
      src/CourseBundle/Entity/CTool.php
  4. 7
      src/CourseBundle/Entity/CToolIntro.php
  5. 2
      tests/CourseBundle/Repository/CToolRepositoryTest.php

@ -176,6 +176,11 @@ class Version20191101132000 extends AbstractMigrationChamilo
$this->addSql('DROP INDEX course ON c_tool_intro;');
}
$this->addSql("DELETE FROM c_tool_intro WHERE id = '0' ");
// Migrate only course intro.
$this->addSql("DELETE FROM c_tool_intro WHERE id <> 'course_homepage' ");
$this->addSql('ALTER TABLE c_tool_intro CHANGE id id VARCHAR(255) DEFAULT NULL');
if (!$table->hasColumn('c_tool_id')) {

@ -0,0 +1,115 @@
<?php
declare(strict_types=1);
namespace Chamilo\CoreBundle\Migrations\Schema\V200;
use Chamilo\CoreBundle\Entity\Course;
use Chamilo\CoreBundle\Migrations\AbstractMigrationChamilo;
use Chamilo\CoreBundle\Repository\Node\CourseRepository;
use Chamilo\CoreBundle\Repository\SessionRepository;
use Chamilo\CoreBundle\Repository\ToolRepository;
use Chamilo\CourseBundle\Entity\CTool;
use Chamilo\CourseBundle\Entity\CToolIntro;
use Chamilo\CourseBundle\Repository\CToolIntroRepository;
use Chamilo\CourseBundle\Repository\CToolRepository;
use Doctrine\DBAL\Schema\Schema;
final class Version20210930130343 extends AbstractMigrationChamilo
{
public function getDescription(): string
{
return 'CToolIntro';
}
public function up(Schema $schema): void
{
$container = $this->getContainer();
$em = $this->getEntityManager();
$connection = $em->getConnection();
$batchSize = self::BATCH_SIZE;
$introRepo = $container->get(CToolIntroRepository::class);
$cToolRepo = $container->get(CToolRepository::class);
$toolRepo = $container->get(ToolRepository::class);
$sessionRepo = $container->get(SessionRepository::class);
$courseRepo = $container->get(CourseRepository::class);
$q = $em->createQuery('SELECT c FROM Chamilo\CoreBundle\Entity\Course c');
/** @var Course $course */
foreach ($q->toIterable() as $course) {
$courseId = $course->getId();
$sql = "SELECT * FROM c_tool_intro WHERE c_id = {$courseId}
ORDER BY iid";
$result = $connection->executeQuery($sql);
$items = $result->fetchAllAssociative();
foreach ($items as $itemData) {
$id = $itemData['iid'];
$sessionId = (int) $itemData['session_id'];
$toolName = $itemData['id'];
/** @var CToolIntro $intro */
$intro = $introRepo->find($id);
if ($intro->hasResourceNode()) {
continue;
}
$session = null;
if (!empty($sessionId)) {
$session = $sessionRepo->find($sessionId);
}
$admin = $this->getAdmin();
$cTool = null;
if ('course_homepage' === $toolName) {
$tool = $toolRepo->findOneBy(['name' => $toolName]);
if (null === $tool) {
continue;
}
$course = $courseRepo->find($courseId);
$cTool = (new CTool())
->setName('course_homepage')
->setCourse($course)
->setSession($session)
->setTool($tool)
->setCreator($admin)
->setParent($course)
->addCourseLink($course, $session)
;
$em->persist($cTool);
} else {
$cTool = $cToolRepo->findCourseResourceByTitle($toolName, $course->getResourceNode(), $course, $session);
}
if (null === $cTool) {
continue;
}
$intro
->setParent($course)
->setCourseTool($cTool)
;
$introRepo->addResourceNode($intro, $admin, $course);
$intro->addCourseLink($course, $session);
$em->persist($intro);
$em->flush();
}
$em->flush();
$em->clear();
}
}
public function down(Schema $schema): void
{
}
}

@ -48,7 +48,7 @@ class CTool extends AbstractResource implements ResourceInterface, ResourceShowC
* @ORM\GeneratedValue
*/
#[Groups(['ctool:read'])]
protected int $iid;
protected ?int $iid = null;
/**
* @ORM\Column(name="name", type="text", nullable=false)
@ -92,6 +92,7 @@ class CTool extends AbstractResource implements ResourceInterface, ResourceShowC
public function __construct()
{
$this->visibility = true;
$this->position = 0;
}
@ -117,10 +118,7 @@ class CTool extends AbstractResource implements ResourceInterface, ResourceShowC
return $this;
}
/**
* @return int
*/
public function getIid()
public function getIid(): ?int
{
return $this->iid;
}
@ -156,7 +154,7 @@ class CTool extends AbstractResource implements ResourceInterface, ResourceShowC
return $this;
}
public function getVisibility(): bool
public function getVisibility(): ?bool
{
return $this->visibility;
}

@ -86,7 +86,7 @@ class CToolIntro extends AbstractResource implements ResourceInterface, Resource
public function __toString(): string
{
return $this->getIntroText();
return (string) $this->getIid();
}
public function getIid(): int
@ -125,11 +125,12 @@ class CToolIntro extends AbstractResource implements ResourceInterface, Resource
public function getResourceName(): string
{
return $this->getIntroText();
return (string) $this->getIid();
}
public function setResourceName(string $name): self
{
return $this->setIntroText($name);
//return $this->setIntroText($name);
return $this;
}
}

@ -42,7 +42,7 @@ class CToolRepositoryTest extends AbstractApiTest
$this->assertHasNoEntityViolations($cTool);
$em->persist($cTool);
$em->flush();
$this->assertSame($defaultCount + 1, $repo->count([]));
}

Loading…
Cancel
Save