Reporting: Add export for user progress in all sessions of one course - refs BT#21794

Authored-by: @christianbeeznest
pull/3943/head^2
christianbeeznest 1 year ago committed by GitHub
parent 5b3cc85363
commit a5e65df633
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 83
      main/admin/statistics/index.php
  2. 30
      main/inc/lib/sessionmanager.lib.php
  3. 51
      main/inc/lib/statistics.lib.php
  4. 37
      main/inc/lib/tracking.lib.php

@ -581,8 +581,6 @@ switch ($report) {
$sessionCount++;
}
$content .= Display::page_subheader2(get_lang('GeneralStats'));
// Coach
$sql = "SELECT count(DISTINCT(id_coach)) count FROM $tableSession
WHERE
@ -635,6 +633,52 @@ switch ($report) {
}
}
$content .= Display::page_subheader2(get_lang('UsersReportByCourseInSessions'));
$tableCourse = new HTML_Table(['class' => 'table table-responsive']);
$headers = [
get_lang('Course'),
get_lang('CountOfSessions'),
get_lang('UsersReport'),
];
$row = 0;
$column = 0;
foreach ($headers as $header) {
$tableCourse->setHeaderContents($row, $column, $header);
$column++;
}
$row++;
if (!empty($courseSessions)) {
$dateStart = null;
$dateEnd = null;
if (isset($_REQUEST['range_start'])) {
$dateStart = Security::remove_XSS($_REQUEST['range_start']);
}
if (isset($_REQUEST['range_end'])) {
$dateEnd = Security::remove_XSS($_REQUEST['range_end']);
}
$conditions = "&date_start=$dateStart&date_end=$dateEnd";
arsort($courseSessions);
foreach ($courseSessions as $courseId => $count) {
$courseInfo = api_get_course_info_by_id($courseId);
$tableCourse->setCellContents($row, 0, $courseInfo['name']);
$tableCourse->setCellContents($row, 1, $count);
$exportLink = api_get_self().'?report=session_by_date&course_id='.$courseId.'&action=export_users'.$conditions;
$urlExport = Display::url(
Display::return_icon('excel.png', get_lang('UsersReport')),
$exportLink
);
$tableCourse->setCellContents($row, 2, $urlExport);
$row++;
}
}
$content .= $tableCourse->toHtml();
$content .= Display::page_subheader2(get_lang('GeneralStats'));
$table = new HTML_Table(['class' => 'table table-responsive']);
$row = 0;
$table->setCellContents($row, 0, get_lang('Weeks'));
@ -668,32 +712,6 @@ switch ($report) {
}
$content .= '</div>';
$tableCourse = new HTML_Table(['class' => 'table table-responsive']);
$headers = [
get_lang('Course'),
get_lang('CountOfSessions'),
];
$row = 0;
$column = 0;
foreach ($headers as $header) {
$tableCourse->setHeaderContents($row, $column, $header);
$column++;
}
$row++;
if (!empty($courseSessions)) {
arsort($courseSessions);
foreach ($courseSessions as $courseId => $count) {
$courseInfo = api_get_course_info_by_id($courseId);
$tableCourse->setCellContents($row, 0, $courseInfo['name']);
$tableCourse->setCellContents($row, 1, $count);
$row++;
}
}
$content .= $tableCourse->toHtml();
$content .= '<div class="row">';
$content .= '<div class="col-md-4"><canvas id="canvas1" style="margin-bottom: 20px"></canvas></div>';
$content .= '<div class="col-md-4"><canvas id="canvas2" style="margin-bottom: 20px"></canvas></div>';
@ -754,6 +772,15 @@ switch ($report) {
$content .= $table->toHtml();
if (isset($_REQUEST['action']) && 'export_users' === $_REQUEST['action'] && isset($_REQUEST['course_id'])) {
$courseId = intval($_REQUEST['course_id']);
$startDate = isset($_REQUEST['date_start']) ? Database::escape_string($_REQUEST['date_start']) : null;
$endDate = isset($_REQUEST['date_end']) ? Database::escape_string($_REQUEST['date_end']) : null;
Statistics::exportUserReportByCourseSession($courseId, $startDate, $endDate);
exit;
}
if (isset($_REQUEST['action']) && 'export' === $_REQUEST['action']) {
$data = $table->toArray();
Export::arrayToXls($data);

@ -4906,12 +4906,14 @@ class SessionManager
/**
* @param int $courseId
* @param string|null $startDate
* @param string|null $endDate
*
* @return array
*
* @todo Add param to get only active sessions (not expires ones)
*/
public static function get_session_by_course($courseId)
public static function get_session_by_course(int $courseId, ?string $startDate = null, ?string $endDate = null): array
{
$table_session_course = Database::get_main_table(TABLE_MAIN_SESSION_COURSE);
$table_session = Database::get_main_table(TABLE_MAIN_SESSION);
@ -4923,15 +4925,25 @@ class SessionManager
return [];
}
$dateCondition = '';
if ($startDate && $endDate) {
$dateCondition .= "AND (s.display_start_date BETWEEN '$startDate' AND '$endDate' OR s.display_end_date BETWEEN '$startDate' AND '$endDate') ";
} elseif ($startDate) {
$dateCondition .= "AND s.display_start_date >= '$startDate' ";
} elseif ($endDate) {
$dateCondition .= "AND s.display_end_date <= '$endDate' ";
}
$sql = "SELECT name, s.id
FROM $table_session_course sc
INNER JOIN $table_session s
ON (sc.session_id = s.id)
INNER JOIN $url u
ON (u.session_id = s.id)
WHERE
u.access_url_id = $urlId AND
sc.c_id = '$courseId' ";
FROM $table_session_course sc
INNER JOIN $table_session s
ON (sc.session_id = s.id)
INNER JOIN $url u
ON (u.session_id = s.id)
WHERE
u.access_url_id = $urlId AND
sc.c_id = '$courseId'
$dateCondition";
$result = Database::query($sql);
return Database::store_result($result);

@ -2103,4 +2103,55 @@ class Statistics
return $usersInfo;
}
/**
* Exports a user report by course and session to an Excel file.
*/
public static function exportUserReportByCourseSession(int $courseId, ?string $startDate = null, ?string $endDate = null): void
{
$courseInfo = api_get_course_info_by_id($courseId);
$sessions = SessionManager::get_session_by_course($courseId, $startDate, $endDate);
$headers = [
get_lang('CourseName'),
get_lang('SessionName'),
get_lang('LastName'),
get_lang('FirstName'),
get_lang('Email'),
get_lang('EndDate'),
get_lang('Score'),
get_lang('Progress')
];
$exportData = [$headers];
foreach ($sessions as $session) {
$sessionId = (int) $session['id'];
$students = SessionManager::get_users_by_session($sessionId);
foreach ($students as $student) {
$studentId = $student['user_id'];
$studentInfo = api_get_user_info($studentId);
$courseCode = $courseInfo['code'];
$lastConnection = Tracking::getLastConnectionTimeInSessionCourseLp($studentId, $courseCode, $sessionId);
$lastConnectionFormatted = $lastConnection ? date('Y-m-d', $lastConnection) : '';
$averageScore = round(Tracking::getAverageStudentScore($studentId, $courseCode, [], $sessionId));
$averageProgress = round(Tracking::get_avg_student_progress($studentId, $courseCode, [], $sessionId));
$exportData[] = [
$courseInfo['name'],
$session['name'],
$studentInfo['lastname'],
$studentInfo['firstname'],
$studentInfo['mail'],
$lastConnectionFormatted,
$averageScore,
$averageProgress,
];
}
}
Export::arrayToXls($exportData, 'session_report');
}
}

@ -4573,6 +4573,43 @@ class Tracking
return $lastTime;
}
/**
* Gets the last connection time in the last learning path for a student in a course session.
*/
public static function getLastConnectionTimeInSessionCourseLp(
int $studentId,
string $courseCode,
int $sessionId = 0
): int {
$course = api_get_course_info($courseCode);
if (empty($course)) {
return 0;
}
$courseId = $course['real_id'];
$lastTime = 0;
$tLpv = Database::get_course_table(TABLE_LP_VIEW);
$tLpiv = Database::get_course_table(TABLE_LP_ITEM_VIEW);
$sql = 'SELECT MAX(item_view.start_time) as last_time
FROM '.$tLpiv.' AS item_view
INNER JOIN '.$tLpv.' AS view
ON (item_view.lp_view_id = view.id)
WHERE
view.c_id = '.$courseId.' AND
view.user_id = '.$studentId.' AND
view.session_id = '.$sessionId;
$rs = Database::query($sql);
if ($rs && Database::num_rows($rs) > 0) {
$lastTime = (int) Database::result($rs, 0, 'last_time');
}
return $lastTime;
}
public static function getFirstConnectionTimeInLp(
$student_id,
$course_code,

Loading…
Cancel
Save