Skip to content

Commit

Permalink
Merge pull request #5999 from christianbeeznest/storm-22278
Browse files Browse the repository at this point in the history
Session: Fix duplicate entry error in session import - refs BT#22278
  • Loading branch information
NicoDucou authored Dec 23, 2024
2 parents 94b92a4 + 1812b56 commit 68360b8
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 12 deletions.
11 changes: 4 additions & 7 deletions public/main/inc/lib/sessionmanager.lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -5558,13 +5558,10 @@ public static function importCSV(
}
}

$userList[] = $user_id;
// Insert new users.
$sql = "INSERT IGNORE INTO $tbl_session_user SET
user_id = '$user_id',
session_id = '$session_id',
registered_at = '".api_get_utc_datetime()."'";
Database::query($sql);
$userEntity = api_get_user_entity($user_id);
$sessionEntity = api_get_session_entity($session_id);
$sessionEntity->addUserInSession(Session::STUDENT, $userEntity);

if ($debug) {
$logger->debug("Adding User #$user_id ($user) to session #$session_id");
}
Expand Down
27 changes: 22 additions & 5 deletions src/CoreBundle/Entity/Session.php
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ class Session implements ResourceWithAccessUrlInterface, Stringable
'session:write',
])]
#[ORM\Column(name: 'show_description', type: 'boolean', nullable: true)]
protected ?bool $showDescription;
protected ?bool $showDescription = false;

#[Groups(['session:read', 'session:write', 'user_subscriptions:sessions'])]
#[ORM\Column(name: 'duration', type: 'integer', nullable: true)]
Expand Down Expand Up @@ -446,7 +446,7 @@ public function setDuration(int $duration): self

public function getShowDescription(): bool
{
return $this->showDescription;
return $this->showDescription ?? false;
}

public function setShowDescription(bool $showDescription): self
Expand Down Expand Up @@ -810,7 +810,18 @@ public function addGeneralCoach(User $coach): self

public function addUserInSession(int $relationType, User $user): self
{
$sessionRelUser = (new SessionRelUser())->setUser($user)->setRelationType($relationType);
foreach ($this->getUsers() as $existingSubscription) {
if (
$existingSubscription->getUser()->getId() === $user->getId() &&
$existingSubscription->getRelationType() === $relationType
) {
return $this;
}
}

$sessionRelUser = (new SessionRelUser())
->setUser($user)
->setRelationType($relationType);
$this->addUserSubscription($sessionRelUser);

return $this;
Expand Down Expand Up @@ -985,8 +996,12 @@ public function removeCourse(Course $course): bool
* If user status in session is student, then increase number of course users.
* Status example: Session::STUDENT.
*/
public function addUserInCourse(int $status, User $user, Course $course): SessionRelCourseRelUser
public function addUserInCourse(int $status, User $user, Course $course): ?SessionRelCourseRelUser
{
if ($this->hasUserInCourse($user, $course, $status)) {
return null;
}

$userRelCourseRelSession = (new SessionRelCourseRelUser())
->setCourse($course)
->setUser($user)
Expand All @@ -998,7 +1013,9 @@ public function addUserInCourse(int $status, User $user, Course $course): Sessio

if (self::STUDENT === $status) {
$sessionCourse = $this->getCourseSubscription($course);
$sessionCourse->setNbrUsers($sessionCourse->getNbrUsers() + 1);
if ($sessionCourse) {
$sessionCourse->setNbrUsers($sessionCourse->getNbrUsers() + 1);
}
}

return $userRelCourseRelSession;
Expand Down

0 comments on commit 68360b8

Please sign in to comment.