post.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. use Doctrine\Common\Collections\Criteria;
  4. use Gaufrette\Adapter\Ftp as FtpAdapter;
  5. use Gaufrette\Filesystem;
  6. use Chamilo\PluginBundle\Entity\StudentFollowUp\CarePost;
  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. echo $filesystem->get($attachmentUrlData['path']);
  62. } else {
  63. api_not_allowed(true);
  64. }
  65. } else {
  66. api_not_allowed(true);
  67. }
  68. }
  69. $qb = $em->createQueryBuilder();
  70. $criteria = Criteria::create();
  71. if (!empty($post->getParent())) {
  72. $criteria->where(Criteria::expr()->in('parent', [$post->getParent()->getId(), $post->getId()]));
  73. } else {
  74. $criteria->where(Criteria::expr()->eq('parent', $post->getId()));
  75. }
  76. if ($showPrivate == false) {
  77. $criteria->andWhere(Criteria::expr()->eq('private', false));
  78. }
  79. $criteria->orWhere(Criteria::expr()->eq('id', $post->getId()));
  80. $qb
  81. ->select('p')
  82. ->distinct()
  83. ->from('ChamiloPluginBundle:StudentFollowUp\CarePost', 'p')
  84. ->addCriteria($criteria)
  85. ->orderBy('p.createdAt', 'desc')
  86. ;
  87. $query = $qb->getQuery();
  88. $relatedPosts = $query->getResult();
  89. }
  90. //var_dump($post->getTitle());
  91. $tpl = new Template($plugin->get_lang('plugin_title'));
  92. $tpl->assign('post', $post);
  93. $tpl->assign('related_posts', $relatedPosts);
  94. $url = api_get_path(WEB_PLUGIN_PATH).'/studentfollowup/post.php?student_id='.$studentId;
  95. $tpl->assign('post_url', $url);
  96. $tpl->assign(
  97. 'back_link',
  98. Display::url(
  99. Display::return_icon('back.png'),
  100. api_get_path(WEB_PLUGIN_PATH).'studentfollowup/posts.php?student_id='.$studentId
  101. )
  102. );
  103. $tpl->assign('information_icon', Display::return_icon('info.png'));
  104. $tpl->assign('student_info', api_get_user_info($studentId));
  105. $tpl->assign('care_title', $plugin->get_lang('CareDetailView'));
  106. $content = $tpl->fetch('/'.$plugin->get_name().'/view/post.html.twig');
  107. // Assign into content
  108. $tpl->assign('content', $content);
  109. // Display
  110. $tpl->display_one_col_template();