Completed
Push — master ( fc1588...7621d0 )
by Julito
16:55
created

CDocumentRepository::saveUpload()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

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