Completed
Push — master ( a3ace3...0ae69c )
by Julito
11:10
created

CDocumentRepository::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 2
dl 0
loc 5
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 Chamilo\CoreBundle\Entity\Resource\ResourceLink;
7
use Chamilo\CoreBundle\Entity\Resource\ResourceNode;
8
use Chamilo\CoreBundle\Entity\Resource\ResourceRight;
9
use Chamilo\CoreBundle\Repository\ResourceRepository;
10
use Chamilo\CoreBundle\Security\Authorization\Voter\ResourceNodeVoter;
11
use Chamilo\CourseBundle\Entity\CDocument;
12
use Gaufrette\Exception\FileNotFound;
13
14
/**
15
 * Class CDocumentRepository.
16
 */
17
final class CDocumentRepository extends ResourceRepository
18
{
19
    /**
20
     * @param int $id
21
     *
22
     * @return string
23
     */
24
    public function getDocumentContent($id): string
25
    {
26
        try {
27
            /** @var CDocument $document */
28
            $document = $this->find($id);
29
            $resourceNode = $document->getResourceNode();
30
            $resourceFile = $resourceNode->getResourceFile();
31
            $fileName = $resourceFile->getFile()->getPathname();
32
33
            return $this->fs->read($fileName);
34
        } catch (\Throwable $exception) {
35
            throw new FileNotFound($id);
36
        }
37
    }
38
39
    /**
40
     * @param CDocument $document
41
     * @param string    $content
42
     *
43
     * @return bool
44
     */
45
    public function updateDocumentContent(CDocument $document, $content)
46
    {
47
        try {
48
            $resourceNode = $document->getResourceNode();
49
            $resourceFile = $resourceNode->getResourceFile();
50
            $fileName = $resourceFile->getFile()->getPathname();
51
52
            $this->fs->update($fileName, $content);
53
            $size = $this->fs->getSize($fileName);
54
            $document->setSize($size);
55
            $this->entityManager->persist($document);
56
57
            return true;
58
        } catch (\Throwable $exception) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
59
        }
60
    }
61
62
    /**
63
     * @param CDocument $document
64
     *
65
     * @return CDocument|null
66
     */
67
    public function getParent(CDocument $document)
68
    {
69
        $resourceParent = $document->getResourceNode()->getParent();
70
71
        if ($resourceParent !== null) {
72
            $resourceParentId = $resourceParent->getId();
73
            $criteria = [
74
                'resourceNode' => $resourceParentId,
75
            ];
76
77
            return $this->findOneBy($criteria);
78
        }
79
80
        return null;
81
    }
82
83
    /**
84
     * @param int    $courseId
85
     * @param string $path
86
     *
87
     * @throws \Doctrine\ORM\NonUniqueResultException
88
     *
89
     * @return mixed
90
     */
91
    public function getFolderSize($courseId, $path)
92
    {
93
        $path = str_replace('_', '\_', $path);
94
        $addedSlash = $path === '/' ? '' : '/';
95
96
        $repo = $this->repository;
97
        $qb = $repo->createQueryBuilder('d');
98
        $query = $qb
99
            ->select('SUM(d.size)')
100
            ->innerJoin('d.resourceNode', 'r')
101
            ->innerJoin('r.resourceLinks', 'l')
102
            ->where('d.path LIKE :path')
103
            ->andWhere('d.path NOT LIKE :deleted')
104
            ->andWhere('d.path NOT LIKE :extra_path ')
105
            ->andWhere('l.visibility <> :visibility')
106
            ->andWhere('d.course = :course')
107
            ->setParameters([
108
                'path' => $path.$addedSlash.'%',
109
                'extra_path' => $path.$addedSlash.'%/%',
110
                'course' => $courseId,
111
                'deleted' => '%_DELETED_%',
112
                'visibility' => ResourceLink::VISIBILITY_DELETED,
113
            ])
114
            ->getQuery();
115
116
        return $query->getSingleScalarResult();
117
    }
118
119
    /**
120
     * @param int $courseId
121
     * @param int $groupId
122
     * @param int $sessionId
123
     *
124
     * @throws \Doctrine\ORM\NonUniqueResultException
125
     *
126
     * @return mixed
127
     */
128
    public function getTotalSpace($courseId, $groupId = null, $sessionId = null)
