Skip to content

Commit 055bbd4

Browse files
Merge pull request #7446 from christianbeeznest/GH-7443
Course: Fix session-specific course introduction display - refs #7443
2 parents acec448 + 6f1c393 commit 055bbd4

1 file changed

Lines changed: 109 additions & 27 deletions

File tree

src/CoreBundle/Controller/CourseController.php

Lines changed: 109 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -839,50 +839,90 @@ private function findIntroOfCourse(Course $course): ?CTool
839839
#[Route('/{id}/getToolIntro', name: 'chamilo_core_course_gettoolintro')]
840840
public function getToolIntro(Request $request, Course $course, EntityManagerInterface $em): Response
841841
{
842-
$sessionId = (int) $request->get('sid');
842+
$sessionId = (int) $request->query->get('sid', 0);
843+
844+
$session = null;
845+
if ($sessionId > 0) {
846+
$session = $em->getRepository(Session::class)->find($sessionId);
847+
}
843848

844-
$responseData = [];
845849
$ctoolRepo = $em->getRepository(CTool::class);
846-
$sessionRepo = $em->getRepository(Session::class);
847-
$createInSession = false;
850+
$ctoolintroRepo = $em->getRepository(CToolIntro::class);
848851

849-
$session = null;
852+
// Base tool + base intro (course context, no session).
853+
$baseTool = $this->findIntroOfCourse($course);
854+
if (!$baseTool) {
855+
// ensure the base tool exists (should rarely happen).
856+
$baseTool = $this->ensureCourseHomepageTool($course, null, $em);
857+
}
850858

851-
if (!empty($sessionId)) {
852-
$session = $sessionRepo->find($sessionId);
859+
$baseIntro = null;
860+
if ($baseTool) {
861+
$baseIntro = $ctoolintroRepo->findOneBy(
862+
['courseTool' => $baseTool],
863+
['iid' => 'DESC']
864+
);
853865
}
854866

855-
$ctool = $this->findIntroOfCourse($course);
867+
$activeTool = $baseTool;
868+
$activeIntro = $baseIntro;
869+
$createInSession = false;
856870

857871
if ($session) {
858-
$ctoolSession = $ctoolRepo->findOneBy(['title' => 'course_homepage', 'course' => $course, 'session' => $session]);
872+
// Ensure the session tool exists so the frontend can create the intro in the right context.
873+
$sessionTool = $ctoolRepo->findOneBy([
874+
'title' => 'course_homepage',
875+
'course' => $course,
876+
'session' => $session,
877+
]);
878+
879+
if (!$sessionTool) {
880+
$sessionTool = $this->ensureCourseHomepageTool($course, $session, $em);
881+
}
859882

860-
if (!$ctoolSession) {
861-
$createInSession = true;
883+
// Use session tool for editing/creation in session context.
884+
if ($sessionTool) {
885+
$activeTool = $sessionTool;
886+
887+
$sessionIntro = $ctoolintroRepo->findOneBy(
888+
['courseTool' => $sessionTool],
889+
['iid' => 'DESC']
890+
);
891+
892+
if ($sessionIntro) {
893+
// Session-specific intro exists: show it.
894+
$activeIntro = $sessionIntro;
895+
$createInSession = false;
896+
} else {
897+
// No session-specific intro yet: show base intro (if any), but allow creating in session.
898+
$activeIntro = $baseIntro;
899+
$createInSession = true;
900+
}
862901
} else {
863-
$ctool = $ctoolSession;
902+
// If session tool cannot be created, fallback to base (display only).
903+
$activeTool = $baseTool;
904+
$activeIntro = $baseIntro;
905+
$createInSession = false;
864906
}
865907
}
866908

867-
if ($ctool) {
868-
$ctoolintroRepo = $em->getRepository(CToolIntro::class);
869-
870-
/** @var CToolIntro $ctoolintro */
871-
$ctoolintro = $ctoolintroRepo->findOneBy(['courseTool' => $ctool]);
872-
if ($ctoolintro) {
873-
$responseData = [
874-
'iid' => $ctoolintro->getIid(),
875-
'introText' => $ctoolintro->getIntroText(),
876-
'createInSession' => $createInSession,
877-
'cToolId' => $ctool->getIid(),
878-
];
879-
}
909+
$responseData = [
910+
'createInSession' => $createInSession,
911+
];
912+
913+
if ($activeTool) {
914+
$responseData['cToolId'] = $activeTool->getIid();
880915
$responseData['c_tool'] = [
881-
'iid' => $ctool->getIid(),
882-
'title' => $ctool->getTitle(),
916+
'iid' => $activeTool->getIid(),
917+
'title' => $activeTool->getTitle(),
883918
];
884919
}
885920

921+
if ($activeIntro) {
922+
$responseData['iid'] = $activeIntro->getIid();
923+
$responseData['introText'] = $activeIntro->getIntroText();
924+
}
925+
886926
return new JsonResponse($responseData);
887927
}
888928

@@ -936,6 +976,7 @@ public function addToolIntro(Request $request, Course $course, EntityManagerInte
936976
return new JsonResponse([
937977
'status' => 'created',
938978
'cToolId' => $ctoolSession->getIid(),
979+
'iid' => $ctoolIntro->getIid(),
939980
'introIid' => $ctoolIntro->getIid(),
940981
'introText' => $ctoolIntro->getIntroText(),
941982
]);
@@ -949,6 +990,7 @@ public function addToolIntro(Request $request, Course $course, EntityManagerInte
949990
return new JsonResponse([
950991
'status' => 'updated',
951992
'cToolId' => $ctoolSession->getIid(),
993+
'iid' => $ctoolIntro->getIid(),
952994
'introIid' => $ctoolIntro->getIid(),
953995
'introText' => $ctoolIntro->getIntroText(),
954996
]);
@@ -1303,6 +1345,46 @@ private function autoLaunch(): void
13031345
}
13041346
}
13051347

1348+
/**
1349+
* Ensure a "course_homepage" CTool exists for the given course + session context.
1350+
* - If $session is null, it creates/returns the base tool.
1351+
* - If $session is not null, it creates/returns the session tool.
1352+
*/
1353+
private function ensureCourseHomepageTool(Course $course, ?Session $session, EntityManagerInterface $em): ?CTool
1354+
{
1355+
$ctoolRepo = $em->getRepository(CTool::class);
1356+
1357+
$existing = $ctoolRepo->findOneBy([
1358+
'title' => 'course_homepage',
1359+
'course' => $course,
1360+
'session' => $session,
1361+
]);
1362+
1363+
if ($existing) {
1364+
return $existing;
1365+
}
1366+
1367+
$toolEntity = $em->getRepository(Tool::class)->findOneBy(['title' => 'course_homepage']);
1368+
if (!$toolEntity) {
1369+
return null;
1370+
}
1371+
1372+
$ctool = (new CTool())
1373+
->setTool($toolEntity)
1374+
->setTitle('course_homepage')
1375+
->setCourse($course)
1376+
->setPosition(1)
1377+
->setParent($course)
1378+
->setCreator($course->getCreator())
1379+
->setSession($session)
1380+
->addCourseLink($course);
1381+
1382+
$em->persist($ctool);
1383+
$em->flush();
1384+
1385+
return $ctool;
1386+
}
1387+
13061388
// Implement the real logic to check course enrollment
13071389
private function isUserEnrolledInAnyCourse(User $user, EntityManagerInterface $em): bool
13081390
{

0 commit comments

Comments
 (0)