Passed
Push — master ( c032cd...29e9e9 )
by Julito
09:25
created

CourseController::aboutAction()   C

Complexity

Conditions 12
Paths 40

Size

Total Lines 146
Code Lines 89

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 12
eloc 89
nc 40
nop 1
dl 0
loc 146
rs 5.8133
c 0
b 0
f 0

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
3
/* For licensing terms, see /license.txt */
4
5
namespace Chamilo\CoreBundle\Controller;
6
7
use Chamilo\CoreBundle\Entity\Course;
8
use Chamilo\CoreBundle\Entity\ExtraField;
9
use Chamilo\CoreBundle\Framework\Container;
10
use Chamilo\CourseBundle\Entity\CCourseDescription;
11
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Entity;
12
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
13
use Symfony\Component\HttpFoundation\Response;
14
use Symfony\Component\Routing\Annotation\Route;
15
use UserManager;
16
17
/**
18
 * Class CourseController.
19
 *
20
 * @Route("/course")
21
 */
22
class CourseController extends AbstractController
23
{
24
    /**
25
     * Redirects legacy /courses/ABC/index.php to /courses/1/ (where 1 is the course id) see CourseHomeController.
26
     *
27
     * @Route("/{courseCode}/index.php", name="chamilo_core_course_home_redirect")
28
     *
29
     * @Entity("course", expr="repository.findOneByCode(courseCode)")
30
     */
31
    public function homeRedirectAction(Course $course): Response
32
    {
33
        return $this->redirectToRoute('chamilo_core_course_home', ['cid' => $course->getId()]);
34
    }
35
36
    /**
37
     * @Route("/{cid}/welcome", name="chamilo_core_course_welcome")
38
     *
39
     * @Entity("course", expr="repository.find(cid)")
40
     */
41
    public function welcomeAction(Course $course): Response
42
    {
43
        return $this->render('@ChamiloTheme/Course/welcome.html.twig', ['course' => $course]);
44
    }
45
46
    /**
47
     * @Route("/{cid}/about", name="chamilo_core_course_about")
48
     *
49
     * @Entity("course", expr="repository.find(cid)")
50
     */
51
    public function aboutAction(Course $course): Response
52
    {
53
        $courseId = $course->getId();
54
        $userId = $this->getUser()->getId();
55
56
        $em = $this->getDoctrine()->getManager();
57
58
        $fieldsRepo = $em->getRepository('ChamiloCoreBundle:ExtraField');
59
        $fieldTagsRepo = $em->getRepository('ChamiloCoreBundle:ExtraFieldRelTag');
60
61
        /** @var CCourseDescription $courseDescription */
62
        $courseDescriptionTools = $em->getRepository('ChamiloCourseBundle:CCourseDescription')
63
            ->findBy(
64
                [
65
                    'cId' => $course->getId(),
66
                    'sessionId' => 0,
67
                ],
68
                [
69
                    'id' => 'DESC',
70
                    'descriptionType' => 'ASC',
71
                ]
72
            );
73
74
        $courseValues = new \ExtraFieldValue('course');
75
76
        $urlCourse = api_get_path(WEB_PATH)."course/$courseId/about";
77
        $courseTeachers = $course->getTeachers();
78
        $teachersData = [];
79
80
        foreach ($courseTeachers as $teacherSubscription) {
81
            $teacher = $teacherSubscription->getUser();
82
            $userData = [
83
                'complete_name' => UserManager::formatUserFullName($teacher),
84
                'image' => UserManager::getUserPicture(
85
                    $teacher->getId(),
86
                    USER_IMAGE_SIZE_ORIGINAL
87
                ),
88
                'diploma' => $teacher->getDiplomas(),
89
                'openarea' => $teacher->getOpenarea(),
90
            ];
91
92
            $teachersData[] = $userData;
93
        }
94
95
        $tagField = $fieldsRepo->findOneBy([
96
            'extraFieldType' => ExtraField::COURSE_FIELD_TYPE,
97
            'variable' => 'tags',
98
        ]);
99
100
        $courseTags = [];
101
102
        if (!is_null($tagField)) {
103
            $courseTags = $fieldTagsRepo->getTags($tagField, $courseId);
104
        }
105
106
        $courseDescription = $courseObjectives = $courseTopics = $courseMethodology = $courseMaterial = $courseResources = $courseAssessment = '';
107
        $courseCustom = [];
108
        foreach ($courseDescriptionTools as $descriptionTool) {
109
            switch ($descriptionTool->getDescriptionType()) {
110
                case CCourseDescription::TYPE_DESCRIPTION:
111
                    $courseDescription = $descriptionTool->getContent();
112
113
                    break;
114
                case CCourseDescription::TYPE_OBJECTIVES:
115
                    $courseObjectives = $descriptionTool;
116
117
                    break;
118
                case CCourseDescription::TYPE_TOPICS:
119
                    $courseTopics = $descriptionTool;
120
121
                    break;
122
                case CCourseDescription::TYPE_METHODOLOGY:
123
                    $courseMethodology = $descriptionTool;
124
125
                    break;
126
                case CCourseDescription::TYPE_COURSE_MATERIAL:
127
                    $courseMaterial = $descriptionTool;
128
129
                    break;
130
                case CCourseDescription::TYPE_RESOURCES:
131
                    $courseResources = $descriptionTool;
132
133
                    break;
134
                case CCourseDescription::TYPE_ASSESSMENT:
135
                    $courseAssessment = $descriptionTool;
136
137
                    break;
138
                case CCourseDescription::TYPE_CUSTOM:
139
                    $courseCustom[] = $descriptionTool;
140
141
                    break;
142
            }
143
        }
144
145
        $topics = [
146
            'objectives' => $courseObjectives,
147
            'topics' => $courseTopics,
148
            'methodology' => $courseMethodology,
149
            'material' => $courseMaterial,
150
            'resources' => $courseResources,
151
            'assessment' => $courseAssessment,
152
            'custom' => array_reverse($courseCustom),
153
        ];
154
155
        $subscriptionUser = \CourseManager::is_user_subscribed_in_course($userId, $course->getCode());
156
157
        /*$allowSubscribe = false;
158
        if ($course->getSubscribe() || api_is_platform_admin()) {
159
            $allowSubscribe = true;
160
        }
161
        $plugin = \BuyCoursesPlugin::create();
162
        $checker = $plugin->isEnabled();
163
        $courseIsPremium = null;
164
        if ($checker) {
165
            $courseIsPremium = $plugin->getItemByProduct(
166
                $courseId,
167
                \BuyCoursesPlugin::PRODUCT_TYPE_COURSE
168
            );
169
        }*/
170
171
        $image = Container::getIllustrationRepository()->getIllustrationUrl($course, 'course_picture_medium');
172
        $params = [
173
            'course' => $course,
174
            'description' => $courseDescription,
175
            'image' => $image,
176
            'syllabus' => $topics,
177
            'tags' => $courseTags,
178
            'teachers' => $teachersData,
179
            'extra_fields' => $courseValues->getAllValuesForAnItem(
180
                $course->getId(),
181
                null,
182
                true
183
            ),
184
            'subscription' => $subscriptionUser,
185
        ];
186
187
        $metaInfo = '<meta property="og:url" content="'.$urlCourse.'" />';
188
        $metaInfo .= '<meta property="og:type" content="website" />';
189
        $metaInfo .= '<meta property="og:title" content="'.$course->getTitle().'" />';
190
        $metaInfo .= '<meta property="og:description" content="'.strip_tags($courseDescription).'" />';
191
        $metaInfo .= '<meta property="og:image" content="'.$image.'" />';
192
193
        $htmlHeadXtra[] = $metaInfo;
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...
194
        $htmlHeadXtra[] = api_get_asset('readmore-js/readmore.js');
195
196
        return $this->render('@ChamiloTheme/Course/about.html.twig', [$params]);
197
    }
198
}
199