Passed
Push — 1.11.x ( bce6cd...c146d9 )
by Angel Fernando Quiroz
12:25
created

plugin/ims_lti/configure.php (8 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
/** @var Course $course */
21
$course = $em->find('ChamiloCoreBundle:Course', api_get_course_int_id());
22
$addedTools = $toolsRepo->findBy(['course' => $course]);
23
$globalTools = $toolsRepo->findBy(['parent' => null, 'course' => null]);
24
25
if ($baseTool && !$baseTool->isGlobal()) {
26
    Display::addFlash(
27
        Display::return_message($plugin->get_lang('ToolNotAvailable'), 'warning')
28
    );
29
30
    header('Location: '.api_get_self().'?'.api_get_cidreq());
31
    exit;
32
}
33
34
$categories = Category::load(null, null, $course->getCode());
0 ignored issues
show
The method getCode() does not exist on Chamilo\CourseBundle\Component\CourseCopy\Course. ( Ignorable by Annotation )

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

34
$categories = Category::load(null, null, $course->/** @scrutinizer ignore-call */ getCode());

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...
35
36
switch ($action) {
37
    case 'add':
38
        $form = new \Chamilo\PluginBundle\ImsLti\Form\FrmAdd('ims_lti_add_tool', [], $baseTool);
39
        $form->build();
40
41
        if ($baseTool) {
0 ignored issues
show
$baseTool is of type ImsLtiTool, thus it always evaluated to true.
Loading history...
42
            $form->addHidden('type', $baseTool->getId());
43
        }
44
45
        if ($form->validate()) {
46
            $formValues = $form->getSubmitValues();
47
48
            $tool = new ImsLtiTool();
49
50
            if ($baseTool) {
0 ignored issues
show
$baseTool is of type ImsLtiTool, thus it always evaluated to true.
Loading history...
51
                $tool = clone $baseTool;
52
                $tool->setParent($baseTool);
53
            }
54
55
            $tool
56
                ->setName($formValues['name'])
57
                ->setDescription(
58
                    empty($formValues['description']) ? null : $formValues['description']
59
                )
60
                ->setCustomParams(
61
                    empty($formValues['custom_params']) ? null : $formValues['custom_params']
62
                )
63
                ->setDocumenTarget($formValues['document_target'])
64
                ->setCourse($course)
65
                ->setPrivacy(
66
                    !empty($formValues['share_name']),
67
                    !empty($formValues['share_email']),
68
                    !empty($formValues['share_picture'])
69
                );
70
71
            if (!$baseTool) {
0 ignored issues
show
$baseTool is of type ImsLtiTool, thus it always evaluated to true.
Loading history...
72
                if (ImsLti::V_1P3 === $formValues['version']) {
73
                    $tool
74
                        ->setVersion(ImsLti::V_1P3)
75
                        ->setLaunchUrl($formValues['launch_url'])
76
                        ->setClientId(
77
                            ImsLti::generateClientId()
78
                        )
79
                        ->setLoginUrl($formValues['login_url'])
80
                        ->setRedirectUrl($formValues['redirect_url'])
81
                        ->setAdvantageServices(
82
                            [
83
                                'ags' => isset($formValues['1p3_ags'])
84
                                    ? $formValues['1p3_ags']
85
                                    : LtiAssignmentGradesService::AGS_NONE,
86
                                'nrps' => $formValues['1p3_nrps'],
87
                            ]
88
                        )
89
                        ->publicKey = $formValues['public_key'];
90
                } elseif (ImsLti::V_1P1 === $formValues['version']) {
91
                    if (empty($formValues['consumer_key']) && empty($formValues['shared_secret'])) {
92
                        try {
93
                            $launchUrl = $plugin->getLaunchUrlFromCartridge($formValues['launch_url']);
94
                        } catch (Exception $e) {
95
                            Display::addFlash(
96
                                Display::return_message($e->getMessage(), 'error')
97
                            );
98
99
                            header('Location: '.api_get_self().'?'.api_get_cidreq());
100
                            exit;
101
                        }
102
103
                        $tool->setLaunchUrl($launchUrl);
104
                    } else {
105
                        $tool
106
                            ->setLaunchUrl($formValues['launch_url'])
107
                            ->setConsumerKey($formValues['consumer_key'])
108
                            ->setSharedSecret($formValues['shared_secret']);
109
                    }
110
                }
111
            }
112
113
            if (null === $baseTool ||
114
                ($baseTool && !$baseTool->isActiveDeepLinking())
115
            ) {
116
                $tool
117
                    ->setActiveDeepLinking(
118
                        !empty($formValues['deep_linking'])
119
                    );
120
            }
121
122
            $em->persist($tool);
123
            $em->flush();
124
125
            if ($tool->getVersion() === ImsLti::V_1P3) {
126
                $advServices = $tool->getAdvantageServices();
127
128
                if (LtiAssignmentGradesService::AGS_NONE !== $advServices['ags']) {
129
                    $lineItemResource = new LtiLineItemsResource(
130
                        $tool->getId(),
131
                        $course->getId()
0 ignored issues
show
The method getId() does not exist on Chamilo\CourseBundle\Component\CourseCopy\Course. ( Ignorable by Annotation )

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

131
                        $course->/** @scrutinizer ignore-call */ 
132
                                 getId()

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...
132
                    );
133
                    $lineItemResource->createLineItem(
134
                        ['label' => $tool->getName(), 'scoreMaximum' => 100]
135
                    );
136
137
                    Display::addFlash(
138
                        Display::return_message($plugin->get_lang('GradebookEvaluationCreated'), 'success')
139
                    );
140
                }
141
            }
142
143
            if (!$tool->isActiveDeepLinking()) {
144
                $plugin->addCourseTool($course, $tool);
145
            }
146
147
            Display::addFlash(
148
                Display::return_message($plugin->get_lang('ToolAdded'), 'success')
149
            );
150
151
            header('Location: '.api_get_self().'?'.api_get_cidreq());
152
            exit;
153
        }
154
155
        $form->setDefaultValues();
156
        break;
157
    case 'edit':
158
        /** @var ImsLtiTool|null $tool */
159
        $tool = null;
160
161
        if (!empty($_REQUEST['id'])) {
162
            $tool = $em->find('ChamiloPluginBundle:ImsLti\ImsLtiTool', (int) $_REQUEST['id']);
163
        }
164
165
        if (empty($tool) ||
166
            !ImsLtiPlugin::existsToolInCourse($tool->getId(), $course)
167
        ) {
168
            api_not_allowed(
169
                true,
170
                Display::return_message($plugin->get_lang('ToolNotAvailable'), 'error')
171
            );
172
173
            break;
174
        }
175
176
        $form = new \Chamilo\PluginBundle\Form\FrmEdit('ims_lti_edit_tool', [], $tool);
177
        $form->build(false);
178
179
        if ($form->validate()) {
180
            $formValues = $form->getSubmitValues();
181
182
            $tool
183
                ->setName($formValues['name'])
184
                ->setDescription(
185
                    empty($formValues['description']) ? null : $formValues['description']
186
                )
187
                ->setActiveDeepLinking(
188
                    !empty($formValues['deep_linking'])
189
                )
190
                ->setCustomParams(
191
                    empty($formValues['custom_params']) ? null : $formValues['custom_params']
192
                )
193
                ->setDocumenTarget($formValues['document_target'])
194
                ->setPrivacy(
195
                    !empty($formValues['share_name']),
196
                    !empty($formValues['share_email']),
197
                    !empty($formValues['share_picture'])
198
                );
199
200
            if (null === $tool->getParent()) {
201
                if ($tool->getVersion() === ImsLti::V_1P3) {
202
                    $tool
203
                        ->setLaunchUrl($formValues['launch_url'])
204
                        ->setLoginUrl($formValues['login_url'])
205
                        ->setRedirectUrl($formValues['redirect_url'])
206
                        ->setAdvantageServices(
207
                            [
208
                                'ags' => isset($formValues['1p3_ags'])
209
                                    ? $formValues['1p3_ags']
210
                                    : LtiAssignmentGradesService::AGS_NONE,
211
                                'nrps' => $formValues['1p3_nrps'],
212
                            ]
213
                        )
214
                        ->publicKey = $formValues['public_key'];
215
                } elseif ($tool->getVersion() === ImsLti::V_1P1) {
216
                    $tool
217
                        ->setLaunchUrl($formValues['launch_url'])
218
                        ->setConsumerKey($formValues['consumer_key'])
219
                        ->setSharedSecret($formValues['shared_secret']);
220
                }
221
            }
222
223
            $em->persist($tool);
224
            $em->flush();
225
226
            $courseTool = $plugin->findCourseToolByLink($course, $tool);
227
228
            if ($courseTool) {
0 ignored issues
show
$courseTool is of type Chamilo\CourseBundle\Entity\CTool, thus it always evaluated to true.
Loading history...
229
                $plugin->updateCourseTool($courseTool, $tool);
230
            }
231
232
            Display::addFlash(
233
                Display::return_message($plugin->get_lang('ToolEdited'), 'success')
234
            );
235
236
            header('Location: '.api_get_self().'?'.api_get_cidreq());
237
            exit;
238
        }
239
240
        $form->setDefaultValues();
241
        break;
242
}
243
244
$template = new Template($plugin->get_lang('AddExternalTool'));
245
$template->assign('type', $baseTool ? $baseTool->getId() : null);
0 ignored issues
show
$baseTool is of type ImsLtiTool, thus it always evaluated to true.
Loading history...
246
$template->assign('added_tools', $addedTools);
247
$template->assign('global_tools', $globalTools);
248
$template->assign('form', $form->returnForm());
249
250
$content = $template->fetch('ims_lti/view/add.tpl');
251
252
$actions = Display::url(
253
    Display::return_icon('add.png', $plugin->get_lang('AddExternalTool'), [], ICON_SIZE_MEDIUM),
254
    api_get_self().'?'.api_get_cidreq()
255
);
256
257
if (!empty($categories)) {
258
    $actions .= Display::url(
259
        Display::return_icon('gradebook.png', get_lang('MakeQualifiable'), [], ICON_SIZE_MEDIUM),
260
        './gradebook/add_eval.php?selectcat='.$categories[0]->get_id().'&'.api_get_cidreq()
261
    );
262
}
263
264
$template->assign('actions', Display::toolbarAction('lti_toolbar', [$actions]));
265
$template->assign('content', $content);
266
$template->display_one_col_template();
267