Completed
Push — master ( eb716b...911538 )
by Julito
15:40
created

CDocumentRepository::getParent()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 7
nc 2
nop 1
dl 0
loc 14
rs 10
c 0
b 0
f 0
1
<?php
2
/* For licensing terms, see /license.txt */
3
4
namespace Chamilo\CourseBundle\Repository;
5
6
use APY\DataGridBundle\Grid\Column\Column;
7
use APY\DataGridBundle\Grid\Grid;
8
use Chamilo\CoreBundle\Component\Utils\ResourceSettings;
9
use Chamilo\CoreBundle\Entity\Course;
10
use Chamilo\CoreBundle\Entity\Resource\ResourceLink;
11
use Chamilo\CoreBundle\Entity\Resource\ResourceNode;
12
use Chamilo\CoreBundle\Entity\Session;
13
use Chamilo\CoreBundle\Repository\ResourceRepository;
14
use Chamilo\CoreBundle\Repository\ResourceRepositoryInterface;
15
use Chamilo\CourseBundle\Entity\CDocument;
16
use Chamilo\CourseBundle\Entity\CGroupInfo;
17
use Chamilo\UserBundle\Entity\User;
18
use Symfony\Component\Form\FormInterface;
19
use Symfony\Component\HttpFoundation\File\UploadedFile;
20
21
/**
22
 * Class CDocumentRepository.
23
 */
