Issues (2091)

public/plugin/StudentFollowUp/post.php (1 issue)

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