Skip to content

Commit 1ec72a4

Browse files
committed
Webservice: #add a new get_user_progress_and_time_in_session ws - refs BT#22409
1 parent 0786df7 commit 1ec72a4

File tree

2 files changed

+57
-0
lines changed

2 files changed

+57
-0
lines changed

main/inc/lib/webservices/Rest.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ class Rest extends WebService
104104
public const GET_USER_API_KEY = 'get_user_api_key';
105105
public const GET_USER_LAST_CONNEXION = 'get_user_last_connexion';
106106
public const GET_USER_TOTAL_CONNEXION_TIME = 'get_user_total_connexion_time';
107+
public const GET_USER_PROGRESS_AND_TIME_IN_SESSION = 'get_user_progress_and_time_in_session';
107108
public const GET_USER_SUB_GROUP = 'get_user_sub_group';
108109

109110
public const GET_COURSES = 'get_courses';
@@ -4392,4 +4393,41 @@ private function generateUrl(array $additionalParams = []): string
43924393
return api_get_self().'?'
43934394
.http_build_query(array_merge($queryParams, $additionalParams));
43944395
}
4396+
4397+
/**
4398+
* Returns the progress and time spent by the user in the session
4399+
* @throws Exception
4400+
*/
4401+
public function getUserProgressAndTimeInSession(int $userId, int $sessionId): array
4402+
{
4403+
$totalProgress = 0;
4404+
$totalTime = 0;
4405+
$nbCourses = 0;
4406+
$courses = SessionManager::getCoursesInSession($sessionId);
4407+
foreach ($courses as $courseId) {
4408+
$nbCourses++;
4409+
$totalTime += Tracking::get_time_spent_on_the_course(
4410+
$userId,
4411+
$courseId,
4412+
$sessionId
4413+
);
4414+
$courseInfo = api_get_course_info_by_id($courseId);
4415+
$totalProgress += Tracking::get_avg_student_progress(
4416+
$userId,
4417+
$courseInfo['code'],
4418+
[],
4419+
$sessionId
4420+
);
4421+
}
4422+
$userAverageCoursesTime = 0;
4423+
$userAverageProgress = 0;
4424+
if ($nbCourses != 0) {
4425+
$userAverageCoursesTime = $totalTime/$nbCourses;
4426+
$userAverageProgress = $totalProgress/$nbCourses;
4427+
}
4428+
return [
4429+
'userAverageCoursesTime' => $userAverageCoursesTime,
4430+
'userAverageProgress' => $userAverageProgress,
4431+
];
4432+
}
43954433
}

main/webservices/api/v2.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -680,6 +680,25 @@
680680
)
681681
);
682682
break;
683+
case Rest::GET_USER_PROGRESS_AND_TIME_IN_SESSION:
684+
$userId = (string) $_REQUEST['user_id'];
685+
$sessionId = (string) $_REQUEST['session_id'];
686+
687+
if (empty($userId)) {
688+
throw new Exception('user_id not provided');
689+
}
690+
if (empty($sessionId)) {
691+
throw new Exception('session_id not provided');
692+
}
693+
694+
Event::addEvent(LOG_WS.$action, 'user_id', $userId);
695+
$restResponse->setData(
696+
$restApi->getUserProgressAndTimeInSession(
697+
$userId,
698+
$sessionId
699+
)
700+
);
701+
break;
683702
case Rest::GET_USER_SUB_GROUP:
684703
$userId = isset($_POST['user_id']) ? (int) $_POST['user_id'] : 0;
685704
if (empty($userId)) {

0 commit comments

Comments
 (0)