Skip to content

Commit

Permalink
Merge pull request #5499 from christianbeeznest/ofaj-21623-2
Browse files Browse the repository at this point in the history
Migration: Ensure all courses have the required tools - refs BT#21660
  • Loading branch information
christianbeeznest committed May 9, 2024
2 parents 1e3eba0 + f75cad9 commit 4fff915
Showing 1 changed file with 88 additions and 0 deletions.
88 changes: 88 additions & 0 deletions src/CoreBundle/Migrations/Schema/V200/Version20240509123200.php
@@ -0,0 +1,88 @@
<?php

declare(strict_types=1);

namespace Chamilo\CoreBundle\Migrations\Schema\V200;

use Chamilo\CoreBundle\Entity\ResourceLink;
use Chamilo\CoreBundle\Migrations\AbstractMigrationChamilo;
use Chamilo\CoreBundle\Repository\Node\CourseRepository;
use Chamilo\CoreBundle\Repository\ToolRepository;
use Chamilo\CourseBundle\Entity\CTool;
use Doctrine\DBAL\Schema\Schema;

final class Version20240509123200 extends AbstractMigrationChamilo
{
public function getDescription(): string
{
return 'Ensure all courses have the required tools post-migration.';
}

public function up(Schema $schema): void
{
$courseRepo = $this->container->get(CourseRepository::class);
$toolRepo = $this->container->get(ToolRepository::class);
$admin = $this->getAdmin();

// Define your tool list
$requiredTools = [
'course_description',
'document',
'learnpath',
'link',
'quiz',
'announcement',
'gradebook',
'glossary',
'attendance',
'course_progress',
'agenda',
'forum',
'dropbox',
'member',
'group',
'chat',
'student_publication',
'survey',
'wiki',
'notebook',
'course_tool',
'course_homepage',
'tracking',
'course_setting',
'course_maintenance',
];

$courses = $courseRepo->findAll();
foreach ($courses as $course) {
foreach ($requiredTools as $toolName) {
$ctool = $course->getTools()->filter(
fn(CTool $ct) => $ct->getTool()->getTitle() === $toolName
)->first() ?? null;

if (!$ctool) {
$tool = $toolRepo->findOneBy(['title' => $toolName]);
if ($tool) {
$linkVisibility = ($toolName == 'course_setting' || $toolName == 'course_maintenance')
? ResourceLink::VISIBILITY_DRAFT : ResourceLink::VISIBILITY_PUBLISHED;

$ctool = new CTool();
$ctool->setTool($tool);
$ctool->setTitle($toolName);
$ctool->setVisibility(true);
$ctool->setParent($course);
$ctool->setCreator($admin);
$ctool->addCourseLink($course, null, null, $linkVisibility);
$this->entityManager->persist($ctool);
error_log("Tool '{$toolName}' needs to be added to course ID {$course->getId()}.");

$course->addTool($ctool);
error_log("Tool '{$toolName}' created and linked to course.");
}
}
}
}
$this->entityManager->flush();
$this->entityManager->clear();
}
}

0 comments on commit 4fff915

Please sign in to comment.