Completed
Push — master ( 38accd...2bf9cf )
by Julito
10:36
created

SettingsManager::transformParameters()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 5
nc 3
nop 2
dl 0
loc 11
rs 10
c 0
b 0
f 0
1
<?php
2
3
/* For licensing terms, see /license.txt */
4
5
namespace Chamilo\SettingsBundle\Manager;
6
7
use Chamilo\CoreBundle\Entity\AccessUrl;
8
use Chamilo\CoreBundle\Entity\Course;
9
use Chamilo\CoreBundle\Entity\SettingsCurrent;
10
use Doctrine\ORM\EntityManager;
11
use Doctrine\ORM\EntityRepository;
12
//use Sylius\Bundle\ResourceBundle\Controller\EventDispatcherInterface;
13
use Sylius\Bundle\SettingsBundle\Event\SettingsEvent;
14
use Sylius\Bundle\SettingsBundle\Manager\SettingsManagerInterface;
15
use Sylius\Bundle\SettingsBundle\Model\Settings;
16
use Sylius\Bundle\SettingsBundle\Model\SettingsInterface;
17
use Sylius\Bundle\SettingsBundle\Schema\SchemaInterface;
18
use Sylius\Bundle\SettingsBundle\Schema\SettingsBuilder;
19
use Sylius\Component\Registry\ServiceRegistryInterface;
20
use Sylius\Component\Resource\Factory\FactoryInterface;
21
use Symfony\Component\Validator\ConstraintViolationListInterface;
22
use Symfony\Component\Validator\Exception\ValidatorException;
23
24
/**
25
 * Class SettingsManager.
26
 */
