Total Complexity | 40 |
Total Lines | 247 |
Duplicated Lines | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
Complex classes like StudentFollowUpPlugin often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use StudentFollowUpPlugin, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
10 | class StudentFollowUpPlugin extends Plugin |
||
11 | { |
||
12 | public $hasEntity = true; |
||
13 | |||
14 | /** |
||
15 | * StudentFollowUpPlugin constructor. |
||
16 | */ |
||
17 | protected function __construct() |
||
18 | { |
||
19 | parent::__construct( |
||
20 | '0.1', |
||
21 | 'Julio Montoya', |
||
22 | [ |
||
23 | 'tool_enable' => 'boolean', |
||
24 | ] |
||
25 | ); |
||
26 | } |
||
27 | |||
28 | /** |
||
29 | * @return StudentFollowUpPlugin |
||
30 | */ |
||
31 | public static function create() |
||
32 | { |
||
33 | static $result = null; |
||
34 | |||
35 | return $result ? $result : $result = new self(); |
||
36 | } |
||
37 | |||
38 | public function install() |
||
39 | { |
||
40 | $pluginEntityPath = $this->getEntityPath(); |
||
41 | if (!is_dir($pluginEntityPath)) { |
||
42 | if (!is_writable(dirname($pluginEntityPath))) { |
||
43 | $message = get_lang('ErrorCreatingDir').': '.$pluginEntityPath; |
||
44 | Display::addFlash(Display::return_message($message, 'error')); |
||
45 | |||
46 | return false; |
||
47 | } |
||
48 | mkdir($pluginEntityPath, api_get_permissions_for_new_directories()); |
||
49 | } |
||
50 | |||
51 | $fs = new Filesystem(); |
||
52 | $fs->mirror(__DIR__.'/Entity/', $pluginEntityPath, null, ['override']); |
||
53 | $schema = Database::getManager()->getConnection()->getSchemaManager(); |
||
54 | |||
55 | if ($schema->tablesExist('sfu_post') === false) { |
||
56 | $sql = "CREATE TABLE IF NOT EXISTS 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;"; |
||
57 | Database::query($sql); |
||
58 | $sql = "ALTER TABLE sfu_post ADD CONSTRAINT FK_35F9473C9C859CC3 FOREIGN KEY (insert_user_id) REFERENCES user (id);"; |
||
59 | Database::query($sql); |
||
60 | $sql = "ALTER TABLE sfu_post ADD CONSTRAINT FK_35F9473CA76ED395 FOREIGN KEY (user_id) REFERENCES user (id);"; |
||
61 | Database::query($sql); |
||
62 | $sql = "ALTER TABLE sfu_post ADD CONSTRAINT FK_35F9473C727ACA70 FOREIGN KEY (parent_id) REFERENCES sfu_post (id) ON DELETE SET NULL;"; |
||
63 | Database::query($sql); |
||
64 | } |
||
65 | } |
||
66 | |||
67 | public function uninstall() |
||
68 | { |
||
69 | $pluginEntityPath = $this->getEntityPath(); |
||
70 | $fs = new Filesystem(); |
||
71 | if ($fs->exists($pluginEntityPath)) { |
||
72 | $fs->remove($pluginEntityPath); |
||
73 | } |
||
74 | $table = Database::get_main_table('sfu_post'); |
||
75 | $sql = "DROP TABLE IF EXISTS $table"; |
||
76 | Database::query($sql); |
||
77 | } |
||
78 | |||
79 | /** |
||
80 | * @return string |
||
81 | */ |
||
82 | public function getEntityPath() |
||
85 | } |
||
86 | |||
87 | /** |
||
88 | * @param int $studentId |
||
89 | * @param int $currentUserId |
||
90 | * |
||
91 | * @return array |
||
92 | */ |
||
93 | public static function getPermissions($studentId, $currentUserId) |
||
94 | { |
||
95 | $installed = AppPlugin::getInstance()->isInstalled('studentfollowup'); |
||
96 | if ($installed === false) { |
||
97 | return [ |
||
98 | 'is_allow' => false, |
||
99 | 'show_private' => false, |
||
100 | ]; |
||
101 | } |
||
102 | |||
103 | if ($studentId === $currentUserId) { |
||
104 | $isAllow = true; |
||
105 | $showPrivate = true; |
||
106 | } else { |
||
107 | $isDrh = api_is_drh(); |
||
108 | $isDrhRelatedViaPost = false; |
||
109 | $isCourseCoach = false; |
||
110 | $isDrhRelatedToSession = false; |
||
111 | |||
112 | // Only admins and DRH that follow the user. |
||
113 | $isAdmin = api_is_platform_admin(); |
||
114 | |||
115 | // Check if user is care taker. |
||
116 | if ($isDrh) { |
||
117 | $criteria = [ |
||
118 | 'user' => $studentId, |
||
119 | 'insertUser' => $currentUserId, |
||
120 | ]; |
||
121 | $repo = Database::getManager()->getRepository('ChamiloPluginBundle:StudentFollowUp\CarePost'); |
||
122 | $post = $repo->findOneBy($criteria); |
||
123 | if ($post) { |
||
124 | $isDrhRelatedViaPost = true; |
||
125 | } |
||
126 | } |
||
127 | |||
128 | // Student sessions. |
||
129 | $sessions = SessionManager::get_sessions_by_user($studentId, false, true); |
||
130 | if (!empty($sessions)) { |
||
131 | foreach ($sessions as $session) { |
||
132 | $sessionId = $session['session_id']; |
||
133 | // Check if the current user is following that session. |
||
134 | $sessionDrhInfo = SessionManager::getSessionFollowedByDrh( |
||
135 | $currentUserId, |
||
136 | $sessionId |
||
137 | ); |
||
138 | if (!empty($sessionDrhInfo)) { |
||
139 | $isDrhRelatedToSession = true; |
||
140 | break; |
||
141 | } |
||
142 | |||
143 | // Check if teacher is coach between the date limits. |
||
144 | $visibility = api_get_session_visibility( |
||
145 | $sessionId, |
||
146 | null, |
||
147 | true, |
||
148 | $currentUserId |
||
149 | ); |
||
150 | |||
151 | if (SESSION_AVAILABLE === $visibility && isset($session['courses']) && !empty($session['courses'])) { |
||
152 | foreach ($session['courses'] as $course) { |
||
153 | $coachList = SessionManager::getCoachesByCourseSession( |
||
154 | $sessionId, |
||
155 | $course['real_id'] |
||
156 | ); |
||
157 | if (!empty($coachList) && in_array($currentUserId, $coachList)) { |
||
158 | $isCourseCoach = true; |
||
159 | break 2; |
||
160 | } |
||
161 | } |
||
162 | } |
||
163 | } |
||
164 | } |
||
165 | |||
166 | $isCareTaker = $isDrhRelatedViaPost && $isDrhRelatedToSession; |
||
167 | $isAllow = $isAdmin || $isCareTaker || $isDrhRelatedToSession || $isCourseCoach; |
||
168 | $showPrivate = $isAdmin || $isCareTaker; |
||
169 | } |
||
170 | |||
171 | return [ |
||
172 | 'is_allow' => $isAllow, |
||
173 | 'show_private' => $showPrivate, |
||
174 | ]; |
||
175 | } |
||
176 | |||
177 | /** |
||
178 | * @param string $status |
||
179 | * @param int $currentUserId |
||
180 | * @param int $sessionId |
||
181 | * @param int $start |
||
182 | * @param int $limit |
||
183 | * |
||
184 | * @return array |
||
185 | */ |
||
186 | public static function getUsers($status, $currentUserId, $sessionId, $start, $limit) |
||
187 | { |
||
188 | $sessions = []; |
||
189 | $courses = []; |
||
190 | $sessionsFull = []; |
||
191 | |||
192 | switch ($status) { |
||
193 | case COURSEMANAGER: |
||
194 | $sessionsFull = SessionManager::getSessionsCoachedByUser($currentUserId); |
||
195 | $sessions = array_column($sessionsFull, 'id'); |
||
196 | if (!empty($sessionId)) { |
||
197 | $sessions = [$sessionId]; |
||
198 | } |
||
199 | // Get session courses where I'm coach |
||
200 | $courseList = SessionManager::getCoursesListByCourseCoach($currentUserId); |
||
201 | $courses = []; |
||
202 | /** @var SessionRelCourseRelUser $courseItem */ |
||
203 | foreach ($courseList as $courseItem) { |
||
204 | $courses[] = $courseItem->getCourse()->getId(); |
||
205 | } |
||
206 | break; |
||
207 | case DRH: |
||
208 | $sessionsFull = SessionManager::get_sessions_followed_by_drh($currentUserId); |
||
209 | $sessions = array_column($sessionsFull, 'id'); |
||
210 | |||
211 | if (!empty($sessionId)) { |
||
212 | $sessions = [$sessionId]; |
||
213 | } |
||
214 | $courses = []; |
||
215 | foreach ($sessions as $sessionId) { |
||
|
|||
216 | $sessionDrhInfo = SessionManager::getSessionFollowedByDrh( |
||
217 | $currentUserId, |
||
218 | $sessionId |
||
219 | ); |
||
220 | if ($sessionDrhInfo && isset($sessionDrhInfo['course_list'])) { |
||
221 | $courses = array_merge($courses, array_column($sessionDrhInfo['course_list'], 'id')); |
||
222 | } |
||
223 | } |
||
224 | break; |
||
225 | } |
||
226 | |||
227 | $userList = SessionManager::getUsersByCourseAndSessionList( |
||
228 | $sessions, |
||
229 | $courses, |
||
230 | $start, |
||
231 | $limit |
||
232 | ); |
||
233 | |||
234 | return [ |
||
235 | 'users' => $userList, |
||
236 | 'sessions' => $sessionsFull, |
||
237 | ]; |
||
238 | } |
||
239 | |||
240 | /** |
||
241 | * @return int |
||
242 | */ |
||
243 | public static function getPageSize() |
||
246 | } |
||
247 | |||
248 | /** |
||
249 | * @param int $userId |
||
250 | */ |
||
251 | public function doWhenDeletingUser($userId) |
||
257 | } |
||
258 | } |
||
259 |