Issues (2126)

plugin/ims_lti/multiply.php (1 issue)

1
<?php
2
/* For licensing terms, see /license.txt */
3
4
use Chamilo\PluginBundle\Entity\ImsLti\ImsLtiTool;
5
use Symfony\Component\HttpFoundation\Request;
0 ignored issues
show
This use statement conflicts with another class in this namespace, Request. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
6
7
$cidReset = true;
8
9
require_once __DIR__.'/../../main/inc/global.inc.php';
10
11
api_protect_admin_script();
12
13
$plugin = ImsLtiPlugin::create();
14
$webPluginPath = api_get_path(WEB_PLUGIN_PATH).'ims_lti/';
15
16
$em = Database::getManager();
17
18
try {
19
    if ($plugin->get('enabled') !== 'true') {
20
        throw new Exception(get_lang('NotAllowed'));
21
    }
22
23
    $request = Request::createFromGlobals();
24
    /** @var ImsLtiTool $tool */
25
    $tool = $em->find('ChamiloPluginBundle:ImsLti\ImsLtiTool', $request->query->getInt('id'));
26
27
    if (!$tool) {
28
        throw new Exception($plugin->get_lang('NoTool'));
29
    }
30
31
    if ($tool->getParent()) {
32
        throw new Exception($plugin->get_lang('NoAllowed'));
33
    }
34
35
    $content = '';
36
37
    $courses = ImsLtiPlugin::getCoursesForParentTool($tool);
38
39
    $slctCourses = [];
40
41
    /** @var \Chamilo\CoreBundle\Entity\Course $course */
42
    foreach ($courses as $course) {
43
        $slctCourses[$course->getId()] = $course->getName();
44
    }
45
46
    $selectedCoursesIds = array_keys($slctCourses);
47
48
    $form = new FormValidator('frm_multiply', 'post', api_get_self().'?id='.$tool->getId());
49
    $form->addLabel($plugin->get_lang('Tool'), $tool->getName());
50
    $form->addSelectAjax(
51
        'courses',
52
        get_lang('Courses'),
53
        $slctCourses,
54
        ['url' => api_get_path(WEB_AJAX_PATH).'course.ajax.php?a=search_course', 'multiple' => true]
55
    );
56
    $form->addCheckBox('all_courses', '', $plugin->get_lang('AddInAllCourses'));
57
    $form->addCheckBox('tool_visible', get_lang('SetVisible'), get_lang('ToolIsNowVisible'));
58
    $form->addButtonExport(get_lang('Save'));
59
60
    if ($form->validate()) {
61
        $em = Database::getManager();
62
        $formValues = $form->exportValues();
63
        $formValues['courses'] = empty($formValues['courses']) ? [] : $formValues['courses'];
64
        $formValues['tool_visible'] = !empty($formValues['tool_visible']);
65
66
        if (!empty($formValues['all_courses'])) {
67
            $courseList = Database::select('id', Database::get_main_table(TABLE_MAIN_COURSE));
68
            $formValues['courses'] = array_keys($courseList);
69
        }
70
71
        $courseIdsToDelete = array_diff($selectedCoursesIds, $formValues['courses']);
72
        $newSelectedCourseIds = array_diff($formValues['courses'], $selectedCoursesIds);
73
74
        if ($courseIdsToDelete) {
75
            $toolLinks = [];
76
77
            /** @var ImsLtiTool $childInCourse */
78
            foreach ($tool->getChildrenInCourses($courseIdsToDelete) as $childInCourse) {
79
                $toolLinks[] = "ims_lti/start.php?id={$childInCourse->getId()}";
80
81
                $em->remove($childInCourse);
82
            }
83
84
            $em->flush();
85
86
            if (!empty($toolLinks)) {
87
                $em
88
                    ->createQuery(
89
                        "DELETE FROM ChamiloCourseBundle:CTool ct WHERE ct.category = :category AND ct.link IN (:links)"
90
                    )
91
                    ->execute(['category' => 'plugin', 'links' => $toolLinks]);
92
            }
93
        }
94
95
        if ($newSelectedCourseIds) {
96
            foreach ($newSelectedCourseIds as $newSelectedCourseId) {
97
                $newSelectedCourse = api_get_course_entity($newSelectedCourseId);
98
99
                $newTool = clone $tool;
100
                $newTool->setParent($tool);
101
                $newTool->setCourse($newSelectedCourse);
102
103
                $em->persist($newTool);
104
                $em->flush();
105
106
                if ($tool->isActiveDeepLinking()) {
107
                    continue;
108
                }
109
110
                $plugin->addCourseTool(
111
                    api_get_course_entity($newSelectedCourseId),
112
                    $newTool,
113
                    $formValues['tool_visible']
114
                );
115
            }
116
        }
117
118
        Display::addFlash(
119
            Display::return_message(get_lang('ItemUpdated'))
120
        );
121
122
        header('Location: '.api_get_path(WEB_PLUGIN_PATH).'ims_lti/admin.php');
123
        exit;
124
    }
125
126
    $form->setDefaults(
127
        [
128
            'courses' => $selectedCoursesIds,
129
            'tool_visible' => true,
130
        ]
131
    );
132
    $form->protect();
133
134
    $content = $form->returnForm();
135
136
    $interbreadcrumb[] = ['url' => api_get_path(WEB_CODE_PATH).'admin/index.php', 'name' => get_lang('PlatformAdmin')];
137
    $interbreadcrumb[] = ['url' => api_get_path(WEB_PLUGIN_PATH).'ims_lti/admin.php', 'name' => $plugin->get_title()];
138
139
    $template = new Template($plugin->get_lang('AddInCourses'));
140
    $template->assign('header', $plugin->get_lang('AddInCourses'));
141
    $template->assign('content', $content);
142
    $template->display_one_col_template();
143
} catch (Exception $exception) {
144
    Display::addFlash(
145
        Display::return_message($exception->getMessage(), 'error')
146
    );
147
148
    header('Location: '.api_get_path(WEB_PLUGIN_PATH).'ims_lti/admin.php');
149
}
150