Passed
Push — master ( 50933b...355f71 )
by
unknown
11:10 queued 13s
created

courseSettingsList()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 27
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 16
c 0
b 0
f 0
nc 3
nop 3
dl 0
loc 27
rs 9.7333
1
<?php
2
3
declare(strict_types=1);
4
5
/* For licensing terms, see /license.txt */
6
7
namespace Chamilo\CoreBundle\Controller;
8
9
use Bbb;
10
use Chamilo\CoreBundle\Repository\Node\CourseRepository;
11
use Chamilo\CoreBundle\ServiceHelper\AuthenticationConfigHelper;
12
use Chamilo\CoreBundle\ServiceHelper\ThemeHelper;
13
use Chamilo\CoreBundle\ServiceHelper\TicketProjectHelper;
14
use Chamilo\CoreBundle\ServiceHelper\UserHelper;
15
use Chamilo\CoreBundle\Settings\SettingsManager;
16
use Chamilo\CoreBundle\Traits\ControllerTrait;
17
use Chamilo\CourseBundle\Settings\SettingsCourseManager;
18
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
19
use Symfony\Component\HttpFoundation\JsonResponse;
20
use Symfony\Component\HttpFoundation\Request;
21
use Symfony\Component\HttpFoundation\Response;
22
use Symfony\Component\Routing\Attribute\Route;
23
use Throwable;
24
25
#[Route('/platform-config')]
26
class PlatformConfigurationController extends AbstractController
27
{
28
    use ControllerTrait;
29
30
    public function __construct(
31
        private readonly TicketProjectHelper $ticketProjectHelper,
32
        private readonly UserHelper $userHelper,
33
        private readonly ThemeHelper $themeHelper,
34
        private readonly AuthenticationConfigHelper $authenticationConfigHelper,
35
    ) {}
36
37
    #[Route('/list', name: 'platform_config_list', methods: ['GET'])]
38
    public function list(SettingsManager $settingsManager): Response
39
    {
40
        $requestSession = $this->getRequest()->getSession();
41
42
        $configuration = [
43
            'settings' => [],
44
            'studentview' => $requestSession->get('studentview'),
45
            'plugins' => [],
46
            'visual_theme' => $this->themeHelper->getVisualTheme(),
47
            'oauth2_providers' => $this->authenticationConfigHelper->getEnabledOAuthProviders(),
48
        ];
49
50
        $configuration['settings']['registration.allow_registration'] = $settingsManager->getSetting('registration.allow_registration', true);
51
        $configuration['settings']['course.course_catalog_published'] = $settingsManager->getSetting('course.course_catalog_published', true);
52
        $configuration['settings']['course.catalog_hide_public_link'] = $settingsManager->getSetting('course.catalog_hide_public_link', true);
53
        $configuration['settings']['course.allow_course_extra_field_in_catalog'] = $settingsManager->getSetting('course.allow_course_extra_field_in_catalog', true);
54
        $configuration['settings']['course.course_catalog_display_in_home'] = $settingsManager->getSetting('course.course_catalog_display_in_home', true);
55
        $configuration['settings']['course.courses_catalogue_show_only_category'] = $settingsManager->getSetting('course.courses_catalogue_show_only_category', true);
56
        $configuration['settings']['display.allow_students_to_browse_courses'] = $settingsManager->getSetting('display.allow_students_to_browse_courses', true);
57
        $configuration['settings']['session.catalog_allow_session_auto_subscription'] = $settingsManager->getSetting('session.catalog_allow_session_auto_subscription', true);
58
        $configuration['settings']['session.catalog_course_subscription_in_user_s_session'] = $settingsManager->getSetting('session.catalog_course_subscription_in_user_s_session', true);
59
        $rawCourseCatalogSetting = $settingsManager->getSetting('course.course_catalog_settings', true);
60
        $configuration['settings']['course.course_catalog_settings'] = 'false' !== $rawCourseCatalogSetting ? $this->decodeSettingArray($rawCourseCatalogSetting) : 'false';
61
        $rawSessionCatalogSetting = $settingsManager->getSetting('session.catalog_settings', true);
62
        $configuration['settings']['session.catalog_settings'] = 'false' !== $rawSessionCatalogSetting ? $this->decodeSettingArray($rawSessionCatalogSetting) : 'false';
63
64
        $variables = [];
65
66
        if ($this->isGranted('ROLE_USER')) {
67
            $variables = [
68
                'platform.site_name',
69
                'platform.timezone',
70
                'platform.registered',
71
                'platform.donotlistcampus',
72
                'platform.load_term_conditions_section',
73
                'platform.cookie_warning',
74
                'platform.show_tabs',
75
                'platform.catalog_show_courses_sessions',
76
                'admin.administrator_name',
77
                'admin.administrator_surname',
78
                'editor.enabled_mathjax',
79
                'editor.translate_html',
80
                'display.show_admin_toolbar',
81
                'registration.allow_terms_conditions',
82
                'agenda.allow_personal_agenda',
83
                'agenda.personal_calendar_show_sessions_occupation',
84
                'social.social_enable_messages_feedback',
85
                'social.disable_dislike_option',
86
                'skill.allow_skills_tool',
87
                'gradebook.gradebook_enable_grade_model',
88
                'gradebook.gradebook_dependency',
89
                'course.course_validation',
90
                'course.student_view_enabled',
91
                'course.allow_edit_tool_visibility_in_session',
92
                'session.limit_session_admin_role',
93
                'session.allow_session_admin_read_careers',
94
                'session.limit_session_admin_list_users',
95
                'platform.redirect_index_to_url_for_logged_users',
96
                'language.platform_language',
97
                'language.language_priority_1',
98
                'language.language_priority_2',
99
                'language.language_priority_3',
100
                'language.language_priority_4',
101
                'profile.allow_social_map_fields',
102
                'forum.global_forums_course_id',
103
                'document.students_download_folders',
104
                'social.hide_social_groups_block',
105
                'course.show_course_duration',
106
                'attendance.attendance_allow_comments',
107
                'attendance.multilevel_grading',
108
                'attendance.enable_sign_attendance_sheet',
109
                'exercise.allow_exercise_auto_launch',
110
                'course.access_url_specific_files',
111
                'platform.course_catalog_hide_private',
112
                'course.show_courses_descriptions_in_catalog',
113
                'session.session_automatic_creation_user_id',
114
                'session.session_list_view_remaining_days',
115
                'profile.use_users_timezone',
116
            ];
117
118
            $user = $this->userHelper->getCurrent();
119
120
            $configuration['settings']['display.show_link_ticket_notification'] = 'false';
121
122
            if (!empty($user)) {
123
                $userIsAllowedInProject = $this->ticketProjectHelper->userIsAllowInProject(1);
124
125
                if ($userIsAllowedInProject
126
                    && 'true' === $settingsManager->getSetting('display.show_link_ticket_notification')
127
                ) {
128
                    $configuration['settings']['display.show_link_ticket_notification'] = 'true';
129
                }
130
            }
131
132
            $configuration['plugins']['bbb'] = [
133
                'show_global_conference_link' => Bbb::showGlobalConferenceLink([
134
                    'username' => $user->getUserIdentifier(),
135
                    'status' => $user->getStatus(),
136
                ]),
137
                'listingURL' => (new Bbb('', '', true, $user->getId()))->getListingUrl(),
138
            ];
139
        }
140
141
        foreach ($variables as $variable) {
142
            $value = $settingsManager->getSetting($variable, true);
143
144
            $configuration['settings'][$variable] = $value;
145
        }
146
147
        return new JsonResponse($configuration);
148
    }
149
150
    #[Route('/list/course_settings', name: 'course_settings_list', methods: ['GET'])]
151
    public function courseSettingsList(
152
        SettingsCourseManager $courseSettingsManager,
153
        CourseRepository $courseRepository,
154
        Request $request
155
    ): JsonResponse {
156
        $courseId = $request->query->get('cid');
157
        if (!$courseId) {
158
            return new JsonResponse(['error' => 'Course ID is required'], Response::HTTP_BAD_REQUEST);
159
        }
160
161
        $course = $courseRepository->find($courseId);
162
        if (!$course) {
163
            return new JsonResponse(['error' => 'Course not found'], Response::HTTP_NOT_FOUND);
164
        }
165
166
        $courseSettingsManager->setCourse($course);
167
        $settings = [
168
            'show_course_in_user_language' => $courseSettingsManager->getCourseSettingValue('show_course_in_user_language'),
169
            'allow_user_edit_agenda' => $courseSettingsManager->getCourseSettingValue('allow_user_edit_agenda'),
170
            'enable_document_auto_launch' => $courseSettingsManager->getCourseSettingValue('enable_document_auto_launch'),
171
            'enable_exercise_auto_launch' => $courseSettingsManager->getCourseSettingValue('enable_exercise_auto_launch'),
172
            'enable_lp_auto_launch' => $courseSettingsManager->getCourseSettingValue('enable_lp_auto_launch'),
173
            'enable_forum_auto_launch' => $courseSettingsManager->getCourseSettingValue('enable_forum_auto_launch'),
174
        ];
175
176
        return new JsonResponse(['settings' => $settings]);
177
    }
178
179
    /**
180
     * Attempts to decode a setting value that may be stored as:
181
     * - native PHP array
182
     * - JSON string
183
     * - PHP array code string
184
     */
185
    private function decodeSettingArray(mixed $setting): array
186
    {
187
        // Already an array, return as is
188
        if (\is_array($setting)) {
189
            return $setting;
190
        }
191
192
        // Try to decode JSON string
193
        if (\is_string($setting)) {
194
            $json = json_decode($setting, true);
195
            if (\is_array($json)) {
196
                return $json;
197
            }
198
199
            // Try to evaluate PHP-style array string
200
            $trimmed = rtrim($setting, ';');
201
202
            try {
203
                $evaluated = eval("return $trimmed;");
0 ignored issues
show
introduced by
The use of eval() is discouraged.
Loading history...
204
                if (\is_array($evaluated)) {
205
                    return $evaluated;
206
                }
207
            } catch (Throwable $e) {
208
                // Log error and continue
209
                error_log('Failed to eval setting value: '.$e->getMessage());
210
            }
211
        }
212
213
        // Return empty array as fallback
214
        return [];
215
    }
216
}
217