GamificationUtils.php 5.8 KB

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