123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233 |
- <?php
- /* For licensing terms, see /license.txt */
- /**
- * GamificationUtils class
- * Functions to manage the gamification mode.
- *
- * @package chamilo.library
- *
- * @author Angel Fernando Quiroz Campos <angel.quiroz@beeznest.com>
- */
- class GamificationUtils
- {
- /**
- * Get the calculated points on session with gamification mode.
- *
- * @param int $userId The user ID
- * @param int $userStatus The user Status
- *
- * @return float
- */
- public static function getTotalUserPoints($userId, $userStatus)
- {
- $points = 0;
- $sessions = SessionManager::getSessionsFollowedByUser(
- $userId,
- $userStatus
- );
- if (empty($sessions)) {
- return 0;
- }
- foreach ($sessions as $session) {
- $points += self::getSessionPoints($session['id'], $userId);
- }
- return round($points / count($sessions), 2);
- }
- /**
- * Get the achieved points for an user in a session.
- *
- * @param int $sessionId The session ID
- * @param int $userId The user ID
- *
- * @return int The count of points
- */
- public static function getSessionPoints($sessionId, $userId)
- {
- $totalPoints = 0;
- $courses = SessionManager::get_course_list_by_session_id($sessionId);
- if (empty($courses)) {
- return 0;
- }
- foreach ($courses as $course) {
- $learnPathListObject = new LearnpathList(
- $userId,
- api_get_course_info($course['code']),
- $sessionId
- );
- $learnPaths = $learnPathListObject->get_flat_list();
- if (empty($learnPaths)) {
- continue;
- }
- $score = 0;
- foreach ($learnPaths as $learnPathId => $learnPathInfo) {
- if (empty($learnPathInfo['seriousgame_mode'])) {
- continue;
- }
- $learnPath = new learnpath(
- $course['code'],
- $learnPathId,
- $userId
- );
- $score += $learnPath->getCalculateScore($sessionId);
- }
- $totalPoints += round($score / count($learnPaths), 2);
- }
- return round($totalPoints / count($courses), 2);
- }
- /**
- * Get the calculated progress for an user in a session.
- *
- * @param int $sessionId The session ID
- * @param int $userId The user ID
- *
- * @return float The progress
- */
- public static function getSessionProgress($sessionId, $userId)
- {
- $courses = SessionManager::get_course_list_by_session_id($sessionId);
- $progress = 0;
- if (empty($courses)) {
- return 0;
- }
- foreach ($courses as $course) {
- $courseProgress = Tracking::get_avg_student_progress(
- $userId,
- $course['code'],
- [],
- $sessionId,
- false,
- true
- );
- if ($courseProgress === false) {
- continue;
- }
- $progress += $courseProgress;
- }
- return round($progress / count($courses), 2);
- }
- /**
- * Get the number of stars achieved for an user in a session.
- *
- * @param int $sessionId The session ID
- * @param int $userId The user ID
- *
- * @return int The number of stars
- */
- public static function getSessionStars($sessionId, $userId)
- {
- $totalStars = 0;
- $courses = SessionManager::get_course_list_by_session_id($sessionId);
- if (empty($courses)) {
- return 0;
- }
- foreach ($courses as $course) {
- $learnPathListObject = new LearnpathList(
- $userId,
- api_get_course_info($course['code']),
- $sessionId
- );
- $learnPaths = $learnPathListObject->get_flat_list();
- if (empty($learnPaths)) {
- continue;
- }
- $stars = 0;
- foreach ($learnPaths as $learnPathId => $learnPathInfo) {
- if (empty($learnPathInfo['seriousgame_mode'])) {
- continue;
- }
- $learnPath = new learnpath(
- $course['code'],
- $learnPathId,
- $userId
- );
- $stars += $learnPath->getCalculateStars($sessionId);
- }
- $totalStars += round($stars / count($learnPaths));
- }
- return round($totalStars / count($courses));
- }
- /**
- * Get the stars on sessions with gamification mode.
- *
- * @param int $userId The user ID
- * @param int $userStatus The user Status
- *
- * @return int
- */
- public static function getTotalUserStars($userId, $userStatus)
- {
- $stars = 0;
- $sessions = SessionManager::getSessionsFollowedByUser(
- $userId,
- $userStatus
- );
- if (empty($sessions)) {
- return 0;
- }
- foreach ($sessions as $session) {
- $stars += self::getSessionStars($session['id'], $userId);
- }
- return round($stars / count($sessions));
- }
- /**
- * Get the total progress on sessions with gamification mode.
- *
- * @param int $userId The user ID
- * @param int $userStatus The user Status
- *
- * @return float
- */
- public static function getTotalUserProgress($userId, $userStatus)
- {
- $progress = 0;
- $sessions = SessionManager::getSessionsFollowedByUser(
- $userId,
- $userStatus
- );
- if (empty($sessions)) {
- return 0;
- }
- foreach ($sessions as $session) {
- $progress += self::getSessionProgress($session['id'], $userId);
- }
- return round($progress / count($sessions), 2);
- }
- }
|