Passed
Push — master ( 323135...9f183a )
by Julito
13:17
created

HomeController::hideIconAction()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 17
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 11
nc 3
nop 1
dl 0
loc 17
rs 9.9
c 0
b 0
f 0
1
<?php
2
/* For licensing terms, see /license.txt */
3
4
namespace Chamilo\CourseBundle\Controller\Home;
5
6
use Chamilo\CourseBundle\Controller\ToolBaseController;
7
use Chamilo\CourseBundle\Entity\CTool;
8
use CourseHome;
9
use Display;
10
use Symfony\Component\HttpFoundation\Request;
11
use Symfony\Component\HttpFoundation\Response;
12
use Symfony\Component\Routing\Annotation\Route;
13
14
/**
15
 * Class HomeController.
16
 *
17
 * @author Julio Montoya <[email protected]>
18
 *
19
 * @Route("/")
20
 */
21
class HomeController extends ToolBaseController
22
{
23
    /**
24
     * @Route("/", name="course_home")
25
     * @Route("/index.php", methods={"GET"})
26
     *
27
     * @param Request $request
28
     *
29
     * @return Response
30
     */
31
    public function indexAction(Request $request)
32
    {
33
        $sessionId = api_get_session_id();
34
        $course = $this->getCourse();
35
        $courseCode = $course->getId();
36
        $result = $this->autoLaunch();
37
38
        $showAutoLaunchLpWarning = $result['show_autolaunch_lp_warning'];
39
        $showAutoLaunchExerciseWarning = $result['show_autolaunch_exercise_warning'];
40
41
        if ($showAutoLaunchLpWarning) {
42
            $this->addFlash(
43
                'warning',
44
                $this->trans('TheLPAutoLaunchSettingIsONStudentsWillBeRedirectToAnSpecificLP')
45
            );
46
        }
47
48
        if ($showAutoLaunchExerciseWarning) {
49
            $this->addFlash(
50
                'warning',
51
                $this->trans('TheExerciseAutoLaunchSettingIsONStudentsWillBeRedirectToAnSpecificExercise')
52
            );
53
        }
54
55
        if (true) {
56
            $editIcons = Display::url(
57
                Display::return_icon('edit.png'),
58
                $this->generateUrl(
59
                    'chamilo_course_home_home_iconlist',
60
                    [
61
                        'course' => api_get_course_id(),
62
                    ]
63
                )
64
            );
65
        }
66
67
        $isSpecialCourse = \CourseManager::isSpecialCourse($courseCode);
68
69
        if ($isSpecialCourse) {
70
            $user = $this->getUser();
71
            if (!empty($user)) {
72
                $userId = $this->getUser()->getId();
73
                $autoreg = $request->get('autoreg');
74
                if ($autoreg == 1) {
75
                    \CourseManager::subscribeUser(
76
                        $userId,
77
                        $courseCode,
78
                        STUDENT
79
                    );
80
                }
81
            }
82
        }
83
84
        //$homeView = api_get_setting('course.homepage_view');
85
        $blocks = $this->renderActivityView();
86
87
        $toolList = $result['tool_list'];
88
89
        $introduction = Display::return_introduction_section(
90
            TOOL_COURSE_HOMEPAGE,
91
            $toolList
92
        );
93
94
        $sessionInfo = null;
95
        if (api_get_setting('session.show_session_data') == 'true' && $sessionId) {
96
            $sessionInfo = CourseHome::show_session_data($sessionId);
97
        }
98
99
        return $this->render(
100
            'ChamiloCourseBundle:Home:index.html.twig',
101
            [
102
                'course' => $course,
103
                'session_info' => $sessionInfo,
104
                'icons' => $result['content'],
105
                'blocks' => $blocks,
106
                'edit_icons' => $editIcons,
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $editIcons does not seem to be defined for all execution paths leading up to this point.
Loading history...
107
                'introduction_text' => $introduction,
108
                'exercise_warning' => null,
109
                'lp_warning' => null,
110
            ]
111
        );
112
    }
113
114
    /**
115
     * @Route("/show/{iconId}", methods={"GET"})
116
     *
117
     * @param $iconId
118
     *
119
     * @return string|null
120
     */
121
    public function showIconAction($iconId)
122
    {
123
        $entityManager = $this->getDoctrine()->getManager();
124
        $criteria = ['cId' => api_get_course_int_id(), 'id' => $iconId];
125
        $tool = $this->getRepository(
126
            'Chamilo\CourseBundle\Entity\CTool'
127
        )->findOneBy($criteria);
128
        if ($tool) {
129
            $tool->setVisibility(1);
130
        }
131
        $entityManager->persist($tool);
132
        //$entityManager->flush();
133
        return Display::return_message(get_lang('Visible'), 'confirmation');
134
    }
135
136
    /**
137
     * @Route("/hide/{iconId}", methods={"GET"})
138
     *
139
     * @param $iconId
140
     *
141
     * @return string|null
142
     */
143
    public function hideIconAction($iconId)
144
    {
145
        if (!$this->isCourseTeacher()) {
146
            return $this->abort(404);
147
        }
148
149
        $entityManager = $this->getDoctrine()->getManager();
150
        $criteria = ['cId' => api_get_course_int_id(), 'id' => $iconId];
151
        $tool = $this->getRepository(
152
            'Chamilo\CourseBundle\Entity\CTool'
153
        )->findOneBy($criteria);
154
        if ($tool) {
155
            $tool->setVisibility(0);
156
        }
157
        $entityManager->persist($tool);
158
        //$entityManager->flush();
159
        return Display::return_message(get_lang('The tool is now invisible.'), 'confirmation');
160
    }
161
162
    /**
163
     * @Route("/delete/{iconId}", methods={"GET"})
164
     *
165
     * @param $iconId
166
     *
167
     * @return string|null
168
     */
169
    public function deleteIcon($iconId)
170
    {
171
        if (!$this->isCourseTeacher()) {
172
            return $this->abort(404);
173
        }
174
175
        $entityManager = $this->getDoctrine()->getManager();
176
        $criteria = ['cId' => api_get_course_int_id(), 'id' => $iconId, 'added_tool' => 1];
177
        $tool = $this->getRepository(
178
            'Chamilo\CourseBundle\Entity\CTool'
179
        )->findOneBy($criteria);
180
        $entityManager->remove($tool);
181
        //$entityManager->flush();
182
        return Display::return_message(get_lang('Deleted'), 'confirmation');
183
    }
184
185
    /**
186
     * @Route("/icon_list", methods={"GET"})
187
     *
188
     * @param Request $request
189
     */
190
    public function iconListAction(Request $request)
191
    {
192
        $em = $this->getDoctrine()->getManager();
193
        $repo = $this->getDoctrine()->getRepository('ChamiloCourseBundle:CTool');
194
195
        $sessionId = intval($request->get('id_session'));
196
        $itemsFromSession = [];
197
        if (!empty($sessionId)) {
198
            $query = $repo->createQueryBuilder('a');
199
            $query->select('s');
200
            $query->from('Chamilo\CourseBundle\Entity\CTool', 's');
201
            $query->where('s.cId  = :courseId AND s.sessionId = :sessionId')
202
                ->setParameters(
203
                    [
204
                        'course' => $this->getCourse()->getId(),
205
                        'sessionId' => $sessionId,
206
                    ]
207
                );
208
            $itemsFromSession = $query->getQuery()->getResult();
209
210
            $itemNameList = [];
211
            foreach ($itemsFromSession as $item) {
212
                $itemNameList[] = $item->getName();
213
            }
214
215
            //$itemsFromSession = $this->getRepository()->findBy($criteria);
216
            $query = $repo->createQueryBuilder('a');
217
            $query->select('s');
218
            $query->from('Chamilo\CourseBundle\Entity\CTool', 's');
219
            $query->where('s.cId  = :courseId AND s.sessionId = 0')
220
                ->setParameters(
221
                    [
222
                        'courseId' => $this->getCourse()->getId(),
223
                    ]
224
                );
225
            if (!empty($itemNameList)) {
226
                $query->andWhere($query->expr()->notIn('s.name', $itemNameList));
227
            }
228
            $itemsFromCourse = $query->getQuery()->getResult();
229
        } else {
230
            $criteria = ['cId' => $this->getCourse()->getId(), 'sessionId' => 0];
231
            $itemsFromCourse = $repo->findBy($criteria);
232
        }
233
234
        return $this->render(
235
            '@ChamiloCourse/Home/list.html.twig',
236
            [
237
                'items_from_course' => $itemsFromCourse,
238
                'items_from_session' => $itemsFromSession,
239
                'links' => '',
240
            ]
241
        );
242
    }
243
244
    /**
245
     * @Route("/{itemName}/add", methods={"GET", "POST"})
246
     *
247
     * @param $itemName
248
     *
249
     * @return mixed
250
     */
251
    public function addIconAction($itemName)
252
    {
253
        if (!$this->isCourseTeacher()) {
254
            return $this->abort(404);
255
        }
256
257
        $sessionId = intval($this->getRequest()->get('id_session'));
258
259
        if (empty($sessionId)) {
260
            return $this->abort(500);
261
        }
262
263
        $criteria = ['cId' => $this->getCourse()->getId(), 'sessionId' => 0, 'name' => $itemName];
264
        $itemFromDatabase = $this->getRepository()->findOneBy($criteria);
265
266
        if (!$itemFromDatabase) {
267
            $this->createNotFoundException();
268
        }
269
        /** @var CTool $item */
270
        $item = clone $itemFromDatabase;
271
        $item->setId(null);
272
        $item->setSessionId($sessionId);
273
        $form = $this->createForm($this->getFormType(), $item);
274
275
        $form->handleRequest($this->getRequest());
276
277
        if ($form->isValid()) {
278
            $query = $this->getDoctrine()->getManager()->createQueryBuilder('a');
279
            $query->select('MAX(s.id) as id');
280
            $query->from('Chamilo\CourseBundle\Entity\CTool', 's');
281
            $query->where('s.cId  = :courseId')->setParameter('courseId', $this->getCourse()->getId());
282
            $result = $query->getQuery()->getArrayResult();
283
            $maxId = $result[0]['id'] + 1;
284
            $item->setId($maxId);
285
286
            $entityManager = $this->getDoctrine()->getManager();
287
            $entityManager->persist($item);
288
            $entityManager->flush();
289
            $customIcon = $item->getCustomIcon();
290
            if (!empty($customIcon)) {
291
                $item->createGrayIcon($this->get('imagine'));
0 ignored issues
show
Bug introduced by
The method createGrayIcon() does not exist on Chamilo\CourseBundle\Entity\CTool. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

291
                $item->/** @scrutinizer ignore-call */ 
292
                       createGrayIcon($this->get('imagine'));

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
292
            }
293
294
            $this->get('session')->getFlashBag()->add('success', "Added");
295
            $url = $this->generateUrl('course_home.controller:iconListAction', ['id_session' => $sessionId]);
296
297
            return $this->redirect($url);
298
        }
299
300
        $this->getTemplate()->assign('item', $item);
301
        $this->getTemplate()->assign('form', $form->createView());
302
        $this->getTemplate()->assign('links', $this->generateLinks());
303
304
        return $this->render('@ChamiloCourse/Home/add.html.twig');
305
    }
306
307
    /**
308
     * @Route("/{itemId}/edit", methods={"GET"})
309
     */
310
    public function editIconAction($itemId)
311
    {
312
        if (!$this->isCourseTeacher()) {
313
            return $this->abort(404);
314
        }
315
316
        $sessionId = intval($this->getRequest()->get('id_session'));
317
318
        $criteria = ['cId' => $this->getCourse()->getId(), 'id' => $itemId];
319
        /** @var CTool $item */
320
        $item = $this->getRepository()->findOneBy($criteria);
321
322
        $form = $this->createForm($this->getFormType(), $item);
323
        $form->handleRequest($this->getRequest());
324
325
        if ($form->isValid()) {
326
            $entityManager = $this->getDoctrine()->getManager();
327
            $entityManager->persist($item);
328
            $entityManager->flush();
329
330
            $customIcon = $item->getCustomIcon();
331
            if (!empty($customIcon)) {
332
                $item->createGrayIcon($this->get('imagine'));
333
            }
334
335
            $this->get('session')->getFlashBag()->add('success', "Updated");
336
            $url = $this->generateUrl('course_home.controller:iconListAction', ['id_session' => $sessionId]);
337
338
            return $this->redirect($url);
339
        }
340
341
        $this->getTemplate()->assign('item', $item);
342
        $this->getTemplate()->assign('form', $form->createView());
343
        $this->getTemplate()->assign('links', $this->generateLinks());
344
345
        return $this->render('@ChamiloCourse/Home/edit.html.twig');
346
    }
347
348
    /**
349
     * @Route("/{itemId}/delete", methods={"GET"})
350
     */
351
    public function deleteIconAction($itemId)
352
    {
353
        if (!$this->isCourseTeacher()) {
354
            return $this->abort(404);
355
        }
356
357
        $criteria = ['cId' => $this->getCourse()->getId(), 'id' => $itemId];
358
359
        /** @var CTool $item */
360
        $item = $this->getRepository()->findOneBy($criteria);
361
        $entityManager = $this->getDoctrine()->getManager();
362
        $sessionId = $item->getSessionId();
363
        if (!empty($sessionId)) {
364
            $entityManager->remove($item);
365
        } else {
366
            $item->setCustomIcon(null);
367
            $entityManager->persist($item);
368
        }
369
        $entityManager->flush();
370
        $this->get('session')->getFlashBag()->add('success', "Deleted");
371
372
        $this->getTemplate()->assign('links', $this->generateLinks());
373
        $url = $this->generateUrl('course_home.controller:iconListAction');
374
375
        return $this->redirect($url);
376
    }
377
378
    /**
379
     * @param string $title
380
     * @param string $content
381
     * @param string $class
382
     *
383
     * @return string
384
     */
385
    private function return_block($title, $content, $class = null)
0 ignored issues
show
Unused Code introduced by
The method return_block() is not used, and could be removed.

This check looks for private methods that have been defined, but are not used inside the class.

Loading history...
386
    {
387
        $html = '<div class="row">
388
                <div class="col-xs-12 col-md-12">
389
                    <div class="title-tools">'.$title.'</div>
390
                </div>
391
            </div>
392
            <div class="row '.$class.'">'.$content.'</div>';
393
394
        return $html;
395
    }
396
397
    /**
398
     * @return array
399
     */
400
    private function renderActivityView()
401
    {
402
        $session_id = api_get_session_id();
403
        $urlGenerator = $this->get('router');
404
        $content = '';
405
406
        $enabled = api_get_plugin_setting('courselegal', 'tool_enable');
407
        $pluginExtra = null;
408
        if ($enabled === 'true') {
409
            /*require_once api_get_path(SYS_PLUGIN_PATH).'courselegal/config.php';
410
            $plugin = CourseLegalPlugin::create();
411
            $pluginExtra = $plugin->getTeacherLink();*/
412
        }
413
414
        // Start of tools for CourseAdmins (teachers/tutors)
415
        $totalList = [];
416
417
        // Start of tools for CourseAdmins (teachers/tutors)
418
        if ($session_id === 0 && api_is_course_admin() && api_is_allowed_to_edit(null, true)) {
419
            $content .= '<div class="alert alert-success" style="border:0px; margin-top: 0px;padding:0px;">
420
                <div class="normal-message" id="id_normal_message" style="display:none">';
421
            $content .= '<img src="'.api_get_path(WEB_PATH).'main/inc/lib/javascript/indicator.gif"/>&nbsp;&nbsp;';
422
            $content .= get_lang('Please stand by...');
423
            $content .= '</div>
424
                <div class="alert alert-success" id="id_confirmation_message" style="display:none"></div>
425
            </div>';
426
427
            $content .= $pluginExtra;
428
429
            if (api_get_setting('show_session_data') == 'true' && $session_id > 0) {
430
                $content .= '
431
                <div class="row">
432
                    <div class="col-xs-12 col-md-12">
433
                        <span class="viewcaption">'.get_lang('Session\'s data').'</span>
434
                        <table class="course_activity_home">'.
435
                            CourseHome::show_session_data($session_id).'
436
                        </table>
437
                    </div>
438
                </div>';
439
            }
440
441
            $my_list = CourseHome::get_tools_category(TOOL_AUTHORING);
442
443
            $blocks[] = [
0 ignored issues
show
Comprehensibility Best Practice introduced by
$blocks was never initialized. Although not strictly required by PHP, it is generally a good practice to add $blocks = array(); before regardless.
Loading history...
444
                'title' => get_lang('Authoring'),
445
                'class' => 'course-tools-author',
446
                'content' => CourseHome::show_tools_category($my_list),
447
            ];
448
449
            $list1 = CourseHome::get_tools_category(TOOL_INTERACTION);
450
            $list2 = CourseHome::get_tools_category(TOOL_COURSE_PLUGIN);
451
            $my_list = array_merge($list1, $list2);
452
453
            $blocks[] = [
454
                'title' => get_lang('Interaction'),
455
                'class' => 'course-tools-interaction',
456
                'content' => CourseHome::show_tools_category($my_list),
457
            ];
458
459
            $my_list = CourseHome::get_tools_category(TOOL_ADMIN_PLATFORM);
460
461
            $blocks[] = [
462
                'title' => get_lang('Administration'),
463
                'class' => 'course-tools-administration',
464
                'content' => CourseHome::show_tools_category($my_list),
465
            ];
466
        } elseif (api_is_coach()) {
467
            $content .= $pluginExtra;
468
            if (api_get_setting('show_session_data') === 'true' && $session_id > 0) {
469
                $content .= '<div class="row">
470
                    <div class="col-xs-12 col-md-12">
471
                    <span class="viewcaption">'.get_lang('Session\'s data').'</span>
472
                    <table class="course_activity_home">';
473
                $content .= CourseHome::show_session_data($session_id);
474
                $content .= '</table></div></div>';
475
            }
476
477
            $my_list = CourseHome::get_tools_category(TOOL_STUDENT_VIEW);
478
479
            $blocks[] = [
480
                'content' => CourseHome::show_tools_category($my_list),
481
            ];
482
483
            $sessionsCopy = api_get_setting('allow_session_course_copy_for_teachers');
484
            if ($sessionsCopy === 'true') {
485
                // Adding only maintenance for coaches.
486
                $myList = CourseHome::get_tools_category(TOOL_ADMIN_PLATFORM);
487
                $onlyMaintenanceList = [];
488
489
                foreach ($myList as $item) {
490
                    if ($item['name'] === 'course_maintenance') {
491
                        $item['link'] = 'course_info/maintenance_coach.php';
492
493
                        $onlyMaintenanceList[] = $item;
494
                    }
495
                }
496
497
                $blocks[] = [
498
                    'title' => get_lang('Administration'),
499
                    'content' => CourseHome::show_tools_category($onlyMaintenanceList),
500
                ];
501
            }
502
        } else {
503
            $tools = CourseHome::get_tools_category(TOOL_STUDENT_VIEW);
504
505
            $isDrhOfCourse = \CourseManager::isUserSubscribedInCourseAsDrh(
506
                api_get_user_id(),
507
                api_get_course_info()
508
            );
509
510
            // Force user icon for DRH
511
            if ($isDrhOfCourse) {
512
                $addUserTool = true;
513
                foreach ($tools as $tool) {
514
                    if ($tool['name'] === 'user') {
515
                        $addUserTool = false;
516
                        break;
517
                    }
518
                }
519
520
                if ($addUserTool) {
521
                    $tools[] = [
522
                        'c_id' => api_get_course_int_id(),
523
                        'name' => 'user',
524
                        'link' => 'user/user.php',
525
                        'image' => 'members.gif',
526
                        'visibility' => '1',
527
                        'admin' => '0',
528
                        'address' => 'squaregrey.gif',
529
                        'added_tool' => '0',
530
                        'target' => '_self',
531
                        'category' => 'interaction',
532
                        'session_id' => api_get_session_id(),
533
                    ];
534
                }
535
            }
536
537
            if (count($tools) > 0) {
538
                $blocks[] = ['content' => CourseHome::show_tools_category($tools)];
539
            }
540
541
            if ($isDrhOfCourse) {
542
                $drhTool = CourseHome::get_tools_category(TOOL_DRH);
543
                $blocks[] = ['content' => CourseHome::show_tools_category($drhTool)];
544
            }
545
        }
