GamificationUtils.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * GamificationUtils class
  5. * Functions to manage the gamification mode
  6. * @package chamilo.library
  7. * @author Angel Fernando Quiroz Campos <angel.quiroz@beeznest.com>
  8. */
  9. class GamificationUtils
  10. {
  11. /**
  12. * Get the calculated points on session with gamification mode
  13. * @param int $userId The user ID
  14. * @param int $userStatus The user Status
  15. * @return int
  16. */
  17. public static function getTotalUserPoints($userId, $userStatus)
  18. {
  19. $points = 0;
  20. $sessions = SessionManager::getSessionsFollowedByUser(
  21. $userId,
  22. $userStatus
  23. );
  24. if (empty($sessions)) {
  25. return 0;
  26. }
  27. foreach ($sessions as $session) {
  28. $points += self::getSessionPoints($session['id'], $userId);
  29. }
  30. return $points;
  31. }
  32. /**
  33. * Get the achieved points for an user in a session
  34. * @param int $sessionId The session ID
  35. * @param int $userId The user ID
  36. * @return int The count of points
  37. */
  38. public static function getSessionPoints($sessionId, $userId)
  39. {
  40. $totalPoints = 0;
  41. $courses = SessionManager::get_course_list_by_session_id($sessionId);
  42. if (empty($courses)) {
  43. return 0;
  44. }
  45. foreach ($courses as $course) {
  46. $learnPathListObject = new LearnpathList(
  47. $userId,
  48. $course['code'],
  49. $sessionId
  50. );
  51. $learnPaths = $learnPathListObject->get_flat_list();
  52. $score = 0;
  53. foreach ($learnPaths as $learnPathId => $learnPathInfo) {
  54. if (empty($learnPathInfo['seriousgame_mode'])) {
  55. continue;
  56. }
  57. $learnPath = new learnpath(
  58. $course['code'],
  59. $learnPathId,
  60. $userId
  61. );
  62. $score += $learnPath->getCalculateScore($sessionId);
  63. }
  64. $totalPoints += $score;
  65. }
  66. return $totalPoints / count($courses);
  67. }
  68. /**
  69. * Get the calculated progress for an user in a session
  70. * @param int $sessionId The session ID
  71. * @param int $userId The user ID
  72. * @return float The progress
  73. */
  74. public static function getSessionProgress($sessionId, $userId)
  75. {
  76. $courses = SessionManager::get_course_list_by_session_id($sessionId);
  77. $progress = 0;
  78. if (empty($courses)) {
  79. return 0;
  80. }
  81. foreach ($courses as $course) {
  82. $courseProgress = Tracking::get_avg_student_progress(
  83. $userId,
  84. $course['code'],
  85. [],
  86. $sessionId,
  87. false,
  88. true
  89. );
  90. if ($courseProgress === false) {
  91. continue;
  92. }
  93. $progress += $courseProgress;
  94. }
  95. return $progress / count($courses);
  96. }
  97. }