|
2 | 2 | /* For licensing terms, see /license.txt */ |
3 | 3 |
|
4 | 4 | use Chamilo\CoreBundle\Component\Utils\ChamiloApi; |
| 5 | +use Chamilo\CoreBundle\Entity\CourseRelUser; |
5 | 6 | use Chamilo\CoreBundle\Entity\MessageRelUser; |
6 | 7 | use Chamilo\CoreBundle\Entity\ResourceLink; |
| 8 | +use Chamilo\CoreBundle\Entity\TrackEAccess; |
| 9 | +use Chamilo\CoreBundle\Entity\Course; |
| 10 | +use Chamilo\CoreBundle\Entity\User; |
7 | 11 | use Chamilo\CoreBundle\Entity\UserRelUser; |
8 | 12 | use Chamilo\CoreBundle\Component\Utils\ActionIcon; |
9 | 13 | use Chamilo\CoreBundle\Framework\Container; |
@@ -1944,4 +1948,73 @@ public static function getUnsubscriptionsByDay(string $startDate, string $endDat |
1944 | 1948 | 'eventType2' => ParameterType::STRING, |
1945 | 1949 | ])->fetchAllAssociative(); |
1946 | 1950 | } |
| 1951 | + |
| 1952 | + /** |
| 1953 | + * Returns users who have activity in open courses without being officially enrolled. |
| 1954 | + */ |
| 1955 | + public static function getUsersWithActivityButNotRegistered(int $sessionId = 0): array |
| 1956 | + { |
| 1957 | + $em = Database::getManager(); |
| 1958 | + |
| 1959 | + $qb = $em->createQueryBuilder(); |
| 1960 | + $qb->select('t.accessUserId AS userId, t.cId AS courseId, MAX(t.accessDate) AS lastAccess') |
| 1961 | + ->from(TrackEAccess::class, 't') |
| 1962 | + ->where('t.accessUserId IS NOT NULL') |
| 1963 | + ->andWhere('t.cId IS NOT NULL') |
| 1964 | + ->groupBy('t.accessUserId, t.cId'); |
| 1965 | + |
| 1966 | + if ($sessionId > 0) { |
| 1967 | + $qb->andWhere('t.sessionId = :sessionId') |
| 1968 | + ->setParameter('sessionId', $sessionId); |
| 1969 | + } |
| 1970 | + |
| 1971 | + $results = $qb->getQuery()->getArrayResult(); |
| 1972 | + |
| 1973 | + $nonRegistered = []; |
| 1974 | + |
| 1975 | + foreach ($results as $row) { |
| 1976 | + $userId = $row['userId']; |
| 1977 | + $courseId = $row['courseId']; |
| 1978 | + |
| 1979 | + $course = $em->getRepository(Course::class)->find($courseId); |
| 1980 | + if (!$course) { |
| 1981 | + continue; |
| 1982 | + } |
| 1983 | + |
| 1984 | + if (!\in_array($course->getVisibility(), [Course::OPEN_PLATFORM, Course::OPEN_WORLD], true)) { |
| 1985 | + continue; |
| 1986 | + } |
| 1987 | + |
| 1988 | + $isRegistered = $em->createQueryBuilder() |
| 1989 | + ->select('1') |
| 1990 | + ->from(CourseRelUser::class, 'cu') |
| 1991 | + ->where('cu.user = :userId AND cu.course = :courseId') |
| 1992 | + ->setParameter('userId', $userId) |
| 1993 | + ->setParameter('courseId', $courseId); |
| 1994 | + |
| 1995 | + if ($sessionId > 0) { |
| 1996 | + $isRegistered->andWhere('cu.session = :sessionId') |
| 1997 | + ->setParameter('sessionId', $sessionId); |
| 1998 | + } |
| 1999 | + |
| 2000 | + if (empty($isRegistered->getQuery()->getResult())) { |
| 2001 | + $user = $em->getRepository(User::class)->find($userId); |
| 2002 | + if (!$user) { |
| 2003 | + continue; |
| 2004 | + } |
| 2005 | + |
| 2006 | + $nonRegistered[] = [ |
| 2007 | + 'id' => $user->getId(), |
| 2008 | + 'firstname' => $user->getFirstname(), |
| 2009 | + 'lastname' => $user->getLastname(), |
| 2010 | + 'email' => $user->getEmail(), |
| 2011 | + 'courseTitle' => $course->getTitle(), |
| 2012 | + 'courseCode' => $course->getCode(), |
| 2013 | + 'lastAccess' => $row['lastAccess'] ? (new \DateTime($row['lastAccess']))->format('Y-m-d H:i:s') : '', |
| 2014 | + ]; |
| 2015 | + } |
| 2016 | + } |
| 2017 | + |
| 2018 | + return $nonRegistered; |
| 2019 | + } |
1947 | 2020 | } |
0 commit comments