add course to user session and not user to course - refs BT#16808

as an option
pull/3122/head
Sébastien Ducoulombier 6 years ago
parent 1aee459da2
commit 850a3c4bdf
  1. 77
      main/inc/lib/course.lib.php
  2. 6
      main/install/configuration.dist.php
  3. 3
      src/Chamilo/CoreBundle/Entity/Session.php

@ -536,9 +536,9 @@ class CourseManager
*/
public static function autoSubscribeToCourse($courseCode, $status = STUDENT)
{
$courseInfo = api_get_course_info($courseCode);
$course = Database::getManager()->getRepository('ChamiloCoreBundle:Course')->findOneBy(['code' => $courseCode]);
if (empty($courseInfo)) {
if (is_null($course)) {
return false;
}
@ -546,15 +546,11 @@ class CourseManager
return false;
}
if (in_array(
$courseInfo['visibility'],
[
if (in_array($course->getVisibility(), [
COURSE_VISIBILITY_CLOSED,
COURSE_VISIBILITY_REGISTERED,
COURSE_VISIBILITY_HIDDEN,
]
)
) {
])) {
Display::addFlash(
Display::return_message(
get_lang('SubscribingNotAllowed'),
@ -565,7 +561,70 @@ class CourseManager
return false;
}
return self::subscribeUser(api_get_user_id(), $courseInfo['code'], $status);
$userId = api_get_user_id();
if (api_get_configuration_value('catalog_course_subscription_in_user_s_session')) {
/**
* @var $user Chamilo\UserBundle\Entity\User
*/
$user = UserManager::getRepository()->find($userId);
$sessionRelUser = Database::getManager()->getRepository('ChamiloCoreBundle:SessionRelUser')->findOneBy([
'user' => $user,
]);
if (is_null($sessionRelUser)) {
$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'
)
);
return false;
}
$endDate = new DateTime();
$endDate->add($duration);
$session = new \Chamilo\CoreBundle\Entity\Session();
$session->setName(get_lang(sprintf("%s %s Courses", $user->getFirstname(), $user->getLastname())));
$session->setAccessEndDate($endDate);
$session->setCoachAccessEndDate($endDate);
$session->setDisplayEndDate($endDate);
$session->setSendSubscriptionNotification(false);
$session->addUserInSession($status, $user);
} else {
$session = $sessionRelUser->getSession();
}
$now = new DateTime();
if ($now < $session->getAccessStartDate() or $session->getAccessEndDate() < $now) {
Display::addFlash(
Display::return_message(
get_lang('UserSessionExpired')
.': now='.$now->format('y-M-d h:m')
.', start='.$session->getAccessStartDate()->format('y-M-d h:m')
.', end='.$session->getAccessEndDate()->format('y-M-d h:m'),
'warning'
)
);
return false;
}
$session->addCourse($course);
Database::getManager()->persist($session);
try {
Database::getManager()->flush();
} catch (\Doctrine\ORM\OptimisticLockException $exception) {
Display::addFlash(
Display::return_message(
get_lang('InternalDatabaseError') . ': ' . $exception->getMessage(),
'warning'
)
);
return false;
}
return true;
}
return self::subscribeUser($userId, $course->getCode(), $status);
}
/**

@ -1408,6 +1408,12 @@ ALTER TABLE notification_event ADD COLUMN event_id INT NULL;
// Search user by extra field in the user list.
//$_configuration['user_search_on_extra_fields'] = ['extra_fields' = > ['variable1', 'variable2']];
// user subscription to a session rather than to a base course
// user session is created at first subscription
//$_configuration['catalog_course_subscription_in_user_s_session'] = false;
// user session duration in days - after the session end date, more subscriptions are prevented
//$_configuration['user_s_session_duration'] = 3*365;
// KEEP THIS AT THE END
// -------- Custom DB changes
// Add user activation by confirmation email

@ -329,6 +329,7 @@ class Session
if (!$this->hasUser($user)) {
$this->users[] = $user;
$this->setNbrUsers(count($this->users));
}
}
@ -902,7 +903,9 @@ class Session
{
$entity = new SessionRelCourse();
$entity->setCourse($course);
$entity->setPosition(0);
$this->addCourses($entity);
$this->setNbrCourses(count($this->courses));
}
/**

Loading…
Cancel
Save