Internal: Add migration for message tags - refs BT#21705

pull/5588/head
Angel Fernando Quiroz Campos 5 months ago
parent f05e41439d
commit 5dfac062ec
  1. 47
      src/CoreBundle/Migrations/Schema/V200/Version20200821224230.php
  2. 2
      src/CoreBundle/Migrations/Schema/V200/Version20200821224244.php
  3. 69
      src/CoreBundle/Migrations/Schema/V200/Version20200821224245.php

@ -0,0 +1,47 @@
<?php
/* For licensing terms, see /license.txt */
declare(strict_types=1);
namespace Chamilo\CoreBundle\Migrations\Schema\V200;
use Chamilo\CoreBundle\Entity\ExtraField;
use Chamilo\CoreBundle\Migrations\AbstractMigrationChamilo;
use Doctrine\DBAL\Exception;
use Doctrine\DBAL\Schema\Schema;
final class Version20200821224230 extends AbstractMigrationChamilo
{
public const INBOX_TAGS_FILE = 'inbox_message_tags';
public function getDescription(): string
{
return 'Prepare data to migrate message tags from extra fields.';
}
public function up(Schema $schema): void
{
$this->prepareTagsFromInboxMessages();
}
/**
* @throws Exception
*/
private function prepareTagsFromInboxMessages(): void
{
// Tags from inbox message
$resMessageTag = $this->connection->executeQuery(
"SELECT m.id AS m_id, m.user_receiver_id AS m_receiver_id, t.id AS t_id, t.tag AS t_tag
FROM message m
INNER JOIN extra_field_rel_tag efrt ON m.id = efrt.item_id
INNER JOIN extra_field ef ON efrt.field_id = ef.id
INNER JOIN tag t ON (efrt.tag_id = t.id AND ef.id = t.field_id)
WHERE m.msg_status = 0
AND ef.item_type = ".ExtraField::MESSAGE_TYPE." AND ef.variable = 'tags'"
);
$oldMessageTagInfo = $resMessageTag->fetchAllAssociative();
$this->writeFile(self::INBOX_TAGS_FILE, serialize($oldMessageTagInfo));
}
}

@ -10,7 +10,7 @@ use Chamilo\CoreBundle\Entity\Message;
use Chamilo\CoreBundle\Migrations\AbstractMigrationChamilo;
use Doctrine\DBAL\Schema\Schema;
final class Version20200821224243 extends AbstractMigrationChamilo
final class Version20200821224244 extends AbstractMigrationChamilo
{
private const OLD_MESSAGE_STATUS_NEW = 0;
private const OLD_MESSAGE_STATUS_UNREAD = 1;

@ -0,0 +1,69 @@
<?php
/* For licensing terms, see /license.txt */
declare(strict_types=1);
namespace Chamilo\CoreBundle\Migrations\Schema\V200;
use Chamilo\CoreBundle\Entity\Message;
use Chamilo\CoreBundle\Entity\MessageRelUser;
use Chamilo\CoreBundle\Entity\MessageTag;
use Chamilo\CoreBundle\Entity\User;
use Chamilo\CoreBundle\Migrations\AbstractMigrationChamilo;
use Doctrine\DBAL\Schema\Schema;
final class Version20200821224245 extends AbstractMigrationChamilo
{
/**
* @inheritDoc
*/
public function up(Schema $schema): void
{
$this->migrateTagsFromInboxMessages();
}
private function migrateTagsFromInboxMessages(): void
{
// File generated in the Version20180928172830 migration
$messageTagsInfo = $this->readFile(Version20200821224230::INBOX_TAGS_FILE);
if (empty($messageTagsInfo)) {
$this->write(Version20200821224230::INBOX_TAGS_FILE.' file not found. Exiting.');
return;
}
$oldMessageTagsInfo = unserialize($messageTagsInfo);
$messageRelUserRepo = $this->entityManager->getRepository(MessageRelUser::class);
$tagRepo = $this->entityManager->getRepository(MessageTag::class);
foreach ($oldMessageTagsInfo as $rowMessageTag) {
$message = $this->entityManager->find(Message::class, $rowMessageTag['m_id']);
$receiver = $this->entityManager->find(User::class, $rowMessageTag['m_receiver_id']);
$messageTag = $tagRepo->findOneBy(['tag' => $rowMessageTag['t_tag'], 'user' => $receiver]);
if (!$messageTag) {
$messageTag = (new MessageTag())
->setTag($rowMessageTag['t_tag'])
->setUser($receiver)
;
}
$messageRelUser = $messageRelUserRepo->findOneBy(['message' => $message, 'receiver' => $receiver]);
if ($messageRelUser) {
$messageRelUser->addTag($messageTag);
$this->entityManager->persist($messageRelUser);
$this->entityManager->flush();
}
}
$this->entityManager->clear();
$this->removeFile(Version20200821224230::INBOX_TAGS_FILE);
}
}
Loading…
Cancel
Save