src/Twig/NotificationExtensionRuntime.php line 462

Open in your IDE?
  1. <?php
  2. namespace App\Twig;
  3. use App\Entity\Admin;
  4. use App\Entity\Child;
  5. use App\Entity\ChildPreference;
  6. use App\Entity\Guardian;
  7. use App\Entity\Lesson;
  8. use App\Entity\Teacher;
  9. use App\Entity\TeacherSurveyResponse;
  10. use App\Repository\AdminRepository;
  11. use App\Repository\ChildRepository;
  12. use App\Repository\GuardianRepository;
  13. use App\Repository\InfoLineRepository;
  14. use App\Repository\LessonRepository;
  15. use App\Repository\Notification\NotificationRepository;
  16. use App\Repository\Notification\UserNotificationRepository;
  17. use App\Repository\TeacherRepository;
  18. use App\Repository\TeacherSurveyResponseRepository;
  19. use App\Service\LearnCubeService;
  20. use App\Service\TeacherFirstLessonReviewService;
  21. use Symfony\Component\Asset\Packages;
  22. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  23. use Twig\Extension\RuntimeExtensionInterface;
  24. class NotificationExtensionRuntime implements RuntimeExtensionInterface
  25. {
  26. private TokenStorageInterface $tokenStorage;
  27. private LessonRepository $lessonRepository;
  28. private LearnCubeService $learnCubeService;
  29. private NotificationRepository $notificationRepository;
  30. private UserNotificationRepository $userNotificationRepository;
  31. private AdminRepository $adminRepository;
  32. private TeacherRepository $teacherRepository;
  33. private ChildRepository $childRepository;
  34. private GuardianRepository $guardianRepository;
  35. private Packages $assetsManager;
  36. private TeacherSurveyResponseRepository $teacherSurveyResponseRepository;
  37. private InfoLineRepository $infoLineRepository;
  38. private TeacherFirstLessonReviewService $teacherFirstLessonReviewService;
  39. public function __construct(TokenStorageInterface $tokenStorage,
  40. LessonRepository $lessonRepository,
  41. LearnCubeService $learnCubeService,
  42. NotificationRepository $notificationRepository,
  43. UserNotificationRepository $userNotificationRepository,
  44. AdminRepository $adminRepository,
  45. TeacherRepository $teacherRepository,
  46. ChildRepository $childRepository,
  47. GuardianRepository $guardianRepository,
  48. Packages $assetsManager,
  49. TeacherSurveyResponseRepository $teacherSurveyResponseRepository,
  50. InfoLineRepository $infoLineRepository,
  51. TeacherFirstLessonReviewService $teacherFirstLessonReviewService
  52. )
  53. {
  54. $this->tokenStorage = $tokenStorage;
  55. $this->lessonRepository = $lessonRepository;
  56. $this->learnCubeService = $learnCubeService;
  57. $this->notificationRepository = $notificationRepository;
  58. $this->userNotificationRepository = $userNotificationRepository;
  59. $this->adminRepository = $adminRepository;
  60. $this->teacherRepository = $teacherRepository;
  61. $this->childRepository = $childRepository;
  62. $this->guardianRepository = $guardianRepository;
  63. $this->assetsManager = $assetsManager;
  64. $this->teacherSurveyResponseRepository = $teacherSurveyResponseRepository;
  65. $this->infoLineRepository = $infoLineRepository;
  66. $this->teacherFirstLessonReviewService = $teacherFirstLessonReviewService;
  67. }
  68. public function getInfoLines()
  69. {
  70. $user = $this->tokenStorage->getToken()->getUser();
  71. $infoLines = $this->infoLineRepository->findBy(['active' => true], ['id' => 'DESC']);
  72. foreach ($infoLines as $key => $infoLine) {
  73. if($user instanceof Admin && !in_array('admin',$infoLine->getReceivers())) {
  74. unset($infoLines[$key]);
  75. }
  76. if($user instanceof Guardian && !in_array('guardian',$infoLine->getReceivers())) {
  77. unset($infoLines[$key]);
  78. }
  79. if($user instanceof Child && !in_array('child',$infoLine->getReceivers())) {
  80. unset($infoLines[$key]);
  81. }
  82. if($user instanceof Teacher && !in_array('teacher',$infoLine->getReceivers())) {
  83. unset($infoLines[$key]);
  84. }
  85. }
  86. return $infoLines;
  87. }
  88. public function getTeacherFirstLessonSurveyData()
  89. {
  90. /** @var Teacher $teacher */
  91. $teacher = $this->tokenStorage->getToken()->getUser();
  92. foreach ($teacher->getChildPreferences() as $childPreference)
  93. {
  94. // dump($childPreference->getDiscipline());
  95. // die();
  96. $surveyAnswered = false;
  97. /** @var Lesson $lesson */
  98. $lesson = $this->teacherFirstLessonReviewService->findFirstActualLesson(
  99. $teacher,
  100. $childPreference->getChild(),
  101. $childPreference->getDiscipline()
  102. );
  103. // if(!$lesson || $lesson->getStartTime()->format('Y-m-d') != (new \DateTime('now'))->format('Y-m-d'))
  104. // {
  105. // continue;
  106. // }
  107. if(!$lesson || $lesson->getStartTime() <= (new \DateTime('-2 days')))
  108. {
  109. continue;
  110. }
  111. // dump($lesson->getStartTime());
  112. // dump((new \DateTime('-2 days')));
  113. // dump($lesson);
  114. // die();
  115. $firstDayComments = $this->teacherFirstLessonReviewService->findReviewsAfterLesson(
  116. $teacher,
  117. $lesson->getChild(),
  118. $lesson->getStartTime()
  119. );
  120. $filled = '';
  121. foreach ($firstDayComments as $comment)
  122. {
  123. if($this->teacherFirstLessonReviewService->isReviewFilled($comment))
  124. {
  125. $surveyAnswered = true;
  126. }
  127. $filled = $comment->getComment();
  128. }
  129. if($lesson && !$surveyAnswered)
  130. {
  131. // dump($lesson);die();
  132. $surveyData = [];
  133. $identifier = $childPreference->getChild()->getId() . '-' . $childPreference->getDiscipline()->getId();
  134. $surveyData[$identifier] = [
  135. 'child' => ['id' => $childPreference->getChild()->getId(), 'name' => $childPreference->getChild()->getFullname(), 'class' => $childPreference->getChild()->getClass()],
  136. 'discipline' => ['id' => $childPreference->getDiscipline()->getId(), 'name' => $childPreference->getDiscipline()->getName(), 'displayName' => $childPreference->getDiscipline()->getDisplayName()],
  137. 'guardian' => ['id' => $childPreference->getChild()->getGuardian()->getId(), 'phone' => $childPreference->getChild()->getGuardian()->getPhoneNumber()],
  138. 'filled' => $filled,
  139. 'filled_count' => count(array_filter(explode(' ', $filled)))
  140. ];
  141. return $surveyData;
  142. }
  143. }
  144. // die();
  145. return [];
  146. }
  147. public function getTeacherLastLessonSurveyData()
  148. {
  149. /** @var Teacher $teacher */
  150. $teacher = $this->tokenStorage->getToken()->getUser();
  151. $query = $this->teacherSurveyResponseRepository->createQueryBuilder('s');
  152. $lastDayLessons = $query
  153. ->where('s.teacher = :teacher')
  154. ->setParameter('teacher', $teacher)
  155. ->andWhere('s.lastLesson = 1')
  156. ->andWhere('s.comment IS NULL')
  157. ->andWhere('s.dateTo >= :current')
  158. ->andWhere('s.dateFrom <= :current')
  159. ->setParameter('current', new \DateTime())
  160. ->orderBy('s.createdAt', 'ASC')
  161. ->setMaxResults(1)
  162. ->getQuery()
  163. ->getResult();
  164. $surveyData = [];
  165. /**
  166. * @var TeacherSurveyResponse $item
  167. */
  168. foreach ($lastDayLessons as $item) {
  169. $identifier = $item->getChild()->getId() . '-' . $item->getDiscipline()->getId();
  170. $surveyData[$identifier] = [
  171. 'child' => ['id' => $item->getChild()->getId(), 'name' => $item->getChild()->getFullname(), 'class' => $item->getChild()->getClass()],
  172. 'discipline' => ['id' => $item->getDiscipline()->getId(), 'name' => $item->getDiscipline()->getName()],
  173. 'guardian' => ['id' => $item->getChild()->getGuardian()->getId()],
  174. 'filled' => '',
  175. 'filled_count' => 0
  176. ];
  177. }
  178. return $surveyData;
  179. }
  180. public function getNewestLessons()
  181. {
  182. $user = $this->tokenStorage->getToken()->getUser();
  183. $query = $this->lessonRepository->createQueryBuilder('l');
  184. //Disable all lessons exept allowed oned
  185. $query
  186. ->leftJoin('l.teacher', 'teacher')
  187. ->andWhere('teacher.unickoFunction = :unickoFunction')
  188. ->setParameter('unickoFunction', 1);
  189. if (!$user || in_array('ROLE_TEACHER_REGISTER',$user->getRoles())) {
  190. return ['lessons'=>[], 'learncubeUser' => null];
  191. }
  192. if (in_array('ROLE_GUARDIAN', $user->getRoles())) {
  193. $learnCubeUser = $this->learnCubeService->tag_child;
  194. $query
  195. ->leftJoin('l.children', 'c')
  196. ->andWhere('c.guardian = :guardian')
  197. ->setParameter('guardian', $user);
  198. }
  199. if (in_array('ROLE_TEACHER', $user->getRoles())) {
  200. $learnCubeUser = $this->learnCubeService->tag_teacher;
  201. $query
  202. ->andWhere('l.teacher = :teacher')
  203. ->setParameter('teacher', $user)
  204. ->setMaxResults(1);
  205. }
  206. if (in_array('ROLE_ADMIN', $user->getRoles())) {
  207. return ['lessons'=>[]];
  208. }
  209. if (in_array('ROLE_CHILD', $user->getRoles())) {
  210. $learnCubeUser = $this->learnCubeService->tag_child;
  211. /**
  212. * @var Child $child
  213. */
  214. $query
  215. ->andWhere(':children member of l.children')
  216. ->setParameter('children', $user)
  217. ->setMaxResults(1);
  218. }
  219. $dateNow = new \DateTime();
  220. $dateNow->modify('-4 hour');
  221. $dateEnd = new \DateTime();
  222. $dateEnd->modify('+4 hour');
  223. $dateEnd->modify('+1 minutes');
  224. $dateDeadline = new \DateTime();
  225. $dateDeadline->modify('-5 minutes');
  226. $lessons = $query
  227. ->andWhere('l.startTime >= :startTime')
  228. ->setParameter('startTime', $dateNow)
  229. ->andWhere("DATE_SUB(l.endTime, l.duration, 'MINUTE') <= :endTime")
  230. ->andWhere("l.endTime >= :dateDeadline")
  231. ->andWhere("l.childMissed = 0")
  232. ->andWhere("l.teacherMissed = 0")
  233. ->setParameter('endTime', $dateEnd)
  234. ->setParameter('dateDeadline', $dateDeadline)
  235. ->orderBy('l.startTime')
  236. ->getQuery()
  237. ->getResult();
  238. if($lessons && count($lessons) > 1) {
  239. $addedChilds = [];
  240. $lessonsToShow = [];
  241. foreach ($lessons as $lesson)
  242. {
  243. if(!in_array($lesson->getChild()->getId(), $addedChilds))
  244. {
  245. $addedChilds[] = $lesson->getChild()->getId();
  246. $lessonsToShow[] = $lesson;
  247. }
  248. }
  249. $lessons = $lessonsToShow;
  250. }
  251. return [
  252. 'lessons' => $lessons,
  253. 'learncubeUser' => $learnCubeUser,
  254. 'learncubeTagLesson' => $this->learnCubeService->tag_lesson,
  255. 'learncubePublicKey' => $this->learnCubeService->publicKey,
  256. ];
  257. }
  258. public function getNotificationCount()
  259. {
  260. if (!$token = $this->tokenStorage->getToken()) {
  261. return;
  262. }
  263. if (!$token->isAuthenticated()) {
  264. return;
  265. }
  266. if (!$user = $token->getUser()) {
  267. return;
  268. }
  269. if ($user instanceof Teacher) {
  270. /**
  271. * @var Teacher $teacher
  272. */
  273. $teacher = $user;
  274. $count = 0;
  275. foreach ($teacher->getChildPreferences() as $childPreference) {
  276. if ($childPreference->getStatus() == ChildPreference::STATUS_ASSIGNED['value']) {
  277. $count++;
  278. }
  279. }
  280. return $count;
  281. }
  282. }
  283. public function getNewNotificationCount()
  284. {
  285. if (!$token = $this->tokenStorage->getToken()) {
  286. return;
  287. }
  288. if (!$token->isAuthenticated()) {
  289. return;
  290. }
  291. if (!$user = $token->getUser()) {
  292. return;
  293. }
  294. if ($user instanceof Teacher) {
  295. /**
  296. * @var Teacher $teacher
  297. */
  298. $teacher = $user;
  299. $count = 0;
  300. foreach ($teacher->getChildPreferences() as $childPreference) {
  301. if ($childPreference->getAcceptedAt() >= (new \DateTime())->modify('-72 hours')) {
  302. $count++;
  303. }
  304. }
  305. return $count;
  306. }
  307. }
  308. public function getUserNotificationData()
  309. {
  310. if (!$token = $this->tokenStorage->getToken()) {
  311. return;
  312. }
  313. if (!$token->isAuthenticated()) {
  314. return;
  315. }
  316. if (!$user = $token->getUser()) {
  317. return;
  318. }
  319. $userType = null;
  320. if ($user instanceof Teacher) {
  321. $userType = 'teacher';
  322. }
  323. if ($user instanceof Admin) {
  324. $userType = 'admin';
  325. }
  326. if ($user instanceof Guardian) {
  327. $userType = 'guardian';
  328. }
  329. if ($user instanceof Child) {
  330. $userType = 'child';
  331. }
  332. $query = $this->userNotificationRepository->createQueryBuilder('n');
  333. $newCount = $query
  334. ->select('count(n.id)')
  335. ->andWhere('n.userType = :userType')
  336. ->setParameter('userType', $userType)
  337. ->andWhere('n.userId = :userId')
  338. ->setParameter('userId', $user->getId())
  339. ->andWhere('n.seen = :seen')
  340. ->setParameter('seen', 0)
  341. ->leftJoin('n.notification', 'notification')
  342. ->andWhere('notification.isDeleted = :isDeleted')
  343. ->setParameter('isDeleted', 0)
  344. ->getQuery()
  345. ->getSingleScalarResult();
  346. $query = $this->notificationRepository->createQueryBuilder('n');
  347. $notifications = $query
  348. ->leftJoin('n.users', 'u')
  349. ->andWhere('u.userType = :userType')
  350. ->setParameter('userType', $userType)
  351. ->andWhere('u.userId = :userId')
  352. ->setParameter('userId', $user->getId())
  353. ->setMaxResults(5)
  354. ->orderBy('n.dateAdd', 'DESC')
  355. ->getQuery()
  356. ->getResult();
  357. foreach ($notifications as $key => $notificationItem)
  358. {
  359. $query = $this->userNotificationRepository->createQueryBuilder('u');
  360. $userNotification = $query
  361. ->andWhere('u.userType = :userType')
  362. ->setParameter('userType', $userType)
  363. ->andWhere('u.userId = :userId')
  364. ->setParameter('userId', $user->getId())
  365. ->andWhere('u.notification = :notification')
  366. ->setParameter('notification', $notificationItem->getId())
  367. ->setMaxResults(1)
  368. ->getQuery()
  369. ->getOneOrNullResult();
  370. if($userNotification->isSeen()) {
  371. $notifications[$key]->seen = true;
  372. }
  373. $createdByUserType = $notificationItem->getCreatedByUserType();
  374. $createdByUserId = $notificationItem->getCreatedByUserId();
  375. $createdBy = null;
  376. if ($createdByUserType == 'admin') {
  377. $createdBy = $this->adminRepository->find($createdByUserId);
  378. }
  379. elseif ($createdByUserType == 'teacher') {
  380. $createdBy = $this->teacherRepository->find($createdByUserId);
  381. }
  382. elseif ($createdByUserType == 'guardian') {
  383. $createdBy = $this->guardianRepository->find($createdByUserId);
  384. }
  385. elseif ($createdByUserType == 'child') {
  386. $createdBy = $this->childRepository->find($createdByUserId);
  387. }
  388. if($createdByUserType == 'admin')
  389. {
  390. $notifications[$key]->{'createdBy'} = [
  391. 'fullname' => 'Administracija',
  392. 'email' => 'info@corepetitus.lt',
  393. 'profileImagePath' => $this->assetsManager->getUrl('assets/images/core-logo-min.png')
  394. ];
  395. } else {
  396. $notifications[$key]->{'createdBy'} = [
  397. 'fullname' => $createdBy->getFullname(),
  398. 'email' => $createdBy->getEmail(),
  399. 'profileImagePath' => $createdBy->getProfileImagePath()
  400. ];
  401. }
  402. if(!$notifications[$key]->createdBy['profileImagePath'])
  403. {
  404. $notifications[$key]->createdBy['profileImagePath'] = '/uploads/profile-placeholder.png';
  405. }
  406. }
  407. $notificationsData = [
  408. 'new_count' => $newCount,
  409. 'notifications' => $notifications
  410. ];
  411. return $notificationsData;
  412. }
  413. public function getUserNotificationCount()
  414. {
  415. if (!$token = $this->tokenStorage->getToken()) {
  416. return;
  417. }
  418. if (!$token->isAuthenticated()) {
  419. return;
  420. }
  421. if (!$user = $token->getUser()) {
  422. return;
  423. }
  424. $userType = null;
  425. if ($user instanceof Teacher) {
  426. $userType = 'teacher';
  427. }
  428. if ($user instanceof Admin) {
  429. $userType = 'admin';
  430. }
  431. if ($user instanceof Guardian) {
  432. $userType = 'guardian';
  433. }
  434. if ($user instanceof Child) {
  435. $userType = 'child';
  436. }
  437. $query = $this->userNotificationRepository->createQueryBuilder('n');
  438. $newCount = $query
  439. ->select('count(n.id)')
  440. ->andWhere('n.userType = :userType')
  441. ->setParameter('userType', $userType)
  442. ->andWhere('n.userId = :userId')
  443. ->setParameter('userId', $user->getId())
  444. ->andWhere('n.seen = :seen')
  445. ->setParameter('seen', 0)
  446. ->leftJoin('n.notification', 'notification')
  447. ->andWhere('notification.isDeleted = :isDeleted')
  448. ->setParameter('isDeleted', 0)
  449. ->getQuery()
  450. ->getSingleScalarResult();
  451. return $newCount;
  452. }
  453. }