Skip to content

Commit

Permalink
Internal: Migration: Create shortcut for lp and lp categories publish…
Browse files Browse the repository at this point in the history
…ed on home chamilo#5088
  • Loading branch information
AngelFQC committed Apr 23, 2024
1 parent 8956a5f commit 09df6e1
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/CoreBundle/Migrations/AbstractMigrationChamilo.php
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,7 @@ public function findSession(int $id): ?Session

private function generateFilePath(string $filename): string
{
$cacheDir = $this->getContainer()->get('kernel')->getCacheDir();
$cacheDir = $this->container->get('kernel')->getCacheDir();

return $cacheDir.'/migration_'.$filename;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,13 @@ public function up(Schema $schema): void
}

if ($table->hasColumn('link')) {
$lpTools = $this->connection
->prepare("SELECT c_id, session_id, link FROM c_tool WHERE link LIKE '%lp_controller.php%'")
->executeQuery()
->fetchAllAssociative();

$this->writeFile('tool_links', serialize($lpTools));

$this->addSql('ALTER TABLE c_tool DROP link');
}

Expand Down
72 changes: 72 additions & 0 deletions src/CoreBundle/Migrations/Schema/V200/Version20240327172830.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?php

/* For licensing terms, see /license.txt */

declare(strict_types=1);

namespace Chamilo\CoreBundle\Migrations\Schema\V200;

use Chamilo\CoreBundle\Migrations\AbstractMigrationChamilo;
use Chamilo\CourseBundle\Repository\CLpCategoryRepository;
use Chamilo\CourseBundle\Repository\CLpRepository;
use Chamilo\CourseBundle\Repository\CShortcutRepository;
use Doctrine\DBAL\Schema\Schema;

class Version20240327172830 extends AbstractMigrationChamilo
{
public function getDescription(): string
{
return 'Create shortcuts for c_lp and c_lp_category published on course home';
}

public function up(Schema $schema): void
{
// File generated in the Version20180928172830 migration
$toolLinksContent = $this->readFile('tool_links');

if (empty($toolLinksContent)) {
$this->write('tool_links file not found. Exiting.');

return;
}

$toolLinks = unserialize($toolLinksContent);

$lpRepo = $this->container->get(CLpRepository::class);
$lpCategoryRepo = $this->container->get(CLpCategoryRepository::class);
$shortcutRepo = $this->container->get(CShortcutRepository::class);

foreach ($toolLinks as $toolLink) {
$url = parse_url($toolLink['link']);
$query = [];
parse_str($url['query'] ?? '', $query);

$admin = $this->getAdmin();
$course = $this->findCourse($toolLink['c_id']);
$session = $this->findSession($toolLink['session_id']);
$resource = null;

if (str_contains($url['path'], 'lp_controller.php') && isset($query['action'])) {
if (isset($query['lp_id']) && 'view' === $query['action']) {
$resource = $lpRepo->find($query['lp_id']);
} elseif (isset($query['id']) && 'view_category' === $query['action']) {
$resource = $lpCategoryRepo->find($query['id']);
}
}

if ($resource) {
$shortcut = $shortcutRepo->getShortcutFromResource($resource);

if ($shortcut) {
continue;
}

$shortcutRepo->addShortCut($resource, $admin, $course, $session);
}
}

$this->entityManager->flush();

$this->removeFile('tool_links');
}
}

0 comments on commit 09df6e1

Please sign in to comment.