Completed
Push — master ( d28136...3a5b5f )
by Julito
16:58
created

CDocumentRepository   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 159
Duplicated Lines 0 %

Importance

Changes 8
Bugs 0 Features 1
Metric Value
eloc 79
dl 0
loc 159
rs 10
c 8
b 0
f 1
wmc 13

7 Methods

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