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

CourseController::addAction()   A

Complexity

Conditions 3
Paths 1

Size

Total Lines 21
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 11
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 21
rs 9.9
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}/about", name="chamilo_core_course_about")
70
     *
71
     * @Entity("course", expr="repository.find(cid)")
72
     */
73
    public function aboutAction(Course $course): Response
74
    {
75
        $courseId = $course->getId();
76
        $userId = $this->getUser()->getId();
77
78
        $userRepo = \UserManager::getRepository();
79
        $em = $this->getDoctrine()->getManager();
80
81
        $fieldsRepo = $em->getRepository('ChamiloCoreBundle:ExtraField');
82
        $fieldTagsRepo = $em->getRepository('ChamiloCoreBundle:ExtraFieldRelTag');
83
84
        /** @var CCourseDescription $courseDescription */
85
        $courseDescriptionTools = $em->getRepository('ChamiloCourseBundle:CCourseDescription')
86
            ->findBy(
87
                [
88
                    'cId' => $course->getId(),
89
                    'sessionId' => 0,
90
                ],
91
                [
92
                    'id' => 'DESC',
93
                    'descriptionType' => 'ASC',
94
                ]
95
            );
96
97
        $courseValues = new \ExtraFieldValue('course');
98
99
        $urlCourse = api_get_path(WEB_PATH)."course/$courseId/about";
100
        $courseTeachers = $course->getTeachers();
101
        $teachersData = [];
102
103
        /** @var CourseRelUser $teacherSubscription */
104
        foreach ($courseTeachers as $teacherSubscription) {
105
            $teacher = $teacherSubscription->getUser();
106
            $userData = [
107
                'complete_name' => UserManager::formatUserFullName($teacher),
108
                'image' => UserManager::getUserPicture(
109
                    $teacher->getId(),
110
                    USER_IMAGE_SIZE_ORIGINAL
111
                ),
112
                'diploma' => $teacher->getDiplomas(),
113
                'openarea' => $teacher->getOpenarea(),
114
            ];
115
116
            $teachersData[] = $userData;
117
        }
118
119
        $tagField = $fieldsRepo->findOneBy([
120
            'extraFieldType' => ExtraField::COURSE_FIELD_TYPE,
121
            'variable' => 'tags',
122
        ]);
123
124
        $courseTags = [];
125
126
        if (!is_null($tagField)) {
127
            $courseTags = $fieldTagsRepo->getTags($tagField, $courseId);
128
        }
129
130
        $courseDescription = $courseObjectives = $courseTopics = $courseMethodology = $courseMaterial = $courseResources = $courseAssessment = '';
131
        $courseCustom = [];
132
        foreach ($courseDescriptionTools as $descriptionTool) {
133
            switch ($descriptionTool->getDescriptionType()) {
134
                case CCourseDescription::TYPE_DESCRIPTION:
135
                    $courseDescription = $descriptionTool->getContent();
136
                    break;
137
                case CCourseDescription::TYPE_OBJECTIVES:
138
                    $courseObjectives = $descriptionTool;
139
                    break;
140
                case CCourseDescription::TYPE_TOPICS:
141
                    $courseTopics = $descriptionTool;
142
                    break;
143
                case CCourseDescription::TYPE_METHODOLOGY:
144
                    $courseMethodology = $descriptionTool;
145
                    break;
146
                case CCourseDescription::TYPE_COURSE_MATERIAL:
147
                    $courseMaterial = $descriptionTool;
148
                    break;
149
                case CCourseDescription::TYPE_RESOURCES:
150
                    $courseResources = $descriptionTool;
151
                    break;
152
                case CCourseDescription::TYPE_ASSESSMENT:
153
                    $courseAssessment = $descriptionTool;
154
                    break;
155
                case CCourseDescription::TYPE_CUSTOM:
156
                    $courseCustom[] = $descriptionTool;
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
        $params = [
189
            'course' => $course,
190
            'description' => $courseDescription,
191
            'image' => $image,
192
            'syllabus' => $topics,
193
            'tags' => $courseTags,
194
            'teachers' => $teachersData,
195
            'extra_fields' => $courseValues->getAllValuesForAnItem(
196
                $course->getId(),
197
                null,
198
                true
199
            ),
200
            'subscription' => $subscriptionUser,
201
        ];
202
203
        $metaInfo = '<meta property="og:url" content="'.$urlCourse.'" />';
204
        $metaInfo .= '<meta property="og:type" content="website" />';
205
        $metaInfo .= '<meta property="og:title" content="'.$course->getTitle().'" />';
206
        $metaInfo .= '<meta property="og:description" content="'.strip_tags($courseDescription).'" />';
207
        $metaInfo .= '<meta property="og:image" content="'.$image.'" />';
208
209
        $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...
210
        $htmlHeadXtra[] = api_get_asset('readmore-js/readmore.js');
211
212
        return $this->render('@ChamiloTheme/Course/about.html.twig', [$params]);
213
    }
214
215
216
}
217