Completed
Push — master ( 667dde...0fb228 )
by Julito
13:48
created

CourseController::welcomeAction()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 1
b 1
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\ExtraField;
8
use Chamilo\CoreBundle\Form\Type\CourseType;
9
use Chamilo\CoreBundle\Framework\Container;
10
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Entity;
11
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
12
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
13
use Symfony\Component\HttpFoundation\Request;
14
use Symfony\Component\HttpFoundation\Response;
15
use Symfony\Component\Routing\Annotation\Route;
16
use \UserManager;
17
use \CCourseDescription;
18
19
/**
20
 * Class CourseController.
21
 *
22
 * @Route("/courses")
23
 */
24
class CourseController extends AbstractController
25
{
26
    /**
27
     * @Route("/add")
28
     *
29
     * @Security("has_role('ROLE_TEACHER')")
30
     *
31
     * @return Response
32
     */
33
    public function addAction(Request $request)
34
    {
35
        exit;
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
36
        $form = $this->createForm(CourseType::class);
0 ignored issues
show
Unused Code introduced by
$form = $this->createFor...Type\CourseType::class) is not reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
37
38
        $form->handleRequest($request);
39
40
        if ($form->isSubmitted() && $form->isValid()) {
41
            $em = $this->getDoctrine()->getManager();
42
            $course = $form->getData();
43
            /*$em->persist($course);
44
            $em->flush();*/
45
            $this->addFlash('sonata_flash_success', 'Course created');
46
47
            return $this->redirectToRoute(
48
                'chamilo_core_course_welcome',
49
                ['cid' => $course->getId()]
50
            );
51
        }
52
53
        return $this->render('ChamiloThemeBundle:Course:add.html.twig', ['form' => $form->createView()]);
54
    }
55
56
    /**
57
     * Redirects legacy /courses/ABC/index.php to /courses/1/ (where 1 is the course id) see CourseHomeController.
58
     *
59
     * @Route("/{courseCode}/index.php", name="chamilo_core_course_home_redirect")
60
     *
61
     * @Entity("course", expr="repository.findOneByCode(courseCode)")
62
     */
63
    public function homeRedirectAction(Course $course): Response
64
    {
65
        return $this->redirectToRoute('chamilo_core_course_home', ['cid' => $course->getId()]);
66
    }
67
68
    /**
69
     * @Route("/{cid}/welcome", name="chamilo_core_course_welcome")
70
     *
71
     * @Entity("course", expr="repository.find(cid)")
72
     */
73
    public function welcomeAction(Course $course): Response
74
    {
75
        return $this->render('@ChamiloTheme/Course/welcome.html.twig', ['course' => $course]);
76
    }
77
78
    /**
79
     * @Route("/{cid}/about", name="chamilo_core_course_about")
80
     *
81
     * @Entity("course", expr="repository.find(cid)")
82
     */
83
    public function aboutAction(Course $course): Response
