Platform: Improve sidebar menu of course item - refs BT#21133

pull/5002/head
christian 2 years ago
parent 81109a634c
commit f3e521f813
  1. 236
      assets/vue/composables/sidebarMenu.js
  2. 33
      assets/vue/store/enrolledStore.js
  3. 41
      src/CoreBundle/Controller/CourseController.php

@ -1,150 +1,178 @@
import { useI18n } from "vue-i18n" import { useI18n } from "vue-i18n"
import { ref, onMounted, watch } from "vue"
import { useSecurityStore } from "../store/securityStore" import { useSecurityStore } from "../store/securityStore"
import { usePlatformConfig } from "../store/platformConfig" import { usePlatformConfig } from "../store/platformConfig"
import { useEnrolledStore } from "../store/enrolledStore"
export function useSidebarMenu() { export function useSidebarMenu() {
const { t } = useI18n() const { t } = useI18n()
const securityStore = useSecurityStore() const securityStore = useSecurityStore()
const platformConfigStore = usePlatformConfig() const platformConfigStore = usePlatformConfig()
const enrolledStore = useEnrolledStore()
const showTabsSetting = platformConfigStore.getSetting("platform.show_tabs") const showTabsSetting = platformConfigStore.getSetting("platform.show_tabs")
const items = [] const items = ref([])
const coursesItems = ref([])
if (showTabsSetting.indexOf("campus_homepage") > -1) { const updateItems = () => {
items.push({ items.value = []
icon: "mdi mdi-home",
label: t("Home"),
to: { name: "Home" },
})
}
if (securityStore.isAuthenticated) {
if (showTabsSetting.indexOf("campus_homepage") > -1) { if (showTabsSetting.indexOf("campus_homepage") > -1) {
items.push({ items.value.push({
icon: "mdi mdi-book-open-page-variant", icon: "mdi mdi-home",
label: t("Home"),
to: { name: "Home" },
})
}
if (securityStore.isAuthenticated) {
if (coursesItems.value.length > 0) {
const coursesMenu = {
icon: "mdi mdi-book-open-page-variant",
label: coursesItems.value.length > 1 ? t("Courses") : coursesItems.value[0].label,
items: coursesItems.value.length > 1 ? coursesItems.value : undefined,
to: coursesItems.value.length === 1 ? coursesItems.value[0].to : undefined,
}
items.value.push(coursesMenu)
}
if (showTabsSetting.indexOf("my_agenda") > -1) {
items.value.push({
icon: "mdi mdi-calendar-text",
label: t("Events"),
to: { name: "CCalendarEventList" },
})
}
if (showTabsSetting.indexOf("reporting") > -1) {
let subItems = []
if (securityStore.isTeacher || securityStore.isHRM || securityStore.isSessionAdmin) {
subItems.push({
label: securityStore.isHRM ? t("Course sessions") : t("Reporting"),
url: "/main/my_space/" + (securityStore.isHRM ? "session.php" : "index.php"),
})
} else if (securityStore.isStudentBoss) {
subItems.push({
label: t("Learners"),
url: "/main/my_space/student.php",
})
} else {
subItems.push({
label: t("Progress"),
url: "/main/auth/my_progress.php",
})
}
items.value.push({
icon: "mdi mdi-chart-box",
label: t("Reporting"),
items: subItems,
})
}
if (showTabsSetting.indexOf("social") > -1) {
items.value.push({
icon: "mdi mdi-sitemap-outline",
label: t("Social network"),
to: { name: "SocialWall" },
})
}
if (platformConfigStore.plugins?.bbb?.show_global_conference_link) {
items.value.push({
icon: "mdi mdi-video",
label: t("Videoconference"),
url: platformConfigStore.plugins.bbb.listingURL,
})
}
}
if (securityStore.isStudentBoss || securityStore.isStudent) {
items.value.push({
icon: "mdi mdi-text-box-search",
items: [ items: [
{ {
label: t("My courses"), label: t("Diagnosis Management"),
to: { name: "MyCourses" }, url: "/main/search/load_search.php",
visible: securityStore.isStudentBoss,
}, },
{ {
label: t("My sessions"), label: t("Diagnostic Form"),
to: { name: "MySessions" }, url: "/main/search/search.php",
}, },
], ],
label: t("Courses"), label: t("Diagnosis"),
})
}
if (showTabsSetting.indexOf("my_agenda") > -1) {
items.push({
icon: "mdi mdi-calendar-text",
label: t("Events"),
to: { name: "CCalendarEventList" },
}) })
} }
if (showTabsSetting.indexOf("reporting") > -1) { if (securityStore.isAdmin || securityStore.isSessionAdmin) {
let subItems = [] const adminItems = [
{
label: t("Administration"),
to: { name: "AdminIndex" },
},
]
if (securityStore.isTeacher || securityStore.isHRM || securityStore.isSessionAdmin) { if (securityStore.isSessionAdmin && 'true' === platformConfigStore.getSetting('session.limit_session_admin_list_users')) {
subItems.push({ adminItems.push({
label: securityStore.isHRM ? t("Course sessions") : t("Reporting"), label: t("Add user"),
url: "/main/my_space/" + (securityStore.isHRM ? "session.php" : "index.php"), url: "/main/admin/user_add.php",
})
} else if (securityStore.isStudentBoss) {
subItems.push({
label: t("Learners"),
url: "/main/my_space/student.php",
}) })
} else { } else {
subItems.push({ adminItems.push({
label: t("Progress"), label: t("Users"),
url: "/main/auth/my_progress.php", url: "/main/admin/user_list.php",
}) })
} }
items.push({ if (securityStore.isAdmin) {
icon: "mdi mdi-chart-box", adminItems.push({
label: t("Reporting"), label: t("Courses"),
items: subItems, url: "/main/admin/course_list.php",
}) })
} }
if (showTabsSetting.indexOf("social") > -1) { adminItems.push({
items.push({ label: t("Sessions"),
icon: "mdi mdi-sitemap-outline", url: "/main/session/session_list.php",
label: t("Social network"),
to: { name: "SocialWall" },
}) })
}
if (platformConfigStore.plugins?.bbb?.show_global_conference_link) { items.value.push({
items.push({ icon: "mdi mdi-cog",
icon: "mdi mdi-video", items: adminItems,
label: t("Videoconference"), label: t("Administration"),
url: platformConfigStore.plugins.bbb.listingURL,
}) })
} }
} }
if (securityStore.isStudentBoss || securityStore.isStudent) { const updateCoursesItems = () => {
items.push({ coursesItems.value = [];
icon: "mdi mdi-text-box-search",
items: [
{
label: t("Diagnosis Management"),
url: "/main/search/load_search.php",
visible: securityStore.isStudentBoss,
},
{
label: t("Diagnostic Form"),
url: "/main/search/search.php",
},
],
label: t("Diagnosis"),
})
}
if (securityStore.isAdmin || securityStore.isSessionAdmin) {
const adminItems = [
{
label: t("Administration"),
to: { name: "AdminIndex" },
},
]
if (securityStore.isSessionAdmin && 'true' === platformConfigStore.getSetting('session.limit_session_admin_list_users')) { if (enrolledStore.isEnrolledInCourses) {
adminItems.push({ coursesItems.value.push({
label: t("Add user"), label: t("My courses"),
url: "/main/admin/user_add.php", to: { name: "MyCourses" },
})
} else {
adminItems.push({
label: t("Users"),
url: "/main/admin/user_list.php",
}) })
} }
if (securityStore.isAdmin) { if (enrolledStore.isEnrolledInSessions) {
adminItems.push({ coursesItems.value.push({
label: t("Courses"), label: t("My sessions"),
url: "/main/admin/course_list.php", to: { name: "MySessions" },
}) })
} }
adminItems.push({ updateItems();
label: t("Sessions"),
url: "/main/session/session_list.php",
})
items.push({
icon: "mdi mdi-cog",
items: adminItems,
label: t("Administration"),
})
} }
onMounted(async () => {
await enrolledStore.initialize();
updateCoursesItems();
})
watch(() => enrolledStore.isEnrolledInCourses, updateCoursesItems)
watch(() => enrolledStore.isEnrolledInSessions, updateCoursesItems)
return items return items
} }

