parent
							
								
									24081381b1
								
							
						
					
					
						commit
						c34412ebff
					
				@ -0,0 +1,87 @@ | 
				
			||||
<?php | 
				
			||||
 | 
				
			||||
declare(strict_types=1); | 
				
			||||
 | 
				
			||||
namespace Chamilo\CoreBundle\Migrations\Schema\V200; | 
				
			||||
 | 
				
			||||
use Chamilo\CoreBundle\Entity\GradebookCertificate; | 
				
			||||
use Chamilo\CoreBundle\Entity\PersonalFile; | 
				
			||||
use Chamilo\CoreBundle\Migrations\AbstractMigrationChamilo; | 
				
			||||
use Doctrine\DBAL\Schema\Schema; | 
				
			||||
use Symfony\Component\HttpFoundation\File\UploadedFile; | 
				
			||||
 | 
				
			||||
final class Version20240128205500 extends AbstractMigrationChamilo | 
				
			||||
{ | 
				
			||||
    public function getDescription(): string | 
				
			||||
    { | 
				
			||||
        return 'Migrate certificate files of users to personal_files'; | 
				
			||||
    } | 
				
			||||
 | 
				
			||||
    public function up(Schema $schema): void | 
				
			||||
    { | 
				
			||||
        $container = $this->getContainer(); | 
				
			||||
        $em = $this->getEntityManager(); | 
				
			||||
 | 
				
			||||
        $kernel = $container->get('kernel'); | 
				
			||||
        $rootPath = $kernel->getProjectDir(); | 
				
			||||
 | 
				
			||||
        $q = $em->createQuery('SELECT u FROM Chamilo\CoreBundle\Entity\User u'); | 
				
			||||
 | 
				
			||||
        foreach ($q->toIterable() as $userEntity) { | 
				
			||||
            $id = $userEntity->getId(); | 
				
			||||
            $path = 'users/' . substr((string) $id, 0, 1) . '/' . $id . '/'; | 
				
			||||
 | 
				
			||||
            $certificateDir = $rootPath . '/app/upload/' . $path . 'certificate/'; | 
				
			||||
 | 
				
			||||
            if (!is_dir($certificateDir)) { | 
				
			||||
                continue; | 
				
			||||
            } | 
				
			||||
 | 
				
			||||
            $files = glob($certificateDir . '*'); | 
				
			||||
 | 
				
			||||
            foreach ($files as $file) { | 
				
			||||
                if (!is_file($file)) { | 
				
			||||
                    continue; | 
				
			||||
                } | 
				
			||||
 | 
				
			||||
                $originalTitle = basename($file); | 
				
			||||
 | 
				
			||||
                // Search in gradebook_certificate for a record with a path_certificate that matches $originalTitle | 
				
			||||
                $certificate = $em->getRepository(GradebookCertificate::class)->findOneBy(['pathCertificate' => '/' . $originalTitle]); | 
				
			||||
                if (!$certificate) { | 
				
			||||
                    // If not found, continue with the next file | 
				
			||||
                    continue; | 
				
			||||
                } | 
				
			||||
 | 
				
			||||
                $catId = null !== $certificate->getCategory() ? $certificate->getCategory()->getId() : 0; | 
				
			||||
                $newTitle = hash('sha256', $id . $catId) . '.html'; | 
				
			||||
 | 
				
			||||
                $existingFile = $em->getRepository(PersonalFile::class)->findOneBy(['title' => $newTitle]); | 
				
			||||
                if ($existingFile) { | 
				
			||||
                    error_log('MIGRATIONS :: Skipping file -- ' . $file . ' (Already exists)'); | 
				
			||||
                    continue; | 
				
			||||
                } | 
				
			||||
 | 
				
			||||
                error_log('MIGRATIONS :: Processing file -- ' . $file); | 
				
			||||
 | 
				
			||||
                $personalFile = new PersonalFile(); | 
				
			||||
                $personalFile->setTitle($newTitle); | 
				
			||||
                $personalFile->setCreator($userEntity); | 
				
			||||
                $personalFile->setParentResourceNode($userEntity->getResourceNode()->getId()); | 
				
			||||
                $personalFile->setResourceName($newTitle); | 
				
			||||
                $mimeType = mime_content_type($file); | 
				
			||||
                $uploadedFile = new UploadedFile($file, $newTitle, $mimeType, null, true); | 
				
			||||
                $personalFile->setUploadFile($uploadedFile); | 
				
			||||
                $personalFile->addUserLink($userEntity); | 
				
			||||
 | 
				
			||||
                $em->persist($personalFile); | 
				
			||||
                $em->flush(); | 
				
			||||
 | 
				
			||||
                // Update the record in gradebook_certificate with the new title | 
				
			||||
                $certificate->setPathCertificate('/' . $newTitle); | 
				
			||||
                $em->flush(); | 
				
			||||
 | 
				
			||||
            } | 
				
			||||
        } | 
				
			||||
    } | 
				
			||||
} | 
				
			||||
