Completed
Push — master ( 0a6a4f...5b557d )
by Julito
12:14
created

CDocumentRepository::updateVisibility()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 31
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 21
c 0
b 0
f 0
nop 2
dl 0
loc 31
rs 9.584
nc 4
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 Symfony\Component\Filesystem\Exception\FileNotFoundException;
12
13
/**
14
 * Class CDocumentRepository.
15
 */
16
final class CDocumentRepository extends ResourceRepository
17
{
18
    /**
19
     * @return string
20
     */
21
    public function getDocumentUrl(CDocument $document)
22
    {
23
        // There are no URL for folders.
24
        if ($document->getFiletype() === 'folder') {
25
            return '';
26
        }
27
        $file = $document->getResourceNode()->getResourceFile();
28
29
        if ($file === null) {
30
            return '';
31
        }
32
33
        $params = [
34
            'course' => $document->getCourse()->getCode(),
35
            'file' => ltrim($document->getPath(), '/'),
36
        ];
37
38
        return $this->getRouter()->generate(
39
            'resources_document_get_file',
40
            $params
41
        );
42
    }
43
44
    /**
45
     * @param int $id
46
     */
47
    public function getDocumentContent($id): string
48
    {
49
        try {
50
            /** @var CDocument $document */
51
            $document = $this->find($id);
52
            $resourceNode = $document->getResourceNode();
53
            $resourceFile = $resourceNode->getResourceFile();
54
            $fileName = $resourceFile->getFile()->getPathname();
55
56
            return $this->fs->read($fileName);
57
        } catch (\Throwable $exception) {
58
            throw new FileNotFoundException($id);
59
        }
60
    }
61
62
    /**
63
     * @param string $content
64
     *
65
     * @return bool
66
     */
67
    public function updateDocumentContent(CDocument $document, $content)
68
    {
69
        try {
70
            $resourceNode = $document->getResourceNode();
71
            $resourceFile = $resourceNode->getResourceFile();
72
            $fileName = $resourceFile->getFile()->getPathname();
73
74
            $this->fs->update($fileName, $content);
75
            $size = $this->fs->getSize($fileName);
76
            $document->setSize($size);
77
            $this->entityManager->persist($document);
78
79
            return true;
80
        } catch (\Throwable $exception) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
81
        }
82
    }
83
84
    /**
85
     * @return CDocument|null
86
     */
87
    public function getParent(CDocument $document)
88
    {
89
        $resourceParent = $document->getResourceNode()->getParent();
90
91
        if ($resourceParent !== null) {
92
            $resourceParentId = $resourceParent->getId();
93
            $criteria = [
94
                'resourceNode' => $resourceParentId,
95
            ];
96
97
            return $this->findOneBy($criteria);
98
        }
99
100
        return null;
101
    }
102
103
    /**
104
     * @param int    $courseId
105
     * @param string $path
106
     *
107
     * @throws \Doctrine\ORM\NonUniqueResultException
108
     *
109
     * @return mixed
110
     */
111
    public function getFolderSize($courseId, $path)
112
    {
113
        $path = str_replace('_', '\_', $path);
114
        $addedSlash = $path === '/' ? '' : '/';
115
116
        $repo = $this->getRepository();
117
        $qb = $repo->createQueryBuilder('d');
118
        $query = $qb
119
            ->select('SUM(d.size)')
120
            ->innerJoin('d.resourceNode', 'r')
121
            ->innerJoin('r.resourceLinks', 'l')
122
            ->where('d.path LIKE :path')
123
            ->andWhere('d.path NOT LIKE :deleted')
124
            ->andWhere('d.path NOT LIKE :extra_path ')
125
            ->andWhere('l.visibility <> :visibility')
126
            ->andWhere('d.course = :course')
127
            ->setParameters([
128
                'path' => $path.$addedSlash.'%',
129
                'extra_path' => $path.$addedSlash.'%/%',
130
                'course' => $courseId,
131
                'deleted' => '%_DELETED_%',
132
                'visibility' => ResourceLink::VISIBILITY_DELETED,
133
            ])
134
            ->getQuery();
135
136
        return $query->getSingleScalarResult();
137
    }
138
139
    /**
140
     * @param int $courseId
141
     * @param int $groupId
142
     * @param int $sessionId
143
     *
144
     * @throws \Doctrine\ORM\NonUniqueResultException
145
     *
146
     * @return mixed
147
     */
148
    public function getTotalSpace($courseId, $groupId = null, $sessionId = null)
149
    {
150
        $repo = $this->getRepository();
151
        $groupId = empty($groupId) ? null : $groupId;
152
        $sessionId = empty($sessionId) ? null : $sessionId;
153
154
        $qb = $repo->createQueryBuilder('d');
155
        $query = $qb
156
            ->select('SUM(d.size)')
157
            ->innerJoin('d.resourceNode', 'r')
158
            ->innerJoin('r.resourceLinks', 'l')
159
            ->where('l.course = :course')
160
            ->andWhere('l.group = :group')
161
            ->andWhere('l.session = :session')
162
            ->andWhere('l.visibility <> :visibility')
163
            ->setParameters([
164
                'course' => $courseId,
165
                'group' => $groupId,
166
                'session' => $sessionId,
167
                'visibility' => ResourceLink::VISIBILITY_DELETED,
168
            ])
169
            ->getQuery();
170
171
        return $query->getSingleScalarResult();
172
    }
173
174
    /**
175
     * @param int $userId
176
     *
177
     * @return array
178
     */
179
    public function getAllDocumentsByAuthor($userId)
180
    {
181
        $repo = $this->repository;
182
183
        $qb = $repo->createQueryBuilder('d');
184
        $query = $qb
185
            ->innerJoin('d.resourceNode', 'r')
186
            ->innerJoin('r.resourceLinks', 'l')
187
            ->where('l.user = :user')
188
            ->andWhere('l.visibility <> :visibility')
189
            ->setParameters([
190
                'user' => $userId,
191
                'visibility' => ResourceLink::VISIBILITY_DELETED,
192
            ])
193
            ->getQuery();
194
195
        return $query->getResult();
196
    }
197
}
198