@ -0,0 +1,33 @@
import { defineStore } from "pinia"
import axios from "axios"
import { computed, ref } from "vue"
export const useEnrolledStore = defineStore("enrolledStore", () => {
// Reactive state to track if the user is enrolled in courses or sessions
const isEnrolledInCourses = ref(false)
const isEnrolledInSessions = ref(false)
// Function to check enrollment status
async function checkEnrollments() {
try {
const { data } = await axios.get("/course/check-enrollments")
console.log('Check enrollments data:', data)
isEnrolledInCourses.value = data.isEnrolledInCourses
isEnrolledInSessions.value = data.isEnrolledInSessions
} catch (error) {
console.error("Error verifying enrollments:", error)
}
}
// Function to initialize the store
async function initialize() {
await checkEnrollments()
}
return {
// Computed properties for reactivity
isEnrolledInCourses: computed(() => isEnrolledInCourses.value),
isEnrolledInSessions: computed(() => isEnrolledInSessions.value),
initialize
};
});

@ -7,8 +7,10 @@ declare(strict_types=1);
namespace Chamilo\CoreBundle\Controller; namespace Chamilo\CoreBundle\Controller;
use Chamilo\CoreBundle\Entity\Course; use Chamilo\CoreBundle\Entity\Course;
use Chamilo\CoreBundle\Entity\CourseRelUser;
use Chamilo\CoreBundle\Entity\ExtraField; use Chamilo\CoreBundle\Entity\ExtraField;
use Chamilo\CoreBundle\Entity\Session; use Chamilo\CoreBundle\Entity\Session;
use Chamilo\CoreBundle\Entity\SessionRelUser;
use Chamilo\CoreBundle\Entity\Tag; use Chamilo\CoreBundle\Entity\Tag;
use Chamilo\CoreBundle\Entity\Tool; use Chamilo\CoreBundle\Entity\Tool;
use Chamilo\CoreBundle\Entity\User; use Chamilo\CoreBundle\Entity\User;
@ -897,4 +899,43 @@ class CourseController extends ToolBaseController
return $link.'?'.$this->getCourseUrlQuery(); return $link.'?'.$this->getCourseUrlQuery();
} }
#[Route('/check-enrollments', name: 'chamilo_core_check_enrollments', methods: ['GET'])]
public function checkEnrollments(EntityManagerInterface $em): JsonResponse
{
/** @var User|null $user */
$user = $this->getUser();
if (!$user) {
return new JsonResponse(['error' => 'User not found'], Response::HTTP_UNAUTHORIZED);
}
$isEnrolledInCourses = $this->isUserEnrolledInAnyCourse($user, $em);
$isEnrolledInSessions = $this->isUserEnrolledInAnySession($user, $em);
return new JsonResponse([
'isEnrolledInCourses' => $isEnrolledInCourses,
'isEnrolledInSessions' => $isEnrolledInSessions,
]);
}
// Implement the real logic to check course enrollment
private function isUserEnrolledInAnyCourse(User $user, EntityManagerInterface $em): bool
{
$enrollment = $em
->getRepository(CourseRelUser::class)
->findOneBy(['user' => $user]);
return null !== $enrollment;
}
// Implement the real logic to check session enrollment
private function isUserEnrolledInAnySession(User $user, EntityManagerInterface $em): bool
{
$enrollment = $em->getRepository(SessionRelUser::class)
->findOneBy(['user' => $user]);
return null !== $enrollment;
}
} }

Loading…
Cancel
Save