SequenceResourceManager.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. use \Chamilo\CoreBundle\Entity\SequenceResource;
  4. /**
  5. * SequenceResourceManager class
  6. * Helper for SequenceResource
  7. *
  8. * @author Angel Fernando Quiroz Campos <angel.quiroz@beeznest.com>
  9. */
  10. class SequenceResourceManager
  11. {
  12. /**
  13. * Check if the ser has completed the requirements for the sequences
  14. * @param array $sequences The sequences
  15. * @param int $type The type of sequence resource
  16. * @param int $userId Optional. The user ID
  17. *
  18. * @return array
  19. */
  20. public static function checkRequirementsForUser(array $sequences, $type, $userId = 0)
  21. {
  22. $sequenceList = [];
  23. switch ($type) {
  24. case SequenceResource::SESSION_TYPE:
  25. $sequenceList = self::checkSessionRequirementsForUser($sequences, $userId);
  26. break;
  27. }
  28. return $sequenceList;
  29. }
  30. /**
  31. * Check if the ser has completed the requirements for the session sequences
  32. * @param array $sequences The sequences
  33. * @param int $userId Optional. The user ID
  34. *
  35. * @return array
  36. */
  37. private static function checkSessionRequirementsForUser(array $sequences, $userId = 0)
  38. {
  39. $sequenceList = [];
  40. $entityManager = Database::getManager();
  41. $gradebookCategoryRepo = $entityManager->getRepository(
  42. 'ChamiloCoreBundle:GradebookCategory'
  43. );
  44. foreach ($sequences as $sequenceId => $sequence) {
  45. $item = [
  46. 'name' => $sequence['name'],
  47. 'requirements' => []
  48. ];
  49. foreach ($sequence['requirements'] as $sessionRequired) {
  50. $itemSession = [
  51. 'name' => $sessionRequired->getName(),
  52. 'status' => true
  53. ];
  54. $sessionsCourses = $sessionRequired->getCourses();
  55. foreach ($sessionsCourses as $sessionCourse) {
  56. $course = $sessionCourse->getCourse();
  57. $gradebooks = $gradebookCategoryRepo->findBy([
  58. 'courseCode' => $course->getCode(),
  59. 'sessionId' => $sessionRequired->getId(),
  60. 'isRequirement' => true
  61. ]);
  62. foreach ($gradebooks as $gradebook) {
  63. $category = Category::createCategoryObjectFromEntity(
  64. $gradebook
  65. );
  66. if (!empty($userId)) {
  67. $itemSession['status'] = $itemSession['status'] && Category::userFinishedCourse(
  68. $userId,
  69. $category,
  70. null,
  71. $course->getCode(),
  72. $sessionRequired->getId()
  73. );
  74. }
  75. }
  76. }
  77. $item['requirements'][$sessionRequired->getId()] = $itemSession;
  78. }
  79. $sequenceList[$sequenceId] = $item;
  80. }
  81. return $sequenceList;
  82. }
  83. /**
  84. * Check if at least one sequence are completed
  85. * @param array $sequences The sequences
  86. *
  87. * @return boolean
  88. */
  89. public static function checkSequenceAreCompleted(array $sequences)
  90. {
  91. foreach ($sequences as $sequence) {
  92. $status = true;
  93. foreach ($sequence['requirements'] as $item) {
  94. $status = $status && $item['status'];
  95. }
  96. if ($status) {
  97. return true;
  98. }
  99. }
  100. return false;
  101. }
  102. }