Passed
Push — master ( cfd449...e324b4 )
by Yannick
08:14
created

CourseSettingsSchema::buildForm()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 103
Code Lines 82

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 82
nc 1
nop 1
dl 0
loc 103
rs 8.3927
c 0
b 0
f 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
3
declare(strict_types=1);
4
5
/* For licensing terms, see /license.txt */
6
7
namespace Chamilo\CoreBundle\Settings;
8
9
use Chamilo\CoreBundle\Entity\Course;
10
use Chamilo\CoreBundle\Form\DataTransformer\ResourceToIdentifierTransformer;
11
use Chamilo\CoreBundle\Form\Type\YesNoType;
12
use Chamilo\CoreBundle\Repository\Node\CourseRepository;
13
use Chamilo\CoreBundle\Tool\AbstractTool;
14
use Chamilo\CoreBundle\Tool\ToolChain;
15
use Chamilo\CoreBundle\Transformer\ArrayToIdentifierTransformer;
16
use Sylius\Bundle\SettingsBundle\Schema\AbstractSettingsBuilder;
17
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
18
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
19
use Symfony\Component\Form\Extension\Core\Type\TextareaType;
20
use Symfony\Component\Form\Extension\Core\Type\TextType;
21
use Symfony\Component\Form\Extension\Core\Type\UrlType;
22
use Symfony\Component\Form\FormBuilderInterface;
23
24
class CourseSettingsSchema extends AbstractSettingsSchema
25
{
26
    protected ToolChain $toolChain;
27
28
    public function __construct(
29
        private readonly CourseRepository $courseRepository,
30
    ) {}
31
32
    public function setToolChain(ToolChain $tools): void
33
    {
34
        $this->toolChain = $tools;
35
    }
36
37
    private function getFilteredToolChoices(): array
38
    {
39
        $choices = [];
40
        $excludedSlugs = [
41
            'asset',
42
            'course_homepage',
43
            'course_tool',
44
            'course_maintenance',
45
            'member',
46
            'course_setting',
47
            'shortcuts',
48
            'tool_intro',
49
            'usergroup',
50
        ];
51
52
        /** @var AbstractTool $tool */
53
        foreach ($this->toolChain->getTools() as $tool) {
54
            $title = $tool->getTitle();
55
            $slug = null;
56
            if (method_exists($tool, 'getTitle')) {
57
                $slug = strtolower((string) $tool->getTitle());
58
            } elseif (method_exists($tool, 'getId')) {
59
                $slug = strtolower((string) $tool->getId());
60
            }
61
62
            if ($slug && \in_array($slug, $excludedSlugs, true)) {
63
                continue;
64
            }
65
66
            $choices[$title] = $slug ?: $title;
67
        }
68
69
        ksort($choices, SORT_NATURAL | SORT_FLAG_CASE);
70
71
        return $choices;
72
    }
73
74
75
76
    public function buildSettings(AbstractSettingsBuilder $builder): void
77
    {
78
        $toolChoices = $this->getFilteredToolChoices();
79
80
        $builder
81
            ->setDefaults(
82
                [
83
                    'active_tools_on_create' => array_values($toolChoices),
84
                    'display_coursecode_in_courselist' => 'false',
85
                    'display_teacher_in_courselist' => 'true',
86
                    'student_view_enabled' => 'true',
87
                    'show_navigation_menu' => 'false',
88
                    'enable_tool_introduction' => 'false',
89
                    'breadcrumbs_course_homepage' => 'course_title',
90
                    'example_material_course_creation' => 'true',
91
                    'allow_course_theme' => 'true',
92
                    'send_email_to_admin_when_create_course' => 'false',
93
                    'course_validation' => 'false',
94
                    'course_validation_terms_and_conditions_url' => '',
95
                    'course_hide_tools' => [],
96
                    'scorm_cumulative_session_time' => 'true',
97
                    'courses_default_creation_visibility' => '2',
98
                    'course_creation_use_template' => null,
99
                    'course_images_in_courses_list' => 'true',
100
                    'show_toolshortcuts' => '',
101
                    'course_creation_splash_screen' => 'true',
102
                    'block_registered_users_access_to_open_course_contents' => 'false',
103
                    'view_grid_courses' => 'true',
104
                    'my_courses_show_courses_in_user_language_only' => 'false',
105
                    'allow_public_course_with_no_terms_conditions' => 'false',
106
                    'allow_base_course_category' => 'false',
107
                    'hide_course_sidebar' => 'true',
108
                    'multiple_access_url_show_shared_course_marker' => 'false',
109
                    'course_category_code_to_use_as_model' => 'MY_CATEGORY',
110
                    'enable_unsubscribe_button_on_my_course_page' => 'false',
111
                    'course_creation_donate_message_show' => 'false',
112
                    'course_creation_donate_link' => '<some donate button html>',
113
                    'hide_course_rating' => 'false',
114
                    'course_log_hide_columns' => '',
115
                    'course_student_info' => '',
116
                    'resource_sequence_show_dependency_in_course_intro' => 'false',
117
                    'course_sequence_valid_only_in_same_session' => 'false',
118
                    'course_creation_form_set_course_category_mandatory' => 'false',
119
                    'course_creation_form_hide_course_code' => 'false',
120
                    'course_about_teacher_name_hide' => 'false',
121
                    'course_log_default_extra_fields' => '',
122
                    'course_creation_by_teacher_extra_fields_to_show' => '',
123
                    'course_creation_form_set_extra_fields_mandatory' => '',
124
                    'course_configuration_tool_extra_fields_to_show_and_edit' => '',
125
                    'course_creation_user_course_extra_field_relation_to_prefill' => '',
126
                    'show_course_duration' => 'false',
127
                    'profiling_filter_adding_users' => 'false',
128
                ]
129
            )
130
            ->setTransformer(
131
                'active_tools_on_create',
132
                new ArrayToIdentifierTransformer()
133
            )
134
            ->setTransformer(
135
                'course_hide_tools',
136
                new ArrayToIdentifierTransformer()
137
            )
138
            ->setTransformer(
139
                'course_creation_use_template',
140
                new ResourceToIdentifierTransformer($this->courseRepository, 'id')
141
            )
142
        ;
143
144
        $allowedTypes = [
145
            'active_tools_on_create' => ['array'],
146
            'course_hide_tools' => ['array'],
147
            'display_coursecode_in_courselist' => ['string'],
148
            'display_teacher_in_courselist' => ['string'],
149
            'student_view_enabled' => ['string'],
150
        ];
151
152
        $this->setMultipleAllowedTypes($allowedTypes, $builder);
153
    }
154
155
    public function buildForm(FormBuilderInterface $builder): void
156
    {
157
        $toolChoices = $this->getFilteredToolChoices();
158
159
        $builder
160
            ->add('active_tools_on_create', ChoiceType::class, [
161
                'choices'  => $toolChoices,
162
                'multiple' => true,
163
                'expanded' => true,
164
            ])
165
            ->add('display_coursecode_in_courselist', YesNoType::class)
166
            ->add('display_teacher_in_courselist', YesNoType::class)
167
            ->add('student_view_enabled', YesNoType::class)
168
            ->add('show_navigation_menu', ChoiceType::class, [
169
                'choices' => [
170
                    'No' => 'false',
171
                    'Icons only' => 'icons',
172
                    'Text only' => 'text',
173
                    'Icons text' => 'iconstext',
174
                ],
175
            ])
176
            ->add('enable_tool_introduction', YesNoType::class)
177
            ->add('breadcrumbs_course_homepage', ChoiceType::class, [
178
                'choices' => [
179
                    'Course homepage' => 'course_home',
180
                    'Course code' => 'course_code',
181
                    'Course title' => 'course_title',
182
                    'Session name and course title' => 'session_name_and_course_title',
183
                ],
184
            ])
185
            ->add('example_material_course_creation', YesNoType::class)
186
            ->add('allow_course_theme', YesNoType::class)
187
            ->add('send_email_to_admin_when_create_course', YesNoType::class)
188
            ->add('course_validation', YesNoType::class)
189
            ->add('course_validation_terms_and_conditions_url', UrlType::class)
190
            ->add('course_hide_tools', ChoiceType::class, [
191
                'choices'  => $toolChoices,
192
                'multiple' => true,
193
                'expanded' => true,
194
            ])
195
            ->add('scorm_cumulative_session_time', YesNoType::class)
196
            ->add('courses_default_creation_visibility', ChoiceType::class, [
197
                'choices' => [
198
                    'Public' => '3',
199
                    'Open' => '2',
200
                    'Private' => '1',
201
                    'Closed' => '0',
202
                ],
203
            ])
204
            ->add('course_creation_use_template', EntityType::class, [
205
                'class' => Course::class,
206
                'placeholder' => 'Choose ...',
207
                'empty_data' => null,
208
                'choice_label' => 'title',
209
                'choice_value' => 'id',
210
                'required' => false,
211
            ])
212
            ->add('course_images_in_courses_list', YesNoType::class)
213
            ->add('show_toolshortcuts', YesNoType::class)
214
            ->add('course_creation_splash_screen', YesNoType::class)
215
            ->add('block_registered_users_access_to_open_course_contents', YesNoType::class)
216
            ->add('view_grid_courses', YesNoType::class)
217
            ->add('my_courses_show_courses_in_user_language_only', YesNoType::class)
218
            ->add('allow_public_course_with_no_terms_conditions', YesNoType::class)
219
            ->add('allow_base_course_category', YesNoType::class)
220
            ->add('hide_course_sidebar', YesNoType::class)
221
            ->add('multiple_access_url_show_shared_course_marker', YesNoType::class)
222
            ->add('course_category_code_to_use_as_model', TextType::class)
223
            ->add('enable_unsubscribe_button_on_my_course_page', YesNoType::class)
224
            ->add('course_creation_donate_message_show', YesNoType::class)
225
            ->add('course_creation_donate_link', TextType::class)
226
            ->add('hide_course_rating', YesNoType::class)
227
            ->add('course_log_hide_columns', TextareaType::class, [
228
                'attr' => ['rows' => 5, 'style' => 'font-family: monospace;'],
229
            ])
230
            ->add('course_student_info', TextareaType::class, [
231
                'attr' => ['rows' => 5, 'style' => 'font-family: monospace;'],
232
            ])
233
            ->add('resource_sequence_show_dependency_in_course_intro', YesNoType::class)
234
            ->add('course_sequence_valid_only_in_same_session', YesNoType::class)
235
            ->add('course_creation_form_set_course_category_mandatory', YesNoType::class)
236
            ->add('course_creation_form_hide_course_code', YesNoType::class)
237
            ->add('course_about_teacher_name_hide', YesNoType::class)
238
            ->add('course_log_default_extra_fields', TextareaType::class, [
239
                'attr' => ['rows' => 5, 'style' => 'font-family: monospace;'],
240
            ])
241
            ->add('course_creation_by_teacher_extra_fields_to_show', TextareaType::class, [
242
                'attr' => ['rows' => 3, 'style' => 'font-family: monospace;'],
243
            ])
244
            ->add('course_creation_form_set_extra_fields_mandatory', TextareaType::class, [
245
                'attr' => ['rows' => 3, 'style' => 'font-family: monospace;'],
246
            ])
247
            ->add('course_configuration_tool_extra_fields_to_show_and_edit', TextareaType::class, [
248
                'attr' => ['rows' => 3, 'style' => 'font-family: monospace;'],
249
            ])
250
            ->add('course_creation_user_course_extra_field_relation_to_prefill', TextareaType::class, [
251
                'attr' => ['rows' => 5, 'style' => 'font-family: monospace;'],
252
            ])
253
            ->add('show_course_duration', YesNoType::class)
254
            ->add('profiling_filter_adding_users', YesNoType::class)
255
        ;
256
257
        $this->updateFormFieldsFromSettingsInfo($builder);
258
    }
259
}
260