ItemPropertyRepository.php 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. <?php
  2. namespace Entity\Repository;
  3. use Doctrine\ORM\EntityRepository;
  4. /**
  5. * ItemPropertyRepository
  6. *
  7. * This class was generated by the Doctrine ORM. Add your own custom
  8. * repository methods below.
  9. */
  10. class ItemPropertyRepository extends EntityRepository
  11. {
  12. /**
  13. *
  14. * Get users subscribed to a item LP, Document, etc (item_property)
  15. *
  16. * @param $tool learnpath | document | etc
  17. * @param $itemId
  18. * @param \Entity\Course $course
  19. * @param int $sessionId
  20. * @param int $groupId
  21. *
  22. * @return \Doctrine\ORM\QueryBuilder
  23. */
  24. public function getUsersSubscribedToItem($tool, $itemId, \Entity\Course $course, \Entity\Session $session = null, \Entity\Group $group = null)
  25. {
  26. $criteria = array(
  27. 'tool' => $tool,
  28. 'lasteditType' => 'LearnpathSubscription',
  29. 'ref' => $itemId,
  30. 'course' => $course,
  31. 'session' => $session,
  32. 'group' => $group
  33. );
  34. return $this->findBy($criteria);
  35. /*
  36. $qb = $this->createQueryBuilder('i')
  37. ->select('i');
  38. $wherePart = $qb->expr()->andx();
  39. //Selecting courses for users
  40. $qb->innerJoin('i.user', 'u');
  41. $wherePart->add($qb->expr()->eq('i.tool', $qb->expr()->literal($tool)));
  42. $wherePart->add($qb->expr()->eq('i.lasteditType', $qb->expr()->literal('LearnpathSubscription')));
  43. $wherePart->add($qb->expr()->eq('i.ref', $itemId));
  44. $wherePart->add($qb->expr()->eq('i.cId', $course->getId()));
  45. $wherePart->add($qb->expr()->eq('i.idSession', $sessionId));
  46. $wherePart->add($qb->expr()->eq('i.toGroupId', $groupId));
  47. $qb->where($wherePart);
  48. $q = $qb->getQuery();
  49. //var_dump($q->getSQL());
  50. return $q->execute();*/
  51. }
  52. /**
  53. * Get Groups subscribed to a item: LP, Doc, etc
  54. * @param $tool learnpath | document | etc
  55. * @param $itemId
  56. * @param \Entity\Course $course
  57. * @param \Entity\Session $session
  58. * @return array
  59. */
  60. public function getGroupsSubscribedToItem($tool, $itemId, \Entity\Course $course, \Entity\Session $session = null)
  61. {
  62. $criteria = array(
  63. 'tool' => $tool,
  64. 'lasteditType' => 'LearnpathSubscription',
  65. 'ref' => $itemId,
  66. 'course' => $course,
  67. 'session' => $session,
  68. 'toUserId' => null,
  69. );
  70. return $this->findBy($criteria);
  71. }
  72. /**
  73. * Subscribe groups to a LP, doc (itemproperty)
  74. * @param $tool learnpath | document | etc
  75. * @param \Entity\Course $course
  76. * @param \Entity\Session $session
  77. * @param $itemId
  78. * @param array $newList
  79. */
  80. public function subscribeGroupsToItem($tool, \Entity\Course $course, \Entity\Session $session = null, $itemId, $newList = array())
  81. {
  82. $em = $this->getEntityManager();
  83. $groupsSubscribedToItem = $this->getGroupsSubscribedToItem($tool, $itemId, $course, $session);
  84. $alreadyAdded = array();
  85. if ($groupsSubscribedToItem) {
  86. foreach ($groupsSubscribedToItem as $itemProperty) {
  87. $alreadyAdded[] = $itemProperty->getToGroupId();
  88. }
  89. }
  90. $toDelete = $alreadyAdded;
  91. if (!empty($newList)) {
  92. $toDelete = array_diff($alreadyAdded, $newList);
  93. }
  94. if ($toDelete) {
  95. $this->unsubscribeGroupsToItem($tool, $course, $session, $itemId, $toDelete, true);
  96. }
  97. foreach ($newList as $groupId) {
  98. if (!in_array($groupId, $alreadyAdded)) {
  99. $item = new \Entity\CItemProperty($course);
  100. $groupObj = $em->find('Entity\CGroupInfo', $groupId);
  101. $item->setGroup($groupObj);
  102. $item->setTool($tool);
  103. $item->setRef($itemId);
  104. if (!empty($session)) {
  105. $item->setSession($session);
  106. }
  107. $item->setLasteditType('LearnpathSubscription');
  108. $item->setVisibility('1');
  109. $em->persist($item); //$em is an instance of EntityManager
  110. }
  111. //Adding users from this group to the item
  112. $users = \GroupManager::get_members_and_tutors($groupId);
  113. $newUserList = array();
  114. if (!empty($users)) {
  115. foreach ($users as $user) {
  116. $newUserList[] = $user['user_id'];
  117. }
  118. $this->subscribeUsersToItem(
  119. 'learnpath',
  120. $course,
  121. $session,
  122. $itemId,
  123. $newUserList
  124. );
  125. }
  126. }
  127. $em->flush();
  128. }
  129. /**
  130. * Unsubscribe groups to item
  131. * @param $tool
  132. * @param \Entity\Course $course
  133. * @param \Entity\Session $session
  134. * @param $itemId
  135. * @param $groups
  136. */
  137. function unsubscribeGroupsToItem($tool, \Entity\Course $course, \Entity\Session $session = null, $itemId, $groups, $unsubscribeUserToo = false)
  138. {
  139. if (!empty($groups)) {
  140. $em = $this->getEntityManager();
  141. foreach ($groups as $groupId) {
  142. $item = $this->findOneBy(array(
  143. 'tool' => $tool,
  144. 'session' => $session,
  145. 'ref' => $itemId,
  146. 'toGroupId' => $groupId
  147. ));
  148. if ($item) {
  149. $em->remove($item);
  150. }
  151. if ($unsubscribeUserToo) {
  152. //Adding users from this group to the item
  153. $users = \GroupManager::get_members_and_tutors($groupId);
  154. $newUserList = array();
  155. if (!empty($users)) {
  156. foreach ($users as $user) {
  157. $newUserList[] = $user['user_id'];
  158. }
  159. $this->unsubcribeUsersToItem(
  160. 'learnpath',
  161. $course,
  162. $session,
  163. $itemId,
  164. $newUserList
  165. );
  166. }
  167. }
  168. }
  169. $em->flush();
  170. }
  171. }
  172. /**
  173. * Subscribe users to a LP, doc (itemproperty)
  174. *
  175. * @param $tool
  176. * @param \Entity\Course $course
  177. * @param \Entity\Session $session
  178. * @param $itemId
  179. * @param array $newUserList
  180. */
  181. public function subscribeUsersToItem($tool, \Entity\Course $course, \Entity\Session $session = null, $itemId, $newUserList = array())
  182. {
  183. $em = $this->getEntityManager();
  184. $user = $em->getRepository('Entity\User');
  185. $usersSubscribedToItem = $this->getUsersSubscribedToItem($tool, $itemId, $course, $session);
  186. $alreadyAddedUsers = array();
  187. if ($usersSubscribedToItem) {
  188. foreach ($usersSubscribedToItem as $itemProperty) {
  189. $alreadyAddedUsers[] = $itemProperty->getToUserId();
  190. }
  191. }
  192. $usersToDelete = $alreadyAddedUsers;
  193. if (!empty($newUserList)) {
  194. $usersToDelete = array_diff($alreadyAddedUsers, $newUserList);
  195. }
  196. if ($usersToDelete) {
  197. $this->unsubcribeUsersToItem($tool, $course, $session, $itemId, $usersToDelete);
  198. }
  199. foreach ($newUserList as $userId) {
  200. if (!in_array($userId, $alreadyAddedUsers)) {
  201. $userObj = $user->find($userId);
  202. $item = new \Entity\CItemProperty($course);
  203. $item->setUser($userObj);
  204. $item->setTool($tool);
  205. $item->setRef($itemId);
  206. if (!empty($session)) {
  207. $item->setSession($session);
  208. }
  209. $item->setLasteditType('LearnpathSubscription');
  210. $item->setVisibility('1');
  211. $em->persist($item); //$em is an instance of EntityManager
  212. }
  213. }
  214. $em->flush();
  215. }
  216. /**
  217. * Unsubscribe users to item
  218. * @param $tool
  219. * @param \Entity\Course $course
  220. * @param \Entity\Session $session
  221. * @param $itemId
  222. * @param $usersToDelete
  223. */
  224. public function unsubcribeUsersToItem($tool, \Entity\Course $course, \Entity\Session $session = null, $itemId, $usersToDelete)
  225. {
  226. $em = $this->getEntityManager();
  227. if (!empty($usersToDelete)) {
  228. foreach ($usersToDelete as $userId) {
  229. $item = $this->findOneBy(
  230. array(
  231. 'tool' => $tool,
  232. 'session' => $session,
  233. 'ref' => $itemId,
  234. 'toUserId' => $userId
  235. )
  236. );
  237. if ($item) {
  238. $em->remove($item);
  239. }
  240. }
  241. $em->flush();
  242. }
  243. }
  244. }