Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Internal: Make a request to reset the current course #5485

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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/cid_reset")
}

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('/cid_reset', methods: ['GET'])]
ywarnier marked this conversation as resolved.
Show resolved Hide resolved
public function cidReset(
ywarnier marked this conversation as resolved.
Show resolved Hide resolved
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);
self::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
ywarnier marked this conversation as resolved.
Show resolved Hide resolved
{
$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
Loading