Issues (2128)

plugin/ims_lti/configure.php (6 issues)

1
<?php
2
/* For license terms, see /license.txt */
3
4
use Chamilo\CoreBundle\Entity\Course;
0 ignored issues
show
This use statement conflicts with another class in this namespace, Course. 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...
5
use Chamilo\PluginBundle\Entity\ImsLti\ImsLtiTool;
6
7
require_once __DIR__.'/../../main/inc/global.inc.php';
8
9
api_protect_course_script();
10
api_protect_teacher_script();
11
12
$plugin = ImsLtiPlugin::create();
13
$em = Database::getManager();
14
$toolsRepo = $em->getRepository('ChamiloPluginBundle:ImsLti\ImsLtiTool');
15
16
/** @var ImsLtiTool $baseTool */
17
$baseTool = isset($_REQUEST['type']) ? $toolsRepo->find(intval($_REQUEST['type'])) : null;
18
$action = !empty($_REQUEST['action']) ? $_REQUEST['action'] : 'add';
19
20
$course = api_get_course_entity(api_get_course_int_id());
21
$addedTools = $toolsRepo->findBy(['course' => $course]);
22
$globalTools = $toolsRepo->findBy(['parent' => null, 'course' => null]);
23
24
if ($baseTool && !$baseTool->isGlobal()) {
25
    Display::addFlash(
26
        Display::return_message($plugin->get_lang('ToolNotAvailable'), 'warning')
27
    );
28
29
    header('Location: '.api_get_self().'?'.api_get_cidreq());
30
    exit;
31
}
32
33
$categories = Category::load(null, null, $course->getCode());
34
35
switch ($action) {
36
    case 'add':
37
        $form = new \Chamilo\PluginBundle\ImsLti\Form\FrmAdd('ims_lti_add_tool', [], $baseTool);
38
        $form->build();
39
40
        if ($baseTool) {
0 ignored issues
show
$baseTool is of type ImsLtiTool, thus it always evaluated to true.
Loading history...
41
            $form->addHidden('type', $baseTool->getId());
42
        }
43
44
        if ($form->validate()) {
45
            $formValues = $form->getSubmitValues();
46
47
            $tool = new ImsLtiTool();
48
49
            if ($baseTool) {
0 ignored issues
show
$baseTool is of type ImsLtiTool, thus it always evaluated to true.
Loading history...
50
                $tool = clone $baseTool;
51
                $tool->setParent($baseTool);
52
            }
53
54
            $tool
55
                ->setName($formValues['name'])
56
                ->setDescription(
57
                    empty($formValues['description']) ? null : $formValues['description']
58
                )
59
                ->setCustomParams(
60
                    empty($formValues['custom_params']) ? null : $formValues['custom_params']
61
                )
62
                ->setDocumenTarget($formValues['document_target'])
63
                ->setCourse($course)
64
                ->setPrivacy(
65
                    !empty($formValues['share_name']),
66
                    !empty($formValues['share_email']),
67
                    !empty($formValues['share_picture'])
68
                );
69
70
            if (!empty($formValues['replacement_user_id'])) {
71
                $tool->setReplacementForUserId($formValues['replacement_user_id']);
72
            }
73
74
            if (!$baseTool) {
0 ignored issues
show
$baseTool is of type ImsLtiTool, thus it always evaluated to true.
Loading history...
75
                if (ImsLti::V_1P3 === $formValues['version']) {
76
                    $tool
77
                        ->setVersion(ImsLti::V_1P3)
78
                        ->setLaunchUrl($formValues['launch_url'])
79
                        ->setClientId(
80
                            ImsLti::generateClientId()
81
                        )
82
                        ->setLoginUrl($formValues['login_url'])
83
                        ->setRedirectUrl($formValues['redirect_url'])
84
                        ->setAdvantageServices(
85
                            [
86
                                'ags' => $formValues['1p3_ags'] ?? LtiAssignmentGradesService::AGS_NONE,
87
                                'nrps' => $formValues['1p3_nrps'],
88
                            ]
89
                        )
90
                        ->setJwksUrl($formValues['jwks_url'])
91
                        ->publicKey = $formValues['public_key'];
92
                } elseif (ImsLti::V_1P1 === $formValues['version']) {
93
                    if (empty($formValues['consumer_key']) && empty($formValues['shared_secret'])) {
94
                        try {
95
                            $launchUrl = $plugin->getLaunchUrlFromCartridge($formValues['launch_url']);
96
                        } catch (Exception $e) {
97
                            Display::addFlash(
98
                                Display::return_message($e->getMessage(), 'error')
99
                            );
100
101
                            header('Location: '.api_get_self().'?'.api_get_cidreq());
102
                            exit;
103
                        }
104
105
                        $tool->setLaunchUrl($launchUrl);
106
                    } else {
107
                        $tool
108
                            ->setLaunchUrl($formValues['launch_url'])
109
                            ->setConsumerKey($formValues['consumer_key'])
110
                            ->setSharedSecret($formValues['shared_secret']);
111
                    }
112
                }
113
            }
114
115
            if (null === $baseTool ||
116
                ($baseTool && !$baseTool->isActiveDeepLinking())
117
            ) {
118
                $tool
119
                    ->setActiveDeepLinking(
120
                        !empty($formValues['deep_linking'])
121
                    );
122
            }
123
124
            $em->persist($tool);
125
            $em->flush();
126
127
            if ($tool->getVersion() === ImsLti::V_1P3) {
128
                $advServices = $tool->getAdvantageServices();
129
130
                if (LtiAssignmentGradesService::AGS_NONE !== $advServices['ags']) {
131
                    $lineItemResource = new LtiLineItemsResource(
132
                        $tool->getId(),
133
                        $course->getId()
134
                    );
135
                    $lineItemResource->createLineItem(
136
                        ['label' => $tool->getName(), 'scoreMaximum' => 100]
137
                    );
138
139
                    Display::addFlash(
140
                        Display::return_message($plugin->get_lang('GradebookEvaluationCreated'), 'success')
141
                    );
142
                }
143
            }
144
145
            if (!$tool->isActiveDeepLinking()) {
146
                $plugin->addCourseTool($course, $tool);
147
            }
148
149
            Display::addFlash(
150
                Display::return_message($plugin->get_lang('ToolAdded'), 'success')
151
            );
152
153
            header('Location: '.api_get_self().'?'.api_get_cidreq());
154
            exit;
155
        }
156
157
        $form->setDefaultValues();
158
        break;
159
    case 'edit':
160
        /** @var ImsLtiTool|null $tool */
161
        $tool = null;
162
163
        if (!empty($_REQUEST['id'])) {
164
            $tool = $em->find('ChamiloPluginBundle:ImsLti\ImsLtiTool', (int) $_REQUEST['id']);
165
        }
166
167
        if (empty($tool) ||
168
            !ImsLtiPlugin::existsToolInCourse($tool->getId(), $course)
169
        ) {
170
            api_not_allowed(
171
                true,
172
                Display::return_message($plugin->get_lang('ToolNotAvailable'), 'error')
173
            );
174
175
            break;
176
        }
177
178
        $form = new \Chamilo\PluginBundle\Form\FrmEdit('ims_lti_edit_tool', [], $tool);
179
        $form->build(false);
180
181
        if ($form->validate()) {
182
            $formValues = $form->getSubmitValues();
183
184
            $tool
185
                ->setName($formValues['name'])
186
                ->setDescription(
187
                    empty($formValues['description']) ? null : $formValues['description']
188
                )
189
                ->setActiveDeepLinking(
190
                    !empty($formValues['deep_linking'])
191
                )
192
                ->setCustomParams(
193
                    empty($formValues['custom_params']) ? null : $formValues['custom_params']
194
                )
195
                ->setDocumenTarget($formValues['document_target'])
196
                ->setPrivacy(
197
                    !empty($formValues['share_name']),
198
                    !empty($formValues['share_email']),
199
                    !empty($formValues['share_picture'])
200
                );
201
202
            if (!empty($formValues['replacement_user_id'])) {
203
                $tool->setReplacementForUserId($formValues['replacement_user_id']);
204
            }
205
206
            if (null === $tool->getParent()) {
207
                if ($tool->getVersion() === ImsLti::V_1P3) {
208
                    $tool
209
                        ->setLaunchUrl($formValues['launch_url'])
210
                        ->setLoginUrl($formValues['login_url'])
211
                        ->setRedirectUrl($formValues['redirect_url'])
212
                        ->setAdvantageServices(
213
                            [
214
                                'ags' => $formValues['1p3_ags'] ?? LtiAssignmentGradesService::AGS_NONE,
215
                                'nrps' => $formValues['1p3_nrps'],
216
                            ]
217
                        )
218
                        ->setJwksUrl($formValues['jwks_url'])
219
                        ->publicKey = $formValues['public_key'];
220
                } elseif ($tool->getVersion() === ImsLti::V_1P1) {
221
                    $tool
222
                        ->setLaunchUrl($formValues['launch_url'])
223
                        ->setConsumerKey($formValues['consumer_key'])
224
                        ->setSharedSecret($formValues['shared_secret']);
225
                }
226
            }
227
228
            $em->persist($tool);
229
            $em->flush();
230
231
            $courseTool = $plugin->findCourseToolByLink($course, $tool);
232
233
            if ($courseTool) {
0 ignored issues
show
$courseTool is of type Chamilo\CourseBundle\Entity\CTool, thus it always evaluated to true.
Loading history...
234
                $plugin->updateCourseTool($courseTool, $tool);
235
            }
236
237
            Display::addFlash(
238
                Display::return_message($plugin->get_lang('ToolEdited'), 'success')
239
            );
240
241
            header('Location: '.api_get_self().'?'.api_get_cidreq());
242
            exit;
243
        }
244
245
        $form->setDefaultValues();
246
        break;
247
    default:
248
        api_not_allowed(true);
249
        break;
250
}
251
252
$template = new Template($plugin->get_lang('AddExternalTool'));
253
$template->assign('type', $baseTool ? $baseTool->getId() : null);
0 ignored issues
show
$baseTool is of type ImsLtiTool, thus it always evaluated to true.
Loading history...
254
$template->assign('added_tools', $addedTools);
255
$template->assign('global_tools', $globalTools);
256
$template->assign('form', $form->returnForm());
257
258
$content = $template->fetch('ims_lti/view/add.tpl');
259
260
$actions = Display::url(
261
    Display::return_icon('add.png', $plugin->get_lang('AddExternalTool'), [], ICON_SIZE_MEDIUM),
262
    api_get_self().'?'.api_get_cidreq()
263
);
264
265
if (!empty($categories)) {
266
    $actions .= Display::url(
267
        Display::return_icon('gradebook.png', get_lang('MakeQualifiable'), [], ICON_SIZE_MEDIUM),
268
        './gradebook/add_eval.php?selectcat='.$categories[0]->get_id().'&'.api_get_cidreq()
269
    );
270
}
271
272
$template->assign('actions', Display::toolbarAction('lti_toolbar', [$actions]));
273
$template->assign('content', $content);
274
$template->display_one_col_template();
275