Test Setup Failed
Push — master ( f71949...6c6bd7 )
by Julito
55:21
created

StudentFollowUpPlugin   A

Complexity

Total Complexity 34

Size/Duplication

Total Lines 230
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
dl 0
loc 230
rs 9.2
c 0
b 0
f 0
wmc 34
lcom 1
cbo 6

8 Methods

Rating   Name   Duplication   Size   Complexity  
A uninstall() 0 11 2
A getEntityPath() 0 4 1
A create() 0 5 2
A __construct() 0 10 1
A install() 0 19 2
F getPermissions() 0 81 18
B getUsers() 0 53 7
A getPageSize() 0 4 1
1
<?php
2
/* For licensing terms, see /license.txt */
3
4
use Symfony\Component\Filesystem\Filesystem;
5
use Chamilo\CoreBundle\Entity\SessionRelCourseRelUser;
6
7
/**
8
 * Class StudentFollowUpPlugin
9
 */
10
class StudentFollowUpPlugin extends Plugin
11
{
12
    public $hasEntity = true;
13
14
    /**
15
     * @return StudentFollowUpPlugin
16
     */
17
    public static function create()
18
    {
19
        static $result = null;
20
        return $result ? $result : $result = new self();
21
    }
22
23
    /**
24
     * StudentFollowUpPlugin constructor.
25
     */
26
    protected function __construct()
27
    {
28
        parent::__construct(
29
            '0.1',
30
            'Julio Montoya',
31
            array(
32
                'tool_enable' => 'boolean',
33
            )
34
        );
35
    }
36
37
    /**
38
     *
39
     */
40
    public function install()
41
    {
42
        $pluginEntityPath = $this->getEntityPath();
43
        if (!is_dir($pluginEntityPath)) {
44
            mkdir($pluginEntityPath, api_get_permissions_for_new_directories());
45
        }
46
47
        $fs = new Filesystem();
48
        $fs->mirror(__DIR__.'/Entity/', $pluginEntityPath, null, ['override']);
49
50
        $sql = "CREATE TABLE sfu_post (id INT AUTO_INCREMENT NOT NULL, insert_user_id INT NOT NULL, user_id INT NOT NULL, parent_id INT DEFAULT NULL, title VARCHAR(255) NOT NULL, content LONGTEXT DEFAULT NULL, external_care_id VARCHAR(255) DEFAULT NULL, created_at DATETIME DEFAULT NULL, updated_at DATETIME DEFAULT NULL, private TINYINT(1) NOT NULL, external_source TINYINT(1) NOT NULL, tags LONGTEXT NOT NULL COMMENT '(DC2Type:array)', attachment VARCHAR(255) NOT NULL, lft INT DEFAULT NULL, rgt INT DEFAULT NULL, lvl INT DEFAULT NULL, root INT DEFAULT NULL, INDEX IDX_35F9473C9C859CC3 (insert_user_id), INDEX IDX_35F9473CA76ED395 (user_id), INDEX IDX_35F9473C727ACA70 (parent_id), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE = InnoDB;";
51
        Database::query($sql);
52
        $sql = "ALTER TABLE sfu_post ADD CONSTRAINT FK_35F9473C9C859CC3 FOREIGN KEY (insert_user_id) REFERENCES user (id);";
53
        Database::query($sql);
54
        $sql = "ALTER TABLE sfu_post ADD CONSTRAINT FK_35F9473CA76ED395 FOREIGN KEY (user_id) REFERENCES user (id);";
55
        Database::query($sql);
56
        $sql = "ALTER TABLE sfu_post ADD CONSTRAINT FK_35F9473C727ACA70 FOREIGN KEY (parent_id) REFERENCES sfu_post (id) ON DELETE SET NULL;";
57
        Database::query($sql);
58
    }
59
60
    /**
61
     *
62
     */
63
    public function uninstall()
64
    {
65
        $pluginEntityPath = $this->getEntityPath();
66
        $fs = new Filesystem();
67
        if ($fs->exists($pluginEntityPath)) {
68
            $fs->remove($pluginEntityPath);
69
        }
70
        $table = Database::get_main_table('sfu_post');
71
        $sql = "DROP TABLE IF EXISTS $table";
72
        Database::query($sql);
73
    }
74
75
    /**
76
     * @return string
77
     */
78
    public function getEntityPath()
79
    {
80
        return api_get_path(SYS_PATH).'src/Chamilo/PluginBundle/Entity/'.$this->getCamelCaseName();
81
    }
82
83
    /**
84
     * @param int $studentId
85
     * @param int $currentUserId
86
     * @return array
87
     */
88
    public static function getPermissions($studentId, $currentUserId)
89
    {
90
        $params = ['variable = ? AND subkey = ?' => ['status', 'studentfollowup']];
91
        $result = api_get_settings_params_simple($params);
92
        $installed = false;
93
        if (!empty($result) && $result['selected_value'] === 'installed') {
94
            $installed = true;
95
        }
96
97
        if ($installed == false) {
98
            return [
99
                'is_allow' => false,
100
                'show_private' => false,
101
            ];
102
        }
103
104
        if ($studentId === $currentUserId) {
105
            $isAllow = true;
106
            $showPrivate = true;
107
        } else {
108
            $isDrh = api_is_drh();
109
            $isCareTaker = false;
110
            $isDrhRelatedViaPost = false;
111
            $isCourseCoach = false;
112
            $isDrhRelatedToSession = false;
113
114
            // Only admins and DRH that follow the user
115
            $isAdmin = api_is_platform_admin();
116
117
            // Check if user is care taker
118
            if ($isDrh) {
119
                $criteria = [
120
                    'user' => $studentId,
121
                    'insertUser' => $currentUserId,
122
                ];
123
                $repo = Database::getManager()->getRepository('ChamiloPluginBundle:StudentFollowUp\CarePost');
124
                $post = $repo->findOneBy($criteria);
125
                if ($post) {
126
                    $isDrhRelatedViaPost = true;
127
                }
128
129
                // Check if course session coach
130
                $sessions = SessionManager::get_sessions_by_user($studentId);
131
132
                if (!empty($sessions)) {
133
                    foreach ($sessions as $session) {
134
                        $sessionId = $session['session_id'];
135
                        $sessionDrhInfo = SessionManager::getSessionFollowedByDrh(
136
                            $currentUserId,
137
                            $sessionId
138
                        );
139
                        if (!empty($sessionDrhInfo)) {
140
                            $isDrhRelatedToSession = true;
141
                            break;
142
                        }
143
                        foreach ($session['courses'] as $course) {
144
                            //$isCourseCoach = api_is_coach($sessionId, $course['real_id']);
145
                            $coachList = SessionManager::getCoachesByCourseSession(
146
                                $sessionId,
147
                                $course['real_id']
148
                            );
149
                            if (!empty($coachList) && in_array($currentUserId, $coachList)) {
150
                                $isCourseCoach = true;
151
                                break(2);
152
                            }
153
                        }
154
                    }
155
                }
156
157
                $isCareTaker = $isDrhRelatedViaPost && $isDrhRelatedToSession;
158
            }
159
160
            $isAllow = $isAdmin || $isCareTaker || $isDrhRelatedToSession || $isCourseCoach;
161
            $showPrivate = $isAdmin || $isCareTaker;
162
        }
163
164
        return [
165
            'is_allow' => $isAllow,
166
            'show_private' => $showPrivate,
167
        ];
168
    }
169
170
    /**
171
     * @param string $status
172
     * @param int $currentUserId
173
     * @param int $start
174
     * @param int $limit
175
     *
176
     * @return array
177
     */
178
    public static function getUsers($status, $currentUserId, $start, $limit)
179
    {
180
        switch ($status) {
181
            case COURSEMANAGER:
182
                $sessions = SessionManager::get_sessions_by_user($currentUserId);
183
                $sessions = array_column($sessions, 'session_id');
184
185
                // Get session courses where I'm coach
186
                $courseList = SessionManager::getCoursesListByCourseCoach($currentUserId);
187
                $courses = [];
188
                /** @var SessionRelCourseRelUser $courseItem */
189
                foreach ($courseList as $courseItem) {
190
                    $courses[] = $courseItem->getCourse()->getId();
191
                }
192
                break;
193
            case DRH:
194
                $sessions = SessionManager::get_sessions_followed_by_drh($currentUserId);
195
                $sessions = array_column($sessions, 'id');
196
                $courses = [];
197
                foreach ($sessions as $sessionId) {
198
                    $sessionDrhInfo = SessionManager::getSessionFollowedByDrh(
199
                        $currentUserId,
200
                        $sessionId
201
                    );
202
                    if ($sessionDrhInfo && isset($sessionDrhInfo['course_list'])) {
203
                        $courses = array_merge($courses, array_column($sessionDrhInfo['course_list'], 'id'));
204
                    }
205
                }
206
                break;
207
        }
208
209
        $userList = SessionManager::getUsersByCourseAndSessionList(
210
            $sessions,
211
            $courses,
212
            $start,
213
            $limit
214
        );
215
216
        /*$userList = [];
217
        foreach ($sessions as $sessionId) {
218
            foreach ($courses as $courseId) {
219
                $courseInfo = ['real_id' => $courseId];
220
                $userFromSessionList = SessionManager::getUsersByCourseSession(
221
                    $sessionId,
222
                    $courseInfo
223
                );
224
                $userList = array_merge($userList, $userFromSessionList);
225
            }
226
            $userList = array_unique($userList);
227
        }*/
228
229
        return $userList;
230
    }
231
232
    /**
233
     * @return int
234
     */
235
    public static function getPageSize()
236
    {
237
        return 2;
238
    }
239
}
240