Skip to content

Commit

Permalink
Fixing call and queries with references to resourceNode.resourceFiles c…
Browse files Browse the repository at this point in the history
  • Loading branch information
AngelFQC committed Jul 2, 2024
1 parent 82466cd commit fac8dec
Show file tree
Hide file tree
Showing 18 changed files with 43 additions and 38 deletions.
2 changes: 1 addition & 1 deletion public/main/exercise/annotation_user.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@

$answer_type = $objQuestion->getType(); //very important

$resourceFile = $objQuestion->getResourceNode()->getResourceFile();
$resourceFile = $objQuestion->getResourceNode()->getResourceFiles()->first();
$pictureWidth = $resourceFile->getWidth();
$pictureHeight = $resourceFile->getHeight();
$imagePath = $questionRepo->getHotSpotImageUrl($objQuestion);
Expand Down
2 changes: 1 addition & 1 deletion public/main/exercise/hotspot_actionscript.as.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
if (!$objQuestion->getResourceNode()->hasResourceFile()) {
api_not_allowed();
}
$resourceFile = $objQuestion->getResourceNode()->getResourceFile();
$resourceFile = $objQuestion->getResourceNode()->getResourceFiles()->first();
$pictureWidth = $resourceFile->getWidth();
$pictureHeight = $resourceFile->getHeight();
$imagePath = $questionRepo->getHotSpotImageUrl($objQuestion).'?'.api_get_cidreq();
Expand Down
2 changes: 1 addition & 1 deletion public/main/exercise/hotspot_actionscript_admin.as.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
if (!$objQuestion->getResourceNode()->hasResourceFile()) {
api_not_allowed();
}
$resourceFile = $objQuestion->getResourceNode()->getResourceFile();
$resourceFile = $objQuestion->getResourceNode()->getResourceFiles()->first();
$pictureWidth = $resourceFile->getWidth();
$pictureHeight = $resourceFile->getHeight();
$imagePath = $questionRepo->getHotSpotImageUrl($objQuestion).'?'.api_get_cidreq();
Expand Down
2 changes: 1 addition & 1 deletion public/main/exercise/hotspot_answers.as.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@
$answer_type = $objQuestion->getType(); //very important
$TBL_ANSWERS = Database::get_course_table(TABLE_QUIZ_ANSWER);

$resourceFile = $objQuestion->getResourceNode()->getResourceFile();
$resourceFile = $objQuestion->getResourceNode()->getResourceFiles()->first();
$pictureWidth = $resourceFile->getWidth();
$pictureHeight = $resourceFile->getHeight();
$imagePath = $questionRepo->getHotSpotImageUrl($objQuestion).'?'.api_get_cidreq();
Expand Down
2 changes: 1 addition & 1 deletion public/main/gradebook/lib/be/category.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -2077,7 +2077,7 @@ public static function generateUserCertificate(

$html = [];
if (!empty($my_certificate)) {
$pathToCertificate = $category->getDocument()->getResourceNode()->getResourceFile()->getFile()->getPathname();
$pathToCertificate = $category->getDocument()->getResourceNode()->getResourceFiles()->first()->getFile()->getPathname();

$certificate_obj = new Certificate(
$my_certificate['id'],
Expand Down
8 changes: 1 addition & 7 deletions public/main/inc/lib/CourseChatUtils.php
Original file line number Diff line number Diff line change
Expand Up @@ -485,13 +485,7 @@ public function readMessages($reset = false, $friendId = 0)
$resourceNode = $resource->getResourceNode();
}

if ($resourceNode->hasResourceFile()) {
//$resourceFile = $resourceNode->getResourceFile();
//$fileName = $this->getFilename($resourceFile);
return $this->repository->getResourceNodeFileContent($resourceNode);
}

return '';
return $this->repository->getResourceNodeFileContent($resourceNode);

$remove = 0;
$content = [];
Expand Down
1 change: 0 additions & 1 deletion public/main/inc/lib/document.lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -2272,7 +2272,6 @@ class="moved ui-sortable-handle link_with_id"
->from(ResourceNode::class, 'node')
->innerJoin('node.resourceType', 'type')
->innerJoin('node.resourceLinks', 'links')
->leftJoin('node.resourceFile', 'file')
->where('type = :type')
->andWhere('links.course = :course')
->setParameters(['type' => $type, 'course' => $course])
Expand Down
6 changes: 1 addition & 5 deletions public/main/lp/learnpath.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -8748,10 +8748,6 @@ private function getSavedFinalItem()
/** @var CDocument $document */
$document = $repo->find($finalItem->path);

if ($document && $document->getResourceNode()->hasResourceFile()) {
return $repo->getResourceFileContent($document);
}

return '';
return $document ? $repo->getResourceFileContent($document) : '';
}
}
2 changes: 1 addition & 1 deletion public/main/lp/learnpathItem.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -4035,7 +4035,7 @@ public function addAudio()
);*/

