Completed
Push — master ( 73cb1f...8d952e )
by Julito
33:13
created

SettingsManager::getUrl()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
rs 10
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 Sylius\Bundle\ResourceBundle\Controller\EventDispatcherInterface;
10
use Sylius\Bundle\SettingsBundle\Event\SettingsEvent;
11
use Doctrine\Common\Cache\Cache;
12
use Doctrine\Common\Persistence\ObjectManager;
13
use Sylius\Bundle\SettingsBundle\Manager\SettingsManagerInterface;
14
use Sylius\Bundle\SettingsBundle\Model\Settings;
15
use Sylius\Bundle\SettingsBundle\Model\SettingsInterface;
16
use Sylius\Bundle\SettingsBundle\Resolver\SettingsResolverInterface;
17
use Sylius\Bundle\SettingsBundle\Schema\SchemaInterface;
18
use Sylius\Bundle\SettingsBundle\Schema\SchemaRegistryInterface;
19
use Sylius\Bundle\SettingsBundle\Schema\SettingsBuilder;
20
use Sylius\Component\Registry\ServiceRegistryInterface;
21
use Sylius\Component\Resource\Factory\FactoryInterface;
22
use Sylius\Component\Resource\Repository\RepositoryInterface;
23
use Symfony\Component\Validator\ConstraintViolationListInterface;
24
use Symfony\Component\Validator\Exception\ValidatorException;
25
use Symfony\Component\Validator\ValidatorInterface;
26
use Sylius\Bundle\SettingsBundle\Manager\SettingsManager as SyliusSettingsManager;
27
use Chamilo\CoreBundle\Entity\SettingsCurrent;
28
29
/**
30
 * Class SettingsManager
31
 * @package Chamilo\SettingsBundle\Manager
32
 */
