|
5 | 5 | use Chamilo\CoreBundle\Entity\Course; |
6 | 6 | use Chamilo\CoreBundle\Entity\ResourceLink; |
7 | 7 | use Chamilo\CoreBundle\Entity\ResourceNode; |
| 8 | +use Chamilo\CoreBundle\Entity\Session; |
8 | 9 | use Chamilo\CoreBundle\Enums\ObjectIcon; |
9 | 10 | use Chamilo\CoreBundle\Framework\Container; |
10 | 11 | use Chamilo\CourseBundle\Entity\CDocument; |
| 12 | +use Chamilo\CourseBundle\Entity\CGroup; |
11 | 13 | use Chamilo\CourseBundle\Repository\CDocumentRepository; |
12 | 14 | use Doctrine\ORM\EntityManagerInterface; |
13 | 15 |
|
@@ -541,172 +543,51 @@ public static function get_all_document_folders( |
541 | 543 | return []; |
542 | 544 | } |
543 | 545 |
|
544 | | - $TABLE_DOCUMENT = Database::get_course_table(TABLE_DOCUMENT); |
545 | | - $groupIid = (int) $groupIid; |
546 | | - $courseId = $courseInfo['real_id']; |
547 | 546 | $sessionId = api_get_session_id(); |
548 | 547 |
|
549 | | - $folders = []; |
550 | | - $students = CourseManager::get_user_list_from_course_code( |
551 | | - $courseInfo['code'], |
552 | | - api_get_session_id() |
553 | | - ); |
554 | | - |
555 | | - $conditionList = []; |
556 | | - if (!empty($students)) { |
557 | | - foreach ($students as $studentId => $studentInfo) { |
558 | | - $conditionList[] = '/shared_folder/sf_user_'.$studentInfo['user_id']; |
559 | | - } |
| 548 | + /** @var Course|null $course */ |
| 549 | + $course = api_get_course_entity(); |
| 550 | + if (!$course instanceof Course) { |
| 551 | + return []; |
560 | 552 | } |
561 | 553 |
|
562 | | - $groupCondition = " l.group_id = $groupIid"; |
563 | | - if (empty($groupIid)) { |
564 | | - $groupCondition = ' (l.group_id = 0 OR l.group_id IS NULL)'; |
| 554 | + /** @var Session|null $session */ |
| 555 | + $session = null; |
| 556 | + if (!empty($sessionId)) { |
| 557 | + $session = Container::$container |
| 558 | + ->get('doctrine') |
| 559 | + ->getRepository(Session::class) |
| 560 | + ->find($sessionId); |
565 | 561 | } |
566 | 562 |
|
567 | | - $show_users_condition = ''; |
568 | | - if ($can_see_invisible) { |
569 | | - $sessionId = $sessionId ?: api_get_session_id(); |
570 | | - $condition_session = " AND (l.session_id = '$sessionId' OR (l.session_id = '0' OR l.session_id IS NULL) )"; |
571 | | - $condition_session .= self::getSessionFolderFilters($path, $sessionId); |
572 | | - |
573 | | - $sql = "SELECT DISTINCT docs.iid, n.path |
574 | | - FROM resource_node AS n |
575 | | - INNER JOIN $TABLE_DOCUMENT AS docs |
576 | | - ON (docs.resource_node_id = n.id) |
577 | | - INNER JOIN resource_link l |
578 | | - ON (l.resource_node_id = n.id) |
579 | | - WHERE |
580 | | - l.c_id = $courseId AND |
581 | | - docs.filetype = 'folder' AND |
582 | | - $groupCondition AND |
583 | | - n.path NOT LIKE '%shared_folder%' AND |
584 | | - l.deleted_at IS NULL |
585 | | - $condition_session "; |
586 | | - |
587 | | - if (0 != $groupIid) { |
588 | | - $sql .= " AND n.path NOT LIKE '%shared_folder%' "; |
589 | | - } else { |
590 | | - $sql .= $show_users_condition; |
591 | | - } |
592 | | - |
593 | | - $result = Database::query($sql); |
594 | | - if ($result && 0 != Database::num_rows($result)) { |
595 | | - while ($row = Database::fetch_assoc($result)) { |
596 | | - if (self::is_folder_to_avoid($row['path'])) { |
597 | | - continue; |
598 | | - } |
599 | | - |
600 | | - if (false !== strpos($row['path'], '/shared_folder/')) { |
601 | | - if (!in_array($row['path'], $conditionList)) { |
602 | | - continue; |
603 | | - } |
604 | | - } |
605 | | - |
606 | | - $folders[$row['iid']] = $row['path']; |
607 | | - } |
608 | | - |
609 | | - if (!empty($folders)) { |
610 | | - natsort($folders); |
611 | | - } |
612 | | - |
613 | | - return $folders; |
614 | | - } |
615 | | - |
616 | | - return false; |
617 | | - } else { |
618 | | - // No invisible folders |
619 | | - // Condition for the session |
620 | | - $condition_session = api_get_session_condition( |
621 | | - $sessionId, |
622 | | - true, |
623 | | - false, |
624 | | - 'docs.session_id' |
625 | | - ); |
626 | | - |
627 | | - $visibilityCondition = 'l.visibility = 1'; |
628 | | - $fileType = "docs.filetype = 'folder' AND"; |
629 | | - if ($getInvisibleList) { |
630 | | - $visibilityCondition = 'l.visibility = 0'; |
631 | | - $fileType = ''; |
632 | | - } |
633 | | - |
634 | | - //get visible folders |
635 | | - $sql = "SELECT DISTINCT docs.id |
636 | | - FROM resource_node AS n |
637 | | - INNER JOIN $TABLE_DOCUMENT AS docs |
638 | | - ON (docs.resource_node_id = n.id) |
639 | | - INNER JOIN resource_link l |
640 | | - ON (l.resource_node_id = n.id) |
641 | | - WHERE |
642 | | - $fileType |
643 | | - $groupCondition AND |
644 | | - $visibilityCondition |
645 | | - $show_users_condition |
646 | | - $condition_session AND |
647 | | - l.c_id = $courseId "; |
648 | | - $result = Database::query($sql); |
649 | | - $visibleFolders = []; |
650 | | - while ($row = Database::fetch_assoc($result)) { |
651 | | - $visibleFolders[$row['id']] = $row['path']; |
652 | | - } |
653 | | - |
654 | | - if ($getInvisibleList) { |
655 | | - return $visibleFolders; |
656 | | - } |
657 | | - |
658 | | - // get invisible folders |
659 | | - $sql = "SELECT DISTINCT docs.iid, n.path |
660 | | - FROM resource_node AS n |
661 | | - INNER JOIN $TABLE_DOCUMENT AS docs |
662 | | - ON (docs.resource_node_id = n.id) |
663 | | - INNER JOIN resource_link l |
664 | | - ON (l.resource_node_id = n.id) |
665 | | - WHERE |
666 | | - docs.filetype = 'folder' AND |
667 | | - $groupCondition AND |
668 | | - l.visibility IN ('".ResourceLink::VISIBILITY_PENDING."') |
669 | | - $condition_session AND |
670 | | - l.c_id = $courseId "; |
671 | | - $result = Database::query($sql); |
672 | | - $invisibleFolders = []; |
673 | | - while ($row = Database::fetch_assoc($result)) { |
674 | | - //get visible folders in the invisible ones -> they are invisible too |
675 | | - $sql = "SELECT DISTINCT docs.iid, n.path |
676 | | - FROM resource_node AS n |
677 | | - INNER JOIN $TABLE_DOCUMENT AS docs |
678 | | - ON (docs.resource_node_id = n.id) |
679 | | - INNER JOIN resource_link l |
680 | | - ON (l.resource_node_id = n.id) |
681 | | - WHERE |
682 | | - docs.filetype = 'folder' AND |
683 | | - $groupCondition AND |
684 | | - l.deleted_at IS NULL |
685 | | - $condition_session AND |
686 | | - l.c_id = $courseId "; |
687 | | - $folder_in_invisible_result = Database::query($sql); |
688 | | - while ($folders_in_invisible_folder = Database::fetch_assoc($folder_in_invisible_result)) { |
689 | | - $invisibleFolders[$folders_in_invisible_folder['id']] = $folders_in_invisible_folder['path']; |
690 | | - } |
691 | | - } |
692 | | - |
693 | | - // If both results are arrays -> //calculate the difference between the 2 arrays -> only visible folders are left :) |
694 | | - if (is_array($visibleFolders) && is_array($invisibleFolders)) { |
695 | | - $folders = array_diff($visibleFolders, $invisibleFolders); |
696 | | - natsort($folders); |
697 | | - |
698 | | - return $folders; |
699 | | - } |
| 563 | + /** @var CGroup|null $group */ |
| 564 | + $group = null; |
| 565 | + if (!empty($groupIid)) { |
| 566 | + $group = Container::$container |
| 567 | + ->get('doctrine') |
| 568 | + ->getRepository(CGroup::class) |
| 569 | + ->find($groupIid); |
| 570 | + } |
700 | 571 |
|
701 | | - if (is_array($visibleFolders)) { |
702 | | - natsort($visibleFolders); |
| 572 | + /** @var CDocumentRepository $docRepo */ |
| 573 | + $docRepo = Container::$container->get('doctrine') |
| 574 | + ->getRepository(CDocument::class); |
703 | 575 |
|
704 | | - return $visibleFolders; |
705 | | - } |
| 576 | + $folders = $docRepo->getAllFoldersForContext( |
| 577 | + $course, |
| 578 | + $session, |
| 579 | + $group, |
| 580 | + (bool) $can_see_invisible, |
| 581 | + (bool) $getInvisibleList |
| 582 | + ); |
706 | 583 |
|
707 | | - // no visible folders found |
| 584 | + if (empty($folders)) { |
| 585 | + // Keep backward compatibility: some legacy callers expect "false" |
| 586 | + // when there are no visible folders. |
708 | 587 | return false; |
709 | 588 | } |
| 589 | + |
| 590 | + return $folders; |
710 | 591 | } |
711 | 592 |
|
712 | 593 | /** |
|
0 commit comments