Passed
Push — master ( b0b4c3...57268f )
by Yannick
09:19 queued 13s
created

FrmAdd::build()   C

Complexity

Conditions 9
Paths 48

Size

Total Lines 135
Code Lines 92

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 92
c 0
b 0
f 0
dl 0
loc 135
rs 6.6189
cc 9
nc 48
nop 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/* For licensing terms, see /license.txt */
3
4
namespace Chamilo\PluginBundle\ImsLti\Form;
5
6
use Category;
7
use Chamilo\PluginBundle\Entity\ImsLti\ImsLtiTool;
8
use Display;
9
use FormValidator;
10
use ImsLti;
11
use ImsLtiPlugin;
12
use LtiAssignmentGradesService;
13
use LtiNamesRoleProvisioningService;
14
15
/**
16
 * Class FrmAdd.
17
 */
18
class FrmAdd extends FormValidator
19
{
20
    /**
21
     * @var ImsLtiTool|null
22
     */
23
    private $baseTool;
24
    /**
25
     * @var bool
26
     */
27
    private $toolIsV1p3;
28
29
    /**
30
     * FrmAdd constructor.
31
     *
32
     * @param string $name
33
     * @param array  $attributes
34
     */
35
    public function __construct(
36
        $name,
37
        $attributes = [],
38
        ImsLtiTool $tool = null
39
    ) {
40
        parent::__construct($name, 'POST', '', '', $attributes, self::LAYOUT_HORIZONTAL, true);
41
42
        $this->baseTool = $tool;
43
        $this->toolIsV1p3 = $this->baseTool
44
            && !empty($this->baseTool->publicKey)
45
            && !empty($this->baseTool->getClientId())
46
            && !empty($this->baseTool->getLoginUrl())
47
            && !empty($this->baseTool->getRedirectUrl());
48
    }
49
50
    /**
51
     * Build the form.
52
     */
53
    public function build()
54
    {
55
        $plugin = ImsLtiPlugin::create();
56
57
        $this->addHeader($plugin->get_lang('ToolSettings'));
58
        $this->addText('name', get_lang('Name'));
59
        $this->addTextarea('description', get_lang('Description'));
60
61
        if (null === $this->baseTool) {
62
            $this->addUrl('launch_url', $plugin->get_lang('LaunchUrl'), true);
63
            $this->addRadio(
64
                'version',
65
                $plugin->get_lang('LtiVersion'),
66
                [
67
                    ImsLti::V_1P1 => 'LTI 1.0 / 1.1',
68
                    ImsLti::V_1P3 => 'LTI 1.3.0',
69
                ]
70
            );
71
            $this->addHtml('<div class="'.ImsLti::V_1P1.'" style="display: none;">');
72
            $this->addText('consumer_key', $plugin->get_lang('ConsumerKey'), false);
73
            $this->addText('shared_secret', $plugin->get_lang('SharedSecret'), false);
74
            $this->addHtml('</div>');
75
            $this->addHtml('<div class="'.ImsLti::V_1P3.'" style="display: block;">');
76
            $this->addRadio(
77
                'public_key_type',
78
                $plugin->get_lang('PublicKeyType'),
79
                [
80
                    ImsLti::LTI_JWK_KEYSET => $plugin->get_lang('KeySetUrl'),
81
                    ImsLti::LTI_RSA_KEY => $plugin->get_lang('RsaKey'),
82
                ]
83
            );
84
            $this->addHtml('<div class="'.ImsLti::LTI_JWK_KEYSET.'" style="display: block;">');
85
            $this->addUrl('jwks_url', $plugin->get_lang('PublicKeyset'), false);
86
            $this->addHtml('</div>');
87
            $this->addHtml('<div class="'.ImsLti::LTI_RSA_KEY.'" style="display: none;">');
88
            $this->addTextarea(
89
                'public_key',
90
                $plugin->get_lang('PublicKey'),
91
                ['style' => 'font-family: monospace;', 'rows' => 5]
92
            );
93
            $this->addHtml('</div>');
94
            $this->addUrl('login_url', $plugin->get_lang('LoginUrl'), false);
95
            $this->addUrl('redirect_url', $plugin->get_lang('RedirectUrl'), false);
96
            $this->addHtml('</div>');
97
        }
98
99
        $this->addButtonAdvancedSettings('lti_adv');
100
        $this->addHtml('<div id="lti_adv_options" style="display:none;">');
101
        $this->addTextarea(
102
            'custom_params',
103
            [$plugin->get_lang('CustomParams'), $plugin->get_lang('CustomParamsHelp')]
104
        );
105
        $this->addSelect(
106
            'document_target',
107
            get_lang('LinkTarget'),
108
            ['iframe' => 'iframe', 'window' => 'window']
109
        );
110
111
        if (null === $this->baseTool
112
            || ($this->baseTool && !$this->baseTool->isActiveDeepLinking())
113
        ) {
114
            $this->addCheckBox(
115
                'deep_linking',
116
                [null, $plugin->get_lang('SupportDeppLinkingHelp'), null],
117
                $plugin->get_lang('SupportDeepLinking')
118
            );
119
        }
120
121
        $showAGS = false;
122
123
        if (api_get_course_int_id()) {
124
            $caterories = Category::load(null, null, api_get_course_id());
125
126
            if (!empty($caterories)) {
127
                $showAGS = true;
128
            }
129
        } else {
130
            $showAGS = true;
131
        }
132
133
        $this->addHtml('<div class="'.ImsLti::V_1P3.'" style="display: none;">');
134
135
        if ($showAGS) {
136
            $this->addRadio(
137
                '1p3_ags',
138
                $plugin->get_lang('AssigmentAndGradesService'),
139
                [
140
                    LtiAssignmentGradesService::AGS_NONE => $plugin->get_lang('DontUseService'),
141
                    LtiAssignmentGradesService::AGS_SIMPLE => $plugin->get_lang('AGServiceSimple'),
142
                    LtiAssignmentGradesService::AGS_FULL => $plugin->get_lang('AGServiceFull'),
143
                ]
144
            );
145
        } else {
146
            $gradebookUrl = api_get_path(WEB_CODE_PATH).'gradebook/index.php?'.api_get_cidreq();
147
148
            $this->addLabel(
149
                $plugin->get_lang('AssigmentAndGradesService'),
150
                sprintf(
151
                    $plugin->get_lang('YouNeedCreateTheGradebokInCourseFirst'),
152
                    Display::url($gradebookUrl, $gradebookUrl)
153
                )
154
            );
155
        }
156
157
        $this->addRadio(
158
            '1p3_nrps',
159
            $plugin->get_lang('NamesAndRoleProvisioningService'),
160
            [
161
                LtiNamesRoleProvisioningService::NRPS_NONE => $plugin->get_lang('DontUseService'),
162
                LtiNamesRoleProvisioningService::NRPS_CONTEXT_MEMBERSHIP => $plugin->get_lang('UseService'),
163
            ]
164
        );
165
        $this->addHtml('</div>');
166
167
        if (!$this->baseTool) {
168
            $this->addText(
169
                'replacement_user_id',
170
                [
171
                    $plugin->get_lang('ReplacementUserId'),
172
                    $plugin->get_lang('ReplacementUserIdHelp'),
173
                ],
174
                false
175
            );
176
            $this->applyFilter('replacement_user_id', 'trim');
177
        }
178
179
        $this->addHtml('</div>');
180
        $this->addButtonAdvancedSettings('lti_privacy', get_lang('Privacy'));
181
        $this->addHtml('<div id="lti_privacy_options" style="display:none;">');
182
        $this->addCheckBox('share_name', null, $plugin->get_lang('ShareLauncherName'));
183
        $this->addCheckBox('share_email', null, $plugin->get_lang('ShareLauncherEmail'));
184
        $this->addCheckBox('share_picture', null, $plugin->get_lang('ShareLauncherPicture'));
185
        $this->addHtml('</div>');
186
        $this->addButtonCreate($plugin->get_lang('AddExternalTool'));
187
        $this->applyFilter('__ALL__', 'trim');
188
    }
189
190
    public function setDefaultValues()
191
    {
192
        $defaults = [];
193
        $defaults['version'] = ImsLti::V_1P3;
194
        $defaults['public_key_type'] = ImsLti::LTI_JWK_KEYSET;
195
196
        if ($this->baseTool) {
197
            $defaults['name'] = $this->baseTool->getName();
198
            $defaults['description'] = $this->baseTool->getDescription();
199
            $defaults['custom_params'] = $this->baseTool->getCustomParams();
200
            $defaults['document_target'] = $this->baseTool->getDocumentTarget();
201
            $defaults['share_name'] = $this->baseTool->isSharingName();
202
            $defaults['share_email'] = $this->baseTool->isSharingEmail();
203
            $defaults['share_picture'] = $this->baseTool->isSharingPicture();
204
            $defaults['public_key'] = $this->baseTool->publicKey;
205
            $defaults['login_url'] = $this->baseTool->getLoginUrl();
206
            $defaults['redirect_url'] = $this->baseTool->getRedirectUrl();
207
208
            if ($this->toolIsV1p3) {
209
                $advServices = $this->baseTool->getAdvantageServices();
210
211
                $defaults['1p3_ags'] = $advServices['ags'];
212
                $defaults['1p3_nrps'] = $advServices['nrps'];
213
            }
214
        }
215
216
        $this->setDefaults($defaults);
217
    }
218
219
    public function returnForm(): string
220
    {
221
        $js = "<script>
222
                \$(function () {
223
                    \$('[name=\"version\"]').on('change', function () {
224
                        $('.".ImsLti::V_1P1.", .".ImsLti::V_1P3."').hide();
225
226
                        $('.' + this.value).show();
227
                    })
228
                    \$('[name=\"public_key_type\"]').on('change', function () {
229
                        $('.".ImsLti::LTI_JWK_KEYSET.", .".ImsLti::LTI_RSA_KEY."').hide();
230
                        $('[name=\"public_key\"], [name=\"jwks_url\"]').val('');
231
232
                        $('.' + this.value).show();
233
                    })
234
                });
235
            </script>";
236
237
        return $js.parent::returnForm(); // TODO: Change the autogenerated stub
238
    }
239
}
240