1 | <?php |
||
2 | /* For licensing terms, see /license.txt */ |
||
3 | |||
4 | use Chamilo\CoreBundle\Entity\SessionRelCourseRelUser; |
||
5 | use Chamilo\PluginBundle\Entity\StudentFollowUp\CarePost; |
||
6 | use Doctrine\ORM\Tools\SchemaTool; |
||
7 | |||
8 | /** |
||
9 | * Class StudentFollowUpPlugin. |
||
10 | */ |
||
11 | class StudentFollowUpPlugin extends Plugin |
||
12 | { |
||
13 | public $hasEntity = true; |
||
14 | |||
15 | /** |
||
16 | * StudentFollowUpPlugin constructor. |
||
17 | */ |
||
18 | protected function __construct() |
||
19 | { |
||
20 | parent::__construct( |
||
21 | '0.1', |
||
22 | 'Julio Montoya', |
||
23 | [ |
||
24 | 'tool_enable' => 'boolean', |
||
25 | ] |
||
26 | ); |
||
27 | } |
||
28 | |||
29 | /** |
||
30 | * @return StudentFollowUpPlugin |
||
31 | */ |
||
32 | public static function create() |
||
33 | { |
||
34 | static $result = null; |
||
35 | |||
36 | return $result ? $result : $result = new self(); |
||
37 | } |
||
38 | |||
39 | /** |
||
40 | * @throws \Doctrine\ORM\Tools\ToolsException |
||
41 | */ |
||
42 | public function install() |
||
43 | { |
||
44 | $em = Database::getManager(); |
||
45 | |||
46 | if ($em->getConnection()->getSchemaManager()->tablesExist(['sfu_post'])) { |
||
47 | return; |
||
48 | } |
||
49 | |||
50 | $schemaTool = new SchemaTool($em); |
||
51 | $schemaTool->createSchema( |
||
52 | [ |
||
53 | $em->getClassMetadata(CarePost::class), |
||
54 | ] |
||
55 | ); |
||
56 | } |
||
57 | |||
58 | public function uninstall() |
||
59 | { |
||
60 | $em = Database::getManager(); |
||
61 | |||
62 | if (!$em->getConnection()->getSchemaManager()->tablesExist(['sfu_post'])) { |
||
63 | return; |
||
64 | } |
||
65 | |||
66 | $schemaTool = new SchemaTool($em); |
||
67 | $schemaTool->dropSchema( |
||
68 | [ |
||
69 | $em->getClassMetadata(CarePost::class), |
||
70 | ] |
||
71 | ); |
||
72 | } |
||
73 | |||
74 | /** |
||
75 | * @param int $studentId |
||
76 | * @param int $currentUserId |
||
77 | * |
||
78 | * @return array |
||
79 | */ |
||
80 | public static function getPermissions($studentId, $currentUserId) |
||
81 | { |
||
82 | $installed = AppPlugin::getInstance()->isInstalled('studentfollowup'); |
||
83 | if ($installed === false) { |
||
84 | return [ |
||
85 | 'is_allow' => false, |
||
86 | 'show_private' => false, |
||
87 | ]; |
||
88 | } |
||
89 | |||
90 | if ($studentId === $currentUserId) { |
||
91 | $isAllow = true; |
||
92 | $showPrivate = true; |
||
93 | } else { |
||
94 | $isDrh = api_is_drh(); |
||
95 | $isDrhRelatedViaPost = false; |
||
96 | $isCourseCoach = false; |
||
97 | $isDrhRelatedToSession = false; |
||
98 | |||
99 | // Only admins and DRH that follow the user. |
||
100 | $isAdmin = api_is_platform_admin(); |
||
101 | |||
102 | // Check if user is care taker. |
||
103 | if ($isDrh) { |
||
104 | $criteria = [ |
||
105 | 'user' => $studentId, |
||
106 | 'insertUser' => $currentUserId, |
||
107 | ]; |
||
108 | $repo = Database::getManager()->getRepository('ChamiloPluginBundle:StudentFollowUp\CarePost'); |
||
109 | $post = $repo->findOneBy($criteria); |
||
110 | if ($post) { |
||
111 | $isDrhRelatedViaPost = true; |
||
112 | } |
||
113 | } |
||
114 | |||
115 | // Student sessions. |
||
116 | $sessions = SessionManager::get_sessions_by_user($studentId, true, true); |
||
117 | if (!empty($sessions)) { |
||
118 | foreach ($sessions as $session) { |
||
119 | $sessionId = $session['session_id']; |
||
120 | // Check if the current user is following that session. |
||
121 | $sessionDrhInfo = SessionManager::getSessionFollowedByDrh( |
||
122 | $currentUserId, |
||
123 | $sessionId |
||
124 | ); |
||
125 | if (!empty($sessionDrhInfo)) { |
||
126 | $isDrhRelatedToSession = true; |
||
127 | break; |
||
128 | } |
||
129 | |||
130 | // Check if teacher is coach between the date limits. |
||
131 | $visibility = api_get_session_visibility( |
||
132 | $sessionId, |
||
133 | null, |
||
134 | true, |
||
135 | $currentUserId |
||
136 | ); |
||
137 | |||
138 | if (SESSION_AVAILABLE === $visibility && isset($session['courses']) && !empty($session['courses'])) { |
||
139 | foreach ($session['courses'] as $course) { |
||
140 | $coachList = SessionManager::getCoachesByCourseSession( |
||
141 | $sessionId, |
||
142 | $course['real_id'] |
||
143 | ); |
||
144 | if (!empty($coachList) && in_array($currentUserId, $coachList)) { |
||
145 | $isCourseCoach = true; |
||
146 | break 2; |
||
147 | } |
||
148 | } |
||
149 | } |
||
150 | } |
||
151 | } |
||
152 | |||
153 | $isCareTaker = $isDrhRelatedViaPost && $isDrhRelatedToSession; |
||
154 | $isAllow = $isAdmin || $isCareTaker || $isDrhRelatedToSession || $isCourseCoach; |
||
155 | $showPrivate = $isAdmin || $isCareTaker; |
||
156 | } |
||
157 | |||
158 | return [ |
||
159 | 'is_allow' => $isAllow, |
||
160 | 'show_private' => $showPrivate, |
||
161 | ]; |
||
162 | } |
||
163 | |||
164 | /** |
||
165 | * @param string $status |
||
166 | * @param int $currentUserId |
||
167 | * @param int $sessionId |
||
168 | * @param int $start |
||
169 | * @param int $limit |
||
170 | * |
||
171 | * @return array |
||
172 | */ |
||
173 | public static function getUsers($status, $currentUserId, $sessionId, $start, $limit) |
||
174 | { |
||
175 | $sessions = []; |
||
176 | $courses = []; |
||
177 | $sessionsFull = []; |
||
178 | |||
179 | switch ($status) { |
||
180 | case COURSEMANAGER: |
||
181 | $sessionsFull = SessionManager::getSessionsCoachedByUser($currentUserId); |
||
182 | $sessions = array_column($sessionsFull, 'id'); |
||
183 | if (!empty($sessionId)) { |
||
184 | $sessions = [$sessionId]; |
||
185 | } |
||
186 | // Get session courses where I'm coach |
||
187 | $courseList = SessionManager::getCoursesListByCourseCoach($currentUserId); |
||
188 | $courses = []; |
||
189 | /** @var SessionRelCourseRelUser $courseItem */ |
||
190 | foreach ($courseList as $courseItem) { |
||
191 | $courses[] = $courseItem->getCourse()->getId(); |
||
192 | } |
||
193 | break; |
||
194 | case DRH: |
||
195 | $sessionsFull = SessionManager::get_sessions_followed_by_drh($currentUserId); |
||
196 | $sessions = array_column($sessionsFull, 'id'); |
||
197 | |||
198 | if (!empty($sessionId)) { |
||
199 | $sessions = [$sessionId]; |
||
200 | } |
||
201 | $courses = []; |
||
202 | foreach ($sessions as $sessionId) { |
||
0 ignored issues
–
show
introduced
by
![]() |
|||
203 | $sessionDrhInfo = SessionManager::getSessionFollowedByDrh( |
||
204 | $currentUserId, |
||
205 | $sessionId |
||
206 | ); |
||
207 | if ($sessionDrhInfo && isset($sessionDrhInfo['course_list'])) { |
||
0 ignored issues
–
show
The expression
$sessionDrhInfo of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent. Consider making the comparison explicit by using ![]() |
|||
208 | $courses = array_merge($courses, array_column($sessionDrhInfo['course_list'], 'id')); |
||
209 | } |
||
210 | } |
||
211 | break; |
||
212 | } |
||
213 | |||
214 | $userList = SessionManager::getUsersByCourseAndSessionList( |
||
215 | $sessions, |
||
216 | $courses, |
||
217 | $start, |
||
218 | $limit |
||
219 | ); |
||
220 | |||
221 | return [ |
||
222 | 'users' => $userList, |
||
223 | 'sessions' => $sessionsFull, |
||
224 | ]; |
||
225 | } |
||
226 | |||
227 | /** |
||
228 | * @return int |
||
229 | */ |
||
230 | public static function getPageSize() |
||
231 | { |
||
232 | return 20; |
||
233 | } |
||
234 | |||
235 | /** |
||
236 | * @param int $userId |
||
237 | */ |
||
238 | public function doWhenDeletingUser($userId) |
||
239 | { |
||
240 | $userId = (int) $userId; |
||
241 | |||
242 | Database::query("DELETE FROM sfu_post WHERE user_id = $userId"); |
||
243 | Database::query("DELETE FROM sfu_post WHERE insert_user_id = $userId"); |
||
244 | } |
||
245 | } |
||
246 |