posts.php 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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 = StudentFollowUpPlugin::getPageSize();
  27. $qb
  28. ->select('p')
  29. ->distinct()
  30. ->from('ChamiloPluginBundle:StudentFollowUp\CarePost', 'p')
  31. ->addCriteria($criteria)
  32. ->setFirstResult($pageSize * ($currentPage - 1))
  33. ->setMaxResults($pageSize)
  34. ->orderBy('p.createdAt', 'desc')
  35. ;
  36. $query = $qb->getQuery();
  37. $posts = new Paginator($query, $fetchJoinCollection = true);
  38. $totalItems = count($posts);
  39. $pagesCount = ceil($totalItems / $pageSize);
  40. $pagination = '';
  41. $url = api_get_self().'?student_id='.$studentId;
  42. if ($totalItems > 1) {
  43. $pagination .= '<ul class="pagination">';
  44. for ($i = 0; $i < $pagesCount; $i++) {
  45. $newPage = $i + 1;
  46. if ($currentPage == $newPage) {
  47. $pagination .= '<li class="active"><a href="'.$url.'&page='.$newPage.'">'.$newPage.'</a></li>';
  48. } else {
  49. $pagination .= '<li><a href="'.$url.'&page='.$newPage.'">'.$newPage.'</a></li>';
  50. }
  51. }
  52. $pagination .= '</ul>';
  53. }
  54. $tpl = new Template($plugin->get_lang('plugin_title'));
  55. $tpl->assign('posts', $posts);
  56. $tpl->assign('current_url', $url);
  57. $url = api_get_path(WEB_PLUGIN_PATH).'studentfollowup/post.php?student_id='.$studentId;
  58. $tpl->assign('post_url', $url);
  59. $tpl->assign('information_icon', Display::return_icon('info.png'));
  60. $tpl->assign('student_info', api_get_user_info($studentId));
  61. $tpl->assign('pagination', $pagination);
  62. $tpl->assign('care_title', $plugin->get_lang('CareDetailView'));
  63. $content = $tpl->fetch('/'.$plugin->get_name().'/view/posts.html.twig');
  64. // Assign into content
  65. $tpl->assign('content', $content);
  66. // Display
  67. $tpl->display_one_col_template();