Completed
Push — master ( 2d5548...d5efc3 )
by Julito
10:44
created

BaseController::getCourseUrlQuery()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 16
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 10
nc 4
nop 0
dl 0
loc 16
rs 9.9332
c 0
b 0
f 0
1
<?php
2
/* For licensing terms, see /license.txt */
3
4
namespace Chamilo\CoreBundle\Controller;
5
6
use Chamilo\CoreBundle\Entity\Course;
7
use Chamilo\CoreBundle\Entity\Session;
8
use Knp\Menu\FactoryInterface as MenuFactoryInterface;
9
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
10
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
11
12
/**
13
 * Each entity controller must extends this class.
14
 *
15
 * @abstract
16
 */
17
abstract class BaseController extends AbstractController
18
{
19
    /**
20
     * @return NotFoundHttpException
21
     */
22
    public function abort()
23
    {
24
        return new NotFoundHttpException();
25
    }
26
27
    /**
28
     * Translator shortcut.
29
     *
30
     * @param string $variable
31
     *
32
     * @return string
33
     */
34
    public function trans($variable)
35
    {
36
        return $this->container->get('translator')->trans($variable);
37
    }
38
39
    /**
40
     * @return MenuFactoryInterface
41
     */
42
    public function getMenuFactory()
43
    {
44
        return $this->container->get('knp_menu.factory');
45
    }
46
47
    /**
48
     * Gets the current Chamilo course based in the "_real_cid" session variable.
49
     *
50
     * @return Course
51
     */
52
    public function getCourse()
53
    {
54
        $request = $this->getRequest();
55
        if ($request) {
56
            $courseId = $request->getSession()->get('_real_cid', 0);
57
        }
58
59
        if (empty($courseId)) {
60
            return null;
61
        }
62
63
        return $this->getDoctrine()->getManager()->find('ChamiloCoreBundle:Course', $courseId);
64
    }
65
66
    /**
67
     * Gets the current Chamilo session based in the "id_session" $_SESSION variable.
68
     *
69
     * @return Session|null
70
     */
71
    public function getCourseSession()
72
    {
73
        $request = $this->getRequest();
74
75
        if ($request) {
76
            $sessionId = $request->getSession()->get('id_session', 0);
77
        }
78
79
        if (empty($sessionId)) {
80
            return null;
81
        }
82
83
        return $this->getDoctrine()->getManager()->find('ChamiloCoreBundle:Session', $sessionId);
84
    }
85
86
    /**
87
     * @return string
88
     */
89
    public function getCourseUrlQuery(): string
90
    {
91
        $url = '';
92
        $course = $this->getCourse();
93
        if ($course) {
0 ignored issues
show
introduced by
$course is of type Chamilo\CoreBundle\Entity\Course, thus it always evaluated to true.
Loading history...
94
            $url = 'cidReq='.$course->getCode();
95
        }
96
        $session = $this->getCourseSession();
97
98
        if ($session) {
99
            $url .= '&id_session='.$session->getId();
100
        } else {
101
            $url .= '&id_session=0';
102
        }
103
104
        return $url;
105
    }
106
107
    /**
108
     * @return \Symfony\Component\HttpFoundation\Request|null
109
     */
110
    public function getRequest()
111
    {
112
        $request = $this->container->get('request_stack')->getCurrentRequest();
113
114
        return $request;
115
    }
116
}
117