Completed
Push — master ( c238bb...d3c2be )
by Julito
53:04 queued 30:32
created

SettingsManager::getParametersFromKeyword()   B

Complexity

Conditions 4
Paths 6

Size

Total Lines 23
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 14
nc 6
nop 3
dl 0
loc 23
rs 8.7972
c 0
b 0
f 0
1
<?php
2
/* For licensing terms, see /license.txt */
3
4
namespace Chamilo\SettingsBundle\Manager;
5
6
use Chamilo\CoreBundle\Entity\AccessUrl;
7
use Chamilo\CoreBundle\Entity\Course;
8
use Chamilo\CoreBundle\Settings\PlatformSettingsSchema;
9
use Doctrine\ORM\EntityManager;
10
use Doctrine\ORM\EntityRepository;
11
use Sylius\Bundle\ResourceBundle\Controller\EventDispatcherInterface;
12
use Sylius\Bundle\SettingsBundle\Event\SettingsEvent;
13
use Doctrine\Common\Persistence\ObjectManager;
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\Resolver\SettingsResolverInterface;
18
use Sylius\Bundle\SettingsBundle\Schema\SchemaInterface;
19
use Sylius\Bundle\SettingsBundle\Schema\SchemaRegistryInterface;
20
use Sylius\Bundle\SettingsBundle\Schema\SettingsBuilder;
21
use Sylius\Component\Registry\ServiceRegistryInterface;
22
use Sylius\Component\Resource\Factory\FactoryInterface;
23
use Symfony\Component\Validator\ConstraintViolationListInterface;
24
use Symfony\Component\Validator\Exception\ValidatorException;
25
use Sylius\Bundle\SettingsBundle\Manager\SettingsManager as SyliusSettingsManager;
26
use Chamilo\CoreBundle\Entity\SettingsCurrent;
27
28
/**
29
 * Class SettingsManager
30
 * @package Chamilo\SettingsBundle\Manager
31
 */