546
547
        return $blocks;
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $blocks does not seem to be defined for all execution paths leading up to this point.
Loading history...
548
    }
549
550
    /**
551
     * @return array
552
     */
553
    private function autoLaunch()
554
    {
555
        return;
556
        $showAutoLaunchExerciseWarning = false;
0 ignored issues
show
Unused Code introduced by
$showAutoLaunchExerciseWarning = false 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...
557
558
        // Exercise auto-launch
559
        $auto_launch = api_get_course_setting('enable_exercise_auto_launch');
560
561
        if (!empty($auto_launch)) {
562
            $session_id = api_get_session_id();
563
            //Exercise list
564
            if ($auto_launch == 2) {
565
                if (api_is_platform_admin() || api_is_allowed_to_edit()) {
566
                    $showAutoLaunchExerciseWarning = true;
567
                } else {
568
                    $session_key = 'exercise_autolunch_'.$session_id.'_'.api_get_course_int_id().'_'.api_get_user_id();
569
                    $sessionData = Session::read($session_key);
570
                    if (!isset($sessionData)) {
571
                        //redirecting to the Exercise
572
                        $url = api_get_path(WEB_CODE_PATH).'exercise/exercise.php?'.api_get_cidreq().'&id_session='.$session_id;
573
                        $_SESSION[$session_key] = true;
574
575
                        header("Location: $url");
576
                        exit;
577
                    }
578
                }
579
            } else {
580
                $table = \Database::get_course_table(TABLE_QUIZ_TEST);
581
                $course_id = api_get_course_int_id();
582
                $condition = '';
583
                if (!empty($session_id)) {
584
                    $condition = api_get_session_condition($session_id);
585
                    $sql = "SELECT iid FROM $table
586
                            WHERE c_id = $course_id AND autolaunch = 1 $condition
587
                            LIMIT 1";
588
                    $result = \Database::query($sql);
589
                    //If we found nothing in the session we just called the session_id =  0 autolaunch
590
                    if (\Database::num_rows($result) == 0) {
591
                        $condition = '';
592
                    } else {
593
                        //great, there is an specific auto lunch for this session we leave the $condition
594
                    }
595
                }
596
597
                $sql = "SELECT iid FROM $table
598
                        WHERE c_id = $course_id AND autolaunch = 1 $condition
599
                        LIMIT 1";
600
                $result = \Database::query($sql);
601
                if (\Database::num_rows($result) > 0) {
602
                    $data = \Database::fetch_array($result, 'ASSOC');
603
                    if (!empty($data['iid'])) {
604
                        if (api_is_platform_admin() || api_is_allowed_to_edit()) {
605
                            $showAutoLaunchExerciseWarning = true;
606
                        } else {
607
                            $session_key = 'exercise_autolunch_'.$session_id.'_'.api_get_course_int_id().'_'.api_get_user_id();
608
                            if (!isset($_SESSION[$session_key])) {
609
                                //redirecting to the LP
610
                                $url = api_get_path(WEB_CODE_PATH).'exercise/overview.php?'.api_get_cidreq().'&exerciseId='.$data['iid'];
611
612
                                $_SESSION[$session_key] = true;
613
                                header("Location: $url");
614
                                exit;
615
                            }
616
                        }
617
                    }
618
                }
619
            }
620
        }
621
622
        /* Auto launch code */
623
        $showAutoLaunchLpWarning = false;
624
        $auto_launch = api_get_course_setting('enable_lp_auto_launch');
625
        if (!empty($auto_launch)) {
626
            $session_id = api_get_session_id();
627
            //LP list
628
            if ($auto_launch == 2) {
629
                if (api_is_platform_admin() || api_is_allowed_to_edit()) {
630
                    $showAutoLaunchLpWarning = true;
631
                } else {
632
                    $session_key = 'lp_autolunch_'.$session_id.'_'.api_get_course_int_id().'_'.api_get_user_id();
633
                    if (!isset($_SESSION[$session_key])) {
634
                        //redirecting to the LP
635
                        $url = api_get_path(WEB_CODE_PATH).'lp/lp_controller.php?'.api_get_cidreq().'&id_session='.$session_id;
636
                        $_SESSION[$session_key] = true;
637
                        header("Location: $url");
638
                        exit;
639
                    }
640
                }
641
            } else {
642
                $lp_table = \Database::get_course_table(TABLE_LP_MAIN);
643
                $course_id = api_get_course_int_id();
644
                $condition = '';
645
                if (!empty($session_id)) {
646
                    $condition = api_get_session_condition($session_id);
647
                    $sql = "SELECT id FROM $lp_table WHERE c_id = $course_id AND autolunch = 1 $condition LIMIT 1";
648
                    $result = \Database::query($sql);
649
                    //If we found nothing in the session we just called the session_id =  0 autolunch
650
                    if (\Database::num_rows($result) == 0) {
651
                        $condition = '';
652
                    } else {
653
                        //great, there is an specific auto lunch for this session we leave the $condition
654
                    }
655
                }
656
657
                $sql = "SELECT id FROM $lp_table
658
                        WHERE c_id = $course_id AND autolunch = 1 $condition
659
                        LIMIT 1";
660
                $result = \Database::query($sql);
661
                if (\Database::num_rows($result) > 0) {
662
                    $lp_data = \Database::fetch_array($result, 'ASSOC');
663
                    if (!empty($lp_data['id'])) {
664
                        if (api_is_platform_admin() || api_is_allowed_to_edit()) {
665
                            $showAutoLaunchLpWarning = true;
666
                        } else {
667
                            $session_key = 'lp_autolunch_'.$session_id.'_'.api_get_course_int_id().'_'.api_get_user_id();
668
                            if (!isset($_SESSION[$session_key])) {
669
                                //redirecting to the LP
670
                                $url = api_get_path(WEB_CODE_PATH).'lp/lp_controller.php?'.api_get_cidreq().'&action=view&lp_id='.$lp_data['id'];
671
672
                                $_SESSION[$session_key] = true;
673
                                header("Location: $url");
674
                                exit;
675
                            }
676
                        }
677
                    }
678
                }
679
            }
680
        }
681
682
        return [
683
            'show_autolaunch_exercise_warning' => $showAutoLaunchExerciseWarning,
684
            'show_autolaunch_lp_warning' => $showAutoLaunchLpWarning,
685
        ];
686
    }
687
}
688