Completed
Push — master ( f67c92...339b0b )
by Julito
15:21
created

CDocumentRepository::saveResource()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

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