Move code to function

pull/3951/head
Angel Fernando Quiroz Campos 4 years ago
parent d62bb55eed
commit a0f952ccb3
  1. 62
      main/auth/courses.php
  2. 37
      main/course_home/course_home.php
  3. 108
      main/inc/lib/course.lib.php

@ -90,6 +90,23 @@ if (!empty($settings)) {
}
}
function generateRedirectUrlAfterSubscription($redirectAfterSubscription, $coursePublicUrl): string
{
if ('course_home' !== $redirectAfterSubscription) {
return api_get_self();
}
if (api_get_configuration_value('catalog_course_subscription_in_user_s_session')) {
$user = api_get_user_entity(api_get_user_id());
if ($user && $accesibleSessions = $user->getCurrentlyAccessibleSessions()) {
return $coursePublicUrl.'?id_session='.$accesibleSessions[0]->getId();
}
}
return $coursePublicUrl;
}
switch ($action) {
case 'unsubscribe':
// We are unsubscribing from a course (=Unsubscribe from course).
@ -114,22 +131,12 @@ switch ($action) {
if (Security::check_token('get')) {
$courseInfo = api_get_course_info($courseCodeToSubscribe);
CourseManager::autoSubscribeToCourse($courseCodeToSubscribe);
if ('course_home' === $redirectAfterSubscription) {
$redirectionTarget = $courseInfo['course_public_url'];
if (api_get_configuration_value('catalog_course_subscription_in_user_s_session')) {
$user = api_get_user_entity(api_get_user_id());
if ($user) {
foreach ($user->getCurrentlyAccessibleSessions() as $session) {
$redirectionTarget = $redirectionTarget.'?id_session='.$session->getId();
break;
}
}
}
header('Location: '.$redirectionTarget);
$redirectionTarget = generateRedirectUrlAfterSubscription(
$redirectAfterSubscription,
$courseInfo['course_public_url']
);
exit;
}
header('Location: '.api_get_self());
header("Location: $redirectionTarget");
exit;
}
break;
@ -162,28 +169,17 @@ switch ($action) {
if (sha1($_POST['course_registration_code']) === $courseInfo['registration_code']) {
CourseManager::autoSubscribeToCourse($_POST['subscribe_user_with_password']);
if ('course_home' === $redirectAfterSubscription) {
$redirectionTarget = $courseInfo['course_public_url'];
if (api_get_configuration_value('catalog_course_subscription_in_user_s_session')) {
$user = api_get_user_entity(api_get_user_id());
if ($user) {
foreach ($user->getCurrentlyAccessibleSessions() as $session) {
$redirectionTarget = $redirectionTarget.'?id_session='.$session->getId();
break;
}
}
}
header('Location: '.$redirectionTarget);
exit;
}
$redirectionTarget = generateRedirectUrlAfterSubscription(
$redirectAfterSubscription,
$courseInfo['course_public_url']
);
header('Location: '.api_get_self());
exit;
header("Location: $redirectionTarget");
} else {
Display::addFlash(Display::return_message(get_lang('CourseRegistrationCodeIncorrect')), 'warning');
Display::addFlash(Display::return_message(get_lang('CourseRegistrationCodeIncorrect'), 'warning'));
header('Location: '.$action);
exit;
}
exit;
}
break;

@ -229,40 +229,35 @@ if ($isSpecialCourse) {
$action = !empty($_GET['action']) ? Security::remove_XSS($_GET['action']) : '';
if ($action === 'subscribe') {
if (Security::check_token('get')) {
Security::clear_token();
if ($action === 'subscribe' && Security::check_token('get')) {
Security::clear_token();
$generateRedirectUrlAfterSubscription = function () use ($course_code, $courseId, $user_id) {
$redirectionTarget = api_get_self();
if (CourseManager::autoSubscribeToCourse($course_code)) {
if (CourseManager::is_user_subscribed_in_course($user_id, $course_code)) {
Session::write('is_allowed_in_course', true);
}
if (api_get_configuration_value('catalog_course_subscription_in_user_s_session')) {
// append session id to redirect URL
/**
* @var Chamilo\UserBundle\Entity\User
*/
$user = UserManager::getRepository()->find(api_get_user_id());
if ($user) {
foreach ($user->getCurrentlyAccessibleSessions() as $session) {
$redirectionTarget = api_get_self().'?id_session='.$session->getId();
break;
}
$user = api_get_user_entity(api_get_user_id());
if ($user && $accesibleSessions = $user->getCurrentlyAccessibleSessions()) {
return api_get_self().'?id_session='.$accesibleSessions[0]->getId();
}
}
} elseif (api_get_configuration_value('catalog_course_subscription_in_user_s_session')) {
/**
* @var Chamilo\UserBundle\Entity\User
*/
$user = UserManager::getRepository()->find(api_get_user_id());
$user = api_get_user_entity(api_get_user_id());
if ($user && !$user->getCurrentlyAccessibleSessions()) {
// subscription was probably refused because user session expired, go back to page "about"
$redirectionTarget = api_get_path(WEB_PATH).'course/'.$courseId.'/about';
return api_get_path(WEB_PATH).'course/'.$courseId.'/about';
}
}
header('Location: '.$redirectionTarget);
exit;
}
return $redirectionTarget;
};
header('Location: '.$generateRedirectUrlAfterSubscription());
exit;
}
/* Is the user allowed here? */

@ -618,86 +618,50 @@ class CourseManager
}
/**
* @param string $courseCode
* @param int $status
*
* @return bool
* @throws Exception
*/
public static function autoSubscribeToCourse($courseCode, $status = STUDENT)
public static function processAutoSubscribeToCourse(string $courseCode, int $status = STUDENT)
{
if (api_is_anonymous()) {
return false;
throw new Exception(get_lang('NotAllowed'));
}
$course = Database::getManager()->getRepository('ChamiloCoreBundle:Course')->findOneBy(['code' => $courseCode]);
if (null === $course) {
return false;
throw new Exception(get_lang('NotAllowed'));
}
$visibility = (int) $course->getVisibility();
if (in_array($visibility, [
COURSE_VISIBILITY_CLOSED,
//COURSE_VISIBILITY_REGISTERED,
COURSE_VISIBILITY_HIDDEN,
])) {
Display::addFlash(
Display::return_message(
get_lang('SubscribingNotAllowed'),
'warning'
)
);
return false;
if (in_array($visibility, [COURSE_VISIBILITY_CLOSED, COURSE_VISIBILITY_HIDDEN])) {
throw new Exception(get_lang('SubscribingNotAllowed'));
}
// Private course can allow auto subscription
if (COURSE_VISIBILITY_REGISTERED === $visibility && false === $course->getSubscribe()) {
Display::addFlash(
Display::return_message(
get_lang('SubscribingNotAllowed'),
'warning'
)
);
return false;
throw new Exception(get_lang('SubscribingNotAllowed'));
}
$userId = api_get_user_id();
if (api_get_configuration_value('catalog_course_subscription_in_user_s_session')) {
/**
* @var Chamilo\UserBundle\Entity\User
*/
$user = UserManager::getRepository()->find($userId);
$user = api_get_user_entity($userId);
$sessions = $user->getCurrentlyAccessibleSessions();
if (empty($sessions)) {
// user has no accessible session
if ($user->getStudentSessions()) {
// user has ancient or future student session(s) but not available now
Display::addFlash(
Display::return_message(
get_lang('CanNotSubscribeToCourseUserSessionExpired'),
'warning'
)
);
return false;
throw new Exception(get_lang('CanNotSubscribeToCourseUserSessionExpired'));
}
// user has no session at all, create one starting now
$numberOfDays = api_get_configuration_value('user_s_session_duration') ?: 3 * 365;
try {
$duration = new DateInterval(sprintf('P%dD', $numberOfDays));
} catch (Exception $exception) {
Display::addFlash(
Display::return_message(
get_lang('WrongNumberOfDays').': '.$numberOfDays.': '.$exception->getMessage(),
'warning'
)
throw new Exception(
get_lang('WrongNumberOfDays').': '.$numberOfDays.': '.$exception->getMessage()
);
return false;
}
$endDate = new DateTime();
$endDate->add($duration);
@ -715,14 +679,9 @@ class CourseManager
try {
Database::getManager()->flush();
} catch (\Doctrine\ORM\OptimisticLockException $exception) {
Display::addFlash(
Display::return_message(
get_lang('InternalDatabaseError').': '.$exception->getMessage(),
'warning'
)
throw new Exception(
get_lang('InternalDatabaseError').': '.$exception->getMessage()
);
return false;
}
$accessUrlRelSession = new \Chamilo\CoreBundle\Entity\AccessUrlRelSession();
$accessUrlRelSession->setAccessUrlId(api_get_current_access_url_id());
@ -731,14 +690,9 @@ class CourseManager
try {
Database::getManager()->flush();
} catch (\Doctrine\ORM\OptimisticLockException $exception) {
Display::addFlash(
Display::return_message(
get_lang('InternalDatabaseError').': '.$exception->getMessage(),
'warning'
)
throw new Exception(
get_lang('InternalDatabaseError').': '.$exception->getMessage()
);
return false;
}
} else {
// user has at least one accessible session, let's use it
@ -750,22 +704,36 @@ class CourseManager
try {
Database::getManager()->flush();
} catch (\Doctrine\ORM\OptimisticLockException $exception) {
Display::addFlash(
Display::return_message(
get_lang('InternalDatabaseError').': '.$exception->getMessage(),
'warning'
)
throw new Exception(
get_lang('InternalDatabaseError').': '.$exception->getMessage()
);
return false;
}
// subscribe user to course within this session
SessionManager::subscribe_users_to_session_course([$userId], $session->getId(), $course->getCode());
}
return true;
self::subscribeUser($userId, $course->getCode(), $status, 0);
}
/**
* @param string $courseCode
* @param int $status
*
* @return bool
*/
public static function autoSubscribeToCourse($courseCode, $status = STUDENT): bool
{
try {
self::processAutoSubscribeToCourse($courseCode, $status);
} catch (Exception $e) {
Display::addFlash(
Display::return_message($e->getMessage(), 'warning')
);
return false;
}
return self::subscribeUser($userId, $course->getCode(), $status, 0);
return true;
}
/**

Loading…
Cancel
Save