@ -0,0 +1,149 @@ | 
				
			||||
<?php | 
				
			||||
 | 
				
			||||
declare(strict_types=1); | 
				
			||||
 | 
				
			||||
/* For licensing terms, see /license.txt */ | 
				
			||||
 | 
				
			||||
namespace Chamilo\CoreBundle\Repository; | 
				
			||||
 | 
				
			||||
use Chamilo\CoreBundle\Entity\GradebookCategory; | 
				
			||||
use Chamilo\CoreBundle\Entity\GradebookCertificate; | 
				
			||||
use Chamilo\CoreBundle\Entity\PersonalFile; | 
				
			||||
use Chamilo\CoreBundle\Entity\User; | 
				
			||||
use DateTime; | 
				
			||||
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository; | 
				
			||||
use Doctrine\ORM\AbstractQuery; | 
				
			||||
use Doctrine\Persistence\ManagerRegistry; | 
				
			||||
use Symfony\Component\HttpFoundation\File\UploadedFile; | 
				
			||||
 | 
				
			||||
class GradebookCertificateRepository  extends ServiceEntityRepository | 
				
			||||
{ | 
				
			||||
    public function __construct(ManagerRegistry $registry) | 
				
			||||
    { | 
				
			||||
        parent::__construct($registry, GradebookCertificate::class); | 
				
			||||
    } | 
				
			||||
 | 
				
			||||
    public function getCertificateByUserId(?int $catId, int $userId, bool $asArray = false) | 
				
			||||
    { | 
				
			||||
        $qb = $this->createQueryBuilder('gc') | 
				
			||||
            ->where('gc.user = :userId') | 
				
			||||
            ->setParameter('userId', $userId) | 
				
			||||
            ->setMaxResults(1); | 
				
			||||
 | 
				
			||||
        if ($catId === 0) { | 
				
			||||
            $catId = null; | 
				
			||||
        } | 
				
			||||
 | 
				
			||||
        if (null === $catId) { | 
				
			||||
            $qb->andWhere('gc.category IS NULL'); | 
				
			||||
        } else { | 
				
			||||
            $qb->andWhere('gc.category = :catId') | 
				
			||||
                ->setParameter('catId', $catId); | 
				
			||||
        } | 
				
			||||
 | 
				
			||||
        $qb->orderBy('gc.id', 'ASC'); | 
				
			||||
 | 
				
			||||
        $query = $qb->getQuery(); | 
				
			||||
 | 
				
			||||
        if ($asArray) { | 
				
			||||
            try { | 
				
			||||
                return $query->getOneOrNullResult(AbstractQuery::HYDRATE_ARRAY); | 
				
			||||
            } catch (\Doctrine\ORM\NonUniqueResultException $e) { | 
				
			||||
                return null; | 
				
			||||
            } | 
				
			||||
        } else { | 
				
			||||
            try { | 
				
			||||
                return $query->getOneOrNullResult(); | 
				
			||||
            } catch (\Doctrine\ORM\NonUniqueResultException $e) { | 
				
			||||
                return null; | 
				
			||||
            } | 
				
			||||
        } | 
				
			||||
    } | 
				
			||||
 | 
				
			||||
    public function registerUserInfoAboutCertificate(int $catId, int $userId, float $scoreCertificate, string $fileName = ''): void | 
				
			||||
    { | 
				
			||||
        $existingCertificate = $this->getCertificateByUserId($catId === 0 ? null : $catId, $userId); | 
				
			||||
 | 
				
			||||
        if (!$existingCertificate) { | 
				
			||||
            $certificate = new GradebookCertificate(); | 
				
			||||
 | 
				
			||||
            $category = $catId === 0 ? null : $this->_em->getRepository(GradebookCategory::class)->find($catId); | 
				
			||||
            $user = $this->_em->getRepository(User::class)->find($userId); | 
				
			||||
 | 
				
			||||
            if (!empty($fileName)) { | 
				
			||||
                $fileName = '/'.$fileName; | 
				
			||||
            } | 
				
			||||
 | 
				
			||||
            if ($category) { | 
				
			||||
                $certificate->setCategory($category); | 
				
			||||
            } | 
				
			||||
            $certificate->setUser($user); | 
				
			||||
            $certificate->setPathCertificate($fileName); | 
				
			||||
            $certificate->setScoreCertificate($scoreCertificate); | 
				
			||||
            $certificate->setCreatedAt(new DateTime()); | 
				
			||||
 | 
				
			||||
            $this->_em->persist($certificate); | 
				
			||||
            $this->_em->flush(); | 
				
			||||
        } | 
				
			||||
    } | 
				
			||||
 | 
				
			||||
    public function generateCertificatePersonalFile(int $userId, string $fileName, string $certificateContent): ?PersonalFile | 
				
			||||
    { | 
				
			||||
        $em = $this->getEntityManager(); | 
				
			||||
        $userEntity = $em->getRepository(User::class)->find($userId); | 
				
			||||
 | 
				
			||||
        $existingFile = $em->getRepository(PersonalFile::class)->findOneBy(['title' => $fileName]); | 
				
			||||
 | 
				
			||||
        if (!$existingFile) { | 
				
			||||
            $tempFilePath = tempnam(sys_get_temp_dir(), 'cert'); | 
				
			||||
            file_put_contents($tempFilePath, $certificateContent); | 
				
			||||
 | 
				
			||||
            $mimeType = mime_content_type($tempFilePath); | 
				
			||||
            $uploadedFile = new UploadedFile($tempFilePath, $fileName, $mimeType, null, true); | 
				
			||||
 | 
				
			||||
            $personalFile = new PersonalFile(); | 
				
			||||
            $personalFile->setTitle($fileName); | 
				
			||||
            $personalFile->setCreator($userEntity); | 
				
			||||
            $personalFile->setParentResourceNode($userEntity->getResourceNode()->getId()); | 
				
			||||
            $personalFile->setResourceName($fileName); | 
				
			||||
            $personalFile->setUploadFile($uploadedFile); | 
				
			||||
            $personalFile->addUserLink($userEntity); | 
				
			||||
 | 
				
			||||
            $em->persist($personalFile); | 
				
			||||
            $em->flush(); | 
				
			||||
 | 
				
			||||
            unlink($tempFilePath); | 
				
			||||
 | 
				
			||||
            return $personalFile; | 
				
			||||
        } | 
				
			||||
 | 
				
			||||
        return $existingFile; | 
				
			||||
    } | 
				
			||||
 | 
				
			||||
    public function deleteCertificateAndRelatedFiles(int $userId, int $catId): bool | 
				
			||||
    { | 
				
			||||
        $em = $this->getEntityManager(); | 
				
			||||
        $certificate = $this->getCertificateByUserId($catId, $userId); | 
				
			||||
 | 
				
			||||
        if (!$certificate) { | 
				
			||||
 | 
				
			||||
            return false; | 
				
			||||
        } | 
				
			||||
 | 
				
			||||
        $title = basename(ltrim($certificate->getPathCertificate(), '/')); | 
				
			||||
        $personalFile = $em->getRepository(PersonalFile::class)->findOneBy(['title' => $title]); | 
				
			||||
 | 
				
			||||
        if (!$personalFile) { | 
				
			||||
 | 
				
			||||
            return false; | 
				
			||||
        } | 
				
			||||
 | 
				
			||||
        $em->remove($personalFile); | 
				
			||||
        $em->flush(); | 
				
			||||
 | 
				
			||||
        $em->remove($certificate); | 
				
			||||
        $em->flush(); | 
				
			||||
 | 
				
			||||
        return true; | 
				
			||||
    } | 
				
			||||
} | 
				
			||||
					Loading…
					
					
				
		Reference in new issue