posts.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. use Doctrine\Common\Collections\Criteria;
  4. use Doctrine\ORM\Tools\Pagination\Paginator;
  5. require_once __DIR__.'/../../main/inc/global.inc.php';
  6. $plugin = StudentFollowUpPlugin::create();
  7. $currentUserId = api_get_user_id();
  8. $studentId = isset($_GET['student_id']) ? (int) $_GET['student_id'] : api_get_user_id();
  9. $currentPage = isset($_GET['page']) ? (int) $_GET['page'] : 1;
  10. if (empty($studentId)) {
  11. api_not_allowed(true);
  12. }
  13. $permissions = StudentFollowUpPlugin::getPermissions($studentId, $currentUserId);
  14. $isAllow = $permissions['is_allow'];
  15. $showPrivate = $permissions['show_private'];
  16. if ($isAllow === false) {
  17. api_not_allowed(true);
  18. }
  19. $em = Database::getManager();
  20. $qb = $em->createQueryBuilder();
  21. $criteria = Criteria::create();
  22. $criteria->where(Criteria::expr()->eq('user', $studentId));
  23. if ($showPrivate == false) {
  24. $criteria->andWhere(Criteria::expr()->eq('private', false));
  25. }
  26. $pageSize = 2;
  27. $qb
  28. ->select('p')
  29. ->from('ChamiloPluginBundle:StudentFollowUp\CarePost', 'p')
  30. ->addCriteria($criteria)
  31. ->setFirstResult($pageSize * ($currentPage - 1))
  32. ->setMaxResults($pageSize)
  33. ->orderBy('p.createdAt', 'desc')
  34. ;
  35. $query = $qb->getQuery();
  36. $posts = new Paginator($query, $fetchJoinCollection = true);
  37. $totalItems = count($posts);
  38. $pagesCount = ceil($totalItems / $pageSize);
  39. $pagination = '';
  40. $url = api_get_self().'?student_id='.$studentId;
  41. $pagination .= '<ul class="pagination">';
  42. for ($i = 0; $i < $pagesCount; $i++) {
  43. $newPage = $i + 1;
  44. if ($currentPage == $newPage) {
  45. $pagination .= '<li class="active"><a href="'.$url.'&page='.$newPage.'">'.$newPage.'</a></li>';
  46. } else {
  47. $pagination .= '<li><a href="'.$url.'&page='.$newPage.'">'.$newPage.'</a></li>';
  48. }
  49. }
  50. $pagination .= '</ul>';
  51. $tpl = new Template($plugin->get_lang('plugin_title'));
  52. $tpl->assign('posts', $posts);
  53. $tpl->assign('current_url', $url);
  54. $url = api_get_path(WEB_PLUGIN_PATH).'studentfollowup/post.php?student_id='.$studentId;
  55. $tpl->assign('post_url', $url);
  56. $tpl->assign('information_icon', Display::return_icon('info.png'));
  57. $tpl->assign('pagination', $pagination);
  58. $content = $tpl->fetch('/'.$plugin->get_name().'/view/posts.html.twig');
  59. // Assign into content
  60. $tpl->assign('content', $content);
  61. // Display
  62. $tpl->display_one_col_template();