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 Mar 29, 2024
1 parent 8d660ba commit 59bb221
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,14 @@ public function up(Schema $schema): void
}

if ($table->hasColumn('link')) {
$lpTools = $this->getEntityManager()
->getConnection()
->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
74 changes: 74 additions & 0 deletions src/CoreBundle/Migrations/Schema/V200/Version20240327172830.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?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);

$container = $this->getContainer();
$em = $this->getEntityManager();
$lpRepo = $container->get(CLpRepository::class);
$lpCategoryRepo = $container->get(CLpCategoryRepository::class);
$shortcutRepo = $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);
}
}

$em->flush();

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

0 comments on commit 59bb221

Please sign in to comment.