Skip to content

Commit

Permalink
Internal: Make a request to reset the current course
Browse files Browse the repository at this point in the history
  • Loading branch information
AngelFQC committed May 6, 2024
1 parent 9ad8e94 commit ff11ac6
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 14 deletions.
6 changes: 1 addition & 5 deletions assets/vue/router/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -198,11 +198,7 @@ router.beforeResolve(async (to) => {
cid = parseInt(to.params?.id ?? 0)
}

if (cid) {
await cidReqStore.setCourseAndSessionById(cid, sid)
} else {
cidReqStore.resetCid()
}
await cidReqStore.setCourseAndSessionById(cid, sid)
})

export default router
9 changes: 9 additions & 0 deletions assets/vue/services/courseService.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,18 @@
import api from "../config/api"
import baseService from "./baseService"

/**
* @returns {Promise<void>}
*/
async function cidReset() {
await api.get("/course/cidReset")
}

export default {
find: baseService.get,

cidReset,

/**
* @param {number} courseId
* @param {number=} sessionId
Expand Down
13 changes: 11 additions & 2 deletions assets/vue/store/cidReq.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,11 @@ export const useCidReqStore = defineStore("cidReq", () => {
}
})

const resetCid = () => {
const resetCid = async () => {
if (course.value) {
await courseService.cidReset()
}

course.value = null
session.value = null
group.value = null
Expand Down Expand Up @@ -92,9 +96,14 @@ export const useCidReqStore = defineStore("cidReq", () => {
}
}

/**
* @param {number|null} cId
* @param {number|null} sId
* @returns {Promise<Awaited<void>[]>|Promise<void>}
*/
const setCourseAndSessionById = (cId, sId = undefined) => {
if (!cId) {
return Promise.resolve()
return resetCid()
}

const coursePromise = setCourseByIri(cId, sId)
Expand Down
15 changes: 15 additions & 0 deletions src/CoreBundle/Controller/CourseController.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Chamilo\CoreBundle\Entity\Tag;
use Chamilo\CoreBundle\Entity\Tool;
use Chamilo\CoreBundle\Entity\User;
use Chamilo\CoreBundle\EventListener\CidReqListener;
use Chamilo\CoreBundle\Framework\Container;
use Chamilo\CoreBundle\Repository\CourseCategoryRepository;
use Chamilo\CoreBundle\Repository\ExtraFieldValuesRepository;
Expand Down Expand Up @@ -52,6 +53,7 @@
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\Routing\Attribute\Route;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
use Symfony\Component\Serializer\SerializerInterface;
use Symfony\Component\Validator\Exception\ValidatorException;
use Symfony\Contracts\Translation\TranslatorInterface;
Expand All @@ -69,6 +71,19 @@ public function __construct(
private readonly UserHelper $userHelper,
) {}

#[Route('/cidReset', methods: ['GET'])]
public function cidReset(
Request $request,
TokenStorageInterface $tokenStorage,
): Response {
CidReqListener::cleanSessionHandler(
$request,
$tokenStorage->getToken()
);

return new Response('', Response::HTTP_NO_CONTENT);
}

#[Route('/{cid}/checkLegal.json', name: 'chamilo_core_course_check_legal_json')]
public function checkTermsAndConditionJson(
Request $request,
Expand Down
13 changes: 6 additions & 7 deletions src/CoreBundle/EventListener/CidReqListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
use Symfony\Component\HttpKernel\Event\RequestEvent;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
use Symfony\Component\Security\Core\User\UserInterface;
Expand Down Expand Up @@ -67,7 +68,10 @@ public function onKernelRequest(RequestEvent $event): void
}

if (true === $cidReset) {
$this->cleanSessionHandler($request);
$this->cleanSessionHandler(
$request,
$this->tokenStorage->getToken()
);

return;
}
Expand Down Expand Up @@ -192,8 +196,6 @@ public function onKernelRequest(RequestEvent $event): void
$courseParams = $this->generateCourseUrl($course, $sessionId, $groupId, $origin);
$sessionHandler->set('course_url_params', $courseParams);
$twig->addGlobal('course_url_params', $courseParams);
} else {
$this->cleanSessionHandler($request);
}
}

Expand Down Expand Up @@ -258,7 +260,7 @@ public function onKernelController(ControllerEvent $event): void
}
}

public function cleanSessionHandler(Request $request): void
public static function cleanSessionHandler(Request $request, ?TokenInterface $token): void
{
$sessionHandler = $request->getSession();
$alreadyVisited = $sessionHandler->get('course_already_visited');
Expand Down Expand Up @@ -296,7 +298,6 @@ public function cleanSessionHandler(Request $request): void
ChamiloSession::erase('origin');

// Remove user temp roles
$token = $this->tokenStorage->getToken();
if (null !== $token) {
/** @var User $user */
$user = $token->getUser();
Expand All @@ -309,8 +310,6 @@ public function cleanSessionHandler(Request $request): void
$user->removeRole('ROLE_CURRENT_COURSE_SESSION_TEACHER');
}
}

// $request->setLocale($request->getPreferredLanguage());
}

private function generateCourseUrl(?Course $course, int $sessionId, int $groupId, ?string $origin): string
Expand Down

0 comments on commit ff11ac6

Please sign in to comment.