Completed
Push — master ( 4874d1...5ea477 )
by Julito
10:24
created

CDocumentRepository::updateDocumentContent()   A

Complexity

Conditions 2
Paths 8

Size

Total Lines 14
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 10
c 1
b 0
f 0
nc 8
nop 2
dl 0
loc 14
rs 9.9332
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\Repository\ResourceRepository;
8
use Chamilo\CourseBundle\Entity\CDocument;
9
use Symfony\Component\Filesystem\Exception\FileNotFoundException;
10
11
/**
12
 * Class CDocumentRepository.
13
 */
14
final class CDocumentRepository extends ResourceRepository
15
{
16
    /**
17
     * @return string
18
     */
19
    public function getDocumentUrl(CDocument $document)
20
    {
21
        // There are no URL for folders.
22
        if ($document->getFiletype() === 'folder') {
23
            return '';
24
        }
25
        $file = $document->getResourceNode()->getResourceFile();
26
27
        if ($file === null) {
28
            return '';
29
        }
30
31
        $params = [
32
            'course' => $document->getCourse()->getCode(),
33
            'file' => ltrim($document->getPath(), '/'),
34
        ];
35
36
        return $this->getRouter()->generate(
37
            'resources_document_get_file',
38
            $params
39
        );
40
    }
41
42
43
44
    /**
45
     * @return CDocument|null
46
     */
47
    public function getParent(CDocument $document)
48
    {
49
        $resourceParent = $document->getResourceNode()->getParent();
50
51
        if ($resourceParent !== null) {
52
            $resourceParentId = $resourceParent->getId();
53
            $criteria = [
54
                'resourceNode' => $resourceParentId,
55
            ];
56
57
            return $this->findOneBy($criteria);
58
        }
59
60
        return null;
61
    }
62
63
    /**
64
     * @param int    $courseId
65
     * @param string $path
66
     *
67
     * @throws \Doctrine\ORM\NonUniqueResultException
68
     *
69
     * @return mixed
70
     */
71
    public function getFolderSize($courseId, $path)
72
    {
73
        $path = str_replace('_', '\_', $path);
74
        $addedSlash = $path === '/' ? '' : '/';
75
76
        $repo = $this->getRepository();
77
        $qb = $repo->createQueryBuilder('d');
78
        $query = $qb
79
            ->select('SUM(d.size)')
80
            ->innerJoin('d.resourceNode', 'r')
81
            ->innerJoin('r.resourceLinks', 'l')
82
            ->where('d.path LIKE :path')
83
            ->andWhere('d.path NOT LIKE :deleted')
84
            ->andWhere('d.path NOT LIKE :extra_path ')
85
            ->andWhere('l.visibility <> :visibility')
86
            ->andWhere('d.course = :course')
87
            ->setParameters([
88
                'path' => $path.$addedSlash.'%',
89
                'extra_path' => $path.$addedSlash.'%/%',
90
                'course' => $courseId,
91
                'deleted' => '%_DELETED_%',
92
                'visibility' => ResourceLink::VISIBILITY_DELETED,
93
            ])
94
            ->getQuery();
95
96
        return $query->getSingleScalarResult();
97
    }
98
99
    /**
100
     * @param int $courseId
101
     * @param int $groupId
102
     * @param int $sessionId
103
     *
104
     * @throws \Doctrine\ORM\NonUniqueResultException
105
     *
106
     * @return mixed
107
     */
108
    public function getTotalSpace($courseId, $groupId = null, $sessionId = null)
109
    {
110
        $repo = $this->getRepository();
111
        $groupId = empty($groupId) ? null : $groupId;
112
        $sessionId = empty($sessionId) ? null : $sessionId;
113
114
        $qb = $repo->createQueryBuilder('d');
115
        $query = $qb
116
            ->select('SUM(d.size)')
117
            ->innerJoin('d.resourceNode', 'r')
118
            ->innerJoin('r.resourceLinks', 'l')
119
            ->where('l.course = :course')
120
            ->andWhere('l.group = :group')
121
            ->andWhere('l.session = :session')
122
            ->andWhere('l.visibility <> :visibility')
123
            ->setParameters([
124
                'course' => $courseId,
125
                'group' => $groupId,
126
                'session' => $sessionId,
127
                'visibility' => ResourceLink::VISIBILITY_DELETED,
128
            ])
129
            ->getQuery();
130
131
        return $query->getSingleScalarResult();
132
    }
133
134
    /**
135
     * @param int $userId
136
     *
137
     * @return array
138
     */
139
    public function getAllDocumentsByAuthor($userId)
140
    {
141
        $repo = $this->repository;
142
143
        $qb = $repo->createQueryBuilder('d');
144
        $query = $qb
145
            ->innerJoin('d.resourceNode', 'r')
146
            ->innerJoin('r.resourceLinks', 'l')
147
            ->where('l.user = :user')
148
            ->andWhere('l.visibility <> :visibility')
149
            ->setParameters([
150
                'user' => $userId,
151
                'visibility' => ResourceLink::VISIBILITY_DELETED,
152
            ])
153
            ->getQuery();
154
155
        return $query->getResult();
156
    }
157
}
158