Sessions: Add sessions by user /api/users/{id}/sessions_rel_users.json

pull/3844/head
Julio Montoya 5 years ago
parent 9d2cc37100
commit e8c0ea309e
  1. 4
      src/CoreBundle/Entity/Course.php
  2. 64
      src/CoreBundle/Entity/Session.php
  3. 4
      src/CoreBundle/Entity/SessionRelCourseRelUser.php
  4. 26
      src/CoreBundle/Entity/SessionRelUser.php
  5. 3
      src/CoreBundle/Entity/User.php

@ -59,7 +59,7 @@ class Course extends AbstractResource implements ResourceInterface, ResourceWith
public const HIDDEN = 4;
/**
* @Groups({"course:read", "course_rel_user:read"})
* @Groups({"course:read", "course_rel_user:read", "session_rel_user:read", "session_rel_course_rel_user:read", "session_rel_user:read"})
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
@ -71,7 +71,7 @@ class Course extends AbstractResource implements ResourceInterface, ResourceWith
*
* @Assert\NotBlank(message="A Course requires a title")
*
* @Groups({"course:read", "course:write", "course_rel_user:read", "session_rel_course_rel_user:read"})
* @Groups({"course:read", "course:write", "course_rel_user:read", "session_rel_course_rel_user:read", "session_rel_user:read"})
*
* @ORM\Column(name="title", type="string", length=250, nullable=true, unique=false)
*/

@ -69,7 +69,7 @@ class Session
public const COACH = 2;
/**
* @Groups({"session:read"})
* @Groups({"session:read", "session_rel_user:read"})
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue()
@ -98,6 +98,8 @@ class Session
protected Collection $users;
/**
* @var Collection|SessionRelCourseRelUser[]
*
* @ORM\OneToMany(
* targetEntity="SessionRelCourseRelUser",
* mappedBy="session",
@ -138,7 +140,7 @@ class Session
/**
* @Assert\NotBlank()
* @Groups({"session:read", "session:write", "session_rel_course_rel_user:read", "document:read"})
* @Groups({"session:read", "session:write", "session_rel_course_rel_user:read", "document:read", "session_rel_user:read"})
* @ORM\Column(name="name", type="string", length=150)
*/
protected string $name;
@ -509,11 +511,12 @@ class Session
public function getUserInCourse(User $user, Course $course, $status = null): Collection
{
$criteria = Criteria::create()->where(
Criteria::expr()->eq('course', $course)
)->andWhere(
Criteria::expr()->eq('user', $user)
);
$criteria = Criteria::create()
->where(
Criteria::expr()->eq('course', $course)
)->andWhere(
Criteria::expr()->eq('user', $user)
);
if (null !== $status) {
$criteria->andWhere(
@ -524,12 +527,24 @@ class Session
return $this->getUserCourseSubscriptions()->matching($criteria);
}
/**
* Set name.
*
* @return $this
*/
public function setName(string $name)
public function getCoursesByUser(User $user, $status = null): Collection
{
$criteria = Criteria::create()
->where(
Criteria::expr()->eq('user', $user)
)
;
if (null !== $status) {
$criteria->andWhere(
Criteria::expr()->eq('status', $status)
);
}
return $this->getUserCourseSubscriptions()->matching($criteria);
}
public function setName(string $name): self
{
$this->name = $name;
@ -565,12 +580,7 @@ class Session
return $this;
}
/**
* Get nbrCourses.
*
* @return int
*/
public function getNbrCourses()
public function getNbrCourses(): int
{
return $this->nbrCourses;
}
@ -582,12 +592,7 @@ class Session
return $this;
}
/**
* Get nbrUsers.
*
* @return int
*/
public function getNbrUsers()
public function getNbrUsers(): int
{
return $this->nbrUsers;
}
@ -599,12 +604,7 @@ class Session
return $this;
}
/**
* Get nbrClasses.
*
* @return int
*/
public function getNbrClasses()
public function getNbrClasses(): int
{
return $this->nbrClasses;
}
@ -850,7 +850,7 @@ class Session
return $this->userCourseSubscriptions;
}
public function setUserCourseSubscriptions(ArrayCollection $userCourseSubscriptions): self
public function setUserCourseSubscriptions(Collection $userCourseSubscriptions): self
{
$this->userCourseSubscriptions = new ArrayCollection();

@ -15,7 +15,7 @@ use Symfony\Component\Serializer\Annotation\Groups;
* Class SessionRelCourseRelUser.
*
* @ApiResource(
* shortName="SessionSubscription",
* shortName="SessionCourseSubscription",
* normalizationContext={"groups"={"session_rel_course_rel_user:read", "user:read"}}
* )
* @ORM\Table(
@ -62,7 +62,7 @@ class SessionRelCourseRelUser
protected Session $session;
/**
* @Groups({"session_rel_course_rel_user:read"})
* @Groups({"session_rel_course_rel_user:read", "session_rel_user:read"})
* @ORM\ManyToOne(targetEntity="Chamilo\CoreBundle\Entity\Course", inversedBy="sessionUserSubscriptions", cascade={"persist"})
* @ORM\JoinColumn(name="c_id", referencedColumnName="id", nullable=false, onDelete="CASCADE")
*/

@ -6,14 +6,21 @@ declare(strict_types=1);
namespace Chamilo\CoreBundle\Entity;
use ApiPlatform\Core\Annotation\ApiResource;
use Chamilo\CoreBundle\Traits\UserTrait;
use DateTime;
use DateTimeZone;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Serializer\Annotation\Groups;
/**
* SessionRelUser.
*
* @ApiResource(
* shortName="SessionSubscription",
* normalizationContext={"groups"={"session_rel_user:read"}}
* )
* @ORM\Table(
* name="session_rel_user",
* indexes={
@ -42,12 +49,16 @@ class SessionRelUser
protected int $id;
/**
* @Groups({"session_rel_user:read"})
*
* @ORM\ManyToOne(targetEntity="Session", inversedBy="users", cascade={"persist"})
* @ORM\JoinColumn(name="session_id", referencedColumnName="id")
*/
protected Session $session;
/**
* @Groups({"session_rel_user:read"})
*
* @ORM\ManyToOne(targetEntity="User", inversedBy="sessionsRelUser", cascade={"persist"})
* @ORM\JoinColumn(name="user_id", referencedColumnName="id")
*/
@ -83,6 +94,11 @@ class SessionRelUser
*/
protected DateTime $registeredAt;
/**
* @Groups({"session_rel_user:read"})
*/
protected Collection $courses;
public function __construct()
{
$this->duration = 0;
@ -91,6 +107,16 @@ class SessionRelUser
$this->registeredAt = new DateTime('now', new DateTimeZone('UTC'));
}
public function getCourses()
{
return $this->session->getCoursesByUser($this->getUser());
}
public function getId(): int
{
return $this->id;
}
public function setSession(Session $session): self
{
$this->session = $session;

@ -340,9 +340,8 @@ class User implements UserInterface, EquatableInterface, ResourceInterface, Reso
protected Collection $resourceNodes;
/**
* @ApiSubresource()
*
* @var Collection<int, SessionRelCourseRelUser>|SessionRelCourseRelUser[]
*
* @ORM\OneToMany(
* targetEntity="Chamilo\CoreBundle\Entity\SessionRelCourseRelUser",
* mappedBy="user",

Loading…
Cancel
Save