33
class SettingsManager implements SettingsManagerInterface
34
{
35
    private $url;
36
37
    /**
38
     * @var ServiceRegistryInterface
39
     */
40
    private $schemaRegistry;
41
42
    /**
43
     * @var ServiceRegistryInterface
44
     */
45
    private $resolverRegistry;
46
47
    /**
48
     * @var ObjectManager
49
     */
50
    private $manager;
51
52
    /**
53
     * @var FactoryInterface
54
     */
55
    private $settingsFactory;
56
57
    /**
58
     * @var EventDispatcherInterface
59
     */
60
    private $eventDispatcher;
61
62
    /**
63
     * Runtime cache for resolved parameters
64
     *
65
     * @var Settings[]
66
     */
67
    protected $resolvedSettings = array();
68
69
    public function __construct(
70
        ServiceRegistryInterface $schemaRegistry,
71
        ServiceRegistryInterface $resolverRegistry,
72
        ObjectManager $manager,
73
        FactoryInterface $settingsFactory,
74
        $eventDispatcher
75
    ) {
76
        $this->schemaRegistry = $schemaRegistry;
77
        $this->resolverRegistry = $resolverRegistry;
78
        $this->manager = $manager;
79
        $this->settingsFactory = $settingsFactory;
80
        $this->eventDispatcher = $eventDispatcher;
81
    }
82
83
    /**
84
     * @return AccessUrl
85
     */
86
    public function getUrl()
87
    {
88
        return $this->url;
89
    }
90
91
    /**
92
     * @param AccessUrl $url
93
     */
94
    public function setUrl(AccessUrl $url)
95
    {
96
        $this->url = $url;
97
    }
98
99
    /**
100
     * @param AccessUrl $url
101
     */
102
    public function installSchemas(AccessUrl $url)
103
    {
104
        $this->url = $url;
105
        //$schemas = $this->getSchemas();
106
        //$schemas = array_keys($schemas);
107
108
        /**
109
         * @var string $key
110
         * @var \Sylius\Bundle\SettingsBundle\Schema\SchemaInterface $schema
111
         */
112
        /*foreach ($schemas as $schema) {
113
            $settings = $this->load($schema);
114
            $this->save($schema, $settings);
115
        }*/
116
    }
117
118
    /**
119
     * @return array
120
     */
121
    public function getSchemas()
122
    {
123
        return $this->schemaRegistry->all();
124
    }
125
126
    /**
127
     * Get variables and categories as in 1.11.x
128
     * @return array
129
     */
130
    public function getVariablesAndCategories()
131
    {
132
        $oldItems = [
133
            'Institution' => 'Platform',
134
            'InstitutionUrl' => 'Platform',
135
            'siteName' => 'Platform',
136
            'emailAdministrator' => 'admin',//'emailAdministrator' => 'Platform',
137
            'administratorSurname' => 'admin',
138
            'administratorTelephone' => 'admin',
139
            'administratorName' => 'admin',
140
            'show_administrator_data' => 'Platform',
141
            'show_tutor_data' => 'Session',
142
            'show_teacher_data' => 'Platform',
143
            'homepage_view' => 'Course',
144
            'show_toolshortcuts' => 'Course',
145
            'allow_group_categories' => 'Course',
146
            'server_type' => 'Platform',
147
            'platformLanguage' => 'Languages',
148
            'showonline' => 'Platform',
149
            'profile' => 'User',
150
            'default_document_quotum' => 'Course',
151
            'registration' => 'User',
152
            'default_group_quotum' => 'Course',
153
            'allow_registration' => 'Platform',
154
            'allow_registration_as_teacher' => 'Platform',
155
            'allow_lostpassword' => 'Platform',
156
            'allow_user_headings' => 'Course',
157
            'allow_personal_agenda' => 'User',
158
            'display_coursecode_in_courselist' => 'Platform',
159
            'display_teacher_in_courselist' => 'Platform',
160
            'permanently_remove_deleted_files' => 'Tools',
161
            'dropbox_allow_overwrite' => 'Tools',
162
            'dropbox_max_filesize' => 'Tools',
163
            'dropbox_allow_just_upload' => 'Tools',
164
            'dropbox_allow_student_to_student' => 'Tools',
165
            'dropbox_allow_group' => 'Tools',
166
            'dropbox_allow_mailing' => 'Tools',
167
            //'extended_profile' => 'User', // changed
168
            'extended_profile' => 'profile', // changed
169
            'student_view_enabled' => 'Platform',
170
            'show_navigation_menu' => 'Course',
171
            'enable_tool_introduction' => 'course',
172
            'page_after_login' => 'Platform',
173
            'time_limit_whosonline' => 'Platform',
174
            'breadcrumbs_course_homepage' => 'Course',
175
            'example_material_course_creation' => 'Platform',
176
            'account_valid_duration' => 'Platform',
177
            'use_session_mode' => 'Session',
178
            'allow_email_editor' => 'Tools',
179
            //'registered' => null',
180
            //'donotlistcampus' =>'null',
181
            'show_email_addresses' => 'Platform',
182
            'service_ppt2lp' => 'NULL',
183
            'stylesheets' => 'stylesheets',
184
            'upload_extensions_list_type' => 'Security',
185
            'upload_extensions_blacklist' => 'Security',
186
            'upload_extensions_whitelist' => 'Security',
187
            'upload_extensions_skip' => 'Security',
188
            'upload_extensions_replace_by' => 'Security',
189
            'show_number_of_courses' => 'Platform',
190
            'show_empty_course_categories' => 'Platform',
191
            'show_back_link_on_top_of_tree' => 'Platform',
192
            'show_different_course_language' => 'Platform',
193
            'split_users_upload_directory' => 'profile',//'split_users_upload_directory' => 'Tuning',
194
            'hide_dltt_markup' => 'Languages',
195
            'display_categories_on_homepage' => 'Platform',
196
            'permissions_for_new_directories' => 'Security',
197
            'permissions_for_new_files' => 'Security',
198
            'show_tabs' => 'Platform',
199
            'default_forum_view' => 'Course',
200
            'platform_charset' => 'Languages',
201
            'noreply_email_address' => 'Platform',
202
            'survey_email_sender_noreply' => 'Course',
203
            'openid_authentication' => 'Security',
204
            'gradebook_enable' => 'Gradebook',
205
            'gradebook_score_display_coloring' => 'Gradebook',
206
            'gradebook_score_display_custom' => 'Gradebook',
207
            'gradebook_score_display_colorsplit' => 'Gradebook',
208
            'gradebook_score_display_upperlimit' => 'Gradebook',
209
            'gradebook_number_decimals' => 'Gradebook',
210
            'user_selected_theme' => 'Platform',
211
            'allow_course_theme' => 'Course',
212
            'show_closed_courses' => 'Platform',
213
            'extendedprofile_registration' => 'User',
214
            'extendedprofile_registrationrequired' => 'User',
215
            'add_users_by_coach' => 'Session',
216
            'extend_rights_for_coach' => 'Security',
217
            'extend_rights_for_coach_on_survey' => 'Security',
218
            'course_create_active_tools' => 'Tools',
219
            'show_session_coach' => 'Session',
220
            'allow_users_to_create_courses' => 'Platform',
221
            'allow_message_tool' => 'Tools',
222
            'allow_social_tool' => 'Tools',
223
            'allow_students_to_browse_courses' => 'Platform',
224
            'show_session_data' => 'Session',
225
            'allow_use_sub_language' => 'Languages',
226
            'show_glossary_in_documents' => 'Course',
227
            'allow_terms_conditions' => 'Platform',
228
            'search_enabled' => 'Search',
229
            'search_prefilter_prefix' => 'Search',
230
            'search_show_unlinked_results' => 'Search',
231
            'show_courses_descriptions_in_catalog' => 'Course',
232
            'allow_coach_to_edit_course_session' => 'Session',
233
            'show_glossary_in_extra_tools' => 'Course',
234
            'send_email_to_admin_when_create_course' => 'Platform',
235
            'go_to_course_after_login' => 'Course',
236
            'math_asciimathML' => 'Editor',
237
            'enabled_asciisvg' => 'Editor',
238
            'include_asciimathml_script' => 'Editor',
239
            'youtube_for_students' => 'Editor',
240
            'block_copy_paste_for_students' => 'Editor',
241
            'more_buttons_maximized_mode' => 'Editor',
242
            'students_download_folders' => 'Tools',
243
            'users_copy_files' => 'Tools',
244
            'allow_students_to_create_groups_in_social' => 'Tools',
245
            'allow_send_message_to_all_platform_users' => 'Tools',
246
            'message_max_upload_filesize' => 'Tools',
247
            'use_users_timezone' => 'profile', //'use_users_timezone' => 'Timezones',
248
            'timezone_value' => 'platform',//'timezone_value' => 'Timezones',
249
            'allow_user_course_subscription_by_course_admin' => 'Security',
250
            'show_link_bug_notification' => 'Platform',
251
            'show_link_ticket_notification' => 'Platform',
252
            'course_validation' => 'course',//'course_validation' => 'Platform',
253
            'course_validation_terms_and_conditions_url' => 'Platform',
254
            'sso_authentication' => 'Security',
255
            'sso_authentication_domain' => 'Security',
256
            'sso_authentication_auth_uri' => 'Security',
257
            'sso_authentication_unauth_uri' => 'Security',
258
            'sso_authentication_protocol' => 'Security',
259
            'enabled_wiris' => 'Editor',
260
            'allow_spellcheck' => 'Editor',
261
            'force_wiki_paste_as_plain_text' => 'Editor',
262
            'enabled_googlemaps' => 'Editor',
263
            'enabled_imgmap' => 'Editor',
264
            'enabled_support_svg' => 'Tools',
265
            'pdf_export_watermark_enable' => 'Platform',
266
            'pdf_export_watermark_by_course' => 'Platform',
267
            'pdf_export_watermark_text' => 'Platform',
268
            'enabled_insertHtml' => 'Editor',
269
            'students_export2pdf' => 'Tools',
270
            'exercise_min_score' => 'Course',
271
            'exercise_max_score' => 'Course',
272
            'show_users_folders' => 'Tools',
273
            'show_default_folders' => 'Tools',
274
            'show_chat_folder' => 'Tools',
275
            'enabled_text2audio' => 'Tools',
276
            'course_hide_tools' => 'Tools',
277
            'enabled_support_pixlr' => 'Tools',
278
            'show_groups_to_users' => 'Session',
279
            'accessibility_font_resize' => 'Platform',
280
            'hide_courses_in_sessions' => 'Session',
281
            'enable_quiz_scenario' => 'Course',
282
            'filter_terms' => 'Security',
283
            'header_extra_content' => 'Tracking',
284
            'footer_extra_content' => 'Tracking',
285
            'show_documents_preview' => 'Tools',
286
            'htmlpurifier_wiki' => 'Editor',
287
            'cas_activate' => 'CAS',
288
            'cas_server' => 'CAS',
289
            'cas_server_uri' => 'CAS',
290
            'cas_port' => 'CAS',
291
            'cas_protocol' => 'CAS',
292
            'cas_add_user_activate' => 'CAS',
293
            'update_user_info_cas_with_ldap' => 'CAS',
294
            'student_page_after_login' => 'Platform',
295
            'teacher_page_after_login' => 'Platform',
296
            'drh_page_after_login' => 'Platform',
297
            'sessionadmin_page_after_login' => 'Session',
298
            'student_autosubscribe' => 'Platform',
299
            'teacher_autosubscribe' => 'Platform',
300
            'drh_autosubscribe' => 'Platform',
301
            'sessionadmin_autosubscribe' => 'Session',
302
            'scorm_cumulative_session_time' => 'Course',
303
            'allow_hr_skills_management' => 'Gradebook',
304
            'enable_help_link' => 'Platform',
305
            'teachers_can_change_score_settings' => 'Gradebook',
306
            'allow_users_to_change_email_with_no_password' => 'User',
307
            'show_admin_toolbar' => 'display',
308
            'allow_global_chat' => 'Platform',
309
            'languagePriority1' => 'Languages',
310
            'languagePriority2' => 'Languages',
311
            'languagePriority3' => 'Languages',
312
            'languagePriority4' => 'Languages',
313
            'login_is_email' => 'Platform',
314
            'courses_default_creation_visibility' => 'Course',
315
            'gradebook_enable_grade_model' => 'Gradebook',
316
            'teachers_can_change_grade_model_settings' => 'Gradebook',
317
            'gradebook_default_weight' => 'Gradebook',
318
            'ldap_description' => 'LDAP',
319
            'shibboleth_description' => 'Shibboleth',
320
            'facebook_description' => 'Facebook',
321
            'gradebook_locking_enabled' => 'Gradebook',
322
            'gradebook_default_grade_model_id' => 'Gradebook',
323
            'allow_session_admins_to_manage_all_sessions' => 'Session',
324
            'allow_skills_tool' => 'Platform',
325
            'allow_public_certificates' => 'Course',
326
            'platform_unsubscribe_allowed' => 'Platform',
327
            'activate_email_template' => 'Platform',
328
            'enable_iframe_inclusion' => 'Editor',
329
            'show_hot_courses' => 'Platform',
330
            'enable_webcam_clip' => 'Tools',
331
            'use_custom_pages' => 'Platform',
332
            'tool_visible_by_default_at_creation' => 'Tools',
333
            'prevent_session_admins_to_manage_all_users' => 'Session',
334
            'documents_default_visibility_defined_in_course' => 'Tools',
335
            'enabled_mathjax' => 'Editor',
336
            'meta_twitter_site' => 'Tracking',
337
            'meta_twitter_creator' => 'Tracking',
338
            'meta_title' => 'Tracking',
339
            'meta_description' => 'Tracking',
340
            'meta_image_path' => 'Tracking',
341
            'allow_teachers_to_create_sessions' => 'Session',
342
            'institution_address' => 'Platform',
343
            'chamilo_database_version' => 'null',
344
            'cron_remind_course_finished_activate' => 'Crons',
345
            'cron_remind_course_expiration_frequency' => 'Crons',
346
            'cron_remind_course_expiration_activate' => 'Crons',
347
            'allow_coach_feedback_exercises' => 'Session',
348
            'allow_my_files' => 'Platform',
349
            'ticket_allow_student_add' => 'Ticket',
350
            'ticket_send_warning_to_all_admins' => 'Ticket',
351
            'ticket_warn_admin_no_user_in_category' => 'Ticket',
352
            'ticket_allow_category_edition' => 'Ticket',
353
            'load_term_conditions_section' => 'Platform',
354
            'show_terms_if_profile_completed' => 'Ticket',
355
            'hide_home_top_when_connected' => 'Platform',
356
            'hide_global_announcements_when_not_connected' => 'Platform',
357
            'course_creation_use_template' => 'Course',
358
            'allow_strength_pass_checker' => 'Security',
359
            'allow_captcha' => 'Security',
360
            'captcha_number_mistakes_to_block_account' => 'Security',
361
            'captcha_time_to_block' => 'Security',
362
            'drh_can_access_all_session_content' => 'Session',
363
            'display_groups_forum_in_general_tool' => 'Tools',
364
            'allow_tutors_to_assign_students_to_session' => 'Session',
365
            'allow_lp_return_link' => 'Course',
366
            'hide_scorm_export_link' => 'Course',
367
            'hide_scorm_copy_link' => 'Course',
368
            'hide_scorm_pdf_link' => 'Course',
369
            'session_days_before_coach_access' => 'Session',
370
            'session_days_after_coach_access' => 'Session',
371
            'pdf_logo_header' => 'Course',
372
            'order_user_list_by_official_code' => 'Platform',
373
            'email_alert_manager_on_new_quiz' => 'Tools',
374
            'show_official_code_exercise_result_list' => 'Tools',
375
            'course_catalog_hide_private' => 'Platform',
376
            'catalog_show_courses_sessions' => 'Platform',
377
            'auto_detect_language_custom_pages' => 'Platform',
378
            'lp_show_reduced_report' => 'Tools',
379
            'allow_session_course_copy_for_teachers' => 'Session',
380
            'hide_logout_button' => 'Platform',
381
            'redirect_admin_to_courses_list' => 'Platform',
382
            'course_images_in_courses_list' => 'Course',
383
            'student_publication_to_take_in_gradebook' => 'Gradebook',
384
            'certificate_filter_by_official_code' => 'Gradebook',
385
            'exercise_max_ckeditors_in_page' => 'Tools',
386
            'document_if_file_exists_option' => 'Tools',
387
            'add_gradebook_certificates_cron_task_enabled' => 'Tools',
388
            'openbadges_backpack' => 'Gradebook',
389
            'cookie_warning' => 'Tools',
390
            'hide_course_group_if_no_tools_available' => 'Tools',
391
            'catalog_allow_session_auto_subscription' => 'Session',
392
            'registration.soap.php.decode_utf8' => 'Platform',
393
            'allow_delete_attendance' => 'Tools',
394
            'gravatar_enabled' => 'Platform',
395
            'gravatar_type' => 'Platform',
396
            'limit_session_admin_role' => 'Session',
397
            'show_session_description' => 'Session',
398
            'hide_certificate_export_link_students' => 'Gradebook',
399
            'hide_certificate_export_link' => 'Gradebook',
400
            'dropbox_hide_course_coach' => 'Tools',
401
            'dropbox_hide_general_coach' => 'Tools',
402
            'sso_force_redirect' => 'Security',
403
            'session_course_ordering' => 'Session',
404
            'gamification_mode' => 'Platform',
405
            'prevent_multiple_simultaneous_login' => 'Security',
406
            'gradebook_detailed_admin_view' => 'Gradebook',
407
            'course_catalog_published' => 'Course',
408
            'user_reset_password' => 'Security',
409
            'user_reset_password_token_limit' => 'Security',
410
            'my_courses_view_by_session' => 'Session',
411
            'show_full_skill_name_on_skill_wheel' => 'Platform',
412
            'messaging_allow_send_push_notification' => 'WebServices',
413
            'messaging_gdc_project_number' => 'WebServices',
414
            'messaging_gdc_api_key' => 'WebServices',
415
            'teacher_can_select_course_template' => 'Course',
416
            'enable_record_audio' => 'Tools',
417
            'allow_show_skype_account' => 'Platform',
418
            'allow_show_linkedin_url' => 'Platform',
419
            'enable_profile_user_address_geolocalization' => 'User',
420
            'show_official_code_whoisonline' => 'User',
421
            'icons_mode_svg' => 'Tuning',
422
            'user_name_sort_by' => 'display'
423
        ];
424
425
        return $oldItems;
426
    }
427
428
    /**
429
     * Rename old variable with variable used in Chamilo 2.0
430
     * @param string $variable
431
     * @return mixed
432
     */
433
    public function renameVariable($variable)
434
    {
435
        $list = [
436
            'timezone_value' => 'timezone',
437
            'Institution' => 'institution',
438
            'SiteName' => 'site_name',
439
            'siteName' => 'site_name',
440
            'InstitutionUrl' => 'institution_url',
441
            'registration' => 'required_profile_fields',
442
            'profile' => 'changeable_options',
443
            'stylesheets' => 'theme',
444
            'platformLanguage' => 'platform_language',
445
            'languagePriority1' => 'language_priority_1',
446
            'languagePriority2' => 'language_priority_2',
447
            'languagePriority3' => 'language_priority_3',
448
            'languagePriority4' => 'language_priority_4',
449
            'gradebook_score_display_coloring' => 'my_display_coloring',
450
            'document_if_file_exists_option' => 'if_file_exists_option',
451
            'ProfilingFilterAddingUsers' => 'profiling_filter_adding_users',
452
            'course_create_active_tools' => 'active_tools_on_create',
453
            'emailAdministrator' => 'administrator_email',
454
            'administratorSurname' => 'administrator_surname',
455
            'administratorName' => 'administrator_name',
456
            'administratorTelephone' => 'administrator_phone',
457
            'registration.soap.php.decode_utf8' => 'decode_utf8',
458
        ];
459
460
        return isset($list[$variable]) ? $list[$variable] : $variable;
461
    }
462
463
    /**
464
     * Replace old Chamilo 1.x category with 2.0 version
465
     * @param string $variable
466
     * @param string $defaultCategory
467
     * @return mixed
468
     */
469
    public function fixCategory($variable, $defaultCategory)
470
    {
471
        $settings = [
472
            'cookie_warning' => 'platform',
473
            'donotlistcampus' => 'platform',
474
            'administrator_email' => 'admin',
475
            'administrator_surname' => 'admin',
476
            'administrator_name' => 'admin',
477
            'administrator_phone' => 'admin',
478
            'exercise_max_ckeditors_in_page' => 'exercise',
479
            'allow_hr_skills_management' => 'skill',
480
            'accessibility_font_resize' => 'display',
481
            'account_valid_duration' => 'profile',
482
            'activate_email_template' => 'mail',
483
            'allow_global_chat' => 'chat',
484
            'allow_lostpassword' => 'registration',
485
            'allow_registration' => 'registration',
486
            'allow_registration_as_teacher' => 'registration',
487
            'required_profile_fields' => 'registration',
488
            'allow_skills_tool' => 'skill',
489
            'allow_students_to_browse_courses' => 'display',
490
            'allow_terms_conditions' => 'registration',
491
            'allow_users_to_create_courses' => 'course',
492
            'auto_detect_language_custom_pages' => 'language',
493
            'platform_language' => 'language',
494
            'course_validation' => 'course',
495
            'course_validation_terms_and_conditions_url' => 'course',
496
            'display_categories_on_homepage' => 'display',
497
            'display_coursecode_in_courselist' => 'course',
498
            'display_teacher_in_courselist' => 'course',
499
            'drh_autosubscribe' => 'registration',
500
            'drh_page_after_login' => 'registration',
501
            'enable_help_link' => 'display',
502
            'example_material_course_creation' => 'course',
503
            'login_is_email' => 'profile',
504
            'noreply_email_address' => 'mail',
505
            'page_after_login' => 'registration',
506
            'pdf_export_watermark_by_course' => 'document',
507
            'pdf_export_watermark_enable' => 'document',
508
            'pdf_export_watermark_text' => 'document',
509
            'platform_unsubscribe_allowed' => 'registration',
510
            'send_email_to_admin_when_create_course' => 'course',
511
            'show_admin_toolbar' => 'display',
512
            'show_administrator_data' => 'display',
513
            'show_back_link_on_top_of_tree' => 'display',
514
            'show_closed_courses' => 'display',
515
            'show_different_course_language' => 'display',
516
            'show_email_addresses' => 'display',
517
            'show_empty_course_categories' => 'display',
518
            'show_full_skill_name_on_skill_wheel' => 'skill',
519
            'show_hot_courses' => 'display',
520
            'show_link_bug_notification' => 'display',
521
            'show_number_of_courses' => 'display',
522
            'show_teacher_data' => 'display',
523
            'showonline' => 'display',
524
            'student_autosubscribe' => 'registration',
525
            'student_page_after_login' => 'registration',
526
            'student_view_enabled' => 'course',
527
            'teacher_autosubscribe' => 'registration',
528
            'teacher_page_after_login' => 'registration',
529
            'time_limit_whosonline' => 'display',
530
            'user_selected_theme' => 'profile',
531
            'hide_global_announcements_when_not_connected' => 'announcement',
532
            'hide_home_top_when_connected' => 'display',
533
            'hide_logout_button' => 'display',
534
            'institution_address' => 'platform',
535
            'redirect_admin_to_courses_list' => 'admin',
536
            'decode_utf8' => 'webservice',
537
            'use_custom_pages' => 'platform',
538
            'allow_group_categories' => 'group',
539
            'allow_user_headings' => 'display',
540
            'default_document_quotum' => 'document',
541
            'default_forum_view' => 'forum',
542
            'default_group_quotum' => 'document',
543
            'enable_quiz_scenario' => 'exercise',
544
            'exercise_max_score' => 'exercise',
545
            'exercise_min_score' => 'exercise',
546
            'pdf_logo_header' => 'platform',
547
            'show_glossary_in_documents' => 'document',
548
            'show_glossary_in_extra_tools' => 'glossary',
549
            //'show_toolshortcuts' => '',
550
            'survey_email_sender_noreply'=> 'survey',
551
            'allow_coach_feedback_exercises' => 'exercise',
552
            'sessionadmin_autosubscribe' => 'registration',
553
            'sessionadmin_page_after_login' => 'registration',
554
            'show_tutor_data' => 'display'
555
        ];
556
557
        return isset($settings[$variable]) ? $settings[$variable] : $defaultCategory;
558
    }
559
560
    /**
561
     * @param $name
562
     * @return mixed
563
     * @throws \InvalidArgumentException
564
     */
565
    public function getSetting($name)
566
    {
567
        if (false === strpos($name, '.')) {
568
            //throw new \InvalidArgumentException(sprintf('Parameter must be in format "namespace.name", "%s" given.', $name));
569
570
            // This code allows the possibility of calling
571
            // api_get_setting('allow_skills_tool') instead of
572
            // the correct way api_get_setting('platform.allow_skills_tool')
573
            $items = $this->getVariablesAndCategories();
574
575
            if (isset($items[$name])) {
576
                $originalName = $name;
577
                $name = $this->renameVariable($name);
578
                $category = $this->fixCategory(
579
                    strtolower($name),
580
                    strtolower($items[$originalName])
581
                );
582
                $name = $category.'.'.$name;
583
            } else {
584
                throw new \InvalidArgumentException(sprintf('Parameter must be in format "category.name", "%s" given.', $name));
585
            }
586
        }
587
588
        list($category, $name) = explode('.', $name);
589
        $settings = $this->load($category, $name);
590
591
        return $settings->get($name);
592
    }
593
594
    /**
595
     * @param string $category
596
     * @return string
597
     */
598
    public function convertNameSpaceToService($category)
599
    {
600
        return 'chamilo_core.settings.'.$category;
601
        //return ''.$namespace;
602
    }
603
604
    /**
605
     * {@inheritdoc}
606
     */
607
    public function load($schemaAlias, $namespace = null, $ignoreUnknown = true)
608
    {
609
        /*$schema = $this->schemaRegistry->get($schemaAlias);
610
        $resolver = $this->resolverRegistry->get($schemaAlias);
611
612
        // try to resolve settings for schema alias and namespace
613
        $settings = $resolver->resolve($schemaAlias, $namespace);
614
615
        $parameters = $this->getParameters($namespace);
616
617
        $schema = $this->schemaRegistry->get($namespace);
618
619
        $settingsBuilder = new SettingsBuilder();
620
        $schema->buildSettings($settingsBuilder);
621
622
        foreach ($settingsBuilder->getTransformers() as $parameter => $transformer) {
623
            if (array_key_exists($parameter, $parameters)) {
624
                $parameters[$parameter] = $transformer->reverseTransform($parameters[$parameter]);
625
            }
626
        }
627
        $parameters = $settingsBuilder->resolve($parameters);
628
629
        return $this->resolvedSettings[$namespace] = new Settings($parameters);*/
630
631
        /*$schema = $this->schemaRegistry->get($schemaAlias);
632
        $resolver = $this->resolverRegistry->get($schemaAlias);
633
634
        // try to resolve settings for schema alias and namespace
635
        $settings = $resolver->resolve($schemaAlias, $namespace);
636
637
        if (!$settings) {
638
            $settings = $this->settingsFactory->createNew();
639
            $settings->setSchemaAlias($schemaAlias);
640
        }*/
641
642
        //$schemaAlias = 'chamilo_core.settings.'.$schemaAlias;
643
644
        $schemaAlias = 'chamilo_core.settings.'.$schemaAlias;
645
646
        /** @var SchemaInterface $schema */
647
        $schema = $this->schemaRegistry->get($schemaAlias);
648
649
        /** @var SettingsResolverInterface $resolver */
650
        $resolver = $this->resolverRegistry->get($schemaAlias);
651
652
        // try to resolve settings for schema alias and namespace
653
        $settings = $resolver->resolve($schemaAlias, $namespace);
654
655
        if (!$settings) {
656
            $settings = $this->settingsFactory->createNew();
657
            $settings->setSchemaAlias($schemaAlias);
658
        }
659
660
         // We need to get a plain parameters array since we use the options resolver on it
661
        $parameters = $settings->getParameters();
662
663
        $settingsBuilder = new SettingsBuilder();
664
        $schema->buildSettings($settingsBuilder);
665
666
        // Remove unknown settings' parameters (e.g. From a previous version of the settings schema)
667
        if (true === $ignoreUnknown) {
668
            foreach ($parameters as $name => $value) {
669
                if (!$settingsBuilder->isDefined($name)) {
670
                    unset($parameters[$name]);
671
                }
672
            }
673
        }
674
675
        $parameters = $settingsBuilder->resolve($parameters);
676
        $settings->setParameters($parameters);
677
678
        return $settings;
679
    }
680
681
    /**
682
     * {@inheritdoc}
683
     * @throws ValidatorException
684
     */
685
    //public function save($namespace, $settings)
686
    public function save(SettingsInterface $settings)
687
    {
688
        $namespace = $settings->getSchemaAlias();
689
690
        /** @var SchemaInterface $schema */
691
        $schema = $this->schemaRegistry->get($settings->getSchemaAlias());
692
693
        $settingsBuilder = new SettingsBuilder();
694
        $schema->buildSettings($settingsBuilder);
695
696
        $parameters = $settingsBuilder->resolve($settings->getParameters());
697
        $settings->setParameters($parameters);
698
699
        /*foreach ($settingsBuilder->getTransformers() as $parameter => $transformer) {
700
            if (array_key_exists($parameter, $parameters)) {
701
                $parameters[$parameter] = $transformer->transform($parameters[$parameter]);
702
            }
703
        }*/
704
705
        /*if (isset($this->resolvedSettings[$namespace])) {
706
            $transformedParameters = $this->transformParameters($settingsBuilder, $parameters);
707
            $this->resolvedSettings[$namespace]->setParameters($transformedParameters);
708
        }*/
709
710
        $repo = $this->manager->getRepository('ChamiloCoreBundle:SettingsCurrent');
711
        $persistedParameters = $repo->findBy(array('category' => $settings->getSchemaAlias()));
712
        $persistedParametersMap = array();
713
714
        foreach ($persistedParameters as $parameter) {
715
            $persistedParametersMap[$parameter->getTitle()] = $parameter;
716
        }
717
718
        /** @var SettingsEvent $event */
719
        /*$event = $this->eventDispatcher->dispatch(
720
            SettingsEvent::PRE_SAVE,
721
            new SettingsEvent($settings, $parameters)
722
        );*/
723
724
        /** @var \Chamilo\CoreBundle\Entity\SettingsCurrent $url */
725
        //$url = $event->getArgument('url');
726
        $url = $this->getUrl();
727
728
        $simpleCategoryName = str_replace('chamilo_core.settings.', '', $namespace);
729
730
        foreach ($parameters as $name => $value) {
731
            if (isset($persistedParametersMap[$name])) {
732
                $persistedParametersMap[$name]->setValue($value);
733
            } else {
734
                $parameter = new SettingsCurrent();
735
736
                $parameter
737
                    ->setVariable($name)
738
                    ->setCategory($simpleCategoryName)
739
                    ->setTitle($name)
740
                    ->setSelectedValue($value)
741
                    ->setUrl($url)
742
                    ->setAccessUrlChangeable(1)
743
                    ->setAccessUrlLocked(1)
744
                ;
745
746
                /* @var $errors ConstraintViolationListInterface */
747
                /*$errors = $this->validator->validate($parameter);
748
                if (0 < $errors->count()) {
749
                    throw new ValidatorException($errors->get(0)->getMessage());
750
                }*/
751
752
                $this->manager->persist($parameter);
753
            }
754
        }
755
756
        $this->manager->flush();
757
758
        return;
759
760
        ////
761
        $schemaAlias = $settings->getSchemaAlias();
0 ignored issues
show
Unused Code introduced by
//// $schemaAlias = $settings->getSchemaAlias(); does not seem to be 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...
762
        $schemaAliasChamilo = str_replace('chamilo_core.settings.', '', $schemaAlias);
763
764
        $schema = $this->schemaRegistry->get($schemaAlias);
765
766
        $settingsBuilder = new SettingsBuilder();
767
        $schema->buildSettings($settingsBuilder);
768
769
        $parameters = $settingsBuilder->resolve($settings->getParameters());
770
771 View Code Duplication
        foreach ($settingsBuilder->getTransformers() as $parameter => $transformer) {
772
            if (array_key_exists($parameter, $parameters)) {
773
                $parameters[$parameter] = $transformer->transform($parameters[$parameter]);
774
            }
775
        }
776
777
        /** @var \Sylius\Bundle\SettingsBundle\Event\SettingsEvent $event */
778
        $event = $this->eventDispatcher->dispatch(
0 ignored issues
show
Bug introduced by
The call to dispatch() misses a required argument $resource.

This check looks for function calls that miss required arguments.

Loading history...
779
            SettingsEvent::PRE_SAVE,
780
            new SettingsEvent($settings)
781
        );
782
783
        /** @var \Chamilo\CoreBundle\Entity\SettingsCurrent $url */
784
        $url = $event->getSettings()->getAccessUrl();
785
786
        foreach ($parameters as $name => $value) {
787
            if (isset($persistedParametersMap[$name])) {
788
                if ($value instanceof Course) {
789
                    $value = $value->getId();
790
                }
791
                $persistedParametersMap[$name]->setValue($value);
792
            } else {
793
                /** @var SettingsCurrent $setting */
794
                $setting = $this->settingsFactory->createNew();
795
                $setting->setSchemaAlias($schemaAlias);
796
797
                $setting
798
                    ->setNamespace($schemaAliasChamilo)
799
                    ->setName($name)
800
                    ->setValue($value)
801
                    ->setUrl($url)
802
                    ->setAccessUrlLocked(0)
803
                    ->setAccessUrlChangeable(1)
804
                ;
805
806
807
                /* @var $errors ConstraintViolationListInterface */
808
                /*$errors = $this->->validate($parameter);
809
                if (0 < $errors->count()) {
810
                    throw new ValidatorException($errors->get(0)->getMessage());
811
                }*/
812
                $this->manager->persist($setting);
813
                $this->manager->flush();
814
            }
815
        }
816
        /*$parameters = $settingsBuilder->resolve($settings->getParameters());
817
        $settings->setParameters($parameters);
818
819
        $this->eventDispatcher->dispatch(SettingsEvent::PRE_SAVE, new SettingsEvent($settings));
820
821
        $this->manager->persist($settings);
822
        $this->manager->flush();
823
824
        $this->eventDispatcher->dispatch(SettingsEvent::POST_SAVE, new SettingsEvent($settings));*/
825
    }
826
827
    /**
828
     * Load parameter from database.
829
     *
830
     * @param string $namespace
831
     *
832
     * @return array
833
     */
834
    private function getParameters($namespace)
0 ignored issues
show
Unused Code introduced by
This method is not used, and could be removed.
Loading history...
835
    {
836
        //$repo = $this->parameterRepository;
837
        $repo = $this->manager->getRepository('ChamiloCoreBundle:SettingsCurrent');
838
        $parameters = [];
839
        foreach ($repo->findBy(array('category' => $namespace)) as $parameter) {
840
            $parameters[$parameter->getName()] = $parameter->getValue();
841
        }
842
843
        return $parameters;
844
    }
845
846
    public function getParametersFromKeyword($namespace, $keyword = '')
847
    {
848
        $criteria = array('category' => $namespace);
849
        if (!empty($keyword)) {
850
            $criteria['variable'] = $keyword;
851
        }
852
853
        $parametersFromDb = $this->parameterRepository->findBy($criteria);
854
855
        $parameters = array();
856
        /** @var \Chamilo\CoreBundle\Entity\SettingsCurrent $parameter */
857
        foreach ($parametersFromDb as $parameter) {
858
            $parameters[$parameter->getName()] = $parameter->getValue();
859
        }
860
861
        return $parameters;
862
    }
863
864
    private function transformParameters(SettingsBuilder $settingsBuilder, array $parameters)
0 ignored issues
show
Unused Code introduced by
This method is not used, and could be removed.
Loading history...
865
    {
866
        $transformedParameters = $parameters;
867
868 View Code Duplication
        foreach ($settingsBuilder->getTransformers() as $parameter => $transformer) {
869
            if (array_key_exists($parameter, $parameters)) {
870
                $transformedParameters[$parameter] = $transformer->reverseTransform($parameters[$parameter]);
871
            }
872
        }
873
874
        return $transformedParameters;
875
    }
876
}
877