if ($document) {
$name = '/audio/'.$document->getResourceNode()->getResourceFile()->getOriginalName();
$name = '/audio/'.$document->getResourceNode()->getResourceFiles()->first()->getOriginalName();
// Store the mp3 file in the lp_item table.
$table = Database::get_course_table(TABLE_LP_ITEM);
$sql = "UPDATE $table SET
Expand Down
7 changes: 5 additions & 2 deletions public/main/lp/lp_edit.php
Original file line number Diff line number Diff line change
Expand Up @@ -333,8 +333,11 @@ function activate_end_date() {
}

if (isset($_REQUEST['remove_picture']) && $_REQUEST['remove_picture']) {
if ($lp->getResourceNode()->hasResourceFile()) {
$lp->getResourceNode()->setResourceFile(null);
$resourceFiles = $lp->getResourceNode()->getResourceFiles();

foreach ($resourceFiles as $resourceFile) {
$em->remove($resourceFile);
$em->flush();
}
}

Expand Down
9 changes: 3 additions & 6 deletions public/main/work/work.lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -3987,7 +3987,7 @@ function getWorkComment(CStudentPublicationComment $commentEntity, array $course
$filePath = '';
$deleteUrl = api_get_path(WEB_CODE_PATH).
'work/view.php?'.api_get_cidreq().'&id='.$workId.'&action=delete_attachment&comment_id='.$id;
$fileName = $commentEntity->getResourceNode()->getResourceFile()->getTitle();
$fileName = $commentEntity->getResourceNode()->getResourceFiles()->first()->getTitle();
}
$comment['comment'] = $commentEntity->getComment();
$comment['delete_file_url'] = $deleteUrl;
Expand Down Expand Up @@ -4016,10 +4016,7 @@ function deleteCommentFile($id, $courseInfo = [])
/** @var CStudentPublicationComment $commentEntity */
$commentEntity = $repo->findOneBy($criteria);

if ($commentEntity->getResourceNode()->hasResourceFile()) {
$file = $commentEntity->getResourceNode()->getResourceFile();

$commentEntity->getResourceNode()->setResourceFile(null);
foreach ($commentEntity->getResourceNode()->getResourceFiles() as $file) {
$em->remove($file);
$em->flush();
}
Expand Down Expand Up @@ -5969,7 +5966,7 @@ function getFileContents($id, $courseInfo, $sessionId = 0, $correction = false,
$title = $titleCorrection = $studentPublication->getCorrection()->getTitle();
}
if ($hasFile) {
$title = $studentPublication->getResourceNode()->getResourceFile()->getTitle();
$title = $studentPublication->getResourceNode()->getResourceFiles()->first()->getTitle();
}

$title = str_replace(' ', '_', $title);
Expand Down
10 changes: 8 additions & 2 deletions src/CoreBundle/Controller/ResourceController.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Criteria;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\Query\Expr\Join;
use Symfony\Bundle\SecurityBundle\Security;
use Symfony\Component\Filesystem\Exception\FileNotFoundException;
use Symfony\Component\HttpFoundation\JsonResponse;
Expand Down Expand Up @@ -251,12 +252,17 @@ public function download(Request $request, EntityManagerInterface $entityManager
$type = $repo->getResourceType();

$criteria = Criteria::create()
->where(Criteria::expr()->neq('resourceFile', null)) // must have a file
->where(Criteria::expr()->neq('resourceFiles', null)) // must have a file
->andWhere(Criteria::expr()->eq('resourceType', $type)) // only download same type
;

$qb = $resourceNodeRepo->getChildrenQueryBuilder($resourceNode);
$qb->addCriteria($criteria);
$qbAlias = $qb->getRootAliases()[0];

$qb
->leftJoin(sprintf('%s.resourceFiles', $qbAlias), 'resourceFiles') // must have a file
->addCriteria($criteria)
;

/** @var ArrayCollection|ResourceNode[] $children */
$children = $qb->getQuery()->getResult();
Expand Down
10 changes: 8 additions & 2 deletions src/CoreBundle/Entity/ResourceNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@
],
]
)]
#[ApiFilter(filterClass: OrderFilter::class, properties: ['id', 'title', 'resourceFile', 'createdAt', 'updatedAt'])]
#[ApiFilter(filterClass: OrderFilter::class, properties: ['id', 'title', 'createdAt', 'updatedAt'])]
#[ApiFilter(filterClass: PropertyFilter::class)]
#[ApiFilter(filterClass: SearchFilter::class, properties: ['title' => 'partial'])]
class ResourceNode implements Stringable
Expand Down Expand Up @@ -186,7 +186,13 @@ class ResourceNode implements Stringable
*
* @var Collection<int, ResourceFile>
*/
#[ORM\OneToMany(mappedBy: 'resourceNode', targetEntity: ResourceFile::class, cascade: ['persist'])]
#[Groups(['resource_node:read', 'resource_node:write', 'document:read', 'document:write', 'message:read'])]
#[ORM\OneToMany(
mappedBy: 'resourceNode',
targetEntity: ResourceFile::class,
cascade: ['persist', 'remove'],
fetch: 'EXTRA_LAZY',
)]
private Collection $resourceFiles;

public function __construct()
Expand Down
2 changes: 1 addition & 1 deletion src/CoreBundle/Repository/Node/IllustrationRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public function getIllustrationNodeFromParent(ResourceNode $resourceNode): ?Reso
->select('node')
->from(ResourceNode::class, 'node')
->innerJoin('node.resourceType', 'type')
->innerJoin('node.resourceFile', 'file')
->innerJoin('node.resourceFiles', 'file')
->where('node.parent = :parent')
->andWhere('type.title = :name')
->setParameters([
Expand Down
2 changes: 1 addition & 1 deletion src/CoreBundle/Repository/ResourceNodeRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ public function getSize(ResourceNode $resourceNode, ResourceType $type, ?Course
{
$qb = $this->createQueryBuilder('node')
->select('SUM(file.size) as total')
->innerJoin('node.resourceFile', 'file')
->innerJoin('node.resourceFiles', 'file')
->innerJoin('node.resourceLinks', 'l')
->where('node.resourceType = :type')
->andWhere('node.parent = :parentNode')
Expand Down
4 changes: 2 additions & 2 deletions src/CoreBundle/Repository/ResourceRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,7 @@ public function getResources(?ResourceNode $parentNode = null): QueryBuilder
->innerJoin('resource.resourceNode', 'node')
->innerJoin('node.resourceLinks', 'links')
->innerJoin('node.resourceType', 'type')
->leftJoin('node.resourceFile', 'file')
->leftJoin('node.resourceFiles', 'file')
->where('type.title = :type')
->setParameter('type', $resourceTypeName, Types::STRING)
->addSelect('node')
Expand Down Expand Up @@ -695,7 +695,7 @@ public function getTotalSpaceByCourse(Course $course, ?CGroup $group = null, ?Se
->select('SUM(file.size) as total')
->innerJoin('resource.resourceNode', 'node')
->innerJoin('node.resourceLinks', 'l')
->innerJoin('node.resourceFile', 'file')
->innerJoin('node.resourceFiles', 'file')
->where('l.course = :course')
->andWhere('file IS NOT NULL')
->setParameters(
Expand Down
8 changes: 5 additions & 3 deletions tests/CoreBundle/Repository/MessageRepositoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -346,9 +346,11 @@ public function testCreateMessageWithAttachment(): void
'@type' => 'http://schema.org/MediaObject',
'resourceNode' => [
'@type' => 'ResourceNode',
'resourceFile' => [
'@type' => 'http://schema.org/MediaObject',
'@id' => $resourceFileId,
'resourceFiles' => [
[
'@type' => 'http://schema.org/MediaObject',
'@id' => $resourceFileId,
],
],
],
],
Expand Down
2 changes: 2 additions & 0 deletions tests/CoreBundle/Repository/MessageTagRepositoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
namespace Chamilo\Tests\CoreBundle\Repository;

use Chamilo\CoreBundle\Entity\MessageTag;
use Chamilo\CoreBundle\Framework\Container;
use Chamilo\CoreBundle\Repository\MessageTagRepository;
use Chamilo\CoreBundle\Repository\Node\UserRepository;
use Chamilo\Tests\AbstractApiTest;
Expand All @@ -19,6 +20,7 @@ class MessageTagRepositoryTest extends AbstractApiTest

public function testCreateTagAndDeleteUser(): void
{
Container::$container = self::getContainer();
$tagRepo = self::getContainer()->get(MessageTagRepository::class);

$testUser = $this->createUser('test');
Expand Down

0 comments on commit fac8dec

Please sign in to comment.