Passed
Push — 1.11.x ( 4d8435...e590f3 )
by Angel Fernando Quiroz
11:50
created

FrmEdit   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 198
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 118
dl 0
loc 198
rs 10
c 1
b 0
f 0
wmc 17

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
A setDefaultValues() 0 25 1
F build() 0 139 15
1
<?php
2
/* For licensing terms, see /license.txt */
3
4
namespace Chamilo\PluginBundle\Form;
5
6
use Category;
7
use Chamilo\PluginBundle\Entity\ImsLti\ImsLtiTool;
8
use Display;
9
use Exception;
10
use FormValidator;
11
use ImsLti;
12
use ImsLtiPlugin;
13
use LtiAssignmentGradesService;
14
use LtiNamesRoleProvisioningService;
15
16
/**
17
 * Class FrmAdd.
18
 */
19
class FrmEdit extends FormValidator
20
{
21
    /**
22
     * @var ImsLtiTool|null
23
     */
24
    private $tool;
25
26
    /**
27
     * FrmAdd constructor.
28
     *
29
     * @param string          $name
30
     * @param array           $attributes
31
     * @param ImsLtiTool|null $tool
32
     */
33
    public function __construct(
34
        $name,
35
        $attributes = [],
36
        ImsLtiTool $tool = null
37
    ) {
38
        parent::__construct($name, 'POST', '', '', $attributes, self::LAYOUT_HORIZONTAL, true);
39
40
        $this->tool = $tool;
41
    }
42
43
    /**
44
     * Build the form.
45
     *
46
     * @param bool $globalMode
47
     */
48
    public function build($globalMode = true)
49
    {
50
        $plugin = ImsLtiPlugin::create();
51
        $course = $this->tool->getCourse();
0 ignored issues
show
Bug introduced by
The method getCourse() does not exist on null. ( Ignorable by Annotation )

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

51
        /** @scrutinizer ignore-call */ 
52
        $course = $this->tool->getCourse();

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...
52
        $parent = $this->tool->getParent();
53
54
        $this->addHeader($plugin->get_lang('ToolSettings'));
55
56
        if (null !== $course && $globalMode) {
57
            $this->addHtml(
58
                Display::return_message(
59
                    sprintf($plugin->get_lang('ToolAddedOnCourseX'), $course->getTitle()),
60
                    'normal',
61
                    false
62
                )
63
            );
64
        }
65
66
        $this->addText('name', get_lang('Name'));
67
        $this->addTextarea('description', get_lang('Description'));
68
        $this->addRadio(
69
            'version',
70
            $plugin->get_lang('LtiVersion'),
71
            [
72
                ImsLti::V_1P1 => 'LTI 1.0 / 1.1',
73
                ImsLti::V_1P3 => 'LTI 1.3.0',
74
            ]
75
        );
76
        $this->freeze(['version']);
77
78
        if (null === $parent) {
79
            $this->addUrl('launch_url', $plugin->get_lang('LaunchUrl'), true);
80
            if ($this->tool->getVersion() === ImsLti::V_1P1) {
81
                $this->addText('consumer_key', $plugin->get_lang('ConsumerKey'), false);
82
                $this->addText('shared_secret', $plugin->get_lang('SharedSecret'), false);
83
            } elseif ($this->tool->getVersion() === ImsLti::V_1P3) {
84
                $this->addText('client_id', $plugin->get_lang('ClientId'), true);
85
                $this->freeze(['client_id']);
86
                $this->addTextarea(
87
                    'public_key',
88
                    $plugin->get_lang('PublicKey'),
89
                    ['style' => 'font-family: monospace;', 'rows' => 5],
90
                    true
91
                );
92
                $this->addUrl('login_url', $plugin->get_lang('LoginUrl'));
93
                $this->addUrl('redirect_url', $plugin->get_lang('RedirectUrl'));
94
            }
95
        }
96
97
        $this->addButtonAdvancedSettings('lti_adv');
98
        $this->addHtml('<div id="lti_adv_options" style="display:none;">');
99
        $this->addTextarea(
100
            'custom_params',
101
            [$plugin->get_lang('CustomParams'), $plugin->get_lang('CustomParamsHelp')]
102
        );
103
        $this->addSelect(
104
            'document_target',
105
            get_lang('LinkTarget'),
106
            ['iframe' => 'iframe', 'window' => 'window']
107
        );
108
109
        if (null === $parent
110
            || (null !== $parent && !$parent->isActiveDeepLinking())
111
        ) {
112
            $this->addCheckBox(
113
                'deep_linking',
114
                [null, $plugin->get_lang('SupportDeppLinkingHelp'), null],
115
                $plugin->get_lang('SupportDeepLinking')
116
            );
117
        }
118
119
        if (null === $parent && $this->tool->getVersion() === ImsLti::V_1P3) {
120
            $showAGS = false;
121
122
            if (api_get_course_int_id()) {
123
                $caterories = Category::load(null, null, api_get_course_id());
124
125
                if (!empty($caterories)) {
126
                    $showAGS = true;
127
                }
128
            } else {
129
                $showAGS = true;
130
            }
131
132
            if ($showAGS) {
133
                $this->addRadio(
134
                    '1p3_ags',
135
                    $plugin->get_lang('AssigmentAndGradesService'),
136
                    [
137
                        LtiAssignmentGradesService::AGS_NONE => $plugin->get_lang('DontUseService'),
138
                        LtiAssignmentGradesService::AGS_SIMPLE => $plugin->get_lang('AGServiceSimple'),
139
                        LtiAssignmentGradesService::AGS_FULL => $plugin->get_lang('AGServiceFull'),
140
                    ]
141
                );
142
            } else {
143
                $gradebookUrl = api_get_path(WEB_CODE_PATH).'gradebook/index.php?'.api_get_cidreq();
144
145
                $this->addLabel(
146
                    $plugin->get_lang('AssigmentAndGradesService'),
147
                    sprintf(
148
                        $plugin->get_lang('YouNeedCreateTheGradebokInCourseFirst'),
149
                        Display::url($gradebookUrl, $gradebookUrl)
150
                    )
151
                );
152
            }
153
154
            $this->addRadio(
155
                '1p3_nrps',
156
                $plugin->get_lang('NamesAndRoleProvisioningService'),
157
                [
158
                    LtiNamesRoleProvisioningService::NRPS_NONE => $plugin->get_lang('DontUseService'),
159
                    LtiNamesRoleProvisioningService::NRPS_CONTEXT_MEMBERSHIP => $plugin->get_lang('UseService'),
160
                ]
161
            );
162
        }
163
164
        if (!$parent) {
165
            $this->addText(
166
                'replacement_user_id',
167
                [
168
                    $plugin->get_lang('ReplacementUserId'),
169
                    $plugin->get_lang('ReplacementUserIdHelp'),
170
                ],
171
                false
172
            );
173
            $this->applyFilter('replacement_user_id', 'trim');
174
        }
175
176
        $this->addHtml('</div>');
177
        $this->addButtonAdvancedSettings('lti_privacy', get_lang('Privacy'));
178
        $this->addHtml('<div id="lti_privacy_options" style="display:none;">');
179
        $this->addCheckBox('share_name', null, $plugin->get_lang('ShareLauncherName'));
180
        $this->addCheckBox('share_email', null, $plugin->get_lang('ShareLauncherEmail'));
181
        $this->addCheckBox('share_picture', null, $plugin->get_lang('ShareLauncherPicture'));
182
        $this->addHtml('</div>');
183
        $this->addButtonUpdate($plugin->get_lang('EditExternalTool'));
184
        $this->addHidden('id', $this->tool->getId());
185
        $this->addHidden('action', 'edit');
186
        $this->applyFilter('__ALL__', 'trim');
187
    }
188
189
    /**
190
     * @throws Exception
191
     */
192
    public function setDefaultValues()
193
    {
194
        $advServices = $this->tool->getAdvantageServices();
195
196
        $this->setDefaults(
197
            [
198
                'name' => $this->tool->getName(),
199
                'description' => $this->tool->getDescription(),
200
                'launch_url' => $this->tool->getLaunchUrl(),
201
                'consumer_key' => $this->tool->getConsumerKey(),
202
                'shared_secret' => $this->tool->getSharedSecret(),
203
                'custom_params' => $this->tool->getCustomParams(),
204
                'deep_linking' => $this->tool->isActiveDeepLinking(),
205
                'share_name' => $this->tool->isSharingName(),
206
                'share_email' => $this->tool->isSharingEmail(),
207
                'share_picture' => $this->tool->isSharingPicture(),
208
                'version' => $this->tool->getVersion(),
209
                'client_id' => $this->tool->getClientId(),
210
                'public_key' => $this->tool->publicKey,
211
                'login_url' => $this->tool->getLoginUrl(),
212
                'redirect_url' => $this->tool->getRedirectUrl(),
213
                '1p3_ags' => $advServices['ags'],
214
                '1p3_nrps' => $advServices['nrps'],
215
                'document_target' => $this->tool->getDocumentTarget(),
216
                'replacement_user_id' => $this->tool->getReplacementForUserId(),
217
            ]
218
        );
219
    }
220
}
221