Skip to content

Commit

Permalink
Click on course's "about" page "Subscribe" button leads to course pag…
Browse files Browse the repository at this point in the history
…e only if session is currently accessible - refs BT#16808

also factorized code as User and Session methods
  • Loading branch information
Sébastien Ducoulombier committed Mar 12, 2020
1 parent 7d9c6f8 commit 0e61a23
Show file tree
Hide file tree
Showing 4 changed files with 127 additions and 34 deletions.
15 changes: 12 additions & 3 deletions main/course_home/course_home.php
Original file line number Diff line number Diff line change
Expand Up @@ -147,13 +147,22 @@
if ($action == 'subscribe') {
if (Security::check_token('get')) {
Security::clear_token();
$result = CourseManager::autoSubscribeToCourse($course_code);
if ($result) {
$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);
}
} elseif (api_get_configuration_value('catalog_course_subscription_in_user_s_session')) {
/**
* @var $user Chamilo\UserBundle\Entity\User
*/
$user = UserManager::getRepository()->find(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';
}
}
header('Location: '.api_get_self());
header('Location: ' . $redirectionTarget);
exit;
}
}
Expand Down
61 changes: 30 additions & 31 deletions main/inc/lib/course.lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -568,10 +568,20 @@ public static function autoSubscribeToCourse($courseCode, $status = STUDENT)
* @var $user Chamilo\UserBundle\Entity\User
*/
$user = UserManager::getRepository()->find($userId);
$sessionRelUser = Database::getManager()->getRepository('ChamiloCoreBundle:SessionRelUser')->findOneBy([
'user' => $user,
]);
if (is_null($sessionRelUser)) {
$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;
}
// 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));
Expand All @@ -584,7 +594,6 @@ public static function autoSubscribeToCourse($courseCode, $status = STUDENT)
);
return false;
}
$sessionAdminId = api_get_configuration_value('session_automatic_creation_user_id') ?: 1;
$endDate = new DateTime();
$endDate->add($duration);
$session = new \Chamilo\CoreBundle\Entity\Session();
Expand All @@ -595,7 +604,7 @@ public static function autoSubscribeToCourse($courseCode, $status = STUDENT)
$session->setCoachAccessEndDate($endDate);
$session->setDisplayEndDate($endDate);
$session->setSendSubscriptionNotification(false);
$session->setSessionAdminId($sessionAdminId);
$session->setSessionAdminId(api_get_configuration_value('session_automatic_creation_user_id') ?: 1);
$session->addUserInSession(0, $user);
Database::getManager()->persist($session);
try {
Expand Down Expand Up @@ -624,20 +633,11 @@ public static function autoSubscribeToCourse($courseCode, $status = STUDENT)
);
return false;
}

} else {
$session = $sessionRelUser->getSession();
}
$now = new DateTime();
if ($now < $session->getAccessStartDate() or $session->getAccessEndDate() < $now) {
Display::addFlash(
Display::return_message(
get_lang('CanNotSubscribeToCourseUserSessionExpired'),
'warning'
)
);
return false;
// user has at least one accessible session, let's use it
$session = $sessions[0];
}
// add chosen course to the user session
$session->addCourse($course);
Database::getManager()->persist($session);
try {
Expand All @@ -651,6 +651,7 @@ public static function autoSubscribeToCourse($courseCode, $status = STUDENT)
);
return false;
}
// subscribe user to course within this session
SessionManager::subscribe_users_to_session_course([$userId], $session->getId(), $course->getCode());
return true;
}
Expand Down Expand Up @@ -1180,28 +1181,26 @@ public static function is_user_subscribed_in_course(
$session_id = (int) $session_id;

if (api_get_configuration_value('catalog_course_subscription_in_user_s_session')) {
// with this option activated, only check whether the course is in one of the users' accessible sessions
$course = Database::getManager()->getRepository('ChamiloCoreBundle:Course')->findOneBy([
'code' => $course_code
]);
if (is_null($course)) {
return false;
}
/**
* @var $user \Chamilo\UserBundle\Entity\User
*/
$user = UserManager::getRepository()->find($user_id);
$sessionRelUser = Database::getManager()->getRepository('ChamiloCoreBundle:SessionRelUser')->findOneBy([
'user' => $user,
]);
if (is_null($sessionRelUser)) {
if (is_null($user)) {
return false;
}
/**
* @var $session \Chamilo\CoreBundle\Entity\Session
*/
$session = $sessionRelUser->getSession();
$sessionRelCourse = Database::getManager()->getRepository('ChamiloCoreBundle:SessionRelCourse')->findOneBy([
'session' => $session,
'course' => $course,
]);
return !is_null($sessionRelCourse);
foreach($user->getCurrentlyAccessibleSessions() as $session) {
if ($session->isRelatedToCourse($course)) {
return true;
}
}
return false;
}

if (empty($session_id)) {
Expand Down
33 changes: 33 additions & 0 deletions src/Chamilo/CoreBundle/Entity/Session.php
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,22 @@ public function hasCourse(Course $course)
return false;
}

/**
* Check for existence of a relation (SessionRelCourse) between a course and this session
*
* @param Course $course
* @return bool whether the course is related to this session
*/
public function isRelatedToCourse(Course $course)
{
return !is_null(
\Database::getManager()->getRepository('ChamiloCoreBundle:SessionRelCourse')->findOneBy([
'session' => $this,
'course' => $course,
])
);
}

/**
* Remove $course.
*
Expand Down Expand Up @@ -899,6 +915,23 @@ public function isActive()
return $now > $this->getAccessStartDate();
}

/**
* Compare the current date with start and end access dates.
* Either missing date is interpreted as no limit.
*
* @return bool whether now is between the session access start and end dates
*/
public function isCurrentlyAccessible()
{
try {
$now = new \Datetime();
} catch (\Exception $exception) {
return false;
}
return (is_null($this->accessStartDate) || $this->accessStartDate < $now)
&& (is_null($this->accessEndDate) || $now < $this->accessEndDate);
}

public function addCourse(Course $course)
{
$entity = new SessionRelCourse();
Expand Down
52 changes: 52 additions & 0 deletions src/Chamilo/UserBundle/Entity/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -2585,4 +2585,56 @@ public function getPictureLegacy()

return 'users/'.substr((string) $id, 0, 1).'/'.$id.'/'.'small_'.$this->getPictureUri();
}

/**
* Retreives this user's related sessions
*
* @param int $relationType \Chamilo\CoreBundle\Entity\SessionRelUser::relationTypeList key
* @return \Chamilo\CoreBundle\Entity\Session[]
*/
public function getSessions($relationType) {
$sessions = [];
foreach (\Database::getManager()->getRepository('ChamiloCoreBundle:SessionRelUser')->findBy([
'user' => $this,
]) as $sessionRelUser) {
if ($sessionRelUser->getRelationType() == $relationType) {
$sessions[] = $sessionRelUser->getSession();
}
}
return $sessions;
}

/**
* Retreives this user's related student sessions
*
* @return \Chamilo\CoreBundle\Entity\Session[]
*/
public function getStudentSessions() {
return $this->getSessions(0);
}

/**
* Retreives this user's related DRH sessions
*
* @return \Chamilo\CoreBundle\Entity\Session[]
*/
public function getDRHSessions() {
return $this->getSessions(1);
}

/**
* Retreives this user's related accessible sessions of a type, student by default
*
* @param int $relationType \Chamilo\CoreBundle\Entity\SessionRelUser::relationTypeList key
* @return \Chamilo\CoreBundle\Entity\Session[]
*/
public function getCurrentlyAccessibleSessions($relationType = 0) {
$sessions = [];
foreach ($this->getSessions($relationType) as $session) {
if ($session->isCurrentlyAccessible()) {
$sessions[] = $session;
}
}
return $sessions;
}
}

0 comments on commit 0e61a23

Please sign in to comment.