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 { ref, onMounted, watch } from "vue"
import { useSecurityStore } from "../store/securityStore"
import { usePlatformConfig } from "../store/platformConfig"
import { useEnrolledStore } from "../store/enrolledStore"
export function useSidebarMenu() {
const { t } = useI18n()
const securityStore = useSecurityStore()
const platformConfigStore = usePlatformConfig()
const enrolledStore = useEnrolledStore()
const showTabsSetting = platformConfigStore.getSetting("platform.show_tabs")
const items = []
const items = ref([])
const coursesItems = ref([])
if (showTabsSetting.indexOf("campus_homepage") > -1) {
items.push({
icon: "mdi mdi-home",
label: t("Home"),
to: { name: "Home" },
})
}
const updateItems = () => {
items.value = []
if (securityStore.isAuthenticated) {
if (showTabsSetting.indexOf("campus_homepage") > -1) {
items.push({
icon: "mdi mdi-book-open-page-variant",
items.value.push({
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: [
{
label: t("My courses"),
to: { name: "MyCourses" },
label: t("Diagnosis Management"),
url: "/main/search/load_search.php",
visible: securityStore.isStudentBoss,
},
{
label: t("My sessions"),
to: { name: "MySessions" },
label: t("Diagnostic Form"),
url: "/main/search/search.php",
},
],
label: t("Courses"),
})
}
if (showTabsSetting.indexOf("my_agenda") > -1) {
items.push({
icon: "mdi mdi-calendar-text",
label: t("Events"),
to: { name: "CCalendarEventList" },
label: t("Diagnosis"),
})
}
if (showTabsSetting.indexOf("reporting") > -1) {
let subItems = []
if (securityStore.isAdmin || securityStore.isSessionAdmin) {
const adminItems = [
{
label: t("Administration"),
to: { name: "AdminIndex" },
},
]
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",
if (securityStore.isSessionAdmin && 'true' === platformConfigStore.getSetting('session.limit_session_admin_list_users')) {
adminItems.push({
label: t("Add user"),
url: "/main/admin/user_add.php",
})
} else {
subItems.push({
label: t("Progress"),
url: "/main/auth/my_progress.php",
adminItems.push({
label: t("Users"),
url: "/main/admin/user_list.php",
})
}
items.push({
icon: "mdi mdi-chart-box",
label: t("Reporting"),
items: subItems,
})
}
if (securityStore.isAdmin) {
adminItems.push({
label: t("Courses"),
url: "/main/admin/course_list.php",
})
}
if (showTabsSetting.indexOf("social") > -1) {
items.push({
icon: "mdi mdi-sitemap-outline",
label: t("Social network"),
to: { name: "SocialWall" },
adminItems.push({
label: t("Sessions"),
url: "/main/session/session_list.php",
})
}
if (platformConfigStore.plugins?.bbb?.show_global_conference_link) {
items.push({
icon: "mdi mdi-video",
label: t("Videoconference"),
url: platformConfigStore.plugins.bbb.listingURL,
items.value.push({
icon: "mdi mdi-cog",
items: adminItems,
label: t("Administration"),
})
}
}
if (securityStore.isStudentBoss || securityStore.isStudent) {
items.push({
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" },
},
]
const updateCoursesItems = () => {
coursesItems.value = [];
if (securityStore.isSessionAdmin && 'true' === platformConfigStore.getSetting('session.limit_session_admin_list_users')) {
adminItems.push({
label: t("Add user"),
url: "/main/admin/user_add.php",
})
} else {
adminItems.push({
label: t("Users"),
url: "/main/admin/user_list.php",
if (enrolledStore.isEnrolledInCourses) {
coursesItems.value.push({
label: t("My courses"),
to: { name: "MyCourses" },
})
}
if (securityStore.isAdmin) {
adminItems.push({
label: t("Courses"),
url: "/main/admin/course_list.php",
if (enrolledStore.isEnrolledInSessions) {
coursesItems.value.push({
label: t("My sessions"),
to: { name: "MySessions" },
})
}
adminItems.push({
label: t("Sessions"),
url: "/main/session/session_list.php",
})
items.push({
icon: "mdi mdi-cog",
items: adminItems,
label: t("Administration"),
})
updateItems();
}
onMounted(async () => {
await enrolledStore.initialize();
updateCoursesItems();
})
watch(() => enrolledStore.isEnrolledInCourses, updateCoursesItems)
watch(() => enrolledStore.isEnrolledInSessions, updateCoursesItems)
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;
use Chamilo\CoreBundle\Entity\Course;
use Chamilo\CoreBundle\Entity\CourseRelUser;
use Chamilo\CoreBundle\Entity\ExtraField;
use Chamilo\CoreBundle\Entity\Session;
use Chamilo\CoreBundle\Entity\SessionRelUser;
use Chamilo\CoreBundle\Entity\Tag;
use Chamilo\CoreBundle\Entity\Tool;
use Chamilo\CoreBundle\Entity\User;
@ -897,4 +899,43 @@ class CourseController extends ToolBaseController
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