24
final class CDocumentRepository extends ResourceRepository implements ResourceRepositoryInterface
25
{
26
    public function getResources(User $user, ResourceNode $parentNode, Course $course = null, Session $session = null, CGroupInfo $group = null)
27
    {
28
        return $this->getResourcesByCourse($course, $session, $group, $parentNode);
0 ignored issues
show
Bug introduced by
It seems like $course can also be of type null; however, parameter $course of Chamilo\CoreBundle\Repos...:getResourcesByCourse() does only seem to accept Chamilo\CoreBundle\Entity\Course, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

28
        return $this->getResourcesByCourse(/** @scrutinizer ignore-type */ $course, $session, $group, $parentNode);
Loading history...
29
    }
30
31
    public function getResourceSettings(): ResourceSettings
32
    {
33
        $settings = parent::getResourceSettings();
34
35
        $settings
36
            ->setAllowNodeCreation(true)
37
            ->setAllowResourceCreation(true)
38
            ->setAllowResourceUpload(true)
39
        ;
40
41
        return $settings;
42
    }
43
44
    public function saveUpload(UploadedFile $file)
45
    {
46
        $resource = new CDocument();
47
        $resource
48
            ->setFiletype('file')
49
            ->setSize($file->getSize())
50
            ->setTitle($file->getClientOriginalName())
51
        ;
52
53
        return $resource;
54
    }
55
56
    public function saveResource(FormInterface $form, $course, $session, $fileType)
57
    {
58
        $newResource = $form->getData();
59
        $newResource
60
            ->setCourse($course)
61
            ->setSession($session)
62
            ->setFiletype($fileType)
63
            //->setTitle($title) // already added in $form->getData()
64
            ->setReadonly(false)
65
        ;
66
67
        return $newResource;
68
    }
69
70
    /**
71
     * @return string
72
     */
73
    public function getDocumentUrl(CDocument $document)
74
    {
75
        // There are no URL for folders.
76
        if ($document->getFiletype() === 'folder') {
77
            return '';
78
        }
79
        $file = $document->getResourceNode()->getResourceFile();
80
81
        if ($file === null) {
82
            return '';
83
        }
84
85
        $params = [
86
            'course' => $document->getCourse()->getCode(),
87
            'file' => ltrim($document->getPath(), '/'),
88
            'tool' => 'document',
89
            'type' => 'files',
90
        ];
91
92
        return $this->getRouter()->generate(
93
            'chamilo_core_resource_view',
94
            $params
95
        );
96
    }
97
98
    /**
99
     * @return CDocument|null
100
     */
101
    public function getParent(CDocument $document)
102
    {
103
        $resourceParent = $document->getResourceNode()->getParent();
104
105
        if ($resourceParent !== null) {
106
            $resourceParentId = $resourceParent->getId();
107
            $criteria = [
108
                'resourceNode' => $resourceParentId,
109
            ];
110
111
            return $this->findOneBy($criteria);
112
        }
113
114
        return null;
115
    }
116
117
    /**
118
     * @param int    $courseId
119
     * @param string $path
120
     *
121
     * @throws \Doctrine\ORM\NonUniqueResultException
122
     *
123
     * @return mixed
124
     */
125
    public function getFolderSize($courseId, $path)
126
    {
127
        $path = str_replace('_', '\_', $path);
128
        $addedSlash = $path === '/' ? '' : '/';
129
130
        $repo = $this->getRepository();
131
        $qb = $repo->createQueryBuilder('d');
132
        $query = $qb
133
            ->select('SUM(d.size)')
134
            ->innerJoin('d.resourceNode', 'r')
135
            ->innerJoin('r.resourceLinks', 'l')
136
            ->where('d.path LIKE :path')
137
            ->andWhere('d.path NOT LIKE :deleted')
138
            ->andWhere('d.path NOT LIKE :extra_path ')
139
            ->andWhere('l.visibility <> :visibility')
140
            ->andWhere('d.course = :course')
141
            ->setParameters([
142
                'path' => $path.$addedSlash.'%',
143
                'extra_path' => $path.$addedSlash.'%/%',
144
                'course' => $courseId,
145
                'deleted' => '%_DELETED_%',
146
                'visibility' => ResourceLink::VISIBILITY_DELETED,
147
            ])
148
            ->getQuery();
149
150
        return $query->getSingleScalarResult();
151
    }
152
153
    /**
154
     * @param int $courseId
155
     * @param int $groupId
156
     * @param int $sessionId
157
     *
158
     * @throws \Doctrine\ORM\NonUniqueResultException
159
     *
160
     * @return mixed
161
     */
162
    public function getTotalSpace($courseId, $groupId = null, $sessionId = null)
163
    {
164
        $repo = $this->getRepository();
165
        $groupId = empty($groupId) ? null : $groupId;
166
        $sessionId = empty($sessionId) ? null : $sessionId;
167
168
        $qb = $repo->createQueryBuilder('d');
169
        $query = $qb
170
            ->select('SUM(d.size)')
171
            ->innerJoin('d.resourceNode', 'r')
172
            ->innerJoin('r.resourceLinks', 'l')
173
            ->where('l.course = :course')
174
            ->andWhere('l.group = :group')
175
            ->andWhere('l.session = :session')
176
            ->andWhere('l.visibility <> :visibility')
177
            ->setParameters([
178
                'course' => $courseId,
179
                'group' => $groupId,
180
                'session' => $sessionId,
181
                'visibility' => ResourceLink::VISIBILITY_DELETED,
182
            ])
183
            ->getQuery();
184
185
        return $query->getSingleScalarResult();
186
    }
187
188
    /**
189
     * @param int $userId
190
     *
191
     * @return array
192
     */
193
    public function getAllDocumentsByAuthor($userId)
194
    {
195
        $repo = $this->repository;
196
197
        $qb = $repo->createQueryBuilder('d');
198
        $query = $qb
199
            ->innerJoin('d.resourceNode', 'r')
200
            ->innerJoin('r.resourceLinks', 'l')
201
            ->where('l.user = :user')
202
            ->andWhere('l.visibility <> :visibility')
203
            ->setParameters([
204
                'user' => $userId,
205
                'visibility' => ResourceLink::VISIBILITY_DELETED,
206
            ])
207
            ->getQuery();
208
209
        return $query->getResult();
210
    }
211
212
    public function getTitleColumn(Grid $grid): Column
213
    {
214
        return $grid->getColumn('title');
215
    }
216
}
217