diff --git a/main/document/document.php b/main/document/document.php index 7661a524dc..70192548f5 100755 --- a/main/document/document.php +++ b/main/document/document.php @@ -3,6 +3,7 @@ use Chamilo\CoreBundle\Entity\Resource\ResourceLink; use Chamilo\CoreBundle\Entity\Resource\ResourceRight; +use Chamilo\CoreBundle\Framework\Container; use Chamilo\CoreBundle\Security\Authorization\Voter\ResourceNodeVoter; use ChamiloSession as Session; @@ -1420,6 +1421,9 @@ if ($isAllowedToEdit || } } Display::addFlash($message); + + header('Location: '.$currentUrl); + exit; } // Show them the form for the directory name @@ -1454,40 +1458,11 @@ if ($isAllowedToEdit) { api_not_allowed(true); } } - + $repo = Container::$container->get('Chamilo\CourseBundle\Repository\CDocumentRepository'); /** @var \Chamilo\CourseBundle\Entity\CDocument $document */ - $document = $em->getRepository('ChamiloCourseBundle:CDocument')->find($update_id); - - $link = $document->getCourseSessionResourceLink(); - - $link->setVisibility($defaultVisibility); - - if ($defaultVisibility === ResourceLink::VISIBILITY_DRAFT) { - $editorMask = ResourceNodeVoter::getEditorMask(); - $rights = []; - $resourceRight = new ResourceRight(); - $resourceRight - ->setMask($editorMask) - ->setRole(ResourceNodeVoter::ROLE_CURRENT_COURSE_TEACHER) - ->setResourceLink($link) - ; - $rights[] = $resourceRight; - - if (!empty($rights)) { - $link->setResourceRight($rights); - /*foreach ($rights as $right) { - $link->addResourceRight($right); - }*/ - } - } else { - $link->setResourceRight([]); - } - $em->persist($link); - $em->flush(); - - Display::addFlash( - Display::return_message(get_lang('VisibilityChanged'), 'confirmation') - ); + $document = $repo->find($update_id); + $total = $repo->setVisibility($document, $defaultVisibility); + Display::addFlash(Display::return_message(get_lang('VisibilityChanged'), 'confirmation')); header('Location: '.$currentUrl); exit; @@ -2058,6 +2033,18 @@ if (count($documentAndFolders) > 1) { if (empty($documentAndFolders)) { echo Display::return_message(get_lang('NoDocsInFolder'), 'warning'); +} +$em = Database::getManager(); + +$repo = $em->getRepository('ChamiloCourseBundle:CDocument'); + +/** @var \Chamilo\CourseBundle\Entity\CDocument $document */ +$document = $repo->find(19); + +if ($document->isVisible()) { + + + } echo ' diff --git a/main/inc/ajax/document.ajax.php b/main/inc/ajax/document.ajax.php index cf0078e036..1c317ea570 100755 --- a/main/inc/ajax/document.ajax.php +++ b/main/inc/ajax/document.ajax.php @@ -27,7 +27,9 @@ switch ($action) { $courseQuota = DocumentManager::get_course_quota(); // Calculating the total space - $total = DocumentManager::documents_total_space(api_get_course_int_id()); + //$total = DocumentManager::getTotalSpace(api_get_course_int_id()); + $repo = Container::$container->get('Chamilo\CourseBundle\Repository\CDocumentRepository'); + $total = $repo->getTotalSpace(api_get_course_int_id()); // Displaying the quota echo DocumentManager::displaySimpleQuota($courseQuota, $total); diff --git a/main/inc/lib/document.lib.php b/main/inc/lib/document.lib.php index 71b65eb8e0..1b4451a4d0 100644 --- a/main/inc/lib/document.lib.php +++ b/main/inc/lib/document.lib.php @@ -1038,9 +1038,6 @@ class DocumentManager $session_id = 0, $remove_content_from_db = false ) { - $TABLE_DOCUMENT = Database::get_course_table(TABLE_DOCUMENT); - $TABLE_ITEMPROPERTY = Database::get_course_table(TABLE_ITEM_PROPERTY); - // Deleting from the DB $user_id = api_get_user_id(); $document_id = intval($document_id); @@ -1053,7 +1050,7 @@ class DocumentManager $session_id = api_get_session_id(); } // Soft DB delete - api_item_property_update( + /*api_item_property_update( $course_info, TOOL_DOCUMENT, $document_id, @@ -1064,13 +1061,20 @@ class DocumentManager null, null, $session_id - ); + );*/ self::delete_document_from_search_engine($course_info['code'], $document_id); self::unsetDocumentAsTemplate($document_id, $course_info['real_id'], $user_id); - //Hard DB delete + // Hard DB delete if ($remove_content_from_db) { - $sql = "DELETE FROM $TABLE_ITEMPROPERTY + $repo = Container::$container->get('Chamilo\CourseBundle\Repository\CDocumentRepository'); + /** @var CDocument $document */ + $document = $repo->find($document_id); + $repo->softDelete($document); + + return true; + + /*$sql = "DELETE FROM $TABLE_ITEMPROPERTY WHERE c_id = {$course_info['real_id']} AND ref = ".$document_id." AND @@ -1079,7 +1083,7 @@ class DocumentManager $sql = "DELETE FROM $TABLE_DOCUMENT WHERE c_id = {$course_info['real_id']} AND id = ".$document_id; - Database::query($sql); + Database::query($sql);*/ } } @@ -1123,10 +1127,6 @@ class DocumentManager return false; } - if (empty($base_work_dir)) { - return false; - } - if (empty($documentId)) { $documentId = self::get_document_id($_course, $path, $sessionId); $docInfo = self::get_document_data_by_id( @@ -1149,18 +1149,16 @@ class DocumentManager $path = $docInfo['path']; } - $em = Database::getManager(); $documentId = (int) $documentId; if (empty($path) || empty($docInfo) || empty($documentId)) { return false; } + $repo = Container::$container->get('Chamilo\CourseBundle\Repository\CDocumentRepository'); /** @var CDocument $document */ - $document = $em->getRepository('ChamiloCourseBundle:CDocument')->find($docInfo['iid']); - $document->setSoftDelete(); - $em->persist($document); - $em->flush(); + $document = $repo->find($docInfo['iid']); + $repo->softDelete($document); return true; @@ -1565,14 +1563,13 @@ class DocumentManager $file_type = 'file' ) { $docTable = Database::get_course_table(TABLE_DOCUMENT); - $propTable = Database::get_course_table(TABLE_ITEM_PROPERTY); $course_id = $course['real_id']; // note the extra / at the end of doc_path to match every path in // the document table that is part of the document path $session_id = intval($session_id); - $condition = "AND d.session_id IN ('$session_id', '0') "; + $condition = " AND d.session_id IN ('$session_id', '0') "; // The " d.filetype='file' " let the user see a file even if the folder is hidden see #2198 /* @@ -1604,14 +1601,11 @@ class DocumentManager } $doc_path = Database::escape_string($doc_path).'/'; - $sql = "SELECT visibility + $sql = "SELECT iid FROM $docTable d - INNER JOIN $propTable ip - ON (d.id = ip.ref AND d.c_id = ip.c_id) WHERE d.c_id = $course_id AND - ip.c_id = $course_id AND - ip.tool = '".TOOL_DOCUMENT."' $condition AND + $condition AND filetype = '$file_type' AND locate(concat(path,'/'), '$doc_path')=1 "; @@ -1620,7 +1614,13 @@ class DocumentManager $is_visible = false; if (Database::num_rows($result) > 0) { $row = Database::fetch_array($result, 'ASSOC'); - if ($row['visibility'] == 1) { + + $em = Database::getManager(); + + $repo = $em->getRepository('ChamiloCourseBundle:CDocument'); + /** @var \Chamilo\CourseBundle\Entity\CDocument $document */ + $document = $repo->find($row['iid']); + if ($document->getVisibility() === ResourceLink::VISIBILITY_PUBLISHED) { $is_visible = api_is_allowed_in_course() || api_is_platform_admin(); } } @@ -1715,9 +1715,8 @@ class DocumentManager $repo = $em->getRepository('ChamiloCourseBundle:CDocument'); /** @var \Chamilo\CourseBundle\Entity\CDocument $document */ $document = $repo->find($doc_id); - $link = $document->getCourseSessionResourceLink(); - if ($link && $link->getVisibility() == ResourceLink::VISIBILITY_PUBLISHED) { + if ($document->isVisible()) { return true; } @@ -6056,50 +6055,40 @@ class DocumentManager return false; } - $itemPropertyTable = Database::get_course_table(TABLE_ITEM_PROPERTY); $documentTable = Database::get_course_table(TABLE_DOCUMENT); - $conditionSession = api_get_session_condition($sessionId, true, false, 'd.session_id'); $courseId = $courseInfo['real_id']; // get invisible folders $sql = "SELECT DISTINCT d.id, path - FROM $itemPropertyTable i - INNER JOIN $documentTable d - ON (i.c_id = d.c_id) - WHERE - d.id = i.ref AND - i.tool = '".TOOL_DOCUMENT."' + FROM $documentTable d + WHERE $conditionSession AND - i.c_id = $courseId AND d.c_id = $courseId "; $result = Database::query($sql); $documents = Database::store_result($result, 'ASSOC'); if ($documents) { - $course_dir = $courseInfo['directory'].'/document'; - $sys_course_path = api_get_path(SYS_COURSE_PATH); - $base_work_dir = $sys_course_path.$course_dir; - foreach ($documents as $document) { $documentId = $document['id']; self::delete_document( $courseInfo, null, - $base_work_dir, + null, $sessionId, $documentId ); } } + /* $sql = "DELETE FROM $documentTable WHERE c_id = $courseId AND session_id = $sessionId"; Database::query($sql); $sql = "DELETE FROM $itemPropertyTable WHERE c_id = $courseId AND session_id = $sessionId AND tool = '".TOOL_DOCUMENT."'"; - Database::query($sql); + Database::query($sql);*/ } /** @@ -6492,8 +6481,6 @@ class DocumentManager $provider = 'sonata.media.provider.image'; } - //error_log("Provider: $provider"); - $media->setProviderName($provider); $media->setEnabled(true); diff --git a/src/CoreBundle/Entity/Resource/ResourceNode.php b/src/CoreBundle/Entity/Resource/ResourceNode.php index 2293bdddd7..ffc3d70237 100644 --- a/src/CoreBundle/Entity/Resource/ResourceNode.php +++ b/src/CoreBundle/Entity/Resource/ResourceNode.php @@ -55,14 +55,16 @@ class ResourceNode protected $resourceType; /** - * @ORM\OneToMany(targetEntity="Chamilo\CoreBundle\Entity\Resource\ResourceLink", mappedBy="resourceNode", cascade={"remove"}) + * @ORM\OneToMany(targetEntity="Chamilo\CoreBundle\Entity\Resource\ResourceLink", mappedBy="resourceNode", + * cascade={"remove"}) */ protected $resourceLinks; /** * @var ResourceFile * - * @ORM\OneToOne(targetEntity="Chamilo\CoreBundle\Entity\Resource\ResourceFile", inversedBy="resourceNode", cascade={"remove"}) + * @ORM\OneToOne(targetEntity="Chamilo\CoreBundle\Entity\Resource\ResourceFile", inversedBy="resourceNode", + * cascade={"remove"}) * @ORM\JoinColumn(name="resource_file_id", referencedColumnName="id") */ protected $resourceFile; diff --git a/src/CourseBundle/Entity/CDocument.php b/src/CourseBundle/Entity/CDocument.php index ba6049ed5e..adf7e22daf 100644 --- a/src/CourseBundle/Entity/CDocument.php +++ b/src/CourseBundle/Entity/CDocument.php @@ -8,7 +8,10 @@ use Chamilo\CoreBundle\Entity\Course; use Chamilo\CoreBundle\Entity\Resource\AbstractResource; use Chamilo\CoreBundle\Entity\Resource\ResourceInterface; use Chamilo\CoreBundle\Entity\Resource\ResourceLink; +use Chamilo\CoreBundle\Entity\Resource\ResourceNode; +use Chamilo\CoreBundle\Entity\Resource\ResourceRight; use Chamilo\CoreBundle\Entity\Session; +use Chamilo\CoreBundle\Security\Authorization\Voter\ResourceNodeVoter; use Doctrine\Common\Collections\Criteria; use Doctrine\ORM\Event\LifecycleEventArgs; use Doctrine\ORM\Mapping as ORM; @@ -353,11 +356,19 @@ class CDocument extends AbstractResource implements ResourceInterface } /** - * This is a "soft delete" only changes visibility, doesn't delete the record. + * Visiblity types ResourceLink::VISIBILITY_DELETED + * + * @return int */ - public function setSoftDelete() + public function getVisibility() + { + return $this->getCourseSessionResourceLink()->getVisibility(); + } + + + public function isVisible() { - $this->getCourseSessionResourceLink()->setVisibility(ResourceLink::VISIBILITY_DELETED); + return $this->getVisibility() === ResourceLink::VISIBILITY_PUBLISHED; } /** diff --git a/src/CourseBundle/Repository/CDocumentRepository.php b/src/CourseBundle/Repository/CDocumentRepository.php index 3a220372af..9b06854b8b 100644 --- a/src/CourseBundle/Repository/CDocumentRepository.php +++ b/src/CourseBundle/Repository/CDocumentRepository.php @@ -4,7 +4,10 @@ namespace Chamilo\CourseBundle\Repository; use Chamilo\CoreBundle\Entity\Resource\ResourceLink; +use Chamilo\CoreBundle\Entity\Resource\ResourceNode; +use Chamilo\CoreBundle\Entity\Resource\ResourceRight; use Chamilo\CoreBundle\Repository\ResourceRepository; +use Chamilo\CoreBundle\Security\Authorization\Voter\ResourceNodeVoter; use Chamilo\CourseBundle\Entity\CDocument; use Doctrine\ORM\EntityManager; use Doctrine\ORM\EntityRepository; @@ -144,9 +147,20 @@ class CDocumentRepository extends ResourceRepository return $query->getSingleScalarResult(); } + /** + * @param int $courseId + * @param int $groupId + * @param int $sessionId + * + * @return mixed + * @throws \Doctrine\ORM\NonUniqueResultException + */ public function getTotalSpace($courseId, $groupId = null, $sessionId = null) { $repo = $this->repository; + $groupId = empty($groupId) ? null : $groupId; + $sessionId = empty($sessionId) ? null : $sessionId; + $qb = $repo->createQueryBuilder('d'); $query = $qb ->select('SUM(d.size)') @@ -166,4 +180,133 @@ class CDocumentRepository extends ResourceRepository return $query->getSingleScalarResult(); } + + /** + * Changes current document link visibility. + * + * @param CDocument $document + * @param int $visibility + * + * @return bool + */ + public function setVisibility($document, $visibility) + { + if (empty($document)) { + return false; + } + + $em = $this->entityManager; + $link = $document->getCourseSessionResourceLink(); + $link->setVisibility($visibility); + + if ($visibility === ResourceLink::VISIBILITY_DRAFT) { + $editorMask = ResourceNodeVoter::getEditorMask(); + $rights = []; + $resourceRight = new ResourceRight(); + $resourceRight + ->setMask($editorMask) + ->setRole(ResourceNodeVoter::ROLE_CURRENT_COURSE_TEACHER) + ->setResourceLink($link) + ; + $rights[] = $resourceRight; + + if (!empty($rights)) { + $link->setResourceRight($rights); + } + } else { + $link->setResourceRight([]); + } + $em->persist($link); + $em->flush(); + + return true; + } + + /** + * Change all links visibility to DELETED. + * + * @param CDocument $document + */ + public function softDelete($document) + { + $this->setLinkVisibility($document, ResourceLink::VISIBILITY_DELETED); + } + + /** + * @param int $userId + * + * @return array + */ + public function getAllDocumentsByAuthor($userId) + { + $repo = $this->repository; + + $qb = $repo->createQueryBuilder('d'); + $query = $qb + ->innerJoin('d.resourceNode', 'r') + ->innerJoin('r.resourceLinks', 'l') + ->where('l.user = :user') + ->andWhere('l.visibility <> :visibility') + ->setParameters([ + 'user' => $userId, + 'visibility' => ResourceLink::VISIBILITY_DELETED, + ]) + ->getQuery(); + + return $query->getResult(); + } + + /** + * @param CDocument $document + * @param int $visibility + * @param bool $recursive + */ + private function setLinkVisibility($document, $visibility, $recursive = true) + { + $resourceNode = $document->getResourceNode(); + $children = $resourceNode->getChildren(); + + if ($recursive) { + if (!empty($children)) { + /** @var ResourceNode $child */ + foreach ($children as $child) { + $criteria = ['resourceNode' => $child]; + $childDocument = $this->repository->findOneBy($criteria); + if ($childDocument) { + $this->setLinkVisibility($childDocument, $visibility); + } + } + } + } + + $links = $resourceNode->getResourceLinks(); + + if (!empty($links)) { + /** @var ResourceLink $link */ + foreach ($links as $link) { + $link->setVisibility($visibility); + + if ($visibility === ResourceLink::VISIBILITY_DRAFT) { + $editorMask = ResourceNodeVoter::getEditorMask(); + $rights = []; + $resourceRight = new ResourceRight(); + $resourceRight + ->setMask($editorMask) + ->setRole(ResourceNodeVoter::ROLE_CURRENT_COURSE_TEACHER) + ->setResourceLink($link) + ; + $rights[] = $resourceRight; + + if (!empty($rights)) { + $link->setResourceRight($rights); + } + } else { + $link->setResourceRight([]); + } + $this->entityManager->merge($link); + } + } + + $this->entityManager->flush(); + } }