32
class SettingsManager implements SettingsManagerInterface
33
{
34
    protected $url;
35
36
    /**
37
     * @var ServiceRegistryInterface
38
     */
39
    protected $schemaRegistry;
40
41
    /**
42
     * @var ServiceRegistryInterface
43
     */
44
    protected $resolverRegistry;
45
46
    /**
47
     * @var EntityManager
48
     */
49
    protected $manager;
50
51
    /**
52
     * @var EntityRepository
53
     */
54
    protected $repository;
55
56
    /**
57
     * @var FactoryInterface
58
     */
59
    protected $settingsFactory;
60
61
    /**
62
     * @var EventDispatcherInterface
63
     */
64
    protected $eventDispatcher;
65
66
    /**
67
     * Runtime cache for resolved parameters
68
     *
69
     * @var Settings[]
70
     */
71
    protected $resolvedSettings = [];
72
73
    /**
74
     * SettingsManager constructor.
75
     * @param ServiceRegistryInterface $schemaRegistry
76
     * @param ServiceRegistryInterface $resolverRegistry
77
     * @param EntityManager $manager
78
     * @param EntityRepository $repository
79
     * @param FactoryInterface $settingsFactory
80
     * @param $eventDispatcher
81
     */
82
    public function __construct(
83
        ServiceRegistryInterface $schemaRegistry,
84
        ServiceRegistryInterface $resolverRegistry,
85
        EntityManager $manager,
86
        EntityRepository $repository,
87
        FactoryInterface $settingsFactory,
88
        $eventDispatcher
89
    ) {
90
        $this->schemaRegistry = $schemaRegistry;
91
        $this->resolverRegistry = $resolverRegistry;
92
        $this->manager = $manager;
93
        $this->repository = $repository;
94
        $this->settingsFactory = $settingsFactory;
95
        $this->eventDispatcher = $eventDispatcher;
96
    }
97
98
    /**
99
     * @return AccessUrl
100
     */
101
    public function getUrl()
102
    {
103
        return $this->url;
104
    }
105
106
    /**
107
     * @param AccessUrl $url
108
     */
109
    public function setUrl(AccessUrl $url)
110
    {
111
        $this->url = $url;
112
    }
113
114
    /**
115
     * @param AccessUrl $url
116
     */
117
    public function installSchemas(AccessUrl $url)
118
    {
119
        $this->url = $url;
120
        $schemas = array_keys($this->getSchemas());
121
122
        /**
123
         * @var string $key
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);
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
     * @return array
143
     */
144
    public function getVariablesAndCategories()
145
    {
146
        $oldItems = [
147
            'Institution' => 'Platform',
148
            'InstitutionUrl' => 'Platform',
149
            'siteName' => 'Platform',
150
            'emailAdministrator' => 'admin',//'emailAdministrator' => 'Platform',
151
            'administratorSurname' => 'admin',
152
            'administratorTelephone' => 'admin',
153
            'administratorName' => 'admin',
154
            'show_administrator_data' => 'Platform',
155
            'show_tutor_data' => 'Session',
156
            'show_teacher_data' => 'Platform',
157
            'homepage_view' => 'Course',
158
            'show_toolshortcuts' => 'Course',
159
            'allow_group_categories' => 'Course',
160
            'server_type' => 'Platform',
161
            'platformLanguage' => 'Languages',
162
            'showonline' => 'Platform',
163
            'profile' => 'User',
164
            'default_document_quotum' => 'Course',
165
            'registration' => 'User',
166
            'default_group_quotum' => 'Course',
167
            'allow_registration' => 'Platform',
168
            'allow_registration_as_teacher' => 'Platform',
169
            'allow_lostpassword' => 'Platform',
170
            'allow_user_headings' => 'Course',
171
            'allow_personal_agenda' => 'User',
172
            'display_coursecode_in_courselist' => 'Platform',
173
            'display_teacher_in_courselist' => 'Platform',
174
            'permanently_remove_deleted_files' => 'Tools',
175
            'dropbox_allow_overwrite' => 'Tools',
176
            'dropbox_max_filesize' => 'Tools',
177
            'dropbox_allow_just_upload' => 'Tools',
178
            'dropbox_allow_student_to_student' => 'Tools',
179
            'dropbox_allow_group' => 'Tools',
180
            'dropbox_allow_mailing' => 'Tools',
181
            'extended_profile' => 'User',
182
            'student_view_enabled' => 'Platform',
183
            'show_navigation_menu' => 'Course',
184
            'enable_tool_introduction' => 'course',
185
            'page_after_login' => 'Platform',
186
            'time_limit_whosonline' => 'Platform',
187
            'breadcrumbs_course_homepage' => 'Course',
188
            'example_material_course_creation' => 'Platform',
189
            'account_valid_duration' => 'Platform',
190
            'use_session_mode' => 'Session',
191
            'allow_email_editor' => 'Tools',
192
            //'registered' => null',
193
            //'donotlistcampus' =>'null',
194
            'show_email_addresses' => 'Platform',
195
            'service_ppt2lp' => 'NULL',
196
            'stylesheets' => 'stylesheets',
197
            'upload_extensions_list_type' => 'Security',
198
            'upload_extensions_blacklist' => 'Security',
199
            'upload_extensions_whitelist' => 'Security',
200
            'upload_extensions_skip' => 'Security',
201
            'upload_extensions_replace_by' => 'Security',
202
            'show_number_of_courses' => 'Platform',
203
            'show_empty_course_categories' => 'Platform',
204
            'show_back_link_on_top_of_tree' => 'Platform',
205
            'show_different_course_language' => 'Platform',
206
            'split_users_upload_directory' => 'Tuning',
207
            'hide_dltt_markup' => 'Languages',
208
            'display_categories_on_homepage' => 'Platform',
209
            'permissions_for_new_directories' => 'Security',
210
            'permissions_for_new_files' => 'Security',
211
            'show_tabs' => 'Platform',
212
            'default_forum_view' => 'Course',
213
            'platform_charset' => 'Languages',
214
            'noreply_email_address' => 'Platform',
215
            'survey_email_sender_noreply' => 'Course',
216
            'openid_authentication' => 'Security',
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' => 'Languages',
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' => 'Tools',
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
            'sso_authentication' => 'Security',
268
            'sso_authentication_domain' => 'Security',
269
            'sso_authentication_auth_uri' => 'Security',
270
            'sso_authentication_unauth_uri' => 'Security',
271
            'sso_authentication_protocol' => 'Security',
272
            'enabled_wiris' => 'Editor',
273
            'allow_spellcheck' => 'Editor',
274
            'force_wiki_paste_as_plain_text' => 'Editor',
275
            'enabled_googlemaps' => 'Editor',
276
            'enabled_imgmap' => 'Editor',
277
            'enabled_support_svg' => 'Tools',
278
            'pdf_export_watermark_enable' => 'Platform',
279
            'pdf_export_watermark_by_course' => 'Platform',
280
            'pdf_export_watermark_text' => 'Platform',
281
            'enabled_insertHtml' => 'Editor',
282
            'students_export2pdf' => 'Tools',
283
            'exercise_min_score' => 'Course',
284
            'exercise_max_score' => 'Course',
285
            'show_users_folders' => 'Tools',
286
            'show_default_folders' => 'Tools',
287
            'show_chat_folder' => 'Tools',
288
            'enabled_text2audio' => 'Tools',
289
            'course_hide_tools' => 'Tools',
290
            'enabled_support_pixlr' => 'Tools',
291
            'show_groups_to_users' => 'Session',
292
            'accessibility_font_resize' => 'Platform',
293
            'hide_courses_in_sessions' => 'Session',
294
            'enable_quiz_scenario' => 'Course',
295
            'filter_terms' => 'Security',
296
            'header_extra_content' => 'Tracking',
297
            'footer_extra_content' => 'Tracking',
298
            'show_documents_preview' => 'Tools',
299
            'htmlpurifier_wiki' => 'Editor',
300
            'cas_activate' => 'CAS',
301
            'cas_server' => 'CAS',
302
            'cas_server_uri' => 'CAS',
303
            'cas_port' => 'CAS',
304
            'cas_protocol' => 'CAS',
305
            'cas_add_user_activate' => 'CAS',
306
            'update_user_info_cas_with_ldap' => 'CAS',
307
            'student_page_after_login' => 'Platform',
308
            'teacher_page_after_login' => 'Platform',
309
            'drh_page_after_login' => 'Platform',
310
            'sessionadmin_page_after_login' => 'Session',
311
            'student_autosubscribe' => 'Platform',
312
            'teacher_autosubscribe' => 'Platform',
313
            'drh_autosubscribe' => 'Platform',
314
            'sessionadmin_autosubscribe' => 'Session',
315
            'scorm_cumulative_session_time' => 'Course',
316
            'allow_hr_skills_management' => 'Gradebook',
317
            'enable_help_link' => 'Platform',
318
            'teachers_can_change_score_settings' => 'Gradebook',
319
            'allow_users_to_change_email_with_no_password' => 'User',
320
            'show_admin_toolbar' => 'display',
321
            'allow_global_chat' => 'Platform',
322
            'languagePriority1' => 'Languages',
323
            'languagePriority2' => 'Languages',
324
            'languagePriority3' => 'Languages',
325
            'languagePriority4' => 'Languages',
326
            'login_is_email' => 'Platform',
327
            'courses_default_creation_visibility' => 'Course',
328
            'gradebook_enable_grade_model' => 'Gradebook',
329
            'teachers_can_change_grade_model_settings' => 'Gradebook',
330
            'gradebook_default_weight' => 'Gradebook',
331
            'ldap_description' => 'LDAP',
332
            'shibboleth_description' => 'Shibboleth',
333
            'facebook_description' => 'Facebook',
334
            'gradebook_locking_enabled' => 'Gradebook',
335
            'gradebook_default_grade_model_id' => 'Gradebook',
336
            'allow_session_admins_to_manage_all_sessions' => 'Session',
337
            'allow_skills_tool' => 'Platform',
338
            'allow_public_certificates' => 'Course',
339
            'platform_unsubscribe_allowed' => 'Platform',
340
            'activate_email_template' => 'Platform',
341
            'enable_iframe_inclusion' => 'Editor',
342
            'show_hot_courses' => 'Platform',
343
            'enable_webcam_clip' => 'Tools',
344
            'use_custom_pages' => 'Platform',
345
            'tool_visible_by_default_at_creation' => 'Tools',
346
            'prevent_session_admins_to_manage_all_users' => 'Session',
347
            'documents_default_visibility_defined_in_course' => 'Tools',
348
            'enabled_mathjax' => 'Editor',
349
            'meta_twitter_site' => 'Tracking',
350
            'meta_twitter_creator' => 'Tracking',
351
            'meta_title' => 'Tracking',
352
            'meta_description' => 'Tracking',
353
            'meta_image_path' => 'Tracking',
354
            'allow_teachers_to_create_sessions' => 'Session',
355
            'institution_address' => 'Platform',
356
            'chamilo_database_version' => 'null',
357
            'cron_remind_course_finished_activate' => 'Crons',
358
            'cron_remind_course_expiration_frequency' => 'Crons',
359
            'cron_remind_course_expiration_activate' => 'Crons',
360
            'allow_coach_feedback_exercises' => 'Session',
361
            'allow_my_files' => 'Platform',
362
            'ticket_allow_student_add' => 'Ticket',
363
            'ticket_send_warning_to_all_admins' => 'Ticket',
364
            'ticket_warn_admin_no_user_in_category' => 'Ticket',
365
            'ticket_allow_category_edition' => 'Ticket',
366
            'load_term_conditions_section' => 'Platform',
367
            'show_terms_if_profile_completed' => 'Ticket',
368
            'hide_home_top_when_connected' => 'Platform',
369
            'hide_global_announcements_when_not_connected' => 'Platform',
370
            'course_creation_use_template' => 'Course',
371
            'allow_strength_pass_checker' => 'Security',
372
            'allow_captcha' => 'Security',
373
            'captcha_number_mistakes_to_block_account' => 'Security',
374
            'captcha_time_to_block' => 'Security',
375
            'drh_can_access_all_session_content' => 'Session',
376
            'display_groups_forum_in_general_tool' => 'Tools',
377
            'allow_tutors_to_assign_students_to_session' => 'Session',
378
            'allow_lp_return_link' => 'Course',
379
            'hide_scorm_export_link' => 'Course',
380
            'hide_scorm_copy_link' => 'Course',
381
            'hide_scorm_pdf_link' => 'Course',
382
            'session_days_before_coach_access' => 'Session',
383
            'session_days_after_coach_access' => 'Session',
384
            'pdf_logo_header' => 'Course',
385
            'order_user_list_by_official_code' => 'Platform',
386
            'email_alert_manager_on_new_quiz' => 'Tools',
387
            'show_official_code_exercise_result_list' => 'Tools',
388
            'course_catalog_hide_private' => 'Platform',
389
            'catalog_show_courses_sessions' => 'Platform',
390
            'auto_detect_language_custom_pages' => 'Platform',
391
            'lp_show_reduced_report' => 'Tools',
392
            'allow_session_course_copy_for_teachers' => 'Session',
393
            'hide_logout_button' => 'Platform',
394
            'redirect_admin_to_courses_list' => 'Platform',
395
            'course_images_in_courses_list' => 'Course',
396
            'student_publication_to_take_in_gradebook' => 'Gradebook',
397
            'certificate_filter_by_official_code' => 'Gradebook',
398
            'exercise_max_ckeditors_in_page' => 'Tools',
399
            'document_if_file_exists_option' => 'Tools',
400
            'add_gradebook_certificates_cron_task_enabled' => 'Gradebook',
401
            'openbadges_backpack' => 'Gradebook',
402
            'cookie_warning' => 'Tools',
403
            'hide_course_group_if_no_tools_available' => 'Tools',
404
            'catalog_allow_session_auto_subscription' => 'Session',
405
            'registration.soap.php.decode_utf8' => 'Platform',
406
            'allow_delete_attendance' => 'Tools',
407
            'gravatar_enabled' => 'Platform',
408
            'gravatar_type' => 'Platform',
409
            'limit_session_admin_role' => 'Session',
410
            'show_session_description' => 'Session',
411
            'hide_certificate_export_link_students' => 'Gradebook',
412
            'hide_certificate_export_link' => 'Gradebook',
413
            'dropbox_hide_course_coach' => 'Tools',
414
            'dropbox_hide_general_coach' => 'Tools',
415
            'sso_force_redirect' => 'Security',
416
            'session_course_ordering' => 'Session',
417
            'gamification_mode' => 'Platform',
418
            'prevent_multiple_simultaneous_login' => 'Security',
419
            'gradebook_detailed_admin_view' => 'Gradebook',
420
            'course_catalog_published' => 'Course',
421
            'user_reset_password' => 'Security',
422
            'user_reset_password_token_limit' => 'Security',
423
            'my_courses_view_by_session' => 'Session',
424
            'show_full_skill_name_on_skill_wheel' => 'Platform',
425
            'messaging_allow_send_push_notification' => 'WebServices',
426
            'messaging_gdc_project_number' => 'WebServices',
427
            'messaging_gdc_api_key' => 'WebServices',
428
            'teacher_can_select_course_template' => 'Course',
429
            'enable_record_audio' => 'Tools',
430
            'allow_show_skype_account' => 'Platform',
431
            'allow_show_linkedin_url' => 'Platform',
432
            'enable_profile_user_address_geolocalization' => 'User',
433
            'show_official_code_whoisonline' => 'User',
434
            'icons_mode_svg' => 'display',
435
            'user_name_order' => 'display',
436
            'user_name_sort_by' => 'display',
437
            'default_calendar_view' => 'agenda',
438
            'exercise_invisible_in_session' => 'exercise',
439
            'configure_exercise_visibility_in_course' => 'exercise',
440
            'allow_download_documents_by_api_key' => 'Webservices',
441
            'ProfilingFilterAddingUsers' => 'profile',
442
            'donotlistcampus' => 'platform',
443
            'gradebook_show_percentage_in_reports' => 'gradebook',
444
445
        ];
446
447
        return $oldItems;
448
    }
449
450
    /**
451
     * Rename old variable with variable used in Chamilo 2.0
452
     * @param string $variable
453
     * @return mixed
454
     */
455
    public function renameVariable($variable)
456
    {
457
        $list = [
458
            'timezone_value' => 'timezone',
459
            'Institution' => 'institution',
460
            'SiteName' => 'site_name',
461
            'siteName' => 'site_name',
462
            'InstitutionUrl' => 'institution_url',
463
            'registration' => 'required_profile_fields',
464
            'stylesheets' => 'theme',
465
            'platformLanguage' => 'platform_language',
466
            'languagePriority1' => 'language_priority_1',
467
            'languagePriority2' => 'language_priority_2',
468
            'languagePriority3' => 'language_priority_3',
469
            'languagePriority4' => 'language_priority_4',
470
            'gradebook_score_display_coloring' => 'my_display_coloring',
471
            'document_if_file_exists_option' => 'if_file_exists_option',
472
            'ProfilingFilterAddingUsers' => 'profiling_filter_adding_users',
473
            'course_create_active_tools' => 'active_tools_on_create',
474
            'emailAdministrator' => 'administrator_email',
475
            'administratorSurname' => 'administrator_surname',
476
            'administratorName' => 'administrator_name',
477
            'administratorTelephone' => 'administrator_phone',
478
            'registration.soap.php.decode_utf8' => 'decode_utf8',
479
            'profile' => 'changeable_options',
480
        ];
481
482
        return isset($list[$variable]) ? $list[$variable] : $variable;
483
    }
484
485
    /**
486
     * Replace old Chamilo 1.x category with 2.0 version
487
     * @param string $variable
488
     * @param string $defaultCategory
489
     * @return mixed
490
     */
491
    public function fixCategory($variable, $defaultCategory)
492
    {
493
        $settings = [
494
            'cookie_warning' => 'platform',
495
            'donotlistcampus' => 'platform',
496
            'administrator_email' => 'admin',
497
            'administrator_surname' => 'admin',
498
            'administrator_name' => 'admin',
499
            'administrator_phone' => 'admin',
500
            'exercise_max_ckeditors_in_page' => 'exercise',
501
            'allow_hr_skills_management' => 'skill',
502
            'accessibility_font_resize' => 'display',
503
            'account_valid_duration' => 'profile',
504
            'activate_email_template' => 'mail',
505
            'allow_global_chat' => 'chat',
506
            'allow_lostpassword' => 'registration',
507
            'allow_registration' => 'registration',
508
            'allow_registration_as_teacher' => 'registration',
509
            'required_profile_fields' => 'registration',
510
            'allow_skills_tool' => 'skill',
511
            'allow_students_to_browse_courses' => 'display',
512
            'allow_terms_conditions' => 'registration',
513
            'allow_users_to_create_courses' => 'course',
514
            'auto_detect_language_custom_pages' => 'language',
515
            'platform_language' => 'language',
516
            'course_validation' => 'course',
517
            'course_validation_terms_and_conditions_url' => 'course',
518
            'display_categories_on_homepage' => 'display',
519
            'display_coursecode_in_courselist' => 'course',
520
            'display_teacher_in_courselist' => 'course',
521
            'drh_autosubscribe' => 'registration',
522
            'drh_page_after_login' => 'registration',
523
            'enable_help_link' => 'display',
524
            'example_material_course_creation' => 'course',
525
            'login_is_email' => 'profile',
526
            'noreply_email_address' => 'mail',
527
            'page_after_login' => 'registration',
528
            'pdf_export_watermark_by_course' => 'document',
529
            'pdf_export_watermark_enable' => 'document',
530
            'pdf_export_watermark_text' => 'document',
531
            'platform_unsubscribe_allowed' => 'registration',
532
            'send_email_to_admin_when_create_course' => 'course',
533
            'show_admin_toolbar' => 'display',
534
            'show_administrator_data' => 'display',
535
            'show_back_link_on_top_of_tree' => 'display',
536
            'show_closed_courses' => 'display',
537
            'show_different_course_language' => 'display',
538
            'show_email_addresses' => 'display',
539
            'show_empty_course_categories' => 'display',
540
            'show_full_skill_name_on_skill_wheel' => 'skill',
541
            'show_hot_courses' => 'display',
542
            'show_link_bug_notification' => 'display',
543
            'show_number_of_courses' => 'display',
544
            'show_teacher_data' => 'display',
545
            'showonline' => 'display',
546
            'student_autosubscribe' => 'registration',
547
            'student_page_after_login' => 'registration',
548
            'student_view_enabled' => 'course',
549
            'teacher_autosubscribe' => 'registration',
550
            'teacher_page_after_login' => 'registration',
551
            'time_limit_whosonline' => 'display',
552
            'user_selected_theme' => 'profile',
553
            'hide_global_announcements_when_not_connected' => 'announcement',
554
            'hide_home_top_when_connected' => 'display',
555
            'hide_logout_button' => 'display',
556
            'institution_address' => 'platform',
557
            'redirect_admin_to_courses_list' => 'admin',
558
            'use_custom_pages' => 'platform',
559
            'allow_group_categories' => 'group',
560
            'allow_user_headings' => 'display',
561
            'default_document_quotum' => 'document',
562
            'default_forum_view' => 'forum',
563
            'default_group_quotum' => 'document',
564
            'enable_quiz_scenario' => 'exercise',
565
            'exercise_max_score' => 'exercise',
566
            'exercise_min_score' => 'exercise',
567
            'pdf_logo_header' => 'platform',
568
            'show_glossary_in_documents' => 'document',
569
            'show_glossary_in_extra_tools' => 'glossary',
570
            'survey_email_sender_noreply'=> 'survey',
571
            'allow_coach_feedback_exercises' => 'exercise',
572
            'sessionadmin_autosubscribe' => 'registration',
573
            'sessionadmin_page_after_login' => 'registration',
574
            'show_tutor_data' => 'display',
575
            'allow_social_tool' => 'social',
576
            'allow_message_tool' => 'message',
577
            'allow_email_editor' => 'editor',
578
            'show_link_ticket_notification' => 'display',
579
            'permissions_for_new_directories' => 'document',
580
            'enable_profile_user_address_geolocalization' => 'profile',
581
            'allow_show_skype_account' => 'profile',
582
            'allow_show_linkedin_url' => 'profile',
583
            'allow_students_to_create_groups_in_social' => 'social',
584
            'default_calendar_view' => 'agenda',
585
            'documents_default_visibility_defined_in_course' => 'document',
586
            'message_max_upload_filesize' => 'message',
587
            'course_create_active_tools' => 'course',
588
            'tool_visible_by_default_at_creation' => 'document',
589
            'show_users_folders' => 'document',
590
            'show_default_folders' => 'document',
591
            'show_chat_folder' => 'chat',
592
            'enabled_support_svg' => 'editor',
593
            'enabled_support_pixlr' => 'editor',
594
            'enable_webcam_clip' => 'document',
595
            'enable_record_audio' => 'course',
596
            'enabled_text2audio' => 'document',
597
            'permanently_remove_deleted_files' => 'document',
598
            'allow_delete_attendance' => 'attendance',
599
            'display_groups_forum_in_general_tool' => 'forum',
600
            'dropbox_allow_overwrite' => 'dropbox',
601
            'allow_user_course_subscription_by_course_admin' => 'course',
602
            'hide_course_group_if_no_tools_available' => 'group',
603
            'extend_rights_for_coach_on_survey' => 'survey',
604
            'show_official_code_exercise_result_list' => 'exercise',
605
            'dropbox_max_filesize' => 'dropbox',
606
            'dropbox_allow_just_upload' => 'dropbox',
607
            'dropbox_allow_student_to_student' => 'dropbox',
608
            'dropbox_allow_group' => 'dropbox',
609
            'dropbox_allow_mailing' => 'dropbox',
610
            'upload_extensions_list_type' => 'document',
611
            'upload_extensions_blacklist' => 'document',
612
            'upload_extensions_skip' => 'document',
613
            'changeable_options' => 'profile',
614
            'users_copy_files' => 'document',
615
            'if_file_exists_option' => 'document',
616
            'permissions_for_new_files' => 'document',
617
            'extended_profile' => 'profile',
618
            'split_users_upload_directory' => 'profile',
619
            'show_documents_preview' => 'document',
620
            'decode_utf8' => 'webservice',
621
            'messaging_allow_send_push_notification' => 'webservice',
622
            'messaging_gdc_project_number' => 'webservice',
623
            'messaging_gdc_api_key' => 'webservice',
624
            'allow_download_documents_by_api_key' => 'webservice',
625
            'profiling_filter_adding_users' => 'profile',
626
            'hide_dltt_markup' => 'language',
627
        ];
628
629
        return isset($settings[$variable]) ? $settings[$variable] : $defaultCategory;
630
    }
631
632
    /**
633
     * @param $name
634
     * @return mixed
635
     * @throws \InvalidArgumentException
636
     */
637
    public function getSetting($name)
638
    {
639
        if (false === strpos($name, '.')) {
640
            //throw new \InvalidArgumentException(sprintf('Parameter must be in format "namespace.name", "%s" given.', $name));
641
642
            // This code allows the possibility of calling
643
            // api_get_setting('allow_skills_tool') instead of
644
            // the correct way api_get_setting('platform.allow_skills_tool')
645
            $items = $this->getVariablesAndCategories();
646
647
            if (isset($items[$name])) {
648
                $originalName = $name;
649
                $name = $this->renameVariable($name);
650
                $category = $this->fixCategory(
651
                    strtolower($name),
652
                    strtolower($items[$originalName])
653
                );
654
                $name = $category.'.'.$name;
655
            } else {
656
                throw new \InvalidArgumentException(sprintf('Parameter must be in format "category.name", "%s" given.', $name));
657
            }
658
        }
659
660
        list($category, $name) = explode('.', $name);
661
        //var_dump($category, $name);
662
        $settings = $this->load($category, $name);
663
664
        return $settings->get($name);
665
    }
666
667
    /**
668
     * @param string $category
669
     * @return string
670
     */
671
    public function convertNameSpaceToService($category)
672
    {
673
        return 'chamilo_core.settings.'.$category;
674
    }
675
676
    /**
677
     * @param string $category
678
     * @return string
679
     */
680
    public function convertServiceToNameSpace($category)
681
    {
682
        return str_replace('chamilo_core.settings.', '', $category);
683
    }
684
685
    /**
686
     * {@inheritdoc}
687
     */
688
    public function load($schemaAlias, $namespace = null, $ignoreUnknown = true)
689
    {
690
        $schemaAliasNoPrefix = $schemaAlias;
691
        $schemaAlias = 'chamilo_core.settings.'.$schemaAlias;
692
693
        /** @var SchemaInterface $schema */
694
        $schema = $this->schemaRegistry->get($schemaAlias);
695
696
        /** @var \Sylius\Bundle\SettingsBundle\Model\Settings  $settings */
697
        $settings = $this->settingsFactory->createNew();
698
        $settings->setSchemaAlias($schemaAlias);
699
700
        // We need to get a plain parameters array since we use the options resolver on it
701
        $parameters = $this->getParameters($schemaAliasNoPrefix);
702
        $settingsBuilder = new SettingsBuilder();
703
        $schema->buildSettings($settingsBuilder);
704
705
        // Remove unknown settings' parameters (e.g. From a previous version of the settings schema)
706
        if (true === $ignoreUnknown) {
707
            foreach ($parameters as $name => $value) {
708
                if (!$settingsBuilder->isDefined($name)) {
709
                    unset($parameters[$name]);
710
                }
711
            }
712
        }
713
714
        foreach ($settingsBuilder->getTransformers() as $parameter => $transformer) {
715
            if (array_key_exists($parameter, $parameters)) {
716
                $parameters[$parameter] = $transformer->reverseTransform($parameters[$parameter]);
717
            }
718
        }
719
720
        $parameters = $settingsBuilder->resolve($parameters);
721
        $settings->setParameters($parameters);
722
723
        return $settings;
724
    }
725
726
    /**
727
     * {@inheritdoc}
728
     * @throws ValidatorException
729
     */
730
    public function save(SettingsInterface $settings)
731
    {
732
        $namespace = $settings->getSchemaAlias();
733
734
        /** @var SchemaInterface $schema */
735
        $schema = $this->schemaRegistry->get($settings->getSchemaAlias());
736
737
        $settingsBuilder = new SettingsBuilder();
738
        $schema->buildSettings($settingsBuilder);
739
        $parameters = $settingsBuilder->resolve($settings->getParameters());
740
        // Transform value. Example array to string using transformer. Example:
741
        // 1. Setting "tool_visible_by_default_at_creation" it's a multiple select
742
        // 2. Is defined as an array in class DocumentSettingsSchema
743
        // 3. Add transformer for that variable "ArrayToIdentifierTransformer"
744
        // 4. Here we recover the transformer and convert the array to string
745
        foreach ($settingsBuilder->getTransformers() as $parameter => $transformer) {
746
            if (array_key_exists($parameter, $parameters)) {
747
                $parameters[$parameter] = $transformer->transform($parameters[$parameter]);
748
            }
749
        }
750
        $settings->setParameters($parameters);
751
        $persistedParameters = $this->repository->findBy(['category' => $this->convertServiceToNameSpace($settings->getSchemaAlias())]);
752
        $persistedParametersMap = [];
753
754
        foreach ($persistedParameters as $parameter) {
755
            $persistedParametersMap[$parameter->getTitle()] = $parameter;
756
        }
757
758
        /** @var SettingsEvent $event */
759
        /*$event = $this->eventDispatcher->dispatch(
760
            SettingsEvent::PRE_SAVE,
761
            new SettingsEvent($settings, $parameters)
762
        );*/
763
764
        /** @var \Chamilo\CoreBundle\Entity\SettingsCurrent $url */
765
        $url = $this->getUrl();
766
        $simpleCategoryName = str_replace('chamilo_core.settings.', '', $namespace);
767
768
        foreach ($parameters as $name => $value) {
769
            if (isset($persistedParametersMap[$name])) {
770
                $parameter = $persistedParametersMap[$name];
771
                $parameter->setSelectedValue($value);
772
            } else {
773
                $parameter = new SettingsCurrent();
774
                $parameter
775
                    ->setVariable($name)
776
                    ->setCategory($simpleCategoryName)
777
                    ->setTitle($name)
778
                    ->setSelectedValue($value)
779
                    ->setUrl($url)
780
                    ->setAccessUrlChangeable(1)
781
                    ->setAccessUrlLocked(1)
782
                ;
783
784
                /* @var $errors ConstraintViolationListInterface */
785
                /*$errors = $this->validator->validate($parameter);
786
                if (0 < $errors->count()) {
787
                    throw new ValidatorException($errors->get(0)->getMessage());
788
                }*/
789
                $this->manager->persist($parameter);
790
            }
791
            $this->manager->persist($parameter);
792
        }
793
794
        $this->manager->flush();
795
796
        return;
797
798
        ////
799
        $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...
800
        $schemaAliasChamilo = str_replace('chamilo_core.settings.', '', $schemaAlias);
801
802
        $schema = $this->schemaRegistry->get($schemaAlias);
803
804
        $settingsBuilder = new SettingsBuilder();
805
        $schema->buildSettings($settingsBuilder);
806
807
        $parameters = $settingsBuilder->resolve($settings->getParameters());
808
809
        foreach ($settingsBuilder->getTransformers() as $parameter => $transformer) {
810
            if (array_key_exists($parameter, $parameters)) {
811
                $parameters[$parameter] = $transformer->transform($parameters[$parameter]);
812
            }
813
        }
814
815
        /** @var \Sylius\Bundle\SettingsBundle\Event\SettingsEvent $event */
816
        $event = $this->eventDispatcher->dispatch(
817
            SettingsEvent::PRE_SAVE,
818
            new SettingsEvent($settings)
819
        );
820
821
        /** @var \Chamilo\CoreBundle\Entity\SettingsCurrent $url */
822
        $url = $event->getSettings()->getAccessUrl();
823
824
        foreach ($parameters as $name => $value) {
825
            if (isset($persistedParametersMap[$name])) {
826
                if ($value instanceof Course) {
827
                    $value = $value->getId();
828
                }
829
                $persistedParametersMap[$name]->setValue($value);
830
            } else {
831
                /** @var SettingsCurrent $setting */
832
                $setting = $this->settingsFactory->createNew();
833
                $setting->setSchemaAlias($schemaAlias);
834
835
                $setting
836
                    ->setNamespace($schemaAliasChamilo)
837
                    ->setName($name)
838
                    ->setValue($value)
839
                    ->setUrl($url)
840
                    ->setAccessUrlLocked(0)
841
                    ->setAccessUrlChangeable(1)
842
                ;
843
844
845
                /* @var $errors ConstraintViolationListInterface */
846
                /*$errors = $this->->validate($parameter);
847
                if (0 < $errors->count()) {
848
                    throw new ValidatorException($errors->get(0)->getMessage());
849
                }*/
850
                $this->manager->persist($setting);
851
                $this->manager->flush();
852
            }
853
        }
854
        /*$parameters = $settingsBuilder->resolve($settings->getParameters());
855
        $settings->setParameters($parameters);
856
857
        $this->eventDispatcher->dispatch(SettingsEvent::PRE_SAVE, new SettingsEvent($settings));
858
859
        $this->manager->persist($settings);
860
        $this->manager->flush();
861
862
        $this->eventDispatcher->dispatch(SettingsEvent::POST_SAVE, new SettingsEvent($settings));*/
863
    }
864
865
    /**
866
     * Load parameter from database.
867
     *
868
     * @param string $namespace
869
     *
870
     * @return array
871
     */
872
    private function getParameters($namespace)
873
    {
874
        $parameters = [];
875
        /** @var  SettingsCurrent $parameter */
876
        foreach ($this->repository->findBy(['category' => $namespace]) as $parameter) {
877
            $parameters[$parameter->getVariable()] = $parameter->getSelectedValue();
878
        }
879
880
        return $parameters;
881
    }
882
883
    public function getParametersFromKeywordOrderedByCategory($keyword)
884
    {
885
        $query = $this->repository->createQueryBuilder('s')
886
            ->where('s.variable LIKE :keyword')
887
            ->setParameter('keyword', "%$keyword%")
888
        ;
889
        $parametersFromDb = $query->getQuery()->getResult();
890
        $parameters = [];
891
        /** @var \Chamilo\CoreBundle\Entity\SettingsCurrent $parameter */
892
        foreach ($parametersFromDb as $parameter) {
893
            $parameters[$parameter->getCategory()][] = $parameter;
894
        }
895
        return $parameters;
896
    }
897
898
    /**
899
     * @param string $namespace
900
     * @param string $keyword
901
     * @param bool $returnObjects
902
     * @return array
903
     */
904
    public function getParametersFromKeyword($namespace, $keyword = '', $returnObjects = false)
905
    {
906
        if (empty($keyword)) {
907
            $criteria = ['category' => $namespace];
908
            $parametersFromDb = $this->repository->findBy($criteria);
909
        } else {
910
            $query = $this->repository->createQueryBuilder('s')
911
                ->where('s.variable LIKE :keyword')
912
                ->setParameter('keyword', "%$keyword%")
913
            ;
914
            $parametersFromDb = $query->getQuery()->getResult();
915
        }
916
917
        if ($returnObjects) {
918
            return $parametersFromDb;
919
        }
920
        $parameters = [];
921
        /** @var \Chamilo\CoreBundle\Entity\SettingsCurrent $parameter */
922
        foreach ($parametersFromDb as $parameter) {
923
            $parameters[$parameter->getVariable()] = $parameter->getSelectedValue();
924
        }
925
926
        return $parameters;
927
    }
928
929
    private function transformParameters(SettingsBuilder $settingsBuilder, array $parameters)
0 ignored issues
show
Unused Code introduced by
The method transformParameters() is not used, and could be removed.

This check looks for private methods that have been defined, but are not used inside the class.

Loading history...
930
    {
931
        $transformedParameters = $parameters;
932
933
        foreach ($settingsBuilder->getTransformers() as $parameter => $transformer) {
934
            if (array_key_exists($parameter, $parameters)) {
935
                $transformedParameters[$parameter] = $transformer->reverseTransform($parameters[$parameter]);
936
            }
937
        }
938
939
        return $transformedParameters;
940
    }
941
}
942