post.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. use Chamilo\PluginBundle\Entity\StudentFollowUp\CarePost;
  4. use Doctrine\Common\Collections\Criteria;
  5. use Gaufrette\Adapter\Ftp as FtpAdapter;
  6. use Gaufrette\Filesystem;
  7. require_once __DIR__.'/../../main/inc/global.inc.php';
  8. $plugin = StudentFollowUpPlugin::create();
  9. $currentUserId = api_get_user_id();
  10. $studentId = isset($_GET['student_id']) ? (int) $_GET['student_id'] : api_get_user_id();
  11. $postId = isset($_GET['post_id']) ? (int) $_GET['post_id'] : 1;
  12. $action = isset($_GET['action']) ? $_GET['action'] : '';
  13. if (empty($studentId)) {
  14. api_not_allowed(true);
  15. }
  16. $permissions = StudentFollowUpPlugin::getPermissions($studentId, $currentUserId);
  17. $isAllow = $permissions['is_allow'];
  18. $showPrivate = $permissions['show_private'];
  19. if ($isAllow === false) {
  20. api_not_allowed(true);
  21. }
  22. $em = Database::getManager();
  23. $qb = $em->createQueryBuilder();
  24. $criteria = Criteria::create();
  25. $criteria->where(Criteria::expr()->eq('user', $studentId));
  26. if ($showPrivate == false) {
  27. $criteria->andWhere(Criteria::expr()->eq('private', false));
  28. }
  29. $criteria->andWhere(Criteria::expr()->eq('id', $postId));
  30. $qb
  31. ->select('distinct p')
  32. ->from('ChamiloPluginBundle:StudentFollowUp\CarePost', 'p')
  33. ->addCriteria($criteria)
  34. ->setMaxResults(1)
  35. ;
  36. $query = $qb->getQuery();
  37. /** @var CarePost $post */
  38. $post = $query->getOneOrNullResult();
  39. // Get related posts (post with same parent)
  40. $relatedPosts = [];
  41. if ($post) {
  42. if ($action == 'download') {
  43. $attachment = $post->getAttachment();
  44. $attachmentUrlData = parse_url($attachment);
  45. if (!empty($attachment) && !empty($attachmentUrlData)) {
  46. $adapter = new FtpAdapter(
  47. '/',
  48. $attachmentUrlData['host'],
  49. [
  50. 'port' => 21,
  51. 'username' => isset($attachmentUrlData['user']) ? $attachmentUrlData['user'] : '',
  52. 'password' => isset($attachmentUrlData['pass']) ? $attachmentUrlData['pass'] : '',
  53. 'passive' => true,
  54. 'create' => false, // Whether to create the remote directory if it does not exist
  55. 'mode' => FTP_BINARY, // Or FTP_TEXT
  56. 'ssl' => false,
  57. ]
  58. );
  59. $filesystem = new Filesystem($adapter);
  60. if ($filesystem->has($attachmentUrlData['path'])) {
  61. $contentType = DocumentManager::file_get_mime_type($attachmentUrlData['path']);
  62. $response = new \Symfony\Component\HttpFoundation\Response();
  63. $response->headers->set('Cache-Control', 'private');
  64. $response->headers->set('Content-type', $contentType);
  65. $response->headers->set('Content-Disposition', 'attachment; filename="'.basename($attachmentUrlData['path']).'";');
  66. //$response->headers->set('Content-length', filesize($filename));
  67. // Send headers before outputting anything
  68. $response->sendHeaders();
  69. $response->setContent($filesystem->read($attachmentUrlData['path']));
  70. $response->send();
  71. exit;
  72. } else {
  73. api_not_allowed(true);
  74. }
  75. } else {
  76. api_not_allowed(true);
  77. }
  78. }
  79. $qb = $em->createQueryBuilder();
  80. $criteria = Criteria::create();
  81. if (!empty($post->getParent())) {
  82. $criteria->where(Criteria::expr()->in('parent', [$post->getParent()->getId(), $post->getId()]));
  83. } else {
  84. $criteria->where(Criteria::expr()->eq('parent', $post->getId()));
  85. }
  86. if ($showPrivate == false) {
  87. $criteria->andWhere(Criteria::expr()->eq('private', false));
  88. }
  89. $criteria->orWhere(Criteria::expr()->eq('id', $post->getId()));
  90. $qb
  91. ->select('p')
  92. ->distinct()
  93. ->from('ChamiloPluginBundle:StudentFollowUp\CarePost', 'p')
  94. ->addCriteria($criteria)
  95. ->orderBy('p.createdAt', 'desc')
  96. ;
  97. $query = $qb->getQuery();
  98. $relatedPosts = $query->getResult();
  99. }
  100. //var_dump($post->getTitle());
  101. $tpl = new Template($plugin->get_lang('plugin_title'));
  102. $tpl->assign('post', $post);
  103. $tpl->assign('related_posts', $relatedPosts);
  104. $url = api_get_path(WEB_PLUGIN_PATH).'/studentfollowup/post.php?student_id='.$studentId;
  105. $tpl->assign('post_url', $url);
  106. $tpl->assign(
  107. 'back_link',
  108. Display::url(
  109. Display::return_icon('back.png'),
  110. api_get_path(WEB_PLUGIN_PATH).'studentfollowup/posts.php?student_id='.$studentId
  111. )
  112. );
  113. $tpl->assign('information_icon', Display::return_icon('info.png'));
  114. $tpl->assign('student_info', api_get_user_info($studentId));
  115. $tpl->assign('care_title', $plugin->get_lang('Student care detail view'));
  116. $content = $tpl->fetch('/'.$plugin->get_name().'/view/post.html.twig');
  117. // Assign into content
  118. $tpl->assign('content', $content);
  119. // Display
  120. $tpl->display_one_col_template();