GamificationUtils.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * GamificationUtils class
  5. * Functions to manage the gamification mode.
  6. *
  7. * @package chamilo.library
  8. *
  9. * @author Angel Fernando Quiroz Campos <angel.quiroz@beeznest.com>
  10. */
  11. class GamificationUtils
  12. {
  13. /**
  14. * Get the calculated points on session with gamification mode.
  15. *
  16. * @param int $userId The user ID
  17. * @param int $userStatus The user Status
  18. *
  19. * @return float
  20. */
  21. public static function getTotalUserPoints($userId, $userStatus)
  22. {
  23. $points = 0;
  24. $sessions = SessionManager::getSessionsFollowedByUser(
  25. $userId,
  26. $userStatus
  27. );
  28. if (empty($sessions)) {
  29. return 0;
  30. }
  31. foreach ($sessions as $session) {
  32. $points += self::getSessionPoints($session['id'], $userId);
  33. }
  34. return round($points / count($sessions), 2);
  35. }
  36. /**
  37. * Get the achieved points for an user in a session.
  38. *
  39. * @param int $sessionId The session ID
  40. * @param int $userId The user ID
  41. *
  42. * @return int The count of points
  43. */
  44. public static function getSessionPoints($sessionId, $userId)
  45. {
  46. $totalPoints = 0;
  47. $courses = SessionManager::get_course_list_by_session_id($sessionId);
  48. if (empty($courses)) {
  49. return 0;
  50. }
  51. foreach ($courses as $course) {
  52. $learnPathListObject = new LearnpathList(
  53. $userId,
  54. $course['code'],
  55. $sessionId
  56. );
  57. $learnPaths = $learnPathListObject->get_flat_list();
  58. if (empty($learnPaths)) {
  59. continue;
  60. }
  61. $score = 0;
  62. foreach ($learnPaths as $learnPathId => $learnPathInfo) {
  63. if (empty($learnPathInfo['seriousgame_mode'])) {
  64. continue;
  65. }
  66. $learnPath = new learnpath(
  67. $course['code'],
  68. $learnPathId,
  69. $userId
  70. );
  71. $score += $learnPath->getCalculateScore($sessionId);
  72. }
  73. $totalPoints += round($score / count($learnPaths), 2);
  74. }
  75. return round($totalPoints / count($courses), 2);
  76. }
  77. /**
  78. * Get the calculated progress for an user in a session.
  79. *
  80. * @param int $sessionId The session ID
  81. * @param int $userId The user ID
  82. *
  83. * @return float The progress
  84. */
  85. public static function getSessionProgress($sessionId, $userId)
  86. {
  87. $courses = SessionManager::get_course_list_by_session_id($sessionId);
  88. $progress = 0;
  89. if (empty($courses)) {
  90. return 0;
  91. }
  92. foreach ($courses as $course) {
  93. $courseProgress = Tracking::get_avg_student_progress(
  94. $userId,
  95. $course['code'],
  96. [],
  97. $sessionId,
  98. false,
  99. true
  100. );
  101. if ($courseProgress === false) {
  102. continue;
  103. }
  104. $progress += $courseProgress;
  105. }
  106. return round($progress / count($courses), 2);
  107. }
  108. /**
  109. * Get the number of stars achieved for an user in a session.
  110. *
  111. * @param int $sessionId The session ID
  112. * @param int $userId The user ID
  113. *
  114. * @return int The number of stars
  115. */
  116. public static function getSessionStars($sessionId, $userId)
  117. {
  118. $totalStars = 0;
  119. $courses = SessionManager::get_course_list_by_session_id($sessionId);
  120. if (empty($courses)) {
  121. return 0;
  122. }
  123. foreach ($courses as $course) {
  124. $learnPathListObject = new LearnpathList(
  125. $userId,
  126. $course['code'],
  127. $sessionId
  128. );
  129. $learnPaths = $learnPathListObject->get_flat_list();
  130. if (empty($learnPaths)) {
  131. continue;
  132. }
  133. $stars = 0;
  134. foreach ($learnPaths as $learnPathId => $learnPathInfo) {
  135. if (empty($learnPathInfo['seriousgame_mode'])) {
  136. continue;
  137. }
  138. $learnPath = new learnpath(
  139. $course['code'],
  140. $learnPathId,
  141. $userId
  142. );
  143. $stars += $learnPath->getCalculateStars($sessionId);
  144. }
  145. $totalStars += round($stars / count($learnPaths));
  146. }
  147. return round($totalStars / count($courses));
  148. }
  149. /**
  150. * Get the stars on sessions with gamification mode.
  151. *
  152. * @param int $userId The user ID
  153. * @param int $userStatus The user Status
  154. *
  155. * @return int
  156. */
  157. public static function getTotalUserStars($userId, $userStatus)
  158. {
  159. $stars = 0;
  160. $sessions = SessionManager::getSessionsFollowedByUser(
  161. $userId,
  162. $userStatus
  163. );
  164. if (empty($sessions)) {
  165. return 0;
  166. }
  167. foreach ($sessions as $session) {
  168. $stars += self::getSessionStars($session['id'], $userId);
  169. }
  170. return round($stars / count($sessions));
  171. }
  172. /**
  173. * Get the total progress on sessions with gamification mode.
  174. *
  175. * @param int $userId The user ID
  176. * @param int $userStatus The user Status
  177. *
  178. * @return float
  179. */
  180. public static function getTotalUserProgress($userId, $userStatus)
  181. {
  182. $progress = 0;
  183. $sessions = SessionManager::getSessionsFollowedByUser(
  184. $userId,
  185. $userStatus
  186. );
  187. if (empty($sessions)) {
  188. return 0;
  189. }
  190. foreach ($sessions as $session) {
  191. $progress += self::getSessionProgress($session['id'], $userId);
  192. }
  193. return round($progress / count($sessions), 2);
  194. }
  195. }