public function UserStatisticsManager::getUserTrainingsTime in Opigno statistics 3.x
Get the time that user spent on trainings in the given amount of days.
Parameters
int $days: The amount of days to get statistics for. If 0 given, statistics will be calculated without time limitation.
int $uid: The ID of user to get statistics for. By default will be calculated for the current user.
Return value
int The time (in seconds) that user spent on trainings in the given amount of days.
1 call to UserStatisticsManager::getUserTrainingsTime()
- UserStatisticsManager::renderUserStatistics in src/
Services/ UserStatisticsManager.php - Prepare the render array to display the user statistics.
File
- src/
Services/ UserStatisticsManager.php, line 299
Class
- UserStatisticsManager
- User statistics manager service definition.
Namespace
Drupal\opigno_statistics\ServicesCode
public function getUserTrainingsTime(int $days = 0, int $uid = 0) : int {
$time = 0;
$uid = $uid ?: $this->currentUid;
if (!$uid || !$this->userModuleStatusStorage instanceof EntityStorageInterface) {
return $time;
}
// Get IDs of user module status entities.
$query = $this->userModuleStatusStorage
->getQuery()
->condition('user_id', $uid)
->condition('finished', 0, '>');
// Add extra condition if time limit is set.
if ($days) {
$timestamp = strtotime("tomorrow -{$days} days");
$query
->condition('started', $timestamp, '>=');
}
$ids = $query
->execute();
if (!$ids) {
return $time;
}
// Load entities and calculate the general time that user spent on modules
// completion.
$entities = $this->userModuleStatusStorage
->loadMultiple($ids);
if (!$entities) {
return $time;
}
$time = 0;
foreach ($entities as $entity) {
if ($entity instanceof UserModuleStatusInterface) {
$time += $entity
->getCompletionTime();
}
}
return $time;
}