Passed
Push — master ( e2c783...35624b )
by Julito
09:25
created

BaseController::getCourse()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 6
nc 4
nop 0
dl 0
loc 12
rs 10
c 0
b 0
f 0
1
<?php
2
3
/* For licensing terms, see /license.txt */
4
5
namespace Chamilo\CoreBundle\Controller;
6
7
use Chamilo\CoreBundle\Block\BreadcrumbBlockService;
8
use Chamilo\CoreBundle\Entity\Course;
9
use Chamilo\CoreBundle\Entity\Session;
10
use Knp\Menu\FactoryInterface as MenuFactoryInterface;
11
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
12
use Symfony\Component\HttpFoundation\Request;
13
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
14
use Symfony\Contracts\Translation\TranslatorInterface;
15
16
/**
17
 * Each entity controller must extends this class.
18
 *
19
 * @abstract
20
 */
21
abstract class BaseController extends AbstractController
22
{
23
    protected $translator;
24
25
    public static function getSubscribedServices(): array
26
    {
27
        $services = parent::getSubscribedServices();
28
        $services['translator'] = TranslatorInterface::class;
29
        $services['breadcrumb'] = BreadcrumbBlockService::class;
30
31
        return $services;
32
    }
33
34
    public function getBreadCrumb(): BreadcrumbBlockService
35
    {
36
        return $this->container->get('breadcrumb');
37
    }
38
39
    /**
40
     * @param string $message
41
     *
42
     * @return NotFoundHttpException
43
     */
44
    public function abort($message = '')
45
    {
46
        return new NotFoundHttpException($message);
47
    }
48
49
    /**
50
     * Translator shortcut.
51
     *
52
     * @param string $variable
53
     *
54
     * @return string
55
     */
56
    public function trans($variable)
57
    {
58
        /** @var TranslatorInterface $translator */
59
        $translator = $this->container->get('translator');
60
61
        return $translator->trans($variable);
62
    }
63
64
    /**
65
     * @return MenuFactoryInterface
66
     */
67
    public function getMenuFactory()
68
    {
69
        return $this->container->get('knp_menu.factory');
70
    }
71
72
    /**
73
     * Gets the current Chamilo course based in the "_real_cid" session variable.
74
     *
75
     * @return Course
76
     */
77
    public function getCourse()
78
    {
79
        $request = $this->getRequest();
80
        if ($request) {
81
            $courseId = $request->getSession()->get('cid', 0);
82
        }
83
84
        if (empty($courseId)) {
85
            return null;
86
        }
87
88
        return $this->getDoctrine()->getManager()->find('ChamiloCoreBundle:Course', $courseId);
89
    }
90
91
    public function hasCourse()
92
    {
93
        $request = $this->getRequest();
94
        if ($request) {
95
            $courseId = $request->getSession()->get('cid', 0);
96
            if (!empty($courseId)) {
97
                return true;
98
            }
99
        }
100
101
        return false;
102
    }
103
104
    /**
105
     * Gets the current Chamilo session based in the "sid" $_SESSION variable.
106
     *
107
     * @return Session|null
108
     */
109
    public function getCourseSession()
110
    {
111
        $request = $this->getRequest();
112
113
        if ($request) {
114
            $sessionId = $request->getSession()->get('sid', 0);
115
        }
116
117
        if (empty($sessionId)) {
118
            return null;
119
        }
120
121
        return $this->getDoctrine()->getManager()->find('ChamiloCoreBundle:Session', $sessionId);
122
    }
123
124
    public function getGroup()
125
    {
126
        $request = $this->getRequest();
127
128
        if ($request) {
129
            $groupId = $request->getSession()->get('gid', 0);
130
        }
131
132
        if (empty($groupId)) {
133
            return null;
134
        }
135
136
        return $this->getDoctrine()->getManager()->find('ChamiloCourseBundle:CGroupInfo', $groupId);
137
    }
138
139
    public function getCourseUrlQuery(): string
140
    {
141
        $url = '';
142
        $course = $this->getCourse();
143
        if ($course) {
0 ignored issues
show
introduced by
$course is of type Chamilo\CoreBundle\Entity\Course, thus it always evaluated to true.
Loading history...
144
            $url = 'cid='.$course->getId();
145
        }
146
147
        $session = $this->getCourseSession();
148
        if ($session) {
149
            $url .= '&sid='.$session->getId();
150
        } else {
151
            $url .= '&sid=0';
152
        }
153
154
        $group = $this->getGroup();
155
        if ($group) {
156
            $url .= '&gid='.$group->getIid();
157
        } else {
158
            $url .= '&gid=0';
159
        }
160
161
        return $url;
162
    }
163
164
    public function getCourseUrlQueryToArray(): array
165
    {
166
        $url = [];
167
        $course = $this->getCourse();
168
        $url['cid'] = 0;
169
        if ($course) {
0 ignored issues
show
introduced by
$course is of type Chamilo\CoreBundle\Entity\Course, thus it always evaluated to true.
Loading history...
170
            $url['cid'] = $course->getId();
171
        }
172
        $session = $this->getCourseSession();
173
174
        $url['sid'] = 0;
175
        if ($session) {
176
            $url['sid'] = $session->getId();
177
        }
178
179
        return $url;
180
    }
181
182
    public function getResourceParams(Request $request): array
183
    {
184
        $tool = $request->get('tool');
185
        $type = $request->get('type');
186
        $id = (int) $request->get('id');
187
188
        $courseId = null;
189
        $sessionId = null;
190
191
        if ($this->hasCourse()) {
192
            $courseId = $this->getCourse()->getId();
193
            $session = $this->getCourseSession();
194
            $sessionId = $session ? $session->getId() : 0;
195
        }
196
197
        return [
198
            'id' => $id,
199
            'tool' => $tool,
200
            'type' => $type,
201
            'cid' => $courseId,
202
            'sid' => $sessionId,
203
        ];
204
    }
205
206
    /**
207
     * @return Request|null
208
     */
209
    public function getRequest()
210
    {
211
        return $this->container->get('request_stack')->getCurrentRequest();
212
    }
213
}
214