Skip to content

Statistics: Include tracking of users of a course who are not subscribed - refs #5958 #6237

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 16, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions public/main/admin/statistics/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -785,6 +785,49 @@
}
// courses for each course category
$content .= Statistics::printStats(get_lang('Courses'), $courses);

$content .= '
<button class="btn btn--info mb-3" onclick="toggleNonRegisteredUsers()">
'.get_lang('Show/Hide users active in open courses (not enrolled)').'
</button>

<div id="non-registered-users-block" style="display: none; margin-top: 10px;">
';

$sessionId = api_get_session_id();
$userList = Statistics::getUsersWithActivityButNotRegistered($sessionId);

if (!empty($userList)) {
$content .= Display::page_subheader2(get_lang('Users active in open courses (not enrolled)'));
$content .= Display::tag('p', get_lang('The following users have accessed one or more courses without being officially registered. They generated activity in open courses but are not listed in the course subscription tables.'));
$table = new HTML_Table(['class' => 'table table-hover table-striped data_table']);
$table->setHeaderContents(0, 0, get_lang('Name'));
$table->setHeaderContents(0, 1, get_lang('Course'));
$table->setHeaderContents(0, 2, get_lang('Last access'));
$row = 1;
foreach ($userList as $user) {
$name = Display::tag('strong', $user['firstname'].' '.$user['lastname']);
$course = Display::tag('em', $user['courseTitle'].' ('.$user['courseCode'].')');
$access = Security::remove_XSS($user['lastAccess']);

$table->setCellContents($row, 0, $name);
$table->setCellContents($row, 1, $course);
$table->setCellContents($row, 2, $access);
$row++;
}
$content .= $table->toHtml();
} else {
$content .= Display::tag('p', get_lang('No users found with activity in open courses without enrollment.'));
}
$content .= '</div>';
$content .= '
<script>
function toggleNonRegisteredUsers() {
const block = document.getElementById("non-registered-users-block");
block.style.display = block.style.display === "none" ? "block" : "none";
}
</script>';

break;
case 'tools':
$content .= '<canvas class="col-md-12" id="canvas" height="300px" style="margin-bottom: 20px"></canvas>';
Expand Down
73 changes: 73 additions & 0 deletions public/main/inc/lib/statistics.lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,12 @@
/* For licensing terms, see /license.txt */

use Chamilo\CoreBundle\Component\Utils\ChamiloApi;
use Chamilo\CoreBundle\Entity\CourseRelUser;
use Chamilo\CoreBundle\Entity\MessageRelUser;
use Chamilo\CoreBundle\Entity\ResourceLink;
use Chamilo\CoreBundle\Entity\TrackEAccess;
use Chamilo\CoreBundle\Entity\Course;
use Chamilo\CoreBundle\Entity\User;
use Chamilo\CoreBundle\Entity\UserRelUser;
use Chamilo\CoreBundle\Component\Utils\ActionIcon;
use Chamilo\CoreBundle\Framework\Container;
Expand Down Expand Up @@ -1944,4 +1948,73 @@ public static function getUnsubscriptionsByDay(string $startDate, string $endDat
'eventType2' => ParameterType::STRING,
])->fetchAllAssociative();
}

/**
* Returns users who have activity in open courses without being officially enrolled.
*/
public static function getUsersWithActivityButNotRegistered(int $sessionId = 0): array
{
$em = Database::getManager();

$qb = $em->createQueryBuilder();
$qb->select('t.accessUserId AS userId, t.cId AS courseId, MAX(t.accessDate) AS lastAccess')
->from(TrackEAccess::class, 't')
->where('t.accessUserId IS NOT NULL')
->andWhere('t.cId IS NOT NULL')
->groupBy('t.accessUserId, t.cId');

if ($sessionId > 0) {
$qb->andWhere('t.sessionId = :sessionId')
->setParameter('sessionId', $sessionId);
}

$results = $qb->getQuery()->getArrayResult();

$nonRegistered = [];

foreach ($results as $row) {
$userId = $row['userId'];
$courseId = $row['courseId'];

$course = $em->getRepository(Course::class)->find($courseId);
if (!$course) {
continue;
}

if (!\in_array($course->getVisibility(), [Course::OPEN_PLATFORM, Course::OPEN_WORLD], true)) {
continue;
}

$isRegistered = $em->createQueryBuilder()
->select('1')
->from(CourseRelUser::class, 'cu')
->where('cu.user = :userId AND cu.course = :courseId')
->setParameter('userId', $userId)
->setParameter('courseId', $courseId);

if ($sessionId > 0) {
$isRegistered->andWhere('cu.session = :sessionId')
->setParameter('sessionId', $sessionId);
}

if (empty($isRegistered->getQuery()->getResult())) {
$user = $em->getRepository(User::class)->find($userId);
if (!$user) {
continue;
}

$nonRegistered[] = [
'id' => $user->getId(),
'firstname' => $user->getFirstname(),
'lastname' => $user->getLastname(),
'email' => $user->getEmail(),
'courseTitle' => $course->getTitle(),
'courseCode' => $course->getCode(),
'lastAccess' => $row['lastAccess'] ? (new \DateTime($row['lastAccess']))->format('Y-m-d H:i:s') : '',
];
}
}

return $nonRegistered;
}
}
Loading