SequenceResourceManager.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. *
  15. * @param array $sequences The sequences
  16. * @param int $type The type of sequence resource
  17. * @param int $userId Optional. The user ID
  18. *
  19. * @return array
  20. */
  21. public static function checkRequirementsForUser(array $sequences, $type, $userId = 0)
  22. {
  23. $sequenceList = [];
  24. switch ($type) {
  25. case SequenceResource::SESSION_TYPE:
  26. $sequenceList = self::checkSessionRequirementsForUser($sequences, $userId);
  27. break;
  28. }
  29. return $sequenceList;
  30. }
  31. /**
  32. * Check if at least one sequence are completed.
  33. *
  34. * @param array $sequences The sequences
  35. *
  36. * @return bool
  37. */
  38. public static function checkSequenceAreCompleted(array $sequences)
  39. {
  40. foreach ($sequences as $sequence) {
  41. $status = true;
  42. foreach ($sequence['requirements'] as $item) {
  43. $status = $status && $item['status'];
  44. }
  45. if ($status) {
  46. return true;
  47. }
  48. }
  49. return false;
  50. }
  51. /**
  52. * Check if the ser has completed the requirements for the session sequences.
  53. *
  54. * @param array $sequences The sequences
  55. * @param int $userId Optional. The user ID
  56. *
  57. * @return array
  58. */
  59. private static function checkSessionRequirementsForUser(array $sequences, $userId = 0)
  60. {
  61. $sequenceList = [];
  62. $entityManager = Database::getManager();
  63. $gradebookCategoryRepo = $entityManager->getRepository('ChamiloCoreBundle:GradebookCategory');
  64. foreach ($sequences as $sequenceId => $sequence) {
  65. $item = [
  66. 'name' => $sequence['name'],
  67. 'requirements' => [],
  68. ];
  69. foreach ($sequence['requirements'] as $sessionRequired) {
  70. $itemSession = [
  71. 'name' => $sessionRequired->getName(),
  72. 'status' => true,
  73. ];
  74. $sessionsCourses = $sessionRequired->getCourses();
  75. foreach ($sessionsCourses as $sessionCourse) {
  76. $course = $sessionCourse->getCourse();
  77. $gradebooks = $gradebookCategoryRepo->findBy([
  78. 'courseCode' => $course->getCode(),
  79. 'sessionId' => $sessionRequired->getId(),
  80. 'isRequirement' => true,
  81. ]);
  82. foreach ($gradebooks as $gradebook) {
  83. $category = Category::createCategoryObjectFromEntity(
  84. $gradebook
  85. );
  86. if (!empty($userId)) {
  87. $itemSession['status'] = $itemSession['status'] && Category::userFinishedCourse(
  88. $userId,
  89. $category
  90. );
  91. }
  92. }
  93. }
  94. $item['requirements'][$sessionRequired->getId()] = $itemSession;
  95. }
  96. $sequenceList[$sequenceId] = $item;
  97. }
  98. return $sequenceList;
  99. }
  100. }