84
    {
85
        $courseId = $course->getId();
86
        $userId = $this->getUser()->getId();
87
88
        $em = $this->getDoctrine()->getManager();
89
90
        $fieldsRepo = $em->getRepository('ChamiloCoreBundle:ExtraField');
91
        $fieldTagsRepo = $em->getRepository('ChamiloCoreBundle:ExtraFieldRelTag');
92
93
        /** @var CCourseDescription $courseDescription */
94
        $courseDescriptionTools = $em->getRepository('ChamiloCourseBundle:CCourseDescription')
95
            ->findBy(
96
                [
97
                    'cId' => $course->getId(),
98
                    'sessionId' => 0,
99
                ],
100
                [
101
                    'id' => 'DESC',
102
                    'descriptionType' => 'ASC',
103
                ]
104
            );
105
106
        $courseValues = new \ExtraFieldValue('course');
107
108
        $urlCourse = api_get_path(WEB_PATH)."course/$courseId/about";
109
        $courseTeachers = $course->getTeachers();
110
        $teachersData = [];
111
112
        /** @var CourseRelUser $teacherSubscription */
113
        foreach ($courseTeachers as $teacherSubscription) {
114
            $teacher = $teacherSubscription->getUser();
115
            $userData = [
116
                'complete_name' => UserManager::formatUserFullName($teacher),
117
                'image' => UserManager::getUserPicture(
118
                    $teacher->getId(),
119
                    USER_IMAGE_SIZE_ORIGINAL
120
                ),
121
                'diploma' => $teacher->getDiplomas(),
122
                'openarea' => $teacher->getOpenarea(),
123
            ];
124
125
            $teachersData[] = $userData;
126
        }
127
128
        $tagField = $fieldsRepo->findOneBy([
129
            'extraFieldType' => ExtraField::COURSE_FIELD_TYPE,
130
            'variable' => 'tags',
131
        ]);
132
133
        $courseTags = [];
134
135
        if (!is_null($tagField)) {
136
            $courseTags = $fieldTagsRepo->getTags($tagField, $courseId);
137
        }
138
139
        $courseDescription = $courseObjectives = $courseTopics = $courseMethodology = $courseMaterial = $courseResources = $courseAssessment = '';
140
        $courseCustom = [];
141
        foreach ($courseDescriptionTools as $descriptionTool) {
142
            switch ($descriptionTool->getDescriptionType()) {
143
                case CCourseDescription::TYPE_DESCRIPTION:
144
                    $courseDescription = $descriptionTool->getContent();
145
                    break;
146
                case CCourseDescription::TYPE_OBJECTIVES:
147
                    $courseObjectives = $descriptionTool;
148
                    break;
149
                case CCourseDescription::TYPE_TOPICS:
150
                    $courseTopics = $descriptionTool;
151
                    break;
152
                case CCourseDescription::TYPE_METHODOLOGY:
153
                    $courseMethodology = $descriptionTool;
154
                    break;
155
                case CCourseDescription::TYPE_COURSE_MATERIAL:
156
                    $courseMaterial = $descriptionTool;
157
                    break;
158
                case CCourseDescription::TYPE_RESOURCES:
159
                    $courseResources = $descriptionTool;
160
                    break;
161
                case CCourseDescription::TYPE_ASSESSMENT:
162
                    $courseAssessment = $descriptionTool;
163
                    break;
164
                case CCourseDescription::TYPE_CUSTOM:
165
                    $courseCustom[] = $descriptionTool;
166
                    break;
167
            }
168
        }
169
170
        $topics = [
171
            'objectives' => $courseObjectives,
172
            'topics' => $courseTopics,
173
            'methodology' => $courseMethodology,
174
            'material' => $courseMaterial,
175
            'resources' => $courseResources,
176
            'assessment' => $courseAssessment,
177
            'custom' => array_reverse($courseCustom),
178
        ];
179
180
        $subscriptionUser = \CourseManager::is_user_subscribed_in_course($userId, $course->getCode());
181
182
        $allowSubscribe = false;
183
        if ($course->getSubscribe() || api_is_platform_admin()) {
184
            $allowSubscribe = true;
185
        }
186
        $plugin = \BuyCoursesPlugin::create();
187
        $checker = $plugin->isEnabled();
188
        $courseIsPremium = null;
189
        if ($checker) {
190
            $courseIsPremium = $plugin->getItemByProduct(
191
                $courseId,
192
                \BuyCoursesPlugin::PRODUCT_TYPE_COURSE
193
            );
194
        }
195
196
        $image = Container::getIllustrationRepository()->getIllustrationUrl($course, 'course_picture_medium');
197
        $params = [
198
            'course' => $course,
199
            'description' => $courseDescription,
200
            'image' => $image,
201
            'syllabus' => $topics,
202
            'tags' => $courseTags,
203
            'teachers' => $teachersData,
204
            'extra_fields' => $courseValues->getAllValuesForAnItem(
205
                $course->getId(),
206
                null,
207
                true
208
            ),
209
            'subscription' => $subscriptionUser,
210
        ];
211
212
        $metaInfo = '<meta property="og:url" content="'.$urlCourse.'" />';
213
        $metaInfo .= '<meta property="og:type" content="website" />';
214
        $metaInfo .= '<meta property="og:title" content="'.$course->getTitle().'" />';
215
        $metaInfo .= '<meta property="og:description" content="'.strip_tags($courseDescription).'" />';
216
        $metaInfo .= '<meta property="og:image" content="'.$image.'" />';
217
218
        $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...
219
        $htmlHeadXtra[] = api_get_asset('readmore-js/readmore.js');
220
221
        return $this->render('@ChamiloTheme/Course/about.html.twig', [$params]);
222
    }
223
224
225
}
226