GamificationUtils.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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 float
  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 round($points / count($sessions), 2);
  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. if (empty($learnPaths)) {
  53. continue;
  54. }
  55. $score = 0;
  56. foreach ($learnPaths as $learnPathId => $learnPathInfo) {
  57. if (empty($learnPathInfo['seriousgame_mode'])) {
  58. continue;
  59. }
  60. $learnPath = new learnpath(
  61. $course['code'],
  62. $learnPathId,
  63. $userId
  64. );
  65. $score += $learnPath->getCalculateScore($sessionId);
  66. }
  67. $totalPoints += round($score / count($learnPaths), 2);
  68. }
  69. return round($totalPoints / count($courses), 2);
  70. }
  71. /**
  72. * Get the calculated progress for an user in a session
  73. * @param int $sessionId The session ID
  74. * @param int $userId The user ID
  75. * @return float The progress
  76. */
  77. public static function getSessionProgress($sessionId, $userId)
  78. {
  79. $courses = SessionManager::get_course_list_by_session_id($sessionId);
  80. $progress = 0;
  81. if (empty($courses)) {
  82. return 0;
  83. }
  84. foreach ($courses as $course) {
  85. $courseProgress = Tracking::get_avg_student_progress(
  86. $userId,
  87. $course['code'],
  88. [],
  89. $sessionId,
  90. false,
  91. true
  92. );
  93. if ($courseProgress === false) {
  94. continue;
  95. }
  96. $progress += $courseProgress;
  97. }
  98. return round($progress / count($courses), 2);
  99. }
  100. /**
  101. * Get the number of stars achieved for an user in a session
  102. * @param int $sessionId The session ID
  103. * @param int $userId The user ID
  104. * @return int The number of stars
  105. */
  106. public static function getSessionStars($sessionId, $userId)
  107. {
  108. $totalStars = 0;
  109. $courses = SessionManager::get_course_list_by_session_id($sessionId);
  110. if (empty($courses)) {
  111. return 0;
  112. }
  113. foreach ($courses as $course) {
  114. $learnPathListObject = new LearnpathList(
  115. $userId,
  116. $course['code'],
  117. $sessionId
  118. );
  119. $learnPaths = $learnPathListObject->get_flat_list();
  120. if (empty($learnPaths)) {
  121. continue;
  122. }
  123. $stars = 0;
  124. foreach ($learnPaths as $learnPathId => $learnPathInfo) {
  125. if (empty($learnPathInfo['seriousgame_mode'])) {
  126. continue;
  127. }
  128. $learnPath = new learnpath(
  129. $course['code'],
  130. $learnPathId,
  131. $userId
  132. );
  133. $stars += $learnPath->getCalculateStars($sessionId);
  134. }
  135. $totalStars += round($stars / count($learnPaths));
  136. }
  137. return round($totalStars / count($courses));
  138. }
  139. /**
  140. * Get the stars on sessions with gamification mode
  141. * @param int $userId The user ID
  142. * @param int $userStatus The user Status
  143. * @return int
  144. */
  145. public static function getTotalUserStars($userId, $userStatus)
  146. {
  147. $stars = 0;
  148. $sessions = SessionManager::getSessionsFollowedByUser(
  149. $userId,
  150. $userStatus
  151. );
  152. if (empty($sessions)) {
  153. return 0;
  154. }
  155. foreach ($sessions as $session) {
  156. $stars += self::getSessionStars($session['id'], $userId);
  157. }
  158. return round($stars / count($sessions));
  159. }
  160. /**
  161. * Get the total progress on sessions with gamification mode
  162. * @param int $userId The user ID
  163. * @param int $userStatus The user Status
  164. * @return float
  165. */
  166. public static function getTotalUserProgress($userId, $userStatus)
  167. {
  168. $progress = 0;
  169. $sessions = SessionManager::getSessionsFollowedByUser(
  170. $userId,
  171. $userStatus
  172. );
  173. if (empty($sessions)) {
  174. return 0;
  175. }
  176. foreach ($sessions as $session) {
  177. $progress += self::getSessionProgress($session['id'], $userId);
  178. }
  179. return round($progress / count($sessions), 2);
  180. }
  181. }