Completed
Push — master ( af42cb...3888f0 )
by Julito
13:17
created

SequenceRepository::getRequirements()   B

Complexity

Conditions 6
Paths 6

Size

Total Lines 47
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 27
nc 6
nop 2
dl 0
loc 47
rs 8.8657
c 0
b 0
f 0
1
<?php
2
/* For licensing terms, see /license.txt */
3
4
namespace Chamilo\CoreBundle\Repository;
5
6
use Chamilo\CoreBundle\Entity\SequenceResource;
7
use Doctrine\ORM\EntityRepository;
8
use Fhaculty\Graph\Set\Vertices;
9
use Fhaculty\Graph\Vertex;
10
11
/**
12
 * Class SequenceRepository
13
 * The functions inside this class should return an instance of QueryBuilder.
14
 *
15
 * @package Chamilo\CoreBundle\Repository
16
 */
17
class SequenceRepository extends EntityRepository
18
{
19
    /**
20
     * Find the SequenceResource based in the resourceId and type.
21
     *
22
     * @param int $resourceId
23
     * @param int $type
24
     *
25
     * @return SequenceResource
26
     */
27
    public function findRequirementForResource($resourceId, $type)
28
    {
29
        return $this->findOneBy(['resourceId' => $resourceId, 'type' => $type]);
30
    }
31
32
    /**
33
     * @todo implement for all types only work for sessions
34
     *
35
     * @param int $resourceId
36
     * @param int $type
37
     *
38
     * @return array
39
     */
40
    public function getRequirementAndDependencies($resourceId, $type)
41
    {
42
        $sequence = $this->findRequirementForResource($resourceId, $type);
43
        $result = ['requirements' => '', 'dependencies' => []];
44
        if ($sequence && $sequence->hasGraph()) {
45
            $graph = $sequence->getSequence()->getUnSerializeGraph();
46
            $vertex = $graph->getVertex($resourceId);
47
            $from = $vertex->getVerticesEdgeFrom();
48
49
            foreach ($from as $subVertex) {
50
                $vertexId = $subVertex->getId();
51
                $sessionInfo = api_get_session_info($vertexId);
52
                $sessionInfo['admin_link'] = '<a href="'.\SessionManager::getAdminPath($vertexId).'">'.$sessionInfo['name'].'</a>';
0 ignored issues
show
Bug introduced by
Are you sure SessionManager::getAdminPath($vertexId) of type false|string can be used in concatenation? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

52
                $sessionInfo['admin_link'] = '<a href="'./** @scrutinizer ignore-type */ \SessionManager::getAdminPath($vertexId).'">'.$sessionInfo['name'].'</a>';
Loading history...
53
                $result['requirements'][] = $sessionInfo;
54
            }
55
56
            $to = $vertex->getVerticesEdgeTo();
57
            foreach ($to as $subVertex) {
58
                $vertexId = $subVertex->getId();
59
                $sessionInfo = api_get_session_info($vertexId);
60
                $sessionInfo['admin_link'] = '<a href="'.\SessionManager::getAdminPath($vertexId).'">'.$sessionInfo['name'].'</a>';
61
                $result['dependencies'][] = $sessionInfo;
62
            }
63
        }
64
65
        return $result;
66
    }
67
68
    /**
69
     * Deletes a node and check in all the dependencies if the node exists in
70
     * order to deleted.
71
     *
72
     * @param int $resourceId
73
     * @param int $type
74
     */
75
    public function deleteResource($resourceId, $type)
76
    {
77
        $sequence = $this->findRequirementForResource($resourceId, $type);
78
        if ($sequence && $sequence->hasGraph()) {
79
            $em = $this->getEntityManager();
80
            $graph = $sequence->getSequence()->getUnSerializeGraph();
81
            $mainVertex = $graph->getVertex($resourceId);
82
            $vertices = $graph->getVertices();
83
84
            /** @var Vertex $vertex */
85
            foreach ($vertices as $vertex) {
86
                $subResourceId = $vertex->getId();
87
                $subSequence = $this->findRequirementForResource($subResourceId, $type);
88
                if ($sequence && $subSequence->hasGraph()) {
89
                    $graph = $subSequence->getSequence()->getUnSerializeGraph();
90
                    $subMainVertex = $graph->getVertex($resourceId);
91
                    $subMainVertex->destroy();
92
                    $subSequence->getSequence()->setGraphAndSerialize($graph);
93
                    $em->persist($subSequence);
94
                }
95
            }
96
97
            $mainVertex->destroy();
98
            $em->remove($sequence);
99
            $em->flush();
100
        }
101
    }
102
103
    /**
104
     * Get the requirements for a resource only.
105
     *
106
     * @param int $resourceId The resource ID
107
     * @param int $type       The type of sequence resource
108
     *
109
     * @return array
110
     */
111
    public function getRequirements($resourceId, $type)
112
    {
113
        $sequencesResource = $this->findBy([
114
            'resourceId' => $resourceId,
115
            'type' => $type,
116
        ]);
117
118
        $result = [];
119
        /** @var SequenceResource $sequenceResource */
120
        foreach ($sequencesResource as $sequenceResource) {
121
            if (!$sequenceResource->hasGraph()) {
122
                continue;
123
            }
124
125
            $sequence = $sequenceResource->getSequence();
126
            $graph = $sequence->getUnSerializeGraph();
127
            $vertex = $graph->getVertex($resourceId);
128
            $from = $vertex->getVerticesEdgeFrom();
129
130
            $sequenceInfo = [
131
                'name' => $sequence->getName(),
132
                'requirements' => [],
133
            ];
134
135
            foreach ($from as $subVertex) {
136
                $vertexId = $subVertex->getId();
137
138
                switch ($type) {
139
                    case SequenceResource::SESSION_TYPE:
140
                        $session = $this->getEntityManager()->getReference(
141
                            'ChamiloCoreBundle:Session',
142
                            $vertexId
143
                        );
144
145
                        if (empty($session)) {
146
                            break;
147
                        }
148
149
                        $sequenceInfo['requirements'][$vertexId] = $session;
150
                        break;
151
                }
152
            }
153
154
            $result[$sequence->getId()] = $sequenceInfo;
155
        }
156
157
        return $result;
158
    }
159
160
    /**
161
     * Get the requirements and dependencies within a sequence for a resource.
162
     *
163
     * @param int $resourceId The resource ID
164
     * @param int $type       The type of sequence resource
165
     *
166
     * @return array
167
     */
168
    public function getRequirementsAndDependenciesWithinSequences($resourceId, $type)
169
    {
170
        $sequencesResource = $this->findBy([
171
            'resourceId' => $resourceId,
172
            'type' => $type,
173
        ]);
174
175
        $result = [];
176
177
        /** @var SequenceResource $sequenceResource */
178
        foreach ($sequencesResource as $sequenceResource) {
179
            if (!$sequenceResource->hasGraph()) {
180
                continue;
181
            }
182
183
            $sequence = $sequenceResource->getSequence();
184
            $graph = $sequence->getUnSerializeGraph();
185
            $vertex = $graph->getVertex($resourceId);
186
            $from = $vertex->getVerticesEdgeFrom();
187
            $to = $vertex->getVerticesEdgeTo();
188
189
            $requirements = [];
190
            $dependencies = [];
191
192
            switch ($type) {
193
                case SequenceResource::SESSION_TYPE:
194
                    $requirements = $this->findSessionFromVerticesEdges($from);
195
                    $dependencies = $this->findSessionFromVerticesEdges($to);
196
                    break;
197
            }
198
199
            $result[$sequence->getId()] = [
200
                'name' => $sequence->getName(),
201
                'requirements' => $requirements,
202
                'dependencies' => $dependencies,
203
            ];
204
        }
205
206
        return [
207
            'sequences' => $result,
208
        ];
209
    }
210
211
    /**
212
     * Get sessions from vertices.
213
     *
214
     * @param Vertices $verticesEdges The vertices
215
     *
216
     * @return array
217
     */
218
    protected function findSessionFromVerticesEdges(Vertices $verticesEdges)
219
    {
220
        $sessionVertices = [];
221
        foreach ($verticesEdges as $supVertex) {
222
            $vertexId = $supVertex->getId();
223
            $session = $this->getEntityManager()->getReference(
224
                'ChamiloCoreBundle:Session',
225
                $vertexId
226
            );
227
228
            if (empty($session)) {
229
                continue;
230
            }
231
232
            $sessionVertices[$vertexId] = $session;
233
        }
234
235
        return $sessionVertices;
236
    }
237
}
238