Completed
Push — master ( 76b5db...d28136 )
by Julito
13:35
created

SessionController::aboutAction()   F

Complexity

Conditions 22
Paths 1296

Size

Total Lines 207
Code Lines 151

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 22
eloc 151
c 1
b 0
f 0
nop 1
dl 0
loc 207
rs 0
nc 1296

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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\ExtraField;
8
use Chamilo\CoreBundle\Entity\SequenceResource;
9
use Chamilo\CoreBundle\Entity\Session;
10
use Chamilo\CoreBundle\Form\Type\CourseType;
11
use Chamilo\CoreBundle\Framework\Container;
12
use Chamilo\CourseBundle\Entity\CCourseDescription;
13
use Essence\Essence;
14
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Entity;
15
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
16
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
17
use Symfony\Component\HttpFoundation\Request;
18
use Symfony\Component\HttpFoundation\Response;
19
use Symfony\Component\Routing\Annotation\Route;
20
use SessionManager;
21
use ExtraFieldValue;
22
use UserManager;
23
use CoursesController;
24
25
/**
26
 * Class SessionController.
27
 *
28
 * @Route("/sessions")
29
 */
30
class SessionController extends AbstractController
31
{
32
    /**
33
     * @Route("/{sid}/about", name="chamilo_core_session_about")
34
     *
35
     * @Entity("session", expr="repository.find(sid)")
36
     */
37
    public function aboutAction(Session $session): Response
38
    {
39
        $htmlHeadXtra[] = api_get_asset('readmore-js/readmore.js');
0 ignored issues
show
Comprehensibility Best Practice introduced by
$htmlHeadXtra was never initialized. Although not strictly required by PHP, it is generally a good practice to add $htmlHeadXtra = array(); before regardless.
Loading history...
40
        $em = $this->getDoctrine()->getManager();
41
42
        $sessionId = $session->getId();
43
44
        $courses = [];
45
        $sessionCourses = $session->getCourses();
46
        $fieldsRepo = $em->getRepository('ChamiloCoreBundle:ExtraField');
47
        $fieldTagsRepo = $em->getRepository('ChamiloCoreBundle:ExtraFieldRelTag');
48
        $userRepo = \UserManager::getRepository();
49
50
        /** @var SequenceRepository $sequenceResourceRepo */
51
        $sequenceResourceRepo = $em->getRepository('ChamiloCoreBundle:SequenceResource');
52
53
        $tagField = $fieldsRepo->findOneBy([
54
            'extraFieldType' => ExtraField::COURSE_FIELD_TYPE,
55
            'variable' => 'tags',
56
        ]);
57
58
        $courseValues = new ExtraFieldValue('course');
59
        $userValues = new ExtraFieldValue('user');
60
        $sessionValues = new ExtraFieldValue('session');
61
62
        /** @var SessionRelCourse $sessionRelCourse */
63
        foreach ($sessionCourses as $sessionRelCourse) {
64
            $sessionCourse = $sessionRelCourse->getCourse();
65
            $courseTags = [];
66
67
            if (!is_null($tagField)) {
68
                $courseTags = $fieldTagsRepo->getTags($tagField, $sessionCourse->getId());
69
            }
70
71
            $courseCoaches = $userRepo->getCoachesForSessionCourse($session, $sessionCourse);
72
            $coachesData = [];
73
            /** @var User $courseCoach */
74
            foreach ($courseCoaches as $courseCoach) {
75
                $coachData = [
76
                    'complete_name' => UserManager::formatUserFullName($courseCoach),
77
                    'image' => UserManager::getUserPicture(
78
                        $courseCoach->getId(),
79
                        USER_IMAGE_SIZE_ORIGINAL
80
                    ),
81
                    'diploma' => $courseCoach->getDiplomas(),
82
                    'openarea' => $courseCoach->getOpenarea(),
83
                    'extra_fields' => $userValues->getAllValuesForAnItem(
84
                        $courseCoach->getId(),
85
                        null,
86
                        true
87
                    ),
88
                ];
89
90
                $coachesData[] = $coachData;
91
            }
92
93
            $cd = new \CourseDescription();
94
            $cd->set_course_id($sessionCourse->getId());
95
            $cd->set_session_id($session->getId());
96
            $descriptionsData = $cd->get_description_data();
97
98
            $courseDescription = [];
99
            $courseObjectives = [];
100
            $courseTopics = [];
101
            $courseMethodology = [];
102
            $courseMaterial = [];
103
            $courseResources = [];
104
            $courseAssessment = [];
105
            $courseCustom = [];
106
107
            if (!empty($descriptionsData['descriptions'])) {
108
                foreach ($descriptionsData['descriptions'] as $descriptionInfo) {
109
                    switch ($descriptionInfo['description_type']) {
110
                        case CCourseDescription::TYPE_DESCRIPTION:
111
                            $courseDescription[] = $descriptionInfo;
112
                            break;
113
                        case CCourseDescription::TYPE_OBJECTIVES:
114
                            $courseObjectives[] = $descriptionInfo;
115
                            break;
116
                        case CCourseDescription::TYPE_TOPICS:
117
                            $courseTopics[] = $descriptionInfo;
118
                            break;
119
                        case CCourseDescription::TYPE_METHODOLOGY:
120
                            $courseMethodology[] = $descriptionInfo;
121
                            break;
122
                        case CCourseDescription::TYPE_COURSE_MATERIAL:
123
                            $courseMaterial[] = $descriptionInfo;
124
                            break;
125
                        case CCourseDescription::TYPE_RESOURCES:
126
                            $courseResources[] = $descriptionInfo;
127
                            break;
128
                        case CCourseDescription::TYPE_ASSESSMENT:
129
                            $courseAssessment[] = $descriptionInfo;
130
                            break;
131
                        case CCourseDescription::TYPE_CUSTOM:
132
                            $courseCustom[] = $descriptionInfo;
133
                            break;
134
                    }
135
                }
136
            }
137
138
            $courses[] = [
139
                'course' => $sessionCourse,
140
                'description' => $courseDescription,
141
                'image' => Container::getIllustrationRepository()->getIllustrationUrl($sessionCourse),
142
                'tags' => $courseTags,
143
                'objectives' => $courseObjectives,
144
                'topics' => $courseTopics,
145
                'methodology' => $courseMethodology,
146
                'material' => $courseMaterial,
147
                'resources' => $courseResources,
148
                'assessment' => $courseAssessment,
149
                'custom' => array_reverse($courseCustom),
150
                'coaches' => $coachesData,
151
                'extra_fields' => $courseValues->getAllValuesForAnItem(
152
                    $sessionCourse->getId(),
153
                    null,
154
                    true
155
                ),
156
            ];
157
        }
158
159
        $sessionDates = SessionManager::parseSessionDates(
160
            [
161
                'display_start_date' => $session->getDisplayStartDate(),
162
                'display_end_date' => $session->getDisplayEndDate(),
163
                'access_start_date' => $session->getAccessStartDate(),
164
                'access_end_date' => $session->getAccessEndDate(),
165
                'coach_access_start_date' => $session->getCoachAccessStartDate(),
166
                'coach_access_end_date' => $session->getCoachAccessEndDate(),
167
            ],
168
            true
169
        );
170
171
        $sessionRequirements = $sequenceResourceRepo->getRequirements(
172
            $session->getId(),
173
            SequenceResource::SESSION_TYPE
174
        );
175
176
        $hasRequirements = false;
177
        foreach ($sessionRequirements as $sequence) {
178
            if (!empty($sequence['requirements'])) {
179
                $hasRequirements = true;
180
                break;
181
            }
182
        }
183
184
        $courseController = new CoursesController();
185
186
187
188
189
        $plugin = \BuyCoursesPlugin::create();
190
        $checker = $plugin->isEnabled();
191
        $sessionIsPremium = null;
192
        if ($checker) {
193
            $sessionIsPremium = $plugin->getItemByProduct(
194
                $sessionId,
195
                \BuyCoursesPlugin::PRODUCT_TYPE_SESSION
196
            );
197
            if ($sessionIsPremium) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $sessionIsPremium of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
198
                ChamiloSession::write('SessionIsPremium', true);
0 ignored issues
show
Bug introduced by
The type Chamilo\CoreBundle\Controller\ChamiloSession was not found. Did you mean ChamiloSession? If so, make sure to prefix the type with \.
Loading history...
199
                ChamiloSession::write('sessionId', $sessionId);
200
            }
201
        }
202
203
        $redirectToSession = api_get_configuration_value('allow_redirect_to_session_after_inscription_about');
204
        $redirectToSession = $redirectToSession ? '?s='.$sessionId : false;
205
206
        $coursesInThisSession = \SessionManager::get_course_list_by_session_id($sessionId);
207
        $coursesCount = count($coursesInThisSession);
208
        $redirectToSession = $coursesCount == 1 && $redirectToSession
209
            ? ($redirectToSession.'&cr='.array_values($coursesInThisSession)[0]['directory'])
210
            : $redirectToSession;
211
212
        $essence = new Essence();
213
214
        $params = [
215
            'session' => $session,
216
            'redirect_to_session' => $redirectToSession,
217
            'courses' => $courses,
218
            'essence' => $essence,
219
            'session_extra_fields' => $sessionValues->getAllValuesForAnItem($session->getId(), null, true),
220
            'has_requirements' => $hasRequirements,
221
            'sequences' => $sessionRequirements,
222
            'is_premium' => $sessionIsPremium,
223
            'show_tutor' => api_get_setting('show_session_coach') === 'true' ? true : false,
224
            'page_url' => api_get_path(WEB_PATH)."sessions/{$session->getId()}/about/",
225
            'session_date' => $sessionDates,
226
            'is_subscribed' => SessionManager::isUserSubscribedAsStudent(
227
                $session->getId(),
228
                api_get_user_id()
229
            ),
230
            'subscribe_button' => $courseController->getRegisteredInSessionButton(
231
                $session->getId(),
232
                $session->getName(),
233
                $hasRequirements,
234
                true,
235
                true
236
            ),
237
            'user_session_time' => SessionManager::getDayLeftInSession(
238
                ['id' => $session->getId(), 'duration' => $session->getDuration()],
239
                api_get_user_id()
240
            ),
241
        ];
242
243
        return $this->render('@ChamiloTheme/Session/about.html.twig', $params);
244
    }
245
}
246