129
    {
130
        $repo = $this->repository;
131
        $groupId = empty($groupId) ? null : $groupId;
132
        $sessionId = empty($sessionId) ? null : $sessionId;
133
134
        $qb = $repo->createQueryBuilder('d');
135
        $query = $qb
136
            ->select('SUM(d.size)')
137
            ->innerJoin('d.resourceNode', 'r')
138
            ->innerJoin('r.resourceLinks', 'l')
139
            ->where('l.course = :course')
140
            ->andWhere('l.group = :group')
141
            ->andWhere('l.session = :session')
142
            ->andWhere('l.visibility <> :visibility')
143
            ->setParameters([
144
                'course' => $courseId,
145
                'group' => $groupId,
146
                'session' => $sessionId,
147
                'visibility' => ResourceLink::VISIBILITY_DELETED,
148
            ])
149
            ->getQuery();
150
151
        return $query->getSingleScalarResult();
152
    }
153
154
    /**
155
     * Changes current document link visibility.
156
     *
157
     * @param CDocument $document
158
     * @param int       $visibility
159
     *
160
     * @return bool
161
     */
162
    public function updateVisibility($document, $visibility): bool
163
    {
164
        if (empty($document)) {
165
            return false;
166
        }
167
168
        $em = $this->entityManager;
169
        $link = $document->getCourseSessionResourceLink();
170
        $link->setVisibility($visibility);
171
172
        if ($visibility === ResourceLink::VISIBILITY_DRAFT) {
173
            $editorMask = ResourceNodeVoter::getEditorMask();
174
            $rights = [];
175
            $resourceRight = new ResourceRight();
176
            $resourceRight
177
                ->setMask($editorMask)
178
                ->setRole(ResourceNodeVoter::ROLE_CURRENT_COURSE_TEACHER)
179
                ->setResourceLink($link)
180
            ;
181
            $rights[] = $resourceRight;
182
183
            if (!empty($rights)) {
184
                $link->setResourceRight($rights);
185
            }
186
        } else {
187
            $link->setResourceRight([]);
188
        }
189
        $em->persist($link);
190
        $em->flush();
191
192
        return true;
193
    }
194
195
    /**
196
     * Change all links visibility to DELETED.
197
     *
198
     * @param CDocument $document
199
     */
200
    public function softDelete($document)
201
    {
202
        $this->setLinkVisibility($document, ResourceLink::VISIBILITY_DELETED);
203
    }
204
205
    /**
206
     * @param int $userId
207
     *
208
     * @return array
209
     */
210
    public function getAllDocumentsByAuthor($userId)
211
    {
212
        $repo = $this->repository;
213
214
        $qb = $repo->createQueryBuilder('d');
215
        $query = $qb
216
            ->innerJoin('d.resourceNode', 'r')
217
            ->innerJoin('r.resourceLinks', 'l')
218
            ->where('l.user = :user')
219
            ->andWhere('l.visibility <> :visibility')
220
            ->setParameters([
221
                'user' => $userId,
222
                'visibility' => ResourceLink::VISIBILITY_DELETED,
223
            ])
224
            ->getQuery();
225
226
        return $query->getResult();
227
    }
228
229
    /**
230
     * @param CDocument $document
231
     * @param int       $visibility
232
     * @param bool      $recursive
233
     */
234
    private function setLinkVisibility($document, $visibility, $recursive = true)
235
    {
236
        $resourceNode = $document->getResourceNode();
237
        $children = $resourceNode->getChildren();
238
239
        if ($recursive) {
240
            if (!empty($children)) {
241
                /** @var ResourceNode $child */
242
                foreach ($children as $child) {
243
                    $criteria = ['resourceNode' => $child];
244
                    $childDocument = $this->repository->findOneBy($criteria);
245
                    if ($childDocument) {
246
                        $this->setLinkVisibility($childDocument, $visibility);
247
                    }
248
                }
249
            }
250
        }
251
252
        $links = $resourceNode->getResourceLinks();
253
254
        if (!empty($links)) {
255
            /** @var ResourceLink $link */
256
            foreach ($links as $link) {
257
                $link->setVisibility($visibility);
258
259
                if ($visibility === ResourceLink::VISIBILITY_DRAFT) {
260
                    $editorMask = ResourceNodeVoter::getEditorMask();
261
                    $rights = [];
262
                    $resourceRight = new ResourceRight();
263
                    $resourceRight
264
                        ->setMask($editorMask)
265
                        ->setRole(ResourceNodeVoter::ROLE_CURRENT_COURSE_TEACHER)
266
                        ->setResourceLink($link)
267
                    ;
268
                    $rights[] = $resourceRight;
269
270
                    if (!empty($rights)) {
271
                        $link->setResourceRight($rights);
272
                    }
273
                } else {
274
                    $link->setResourceRight([]);
275
                }
276
                $this->entityManager->merge($link);
277
            }
278
        }
279
280
        $this->entityManager->flush();
281
    }
282
}
283