1 | <?php |
||
2 | |||
3 | /* For licensing terms, see /license.txt */ |
||
4 | |||
5 | /** |
||
6 | * Entry point for REST web services in Chamilo. |
||
7 | * |
||
8 | * Call it with the 'authenticate' action first, to get an api_key, then use |
||
9 | * the api_key in all subsequent calls. |
||
10 | * |
||
11 | * Send the REST call parameters as a 'hash' in POST or GET. The hash must be |
||
12 | * JSON encoded and contain at least 'action', 'username', and either |
||
13 | * 'password' for the first call or 'api_key' in subsequent calls. |
||
14 | * You can store the API key on an external system (it will remain the same), |
||
15 | * although it is not recommended to do so (for security reasons). |
||
16 | */ |
||
17 | |||
18 | use Symfony\Component\HttpFoundation\Request as HttpRequest; |
||
0 ignored issues
–
show
|
|||
19 | |||
20 | require_once __DIR__.'/../../inc/global.inc.php'; |
||
21 | |||
22 | api_protect_webservices(); |
||
23 | |||
24 | $httpRequest = HttpRequest::createFromGlobals(); |
||
25 | |||
26 | $hash = $httpRequest->query->get('hash'); |
||
27 | |||
28 | if ($hash) { |
||
29 | $hashParams = Rest::decodeParams($hash); |
||
30 | if (!empty($hashParams)) { |
||
31 | foreach ($hashParams as $key => $value) { |
||
32 | $httpRequest->query->set($key, Security::remove_XSS($value)); |
||
33 | } |
||
34 | } |
||
35 | } |
||
36 | |||
37 | $action = $httpRequest->query->get('action') ?: $httpRequest->request->get('action'); |
||
38 | $username = Security::remove_XSS( |
||
39 | $httpRequest->query->get('username') ?: $httpRequest->request->get('username') |
||
40 | ); |
||
41 | $apiKey = Security::remove_XSS( |
||
42 | $httpRequest->query->get('api_key') ?: $httpRequest->request->get('api_key') |
||
43 | ); |
||
44 | $course = $httpRequest->query->getInt('course') ?: $httpRequest->request->getInt('course'); |
||
45 | $session = $httpRequest->query->getInt('session') ?: $httpRequest->request->getInt('session'); |
||
46 | |||
47 | $restResponse = new RestResponse(); |
||
48 | |||
49 | try { |
||
50 | /** @var Rest $restApi */ |
||
51 | $restApi = $apiKey ? Rest::validate($username, $apiKey) : null; |
||
52 | |||
53 | if ($restApi) { |
||
54 | LoginCheck($restApi->getUser()->getId()); |
||
55 | Tracking::updateUserLastLogin($restApi->getUser()->getId()); |
||
56 | |||
57 | $restApi->setCourse($course); |
||
58 | $restApi->setSession($session); |
||
59 | |||
60 | if ($course) { |
||
61 | Event::accessCourse(); |
||
62 | Event::eventCourseLoginUpdate(api_get_course_int_id(), api_get_user_id(), api_get_session_id()); |
||
63 | } |
||
64 | } |
||
65 | |||
66 | switch ($action) { |
||
67 | case Rest::GET_AUTH: |
||
68 | Rest::init(); |
||
69 | |||
70 | $password = $_POST['password'] ?? null; |
||
71 | $isValid = Rest::isValidUser($username, $password); |
||
72 | if (!$isValid) { |
||
73 | throw new Exception(get_lang('InvalideUserDetected')); |
||
74 | } |
||
75 | $userId = UserManager::get_user_id_from_username($username); |
||
76 | Event::addEvent(LOG_WS.$action, 'username', $username, null, $userId); |
||
77 | $restResponse->setData([ |
||
78 | 'url' => api_get_path(WEB_PATH), |
||
79 | 'apiKey' => Rest::findUserApiKey($username, Rest::SERVICE_NAME), |
||
80 | 'gcmSenderId' => api_get_setting('messaging_gdc_project_number'), |
||
81 | ]); |
||
82 | break; |
||
83 | case Rest::SAVE_GCM_ID: |
||
84 | $gcmId = isset($_POST['registration_id']) ? Security::remove_XSS($_POST['registration_id']) : null; |
||
85 | Event::addEvent(LOG_WS.$action, 'gcm_id', $gcmId); |
||
86 | $restApi->setGcmId($gcmId); |
||
87 | $restResponse->setData(['status' => true]); |
||
88 | break; |
||
89 | case Rest::LOGOUT: |
||
90 | Event::addEvent(LOG_WS.$action, 'username', $username); |
||
91 | $restApi->logout(); |
||
92 | $restResponse->setData(['status' => true]); |
||
93 | break; |
||
94 | case Rest::GET_USER_MESSAGES: |
||
95 | $lastMessageId = isset($_POST['last']) ? (int) $_POST['last'] : 0; |
||
96 | $messages = $restApi->getUserMessages($lastMessageId); |
||
97 | Event::addEvent(LOG_WS.$action, 'last_message_id', $lastMessageId); |
||
98 | $restResponse->setData($messages); |
||
99 | break; |
||
100 | case Rest::GET_USER_MESSAGES_RECEIVED: |
||
101 | Event::addEvent(LOG_WS.$action, 'username', $username); |
||
102 | $messages = $restApi->getUserReceivedMessages(); |
||
103 | $restResponse->setData($messages); |
||
104 | break; |
||
105 | case Rest::DELETE_USER_MESSAGE: |
||
106 | $messageId = isset($_POST['message_id']) ? (int) $_POST['message_id'] : 0; |
||
107 | $messageType = !empty($_POST['msg_type']) ? $_POST['msg_type'] : ''; |
||
108 | Event::addEvent(LOG_WS.$action, 'message_id', $messageId); |
||
109 | $restApi->deleteUserMessage($messageId, $messageType); |
||
110 | $restResponse->setData(['status' => true]); |
||
111 | break; |
||
112 | case Rest::GET_USER_MESSAGES_SENT: |
||
113 | Event::addEvent(LOG_WS.$action, 'username', $username); |
||
114 | $messages = $restApi->getUserSentMessages(); |
||
115 | $restResponse->setData($messages); |
||
116 | break; |
||
117 | case Rest::GET_COUNT_NEW_MESSAGES: |
||
118 | Event::addEvent(LOG_WS.$action, 'username', $username); |
||
119 | $restResponse->setData( |
||
120 | MessageManager::getMessagesCountForUser($restApi->getUser()->getId()) |
||
121 | ); |
||
122 | break; |
||
123 | case Rest::SET_MESSAGE_READ: |
||
124 | $messageId = isset($_POST['message_id']) ? (int) $_POST['message_id'] : 0; |
||
125 | $restApi->setMessageRead($messageId); |
||
126 | Event::addEvent(LOG_WS.$action, 'message_id', $messageId); |
||
127 | $restResponse->setData(['status' => true]); |
||
128 | break; |
||
129 | case Rest::POST_USER_MESSAGE_READ: |
||
130 | case Rest::POST_USER_MESSAGE_UNREAD: |
||
131 | $messagesId = isset($_POST['messages']) && is_array($_POST['messages']) |
||
132 | ? array_map('intval', $_POST['messages']) |
||
133 | : []; |
||
134 | |||
135 | $messagesId = array_filter($messagesId); |
||
136 | if (empty($messagesId)) { |
||
137 | throw new Exception(get_lang('NoData')); |
||
138 | } |
||
139 | $messageStatus = $action === Rest::POST_USER_MESSAGE_READ ? MESSAGE_STATUS_NEW : MESSAGE_STATUS_UNREAD; |
||
140 | $data = array_flip($messagesId); |
||
141 | |||
142 | foreach ($messagesId as $messageId) { |
||
143 | $data[$messageId] = MessageManager::update_message_status( |
||
144 | $restApi->getUser()->getId(), |
||
145 | $messageId, |
||
146 | $messageStatus |
||
147 | ); |
||
148 | } |
||
149 | Event::addEvent(LOG_WS.$action, 'messages_id', implode(',', $messagesId)); |
||
150 | |||
151 | $restResponse->setData($data); |
||
152 | break; |
||
153 | case Rest::SAVE_USER_MESSAGE: |
||
154 | $receivers = $_POST['receivers'] ?? []; |
||
155 | $subject = !empty($_POST['subject']) ? $_POST['subject'] : null; |
||
156 | $text = !empty($_POST['text']) ? $_POST['text'] : null; |
||
157 | if (!empty($_POST['only_local']) && ('false' != $_POST['only_local'])) { |
||
158 | $only_local = true; |
||
159 | } else { |
||
160 | $only_local = false; |
||
161 | } |
||
162 | $data = $restApi->saveUserMessage($subject, $text, $receivers, $only_local); |
||
163 | Event::addEvent(LOG_WS.$action, 'username', $username); |
||
164 | $restResponse->setData($data); |
||
165 | break; |
||
166 | case Rest::GET_MESSAGE_USERS: |
||
167 | $search = !empty($_REQUEST['q']) ? $_REQUEST['q'] : null; |
||
168 | if (!$search || strlen($search) < 2) { |
||
169 | throw new Exception(get_lang('TooShort')); |
||
170 | } |
||
171 | Event::addEvent(LOG_WS.$action, 'filter_search', $search); |
||
172 | $data = $restApi->getMessageUsers($search); |
||
173 | $restResponse->setData($data); |
||
174 | break; |
||
175 | case Rest::VIEW_MESSAGE: |
||
176 | $messageId = isset($_GET['message']) ? (int) $_GET['message'] : 0; |
||
177 | Event::addEvent(LOG_WS.$action, 'message_id', $messageId); |
||
178 | $restApi->viewMessage($messageId); |
||
179 | break; |
||
180 | case Rest::GET_USER_COURSES: |
||
181 | $userId = isset($_REQUEST['user_id']) ? (int) $_REQUEST['user_id'] : 0; |
||
182 | Event::addEvent(LOG_WS.$action, 'username', $username); |
||
183 | $courses = $restApi->getUserCourses($userId); |
||
184 | $restResponse->setData($courses); |
||
185 | break; |
||
186 | case Rest::GET_USER_SESSIONS: |
||
187 | Event::addEvent(LOG_WS.$action, 'username', $username); |
||
188 | $courses = $restApi->getUserSessions(); |
||
189 | $restResponse->setData($courses); |
||
190 | break; |
||
191 | case Rest::VIEW_PROFILE: |
||
192 | $userId = isset($_GET['user_id']) ? (int) $_GET['user_id'] : 0; |
||
193 | Event::addEvent(LOG_WS.$action, 'user_id', $userId); |
||
194 | $restApi->viewUserProfile($userId); |
||
195 | break; |
||
196 | case Rest::GET_PROFILE: |
||
197 | Event::addEvent(LOG_WS.$action, 'username', $username); |
||
198 | $userInfo = $restApi->getUserProfile(); |
||
199 | $restResponse->setData($userInfo); |
||
200 | break; |
||
201 | case Rest::GET_PROFILES_BY_EXTRA_FIELD: |
||
202 | Event::addEvent(LOG_WS.$action, 'extra_field_name', $_POST['field_name']); |
||
203 | $active = !empty($_POST['active']) && $_POST['active'] == 1 ? 1 : 0; |
||
204 | // If "active" is set, will drop inactive users (user.active = 0) from the response |
||
205 | $users = $restApi->getUsersProfilesByExtraField($_POST['field_name'], $_POST['field_value'], $active); |
||
206 | $restResponse->setData($users); |
||
207 | break; |
||
208 | case Rest::GET_COURSES_DETAILS_BY_EXTRA_FIELD: |
||
209 | Event::addEvent(LOG_WS.$action, 'extra_field_name', $_POST['field_name']); |
||
210 | $courses = $restApi->getCoursesByExtraField($_POST['field_name'], $_POST['field_value']); |
||
211 | $restResponse->setData($courses); |
||
212 | break; |
||
213 | case Rest::GET_USER_COURSES_BY_DATES: |
||
214 | Event::addEvent(LOG_WS.$action, 'user_id', (int) $_POST['user_id']); |
||
215 | $courses = $restApi->getUserCoursesByDates($_POST['user_id'], $_POST['start_date'], $_POST['end_date']); |
||
216 | $restResponse->setData($courses); |
||
217 | break; |
||
218 | case Rest::VIEW_MY_COURSES: |
||
219 | Event::addEvent(LOG_WS.$action, 'username', $username); |
||
220 | $restApi->viewMyCourses(); |
||
221 | break; |
||
222 | case Rest::VIEW_COURSE_HOME: |
||
223 | Event::addEvent(LOG_WS.$action, 'username', $username); |
||
224 | $restApi->viewCourseHome(); |
||
225 | break; |
||
226 | case Rest::GET_COURSE_INFO: |
||
227 | Event::addEvent(LOG_WS.$action, 'course_id', (int) $_POST['course']); |
||
228 | $courseInfo = $restApi->getCourseInfo(); |
||
229 | $restResponse->setData($courseInfo); |
||
230 | break; |
||
231 | case Rest::GET_COURSE_DESCRIPTIONS: |
||
232 | Event::addEvent(LOG_WS.$action, 'course_id', (int) $_POST['course']); |
||
233 | $fields = $_POST['fields'] ?? []; |
||
234 | $descriptions = $restApi->getCourseDescriptions($fields); |
||
235 | $restResponse->setData($descriptions); |
||
236 | break; |
||
237 | case Rest::GET_COURSE_DOCUMENTS: |
||
238 | $directoryId = isset($_POST['dir_id']) ? Security::remove_XSS($_POST['dir_id']) : null; |
||
239 | Event::addEvent(LOG_WS.$action, 'directory_id', $directoryId); |
||
240 | $documents = $restApi->getCourseDocuments($directoryId); |
||
241 | $restResponse->setData($documents); |
||
242 | break; |
||
243 | case Rest::GET_COURSE_ANNOUNCEMENTS: |
||
244 | Event::addEvent(LOG_WS.$action, 'course_id', (int) $_POST['course']); |
||
245 | $announcements = $restApi->getCourseAnnouncements(); |
||
246 | $restResponse->setData($announcements); |
||
247 | break; |
||
248 | case Rest::GET_COURSE_ANNOUNCEMENT: |
||
249 | $announcementId = isset($_POST['announcement']) ? Security::remove_XSS($_POST['announcement']) : 0; |
||
250 | Event::addEvent(LOG_WS.$action, 'announcement_id', $announcementId); |
||
251 | $announcement = $restApi->getCourseAnnouncement($announcementId); |
||
252 | $restResponse->setData($announcement); |
||
253 | break; |
||
254 | case Rest::GET_COURSE_AGENDA: |
||
255 | Event::addEvent(LOG_WS.$action, 'course_id', (int) $_POST['course']); |
||
256 | $agenda = $restApi->getCourseAgenda(); |
||
257 | $restResponse->setData($agenda); |
||
258 | break; |
||
259 | case Rest::GET_COURSE_NOTEBOOKS: |
||
260 | Event::addEvent(LOG_WS.$action, 'course_id', (int) $_POST['course']); |
||
261 | $notebooks = $restApi->getCourseNotebooks(); |
||
262 | $restResponse->setData($notebooks); |
||
263 | break; |
||
264 | case Rest::GET_COURSE_FORUM_CATEGORIES: |
||
265 | Event::addEvent(LOG_WS.$action, 'course_id', (int) $_POST['course']); |
||
266 | $forums = $restApi->getCourseForumCategories(); |
||
267 | $restResponse->setData($forums); |
||
268 | break; |
||
269 | case Rest::GET_COURSE_FORUM: |
||
270 | $forumId = isset($_POST['forum']) ? Security::remove_XSS($_POST['forum']) : 0; |
||
271 | Event::addEvent(LOG_WS.$action, 'course_id-forum_id', (int) $_POST['forum'].':'.$forumId); |
||
272 | $forum = $restApi->getCourseForum($forumId); |
||
273 | $restResponse->setData($forum); |
||
274 | break; |
||
275 | case Rest::GET_COURSE_FORUM_THREAD: |
||
276 | $forumId = isset($_POST['forum']) ? (int) $_POST['forum'] : 0; |
||
277 | $threadId = isset($_POST['thread']) ? (int) $_POST['thread'] : 0; |
||
278 | Event::addEvent( |
||
279 | LOG_WS.$action, |
||
280 | 'course_id-forum_id-thread_id', |
||
281 | (int) $_POST['course'].':'.$forumId.':'.$threadId |
||
282 | ); |
||
283 | |||
284 | $thread = $restApi->getCourseForumThread($forumId, $threadId); |
||
285 | $restResponse->setData($thread); |
||
286 | break; |
||
287 | case Rest::GET_COURSE_LEARNPATHS: |
||
288 | Event::addEvent(LOG_WS.$action, 'username', $username); |
||
289 | $data = $restApi->getCourseLearnPaths(); |
||
290 | $restResponse->setData($data); |
||
291 | break; |
||
292 | case Rest::GET_COURSE_LEARNPATH: |
||
293 | $lpId = isset($_REQUEST['lp_id']) ? (int) $_REQUEST['lp_id'] : 1; |
||
294 | Event::addEvent(LOG_WS.$action, 'lp_id', $lpId); |
||
295 | $restApi->showLearningPath($lpId); |
||
296 | break; |
||
297 | case Rest::GET_COURSE_LP_PROGRESS: |
||
298 | Event::addEvent(LOG_WS.$action, 'username', $username); |
||
299 | $restResponse->setData($restApi->getCourseLpProgress()); |
||
300 | break; |
||
301 | case Rest::GET_COURSE_LINKS: |
||
302 | Event::addEvent(LOG_WS.$action, 'username', $username); |
||
303 | $restResponse->setData( |
||
304 | $restApi->getCourseLinks() |
||
305 | ); |
||
306 | break; |
||
307 | case Rest::GET_COURSE_WORKS: |
||
308 | Event::addEvent(LOG_WS.$action, 'username', $username); |
||
309 | $restResponse->setData( |
||
310 | $restApi->getCourseWorks() |
||
311 | ); |
||
312 | break; |
||
313 | case Rest::GET_COURSE_EXERCISES: |
||
314 | Event::addEvent(LOG_WS.$action, 'course_id', (int) $_POST['course']); |
||
315 | $fields = $_POST['fields'] ?? []; |
||
316 | $restResponse->setData( |
||
317 | $restApi->getCourseExercises($fields) |
||
318 | ); |
||
319 | break; |
||
320 | case Rest::SAVE_COURSE_NOTEBOOK: |
||
321 | $title = !empty($_POST['title']) ? $_POST['title'] : null; |
||
322 | $text = !empty($_POST['text']) ? $_POST['text'] : null; |
||
323 | $data = $restApi->saveCourseNotebook($title, $text); |
||
324 | Event::addEvent(LOG_WS.$action, 'notebook_id', $data['registered']); |
||
325 | $restResponse->setData($data); |
||
326 | break; |
||
327 | case Rest::SAVE_FORUM_POST: |
||
328 | if ( |
||
329 | empty($_POST['title']) || empty($_POST['text']) || empty($_POST['thread']) || empty($_POST['forum']) |
||
330 | ) { |
||
331 | throw new Exception(get_lang('NoData')); |
||
332 | } |
||
333 | |||
334 | $forumId = $httpRequest->request->getInt('forum'); |
||
335 | $notify = $httpRequest->request->has('notify'); |
||
336 | $parentId = $httpRequest->request->getInt('parent') ?: null; |
||
337 | |||
338 | $postValues = [ |
||
339 | 'post_title' => $_POST['title'], |
||
340 | 'post_text' => nl2br($_POST['text']), |
||
341 | 'thread_id' => $_POST['thread'], |
||
342 | 'forum_id' => $_POST['forum'], |
||
343 | 'post_notification' => $notify, |
||
344 | 'post_parent_id' => $parentId, |
||
345 | ]; |
||
346 | |||
347 | $data = $restApi->saveForumPost($postValues, $forumId); |
||
348 | Event::addEvent(LOG_WS.$action, 'registered', $data['registered']); |
||
349 | $restResponse->setData($data); |
||
350 | break; |
||
351 | case Rest::SAVE_FORUM_THREAD: |
||
352 | if (empty($_POST['title']) || empty($_POST['text']) || empty($_POST['forum'])) { |
||
353 | throw new Exception(get_lang('NoData')); |
||
354 | } |
||
355 | |||
356 | $forumId = $httpRequest->request->getInt('forum'); |
||
357 | $notify = !empty($_POST['notify']); |
||
358 | $threadInfo = [ |
||
359 | 'post_title' => $_POST['title'], |
||
360 | 'forum_id' => $_POST['forum'], |
||
361 | 'post_text' => nl2br($_POST['text']), |
||
362 | 'post_notification' => $notify, |
||
363 | ]; |
||
364 | |||
365 | $data = $restApi->saveForumThread($threadInfo, $forumId); |
||
366 | Event::addEvent(LOG_WS.$action, 'registered', $data['registered']); |
||
367 | $restResponse->setData($data); |
||
368 | break; |
||
369 | case Rest::SET_THREAD_NOTIFY: |
||
370 | $threadId = isset($_POST['thread']) ? (int) $_POST['thread'] : 0; |
||
371 | |||
372 | if (empty($threadId)) { |
||
373 | throw new Exception(get_lang('NoData')); |
||
374 | } |
||
375 | |||
376 | $restResponse->setData( |
||
377 | [ |
||
378 | 'message' => $restApi->setThreadNotify($threadId), |
||
379 | ] |
||
380 | ); |
||
381 | Event::addEvent(LOG_WS.$action, 'thread_id', $threadId); |
||
382 | break; |
||
383 | case Rest::DOWNLOAD_FORUM_ATTACHMENT: |
||
384 | if (empty($_GET['path'])) { |
||
385 | throw new Exception(get_lang('ActionNotAllowed')); |
||
386 | } |
||
387 | Event::addEvent(LOG_WS.$action, 'path', $_GET['path']); |
||
388 | $restApi->downloadForumPostAttachment($_GET['path']); |
||
389 | break; |
||
390 | case Rest::GET_WORK_LIST: |
||
391 | if (!isset($_GET['work'])) { |
||
392 | throw new Exception(get_lang('ActionNotAllowed')); |
||
393 | } |
||
394 | $workId = (int) $_GET['work']; |
||
395 | Event::addEvent(LOG_WS.$action, 'work_id', $workId); |
||
396 | $restResponse->setData( |
||
397 | $restApi->getWorkList($workId) |
||
398 | ); |
||
399 | break; |
||
400 | case Rest::GET_WORK_STUDENTS_WITHOUT_PUBLICATIONS: |
||
401 | if (!isset($_GET['work'])) { |
||
402 | throw new Exception(get_lang('ActionNotAllowed')); |
||
403 | } |
||
404 | |||
405 | if (!api_is_allowed_to_edit(false, true)) { |
||
406 | throw new Exception(get_lang('NotAllowed')); |
||
407 | } |
||
408 | $workId = (int) $_GET['work']; |
||
409 | Event::addEvent(LOG_WS.$action, 'work_id', $workId); |
||
410 | $restResponse->setData( |
||
411 | $restApi->getWorkStudentsWithoutPublications($workId) |
||
412 | ); |
||
413 | break; |
||
414 | case Rest::GET_WORK_USERS: |
||
415 | if (!isset($_GET['work'])) { |
||
416 | throw new Exception(get_lang('ActionNotAllowed')); |
||
417 | } |
||
418 | |||
419 | if (!api_is_allowed_to_edit()) { |
||
420 | throw new Exception(get_lang('NotAllowed')); |
||
421 | } |
||
422 | $workId = (int) $_GET['work']; |
||
423 | Event::addEvent(LOG_WS.$action, 'work_id', $workId); |
||
424 | $restResponse->setData( |
||
425 | $restApi->getWorkUsers($workId) |
||
426 | ); |
||
427 | break; |
||
428 | case Rest::GET_WORK_STUDENT_LIST: |
||
429 | if (!isset($_GET['work'])) { |
||
430 | throw new Exception(get_lang('ActionNotAllowed')); |
||
431 | } |
||
432 | |||
433 | $workId = (int) $_GET['work']; |
||
434 | |||
435 | if (!api_is_allowed_to_edit()) { |
||
436 | throw new Exception(get_lang('NotAllowed')); |
||
437 | } |
||
438 | |||
439 | Event::addEvent(LOG_WS.$action, 'work_id', $workId); |
||
440 | $restResponse->setData( |
||
441 | $restApi->getWorkStudentList($workId) |
||
442 | ); |
||
443 | break; |
||
444 | case Rest::PUT_WORK_STUDENT_ITEM_VISIBILITY: |
||
445 | if (!isset($_POST['status'], $_POST['work'])) { |
||
446 | throw new Exception(get_lang('ActionNotAllowed')); |
||
447 | } |
||
448 | |||
449 | $workId = (int) $_POST['work']; |
||
450 | |||
451 | if (!api_is_allowed_to_edit() && !api_is_coach()) { |
||
452 | throw new Exception(get_lang('NotAllowed')); |
||
453 | } |
||
454 | |||
455 | $data = $restApi->putCourseWorkVisibility( |
||
456 | $workId, |
||
457 | (int) $_POST['status'] |
||
458 | ); |
||
459 | |||
460 | Event::addEvent(LOG_WS.$action, 'work_id', $workId); |
||
461 | $restResponse->setData(['status' => $data]); |
||
462 | break; |
||
463 | case Rest::DELETE_WORK_STUDENT_ITEM: |
||
464 | if (!isset($_POST['work'])) { |
||
465 | throw new Exception(get_lang('ActionNotAllowed')); |
||
466 | } |
||
467 | |||
468 | if (!api_is_allowed_to_edit() && !api_is_coach()) { |
||
469 | throw new Exception(get_lang('NotAllowed')); |
||
470 | } |
||
471 | $workId = (int) $_POST['work']; |
||
472 | Event::addEvent(LOG_WS.$action, 'work_id', $workId); |
||
473 | $restResponse->setData( |
||
474 | [ |
||
475 | 'message' => $restApi->deleteWorkStudentItem($workId), |
||
476 | ] |
||
477 | ); |
||
478 | break; |
||
479 | case Rest::DELETE_WORK_CORRECTIONS: |
||
480 | if (!isset($_POST['work'])) { |
||
481 | throw new Exception(get_lang('ActionNotAllowed')); |
||
482 | } |
||
483 | |||
484 | if (!api_is_allowed_to_edit() && !api_is_coach()) { |
||
485 | throw new Exception(get_lang('NotAllowed')); |
||
486 | } |
||
487 | |||
488 | $workId = (int) $_POST['work']; |
||
489 | Event::addEvent(LOG_WS.$action, 'work_id', $workId); |
||
490 | $restResponse->setData( |
||
491 | [ |
||
492 | 'message' => $restApi->deleteWorkCorrections($workId), |
||
493 | ] |
||
494 | ); |
||
495 | break; |
||
496 | case Rest::DOWNLOAD_WORK_FOLDER: |
||
497 | if (!isset($_GET['work'])) { |
||
498 | throw new Exception(get_lang('ActionNotAllowed')); |
||
499 | } |
||
500 | |||
501 | $workId = (int) $_GET['work']; |
||
502 | Event::addEvent(LOG_WS.$action, 'work_id', $workId); |
||
503 | $restApi->downloadWorkFolder($workId); |
||
504 | break; |
||
505 | case Rest::DOWNLOAD_WORK_COMMENT_ATTACHMENT: |
||
506 | if (!isset($_GET['comment'])) { |
||
507 | throw new Exception(get_lang('ActionNotAllowed')); |
||
508 | } |
||
509 | |||
510 | Event::addEvent(LOG_WS.$action, 'comment_id', (int) $_GET['comment']); |
||
511 | $restApi->downloadWorkCommentAttachment((int) $_GET['comment']); |
||
512 | break; |
||
513 | case Rest::DOWNLOAD_WORK: |
||
514 | if (!isset($_GET['work'])) { |
||
515 | throw new Exception(get_lang('ActionNotAllowed')); |
||
516 | } |
||
517 | |||
518 | $isCorrection = isset($_GET['correction']); |
||
519 | $workId = (int) $_GET['work']; |
||
520 | Event::addEvent(LOG_WS.$action, 'work_id', $workId); |
||
521 | $restApi->downloadWork($workId, $isCorrection); |
||
522 | break; |
||
523 | case Rest::VIEW_DOCUMENT_IN_FRAME: |
||
524 | $lpId = isset($_REQUEST['document']) ? (int) $_REQUEST['document'] : 0; |
||
525 | Event::addEvent(LOG_WS.$action, 'document_id', $lpId); |
||
526 | $restApi->viewDocumentInFrame($lpId); |
||
527 | break; |
||
528 | case Rest::VIEW_QUIZ_TOOL: |
||
529 | Event::addEvent(LOG_WS.$action, 'username', $username); |
||
530 | $restApi->viewQuizTool(); |
||
531 | break; |
||
532 | case Rest::VIEW_SURVEY_TOOL: |
||
533 | Event::addEvent(LOG_WS.$action, 'username', $username); |
||
534 | $restApi->viewSurveyTool(); |
||
535 | break; |
||
536 | case Rest::CREATE_CAMPUS: |
||
537 | $data = $restApi->createCampusURL($_POST); |
||
538 | Event::addEvent(LOG_WS.$action, 'campus_id', $data['id_campus']); |
||
539 | $restResponse->setData($data); |
||
540 | break; |
||
541 | case Rest::EDIT_CAMPUS: |
||
542 | $data = $restApi->editCampusURL($_POST); |
||
543 | Event::addEvent(LOG_WS.$action, 'campus_id', $_POST['id']); |
||
544 | $restResponse->setData($data); |
||
545 | break; |
||
546 | case Rest::DELETE_CAMPUS: |
||
547 | $data = $restApi->deleteCampusURL($_POST); |
||
548 | Event::addEvent(LOG_WS.$action, 'campus_id', $_POST['id']); |
||
549 | $restResponse->setData($data); |
||
550 | break; |
||
551 | case Rest::GET_USERS: |
||
552 | Event::addEvent(LOG_WS.$action, 'username', $username); |
||
553 | $data = $restApi->getUsersCampus($_POST); |
||
554 | $restResponse->setData($data); |
||
555 | break; |
||
556 | case Rest::GET_USER_INFO_FROM_USERNAME: |
||
557 | if (empty($_POST['loginname'])) { |
||
558 | throw new Exception(get_lang('NoData')); |
||
559 | } |
||
560 | $item = api_get_user_info_from_username($_POST['loginname']); |
||
561 | $userInfo = [ |
||
562 | 'id' => $item['user_id'], |
||
563 | 'firstname' => $item['firstname'], |
||
564 | 'lastname' => $item['lastname'], |
||
565 | 'email' => $item['email'], |
||
566 | 'username' => $item['username'], |
||
567 | 'active' => $item['active'], |
||
568 | ]; |
||
569 | Event::addEvent( |
||
570 | LOG_WS.$action, |
||
571 | 'username', |
||
572 | Database::escape_string($_POST['loginname']) |
||
573 | ); |
||
574 | $restResponse->setData($userInfo); |
||
575 | break; |
||
576 | case Rest::USERNAME_EXIST: |
||
577 | Event::addEvent(LOG_WS.$action, 'username', $_POST['loginname']); |
||
578 | $data = $restApi->usernameExist($_POST['loginname']); |
||
579 | $restResponse->setData([$data]); |
||
580 | break; |
||
581 | case Rest::SAVE_USER: |
||
582 | $data = $restApi->addUser($_POST); |
||
583 | Event::addEvent(LOG_WS.$action, 'user_id', $data); |
||
584 | $restResponse->setData($data); |
||
585 | break; |
||
586 | case Rest::SAVE_USER_GET_APIKEY: |
||
587 | $data = $restApi->addUserGetApikey($_POST); |
||
588 | Event::addEvent(LOG_WS.$action, 'user_id', $data['id']); |
||
589 | $restResponse->setData($data); |
||
590 | break; |
||
591 | case Rest::SAVE_USER_JSON: |
||
592 | if (!array_key_exists('json', $_POST)) { |
||
593 | throw new Exception(get_lang('NoData')); |
||
594 | } |
||
595 | $json = json_decode($_POST['json'], true); |
||
596 | if (is_null($json)) { |
||
597 | throw new Exception(get_lang('NoData')); |
||
598 | } |
||
599 | $data = $restApi->addUser($json); |
||
600 | Event::addEvent(LOG_WS.$action, 'user_id', $data); |
||
601 | $restResponse->setData($data); |
||
602 | break; |
||
603 | case Rest::UPDATE_USER_FROM_USERNAME: |
||
604 | $data = $restApi->updateUserFromUserName($_POST); |
||
605 | Event::addEvent(LOG_WS.$action, 'username', $username); |
||
606 | $restResponse->setData([$data]); |
||
607 | break; |
||
608 | case Rest::UPDATE_USER_APIKEY: |
||
609 | $userId = isset($_POST['user_id']) ? (int) $_POST['user_id'] : 0; |
||
610 | $currentApiKey = $_POST['current_api_key'] ?? ''; |
||
611 | |||
612 | if (empty($userId) || empty($currentApiKey)) { |
||
613 | throw new Exception(get_lang('NotAllowed')); |
||
614 | } |
||
615 | |||
616 | Event::addEvent(LOG_WS.$action, 'user_id', $userId); |
||
617 | $data = $restApi->updateUserApiKey($userId, $currentApiKey); |
||
618 | $restResponse->setData($data); |
||
619 | break; |
||
620 | case Rest::DELETE_USER: |
||
621 | if (!api_is_platform_admin()) { |
||
622 | throw new Exception(get_lang('NotAllowed')); |
||
623 | } |
||
624 | |||
625 | $result = UserManager::delete_user($_REQUEST['user_id']); |
||
626 | Event::addEvent(LOG_WS.$action, 'user_id', (int) $_REQUEST['user_id']); |
||
627 | $restResponse->setData(['status' => $result]); |
||
628 | break; |
||
629 | case Rest::GET_USERS_API_KEYS: |
||
630 | Event::addEvent(LOG_WS.$action, 'username', $username); |
||
631 | $restResponse->setData( |
||
632 | $restApi->getAllUsersApiKeys( |
||
633 | $httpRequest->query->getInt('page', 1), |
||
634 | $httpRequest->query->getInt('per_page', 30), |
||
635 | $httpRequest->query->getBoolean('force', false), |
||
636 | $httpRequest->query->getInt('url_id', 0) ?: null |
||
637 | ) |
||
638 | ); |
||
639 | break; |
||
640 | case Rest::GET_USER_API_KEY: |
||
641 | $username = (string) $httpRequest->query->get('user'); |
||
642 | |||
643 | if (empty($username)) { |
||
644 | throw new Exception(get_lang('NoData')); |
||
645 | } |
||
646 | |||
647 | Event::addEvent(LOG_WS.$action, 'username', $username); |
||
648 | $restResponse->setData( |
||
649 | $restApi->getUserApiKey( |
||
650 | $username, |
||
651 | $httpRequest->query->getBoolean('force', false) |
||
652 | ) |
||
653 | ); |
||
654 | break; |
||
655 | case Rest::GET_USER_LAST_CONNEXION: |
||
656 | $username = (string) $_REQUEST['user']; |
||
657 | |||
658 | if (empty($username)) { |
||
659 | throw new Exception(get_lang('NoData')); |
||
660 | } |
||
661 | |||
662 | Event::addEvent(LOG_WS.$action, 'username', $username); |
||
663 | $restResponse->setData( |
||
664 | $restApi->getUserLastConnexion( |
||
665 | $username |
||
666 | ) |
||
667 | ); |
||
668 | break; |
||
669 | case Rest::GET_USER_TOTAL_CONNEXION_TIME: |
||
670 | $username = (string) $_REQUEST['user']; |
||
671 | |||
672 | if (empty($username)) { |
||
673 | throw new Exception(get_lang('NoData')); |
||
674 | } |
||
675 | |||
676 | Event::addEvent(LOG_WS.$action, 'username', $username); |
||
677 | $restResponse->setData( |
||
678 | $restApi->getUserTotalConnexionTime( |
||
679 | $username |
||
680 | ) |
||
681 | ); |
||
682 | break; |
||
683 | case Rest::GET_USER_PROGRESS_AND_TIME_IN_SESSION: |
||
684 | $userId = (string) $_REQUEST['user_id']; |
||
685 | $sessionId = (string) $_REQUEST['session_id']; |
||
686 | |||
687 | if (empty($userId)) { |
||
688 | throw new Exception('user_id not provided'); |
||
689 | } |
||
690 | if (empty($sessionId)) { |
||
691 | throw new Exception('session_id not provided'); |
||
692 | } |
||
693 | |||
694 | Event::addEvent(LOG_WS.$action, 'user_id', $userId); |
||
695 | $restResponse->setData( |
||
696 | $restApi->getUserProgressAndTimeInSession( |
||
697 | $userId, |
||
698 | $sessionId |
||
699 | ) |
||
700 | ); |
||
701 | break; |
||
702 | case Rest::GET_USER_SUB_GROUP: |
||
703 | $userId = isset($_POST['user_id']) ? (int) $_POST['user_id'] : 0; |
||
704 | if (empty($userId)) { |
||
705 | throw new Exception('user_id not provided'); |
||
706 | } |
||
707 | |||
708 | Event::addEvent(LOG_WS.$action, 'user_id', $userId); |
||
709 | $data = $restApi->getUserSubGroup($userId); |
||
710 | $restResponse->setData($data); |
||
711 | break; |
||
712 | case Rest::GET_COURSES: |
||
713 | $campusId = api_get_current_access_url_id(); |
||
714 | if (!empty($_POST['id_campus'])) { |
||
715 | $campusId = (int) $_POST['id_campus']; |
||
716 | } |
||
717 | Event::addEvent(LOG_WS.$action, 'id_campus', $campusId); |
||
718 | $data = $restApi->getCoursesCampus($campusId); |
||
719 | $restResponse->setData($data); |
||
720 | break; |
||
721 | case Rest::GET_COURSES_FROM_EXTRA_FIELD: |
||
722 | $variable = $_REQUEST['extra_field_variable'] ?? ''; |
||
723 | $value = $_REQUEST['extra_field_value'] ?? ''; |
||
724 | $urlId = $_REQUEST['id_campus'] ?? ''; |
||
725 | $extraField = new ExtraField('course'); |
||
726 | $extraFieldInfo = $extraField->get_handler_field_info_by_field_variable($variable); |
||
727 | |||
728 | if (empty($extraFieldInfo)) { |
||
729 | throw new Exception("$variable not found"); |
||
730 | } |
||
731 | |||
732 | Event::addEvent( |
||
733 | LOG_WS.$action, |
||
734 | 'extra_field-extra_field_value', |
||
735 | Database::escape_string($variable).':'.Database::escape_string($value) |
||
736 | ); |
||
737 | $extraFieldValue = new ExtraFieldValue('course'); |
||
738 | $items = $extraFieldValue->get_item_id_from_field_variable_and_field_value( |
||
739 | $variable, |
||
740 | $value, |
||
741 | false, |
||
742 | false, |
||
743 | true |
||
744 | ); |
||
745 | |||
746 | $courseList = []; |
||
747 | foreach ($items as $item) { |
||
748 | $courseId = $item['item_id']; |
||
749 | if (UrlManager::relation_url_course_exist($courseId, $urlId)) { |
||
750 | $courseList[] = api_get_course_info_by_id($courseId); |
||
751 | } |
||
752 | } |
||
753 | |||
754 | $restResponse->setData($courseList); |
||
755 | break; |
||
756 | case Rest::SAVE_COURSE: |
||
757 | $data = $restApi->addCourse($_POST); |
||
758 | Event::addEvent(LOG_WS.$action, 'course_id', $data['id']); |
||
759 | $restResponse->setData($data); |
||
760 | break; |
||
761 | case Rest::DELETE_COURSE: |
||
762 | if (!api_is_platform_admin()) { |
||
763 | throw new Exception(get_lang('NotAllowed')); |
||
764 | } |
||
765 | |||
766 | $courseCode = $_REQUEST['course_code'] ?? ''; |
||
767 | $courseId = $_REQUEST['course_id'] ?? 0; |
||
768 | |||
769 | $course = []; |
||
770 | if (!empty($courseCode)) { |
||
771 | $course = api_get_course_info($courseCode); |
||
772 | } |
||
773 | |||
774 | if (empty($course) && !empty($courseId)) { |
||
775 | $course = api_get_course_info_by_id($courseId); |
||
776 | } |
||
777 | |||
778 | if (empty($course)) { |
||
779 | throw new Exception("Course doesn't exists"); |
||
780 | } |
||
781 | |||
782 | $result = CourseManager::delete_course($course['code']); |
||
783 | Event::addEvent(LOG_WS.$action, 'course_id', $courseId); |
||
784 | $restResponse->setData(['status' => $result]); |
||
785 | break; |
||
786 | case Rest::GET_SESSION_FROM_EXTRA_FIELD: |
||
787 | if (empty($_POST['field_name']) || empty($_POST['field_value'])) { |
||
788 | throw new Exception(get_lang('NoData')); |
||
789 | } |
||
790 | $idSession = $restApi->getSessionFromExtraField($_POST['field_name'], $_POST['field_value']); |
||
791 | Event::addEvent( |
||
792 | LOG_WS.$action, |
||
793 | 'extra_field_name-extra_field_value', |
||
794 | Database::escape_string($_POST['field_name']).':'.Database::escape_string($_POST['field_value']) |
||
795 | ); |
||
796 | $restResponse->setData([$idSession]); |
||
797 | break; |
||
798 | case Rest::GET_SESSION_INFO_FROM_EXTRA_FIELD: |
||
799 | if (empty($_POST['field_name']) || empty($_POST['field_value'])) { |
||
800 | throw new Exception(get_lang('NoData')); |
||
801 | } |
||
802 | $idSession = $restApi->getSessionInfoFromExtraField($_POST['field_name'], $_POST['field_value']); |
||
803 | Event::addEvent( |
||
804 | LOG_WS.$action, |
||
805 | 'extra_field_name-extra_field_value', |
||
806 | Database::escape_string($_POST['field_name']).':'.Database::escape_string($_POST['field_value']) |
||
807 | ); |
||
808 | $restResponse->setData([$idSession]); |
||
809 | break; |
||
810 | case Rest::SAVE_SESSION: |
||
811 | $data = $restApi->addSession($_POST); |
||
812 | Event::addEvent(LOG_WS.$action, 'session_id', $data['id_session']); |
||
813 | $restResponse->setData($data); |
||
814 | break; |
||
815 | case Rest::CREATE_SESSION_FROM_MODEL: |
||
816 | $newSessionId = $restApi->createSessionFromModel($httpRequest); |
||
817 | Event::addEvent(LOG_WS.$action, 'session_id', $newSessionId); |
||
818 | $restResponse->setData([$newSessionId]); |
||
819 | break; |
||
820 | case Rest::UPDATE_SESSION: |
||
821 | $data = $restApi->updateSession($_POST); |
||
822 | Event::addEvent(LOG_WS.$action, 'session_id', $data['id_session']); |
||
823 | $restResponse->setData($data); |
||
824 | break; |
||
825 | case Rest::SUBSCRIBE_USER_TO_COURSE: |
||
826 | $data = $restApi->subscribeUserToCourse($_POST); |
||
827 | Event::addEvent(LOG_WS.$action, 'course_id-user_id', (int) $_POST['course_id'].':'.(int) $_POST['user_id']); |
||
828 | $restResponse->setData($data); |
||
829 | break; |
||
830 | case Rest::SUBSCRIBE_USER_TO_COURSE_PASSWORD: |
||
831 | $courseCode = isset($_POST['code']) ? Security::remove_XSS($_POST['code']) : null; |
||
832 | $password = $_POST['password'] ?? null; |
||
833 | Event::addEvent(LOG_WS.$action, 'course_code', $courseCode); |
||
834 | |||
835 | $restApi->subscribeUserToCoursePassword($courseCode, $password); |
||
836 | $restResponse->setData(['status' => true]); |
||
837 | break; |
||
838 | case Rest::UNSUBSCRIBE_USER_FROM_COURSE: |
||
839 | $data = $restApi->unSubscribeUserToCourse($_POST); |
||
840 | Event::addEvent(LOG_WS.$action, 'course_id-user_id', (int) $_POST['course_id'].':'.(int) $_POST['user_id']); |
||
841 | $restResponse->setData($data); |
||
842 | break; |
||
843 | case Rest::GET_USERS_SUBSCRIBED_TO_COURSE: |
||
844 | Event::addEvent(LOG_WS.$action, 'course_id', (int) $_POST['course']); |
||
845 | $users = $restApi->getUsersSubscribedToCourse(); |
||
846 | $restResponse->setData($users); |
||
847 | break; |
||
848 | case Rest::GET_SESSIONS: |
||
849 | $campusId = api_get_current_access_url_id(); |
||
850 | if (!empty($_POST['id_campus'])) { |
||
851 | $campusId = (int) $_POST['id_campus']; |
||
852 | } |
||
853 | $getExtraFields = false; |
||
854 | if (!empty($_POST['get_extra_fields']) && ('false' != $_POST['get_extra_fields'])) { |
||
855 | $getExtraFields = true; |
||
856 | } |
||
857 | Event::addEvent(LOG_WS.$action, 'id_campus', $campusId); |
||
858 | $data = $restApi->getSessionsCampus($campusId, $getExtraFields); |
||
859 | $restResponse->setData($data); |
||
860 | break; |
||
861 | case Rest::ADD_COURSES_SESSION: |
||
862 | $data = $restApi->addCoursesSession($_POST); |
||
863 | Event::addEvent( |
||
864 | LOG_WS.$action, |
||
865 | 'session_id-course_ids', |
||
866 | (int) $_POST['id_session'].':'.implode(',', $_POST['list_courses']) |
||
867 | ); |
||
868 | $restResponse->setData($data); |
||
869 | break; |
||
870 | case Rest::ADD_USERS_SESSION: |
||
871 | case Rest::SUBSCRIBE_USERS_TO_SESSION: |
||
872 | $data = $restApi->addUsersSession($_POST); |
||
873 | Event::addEvent( |
||
874 | LOG_WS.$action, |
||
875 | 'session_id-users_ids', |
||
876 | (int) $_POST['id_session'].':'.implode(',', $_POST['list_users']) |
||
877 | ); |
||
878 | $restResponse->setData($data); |
||
879 | break; |
||
880 | case Rest::UNSUBSCRIBE_USERS_FROM_SESSION: |
||
881 | $data = $restApi->unsubscribeUsersFromSession($_POST); |
||
882 | Event::addEvent( |
||
883 | LOG_WS.$action, |
||
884 | 'session_id-users_ids', |
||
885 | (int) $_POST['id_session'].':'.implode(',', $_POST['list_users']) |
||
886 | ); |
||
887 | $restResponse->setData($data); |
||
888 | break; |
||
889 | case Rest::SUBSCRIBE_USER_TO_SESSION_FROM_USERNAME: |
||
890 | if (empty($_POST['sessionId']) || empty($_POST['loginname'])) { |
||
891 | throw new Exception(get_lang('NoData')); |
||
892 | } |
||
893 | $subscribed = $restApi->subscribeUserToSessionFromUsername($_POST['sessionId'], $_POST['loginname']); |
||
894 | Event::addEvent( |
||
895 | LOG_WS.$action, |
||
896 | 'session_id-username', |
||
897 | (int) $_POST['sessionId'].':'.Database::escape_string($_POST['loginname']) |
||
898 | ); |
||
899 | $restResponse->setData([$subscribed]); |
||
900 | break; |
||
901 | case Rest::GET_USERS_SUBSCRIBED_TO_SESSION: |
||
902 | Event::addEvent(LOG_WS.$action, 'session_id', (int) $_POST['id_session']); |
||
903 | $users = $restApi->getUsersSubscribedToSession($_POST['id_session'], $_POST['move_info']); |
||
904 | $restResponse->setData($users); |
||
905 | break; |
||
906 | case Rest::GET_COURSE_QUIZ_MDL_COMPAT: |
||
907 | Event::addEvent(LOG_WS.$action, 'course_id', (int) $_POST['course']); |
||
908 | $data = $restApi->getCourseQuizMdlCompat(); |
||
909 | |||
910 | echo json_encode($data, JSON_PRETTY_PRINT); |
||
911 | exit; |
||
912 | case Rest::UPDATE_USER_PAUSE_TRAINING: |
||
913 | $allow = api_get_plugin_setting('pausetraining', 'tool_enable') === 'true'; |
||
914 | |||
915 | if (false === $allow) { |
||
916 | throw new Exception(get_lang('Plugin configured')); |
||
917 | } |
||
918 | |||
919 | if (empty($_POST['user_id'])) { |
||
920 | throw new Exception('user_id is required'); |
||
921 | } |
||
922 | if (null === $restApi) { |
||
923 | throw new Exception('Check that the username and api_key are field in the request'); |
||
924 | } |
||
925 | $plugin = PauseTraining::create(); |
||
926 | $data = $plugin->updateUserPauseTraining($_POST['user_id'], $_POST); |
||
927 | Event::addEvent(LOG_WS.$action, 'user_id', (int) $_POST['user_id']); |
||
928 | $restResponse->setData([$data]); |
||
929 | break; |
||
930 | case Rest::CHECK_CONDITIONAL_LOGIN: |
||
931 | Event::addEvent(LOG_WS.$action, 'username', $username); |
||
932 | $restResponse->setData( |
||
933 | [ |
||
934 | 'check_conditional_login' => $restApi->checkConditionalLogin(), |
||
935 | ] |
||
936 | ); |
||
937 | break; |
||
938 | case Rest::GET_LEGAL_CONDITIONS: |
||
939 | Event::addEvent(LOG_WS.$action, 'username', $username); |
||
940 | $restResponse->setData( |
||
941 | $restApi->getLegalConditions() |
||
942 | ); |
||
943 | break; |
||
944 | case Rest::UPDATE_CONDITION_ACCEPTED: |
||
945 | $restApi->updateConditionAccepted(); |
||
946 | Event::addEvent(LOG_WS.$action, 'success', 'true'); |
||
947 | $restResponse->setData(['status' => true]); |
||
948 | break; |
||
949 | case Rest::GET_TEST_UPDATES_LIST: |
||
950 | Event::addEvent(LOG_WS.$action, 'success', 'true'); |
||
951 | $fields = $_POST['fields'] ?? []; |
||
952 | $restResponse->setData( |
||
953 | $restApi->getTestUpdatesList($fields) |
||
954 | ); |
||
955 | break; |
||
956 | case Rest::GET_TEST_AVERAGE_RESULTS_LIST: |
||
957 | if (empty($_POST['ids'])) { |
||
958 | throw new Exception(get_lang('NoData')); |
||
959 | } |
||
960 | Event::addEvent(LOG_WS.$action, 'success', 'true'); |
||
961 | $fields = $_POST['fields'] ?? []; |
||
962 | $restResponse->setData( |
||
963 | $restApi->getTestAverageResultsList($_POST['ids'], $fields) |
||
964 | ); |
||
965 | break; |
||
966 | /* groups/classes */ |
||
967 | case Rest::GET_GROUPS: |
||
968 | Event::addEvent(LOG_WS.$action, 'username', $username); |
||
969 | $data = $restApi->getGroups($_POST); |
||
970 | $restResponse->setData($data); |
||
971 | break; |
||
972 | case Rest::GROUP_EXISTS: |
||
973 | Event::addEvent(LOG_WS.$action, 'groupname', $_POST['name']); |
||
974 | $data = $restApi->groupExists($_POST['name']); |
||
975 | $restResponse->setData([$data]); |
||
976 | break; |
||
977 | case Rest::ADD_GROUP: |
||
978 | $data = $restApi->addGroup($_POST); |
||
979 | Event::addEvent(LOG_WS.$action, 'user_id', $data); |
||
980 | $restResponse->setData($data); |
||
981 | break; |
||
982 | case Rest::DELETE_GROUP: |
||
983 | $data = $restApi->deleteGroup($_POST['id']); |
||
984 | Event::addEvent(LOG_WS.$action, 'group_id', $data); |
||
985 | $restResponse->setData($data); |
||
986 | break; |
||
987 | case Rest::GET_GROUP_SUB_USERS: |
||
988 | $data = $restApi->getGroupSubscribedUsers($_POST['id']); |
||
989 | Event::addEvent(LOG_WS.$action, 'group_id', $data); |
||
990 | $restResponse->setData($data); |
||
991 | break; |
||
992 | case Rest::GET_GROUP_SUB_COURSES: |
||
993 | $data = $restApi->getGroupSubscribedCourses($_POST['id']); |
||
994 | Event::addEvent(LOG_WS.$action, 'group_id', $data); |
||
995 | $restResponse->setData($data); |
||
996 | break; |
||
997 | case Rest::GET_GROUP_SUB_SESSIONS: |
||
998 | $data = $restApi->getGroupSubscribedSessions($_POST['id']); |
||
999 | Event::addEvent(LOG_WS.$action, 'group_id', $data); |
||
1000 | $restResponse->setData($data); |
||
1001 | break; |
||
1002 | case Rest::ADD_GROUP_SUB_USER: |
||
1003 | $groupId = (int) $_POST['group_id']; |
||
1004 | $userId = (int) $_POST['user_id']; |
||
1005 | if (empty($userId)) { |
||
1006 | throw new Exception('user_id not provided'); |
||
1007 | } |
||
1008 | if (empty($groupId)) { |
||
1009 | throw new Exception('group_id not provided'); |
||
1010 | } |
||
1011 | $role = 2; |
||
1012 | if (isset($_POST['role'])) { |
||
1013 | $role = (int) $_POST['role']; |
||
1014 | } |
||
1015 | $data = $restApi->addGroupSubscribedUser($groupId, $userId, $role); |
||
1016 | Event::addEvent(LOG_WS.$action, 'group_id', $groupId); |
||
1017 | $restResponse->setData($data); |
||
1018 | break; |
||
1019 | case Rest::ADD_GROUP_SUB_COURSE: |
||
1020 | $groupId = (int) $_POST['group_id']; |
||
1021 | $courseId = (int) $_POST['course_id']; |
||
1022 | $data = $restApi->addGroupSubscribedCourse($groupId, $courseId); |
||
1023 | Event::addEvent(LOG_WS.$action, 'group_id', $groupId); |
||
1024 | $restResponse->setData($data); |
||
1025 | break; |
||
1026 | case Rest::ADD_GROUP_SUB_SESSION: |
||
1027 | $groupId = (int) $_POST['group_id']; |
||
1028 | $sessionId = (int) $_POST['session_id']; |
||
1029 | $data = $restApi->addGroupSubscribedSession($groupId, $sessionId); |
||
1030 | Event::addEvent(LOG_WS.$action, 'group_id', $groupId); |
||
1031 | $restResponse->setData($data); |
||
1032 | break; |
||
1033 | case Rest::DELETE_GROUP_SUB_USER: |
||
1034 | $groupId = (int) $_POST['group_id']; |
||
1035 | $userId = (int) $_POST['user_id']; |
||
1036 | $data = $restApi->deleteGroupSubscribedUser($groupId, $userId); |
||
1037 | Event::addEvent(LOG_WS.$action, 'group_id', $groupId); |
||
1038 | $restResponse->setData($data); |
||
1039 | break; |
||
1040 | case Rest::DELETE_GROUP_SUB_COURSE: |
||
1041 | $groupId = (int) $_POST['group_id']; |
||
1042 | $courseId = (int) $_POST['course_id']; |
||
1043 | $data = $restApi->deleteGroupSubscribedCourse($groupId, $courseId); |
||
1044 | Event::addEvent(LOG_WS.$action, 'group_id', $groupId); |
||
1045 | $restResponse->setData($data); |
||
1046 | break; |
||
1047 | case Rest::DELETE_GROUP_SUB_SESSION: |
||
1048 | $groupId = (int) $_POST['group_id']; |
||
1049 | $sessionId = (int) $_POST['session_id']; |
||
1050 | $data = $restApi->deleteGroupSubscribedSession($groupId, $sessionId); |
||
1051 | Event::addEvent(LOG_WS.$action, 'group_id', $groupId); |
||
1052 | $restResponse->setData($data); |
||
1053 | break; |
||
1054 | case Rest::GET_AUDIT_ITEMS: |
||
1055 | $defaultEventType = $_POST['event_type']; |
||
1056 | |||
1057 | $cId = ($_POST['c_id'] ? (int) $_POST['c_id'] : null); |
||
1058 | $sessionId = ($_POST['session_id'] ? (int) $_POST['session_id'] : null); |
||
1059 | $userId = ($_POST['user_id'] ? (int) $_POST['user_id'] : null); |
||
1060 | |||
1061 | $afterDate = ($_POST['after_date'] ?? null); |
||
1062 | $beforeDate = ($_POST['before_date'] ?? null); |
||
1063 | $offset = ($_POST['offset'] ? (int) $_POST['offset'] : 0); |
||
1064 | $limit = ($_POST['limit'] ? (int) $_POST['limit'] : 100); |
||
1065 | |||
1066 | if (empty($defaultEventType)) { |
||
1067 | throw new Exception('event_type is required'); |
||
1068 | } |
||
1069 | |||
1070 | $data = $restApi->getAuditItems($defaultEventType, $cId, $sessionId, $afterDate, $beforeDate, $userId, $offset, $limit); |
||
1071 | Event::addEvent(LOG_WS.$action, 'success', 'true'); |
||
1072 | $restResponse->setData($data); |
||
1073 | break; |
||
1074 | default: |
||
1075 | throw new Exception(get_lang('InvalidAction')); |
||
1076 | } |
||
1077 | } catch (Exception $exception) { |
||
1078 | $restResponse->setErrorMessage( |
||
1079 | $exception->getMessage() |
||
1080 | ); |
||
1081 | } |
||
1082 | |||
1083 | header('Content-Type: application/json'); |
||
1084 | header('Access-Control-Allow-Origin: *'); |
||
1085 | |||
1086 | echo $restResponse->format(); |
||
1087 |
Let?s assume that you have a directory layout like this:
and let?s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as
OtherDir/Foo.php
does not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php
, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: