From 1cb6b828c0afcdef0ce2c81514adc840f9686dd9 Mon Sep 17 00:00:00 2001 From: Julio Montoya Date: Wed, 30 Sep 2020 16:19:22 +0200 Subject: [PATCH] Internal - Fix entities, fix agenda event attachment using resources. --- public/main/calendar/agenda_list.php | 4 +- public/main/inc/lib/agenda.lib.php | 85 ++++++------------- public/main/inc/lib/document.lib.php | 3 +- .../Repository/ResourceRepository.php | 1 - src/CoreBundle/Traits/ControllerTrait.php | 2 + .../Entity/CCalendarEventAttachment.php | 60 ------------- .../Repository/CDocumentRepository.php | 33 +------ 7 files changed, 35 insertions(+), 153 deletions(-) diff --git a/public/main/calendar/agenda_list.php b/public/main/calendar/agenda_list.php index f75e038d97..a992d7aa26 100644 --- a/public/main/calendar/agenda_list.php +++ b/public/main/calendar/agenda_list.php @@ -89,11 +89,11 @@ $tpl->assign('agenda_actions', $actions); $tpl->assign('is_allowed_to_edit', api_is_allowed_to_edit()); if (api_is_allowed_to_edit()) { - if ('change_visibility' == $action) { + if ('change_visibility' === $action) { $courseInfo = api_get_course_info(); $courseCondition = ''; // This happens when list agenda is not inside a course - if (('course' == $type || 'session' == $type && !empty($courseInfo))) { + if (('course' === $type || 'session' === $type) && !empty($courseInfo)) { // For course and session event types // Just needs course ID $agenda->changeVisibility($_GET['id'], $_GET['visibility'], $courseInfo); diff --git a/public/main/inc/lib/agenda.lib.php b/public/main/inc/lib/agenda.lib.php index 610f965fcf..eafed99452 100644 --- a/public/main/inc/lib/agenda.lib.php +++ b/public/main/inc/lib/agenda.lib.php @@ -247,10 +247,10 @@ class Agenda $content, $usersToSend = [], $addAsAnnouncement = false, - $parentEventId = null, + $parentEventId = 0, $attachmentArray = [], $attachmentCommentList = [], - $eventComment = null, + $eventComment = '', $color = '' ) { $start = api_get_utc_datetime($start, false, true); @@ -1032,30 +1032,28 @@ class Agenda ); } - if (api_is_drh()) { - if (api_drh_can_access_all_session_content()) { - $session_list = []; - $sessionList = SessionManager::get_sessions_followed_by_drh( - api_get_user_id(), - null, - null, - null, - true, - false - ); + if (api_is_drh() && api_drh_can_access_all_session_content()) { + $session_list = []; + $sessionList = SessionManager::get_sessions_followed_by_drh( + api_get_user_id(), + null, + null, + null, + true, + false + ); - if (!empty($sessionList)) { - foreach ($sessionList as $sessionItem) { - $sessionId = $sessionItem['id']; - $courses = SessionManager::get_course_list_by_session_id( - $sessionId - ); - $sessionInfo = [ - 'session_id' => $sessionId, - 'courses' => $courses, - ]; - $session_list[] = $sessionInfo; - } + if (!empty($sessionList)) { + foreach ($sessionList as $sessionItem) { + $sessionId = $sessionItem['id']; + $courses = SessionManager::get_course_list_by_session_id( + $sessionId + ); + $sessionInfo = [ + 'session_id' => $sessionId, + 'courses' => $courses, + ]; + $session_list[] = $sessionInfo; } } } @@ -2494,36 +2492,14 @@ class Agenda } if ($valid) { - /*$courseDir = $courseInfo['directory'].'/upload/calendar'; - $sys_course_path = api_get_path(SYS_COURSE_PATH); - $uploadDir = $sys_course_path.$courseDir;*/ - - // Try to add an extension to the file if it hasn't one - /*$new_file_name = add_ext_on_mime( - stripslashes($fileUserUpload['name']), - $fileUserUpload['type'] - );*/ - // user's file name $fileName = $file->getClientOriginalName(); - $courseId = api_get_course_int_id(); - /*$new_file_name = uniqid(''); - $new_path = $uploadDir.'/'.$new_file_name; - $result = @move_uploaded_file( - $fileUserUpload['tmp_name'], - $new_path - ); - $courseId = api_get_course_int_id(); - $size = intval($fileUserUpload['size']);*/ - // Storing the attachments if any - //if ($result) { + $attachment = new CCalendarEventAttachment(); $attachment ->setFilename($fileName) ->setComment($comment) - ->setPath($fileName) ->setEvent($event) - ->setSize($file->getSize()) ->setParent($event) ->addCourseLink( api_get_course_entity(), @@ -2535,16 +2511,9 @@ class Agenda $repo->getEntityManager()->persist($attachment); $repo->getEntityManager()->flush(); - $id = $attachment->getIid(); - if ($id) { - /*api_item_property_update( - $courseInfo, - 'calendar_event_attachment', - $id, - 'AgendaAttachmentAdded', - api_get_user_id() - );*/ - } + $repo->addFile($attachment, $file); + $repo->getEntityManager()->persist($attachment); + $repo->getEntityManager()->flush(); } } diff --git a/public/main/inc/lib/document.lib.php b/public/main/inc/lib/document.lib.php index e297ded920..f0200e036b 100644 --- a/public/main/inc/lib/document.lib.php +++ b/public/main/inc/lib/document.lib.php @@ -2793,8 +2793,9 @@ class DocumentManager public static function enough_space($file_size, $max_dir_space) { if ($max_dir_space) { + $courseEntity = api_get_course_entity(); $repo = Container::getDocumentRepository(); - $total = $repo->getTotalSpace(api_get_course_int_id()); + $total = $repo->getFolderSize($courseEntity->getResourceNode(), $courseEntity); if (($file_size + $total) > $max_dir_space) { return false; diff --git a/src/CoreBundle/Repository/ResourceRepository.php b/src/CoreBundle/Repository/ResourceRepository.php index 4dcf8aeae2..6663d58d77 100644 --- a/src/CoreBundle/Repository/ResourceRepository.php +++ b/src/CoreBundle/Repository/ResourceRepository.php @@ -229,7 +229,6 @@ class ResourceRepository extends EntityRepository } } - $em->persist($resourceNode); $em->persist($resource); $em->flush(); diff --git a/src/CoreBundle/Traits/ControllerTrait.php b/src/CoreBundle/Traits/ControllerTrait.php index c1d464a51c..33213757ff 100644 --- a/src/CoreBundle/Traits/ControllerTrait.php +++ b/src/CoreBundle/Traits/ControllerTrait.php @@ -10,6 +10,7 @@ use Chamilo\CoreBundle\Repository\IllustrationRepository; use Chamilo\CoreBundle\Repository\ResourceFactory; use Chamilo\CourseBundle\Repository\CAnnouncementAttachmentRepository; use Chamilo\CourseBundle\Repository\CAnnouncementRepository; +use Chamilo\CourseBundle\Repository\CCalendarEventAttachmentRepository; use Chamilo\CourseBundle\Repository\CDocumentRepository; use Knp\Menu\FactoryInterface as MenuFactoryInterface; use Sylius\Bundle\SettingsBundle\Form\Factory\SettingsFormFactory; @@ -33,6 +34,7 @@ trait ControllerTrait $services[] = CDocumentRepository::class; $services[] = CAnnouncementRepository::class; $services[] = CAnnouncementAttachmentRepository::class; + $services[] = CCalendarEventAttachmentRepository::class; /*$services[] = CAttendanceRepository::class; $services[] = CDocumentRepository::class; diff --git a/src/CourseBundle/Entity/CCalendarEventAttachment.php b/src/CourseBundle/Entity/CCalendarEventAttachment.php index 6d94398713..c7bfc6cc46 100644 --- a/src/CourseBundle/Entity/CCalendarEventAttachment.php +++ b/src/CourseBundle/Entity/CCalendarEventAttachment.php @@ -29,13 +29,6 @@ class CCalendarEventAttachment extends AbstractResource implements ResourceInter */ protected $iid; - /** - * @var string - * - * @ORM\Column(name="path", type="string", length=255, nullable=false) - */ - protected $path; - /** * @var string * @@ -43,13 +36,6 @@ class CCalendarEventAttachment extends AbstractResource implements ResourceInter */ protected $comment; - /** - * @var int - * - * @ORM\Column(name="size", type="integer", nullable=false) - */ - protected $size; - /** * @var string * @@ -70,28 +56,6 @@ class CCalendarEventAttachment extends AbstractResource implements ResourceInter return $this->getFilename(); } - /** - * Set path. - * - * @param string $path - */ - public function setPath($path): self - { - $this->path = $path; - - return $this; - } - - /** - * Get path. - * - * @return string - */ - public function getPath() - { - return $this->path; - } - /** * Set comment. * @@ -114,30 +78,6 @@ class CCalendarEventAttachment extends AbstractResource implements ResourceInter return $this->comment; } - /** - * Set size. - * - * @param int $size - * - * @return CCalendarEventAttachment - */ - public function setSize($size) - { - $this->size = $size; - - return $this; - } - - /** - * Get size. - * - * @return int - */ - public function getSize() - { - return $this->size; - } - /** * Set filename. * diff --git a/src/CourseBundle/Repository/CDocumentRepository.php b/src/CourseBundle/Repository/CDocumentRepository.php index 9d301380aa..4540c28401 100644 --- a/src/CourseBundle/Repository/CDocumentRepository.php +++ b/src/CourseBundle/Repository/CDocumentRepository.php @@ -119,38 +119,9 @@ final class CDocumentRepository extends ResourceRepository implements GridInterf return null; } - /** - * @param int $courseId - * @param string $path - * - * @throws \Doctrine\ORM\NonUniqueResultException - */ - public function getFolderSize($courseId, $path) + public function getFolderSize(ResourceNode $resourceNode, Course $course, Session $session = null): int { - $path = str_replace('_', '\_', $path); - $addedSlash = '/' === $path ? '' : '/'; - - $repo = $this->getRepository(); - $qb = $repo->createQueryBuilder('d'); - $query = $qb - ->select('SUM(d.size)') - ->innerJoin('d.resourceNode', 'r') - ->innerJoin('r.resourceLinks', 'l') - ->where('d.path LIKE :path') - ->andWhere('d.path NOT LIKE :deleted') - ->andWhere('d.path NOT LIKE :extra_path ') - ->andWhere('l.visibility <> :visibility') - ->andWhere('d.course = :course') - ->setParameters([ - 'path' => $path.$addedSlash.'%', - 'extra_path' => $path.$addedSlash.'%/%', - 'course' => $courseId, - 'deleted' => '%_DELETED_%', - 'visibility' => ResourceLink::VISIBILITY_DELETED, - ]) - ->getQuery(); - - return $query->getSingleScalarResult(); + return $this->getResourceNodeRepository()->getSize($resourceNode, $this->getResourceType(), $course, $session); } /**