Skip to content

Commit

Permalink
User Statistics - Reports: Calculate learning path duration separately.
Browse files Browse the repository at this point in the history
  • Loading branch information
jexi committed Dec 11, 2024
1 parent 40153ad commit 6d4b20a
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 24 deletions.
7 changes: 5 additions & 2 deletions include/lib/learnPathLib.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -591,8 +591,11 @@ function get_learnPath_progress($lpid, $lpUid) {
return calculate_learnPath_progress($lpid, $modules);
}

function get_learnPath_progress_details($lpid, $lpUid, $total=true, $from_date = null): array {
global $course_id;
function get_learnPath_progress_details($lpid, $lpUid, $total=true, $from_date = null, $course_id = null): array {

if ($course_id == null) {
$course_id = $GLOBALS['course_id'];
}

// find progression for this user in each module of the path
$sql = "SELECT UMP.`raw` AS R, UMP.`scoreMax` AS SMax, M.`contentType` AS CTYPE, UMP.`lesson_status` AS STATUS, UMP.`total_time`,
Expand Down
12 changes: 12 additions & 0 deletions include/main_lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -1584,6 +1584,18 @@ function format_time_duration($sec, $hourLimit = 24, $display_days = true) {
}
}


/**
* @brief: convert a 'HH:MM:SS' string to seconds
* (https://stackoverflow.com/questions/4834202/convert-time-in-hhmmss-format-to-seconds-only)
* @param string $time
* @return int
*/
function timeToSeconds(string $time): int {
return array_reduce(explode(':', $time), fn ($x, $i) => $x * 60 + $i, 0);
}


// Move entry $id in $table to $direction 'up' or 'down', where
// order is in field $order_field and id in $id_field
// Use $condition as extra SQL to limit the operation
Expand Down
35 changes: 29 additions & 6 deletions modules/usage/usage.lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -685,9 +685,6 @@ function count_course_users($cid, $user_type = null){
/**
* Return module title based on the course module id
* @param int mid the id of the module
* @global $modules,
* @global $admin_modules,
* @global static_modules,
* @return string the title of the course module
*/
function which_module($mid){
Expand Down Expand Up @@ -760,6 +757,30 @@ function course_visits($cid) {
return $logins;
}

/**
* @brief get learning path statistics for user_id=$uid and course_id=$cid
* @param $cid
* @param $uid
* @return string
*/
function get_lp_stats($cid, $uid) {

require_once 'include/lib/learnPathLib.inc.php';

$learningPathList = Database::get()->queryArray("SELECT learnPath_id FROM lp_learnPath WHERE course_id = ?d", $cid);
$time = "00:00:00";

foreach ($learningPathList as $learningPath) {
$data = get_learnPath_progress_details($learningPath->learnPath_id, $uid, true, null, $cid);
$lpTotalTime = $data[1];
if (!empty($lpTotalTime)) {
$time = addScormTime($time, $lpTotalTime);
}
}
return $time;
}


/**
* @brief Transform seconds to h:mm:ss
* @param int $seconds the number of seconds to be shown properly
Expand Down Expand Up @@ -855,17 +876,19 @@ function user_duration_per_course($u) {
ON actions_daily.user_id = course_user.user_id
AND actions_daily.course_id = course_user.course_id
AND actions_daily.module_id != " . MODULE_ID_TC . "
AND actions_daily.module_id != " . MODULE_ID_LP . "
WHERE course_user.user_id = ?d
AND course.visible != " . COURSE_INACTIVE . "
GROUP BY course.id
ORDER BY duration DESC", $u);
if (count($result) > 0) { // found courses ?
foreach ($result as $item) {
$totalDuration += $item->duration;
$lp_duration = timeToSeconds(get_lp_stats(course_code_to_id($item->code), $u)); // learning path duration
$totalDuration += $item->duration + $lp_duration;
$duration[$item->code] = $item->duration;
}

$totalDuration = format_time_duration(0 + $totalDuration, 240);
$totalDuration = format_time_duration(0 + $totalDuration, 24);

$tool_content .= "<div class='text-center mb-4'>
<div class='alert alert-info'><i class='fa-solid fa-circle-info fa-lg'></i><span>$langInfoUserDuration</span></div>
Expand All @@ -885,7 +908,7 @@ function user_duration_per_course($u) {
$tool_content .= "
<li class='list-group-item element d-flex justify-content-between align-items-center gap-3 flex-wrap'>
<div>" . q(course_code_to_title($code)) . "</div>
<div>" . format_time_duration(0 + $time) . "</div>
<div>" . format_time_duration(0 + $time + timeToSeconds(get_lp_stats(course_code_to_id($code), $u))) . "</div>
</li>";
}
$tool_content .= "
Expand Down
61 changes: 45 additions & 16 deletions modules/usage/userduration.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,14 @@
}

$user_actions = Database::get()->queryArray("SELECT
SUM(ABS(actions_daily.duration)) AS duration,
module_id
FROM actions_daily
WHERE course_id = ?d
AND user_id = ?d
AND module_id != " . MODULE_ID_TC . "
GROUP BY module_id", $course_id, $_GET['u']);

SUM(ABS(actions_daily.duration)) AS duration,
module_id
FROM actions_daily
WHERE course_id = ?d
AND user_id = ?d
AND module_id != " . MODULE_ID_TC . "
AND module_id != " . MODULE_ID_LP . "
GROUP BY module_id", $course_id, $_GET['u']);

if (isset($_GET['format']) and $_GET['format'] == 'xls') { // xls output

Expand Down Expand Up @@ -92,6 +92,13 @@
$dur = format_time_duration(0 + $ua->duration, 24, false);
$data[] = [ $mod, $dur ];
}
// learning path duration
$lp_time = timeToSeconds(get_lp_stats($course_id, $_GET['u']));
if ($lp_time > 0) {
$mod = which_module(MODULE_ID_LP);
$dur = format_time_duration($lp_time, 24, false);
$data[] = [ $mod, $dur ];
}

$sheet->mergeCells("A1:B1");
$sheet->getCell('A1')->getStyle()->getFont()->setItalic(true);
Expand Down Expand Up @@ -173,6 +180,15 @@
$tool_content .= "<td>" . format_time_duration(0 + $ua->duration, 24, false) . "</td>";
$tool_content .= "</tr>";
}

// learning path duration
$lp_time = timeToSeconds(get_lp_stats($course_id, $_GET['u']));
if ($lp_time > 0) {
$tool_content .= "<tr>";
$tool_content .= "<td>" . which_module(MODULE_ID_LP) . "</td>";
$tool_content .= "<td>" . format_time_duration($lp_time, 24, false) . "</td>";
$tool_content .= "</tr>";
}
$tool_content .= "</table></div></div>";

// user last logins
Expand Down Expand Up @@ -204,14 +220,16 @@
draw($tool_content, 2);
}
} else if ($is_course_reviewer and isset($_GET['m']) and $_GET['m'] != -1) { // stats per module

$module = $_GET['m'];
$user_actions = Database::get()->queryArray("SELECT
SUM(actions_daily.duration) AS duration, user_id,
module_id
FROM actions_daily
WHERE course_id = ?d
AND module_id = ?d
GROUP BY user_id", $course_id, $module);
SUM(actions_daily.duration) AS duration, user_id,
module_id
FROM actions_daily
WHERE course_id = ?d
AND module_id = ?d
GROUP BY user_id", $course_id, $module);



if (isset($_GET['format']) and $_GET['format'] == 'xls') { // xls output
Expand All @@ -229,7 +247,12 @@
$grp_name = user_groups($course_id, $um->user_id, false);
$user_am = uid_to_am($um->user_id);
$user_details = uid_to_name($um->user_id);
$data[] = [ $user_details, $grp_name, $user_am, format_time_duration(0 + $um->duration, 24, false) ];
if ($module == MODULE_ID_LP) {
$lp_time = timeToSeconds(get_lp_stats($course_id, $um->user_id));
$data[] = [$user_details, $grp_name, $user_am, format_time_duration(0 + $lp_time, 24, false)];
} else {
$data[] = [$user_details, $grp_name, $user_am, format_time_duration(0 + $um->duration, 24, false)];
}
}

$sheet->getCell('A1')->getStyle()->getFont()->setItalic(true);
Expand Down Expand Up @@ -294,7 +317,13 @@
}
$tool_content .= "<td>" . $grp_name . "</td>";
$tool_content .= "<td>" . $user_am . "</td>";
$tool_content .= "<td>" . format_time_duration(0 + $um->duration, 24, false) . "</td>";

if ($module == MODULE_ID_LP) {
$lp_time = timeToSeconds(get_lp_stats($course_id, $um->user_id));
$tool_content .= "<td>" . format_time_duration(0 + $lp_time, 24, false) . "</td>";
} else {
$tool_content .= "<td>" . format_time_duration(0 + $um->duration, 24, false) . "</td>";
}
$tool_content .= "</tr>";
}
$tool_content .= "</table></div></div>";
Expand Down

0 comments on commit 6d4b20a

Please sign in to comment.