diff --git a/assets/vue/composables/locale.js b/assets/vue/composables/locale.js index f1159fa60a..eeac7de4db 100644 --- a/assets/vue/composables/locale.js +++ b/assets/vue/composables/locale.js @@ -1,62 +1,51 @@ -import {usePlatformConfig} from "../store/platformConfig"; -import {useSecurityStore} from "../store/securityStore"; -import {useCidReqStore} from "../store/cidReq"; -import {computed, ref} from "vue"; +import { usePlatformConfig } from "../store/platformConfig" +import { useSecurityStore } from "../store/securityStore" +import { useCidReqStore } from "../store/cidReq" +import { useCourseSettings } from "../store/courseSettingStore" +import { computed, ref, watch } from "vue" export function useLocale() { const platformConfigStore = usePlatformConfig() const securityStore = useSecurityStore() const cidReqStore = useCidReqStore() + const courseSettingsStore = useCourseSettings() - const localeList = computed(() => { - const list = {}; - - const platformLocale = platformConfigStore.getSetting('language.platform_language') - - if (platformLocale) { - list['platform_lang'] = platformLocale - } + const appLocale = ref(document.querySelector('html').lang) - const userLocale = securityStore.user ? securityStore.user.locale : null - - if (userLocale) { - list['user_profil_lang'] = userLocale + watch(() => { + return cidReqStore.course ? cidReqStore.course.id : null + }, (newId, oldId) => { + if (newId) { + courseSettingsStore.loadCourseSettings(newId) } + }, { immediate: true }) - const courseLocale = cidReqStore.course ? cidReqStore.course.courseLanguage : null - - if (courseLocale) { - list['course_lang'] = courseLocale + const localeList = computed(() => { + const list = {} + list['platform_lang'] = platformConfigStore.getSetting('language.platform_language') + list['user_profil_lang'] = securityStore.user ? securityStore.user.locale : null + + let courseLang = null + if (courseSettingsStore.getSetting('show_course_in_user_language') === '1' && securityStore.user && securityStore.user.locale) { + courseLang = securityStore.user.locale + } else if (cidReqStore.course) { + courseLang = cidReqStore.course.courseLanguage } - - // @todo check language from request - //list['user_selected_lang'] = ? + list['course_lang'] = courseLang return list }); - const priorityList = [ - 'language_priority_1', - 'language_priority_2', - 'language_priority_3', - 'language_priority_4', - ] - - const appLocale = ref('') - - for (const setting of priorityList) { - const priority = platformConfigStore.getSetting(`language.${setting}`) - - if (priority && localeList.value[priority]) { - appLocale.value = localeList.value[priority] - - break + watch(localeList, (newLocaleList) => { + const priorityList = ['language_priority_1', 'language_priority_2', 'language_priority_3', 'language_priority_4'] + for (const priority of priorityList) { + const setting = platformConfigStore.getSetting(`language.${priority}`) + if (setting && newLocaleList[setting]) { + appLocale.value = newLocaleList[setting] + break + } } - } - - if (!appLocale.value) { - appLocale.value = document.querySelector('html').lang - } + }, { immediate: true }) return { appLocale diff --git a/assets/vue/store/courseSettingStore.js b/assets/vue/store/courseSettingStore.js new file mode 100644 index 0000000000..fbbcbdc690 --- /dev/null +++ b/assets/vue/store/courseSettingStore.js @@ -0,0 +1,31 @@ +import { defineStore } from "pinia" +import axios from "axios" +import { computed, ref } from "vue" + +export const useCourseSettings = defineStore("courseSettings", () => { + const isLoading = ref(false) + const settings = ref({}) + + async function loadCourseSettings(courseId) { + isLoading.value = true + try { + const { data } = await axios.get(`/platform-config/list/course_settings?cid=${courseId}`) + settings.value = data.settings + } catch (e) { + console.error("Error loading course settings:", e) + } finally { + isLoading.value = false + } + } + + const getSetting = computed(() => { + return (variable) => settings.value[variable] || null + }); + + return { + isLoading, + settings, + loadCourseSettings, + getSetting + } +}) diff --git a/src/CoreBundle/Controller/PlatformConfigurationController.php b/src/CoreBundle/Controller/PlatformConfigurationController.php index f506711534..1048346209 100644 --- a/src/CoreBundle/Controller/PlatformConfigurationController.php +++ b/src/CoreBundle/Controller/PlatformConfigurationController.php @@ -7,12 +7,15 @@ declare(strict_types=1); namespace Chamilo\CoreBundle\Controller; use bbb; +use Chamilo\CoreBundle\Repository\Node\CourseRepository; use Chamilo\CoreBundle\ServiceHelper\TicketProjectHelper; use Chamilo\CoreBundle\ServiceHelper\UserHelper; use Chamilo\CoreBundle\Settings\SettingsManager; use Chamilo\CoreBundle\Traits\ControllerTrait; +use Chamilo\CourseBundle\Settings\SettingsCourseManager; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Component\HttpFoundation\JsonResponse; +use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\Routing\Attribute\Route; @@ -111,4 +114,28 @@ class PlatformConfigurationController extends AbstractController return new JsonResponse($configuration); } + + #[Route('/list/course_settings', name: 'course_settings_list', methods: ['GET'])] + public function courseSettingsList( + SettingsCourseManager $courseSettingsManager, + CourseRepository $courseRepository, + Request $request + ): JsonResponse { + $courseId = $request->query->get('cid'); + if (!$courseId) { + return new JsonResponse(['error' => 'Course ID is required'], Response::HTTP_BAD_REQUEST); + } + + $course = $courseRepository->find($courseId); + if (!$course) { + return new JsonResponse(['error' => 'Course not found'], Response::HTTP_NOT_FOUND); + } + + $courseSettingsManager->setCourse($course); + $settings = [ + 'show_course_in_user_language' => $courseSettingsManager->getCourseSettingValue('show_course_in_user_language'), + ]; + + return new JsonResponse(['settings' => $settings]); + } } diff --git a/src/CourseBundle/Settings/SettingsCourseManager.php b/src/CourseBundle/Settings/SettingsCourseManager.php index 5b663b1403..0ce642a9a7 100644 --- a/src/CourseBundle/Settings/SettingsCourseManager.php +++ b/src/CourseBundle/Settings/SettingsCourseManager.php @@ -151,4 +151,21 @@ class SettingsCourseManager extends SettingsManager return $list; } + + /** + * Fetches the value of a specific setting for the given course and variable. + * + * @param string $variableName The name of the variable to fetch. + * @return string|null The value of the setting if found, or null if not. + */ + public function getCourseSettingValue(string $variableName): ?string + { + $repo = $this->manager->getRepository(CCourseSetting::class); + $courseSetting = $repo->findOneBy([ + 'cId' => $this->course->getId(), + 'variable' => $variableName + ]); + + return $courseSetting ? $courseSetting->getValue() : null; + } }