commit
a8850e7de5
@ -0,0 +1,35 @@ |
||||
<?php |
||||
|
||||
declare(strict_types=1); |
||||
|
||||
/* For licensing terms, see /license.txt */ |
||||
|
||||
namespace Chamilo\CoreBundle\DataFixtures; |
||||
|
||||
use Chamilo\CoreBundle\Entity\ResourceFormat; |
||||
use Doctrine\Bundle\FixturesBundle\Fixture; |
||||
use Doctrine\Persistence\ObjectManager; |
||||
|
||||
class ResourceFormatFixtures extends Fixture |
||||
{ |
||||
public function load(ObjectManager $manager): void |
||||
{ |
||||
$list = [ |
||||
[ |
||||
'name' => 'html', |
||||
], |
||||
[ |
||||
'name' => 'txt', |
||||
], |
||||
]; |
||||
|
||||
foreach ($list as $key => $data) { |
||||
$resourceFormat = (new ResourceFormat()) |
||||
->setName($data['name']) |
||||
; |
||||
$manager->persist($resourceFormat); |
||||
} |
||||
|
||||
$manager->flush(); |
||||
} |
||||
} |
@ -0,0 +1,80 @@ |
||||
<?php |
||||
|
||||
declare(strict_types=1); |
||||
|
||||
/* For licensing terms, see /license.txt */ |
||||
|
||||
namespace Chamilo\CoreBundle\Entity; |
||||
|
||||
use Doctrine\Common\Collections\ArrayCollection; |
||||
use Doctrine\Common\Collections\Collection; |
||||
use Doctrine\ORM\Mapping as ORM; |
||||
use Gedmo\Timestampable\Traits\TimestampableEntity; |
||||
use Symfony\Component\Validator\Constraints as Assert; |
||||
|
||||
#[ORM\Entity] |
||||
#[ORM\Table(name: "resource_format")] |
||||
class ResourceFormat |
||||
{ |
||||
use TimestampableEntity; |
||||
|
||||
#[ORM\Id] |
||||
#[ORM\Column(type: "integer")] |
||||
#[ORM\GeneratedValue] |
||||
protected ?int $id = null; |
||||
|
||||
#[ORM\Column] |
||||
#[Assert\NotBlank] |
||||
protected string $name; |
||||
|
||||
/** |
||||
* @var Collection<int, ResourceNode> |
||||
*/ |
||||
#[ORM\OneToMany(targetEntity: ResourceNode::class, mappedBy: "resourceFormat", cascade: ["persist", "remove"])] |
||||
protected Collection $resourceNodes; |
||||
|
||||
public function __construct() |
||||
{ |
||||
$this->resourceNodes = new ArrayCollection(); |
||||
} |
||||
|
||||
public function __toString(): string |
||||
{ |
||||
return $this->name; |
||||
} |
||||
|
||||
public function getId(): int |
||||
{ |
||||
return $this->id; |
||||
} |
||||
|
||||
public function getName(): string |
||||
{ |
||||
return $this->name; |
||||
} |
||||
|
||||
public function setName(string $name): self |
||||
{ |
||||
$this->name = $name; |
||||
|
||||
return $this; |
||||
} |
||||
|
||||
/** |
||||
* @return Collection<int, ResourceNode> |
||||
*/ |
||||
public function getResourceNodes(): Collection |
||||
{ |
||||
return $this->resourceNodes; |
||||
} |
||||
|
||||
/** |
||||
* @param Rollection<int, ResourceNode> |
||||
*/ |
||||
public function setResourceNodes(Collection $resourceNodes): self |
||||
{ |
||||
$this->resourceNodes = $resourceNodes; |
||||
|
||||
return $this; |
||||
} |
||||
} |
@ -0,0 +1,36 @@ |
||||
<?php |
||||
|
||||
declare(strict_types=1); |
||||
|
||||
/* For licensing terms, see /license.txt */ |
||||
|
||||
namespace Chamilo\CoreBundle\Migrations\Schema\V200; |
||||
|
||||
use Chamilo\CoreBundle\Migrations\AbstractMigrationChamilo; |
||||
use Doctrine\DBAL\Schema\Schema; |
||||
|
||||
final class Version20230215062918 extends AbstractMigrationChamilo |
||||
{ |
||||
public function getDescription(): string |
||||
{ |
||||
return 'Learnpath subscription changes'; |
||||
} |
||||
|
||||
public function up(Schema $schema): void |
||||
{ |
||||
if (false === $schema->hasTable('c_lp_rel_user')) { |
||||
$this->addSql( |
||||
"CREATE TABLE c_lp_rel_user (iid INT AUTO_INCREMENT NOT NULL, lp_id INT DEFAULT NULL, c_id INT NOT NULL, session_id INT DEFAULT NULL, user_id INT NOT NULL, creator_id INT DEFAULT NULL, created_at DATETIME NOT NULL COMMENT '(DC2Type:datetime)', INDEX IDX_AD97516E68DFD1EF (lp_id), INDEX IDX_AD97516E91D79BD3 (c_id), INDEX IDX_AD97516E613FECDF (session_id), INDEX IDX_AD97516EA76ED395 (user_id), INDEX IDX_AD97516E61220EA6 (creator_id), PRIMARY KEY(iid)) DEFAULT CHARACTER SET utf8mb4 COLLATE `utf8mb4_unicode_ci` ENGINE = InnoDB ROW_FORMAT = DYNAMIC;" |
||||
); |
||||
$this->addSql("ALTER TABLE c_lp_rel_user ADD CONSTRAINT FK_AD97516E68DFD1EF FOREIGN KEY (lp_id) REFERENCES c_lp (iid);"); |
||||
$this->addSql("ALTER TABLE c_lp_rel_user ADD CONSTRAINT FK_AD97516E91D79BD3 FOREIGN KEY (c_id) REFERENCES course (id);"); |
||||
$this->addSql("ALTER TABLE c_lp_rel_user ADD CONSTRAINT FK_AD97516E613FECDF FOREIGN KEY (session_id) REFERENCES session (id);"); |
||||
$this->addSql("ALTER TABLE c_lp_rel_user ADD CONSTRAINT FK_AD97516EA76ED395 FOREIGN KEY (user_id) REFERENCES user (id);"); |
||||
$this->addSql("ALTER TABLE c_lp_rel_user ADD CONSTRAINT FK_AD97516E61220EA6 FOREIGN KEY (creator_id) REFERENCES user (id);"); |
||||
} |
||||
} |
||||
|
||||
public function down(Schema $schema): void |
||||
{ |
||||
} |
||||
} |
@ -0,0 +1,94 @@ |
||||
<?php |
||||
|
||||
declare(strict_types=1); |
||||
|
||||
/* For licensing terms, see /license.txt */ |
||||
|
||||
namespace Chamilo\CoreBundle\Migrations\Schema\V200; |
||||
|
||||
use Chamilo\CoreBundle\Entity\Course; |
||||
use Chamilo\CoreBundle\Entity\Session; |
||||
use Chamilo\CoreBundle\Entity\User; |
||||
use Chamilo\CoreBundle\Migrations\AbstractMigrationChamilo; |
||||
use Chamilo\CoreBundle\Repository\Node\CourseRepository; |
||||
use Chamilo\CourseBundle\Entity\CLp; |
||||
use Chamilo\CourseBundle\Entity\CLpRelUser; |
||||
use Chamilo\CourseBundle\Repository\CLpRelUserRepository; |
||||
use Chamilo\Kernel; |
||||
use Doctrine\DBAL\Connection; |
||||
use Doctrine\DBAL\Schema\Schema; |
||||
|
||||
final class Version20230215072918 extends AbstractMigrationChamilo |
||||
{ |
||||
public function getDescription(): string |
||||
{ |
||||
return 'Migrate learnpath subscription'; |
||||
} |
||||
|
||||
public function up(Schema $schema): void |
||||
{ |
||||
$container = $this->getContainer(); |
||||
$doctrine = $container->get('doctrine'); |
||||
$em = $doctrine->getManager(); |
||||
/** @var Connection $connection */ |
||||
$connection = $em->getConnection(); |
||||
|
||||
$lpRepo = $container->get(CLp::class); |
||||
|
||||
/** @var CLpRelUserRepository $cLpRelUserRepo */ |
||||
$cLpRelUserRepo = $container->get(CLpRelUser::class); |
||||
|
||||
$courseRepo = $container->get(CourseRepository::class); |
||||
$sessionRepo = $container->get(Session::class); |
||||
$userRepo = $container->get(User::class); |
||||
|
||||
/** @var Kernel $kernel */ |
||||
$kernel = $container->get('kernel'); |
||||
$rootPath = $kernel->getProjectDir(); |
||||
$admin = $this->getAdmin(); |
||||
|
||||
|
||||
$q = $em->createQuery('SELECT c FROM Chamilo\CoreBundle\Entity\Course c'); |
||||
/** @var Course $course */ |
||||
foreach ($q->toIterable() as $course) { |
||||
$courseId = $course->getId(); |
||||
$course = $courseRepo->find($courseId); |
||||
|
||||
$sql = "SELECT * FROM c_lp WHERE c_id = {$courseId} |
||||
ORDER BY iid"; |
||||
$result = $connection->executeQuery($sql); |
||||
$lps = $result->fetchAllAssociative(); |
||||
foreach ($lps as $lpData) { |
||||
$id = $lpData['iid']; |
||||
$lp = $lpRepo->find($id); |
||||
$sql = "SELECT * FROM c_item_property |
||||
WHERE tool = 'learnpath' AND c_id = {$courseId} AND ref = {$id} AND lastedit_type = 'LearnpathSubscription'"; |
||||
$result = $connection->executeQuery($sql); |
||||
$items = $result->fetchAllAssociative(); |
||||
|
||||
if (!empty($items)) { |
||||
foreach ($items as $item) { |
||||
$sessionId = $item['session_id'] ?? 0; |
||||
$userId = $item['to_user_id'] ?? 0; |
||||
$session = $sessionRepo->find($sessionId); |
||||
$user = $userRepo->find($userId); |
||||
$item = new CLpRelUser(); |
||||
$item |
||||
->setUser($user) |
||||
->setCourse($course) |
||||
->setLp($lp); |
||||
if (!empty($session)) { |
||||
$item->setSession($session); |
||||
} |
||||
$em->persist($item); |
||||
$em->flush(); |
||||
} |
||||
} |
||||
} |
||||
} |
||||
} |
||||
|
||||
public function down(Schema $schema): void |
||||
{ |
||||
} |
||||
} |
@ -0,0 +1,52 @@ |
||||
<?php |
||||
|
||||
declare(strict_types=1); |
||||
|
||||
namespace Chamilo\CoreBundle\Migrations\Schema\V200; |
||||
|
||||
use Chamilo\CoreBundle\Migrations\AbstractMigrationChamilo; |
||||
use Doctrine\DBAL\Schema\Schema; |
||||
|
||||
final class Version20230306145019 extends AbstractMigrationChamilo |
||||
{ |
||||
public function getDescription(): string |
||||
{ |
||||
return 'Create table resource_format'; |
||||
} |
||||
|
||||
public function up(Schema $schema): void |
||||
{ |
||||
if (!$schema->hasTable('resource_format')) { |
||||
$this->addSql( |
||||
"CREATE TABLE resource_format (id INT AUTO_INCREMENT NOT NULL, name VARCHAR(255) NOT NULL, created_at DATETIME NOT NULL COMMENT '(DC2Type:datetime)', updated_at DATETIME NOT NULL COMMENT '(DC2Type:datetime)', PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8mb4 COLLATE `utf8mb4_unicode_ci` ENGINE = InnoDB ROW_FORMAT = DYNAMIC;" |
||||
); |
||||
$this->addSql( |
||||
'ALTER TABLE resource_node ADD resource_format_id INT DEFAULT NULL;' |
||||
); |
||||
$this->addSql( |
||||
'ALTER TABLE resource_node ADD CONSTRAINT FK_8A5F48FF7EE0A59A FOREIGN KEY (resource_format_id) REFERENCES resource_format (id) ON DELETE SET NULL;' |
||||
); |
||||
$this->addSql( |
||||
'CREATE INDEX IDX_8A5F48FF7EE0A59A ON resource_node (resource_format_id);' |
||||
); |
||||
} |
||||
} |
||||
|
||||
public function down(Schema $schema): void |
||||
{ |
||||
if ($schema->hasTable('resource_format')) { |
||||
$this->addSql( |
||||
'ALTER TABLE resource_node DROP FOREIGN KEY FK_8A5F48FF7EE0A59A;' |
||||
); |
||||
$this->addSql( |
||||
'ALTER TABLE resource_node DROP INDEX IDX_8A5F48FF7EE0A59A;' |
||||
); |
||||
$this->addSql( |
||||
'ALTER TABLE resource_node DROP resource_format_id;' |
||||
); |
||||
$this->addSql( |
||||
'DROP TABLE resource_format;' |
||||
); |
||||
} |
||||
} |
||||
} |
@ -0,0 +1,50 @@ |
||||
<?php |
||||
|
||||
declare(strict_types=1); |
||||
|
||||
namespace Chamilo\CoreBundle\Migrations\Schema\V200; |
||||
|
||||
use Chamilo\CoreBundle\Migrations\AbstractMigrationChamilo; |
||||
use Doctrine\DBAL\Schema\Schema; |
||||
|
||||
final class Version20230306150219 extends AbstractMigrationChamilo |
||||
{ |
||||
public function getDescription(): string |
||||
{ |
||||
return 'Insert default values to table resource_format'; |
||||
} |
||||
|
||||
public function up(Schema $schema): void |
||||
{ |
||||
$connection = $this->getEntityManager()->getConnection(); |
||||
if ($schema->hasTable('resource_format')) { |
||||
$result = $connection->executeQuery(" SELECT * FROM resource_format WHERE name = 'html'"); |
||||
$exists = $result->fetchAllAssociative(); |
||||
if (empty($exists)) { |
||||
$this->addSql("INSERT INTO resource_format SET name = 'html', created_at = NOW(), updated_at = NOW();"); |
||||
} |
||||
$result = $connection->executeQuery(" SELECT * FROM resource_format WHERE name = 'txt'"); |
||||
$exists = $result->fetchAllAssociative(); |
||||
if (empty($exists)) { |
||||
$this->addSql("INSERT INTO resource_format SET name = 'txt', created_at = NOW(), updated_at = NOW();"); |
||||
} |
||||
} |
||||
} |
||||
|
||||
public function down(Schema $schema): void |
||||
{ |
||||
$connection = $this->getEntityManager()->getConnection(); |
||||
if ($schema->hasTable('resource_format')) { |
||||
$result = $connection->executeQuery(" SELECT * FROM resource_format WHERE name = 'txt'"); |
||||
$exists = $result->fetchAllAssociative(); |
||||
if (!empty($exists)) { |
||||
$this->addSql("DELETE FROM resource_format WHERE name = 'txt';"); |
||||
} |
||||
$result = $connection->executeQuery(" SELECT * FROM resource_format WHERE name = 'html'"); |
||||
$exists = $result->fetchAllAssociative(); |
||||
if (!empty($exists)) { |
||||
$this->addSql("DELETE FROM resource_format WHERE name = 'html';"); |
||||
} |
||||
} |
||||
} |
||||
} |
@ -0,0 +1,102 @@ |
||||
<?php |
||||
|
||||
declare(strict_types=1); |
||||
|
||||
namespace Chamilo\CourseBundle\Entity; |
||||
|
||||
use Chamilo\CoreBundle\Entity\Course; |
||||
use Chamilo\CoreBundle\Entity\Session; |
||||
use Chamilo\CoreBundle\Entity\User; |
||||
use Chamilo\CoreBundle\Traits\CourseTrait; |
||||
use Chamilo\CoreBundle\Traits\SessionTrait; |
||||
use Chamilo\CoreBundle\Traits\UserTrait; |
||||
use Chamilo\CourseBundle\Repository\CLpRelUserRepository; |
||||
use DateTime; |
||||
use Doctrine\ORM\Mapping as ORM; |
||||
use Gedmo\Mapping\Annotation as Gedmo; |
||||
|
||||
#[ORM\Table(name: "c_lp_rel_user")] |
||||
#[ORM\Entity(repositoryClass: CLpRelUserRepository::class)] |
||||
class CLpRelUser |
||||
{ |
||||
use CourseTrait; |
||||
use SessionTrait; |
||||
use UserTrait; |
||||
|
||||
#[ORM\Column(name: "iid", type: "integer")] |
||||
#[ORM\Id] |
||||
#[ORM\GeneratedValue] |
||||
protected int $iid; |
||||
|
||||
#[ORM\ManyToOne(targetEntity: CLp::class)] |
||||
#[ORM\JoinColumn(name: "lp_id", referencedColumnName: "iid")] |
||||
protected CLp $lp; |
||||
|
||||
#[ORM\ManyToOne(targetEntity: Course::class)] |
||||
#[ORM\JoinColumn(name: "c_id", referencedColumnName: "id", nullable: false)] |
||||
protected Course $course; |
||||
|
||||
#[ORM\ManyToOne(targetEntity: Session::class)] |
||||
#[ORM\JoinColumn(name: "session_id", referencedColumnName: "id", nullable: true)] |
||||
protected ?Session $session = null; |
||||
|
||||
#[ORM\ManyToOne(targetEntity: User::class)] |
||||
#[ORM\JoinColumn(name: "user_id", referencedColumnName: "id", nullable: false)] |
||||
protected User $user; |
||||
|
||||
#[Gedmo\Timestampable(on: "create")] |
||||
#[ORM\Column(name: "created_at", type: "datetime", nullable: false)] |
||||
protected DateTime $createdAt; |
||||
|
||||
#[ORM\ManyToOne(targetEntity: User::class, cascade: ["persist"])] |
||||
#[ORM\JoinColumn(name: "creator_id", referencedColumnName: "id")] |
||||
protected $creatorUser; |
||||
|
||||
public function getIid(): int |
||||
{ |
||||
return $this->iid; |
||||
} |
||||
|
||||
public function setIid(int $iid): self |
||||
{ |
||||
$this->iid = $iid; |
||||
|
||||
return $this; |
||||
} |
||||
|
||||
public function getLp(): CLp |
||||
{ |
||||
return $this->lp; |
||||
} |
||||
|
||||
public function setLp(CLp $lp): self |
||||
{ |
||||
$this->lp = $lp; |
||||
|
||||
return $this; |
||||
} |
||||
|
||||
public function getCreatedAt(): DateTime |
||||
{ |
||||
return $this->createdAt; |
||||
} |
||||
|
||||
public function setCreatedAt(DateTime $createdAt): self |
||||
{ |
||||
$this->createdAt = $createdAt; |
||||
|
||||
return $this; |
||||
} |
||||
|
||||
public function getCreatorUser() |
||||
{ |
||||
return $this->creatorUser; |
||||
} |
||||
|
||||
public function setCreatorUser(User $creatorUser): self |
||||
{ |
||||
$this->creatorUser = $creatorUser; |
||||
|
||||
return $this; |
||||
} |
||||
} |
@ -0,0 +1,162 @@ |
||||
<?php |
||||
|
||||
declare(strict_types=1); |
||||
|
||||
/* For licensing terms, see /license.txt */ |
||||
|
||||
namespace Chamilo\CourseBundle\Repository; |
||||
|
||||
use Chamilo\CoreBundle\Entity\Course; |
||||
use Chamilo\CoreBundle\Entity\Session; |
||||
use Chamilo\CoreBundle\Entity\User; |
||||
use Chamilo\CoreBundle\Repository\ResourceRepository; |
||||
use Chamilo\CourseBundle\Entity\CLp; |
||||
use Chamilo\CourseBundle\Entity\CLpRelUser; |
||||
use Doctrine\Persistence\ManagerRegistry; |
||||
|
||||
/** |
||||
* Class CLpRelUserRepository. |
||||
*/ |
||||
final class CLpRelUserRepository extends ResourceRepository |
||||
{ |
||||
public function __construct(ManagerRegistry $registry) |
||||
{ |
||||
parent::__construct($registry, CLpRelUser::class); |
||||
} |
||||
|
||||
/** |
||||
* Get users subscribed to a item LP. |
||||
* |
||||
* @param CLp $lp |
||||
* @param Course $course |
||||
* @param Session $session |
||||
* |
||||
* @return array |
||||
*/ |
||||
public function getUsersSubscribedToItem( |
||||
CLp $lp, |
||||
Course $course, |
||||
Session $session = null |
||||
) { |
||||
$criteria = [ |
||||
'lp' => $lp, |
||||
'course' => $course, |
||||
'session' => $session, |
||||
]; |
||||
|
||||
return $this->findBy($criteria); |
||||
} |
||||
|
||||
/** |
||||
* Subscribe users to a LP. |
||||
* |
||||
* @param User $currentUser |
||||
* @param Course $course |
||||
* @param Session $session |
||||
* @param CLp $lp |
||||
* @param array $newUserList |
||||
* @param bool $deleteUsers |
||||
*/ |
||||
public function subscribeUsersToItem( |
||||
$currentUser, |
||||
Course $course, |
||||
Session $session = null, |
||||
CLp $lp, |
||||
$newUserList = [], |
||||
$deleteUsers = true |
||||
) { |
||||
$em = $this->getEntityManager(); |
||||
$user = $em->getRepository(User::class); |
||||
|
||||
$usersSubscribedToItem = $this->getUsersSubscribedToItem( |
||||
$lp, |
||||
$course, |
||||
$session |
||||
); |
||||
|
||||
$alreadyAddedUsers = []; |
||||
if ($usersSubscribedToItem) { |
||||
/** @var CLpRelUser $lpUser */ |
||||
foreach ($usersSubscribedToItem as $lpUser) { |
||||
$getToUser = $lpUser->getUser(); |
||||
if (!empty($getToUser)) { |
||||
$alreadyAddedUsers[] = $lpUser->getUser()->getId(); |
||||
} |
||||
} |
||||
} |
||||
|
||||
if ($deleteUsers) { |
||||
$usersToDelete = $alreadyAddedUsers; |
||||
if (!empty($newUserList)) { |
||||
$usersToDelete = array_diff($alreadyAddedUsers, $newUserList); |
||||
} |
||||
|
||||
if ($usersToDelete) { |
||||
$this->unsubcribeUsersToItem( |
||||
$course, |
||||
$session, |
||||
$lp, |
||||
$usersToDelete |
||||
); |
||||
} |
||||
} |
||||
|
||||
foreach ($newUserList as $userId) { |
||||
if (!in_array($userId, $alreadyAddedUsers)) { |
||||
$userObj = $user->find($userId); |
||||
if ($userObj) { |
||||
$item = new CLpRelUser(); |
||||
$item |
||||
->setUser($userObj) |
||||
->setCourse($course) |
||||
->setLp($lp) |
||||
->setCreatedAt(api_get_utc_datetime(null, false, true)) |
||||
->setCreatorUser(api_get_user_entity()); |
||||
|
||||
if (!empty($session)) { |
||||
$item->setSession($session); |
||||
} |
||||
$em->persist($item); //$em is an instance of EntityManager |
||||
} |
||||
} |
||||
} |
||||
|
||||
$em->flush(); |
||||
} |
||||
|
||||
/** |
||||
* Unsubscribe users to Lp. |
||||
* |
||||
* @param Course $course |
||||
* @param Session $session |
||||
* @param CLp $lp |
||||
* @param array $usersToDelete |
||||
*/ |
||||
public function unsubcribeUsersToItem( |
||||
Course $course, |
||||
Session $session = null, |
||||
CLp $lp, |
||||
$usersToDelete |
||||
) { |
||||
$em = $this->getEntityManager(); |
||||
|
||||
if (!empty($usersToDelete)) { |
||||
foreach ($usersToDelete as $userId) { |
||||
$userId = (int) $userId; |
||||
$item = $this->findOneBy( |
||||
[ |
||||
'course' => $course, |
||||
'session' => $session, |
||||
'lp' => $lp, |
||||
'user' => api_get_user_entity($userId), |
||||
] |
||||
); |
||||
if ($item) { |
||||
$em->remove($item); |
||||
} |
||||
} |
||||
$em->flush(); |
||||
} |
||||
} |
||||
|
||||
} |
Loading…
Reference in new issue