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

FrmAdd   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 201
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 113
c 1
b 0
f 0
dl 0
loc 201
rs 10
wmc 18

3 Methods

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