Passed
Push — master ( 1104d8...ef9410 )
by
unknown
09:25 queued 15s
created

CourseController::isUserEnrolledInAnySession()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 2
dl 0
loc 7
rs 10
c 0
b 0
f 0
1
<?php
2
3
/* For licensing terms, see /license.txt */
4
5
declare(strict_types=1);
6
7
namespace Chamilo\CoreBundle\Controller;
8
9
use Chamilo\CoreBundle\Entity\Course;
10
use Chamilo\CoreBundle\Entity\CourseRelUser;
11
use Chamilo\CoreBundle\Entity\ExtraField;
12
use Chamilo\CoreBundle\Entity\Session;
13
use Chamilo\CoreBundle\Entity\SessionRelUser;
14
use Chamilo\CoreBundle\Entity\Tag;
15
use Chamilo\CoreBundle\Entity\Tool;
16
use Chamilo\CoreBundle\Entity\User;
17
use Chamilo\CoreBundle\Framework\Container;
18
use Chamilo\CoreBundle\Repository\CourseCategoryRepository;
19
use Chamilo\CoreBundle\Repository\ExtraFieldValuesRepository;
20
use Chamilo\CoreBundle\Repository\LanguageRepository;
21
use Chamilo\CoreBundle\Repository\LegalRepository;
22
use Chamilo\CoreBundle\Repository\Node\CourseRepository;
23
use Chamilo\CoreBundle\Repository\Node\IllustrationRepository;
24
use Chamilo\CoreBundle\Repository\TagRepository;
25
use Chamilo\CoreBundle\Security\Authorization\Voter\CourseVoter;
26
use Chamilo\CoreBundle\Service\CourseService;
27
use Chamilo\CoreBundle\ServiceHelper\AccessUrlHelper;
28
use Chamilo\CoreBundle\ServiceHelper\UserHelper;
29
use Chamilo\CoreBundle\Settings\SettingsManager;
30
use Chamilo\CoreBundle\Tool\ToolChain;
31
use Chamilo\CourseBundle\Controller\ToolBaseController;
32
use Chamilo\CourseBundle\Entity\CCourseDescription;
33
use Chamilo\CourseBundle\Entity\CTool;
34
use Chamilo\CourseBundle\Entity\CToolIntro;
35
use Chamilo\CourseBundle\Repository\CCourseDescriptionRepository;
36
use Chamilo\CourseBundle\Repository\CLpRepository;
37
use Chamilo\CourseBundle\Repository\CQuizRepository;
38
use Chamilo\CourseBundle\Repository\CShortcutRepository;
39
use Chamilo\CourseBundle\Repository\CToolRepository;
40
use Chamilo\CourseBundle\Settings\SettingsCourseManager;
41
use Chamilo\CourseBundle\Settings\SettingsFormFactory;
42
use CourseManager;
43
use Database;
44
use Display;
45
use Doctrine\ORM\EntityManagerInterface;
46
use Event;
47
use Exception;
48
use Exercise;
49
use ExtraFieldValue;
50
use Symfony\Bridge\Doctrine\Attribute\MapEntity;
51
use Symfony\Component\HttpFoundation\JsonResponse;
52
use Symfony\Component\HttpFoundation\RedirectResponse;
53
use Symfony\Component\HttpFoundation\Request;
54
use Symfony\Component\HttpFoundation\Response;
55
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
56
use Symfony\Component\Routing\Attribute\Route;
57
use Symfony\Component\Security\Http\Attribute\IsGranted;
58
use Symfony\Component\Serializer\SerializerInterface;
59
use Symfony\Component\Validator\Exception\ValidatorException;
60
use Symfony\Contracts\Translation\TranslatorInterface;
61
use UserManager;
62
63
/**
64
 * @author Julio Montoya <[email protected]>
65
 */