27
class SettingsManager implements SettingsManagerInterface
28
{
29
    protected $url;
30
31
    /**
32
     * @var ServiceRegistryInterface
33
     */
34
    protected $schemaRegistry;
35
36
    /**
37
     * @var ServiceRegistryInterface
38
     */
39
    protected $resolverRegistry;
40
41
    /**
42
     * @var EntityManager
43
     */
44
    protected $manager;
45
46
    /**
47
     * @var EntityRepository
48
     */
49
    protected $repository;
50
51
    /**
52
     * @var FactoryInterface
53
     */
54
    protected $settingsFactory;
55
56
    /**
57
     * @var EventDispatcherInterface
58
     */
59
    protected $eventDispatcher;
60
61
    /**
62
     * Runtime cache for resolved parameters.
63
     *
64
     * @var Settings[]
65
     */
66
    protected $resolvedSettings = [];
67
68
    /**
69
     * SettingsManager constructor.
70
     *
71
     * @param $eventDispatcher
72
     */
73
    public function __construct(
74
        ServiceRegistryInterface $schemaRegistry,
75
        ServiceRegistryInterface $resolverRegistry,
76
        EntityManager $manager,
77
        EntityRepository $repository,
78
        FactoryInterface $settingsFactory,
79
        $eventDispatcher
80
    ) {
81
        $this->schemaRegistry = $schemaRegistry;
82
        $this->resolverRegistry = $resolverRegistry;
83
        $this->manager = $manager;
84
        $this->repository = $repository;
85
        $this->settingsFactory = $settingsFactory;
86
        $this->eventDispatcher = $eventDispatcher;
87
    }
88
89
    /**
90
     * @return AccessUrl
91
     */
92
    public function getUrl()
93
    {
94
        return $this->url;
95
    }
96
97
    public function setUrl(AccessUrl $url)
98
    {
99
        $this->url = $url;
100
    }
101
102
    public function updateSchemas(AccessUrl $url)
103
    {
104
        $this->url = $url;
105
        $schemas = array_keys($this->getSchemas());
106
107
        /**
108
         * @var string
109
         * @var \Sylius\Bundle\SettingsBundle\Schema\SchemaInterface $schema
110
         */
111
        foreach ($schemas as $schema) {
112
            $settings = $this->load($this->convertServiceToNameSpace($schema));
113
            $this->update($settings);
0 ignored issues
show
Bug introduced by
It seems like $settings can also be of type array; however, parameter $settings of Chamilo\SettingsBundle\M...ttingsManager::update() does only seem to accept Sylius\Bundle\SettingsBu...Model\SettingsInterface, maybe add an additional type check? ( Ignorable by Annotation )

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

113
            $this->update(/** @scrutinizer ignore-type */ $settings);
Loading history...
114
        }
115
    }
116
117
    public function installSchemas(AccessUrl $url)
118
    {
119
        $this->url = $url;
120
        $schemas = array_keys($this->getSchemas());
121
122
        /**
123
         * @var string
124
         * @var \Sylius\Bundle\SettingsBundle\Schema\SchemaInterface $schema
125
         */
126
        foreach ($schemas as $schema) {
127
            $settings = $this->load($this->convertServiceToNameSpace($schema));
128
            $this->save($settings);
0 ignored issues
show
Bug introduced by
It seems like $settings can also be of type array; however, parameter $settings of Chamilo\SettingsBundle\M...SettingsManager::save() does only seem to accept Sylius\Bundle\SettingsBu...Model\SettingsInterface, maybe add an additional type check? ( Ignorable by Annotation )

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

128
            $this->save(/** @scrutinizer ignore-type */ $settings);
Loading history...
129
        }
130
    }
131
132
    /**
133
     * @return array
134
     */
135
    public function getSchemas()
136
    {
137
        return $this->schemaRegistry->all();
138
    }
139
140
    /**
141
     * Get variables and categories as in 1.11.x.
142
     *
143
     * @return array
144
     */
145
    public function getVariablesAndCategories()
146
    {
147
        return [
148
            'Institution' => 'Platform',
149
            'InstitutionUrl' => 'Platform',
150
            'siteName' => 'Platform',
151
            'emailAdministrator' => 'admin', //'emailAdministrator' => 'Platform',
152
            'administratorSurname' => 'admin',
153
            'administratorTelephone' => 'admin',
154
            'administratorName' => 'admin',
155
            'show_administrator_data' => 'Platform',
156
            'show_tutor_data' => 'Session',
157
            'show_teacher_data' => 'Platform',
158
            'homepage_view' => 'Course',
159
            'show_toolshortcuts' => 'Course',
160
            'allow_group_categories' => 'Course',
161
            'server_type' => 'Platform',
162
            'platformLanguage' => 'Language',
163
            'showonline' => 'Platform',
164
            'profile' => 'User',
165
            'default_document_quotum' => 'Course',
166
            'registration' => 'User',
167
            'default_group_quotum' => 'Course',
168
            'allow_registration' => 'Platform',
169
            'allow_registration_as_teacher' => 'Platform',
170
            'allow_lostpassword' => 'Platform',
171
            'allow_user_headings' => 'Course',
172
            'allow_personal_agenda' => 'agenda',
173
            'display_coursecode_in_courselist' => 'Platform',
174
            'display_teacher_in_courselist' => 'Platform',
175
            'permanently_remove_deleted_files' => 'Tools',
176
            'dropbox_allow_overwrite' => 'Tools',
177
            'dropbox_max_filesize' => 'Tools',
178
            'dropbox_allow_just_upload' => 'Tools',
179
            'dropbox_allow_student_to_student' => 'Tools',
180
            'dropbox_allow_group' => 'Tools',
181
            'dropbox_allow_mailing' => 'Tools',
182
            'extended_profile' => 'User',
183
            'student_view_enabled' => 'Platform',
184
            'show_navigation_menu' => 'Course',
185
            'enable_tool_introduction' => 'course',
186
            'page_after_login' => 'Platform',
187
            'time_limit_whosonline' => 'Platform',
188
            'breadcrumbs_course_homepage' => 'Course',
189
            'example_material_course_creation' => 'Platform',
190
            'account_valid_duration' => 'Platform',
191
            'use_session_mode' => 'Session',
192
            'allow_email_editor' => 'Tools',
193
            //'registered' => null',
194
            //'donotlistcampus' =>'null',
195
            'show_email_addresses' => 'Platform',
196
            'service_ppt2lp' => 'NULL',
197
            'stylesheets' => 'stylesheets',
198
            'upload_extensions_list_type' => 'Security',
199
            'upload_extensions_blacklist' => 'Security',
200
            'upload_extensions_whitelist' => 'Security',
201
            'upload_extensions_skip' => 'Security',
202
            'upload_extensions_replace_by' => 'Security',
203
            'show_number_of_courses' => 'Platform',
204
            'show_empty_course_categories' => 'Platform',
205
            'show_back_link_on_top_of_tree' => 'Platform',
206
            'show_different_course_language' => 'Platform',
207
            'split_users_upload_directory' => 'Tuning',
208
            'hide_dltt_markup' => 'Languages',
209
            'display_categories_on_homepage' => 'Platform',
210
            'permissions_for_new_directories' => 'Security',
211
            'permissions_for_new_files' => 'Security',
212
            'show_tabs' => 'Platform',
213
            'default_forum_view' => 'Course',
214
            'platform_charset' => 'Languages',
215
            'noreply_email_address' => 'Platform',
216
            'survey_email_sender_noreply' => 'Course',
217
            'gradebook_enable' => 'Gradebook',
218
            'gradebook_score_display_coloring' => 'Gradebook',
219
            'gradebook_score_display_custom' => 'Gradebook',
220
            'gradebook_score_display_colorsplit' => 'Gradebook',
221
            'gradebook_score_display_upperlimit' => 'Gradebook',
222
            'gradebook_number_decimals' => 'Gradebook',
223
            'user_selected_theme' => 'Platform',
224
            'allow_course_theme' => 'Course',
225
            'show_closed_courses' => 'Platform',
226
            'extendedprofile_registration' => 'User',
227
            'extendedprofile_registrationrequired' => 'User',
228
            'add_users_by_coach' => 'Session',
229
            'extend_rights_for_coach' => 'Security',
230
            'extend_rights_for_coach_on_survey' => 'Security',
231
            'course_create_active_tools' => 'Tools',
232
            'show_session_coach' => 'Session',
233
            'allow_users_to_create_courses' => 'Platform',
234
            'allow_message_tool' => 'Tools',
235
            'allow_social_tool' => 'Tools',
236
            'allow_students_to_browse_courses' => 'Platform',
237
            'show_session_data' => 'Session',
238
            'allow_use_sub_language' => 'language',
239
            'show_glossary_in_documents' => 'Course',
240
            'allow_terms_conditions' => 'Platform',
241
            'search_enabled' => 'Search',
242
            'search_prefilter_prefix' => 'Search',
243
            'search_show_unlinked_results' => 'Search',
244
            'show_courses_descriptions_in_catalog' => 'Course',
245
            'allow_coach_to_edit_course_session' => 'Session',
246
            'show_glossary_in_extra_tools' => 'Course',
247
            'send_email_to_admin_when_create_course' => 'Platform',
248
            'go_to_course_after_login' => 'Course',
249
            'math_asciimathML' => 'Editor',
250
            'enabled_asciisvg' => 'Editor',
251
            'include_asciimathml_script' => 'Editor',
252
            'youtube_for_students' => 'Editor',
253
            'block_copy_paste_for_students' => 'Editor',
254
            'more_buttons_maximized_mode' => 'Editor',
255
            'students_download_folders' => 'Document',
256
            'users_copy_files' => 'Tools',
257
            'allow_students_to_create_groups_in_social' => 'Tools',
258
            'allow_send_message_to_all_platform_users' => 'Message',
259
            'message_max_upload_filesize' => 'Tools',
260
            'use_users_timezone' => 'profile', //'use_users_timezone' => 'Timezones',
261
            'timezone_value' => 'platform', //'timezone_value' => 'Timezones',
262
            'allow_user_course_subscription_by_course_admin' => 'Security',
263
            'show_link_bug_notification' => 'Platform',
264
            'show_link_ticket_notification' => 'Platform',
265
            'course_validation' => 'course', //'course_validation' => 'Platform',
266
            'course_validation_terms_and_conditions_url' => 'Platform',
267
            'enabled_wiris' => 'Editor',
268
            'allow_spellcheck' => 'Editor',
269
            'force_wiki_paste_as_plain_text' => 'Editor',
270
            'enabled_googlemaps' => 'Editor',
271
            'enabled_imgmap' => 'Editor',
272
            'enabled_support_svg' => 'Tools',
273
            'pdf_export_watermark_enable' => 'Platform',
274
            'pdf_export_watermark_by_course' => 'Platform',
275
            'pdf_export_watermark_text' => 'Platform',
276
            'enabled_insertHtml' => 'Editor',
277
            'students_export2pdf' => 'Document',
278
            'exercise_min_score' => 'Course',
279
            'exercise_max_score' => 'Course',
280
            'show_users_folders' => 'Tools',
281
            'show_default_folders' => 'Tools',
282
            'show_chat_folder' => 'Tools',
283
            'enabled_text2audio' => 'Tools',
284
            'course_hide_tools' => 'Course',
285
            'enabled_support_pixlr' => 'Tools',
286
            'show_groups_to_users' => 'Session',
287
            'accessibility_font_resize' => 'Platform',
288
            'hide_courses_in_sessions' => 'Session',
289
            'enable_quiz_scenario' => 'Course',
290
            'filter_terms' => 'Security',
291
            'header_extra_content' => 'Tracking',
292
            'footer_extra_content' => 'Tracking',
293
            'show_documents_preview' => 'Tools',
294
            'htmlpurifier_wiki' => 'Editor',
295
            'cas_activate' => 'CAS',
296
            'cas_server' => 'CAS',
297
            'cas_server_uri' => 'CAS',
298
            'cas_port' => 'CAS',
299
            'cas_protocol' => 'CAS',
300
            'cas_add_user_activate' => 'CAS',
301
            'update_user_info_cas_with_ldap' => 'CAS',
302
            'student_page_after_login' => 'Platform',
303
            'teacher_page_after_login' => 'Platform',
304
            'drh_page_after_login' => 'Platform',
305
            'sessionadmin_page_after_login' => 'Session',
306
            'student_autosubscribe' => 'Platform',
307
            'teacher_autosubscribe' => 'Platform',
308
            'drh_autosubscribe' => 'Platform',
309
            'sessionadmin_autosubscribe' => 'Session',
310
            'scorm_cumulative_session_time' => 'Course',
311
            'allow_hr_skills_management' => 'Gradebook',
312
            'enable_help_link' => 'Platform',
313
            'teachers_can_change_score_settings' => 'Gradebook',
314
            'allow_users_to_change_email_with_no_password' => 'User',
315
            'show_admin_toolbar' => 'display',
316
            'allow_global_chat' => 'Platform',
317
            'languagePriority1' => 'language',
318
            'languagePriority2' => 'language',
319
            'languagePriority3' => 'language',
320
            'languagePriority4' => 'language',
321
            'login_is_email' => 'Platform',
322
            'courses_default_creation_visibility' => 'Course',
323
            'gradebook_enable_grade_model' => 'Gradebook',
324
            'teachers_can_change_grade_model_settings' => 'Gradebook',
325
            'gradebook_default_weight' => 'Gradebook',
326
            'ldap_description' => 'LDAP',
327
            'shibboleth_description' => 'Shibboleth',
328
            'facebook_description' => 'Facebook',
329
            'gradebook_locking_enabled' => 'Gradebook',
330
            'gradebook_default_grade_model_id' => 'Gradebook',
331
            'allow_session_admins_to_manage_all_sessions' => 'Session',
332
            'allow_skills_tool' => 'Platform',
333
            'allow_public_certificates' => 'Course',
334
            'platform_unsubscribe_allowed' => 'Platform',
335
            'enable_iframe_inclusion' => 'Editor',
336
            'show_hot_courses' => 'Platform',
337
            'enable_webcam_clip' => 'Tools',
338
            'use_custom_pages' => 'Platform',
339
            'tool_visible_by_default_at_creation' => 'Tools',
340
            'prevent_session_admins_to_manage_all_users' => 'Session',
341
            'documents_default_visibility_defined_in_course' => 'Tools',
342
            'enabled_mathjax' => 'Editor',
343
            'meta_twitter_site' => 'Tracking',
344
            'meta_twitter_creator' => 'Tracking',
345
            'meta_title' => 'Tracking',
346
            'meta_description' => 'Tracking',
347
            'meta_image_path' => 'Tracking',
348
            'allow_teachers_to_create_sessions' => 'Session',
349
            'institution_address' => 'Platform',
350
            'chamilo_database_version' => 'null',
351
            'cron_remind_course_finished_activate' => 'Crons',
352
            'cron_remind_course_expiration_frequency' => 'Crons',
353
            'cron_remind_course_expiration_activate' => 'Crons',
354
            'allow_coach_feedback_exercises' => 'Session',
355
            'allow_my_files' => 'Platform',
356
            'ticket_allow_student_add' => 'Ticket',
357
            'ticket_send_warning_to_all_admins' => 'Ticket',
358
            'ticket_warn_admin_no_user_in_category' => 'Ticket',
359
            'ticket_allow_category_edition' => 'Ticket',
360
            'load_term_conditions_section' => 'Platform',
361
            'show_terms_if_profile_completed' => 'Ticket',
362
            'hide_home_top_when_connected' => 'Platform',
363
            'hide_global_announcements_when_not_connected' => 'Platform',
364
            'course_creation_use_template' => 'Course',
365
            'allow_strength_pass_checker' => 'Security',
366
            'allow_captcha' => 'Security',
367
            'captcha_number_mistakes_to_block_account' => 'Security',
368
            'captcha_time_to_block' => 'Security',
369
            'drh_can_access_all_session_content' => 'Session',
370
            'display_groups_forum_in_general_tool' => 'Tools',
371
            'allow_tutors_to_assign_students_to_session' => 'Session',
372
            'allow_lp_return_link' => 'Course',
373
            'hide_scorm_export_link' => 'Course',
374
            'hide_scorm_copy_link' => 'Course',
375
            'hide_scorm_pdf_link' => 'Course',
376
            'session_days_before_coach_access' => 'Session',
377
            'session_days_after_coach_access' => 'Session',
378
            'pdf_logo_header' => 'Course',
379
            'order_user_list_by_official_code' => 'Platform',
380
            'email_alert_manager_on_new_quiz' => 'exercise',
381
            'show_official_code_exercise_result_list' => 'Tools',
382
            'course_catalog_hide_private' => 'Platform',
383
            'catalog_show_courses_sessions' => 'Platform',
384
            'auto_detect_language_custom_pages' => 'Platform',
385
            'lp_show_reduced_report' => 'Course',
386
            'allow_session_course_copy_for_teachers' => 'Session',
387
            'hide_logout_button' => 'Platform',
388
            'redirect_admin_to_courses_list' => 'Platform',
389
            'course_images_in_courses_list' => 'Course',
390
            'student_publication_to_take_in_gradebook' => 'Gradebook',
391
            'certificate_filter_by_official_code' => 'Gradebook',
392
            'exercise_max_ckeditors_in_page' => 'Tools',
393
            'document_if_file_exists_option' => 'Tools',
394
            'add_gradebook_certificates_cron_task_enabled' => 'Gradebook',
395
            'openbadges_backpack' => 'Gradebook',
396
            'cookie_warning' => 'Tools',
397
            'hide_course_group_if_no_tools_available' => 'Tools',
398
            'catalog_allow_session_auto_subscription' => 'Session',
399
            'registration.soap.php.decode_utf8' => 'Platform',
400
            'allow_delete_attendance' => 'Tools',
401
            'gravatar_enabled' => 'Platform',
402
            'gravatar_type' => 'Platform',
403
            'limit_session_admin_role' => 'Session',
404
            'show_session_description' => 'Session',
405
            'hide_certificate_export_link_students' => 'Gradebook',
406
            'hide_certificate_export_link' => 'Gradebook',
407
            'dropbox_hide_course_coach' => 'Tools',
408
            'dropbox_hide_general_coach' => 'Tools',
409
            'session_course_ordering' => 'Session',
410
            'gamification_mode' => 'Platform',
411
            'prevent_multiple_simultaneous_login' => 'Security',
412
            'gradebook_detailed_admin_view' => 'Gradebook',
413
            'course_catalog_published' => 'Course',
414
            'user_reset_password' => 'Security',
415
            'user_reset_password_token_limit' => 'Security',
416
            'my_courses_view_by_session' => 'Session',
417
            'show_full_skill_name_on_skill_wheel' => 'Platform',
418
            'messaging_allow_send_push_notification' => 'WebServices',
419
            'messaging_gdc_project_number' => 'WebServices',
420
            'messaging_gdc_api_key' => 'WebServices',
421
            'teacher_can_select_course_template' => 'Course',
422
            'enable_record_audio' => 'Tools',
423
            'allow_show_skype_account' => 'Platform',
424
            'allow_show_linkedin_url' => 'Platform',
425
            'enable_profile_user_address_geolocalization' => 'User',
426
            'show_official_code_whoisonline' => 'Profile',
427
            'icons_mode_svg' => 'display',
428
            'user_name_order' => 'display',
429
            'user_name_sort_by' => 'display',
430
            'default_calendar_view' => 'agenda',
431
            'exercise_invisible_in_session' => 'exercise',
432
            'configure_exercise_visibility_in_course' => 'exercise',
433
            'allow_download_documents_by_api_key' => 'Webservices',
434
            'ProfilingFilterAddingUsers' => 'profile',
435
            'donotlistcampus' => 'platform',
436
            'gradebook_show_percentage_in_reports' => 'gradebook',
437
            'course_creation_splash_screen' => 'Course',
438
        ];
439
    }
440
441
    /**
442
     * Rename old variable with variable used in Chamilo 2.0.
443
     *
444
     * @param string $variable
445
     *
446
     * @return mixed
447
     */
448
    public function renameVariable($variable)
449
    {
450
        $list = [
451
            'timezone_value' => 'timezone',
452
            'Institution' => 'institution',
453
            'SiteName' => 'site_name',
454
            'siteName' => 'site_name',
455
            'InstitutionUrl' => 'institution_url',
456
            'registration' => 'required_profile_fields',
457
            'stylesheets' => 'theme',
458
            'platformLanguage' => 'platform_language',
459
            'languagePriority1' => 'language_priority_1',
460
            'languagePriority2' => 'language_priority_2',
461
            'languagePriority3' => 'language_priority_3',
462
            'languagePriority4' => 'language_priority_4',
463
            'gradebook_score_display_coloring' => 'my_display_coloring',
464
            'document_if_file_exists_option' => 'if_file_exists_option',
465
            'ProfilingFilterAddingUsers' => 'profiling_filter_adding_users',
466
            'course_create_active_tools' => 'active_tools_on_create',
467
            'emailAdministrator' => 'administrator_email',
468
            'administratorSurname' => 'administrator_surname',
469
            'administratorName' => 'administrator_name',
470
            'administratorTelephone' => 'administrator_phone',
471
            'registration.soap.php.decode_utf8' => 'decode_utf8',
472
            'profile' => 'changeable_options',
473
        ];
474
475
        return isset($list[$variable]) ? $list[$variable] : $variable;
476
    }
477
478
    /**
479
     * Replace old Chamilo 1.x category with 2.0 version.
480
     *
481
     * @param string $variable
482
     * @param string $defaultCategory
483
     *
484
     * @return mixed
485
     */
486
    public function fixCategory($variable, $defaultCategory)
487
    {
488
        $settings = [
489
            'cookie_warning' => 'platform',
490
            'donotlistcampus' => 'platform',
491
            'administrator_email' => 'admin',
492
            'administrator_surname' => 'admin',
493
            'administrator_name' => 'admin',
494
            'administrator_phone' => 'admin',
495
            'exercise_max_ckeditors_in_page' => 'exercise',
496
            'allow_hr_skills_management' => 'skill',
497
            'accessibility_font_resize' => 'display',
498
            'account_valid_duration' => 'profile',
499
            'allow_global_chat' => 'chat',
500
            'allow_lostpassword' => 'registration',
501
            'allow_registration' => 'registration',
502
            'allow_registration_as_teacher' => 'registration',
503
            'required_profile_fields' => 'registration',
504
            'allow_skills_tool' => 'skill',
505
            'allow_students_to_browse_courses' => 'display',
506
            'allow_terms_conditions' => 'registration',
507
            'allow_users_to_create_courses' => 'course',
508
            'auto_detect_language_custom_pages' => 'language',
509
            'platform_language' => 'language',
510
            'course_validation' => 'course',
511
            'course_validation_terms_and_conditions_url' => 'course',
512
            'display_categories_on_homepage' => 'display',
513
            'display_coursecode_in_courselist' => 'course',
514
            'display_teacher_in_courselist' => 'course',
515
            'drh_autosubscribe' => 'registration',
516
            'drh_page_after_login' => 'registration',
517
            'enable_help_link' => 'display',
518
            'example_material_course_creation' => 'course',
519
            'login_is_email' => 'profile',
520
            'noreply_email_address' => 'mail',
521
            'page_after_login' => 'registration',
522
            'pdf_export_watermark_by_course' => 'document',
523
            'pdf_export_watermark_enable' => 'document',
524
            'pdf_export_watermark_text' => 'document',
525
            'platform_unsubscribe_allowed' => 'registration',
526
            'send_email_to_admin_when_create_course' => 'course',
527
            'show_admin_toolbar' => 'display',
528
            'show_administrator_data' => 'display',
529
            'show_back_link_on_top_of_tree' => 'display',
530
            'show_closed_courses' => 'display',
531
            'show_different_course_language' => 'display',
532
            'show_email_addresses' => 'display',
533
            'show_empty_course_categories' => 'display',
534
            'show_full_skill_name_on_skill_wheel' => 'skill',
535
            'show_hot_courses' => 'display',
536
            'show_link_bug_notification' => 'display',
537
            'show_number_of_courses' => 'display',
538
            'show_teacher_data' => 'display',
539
            'showonline' => 'display',
540
            'student_autosubscribe' => 'registration',
541
            'student_page_after_login' => 'registration',
542
            'student_view_enabled' => 'course',
543
            'teacher_autosubscribe' => 'registration',
544
            'teacher_page_after_login' => 'registration',
545
            'time_limit_whosonline' => 'display',
546
            'user_selected_theme' => 'profile',
547
            'hide_global_announcements_when_not_connected' => 'announcement',
548
            'hide_home_top_when_connected' => 'display',
549
            'hide_logout_button' => 'display',
550
            'institution_address' => 'platform',
551
            'redirect_admin_to_courses_list' => 'admin',
552
            'use_custom_pages' => 'platform',
553
            'allow_group_categories' => 'group',
554
            'allow_user_headings' => 'display',
555
            'default_document_quotum' => 'document',
556
            'default_forum_view' => 'forum',
557
            'default_group_quotum' => 'document',
558
            'enable_quiz_scenario' => 'exercise',
559
            'exercise_max_score' => 'exercise',
560
            'exercise_min_score' => 'exercise',
561
            'pdf_logo_header' => 'platform',
562
            'show_glossary_in_documents' => 'document',
563
            'show_glossary_in_extra_tools' => 'glossary',
564
            'survey_email_sender_noreply' => 'survey',
565
            'allow_coach_feedback_exercises' => 'exercise',
566
            'sessionadmin_autosubscribe' => 'registration',
567
            'sessionadmin_page_after_login' => 'registration',
568
            'show_tutor_data' => 'display',
569
            'allow_social_tool' => 'social',
570
            'allow_message_tool' => 'message',
571
            'allow_email_editor' => 'editor',
572
            'show_link_ticket_notification' => 'display',
573
            'permissions_for_new_directories' => 'document',
574
            'enable_profile_user_address_geolocalization' => 'profile',
575
            'allow_show_skype_account' => 'profile',
576
            'allow_show_linkedin_url' => 'profile',
577
            'allow_students_to_create_groups_in_social' => 'social',
578
            'default_calendar_view' => 'agenda',
579
            'documents_default_visibility_defined_in_course' => 'document',
580
            'message_max_upload_filesize' => 'message',
581
            'course_create_active_tools' => 'course',
582
            'tool_visible_by_default_at_creation' => 'document',
583
            'show_users_folders' => 'document',
584
            'show_default_folders' => 'document',
585
            'show_chat_folder' => 'chat',
586
            'enabled_support_svg' => 'editor',
587
            'enabled_support_pixlr' => 'editor',
588
            'enable_webcam_clip' => 'document',
589
            'enable_record_audio' => 'course',
590
            'enabled_text2audio' => 'document',
591
            'permanently_remove_deleted_files' => 'document',
592
            'allow_delete_attendance' => 'attendance',
593
            'display_groups_forum_in_general_tool' => 'forum',
594
            'dropbox_allow_overwrite' => 'dropbox',
595
            'allow_user_course_subscription_by_course_admin' => 'course',
596
            'hide_course_group_if_no_tools_available' => 'group',
597
            'extend_rights_for_coach_on_survey' => 'survey',
598
            'show_official_code_exercise_result_list' => 'exercise',
599
            'dropbox_max_filesize' => 'dropbox',
600
            'dropbox_allow_just_upload' => 'dropbox',
601
            'dropbox_allow_student_to_student' => 'dropbox',
602
            'dropbox_allow_group' => 'dropbox',
603
            'dropbox_allow_mailing' => 'dropbox',
604
            'upload_extensions_list_type' => 'document',
605
            'upload_extensions_blacklist' => 'document',
606
            'upload_extensions_skip' => 'document',
607
            'changeable_options' => 'profile',
608
            'users_copy_files' => 'document',
609
            'if_file_exists_option' => 'document',
610
            'permissions_for_new_files' => 'document',
611
            'extended_profile' => 'profile',
612
            'split_users_upload_directory' => 'profile',
613
            'show_documents_preview' => 'document',
614
            'decode_utf8' => 'webservice',
615
            'messaging_allow_send_push_notification' => 'webservice',
616
            'messaging_gdc_project_number' => 'webservice',
617
            'messaging_gdc_api_key' => 'webservice',
618
            'allow_download_documents_by_api_key' => 'webservice',
619
            'profiling_filter_adding_users' => 'profile',
620
            'hide_dltt_markup' => 'language',
621
            'active_tools_on_create' => 'course',
622
        ];
623
624
        return isset($settings[$variable]) ? $settings[$variable] : $defaultCategory;
625
    }
626
627
    /**
628
     * @param string $name
629
     *
630
     * @throws \InvalidArgumentException
631
     *
632
     * @return mixed
633
     */
634
    public function getSetting($name)
635
    {
636
        if (false === strpos($name, '.')) {
637
            //throw new \InvalidArgumentException(sprintf('Parameter must be in format "namespace.name", "%s" given.', $name));
638
639
            // This code allows the possibility of calling
640
            // api_get_setting('allow_skills_tool') instead of
641
            // the "correct" way api_get_setting('platform.allow_skills_tool')
642
            $items = $this->getVariablesAndCategories();
643
644
            if (isset($items[$name])) {
645
                $originalName = $name;
646
                $name = $this->renameVariable($name);
647
                $category = $this->fixCategory(
648
                    strtolower($name),
649
                    strtolower($items[$originalName])
650
                );
651
                $name = $category.'.'.$name;
652
            } else {
653
                throw new \InvalidArgumentException(sprintf('Parameter must be in format "category.name", "%s" given.', $name));
654
            }
655
        }
656
657
        list($category, $name) = explode('.', $name);
658
        $settings = $this->load($category, $name);
659
660
        if (!$settings) {
661
            throw new \InvalidArgumentException(sprintf("Parameter '$name' not found in category '$category'"));
662
        }
663
664
        return $settings->get($name);
665
    }
666
667
    /**
668
     * @param string $category
669
     *
670
     * @return string
671
     */
672
    public function convertNameSpaceToService($category)
673
    {
674
        return 'chamilo_core.settings.'.$category;
675
    }
676
677
    /**
678
     * @param string $category
679
     *
680
     * @return string
681
     */
682
    public function convertServiceToNameSpace($category)
683
    {
684
        return str_replace('chamilo_core.settings.', '', $category);
685
    }
686
687
    /**
688
     * {@inheritdoc}
689
     */
690
    public function load($schemaAlias, $namespace = null, $ignoreUnknown = true)
691
    {
692
        $schemaAliasNoPrefix = $schemaAlias;
693
        $schemaAlias = 'chamilo_core.settings.'.$schemaAlias;
694
695
        if ($this->schemaRegistry->has($schemaAlias)) {
696
            /** @var SchemaInterface $schema */
697
            $schema = $this->schemaRegistry->get($schemaAlias);
698
        } else {
699
            return [];
700
        }
701
702
        /** @var \Sylius\Bundle\SettingsBundle\Model\Settings $settings */
703
        $settings = $this->settingsFactory->createNew();
704
        $settings->setSchemaAlias($schemaAlias);
705
706
        // We need to get a plain parameters array since we use the options resolver on it
707
        $parameters = $this->getParameters($schemaAliasNoPrefix);
708
        $settingsBuilder = new SettingsBuilder();
709
        $schema->buildSettings($settingsBuilder);
710
711
        // Remove unknown settings' parameters (e.g. From a previous version of the settings schema)
712
        if (true === $ignoreUnknown) {
713
            foreach ($parameters as $name => $value) {
714
                if (!$settingsBuilder->isDefined($name)) {
715
                    unset($parameters[$name]);
716
                }
717
            }
718
        }
719
720
        foreach ($settingsBuilder->getTransformers() as $parameter => $transformer) {
721
            if (array_key_exists($parameter, $parameters)) {
722
                if ('course_creation_use_template' === $parameter) {
723
                    if (empty($parameters[$parameter])) {
724
                        $parameters[$parameter] = null;
725
                    }
726
                } else {
727
                    $parameters[$parameter] = $transformer->reverseTransform($parameters[$parameter]);
728
                }
729
            }
730
        }
731
732
        $parameters = $settingsBuilder->resolve($parameters);
733
        $settings->setParameters($parameters);
734
735
        return $settings;
736
    }
737
738
    /**
739
     * @throws \Doctrine\ORM\ORMException
740
     * @throws \Doctrine\ORM\OptimisticLockException
741
     */
742
    public function update(SettingsInterface $settings)
743
    {
744
        $namespace = $settings->getSchemaAlias();
745
746
        /** @var SchemaInterface $schema */
747
        $schema = $this->schemaRegistry->get($settings->getSchemaAlias());
748
749
        $settingsBuilder = new SettingsBuilder();
750
        $schema->buildSettings($settingsBuilder);
751
        $parameters = $settingsBuilder->resolve($settings->getParameters());
752
        // Transform value. Example array to string using transformer. Example:
753
        // 1. Setting "tool_visible_by_default_at_creation" it's a multiple select
754
        // 2. Is defined as an array in class DocumentSettingsSchema
755
        // 3. Add transformer for that variable "ArrayToIdentifierTransformer"
756
        // 4. Here we recover the transformer and convert the array to string
757
        foreach ($settingsBuilder->getTransformers() as $parameter => $transformer) {
758
            if (array_key_exists($parameter, $parameters)) {
759
                $parameters[$parameter] = $transformer->transform($parameters[$parameter]);
760
            }
761
        }
762
        $settings->setParameters($parameters);
763
        $persistedParameters = $this->repository->findBy(
764
            ['category' => $this->convertServiceToNameSpace($settings->getSchemaAlias())]
765
        );
766
767
        $persistedParametersMap = [];
768
        /** @var SettingsCurrent $parameter */
769
        foreach ($persistedParameters as $parameter) {
770
            $persistedParametersMap[$parameter->getVariable()] = $parameter;
771
        }
772
773
        /** @var SettingsCurrent $url */
774
        $url = $this->getUrl();
775
        $simpleCategoryName = str_replace('chamilo_core.settings.', '', $namespace);
776
777
        foreach ($parameters as $name => $value) {
778
            if (isset($persistedParametersMap[$name])) {
779
                $parameter = $persistedParametersMap[$name];
780
                $parameter->setSelectedValue($value);
781
                $parameter->setCategory($simpleCategoryName);
782
                $this->manager->merge($parameter);
783
            } else {
784
                $parameter = new SettingsCurrent();
785
                $parameter
786
                    ->setVariable($name)
787
                    ->setCategory($simpleCategoryName)
788
                    ->setTitle($name)
789
                    ->setSelectedValue($value)
790
                    ->setUrl($url)
791
                    ->setAccessUrlChangeable(1)
792
                    ->setAccessUrlLocked(1)
793
                ;
794
795
                /** @var ConstraintViolationListInterface $errors */
796
                /*$errors = $this->validator->validate($parameter);
797
                if (0 < $errors->count()) {
798
                    throw new ValidatorException($errors->get(0)->getMessage());
799
                }*/
800
                $this->manager->persist($parameter);
801
            }
802
        }
803
804
        $this->manager->flush();
805
    }
806
807
    /**
808
     * {@inheritdoc}
809
     *
810
     * @throws ValidatorException
811
     */
812
    public function save(SettingsInterface $settings)
813
    {
814
        $namespace = $settings->getSchemaAlias();
815
816
        /** @var SchemaInterface $schema */
817
        $schema = $this->schemaRegistry->get($settings->getSchemaAlias());
818
819
        $settingsBuilder = new SettingsBuilder();
820
        $schema->buildSettings($settingsBuilder);
821
        $parameters = $settingsBuilder->resolve($settings->getParameters());
822
        // Transform value. Example array to string using transformer. Example:
823
        // 1. Setting "tool_visible_by_default_at_creation" it's a multiple select
824
        // 2. Is defined as an array in class DocumentSettingsSchema
825
        // 3. Add transformer for that variable "ArrayToIdentifierTransformer"
826
        // 4. Here we recover the transformer and convert the array to string
827
        foreach ($settingsBuilder->getTransformers() as $parameter => $transformer) {
828
            if (array_key_exists($parameter, $parameters)) {
829
                $parameters[$parameter] = $transformer->transform($parameters[$parameter]);
830
            }
831
        }
832
        $settings->setParameters($parameters);
833
        $persistedParameters = $this->repository->findBy(
834
            ['category' => $this->convertServiceToNameSpace($settings->getSchemaAlias())]
835
        );
836
        $persistedParametersMap = [];
837
838
        foreach ($persistedParameters as $parameter) {
839
            $persistedParametersMap[$parameter->getTitle()] = $parameter;
840
        }
841
842
        /** @var SettingsEvent $event */
843
        /*$event = $this->eventDispatcher->dispatch(
844
            SettingsEvent::PRE_SAVE,
845
            new SettingsEvent($settings, $parameters)
846
        );*/
847
848
        /** @var SettingsCurrent $url */
849
        $url = $this->getUrl();
850
        $simpleCategoryName = str_replace('chamilo_core.settings.', '', $namespace);
851
852
        foreach ($parameters as $name => $value) {
853
            if (isset($persistedParametersMap[$name])) {
854
                $parameter = $persistedParametersMap[$name];
855
                $parameter->setSelectedValue($value);
856
            } else {
857
                $parameter = new SettingsCurrent();
858
                $parameter
859
                    ->setVariable($name)
860
                    ->setCategory($simpleCategoryName)
861
                    ->setTitle($name)
862
                    ->setSelectedValue($value)
863
                    ->setUrl($url)
864
                    ->setAccessUrlChangeable(1)
865
                    ->setAccessUrlLocked(1)
866
                ;
867
868
                /** @var ConstraintViolationListInterface $errors */
869
                /*$errors = $this->validator->validate($parameter);
870
                if (0 < $errors->count()) {
871
                    throw new ValidatorException($errors->get(0)->getMessage());
872
                }*/
873
                $this->manager->persist($parameter);
874
            }
875
            $this->manager->persist($parameter);
876
        }
877
878
        $this->manager->flush();
879
880
        return;
881
882
        ////
883
        $schemaAlias = $settings->getSchemaAlias();
0 ignored issues
show
Unused Code introduced by
$schemaAlias = $settings->getSchemaAlias() is not reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
884
        $schemaAliasChamilo = str_replace('chamilo_core.settings.', '', $schemaAlias);
885
886
        $schema = $this->schemaRegistry->get($schemaAlias);
887
888
        $settingsBuilder = new SettingsBuilder();
889
        $schema->buildSettings($settingsBuilder);
890
891
        $parameters = $settingsBuilder->resolve($settings->getParameters());
892
893
        foreach ($settingsBuilder->getTransformers() as $parameter => $transformer) {
894
            if (array_key_exists($parameter, $parameters)) {
895
                $parameters[$parameter] = $transformer->transform($parameters[$parameter]);
896
            }
897
        }
898
899
        /** @var \Sylius\Bundle\SettingsBundle\Event\SettingsEvent $event */
900
        $event = $this->eventDispatcher->dispatch(
901
            SettingsEvent::PRE_SAVE,
902
            new SettingsEvent($settings)
903
        );
904
905
        /** @var SettingsCurrent $url */
906
        $url = $event->getSettings()->getAccessUrl();
907
908
        foreach ($parameters as $name => $value) {
909
            if (isset($persistedParametersMap[$name])) {
910
                if ($value instanceof Course) {
911
                    $value = $value->getId();
912
                }
913
                $persistedParametersMap[$name]->setValue($value);
914
            } else {
915
                /** @var SettingsCurrent $setting */
916
                $setting = $this->settingsFactory->createNew();
917
                $setting->setSchemaAlias($schemaAlias);
918
919
                $setting
920
                    ->setNamespace($schemaAliasChamilo)
921
                    ->setName($name)
922
                    ->setValue($value)
923
                    ->setUrl($url)
924
                    ->setAccessUrlLocked(0)
925
                    ->setAccessUrlChangeable(1)
926
                ;
927
928
                /** @var ConstraintViolationListInterface $errors */
929
                /*$errors = $this->->validate($parameter);
930
                if (0 < $errors->count()) {
931
                    throw new ValidatorException($errors->get(0)->getMessage());
932
                }*/
933
                $this->manager->persist($setting);
934
                $this->manager->flush();
935
            }
936
        }
937
        /*$parameters = $settingsBuilder->resolve($settings->getParameters());
938
        $settings->setParameters($parameters);
939
940
        $this->eventDispatcher->dispatch(SettingsEvent::PRE_SAVE, new SettingsEvent($settings));
941
942
        $this->manager->persist($settings);
943
        $this->manager->flush();
944
945
        $this->eventDispatcher->dispatch(SettingsEvent::POST_SAVE, new SettingsEvent($settings));*/
946
    }
947
948
    /**
949
     * @param string $keyword
950
     *
951
     * @return array
952
     */
953
    public function getParametersFromKeywordOrderedByCategory($keyword)
954
    {
955
        $query = $this->repository->createQueryBuilder('s')
956
            ->where('s.variable LIKE :keyword')
957
            ->setParameter('keyword', "%$keyword%")
958
        ;
959
        $parametersFromDb = $query->getQuery()->getResult();
960
        $parameters = [];
961
        /** @var \Chamilo\CoreBundle\Entity\SettingsCurrent $parameter */
962
        foreach ($parametersFromDb as $parameter) {
963
            $parameters[$parameter->getCategory()][] = $parameter;
964
        }
965
966
        return $parameters;
967
    }
968
969
    /**
970
     * @param string $namespace
971
     * @param string $keyword
972
     * @param bool   $returnObjects
973
     *
974
     * @return array
975
     */
976
    public function getParametersFromKeyword($namespace, $keyword = '', $returnObjects = false)
977
    {
978
        if (empty($keyword)) {
979
            $criteria = ['category' => $namespace];
980
            $parametersFromDb = $this->repository->findBy($criteria);
981
        } else {
982
            $query = $this->repository->createQueryBuilder('s')
983
                ->where('s.variable LIKE :keyword')
984
                ->setParameter('keyword', "%$keyword%")
985
            ;
986
            $parametersFromDb = $query->getQuery()->getResult();
987
        }
988
989
        if ($returnObjects) {
990
            return $parametersFromDb;
991
        }
992
        $parameters = [];
993
        /** @var \Chamilo\CoreBundle\Entity\SettingsCurrent $parameter */
994
        foreach ($parametersFromDb as $parameter) {
995
            $parameters[$parameter->getVariable()] = $parameter->getSelectedValue();
996
        }
997
998
        return $parameters;
999
    }
1000
1001
    /**
1002
     * Load parameter from database.
1003
     *
1004
     * @param string $namespace
1005
     *
1006
     * @return array
1007
     */
1008
    private function getParameters($namespace)
1009
    {
1010
        $parameters = [];
1011
        /** @var SettingsCurrent $parameter */
1012
        foreach ($this->repository->findBy(['category' => $namespace]) as $parameter) {
1013
            $parameters[$parameter->getVariable()] = $parameter->getSelectedValue();
1014
        }
1015
1016
        return $parameters;
1017
    }
1018
1019
    /*private function transformParameters(SettingsBuilder $settingsBuilder, array $parameters)
1020
    {
1021
        $transformedParameters = $parameters;
1022
1023
        foreach ($settingsBuilder->getTransformers() as $parameter => $transformer) {
1024
            if (array_key_exists($parameter, $parameters)) {
1025
                $transformedParameters[$parameter] = $transformer->reverseTransform($parameters[$parameter]);
1026
            }
1027
        }
1028
1029
        return $transformedParameters;
1030
    }*/
1031
}
1032