Completed
Push — master ( 5627cd...c7e907 )
by Julito
15:36
created

CDocumentRepository::setLinkVisibility()   B

Complexity

Conditions 9
Paths 6

Size

Total Lines 47
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 9
eloc 29
c 2
b 0
f 0
nop 3
dl 0
loc 47
rs 8.0555
nc 6
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->getRepository();
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->getRepository();
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->getEntityManager();
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
     * @param int $userId
197
     *
198
     * @return array
199
     */
200
    public function getAllDocumentsByAuthor($userId)
201
    {
202
        $repo = $this->repository;
203
204
        $qb = $repo->createQueryBuilder('d');
205
        $query = $qb
206
            ->innerJoin('d.resourceNode', 'r')
207
            ->innerJoin('r.resourceLinks', 'l')
208
            ->where('l.user = :user')
209
            ->andWhere('l.visibility <> :visibility')
210
            ->setParameters([
211
                'user' => $userId,
212
                'visibility' => ResourceLink::VISIBILITY_DELETED,
213
            ])
214
            ->getQuery();
215
216
        return $query->getResult();
217
    }
218
}
219