66
#[Route('/course')]
67
class CourseController extends ToolBaseController
68
{
69
    public function __construct(
70
        private readonly EntityManagerInterface $em,
71
        private readonly SerializerInterface $serializer,
72
        private readonly UserHelper $userHelper,
73
    ) {}
74
75
    #[IsGranted('ROLE_USER')]
76
    #[Route('/{cid}/checkLegal.json', name: 'chamilo_core_course_check_legal_json')]
77
    public function checkTermsAndConditionJson(
78
        Request $request,
79
        LegalRepository $legalTermsRepo,
80
        LanguageRepository $languageRepository,
81
        ExtraFieldValuesRepository $extraFieldValuesRepository,
82
        SettingsManager $settingsManager
83
    ): Response {
84
        $user = $this->userHelper->getCurrent();
85
        $course = $this->getCourse();
86
        $responseData = [
87
            'redirect' => false,
88
            'url' => '#',
89
        ];
90
91
        if ($user->hasRole('ROLE_STUDENT')
92
            && 'true' === $settingsManager->getSetting('registration.allow_terms_conditions')
93
            && 'course' === $settingsManager->getSetting('platform.load_term_conditions_section')
94
        ) {
95
            $termAndConditionStatus = false;
96
            $extraValue = $extraFieldValuesRepository->findLegalAcceptByItemId($user->getId());
97
            if (!empty($extraValue['value'])) {
98
                $result = $extraValue['value'];
99
                $userConditions = explode(':', $result);
100
                $version = $userConditions[0];
101
                $langId = (int) $userConditions[1];
102
                $realVersion = $legalTermsRepo->getLastVersion($langId);
103
                $termAndConditionStatus = ($version >= $realVersion);
104
            }
105
106
            if (false === $termAndConditionStatus) {
107
                $request->getSession()->set('term_and_condition', ['user_id' => $user->getId()]);
108
109
                $redirect = true;
110
111
                if ('true' === $settingsManager->getSetting('course.allow_public_course_with_no_terms_conditions')
112
                    && Course::OPEN_WORLD === $course->getVisibility()
113
                ) {
114
                    $redirect = false;
115
                }
116
117
                if ($redirect && !$this->isGranted('ROLE_ADMIN')) {
118
                    $responseData = [
119
                        'redirect' => true,
120
                        'url' => '/main/auth/inscription.php',
121
                    ];
122
                }
123
            } else {
124
                $request->getSession()->remove('term_and_condition');
125
            }
126
        }
127
128
        return new JsonResponse($responseData);
129
    }
130
131
    #[Route('/{cid}/home.json', name: 'chamilo_core_course_home_json')]
132
    public function indexJson(
133
        Request $request,
134
        CShortcutRepository $shortcutRepository,
135
        EntityManagerInterface $em,
136
    ): Response {
137
        $requestData = json_decode($request->getContent(), true);
138
        // Sort behaviour
139
        if (!empty($requestData) && isset($requestData['toolItem'])) {
140
            $index = $requestData['index'];
141
            $toolItem = $requestData['toolItem'];
142
            $toolId = (int) $toolItem['iid'];
143
144
            /** @var CTool $cTool */
145
            $cTool = $em->find(CTool::class, $toolId);
146
147
            if ($cTool) {
148
                $cTool->setPosition($index + 1);
149
                $em->persist($cTool);
150
                $em->flush();
151
            }
152
        }
153
154
        $course = $this->getCourse();
155
        $sessionId = $this->getSessionId();
156
        $isInASession = $sessionId > 0;
157
158
        if (null === $course) {
159
            throw $this->createAccessDeniedException();
160
        }
161
162
        if (empty($sessionId)) {
163
            $this->denyAccessUnlessGranted(CourseVoter::VIEW, $course);
164
        }
165
166
        $sessionHandler = $request->getSession();
167
168
        $userId = 0;
169
170
        $user = $this->userHelper->getCurrent();
171
        if (null !== $user) {
172
            $userId = $user->getId();
173
        }
174
175
        $courseCode = $course->getCode();
176
        $courseId = $course->getId();
177
178
        if ($user && $user->hasRole('ROLE_INVITEE')) {
179
            $isSubscribed = CourseManager::is_user_subscribed_in_course(
180
                $userId,
181
                $courseCode,
182
                $isInASession,
183
                $sessionId
184
            );
185
186
            if (!$isSubscribed) {
187
                throw $this->createAccessDeniedException();
188
            }
189
        }
190
191
        $isSpecialCourse = CourseManager::isSpecialCourse($courseId);
192
193
        if ($user && $isSpecialCourse && (isset($_GET['autoreg']) && 1 === (int) $_GET['autoreg'])
194
            && CourseManager::subscribeUser($userId, $courseId, STUDENT)
195
        ) {
196
            $sessionHandler->set('is_allowed_in_course', true);
197
        }
198
199
        $logInfo = [
200
            'tool' => 'course-main',
201
        ];
202
        Event::registerLog($logInfo);
203
204
        // Deleting the objects
205
        $sessionHandler->remove('toolgroup');
206
        $sessionHandler->remove('_gid');
207
        $sessionHandler->remove('oLP');
208
        $sessionHandler->remove('lpobject');
209
210
        api_remove_in_gradebook();
211
        Exercise::cleanSessionVariables();
212
213
        $shortcuts = [];
214
        if (null !== $user) {
215
            $shortcutQuery = $shortcutRepository->getResources($course->getResourceNode());
216
            $shortcuts = $shortcutQuery->getQuery()->getResult();
217
        }
218
        $responseData = [
219
            'shortcuts' => $shortcuts,
220
            'diagram' => '',
221
        ];
222
223
        $json = $this->serializer->serialize(
224
            $responseData,
225
            'json',
226
            [
227
                'groups' => ['course:read', 'ctool:read', 'tool:read', 'cshortcut:read'],
228
            ]
229
        );
230
231
        return new Response(
232
            $json,
233
            Response::HTTP_OK,
234
            [
235
                'Content-type' => 'application/json',
236
            ]
237
        );
238
    }
239
240
    /**
241
     * Redirects the page to a tool, following the tools settings.
242
     */
243
    #[Route('/{cid}/tool/{toolName}', name: 'chamilo_core_course_redirect_tool')]
244
    public function redirectTool(
245
        Request $request,
246
        string $toolName,
247
        CToolRepository $repo,
248
        ToolChain $toolChain
249
    ): RedirectResponse {
250
        /** @var CTool|null $tool */
251
        $tool = $repo->findOneBy([
252
            'title' => $toolName,
253
        ]);
254
255
        if (null === $tool) {
256
            throw new NotFoundHttpException($this->trans('Tool not found'));
257
        }
258
259
        $tool = $toolChain->getToolFromName($tool->getTool()->getTitle());
260
        $link = $tool->getLink();
261
262
        if (null === $this->getCourse()) {
263
            throw new NotFoundHttpException($this->trans('Course not found'));
264
        }
265
        $optionalParams = '';
266
267
        $optionalParams = $request->query->get('cert') ? '&cert='.$request->query->get('cert') : '';
268
269
        if (strpos($link, 'nodeId')) {
270
            $nodeId = (string) $this->getCourse()->getResourceNode()->getId();
271
            $link = str_replace(':nodeId', $nodeId, $link);
272
        }
273
274
        $url = $link.'?'.$this->getCourseUrlQuery().$optionalParams;
275
276
        return $this->redirect($url);
277
    }
278
279
    /*public function redirectToShortCut(string $toolName, CToolRepository $repo, ToolChain $toolChain): RedirectResponse
280
     * {
281
     * $tool = $repo->findOneBy([
282
     * 'name' => $toolName,
283
     * ]);
284
     * if (null === $tool) {
285
     * throw new NotFoundHttpException($this->trans('Tool not found'));
286
     * }
287
     * $tool = $toolChain->getToolFromName($tool->getTool()->getTitle());
288
     * $link = $tool->getLink();
289
     * if (strpos($link, 'nodeId')) {
290
     * $nodeId = (string) $this->getCourse()->getResourceNode()->getId();
291
     * $link = str_replace(':nodeId', $nodeId, $link);
292
     * }
293
     * $url = $link.'?'.$this->getCourseUrlQuery();
294
     * return $this->redirect($url);
295
     * }*/
296
297
    /**
298
     * Edit configuration with given namespace.
299
     */
300
    #[Route('/{course}/settings/{namespace}', name: 'chamilo_core_course_settings')]
301
    public function updateSettings(
302
        Request $request,
303
        #[MapEntity(expr: 'repository.find(cid)')]
304
        Course $course,
305
        string $namespace,
306
        SettingsCourseManager $manager,
307
        SettingsFormFactory $formFactory
308
    ): Response {
309
        $this->denyAccessUnlessGranted(CourseVoter::VIEW, $course);
310
311
        $schemaAlias = $manager->convertNameSpaceToService($namespace);
312
        $settings = $manager->load($namespace);
313
314
        $form = $formFactory->create($schemaAlias);
315
316
        $form->setData($settings);
317
        $form->handleRequest($request);
318
319
        if ($form->isSubmitted() && $form->isValid()) {
320
            $messageType = 'success';
321
322
            try {
323
                $manager->setCourse($course);
324
                $manager->save($form->getData());
325
                $message = $this->trans('Update');
326
            } catch (ValidatorException $validatorException) {
327
                $message = $this->trans($validatorException->getMessage());
328
                $messageType = 'error';
329
            }
330
            $this->addFlash($messageType, $message);
331
332
            if ($request->headers->has('referer')) {
333
                return $this->redirect($request->headers->get('referer'));
334
            }
335
        }
336
337
        $schemas = $manager->getSchemas();
338
339
        return $this->render(
340
            '@ChamiloCore/Course/settings.html.twig',
341
            [
342
                'course' => $course,
343
                'schemas' => $schemas,
344
                'settings' => $settings,
345
                'form' => $form,
346
            ]
347
        );
348
    }
349
350
    #[Route('/{id}/about', name: 'chamilo_core_course_about')]
351
    public function about(
352
        Course $course,
353
        IllustrationRepository $illustrationRepository,
354
        CCourseDescriptionRepository $courseDescriptionRepository,
355
        EntityManagerInterface $em,
356
        Request $request
357
    ): Response {
358
        $courseId = $course->getId();
359
360
        $user = $this->userHelper->getCurrent();
361
362
        $fieldsRepo = $em->getRepository(ExtraField::class);
363
364
        /** @var TagRepository $tagRepo */
365
        $tagRepo = $em->getRepository(Tag::class);
366
367
        $courseDescriptions = $courseDescriptionRepository->getResourcesByCourse($course)->getQuery()->getResult();
368
369
        $courseValues = new ExtraFieldValue('course');
370
371
        $urlCourse = api_get_path(WEB_PATH).\sprintf('course/%s/about', $courseId);
372
        $courseTeachers = $course->getTeachersSubscriptions();
373
        $teachersData = [];
374
375
        foreach ($courseTeachers as $teacherSubscription) {
376
            $teacher = $teacherSubscription->getUser();
377
            $userData = [
378
                'complete_name' => UserManager::formatUserFullName($teacher),
379
                'image' => $illustrationRepository->getIllustrationUrl($teacher),
380
                'diploma' => $teacher->getDiplomas(),
381
                'openarea' => $teacher->getOpenarea(),
382
            ];
383
384
            $teachersData[] = $userData;
385
        }
386
387
        /** @var ExtraField $tagField */
388
        $tagField = $fieldsRepo->findOneBy([
389
            'itemType' => ExtraField::COURSE_FIELD_TYPE,
390
            'variable' => 'tags',
391
        ]);
392
393
        $courseTags = [];
394
        if (null !== $tagField) {
395
            $courseTags = $tagRepo->getTagsByItem($tagField, $courseId);
396
        }
397
398
        $courseDescription = $courseObjectives = $courseTopics = $courseMethodology = '';
399
        $courseMaterial = $courseResources = $courseAssessment = '';
400
        $courseCustom = [];
401
        foreach ($courseDescriptions as $descriptionTool) {
402
            switch ($descriptionTool->getDescriptionType()) {
403
                case CCourseDescription::TYPE_DESCRIPTION:
404
                    $courseDescription = $descriptionTool->getContent();
405
406
                    break;
407
408
                case CCourseDescription::TYPE_OBJECTIVES:
409
                    $courseObjectives = $descriptionTool;
410
411
                    break;
412
413
                case CCourseDescription::TYPE_TOPICS:
414
                    $courseTopics = $descriptionTool;
415
416
                    break;
417
418
                case CCourseDescription::TYPE_METHODOLOGY:
419
                    $courseMethodology = $descriptionTool;
420
421
                    break;
422
423
                case CCourseDescription::TYPE_COURSE_MATERIAL:
424
                    $courseMaterial = $descriptionTool;
425
426
                    break;
427
428
                case CCourseDescription::TYPE_RESOURCES:
429
                    $courseResources = $descriptionTool;
430
431
                    break;
432
433
                case CCourseDescription::TYPE_ASSESSMENT:
434
                    $courseAssessment = $descriptionTool;
435
436
                    break;
437
438
                case CCourseDescription::TYPE_CUSTOM:
439
                    $courseCustom[] = $descriptionTool;
440
441
                    break;
442
            }
443
        }
444
445
        $topics = [
446
            'objectives' => $courseObjectives,
447
            'topics' => $courseTopics,
448
            'methodology' => $courseMethodology,
449
            'material' => $courseMaterial,
450
            'resources' => $courseResources,
451
            'assessment' => $courseAssessment,
452
            'custom' => array_reverse($courseCustom),
453
        ];
454
455
        $subscriptionUser = false;
456
457
        if ($user) {
458
            $subscriptionUser = CourseManager::is_user_subscribed_in_course($user->getId(), $course->getCode());
459
        }
460
461
        $allowSubscribe = CourseManager::canUserSubscribeToCourse($course->getCode());
462
463
        $image = Container::getIllustrationRepository()->getIllustrationUrl($course, 'course_picture_medium');
464
465
        $params = [
466
            'course' => $course,
467
            'description' => $courseDescription,
468
            'image' => $image,
469
            'syllabus' => $topics,
470
            'tags' => $courseTags,
471
            'teachers' => $teachersData,
472
            'extra_fields' => $courseValues->getAllValuesForAnItem(
473
                $course->getId(),
474
                null,
475
                true
476
            ),
477
            'subscription' => $subscriptionUser,
478
            'url' => '',
479
            'is_premium' => '',
480
            'token' => '',
481
            'base_url' => $request->getSchemeAndHttpHost(),
482
            'allow_subscribe' => $allowSubscribe,
483
        ];
484
485
        $metaInfo = '<meta property="og:url" content="'.$urlCourse.'" />';
486
        $metaInfo .= '<meta property="og:type" content="website" />';
487
        $metaInfo .= '<meta property="og:title" content="'.$course->getTitle().'" />';
488
        $metaInfo .= '<meta property="og:description" content="'.strip_tags($courseDescription).'" />';
489
        $metaInfo .= '<meta property="og:image" content="'.$image.'" />';
490
491
        $htmlHeadXtra[] = $metaInfo;
492
        $htmlHeadXtra[] = api_get_asset('readmore-js/readmore.js');
493
494
        return $this->render('@ChamiloCore/Course/about.html.twig', $params);
495
    }
496
497
    #[Route('/{id}/welcome', name: 'chamilo_core_course_welcome')]
498
    public function welcome(Course $course): Response
499
    {
500
        return $this->render('@ChamiloCore/Course/welcome.html.twig', [
501
            'course' => $course,
502
        ]);
503
    }
504
505
    private function findIntroOfCourse(Course $course): ?CTool
506
    {
507
        $qb = $this->em->createQueryBuilder();
508
509
        $query = $qb->select('ct')
510
            ->from(CTool::class, 'ct')
511
            ->where('ct.course = :c_id')
512
            ->andWhere('ct.title = :title')
513
            ->andWhere(
514
                $qb->expr()->orX(
515
                    $qb->expr()->eq('ct.session', ':session_id'),
516
                    $qb->expr()->isNull('ct.session')
517
                )
518
            )
519
            ->setParameters([
520
                'c_id' => $course->getId(),
521
                'title' => 'course_homepage',
522
                'session_id' => 0,
523
            ])
524
            ->getQuery()
525
        ;
526
527
        $results = $query->getResult();
528
529
        return \count($results) > 0 ? $results[0] : null;
530
    }
531
532
    #[Route('/{id}/getToolIntro', name: 'chamilo_core_course_gettoolintro')]
533
    public function getToolIntro(Request $request, Course $course, EntityManagerInterface $em): Response
534
    {
535
        $sessionId = (int) $request->get('sid');
536
537
        // $session = $this->getSession();
538
        $responseData = [];
539
        $ctoolRepo = $em->getRepository(CTool::class);
540
        $sessionRepo = $em->getRepository(Session::class);
541
        $createInSession = false;
542
543
        $session = null;
544
545
        if (!empty($sessionId)) {
546
            $session = $sessionRepo->find($sessionId);
547
        }
548
549
        $ctool = $this->findIntroOfCourse($course);
550
551
        if ($session) {
552
            $ctoolSession = $ctoolRepo->findOneBy(['title' => 'course_homepage', 'course' => $course, 'session' => $session]);
553
554
            if (!$ctoolSession) {
555
                $createInSession = true;
556
            } else {
557
                $ctool = $ctoolSession;
558
            }
559
        }
560
561
        if ($ctool) {
562
            $ctoolintroRepo = $em->getRepository(CToolIntro::class);
563
564
            /** @var CToolIntro $ctoolintro */
565
            $ctoolintro = $ctoolintroRepo->findOneBy(['courseTool' => $ctool]);
566
            if ($ctoolintro) {
567
                $responseData = [
568
                    'iid' => $ctoolintro->getIid(),
569
                    'introText' => $ctoolintro->getIntroText(),
570
                    'createInSession' => $createInSession,
571
                    'cToolId' => $ctool->getIid(),
572
                ];
573
            }
574
            $responseData['c_tool'] = [
575
                'iid' => $ctool->getIid(),
576
                'title' => $ctool->getTitle(),
577
            ];
578
        }
579
580
        return new JsonResponse($responseData);
581
    }
582
583
    #[Route('/{id}/addToolIntro', name: 'chamilo_core_course_addtoolintro')]
584
    public function addToolIntro(Request $request, Course $course, EntityManagerInterface $em): Response
585
    {
586
        $data = $request->getContent();
587
        $data = json_decode($data);
588
        $ctoolintroId = $data->iid;
589
        $sessionId = $data->sid ?? 0;
590
591
        $sessionRepo = $em->getRepository(Session::class);
592
        $session = null;
593
        if (!empty($sessionId)) {
594
            $session = $sessionRepo->find($sessionId);
595
        }
596
597
        $ctool = $em->getRepository(CTool::class);
598
        $check = $ctool->findOneBy(['title' => 'course_homepage', 'course' => $course, 'session' => $session]);
599
        if (!$check) {
600
            $toolRepo = $em->getRepository(Tool::class);
601
            $toolEntity = $toolRepo->findOneBy(['title' => 'course_homepage']);
602
            $courseTool = (new CTool())
603
                ->setTool($toolEntity)
604
                ->setTitle('course_homepage')
605
                ->setCourse($course)
606
                ->setPosition(1)
607
                ->setVisibility(true)
608
                ->setParent($course)
609
                ->setCreator($course->getCreator())
610
                ->setSession($session)
611
                ->addCourseLink($course)
612
            ;
613
            $em->persist($courseTool);
614
            $em->flush();
615
            if ($courseTool && !empty($ctoolintroId)) {
616
                $ctoolintroRepo = Container::getToolIntroRepository();
617
618
                /** @var CToolIntro $ctoolintro */
619
                $ctoolintro = $ctoolintroRepo->find($ctoolintroId);
620
                $ctoolintro->setCourseTool($courseTool);
621
                $ctoolintroRepo->update($ctoolintro);
622
            }
623
        }
624
        $responseData = [];
625
        $json = $this->serializer->serialize(
626
            $responseData,
627
            'json',
628
            [
629
                'groups' => ['course:read', 'ctool:read', 'tool:read', 'cshortcut:read'],
630
            ]
631
        );
632
633
        return new JsonResponse($responseData);
634
    }
635
636
    #[Route('/check-enrollments', name: 'chamilo_core_check_enrollments', methods: ['GET'])]
637
    public function checkEnrollments(EntityManagerInterface $em, SettingsManager $settingsManager): JsonResponse
638
    {
639
        $user = $this->userHelper->getCurrent();
640
641
        if (!$user) {
642
            return new JsonResponse(['error' => 'User not found'], Response::HTTP_UNAUTHORIZED);
643
        }
644
645
        $isEnrolledInCourses = $this->isUserEnrolledInAnyCourse($user, $em);
646
        $isEnrolledInSessions = $this->isUserEnrolledInAnySession($user, $em);
647
648
        if (!$isEnrolledInCourses && !$isEnrolledInSessions) {
649
            $defaultMenuEntry = $settingsManager->getSetting('platform.default_menu_entry_for_course_or_session');
650
            $isEnrolledInCourses = 'my_courses' === $defaultMenuEntry;
651
            $isEnrolledInSessions = 'my_sessions' === $defaultMenuEntry;
652
        }
653
654
        return new JsonResponse([
655
            'isEnrolledInCourses' => $isEnrolledInCourses,
656
            'isEnrolledInSessions' => $isEnrolledInSessions,
657
        ]);
658
    }
659
660
    #[Route('/categories', name: 'chamilo_core_course_form_lists')]
661
    public function getCategories(
662
        SettingsManager $settingsManager,
663
        AccessUrlHelper $accessUrlHelper,
664
        CourseCategoryRepository $courseCategoriesRepo
665
    ): JsonResponse {
666
        $allowBaseCourseCategory = 'true' === $settingsManager->getSetting('course.allow_base_course_category');
667
        $accessUrlId = $accessUrlHelper->getCurrent()->getId();
668
669
        $categories = $courseCategoriesRepo->findAllInAccessUrl(
670
            $accessUrlId,
671
            $allowBaseCourseCategory
672
        );
673
674
        $data = [];
675
        $categoryToAvoid = '';
676
        if (!$this->isGranted('ROLE_ADMIN')) {
677
            $categoryToAvoid = $settingsManager->getSetting('course.course_category_code_to_use_as_model');
678
        }
679
680
        foreach ($categories as $category) {
681
            $categoryCode = $category->getCode();
682
            if (!empty($categoryToAvoid) && $categoryToAvoid == $categoryCode) {
683
                continue;
684
            }
685
            $data[] = ['id' => $category->getId(), 'name' => $category->__toString()];
686
        }
687
688
        return new JsonResponse($data);
689
    }
690
691
    #[Route('/search_templates', name: 'chamilo_core_course_search_templates')]
692
    public function searchCourseTemplates(
693
        Request $request,
694
        AccessUrlHelper $accessUrlHelper,
695
        CourseRepository $courseRepository
696
    ): JsonResponse {
697
        $searchTerm = $request->query->get('search', '');
698
        $accessUrl = $accessUrlHelper->getCurrent();
699
700
        $user = $this->userHelper->getCurrent();
701
702
        $courseList = $courseRepository->getCoursesInfoByUser($user, $accessUrl, 1, $searchTerm);
703
        $results = ['items' => []];
704
        foreach ($courseList as $course) {
705
            $title = $course['title'];
706
            $results['items'][] = [
707
                'id' => $course['id'],
708
                'name' => $title.' ('.$course['code'].') ',
709
            ];
710
        }
711
712
        return new JsonResponse($results);
713
    }
714
715
    #[Route('/create', name: 'chamilo_core_course_create')]
716
    public function createCourse(
717
        Request $request,
718
        TranslatorInterface $translator,
719
        CourseService $courseService
720
    ): JsonResponse {
721
        $courseData = json_decode($request->getContent(), true);
722
723
        $title = $courseData['name'] ?? null;
724
        $wantedCode = $courseData['code'] ?? null;
725
        $courseLanguage = $courseData['language']['id'] ?? null;
726
        $categoryCode = $courseData['category'] ?? null;
727
        $exemplaryContent = $courseData['fillDemoContent'] ?? false;
728
        $template = $courseData['template'] ?? '';
729
730
        $params = [
731
            'title' => $title,
732
            'wanted_code' => $wantedCode,
733
            'course_language' => $courseLanguage,
734
            'exemplary_content' => $exemplaryContent,
735
            'course_template' => $template,
736
        ];
737
738
        if ($categoryCode) {
739
            $params['course_categories'] = $categoryCode;
740
        }
741
742
        try {
743
            $course = $courseService->createCourse($params);
744
            if ($course) {
745
                return new JsonResponse([
746
                    'success' => true,
747
                    'message' => $translator->trans('Course created successfully.'),
748
                    'courseId' => $course->getId(),
749
                ]);
750
            }
751
        } catch (Exception $e) {
752
            return new JsonResponse([
753
                'success' => false,
754
                'message' => $translator->trans($e->getMessage()),
755
            ], Response::HTTP_BAD_REQUEST);
756
        }
757
758
        return new JsonResponse(['success' => false, 'message' => $translator->trans('An error occurred while creating the course.')]);
759
    }
760
761
    #[Route('/{id}/getAutoLaunchExerciseId', name: 'chamilo_core_course_get_auto_launch_exercise_id', methods: ['GET'])]
762
    public function getAutoLaunchExerciseId(
763
        Request $request,
764
        Course $course,
765
        CQuizRepository $quizRepository,
766
        EntityManagerInterface $em
767
    ): JsonResponse {
768
        $data = $request->getContent();
769
        $data = json_decode($data);
770
        $sessionId = $data->sid ?? 0;
771
772
        $sessionRepo = $em->getRepository(Session::class);
773
        $session = null;
774
        if (!empty($sessionId)) {
775
            $session = $sessionRepo->find($sessionId);
776
        }
777
778
        $autoLaunchExerciseId = $quizRepository->findAutoLaunchableQuizByCourseAndSession($course, $session);
779
780
        return new JsonResponse(['exerciseId' => $autoLaunchExerciseId], Response::HTTP_OK);
781
    }
782
783
    #[Route('/{id}/getAutoLaunchLPId', name: 'chamilo_core_course_get_auto_launch_lp_id', methods: ['GET'])]
784
    public function getAutoLaunchLPId(
785
        Request $request,
786
        Course $course,
787
        CLPRepository $lpRepository,
788
        EntityManagerInterface $em
789
    ): JsonResponse {
790
        $data = $request->getContent();
791
        $data = json_decode($data);
792
        $sessionId = $data->sid ?? 0;
793
794
        $sessionRepo = $em->getRepository(Session::class);
795
        $session = null;
796
        if (!empty($sessionId)) {
797
            $session = $sessionRepo->find($sessionId);
798
        }
799
800
        $autoLaunchLPId = $lpRepository->findAutoLaunchableLPByCourseAndSession($course, $session);
801
802
        return new JsonResponse(['lpId' => $autoLaunchLPId], Response::HTTP_OK);
803
    }
804
805
    private function autoLaunch(): void
806
    {
807
        $autoLaunchWarning = '';
808
        $showAutoLaunchLpWarning = false;
809
        $course_id = api_get_course_int_id();
810
        $lpAutoLaunch = api_get_course_setting('enable_lp_auto_launch');
811
        $session_id = api_get_session_id();
812
        $allowAutoLaunchForCourseAdmins =
813
            api_is_platform_admin()
814
            || api_is_allowed_to_edit(true, true)
815
            || api_is_coach();
816
817
        if (!empty($lpAutoLaunch)) {
818
            if (2 === $lpAutoLaunch) {
819
                // LP list
820
                if ($allowAutoLaunchForCourseAdmins) {
821
                    $showAutoLaunchLpWarning = true;
822
                } else {
823
                    $session_key = 'lp_autolaunch_'.$session_id.'_'.$course_id.'_'.api_get_user_id();
824
                    if (!isset($_SESSION[$session_key])) {
825
                        // Redirecting to the LP
826
                        $url = api_get_path(WEB_CODE_PATH).'lp/lp_controller.php?'.api_get_cidreq();
827
                        $_SESSION[$session_key] = true;
828
                        header(\sprintf('Location: %s', $url));
829
830
                        exit;
831
                    }
832
                }
833
            } else {
834
                $lp_table = Database::get_course_table(TABLE_LP_MAIN);
835
                $condition = '';
836
                if (!empty($session_id)) {
837
                    $condition = api_get_session_condition($session_id);
838
                    $sql = "SELECT id FROM {$lp_table}
839
                            WHERE c_id = {$course_id} AND autolaunch = 1 {$condition}
840
                            LIMIT 1";
841
                    $result = Database::query($sql);
842
                    // If we found nothing in the session we just called the session_id =  0 autolaunch
843
                    if (0 === Database::num_rows($result)) {
844
                        $condition = '';
845
                    }
846
                }
847
848
                $sql = "SELECT iid FROM {$lp_table}
849
                        WHERE c_id = {$course_id} AND autolaunch = 1 {$condition}
850
                        LIMIT 1";
851
                $result = Database::query($sql);
852
                if (Database::num_rows($result) > 0) {
853
                    $lp_data = Database::fetch_array($result);
854
                    if (!empty($lp_data['iid'])) {
855
                        if ($allowAutoLaunchForCourseAdmins) {
856
                            $showAutoLaunchLpWarning = true;
857
                        } else {
858
                            $session_key = 'lp_autolaunch_'.$session_id.'_'.api_get_course_int_id().'_'.api_get_user_id();
859
                            if (!isset($_SESSION[$session_key])) {
860
                                // Redirecting to the LP
861
                                $url = api_get_path(WEB_CODE_PATH).
862
                                    'lp/lp_controller.php?'.api_get_cidreq().'&action=view&lp_id='.$lp_data['iid'];
863
864
                                $_SESSION[$session_key] = true;
865
                                header(\sprintf('Location: %s', $url));
866
867
                                exit;
868
                            }
869
                        }
870
                    }
871
                }
872
            }
873
        }
874
875
        if ($showAutoLaunchLpWarning) {
876
            $autoLaunchWarning = get_lang(
877
                'The learning path auto-launch setting is ON. When learners enter this course, they will be automatically redirected to the learning path marked as auto-launch.'
878
            );
879
        }
880
881
        $forumAutoLaunch = (int) api_get_course_setting('enable_forum_auto_launch');
882
        if (1 === $forumAutoLaunch) {
883
            if ($allowAutoLaunchForCourseAdmins) {
884
                if (empty($autoLaunchWarning)) {
885
                    $autoLaunchWarning = get_lang(
886
                        "The forum's auto-launch setting is on. Students will be redirected to the forum tool when entering this course."
887
                    );
888
                }
889
            } else {
890
                $url = api_get_path(WEB_CODE_PATH).'forum/index.php?'.api_get_cidreq();
891
                header(\sprintf('Location: %s', $url));
892
893
                exit;
894
            }
895
        }
896
897
        if ('true' === api_get_setting('exercise.allow_exercise_auto_launch')) {
898
            $exerciseAutoLaunch = (int) api_get_course_setting('enable_exercise_auto_launch');
899
            if (2 === $exerciseAutoLaunch) {
900
                if ($allowAutoLaunchForCourseAdmins) {
901
                    if (empty($autoLaunchWarning)) {
902
                        $autoLaunchWarning = get_lang(
903
                            'TheExerciseAutoLaunchSettingIsONStudentsWillBeRedirectToTheExerciseList'
904
                        );
905
                    }
906
                } else {
907
                    // Redirecting to the document
908
                    $url = api_get_path(WEB_CODE_PATH).'exercise/exercise.php?'.api_get_cidreq();
909
                    header(\sprintf('Location: %s', $url));
910
911
                    exit;
912
                }
913
            } elseif (1 === $exerciseAutoLaunch) {
914
                if ($allowAutoLaunchForCourseAdmins) {
915
                    if (empty($autoLaunchWarning)) {
916
                        $autoLaunchWarning = get_lang(
917
                            'TheExerciseAutoLaunchSettingIsONStudentsWillBeRedirectToAnSpecificExercise'
918
                        );
919
                    }
920
                } else {
921
                    // Redirecting to an exercise
922
                    $table = Database::get_course_table(TABLE_QUIZ_TEST);
923
                    $condition = '';
924
                    if (!empty($session_id)) {
925
                        $condition = api_get_session_condition($session_id);
926
                        $sql = "SELECT iid FROM {$table}
927
                                WHERE c_id = {$course_id} AND autolaunch = 1 {$condition}
928
                                LIMIT 1";
929
                        $result = Database::query($sql);
930
                        // If we found nothing in the session we just called the session_id = 0 autolaunch
931
                        if (0 === Database::num_rows($result)) {
932
                            $condition = '';
933
                        }
934
                    }
935
936
                    $sql = "SELECT iid FROM {$table}
937
                            WHERE c_id = {$course_id} AND autolaunch = 1 {$condition}
938
                            LIMIT 1";
939
                    $result = Database::query($sql);
940
                    if (Database::num_rows($result) > 0) {
941
                        $row = Database::fetch_array($result);
942
                        $exerciseId = $row['iid'];
943
                        $url = api_get_path(WEB_CODE_PATH).
944
                            'exercise/overview.php?exerciseId='.$exerciseId.'&'.api_get_cidreq();
945
                        header(\sprintf('Location: %s', $url));
946
947
                        exit;
948
                    }
949
                }
950
            }
951
        }
952
953
        $documentAutoLaunch = (int) api_get_course_setting('enable_document_auto_launch');
954
        if (1 === $documentAutoLaunch) {
955
            if ($allowAutoLaunchForCourseAdmins) {
956
                if (empty($autoLaunchWarning)) {
957
                    $autoLaunchWarning = get_lang(
958
                        'The document auto-launch feature configuration is enabled. Learners will be automatically redirected to document tool.'
959
                    );
960
                }
961
            } else {
962
                // Redirecting to the document
963
                $url = api_get_path(WEB_CODE_PATH).'document/document.php?'.api_get_cidreq();
964
                header("Location: $url");
965
966
                exit;
967
            }
968
        }
969
970
        /*  SWITCH TO A DIFFERENT HOMEPAGE VIEW
971
         the setting homepage_view is adjustable through
972
         the platform administration section */
973
        if (!empty($autoLaunchWarning)) {
974
            $this->addFlash(
975
                'warning',
976
                Display::return_message(
977
                    $autoLaunchWarning,
978
                    'warning'
979
                )
980
            );
981
        }
982
    }
983
984
    // Implement the real logic to check course enrollment
985
    private function isUserEnrolledInAnyCourse(User $user, EntityManagerInterface $em): bool
986
    {
987
        $enrollmentCount = $em
988
            ->getRepository(CourseRelUser::class)
989
            ->count(['user' => $user])
990
        ;
991
992
        return $enrollmentCount > 0;
993
    }
994
995
    // Implement the real logic to check session enrollment
996
    private function isUserEnrolledInAnySession(User $user, EntityManagerInterface $em): bool
997
    {
998
        $enrollmentCount = $em->getRepository(SessionRelUser::class)
999
            ->count(['user' => $user])
1000
        ;
1001
1002
        return $enrollmentCount > 0;
1003
    }
1004
}
1005