Passed
Push — master ( aacfc9...8578c1 )
by Julito
09:03
created

CDocumentRepository::getAllDocumentsByAuthor()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 17
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

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