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