Passed
Push — master ( 8276d9...eeb046 )
by Julito
11:16
created

CourseControllerTrait::getCourse()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
3
/* For licensing terms, see /license.txt */
4
5
namespace Chamilo\CoreBundle\Traits;
6
7
use Chamilo\CoreBundle\Entity\Course;
8
use Chamilo\CoreBundle\Entity\Session;
9
10
/**
11
 * Trait CourseControllerTrait.
12
 * Implements the functions defined by the CourseControllerInterface.
13
 */
14
trait CourseControllerTrait
15
{
16
    protected $course;
17
    protected $session;
18
19
    /**
20
     * Gets the current Chamilo course based in the "_real_cid" session variable.
21
     *
22
     * @return Course
23
     */
24
    /*public function getCourse()
25
    {
26
        $request = $this->getRequest();
27
        if ($request) {
28
            $courseId = $request->getSession()->get('cid', 0);
29
        }
30
31
        if (empty($courseId)) {
32
            return null;
33
        }
34
35
        return $this->getDoctrine()->getManager()->find('ChamiloCoreBundle:Course', $courseId);
36
    }
37
    */
38
39
    /*public function hasCourse()
40
    {
41
        $request = $this->getRequest();
42
        if ($request) {
43
            $courseId = $request->getSession()->get('cid', 0);
44
            if (!empty($courseId)) {
45
                return true;
46
            }
47
        }
48
49
        return false;
50
    }*/
51
52
    /**
53
     * Gets the current Chamilo session based in the "sid" $_SESSION variable.
54
     *
55
     * @return Session|null
56
     */
57
    public function getCourseSession()
58
    {
59
        $request = $this->getRequest();
0 ignored issues
show
Bug introduced by
It seems like getRequest() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

59
        /** @scrutinizer ignore-call */ 
60
        $request = $this->getRequest();
Loading history...
60
61
        if ($request) {
62
            $sessionId = $request->getSession()->get('sid', 0);
63
        }
64
65
        if (empty($sessionId)) {
66
            return null;
67
        }
68
69
        return $this->getDoctrine()->getManager()->find('ChamiloCoreBundle:Session', $sessionId);
0 ignored issues
show
Bug introduced by
It seems like getDoctrine() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

69
        return $this->/** @scrutinizer ignore-call */ getDoctrine()->getManager()->find('ChamiloCoreBundle:Session', $sessionId);
Loading history...
70
    }
71
72
    public function getGroup()
73
    {
74
        $request = $this->getRequest();
75
76
        if ($request) {
77
            $groupId = $request->getSession()->get('gid', 0);
78
        }
79
80
        if (empty($groupId)) {
81
            return null;
82
        }
83
84
        return $this->getDoctrine()->getManager()->find('ChamiloCourseBundle:CGroupInfo', $groupId);
85
    }
86
87
    public function getCourseUrlQuery(): string
88
    {
89
        $url = '';
90
        $course = $this->getCourse();
91
        if ($course) {
0 ignored issues
show
introduced by
$course is of type Chamilo\CoreBundle\Entity\Course, thus it always evaluated to true.
Loading history...
92
            $url = 'cid='.$course->getId();
93
        }
94
95
        $session = $this->getCourseSession();
96
        if ($session) {
97
            $url .= '&sid='.$session->getId();
98
        } else {
99
            $url .= '&sid=0';
100
        }
101
102
        $group = $this->getGroup();
103
        if ($group) {
104
            $url .= '&gid='.$group->getIid();
105
        } else {
106
            $url .= '&gid=0';
107
        }
108
109
        return $url;
110
    }
111
112
    public function getCourseUrlQueryToArray(): array
113
    {
114
        $url = [];
115
        $course = $this->getCourse();
116
        $url['cid'] = 0;
117
        if ($course) {
0 ignored issues
show
introduced by
$course is of type Chamilo\CoreBundle\Entity\Course, thus it always evaluated to true.
Loading history...
118
            $url['cid'] = $course->getId();
119
        }
120
        $session = $this->getCourseSession();
121
122
        $url['sid'] = 0;
123
        if ($session) {
124
            $url['sid'] = $session->getId();
125
        }
126
127
        return $url;
128
    }
129
130
    public function setCourse(Course $course)
131
    {
132
        $this->course = $course;
133
    }
134
135
    /**
136
     * @return Course
137
     */
138
    public function getCourse()
139
    {
140
        return $this->course;
141
    }
142
143
    public function hasCourse(): bool
144
    {
145
        return null !== $this->course;
146
    }
147
148
    /**
149
     * @return Session
150
     */
151
    public function getSession()
152
    {
153
        return $this->session;
154
    }
155
156
    public function setSession(Session $session)
157
    {
158
        $this->session = $session;
159
    }
160
161
    /**
162
     * @return int
163
     */
164
    public function getSessionId()
165
    {
166
        return $this->session ? $this->getSession()->getId() : 0;
167
    }
168
}
169