From bb932fecf930f69d5822bb8e785f6191810a5ace Mon Sep 17 00:00:00 2001 From: Julio Montoya Date: Tue, 22 Jun 2021 11:36:18 +0200 Subject: [PATCH] Move getUserPendingInvitations in a repo --- public/main/survey/pending.php | 24 +++------ public/main/survey/surveyUtil.class.php | 32 +----------- src/CoreBundle/Entity/User.php | 2 +- .../Repository/Node/UserRepository.php | 52 ++++++++++++------- 4 files changed, 43 insertions(+), 67 deletions(-) diff --git a/public/main/survey/pending.php b/public/main/survey/pending.php index 89ae30268e..aa681ecadd 100644 --- a/public/main/survey/pending.php +++ b/public/main/survey/pending.php @@ -2,8 +2,7 @@ /* For licensing terms, see /license.txt */ -use Chamilo\CourseBundle\Entity\CSurvey; -use Chamilo\CourseBundle\Entity\CSurveyInvitation; +use Chamilo\CoreBundle\Framework\Container; $cidReset = true; @@ -11,24 +10,15 @@ require_once __DIR__.'/../inc/global.inc.php'; api_block_anonymous_users(); -$em = Database::getManager(); - $currentUser = api_get_user_entity(api_get_user_id()); -$pending = SurveyUtil::getUserPendingInvitations($currentUser->getId()); +$pendingList = Container::getUserRepository()->getUserPendingInvitations($currentUser); $surveysData = []; -foreach ($pending as $i => $item) { - if (is_a($item, 'Chamilo\CourseBundle\Entity\CSurveyInvitation')) { - continue; - } - - /** @var CSurvey $survey */ - $survey = $item; - /** @var CSurveyInvitation invitation */ - $invitation = $pending[$i + 1]; - $course = api_get_course_entity($survey->getCId()); - $session = api_get_session_entity($survey->getSessionId()); +foreach ($pendingList as $pending) { + $course = $pending->getCourse(); + $session = $pending->getSession(); + $survey = $pending->getSurvey(); //$course = $course ? ['id' => $course->getId(), 'title' => $course->getTitle(), 'code' => $course->getCode()] : null; $session = $session ? ['id' => $session->getId(), 'name' => $session->getName()] : null; @@ -41,7 +31,7 @@ foreach ($pending as $i => $item) { 'session' => $session, 'link' => SurveyUtil::generateFillSurveyLink( $survey, - $invitation->getInvitationCode(), + $pending->getInvitationCode(), $course ), ]; diff --git a/public/main/survey/surveyUtil.class.php b/public/main/survey/surveyUtil.class.php index 54702ccdf8..14bb6abcb2 100644 --- a/public/main/survey/surveyUtil.class.php +++ b/public/main/survey/surveyUtil.class.php @@ -4,6 +4,7 @@ use Chamilo\CoreBundle\Entity\Course; use Chamilo\CoreBundle\Entity\Session as SessionEntity; +use Chamilo\CoreBundle\Entity\User; use Chamilo\CoreBundle\Framework\Container; use Chamilo\CourseBundle\Entity\CGroup; use Chamilo\CourseBundle\Entity\CSurvey; @@ -3959,37 +3960,6 @@ class SurveyUtil return $response > 0; } - /** - * Get the pending surveys for a user. - * - * @param int $userId - * - * @return array - */ - public static function getUserPendingInvitations($userId) - { - $now = api_get_utc_datetime(null, false, true); - - $dql = " - SELECT s, si FROM ChamiloCourseBundle:CSurvey s - INNER JOIN ChamiloCourseBundle:CSurveyInvitation si - WITH (s.code = si.surveyCode AND s.cId = si.cId AND s.sessionId = si.sessionId ) - WHERE - si.user = :user_id AND - s.availFrom <= :now AND - s.availTill >= :now AND - si.answered = 0 - ORDER BY s.availTill ASC - "; - - $pendingSurveys = Database::getManager() - ->createQuery($dql) - ->setParameters(['user_id' => $userId, 'now' => $now->format('Y-m-d')]) - ->getResult(); - - return $pendingSurveys; - } - /** * @param int $surveyId * @param int $courseId diff --git a/src/CoreBundle/Entity/User.php b/src/CoreBundle/Entity/User.php index 61363c9766..5d1c423b1e 100644 --- a/src/CoreBundle/Entity/User.php +++ b/src/CoreBundle/Entity/User.php @@ -699,7 +699,7 @@ class User implements UserInterface, EquatableInterface, ResourceInterface, Reso * @var Collection|CSurveyInvitation[] */ #[ORM\OneToMany( - targetEntity: 'Chamilo\\CourseBundle\\Entity\\CSurveyInvitation', + targetEntity: 'Chamilo\CourseBundle\Entity\CSurveyInvitation', mappedBy: 'user', cascade: ['remove'] )] diff --git a/src/CoreBundle/Repository/Node/UserRepository.php b/src/CoreBundle/Repository/Node/UserRepository.php index ebd94dbbd1..53a817ce12 100644 --- a/src/CoreBundle/Repository/Node/UserRepository.php +++ b/src/CoreBundle/Repository/Node/UserRepository.php @@ -16,6 +16,7 @@ use Chamilo\CoreBundle\Entity\TrackELogin; use Chamilo\CoreBundle\Entity\TrackEOnline; use Chamilo\CoreBundle\Entity\User; use Chamilo\CoreBundle\Repository\ResourceRepository; +use Chamilo\CourseBundle\Entity\CSurveyInvitation; use Datetime; use Doctrine\Common\Collections\Collection; use Doctrine\Common\Collections\Criteria; @@ -23,7 +24,6 @@ use Doctrine\DBAL\Types\Types; use Doctrine\ORM\Query\Expr\Join; use Doctrine\ORM\QueryBuilder; use Doctrine\Persistence\ManagerRegistry; -use Exception; use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface; use Symfony\Component\Security\Core\Exception\UserNotFoundException; use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface; @@ -444,12 +444,12 @@ class UserRepository extends ResourceRepository implements PasswordUpgraderInter */ public function getCountUsersByUrl(AccessUrl $url) { - return $this->createQueryBuilder('a') + return $this->createQueryBuilder('u') ->select('COUNT(a)') - ->innerJoin('a.portals', 'u') - ->where('u.portal = :u') + ->innerJoin('a.portals', 'p') + ->where('p.portal = :p') ->setParameters([ - 'u' => $url, + 'p' => $url, ]) ->getQuery() ->getSingleScalarResult() @@ -463,15 +463,15 @@ class UserRepository extends ResourceRepository implements PasswordUpgraderInter */ public function getCountTeachersByUrl(AccessUrl $url) { - $qb = $this->createQueryBuilder('a'); + $qb = $this->createQueryBuilder('u'); return $qb - ->select('COUNT(a)') - ->innerJoin('a.portals', 'u') - ->where('u.portal = :u') - ->andWhere($qb->expr()->in('a.roles', ['ROLE_TEACHER'])) + ->select('COUNT(u)') + ->innerJoin('a.portals', 'p') + ->where('p.portal = :p') + ->andWhere($qb->expr()->in('u.roles', ['ROLE_TEACHER'])) ->setParameters([ - 'u' => $url, + 'p' => $url, ]) ->getQuery() ->getSingleScalarResult() @@ -581,19 +581,17 @@ class UserRepository extends ResourceRepository implements PasswordUpgraderInter * as user.last_login was only implemented in 1.10 version with a default * value of NULL (not the last record from track_e_login). * - * @throws Exception - * * @return null|TrackELogin */ public function getLastLogin(User $user) { - $repo = $this->getEntityManager()->getRepository(TrackELogin::class); - $qb = $repo->createQueryBuilder('u'); + $qb = $this->createQueryBuilder('u'); return $qb - ->select('u') + ->select('l') + ->innerJoin('u.logins', 'l') ->where( - $qb->expr()->eq('u.loginUserId', $user->getId()) + $qb->expr()->eq('l.user', $user) ) ->setMaxResults(1) ->orderBy('u.loginDate', Criteria::DESC) @@ -637,8 +635,26 @@ class UserRepository extends ResourceRepository implements PasswordUpgraderInter return $qb; } - public function getUserPendingInvitations(): void + /** + * @return CSurveyInvitation[] + */ + public function getUserPendingInvitations(User $user) { + $qb = $this->createQueryBuilder('u'); + $qb + ->select('s') + ->innerJoin('u.surveyInvitations', 's') + ->andWhere('s.user = :u') + ->andWhere('s.availFrom <= :now AND s.availTill >= :now') + ->andWhere('s.answered = 0') + ->setParameters([ + 'now' => new Datetime(), + 'u' => $user, + ]) + ->orderBy('s.availTill', Criteria::ASC) + ; + + return $qb->getQuery()->getResult(); } private function addRoleQueryBuilder(string $role, QueryBuilder $qb = null): QueryBuilder