Passed
Push — master ( fd56f0...b8f617 )
by Julito
09:47
created

CourseController::aboutAction()   C

Complexity

Conditions 12
Paths 40

Size

Total Lines 137
Code Lines 86

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 12
eloc 86
nc 40
nop 3
dl 0
loc 137
rs 5.8787
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A CourseController::welcome() 0 5 1

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
declare(strict_types=1);
4
5
/* For licensing terms, see /license.txt */
6
7
namespace Chamilo\CoreBundle\Controller;
8
9
use Chamilo\CoreBundle\Entity\Course;
10
use Chamilo\CoreBundle\Entity\ExtraField;
11
use Chamilo\CoreBundle\Entity\ExtraFieldRelTag;
12
use Chamilo\CoreBundle\Framework\Container;
13
use Chamilo\CoreBundle\Repository\ExtraFieldRelTagRepository;
14
use Chamilo\CoreBundle\Repository\Node\IllustrationRepository;
15
use Chamilo\CourseBundle\Entity\CCourseDescription;
16
use Chamilo\CourseBundle\Entity\CDocument;
17
use Chamilo\CourseBundle\Repository\CCourseDescriptionRepository;
18
use Chamilo\CourseBundle\Repository\CDocumentRepository;
19
use CourseManager;
20
use Doctrine\ORM\EntityRepository;
21
use ExtraFieldValue;
22
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
23
use Symfony\Component\HttpFoundation\Response;
24
use Symfony\Component\Routing\Annotation\Route;
25
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
26
use UserManager;
27
28
#[Route('/courses')]
29
class CourseController extends AbstractController
30
{
31
    /**
32
     * Redirects legacy /courses/ABC/index.php to /courses/1/ (where 1 is the course id) see CourseHomeController.
33
     */
34
    #[Route('/{code}/index.php', name: 'chamilo_core_course_home_redirect')]
35
    public function homeRedirect(Course $course): Response
36
    {
37
        return $this->redirectToRoute('chamilo_core_course_home', [
38
            'cid' => $course->getId(),
39
        ]);
40
    }
41
42
    /**
43
     * Redirects legacy /courses/ABC/document/images/file.jpg.
44
     */
45
    #[Route('/{code}/document/{path}', name: 'chamilo_core_course_document_redirect', requirements: ['path' => '.*'])]
46
    public function documentRedirect(Course $course, string $path, CDocumentRepository $documentRepository): Response
47
    {
48
        $pathList = explode('/', $path);
49
50
        /** @var CDocument|null $document */
51
        $document = null;
52
        $parent = $course;
53
        foreach ($pathList as $part) {
54
            $document = $documentRepository->findCourseResourceByTitle($part, $parent->getResourceNode(), $course);
55
            if (null !== $document) {
56
                $parent = $document;
57
            }
58
        }
59
60
        if (null !== $document && $document->getResourceNode()->hasResourceFile()) {
61
            return $this->redirectToRoute('chamilo_core_resource_view', [
62
                'tool' => 'document',
63
                'type' => 'file',
64
                'id' => $document->getResourceNode()->getId(),
65
            ]);
66
        }
67
68
        throw new AccessDeniedException('File not found');
69
    }
70
71
    #[Route('/{id}/welcome', name: 'chamilo_core_course_welcome')]
72
    public function welcome(Course $course): Response
73
    {
74
        return $this->render('@ChamiloCore/Course/welcome.html.twig', [
75
            'course' => $course,
76
        ]);
77
    }
78
79
    #[Route('/{id}/about', name: 'chamilo_core_course_about')]
80
    public function about(Course $course, IllustrationRepository $illustrationRepository, CCourseDescriptionRepository $courseDescriptionRepository): Response
81
    {
82
        $courseId = $course->getId();
83
        $userId = $this->getUser()->getId();
84
        $em = $this->getDoctrine()->getManager();
85
86
        /** @var EntityRepository $fieldsRepo */
87
        $fieldsRepo = $em->getRepository(ExtraField::class);
88
        /** @var ExtraFieldRelTagRepository $fieldTagsRepo */
89
        $fieldTagsRepo = $em->getRepository(ExtraFieldRelTag::class);
90
91
        $courseDescriptions = $courseDescriptionRepository->getResourcesByCourse($course)->getQuery()->getResult();
92
93
        $courseValues = new ExtraFieldValue('course');
94
95
        $urlCourse = api_get_path(WEB_PATH).sprintf('course/%s/about', $courseId);
96
        $courseTeachers = $course->getTeachers();
97
        $teachersData = [];
98
99
        foreach ($courseTeachers as $teacherSubscription) {
100
            $teacher = $teacherSubscription->getUser();
101
            $userData = [
102
                'complete_name' => UserManager::formatUserFullName($teacher),
103
                'image' => $illustrationRepository->getIllustrationUrl($teacher),
104
                'diploma' => $teacher->getDiplomas(),
105
                'openarea' => $teacher->getOpenarea(),
106
            ];
107
108
            $teachersData[] = $userData;
109
        }
110
        /** @var ExtraField $tagField */
111
        $tagField = $fieldsRepo->findOneBy([
112
            'extraFieldType' => ExtraField::COURSE_FIELD_TYPE,
113
            'variable' => 'tags',
114
        ]);
115
116
        $courseTags = [];
117
        if (null !== $tagField) {
118
            $courseTags = $fieldTagsRepo->getTags($tagField, $courseId);
119
        }
120
121
        $courseDescription = $courseObjectives = $courseTopics = $courseMethodology = '';
122
        $courseMaterial = $courseResources = $courseAssessment = '';
123
        $courseCustom = [];
124
        foreach ($courseDescriptions as $descriptionTool) {
125
            switch ($descriptionTool->getDescriptionType()) {
126
                case CCourseDescription::TYPE_DESCRIPTION:
127
                    $courseDescription = $descriptionTool->getContent();
128
129
                    break;
130
                case CCourseDescription::TYPE_OBJECTIVES:
131
                    $courseObjectives = $descriptionTool;
132
133
                    break;
134
                case CCourseDescription::TYPE_TOPICS:
135
                    $courseTopics = $descriptionTool;
136
137
                    break;
138
                case CCourseDescription::TYPE_METHODOLOGY:
139
                    $courseMethodology = $descriptionTool;
140
141
                    break;
142
                case CCourseDescription::TYPE_COURSE_MATERIAL:
143
                    $courseMaterial = $descriptionTool;
144
145
                    break;
146
                case CCourseDescription::TYPE_RESOURCES:
147
                    $courseResources = $descriptionTool;
148
149
                    break;
150
                case CCourseDescription::TYPE_ASSESSMENT:
151
                    $courseAssessment = $descriptionTool;
152
153
                    break;
154
                case CCourseDescription::TYPE_CUSTOM:
155
                    $courseCustom[] = $descriptionTool;
156
157
                    break;
158
            }
159
        }
160
161
        $topics = [
162
            'objectives' => $courseObjectives,
163
            'topics' => $courseTopics,
164
            'methodology' => $courseMethodology,
165
            'material' => $courseMaterial,
166
            'resources' => $courseResources,
167
            'assessment' => $courseAssessment,
168
            'custom' => array_reverse($courseCustom),
169
        ];
170
171
        $subscriptionUser = CourseManager::is_user_subscribed_in_course($userId, $course->getCode());
172
173
        /*$allowSubscribe = false;
174
        if ($course->getSubscribe() || api_is_platform_admin()) {
175
            $allowSubscribe = true;
176
        }
177
        $plugin = \BuyCoursesPlugin::create();
178
        $checker = $plugin->isEnabled();
179
        $courseIsPremium = null;
180
        if ($checker) {
181
            $courseIsPremium = $plugin->getItemByProduct(
182
                $courseId,
183
                \BuyCoursesPlugin::PRODUCT_TYPE_COURSE
184
            );
185
        }*/
186
187
        $image = Container::getIllustrationRepository()->getIllustrationUrl($course, 'course_picture_medium');
188
189
        $params = [
190
            'course' => $course,
191
            'description' => $courseDescription,
192
            'image' => $image,
193
            'syllabus' => $topics,
194
            'tags' => $courseTags,
195
            'teachers' => $teachersData,
196
            'extra_fields' => $courseValues->getAllValuesForAnItem(
197
                $course->getId(),
198
                null,
199
                true
200
            ),
201
            'subscription' => $subscriptionUser,
202
            'url' => '',
203
            'is_premium' => '',
204
            'token' => '',
205
        ];
206
207
        $metaInfo = '<meta property="og:url" content="'.$urlCourse.'" />';
208
        $metaInfo .= '<meta property="og:type" content="website" />';
209
        $metaInfo .= '<meta property="og:title" content="'.$course->getTitle().'" />';
210
        $metaInfo .= '<meta property="og:description" content="'.strip_tags($courseDescription).'" />';
211
        $metaInfo .= '<meta property="og:image" content="'.$image.'" />';
212
213
        $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...
214
        $htmlHeadXtra[] = api_get_asset('readmore-js/readmore.js');
215
216
        return $this->render('@ChamiloCore/Course/about.html.twig', $params);
217
    }
218
}
219