Total Complexity | 374 |
Total Lines | 2950 |
Duplicated Lines | 0 % |
Changes | 8 | ||
Bugs | 1 | Features | 0 |
Complex classes like Rest 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 Rest, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
16 | class Rest extends WebService |
||
17 | { |
||
18 | const SERVICE_NAME = 'MsgREST'; |
||
19 | const EXTRA_FIELD_GCM_REGISTRATION = 'gcm_registration_id'; |
||
20 | |||
21 | const GET_AUTH = 'authenticate'; |
||
22 | const SAVE_GCM_ID = 'gcm_id'; |
||
23 | const LOGOUT = 'logout'; |
||
24 | |||
25 | const GET_USER_MESSAGES = 'user_messages'; |
||
26 | const GET_USER_MESSAGES_RECEIVED = 'user_messages_received'; |
||
27 | const DELETE_USER_MESSAGE = 'delete_user_message'; |
||
28 | const GET_USER_MESSAGES_SENT = 'user_messages_sent'; |
||
29 | const GET_COUNT_NEW_MESSAGES = 'get_count_new_messages'; |
||
30 | const SET_MESSAGE_READ = 'set_message_read'; |
||
31 | const POST_USER_MESSAGE_READ = 'user_message_read'; |
||
32 | const POST_USER_MESSAGE_UNREAD = 'user_message_unread'; |
||
33 | const SAVE_USER_MESSAGE = 'save_user_message'; |
||
34 | const GET_MESSAGE_USERS = 'message_users'; |
||
35 | |||
36 | const GET_USER_COURSES = 'user_courses'; |
||
37 | const GET_USER_SESSIONS = 'user_sessions'; |
||
38 | |||
39 | const GET_PROFILE = 'user_profile'; |
||
40 | |||
41 | const GET_COURSE_INFO = 'course_info'; |
||
42 | const GET_COURSE_DESCRIPTIONS = 'course_descriptions'; |
||
43 | const GET_COURSE_DOCUMENTS = 'course_documents'; |
||
44 | const GET_COURSE_ANNOUNCEMENTS = 'course_announcements'; |
||
45 | const GET_COURSE_ANNOUNCEMENT = 'course_announcement'; |
||
46 | const GET_COURSE_AGENDA = 'course_agenda'; |
||
47 | const GET_COURSE_NOTEBOOKS = 'course_notebooks'; |
||
48 | const GET_COURSE_FORUM_CATEGORIES = 'course_forumcategories'; |
||
49 | const GET_COURSE_FORUM = 'course_forum'; |
||
50 | const GET_COURSE_FORUM_THREAD = 'course_forumthread'; |
||
51 | const GET_COURSE_LEARNPATHS = 'course_learnpaths'; |
||
52 | const GET_COURSE_LEARNPATH = 'course_learnpath'; |
||
53 | const GET_COURSE_LP_PROGRESS = 'course_lp_progress'; |
||
54 | const GET_COURSE_LINKS = 'course_links'; |
||
55 | const GET_COURSE_WORKS = 'course_works'; |
||
56 | |||
57 | const SAVE_COURSE_NOTEBOOK = 'save_course_notebook'; |
||
58 | |||
59 | const SAVE_FORUM_POST = 'save_forum_post'; |
||
60 | const SAVE_FORUM_THREAD = 'save_forum_thread'; |
||
61 | const SET_THREAD_NOTIFY = 'set_thread_notify'; |
||
62 | |||
63 | const GET_WORK_LIST = 'get_work_list'; |
||
64 | const GET_WORK_STUDENTS_WITHOUT_PUBLICATIONS = 'get_work_students_without_publications'; |
||
65 | const GET_WORK_USERS = 'get_work_users'; |
||
66 | const GET_WORK_STUDENT_LIST = 'get_work_student_list'; |
||
67 | const PUT_WORK_STUDENT_ITEM_VISIBILITY = 'put_course_work_visibility'; |
||
68 | const DELETE_WORK_STUDENT_ITEM = 'delete_work_student_item'; |
||
69 | const DELETE_WORK_CORRECTIONS = 'delete_work_corrections'; |
||
70 | |||
71 | const VIEW_DOCUMENT_IN_FRAME = 'view_document_in_frame'; |
||
72 | |||
73 | const VIEW_QUIZ_TOOL = 'view_quiz_tool'; |
||
74 | |||
75 | const VIEW_SURVEY_TOOL = 'view_survey_tool'; |
||
76 | |||
77 | const CREATE_CAMPUS = 'add_campus'; |
||
78 | const EDIT_CAMPUS = 'edit_campus'; |
||
79 | const DELETE_CAMPUS = 'delete_campus'; |
||
80 | |||
81 | const GET_USERS = 'get_users'; |
||
82 | const USERNAME_EXIST = 'username_exist'; |
||
83 | const SAVE_USER = 'save_user'; |
||
84 | const SAVE_USER_JSON = 'save_user_json'; |
||
85 | const UPDATE_USER_FROM_USERNAME = 'update_user_from_username'; |
||
86 | const DELETE_USER = 'delete_user'; |
||
87 | |||
88 | const GET_COURSES = 'get_courses'; |
||
89 | const GET_COURSES_FROM_EXTRA_FIELD = 'get_courses_from_extra_field'; |
||
90 | const SAVE_COURSE = 'save_course'; |
||
91 | const DELETE_COURSE = 'delete_course'; |
||
92 | |||
93 | const GET_SESSION_FROM_EXTRA_FIELD = 'get_session_from_extra_field'; |
||
94 | const SAVE_SESSION = 'save_session'; |
||
95 | const CREATE_SESSION_FROM_MODEL = 'create_session_from_model'; |
||
96 | const UPDATE_SESSION = 'update_session'; |
||
97 | |||
98 | const SUBSCRIBE_USER_TO_COURSE = 'subscribe_user_to_course'; |
||
99 | const SUBSCRIBE_USER_TO_COURSE_PASSWORD = 'subscribe_user_to_course_password'; |
||
100 | const UNSUBSCRIBE_USER_FROM_COURSE = 'unsubscribe_user_from_course'; |
||
101 | const GET_USERS_SUBSCRIBED_TO_COURSE = 'get_users_subscribed_to_course'; |
||
102 | |||
103 | const ADD_COURSES_SESSION = 'add_courses_session'; |
||
104 | const ADD_USERS_SESSION = 'add_users_session'; |
||
105 | const SUBSCRIBE_USER_TO_SESSION_FROM_USERNAME = 'subscribe_user_to_session_from_username'; |
||
106 | |||
107 | const GET_COURSE_QUIZ_MDL_COMPAT = 'get_course_quiz_mdl_compat'; |
||
108 | |||
109 | const UPDATE_USER_PAUSE_TRAINING = 'update_user_pause_training'; |
||
110 | |||
111 | const CHECK_CONDITIONAL_LOGIN = 'check_conditional_login'; |
||
112 | const GET_LEGAL_CONDITIONS = 'get_legal_conditions'; |
||
113 | const UPDATE_CONDITION_ACCEPTED = 'update_condition_accepted'; |
||
114 | |||
115 | /** |
||
116 | * @var Session |
||
117 | */ |
||
118 | private $session; |
||
119 | |||
120 | /** |
||
121 | * @var Course |
||
122 | */ |
||
123 | private $course; |
||
124 | |||
125 | /** |
||
126 | * Rest constructor. |
||
127 | * |
||
128 | * @param string $username |
||
129 | * @param string $apiKey |
||
130 | */ |
||
131 | public function __construct($username, $apiKey) |
||
132 | { |
||
133 | parent::__construct($username, $apiKey); |
||
134 | } |
||
135 | |||
136 | /** |
||
137 | * @param string $username |
||
138 | * @param string $apiKeyToValidate |
||
139 | * |
||
140 | * @throws Exception |
||
141 | * |
||
142 | * @return Rest |
||
143 | */ |
||
144 | public static function validate($username, $apiKeyToValidate) |
||
153 | } |
||
154 | |||
155 | /** |
||
156 | * Create the gcm_registration_id extra field for users. |
||
157 | */ |
||
158 | public static function init() |
||
159 | { |
||
160 | $extraField = new ExtraField('user'); |
||
161 | $fieldInfo = $extraField->get_handler_field_info_by_field_variable(self::EXTRA_FIELD_GCM_REGISTRATION); |
||
162 | |||
163 | if (empty($fieldInfo)) { |
||
164 | $extraField->save( |
||
165 | [ |
||
166 | 'variable' => self::EXTRA_FIELD_GCM_REGISTRATION, |
||
167 | 'field_type' => ExtraField::FIELD_TYPE_TEXT, |
||
168 | 'display_text' => self::EXTRA_FIELD_GCM_REGISTRATION, |
||
169 | ] |
||
170 | ); |
||
171 | } |
||
172 | } |
||
173 | |||
174 | /** |
||
175 | * @param string $encoded |
||
176 | * |
||
177 | * @return array |
||
178 | */ |
||
179 | public static function decodeParams($encoded) |
||
180 | { |
||
181 | return json_decode($encoded); |
||
182 | } |
||
183 | |||
184 | /** |
||
185 | * Set the current course. |
||
186 | * |
||
187 | * @param int $id |
||
188 | * |
||
189 | * @throws Exception |
||
190 | */ |
||
191 | public function setCourse($id) |
||
192 | { |
||
193 | global $_course; |
||
194 | |||
195 | if (!$id) { |
||
196 | $this->course = null; |
||
197 | |||
198 | ChamiloSession::erase('_real_cid'); |
||
199 | ChamiloSession::erase('_cid'); |
||
200 | ChamiloSession::erase('_course'); |
||
201 | |||
202 | return; |
||
203 | } |
||
204 | |||
205 | $em = Database::getManager(); |
||
206 | /** @var Course $course */ |
||
207 | $course = $em->find('ChamiloCoreBundle:Course', $id); |
||
208 | |||
209 | if (!$course) { |
||
|
|||
210 | throw new Exception(get_lang('NoCourse')); |
||
211 | } |
||
212 | |||
213 | $this->course = $course; |
||
214 | |||
215 | $courseInfo = api_get_course_info($course->getCode()); |
||
216 | $_course = $courseInfo; |
||
217 | |||
218 | ChamiloSession::write('_real_cid', $course->getId()); |
||
219 | ChamiloSession::write('_cid', $course->getCode()); |
||
220 | ChamiloSession::write('_course', $courseInfo); |
||
221 | } |
||
222 | |||
223 | /** |
||
224 | * Set the current session. |
||
225 | * |
||
226 | * @param int $id |
||
227 | * |
||
228 | * @throws Exception |
||
229 | */ |
||
230 | public function setSession($id) |
||
231 | { |
||
232 | if (!$id) { |
||
233 | $this->session = null; |
||
234 | |||
235 | ChamiloSession::erase('session_name'); |
||
236 | ChamiloSession::erase('id_session'); |
||
237 | |||
238 | return; |
||
239 | } |
||
240 | |||
241 | $em = Database::getManager(); |
||
242 | /** @var Session $session */ |
||
243 | $session = $em->find('ChamiloCoreBundle:Session', $id); |
||
244 | |||
245 | if (!$session) { |
||
246 | throw new Exception(get_lang('NoSession')); |
||
247 | } |
||
248 | |||
249 | $this->session = $session; |
||
250 | |||
251 | ChamiloSession::write('session_name', $session->getName()); |
||
252 | ChamiloSession::write('id_session', $session->getId()); |
||
253 | } |
||
254 | |||
255 | /** |
||
256 | * @param string $registrationId |
||
257 | * |
||
258 | * @return bool |
||
259 | */ |
||
260 | public function setGcmId($registrationId) |
||
261 | { |
||
262 | $registrationId = Security::remove_XSS($registrationId); |
||
263 | $extraFieldValue = new ExtraFieldValue('user'); |
||
264 | |||
265 | return $extraFieldValue->save( |
||
266 | [ |
||
267 | 'variable' => self::EXTRA_FIELD_GCM_REGISTRATION, |
||
268 | 'value' => $registrationId, |
||
269 | 'item_id' => $this->user->getId(), |
||
270 | ] |
||
271 | ); |
||
272 | } |
||
273 | |||
274 | /** |
||
275 | * @param int $lastMessageId |
||
276 | * |
||
277 | * @return array |
||
278 | */ |
||
279 | public function getUserMessages($lastMessageId = 0) |
||
280 | { |
||
281 | $lastMessages = MessageManager::getMessagesFromLastReceivedMessage($this->user->getId(), $lastMessageId); |
||
282 | $messages = []; |
||
283 | |||
284 | foreach ($lastMessages as $message) { |
||
285 | $hasAttachments = MessageManager::hasAttachments($message['id']); |
||
286 | |||
287 | $messages[] = [ |
||
288 | 'id' => $message['id'], |
||
289 | 'title' => $message['title'], |
||
290 | 'sender' => [ |
||
291 | 'id' => $message['user_id'], |
||
292 | 'lastname' => $message['lastname'], |
||
293 | 'firstname' => $message['firstname'], |
||
294 | 'completeName' => api_get_person_name($message['firstname'], $message['lastname']), |
||
295 | ], |
||
296 | 'sendDate' => $message['send_date'], |
||
297 | 'content' => $message['content'], |
||
298 | 'hasAttachments' => $hasAttachments, |
||
299 | 'url' => api_get_path(WEB_CODE_PATH).'messages/view_message.php?' |
||
300 | .http_build_query(['type' => 1, 'id' => $message['id']]), |
||
301 | ]; |
||
302 | } |
||
303 | |||
304 | return $messages; |
||
305 | } |
||
306 | |||
307 | /** |
||
308 | * @return array |
||
309 | */ |
||
310 | public function getUserReceivedMessages() |
||
311 | { |
||
312 | $lastMessages = MessageManager::getReceivedMessages($this->user->getId(), 0); |
||
313 | $messages = []; |
||
314 | |||
315 | $webPath = api_get_path(WEB_PATH); |
||
316 | |||
317 | foreach ($lastMessages as $message) { |
||
318 | $hasAttachments = MessageManager::hasAttachments($message['id']); |
||
319 | $attachmentList = []; |
||
320 | if ($hasAttachments) { |
||
321 | $attachmentList = MessageManager::getAttachmentList($message['id']); |
||
322 | } |
||
323 | $messages[] = [ |
||
324 | 'id' => $message['id'], |
||
325 | 'title' => $message['title'], |
||
326 | 'msgStatus' => $message['msg_status'], |
||
327 | 'sender' => [ |
||
328 | 'id' => $message['user_id'], |
||
329 | 'lastname' => $message['lastname'], |
||
330 | 'firstname' => $message['firstname'], |
||
331 | 'completeName' => api_get_person_name($message['firstname'], $message['lastname']), |
||
332 | 'pictureUri' => $message['pictureUri'], |
||
333 | ], |
||
334 | 'sendDate' => $message['send_date'], |
||
335 | 'content' => str_replace('src="/"', $webPath, $message['content']), |
||
336 | 'hasAttachments' => $hasAttachments, |
||
337 | 'attachmentList' => $attachmentList, |
||
338 | 'url' => '', |
||
339 | ]; |
||
340 | } |
||
341 | |||
342 | return $messages; |
||
343 | } |
||
344 | |||
345 | /** |
||
346 | * @return array |
||
347 | */ |
||
348 | public function getUserSentMessages() |
||
349 | { |
||
350 | $lastMessages = MessageManager::getSentMessages($this->user->getId(), 0); |
||
351 | $messages = []; |
||
352 | |||
353 | foreach ($lastMessages as $message) { |
||
354 | $hasAttachments = MessageManager::hasAttachments($message['id']); |
||
355 | |||
356 | $messages[] = [ |
||
357 | 'id' => $message['id'], |
||
358 | 'title' => $message['title'], |
||
359 | 'msgStatus' => $message['msg_status'], |
||
360 | 'receiver' => [ |
||
361 | 'id' => $message['user_id'], |
||
362 | 'lastname' => $message['lastname'], |
||
363 | 'firstname' => $message['firstname'], |
||
364 | 'completeName' => api_get_person_name($message['firstname'], $message['lastname']), |
||
365 | 'pictureUri' => $message['pictureUri'], |
||
366 | ], |
||
367 | 'sendDate' => $message['send_date'], |
||
368 | 'content' => $message['content'], |
||
369 | 'hasAttachments' => $hasAttachments, |
||
370 | 'url' => '', |
||
371 | ]; |
||
372 | } |
||
373 | |||
374 | return $messages; |
||
375 | } |
||
376 | |||
377 | /** |
||
378 | * Get the user courses. |
||
379 | */ |
||
380 | public function getUserCourses($userId = 0): array |
||
381 | { |
||
382 | if (empty($userId)) { |
||
383 | $userId = $this->user->getId(); |
||
384 | } |
||
385 | |||
386 | Event::courseLogout( |
||
387 | [ |
||
388 | 'uid' => $userId, |
||
389 | 'cid' => api_get_course_id(), |
||
390 | 'sid' => api_get_session_id(), |
||
391 | ] |
||
392 | ); |
||
393 | |||
394 | $courses = CourseManager::get_courses_list_by_user_id($userId); |
||
395 | $data = []; |
||
396 | |||
397 | foreach ($courses as $courseInfo) { |
||
398 | /** @var Course $course */ |
||
399 | $course = Database::getManager()->find('ChamiloCoreBundle:Course', $courseInfo['real_id']); |
||
400 | $teachers = CourseManager::getTeacherListFromCourseCodeToString($course->getCode()); |
||
401 | $picturePath = CourseManager::getPicturePath($course, true) |
||
402 | ?: Display::return_icon('session_default.png', null, null, null, null, true); |
||
403 | |||
404 | $data[] = [ |
||
405 | 'id' => $course->getId(), |
||
406 | 'title' => $course->getTitle(), |
||
407 | 'code' => $course->getCode(), |
||
408 | 'directory' => $course->getDirectory(), |
||
409 | 'urlPicture' => $picturePath, |
||
410 | 'teachers' => $teachers, |
||
411 | 'isSpecial' => !empty($courseInfo['special_course']), |
||
412 | ]; |
||
413 | } |
||
414 | |||
415 | return $data; |
||
416 | } |
||
417 | |||
418 | /** |
||
419 | * @throws Exception |
||
420 | * |
||
421 | * @return array |
||
422 | */ |
||
423 | public function getCourseInfo() |
||
424 | { |
||
425 | $teachers = CourseManager::getTeacherListFromCourseCodeToString($this->course->getCode()); |
||
426 | $tools = CourseHome::get_tools_category( |
||
427 | TOOL_STUDENT_VIEW, |
||
428 | $this->course->getId(), |
||
429 | $this->session ? $this->session->getId() : 0 |
||
430 | ); |
||
431 | |||
432 | return [ |
||
433 | 'id' => $this->course->getId(), |
||
434 | 'title' => $this->course->getTitle(), |
||
435 | 'code' => $this->course->getCode(), |
||
436 | 'directory' => $this->course->getDirectory(), |
||
437 | 'urlPicture' => CourseManager::getPicturePath($this->course, true), |
||
438 | 'teachers' => $teachers, |
||
439 | 'tools' => array_map( |
||
440 | function ($tool) { |
||
441 | return ['type' => $tool['name']]; |
||
442 | }, |
||
443 | $tools |
||
444 | ), |
||
445 | ]; |
||
446 | } |
||
447 | |||
448 | /** |
||
449 | * Get the course descriptions. |
||
450 | * |
||
451 | * @throws Exception |
||
452 | * |
||
453 | * @return array |
||
454 | */ |
||
455 | public function getCourseDescriptions() |
||
456 | { |
||
457 | Event::event_access_tool(TOOL_COURSE_DESCRIPTION); |
||
458 | |||
459 | $descriptions = CourseDescription::get_descriptions($this->course->getId()); |
||
460 | $results = []; |
||
461 | |||
462 | $webPath = api_get_path(WEB_PATH); |
||
463 | |||
464 | /** @var CourseDescription $description */ |
||
465 | foreach ($descriptions as $description) { |
||
466 | $results[] = [ |
||
467 | 'id' => $description->get_description_type(), |
||
468 | 'title' => $description->get_title(), |
||
469 | 'content' => str_replace('src="/', 'src="'.$webPath, $description->get_content()), |
||
470 | ]; |
||
471 | } |
||
472 | |||
473 | return $results; |
||
474 | } |
||
475 | |||
476 | /** |
||
477 | * @param int $directoryId |
||
478 | * |
||
479 | * @throws Exception |
||
480 | * |
||
481 | * @return array |
||
482 | */ |
||
483 | public function getCourseDocuments($directoryId = 0) |
||
484 | { |
||
485 | Event::event_access_tool(TOOL_DOCUMENT); |
||
486 | |||
487 | /** @var string $path */ |
||
488 | $path = '/'; |
||
489 | $sessionId = $this->session ? $this->session->getId() : 0; |
||
490 | |||
491 | if ($directoryId) { |
||
492 | $directory = DocumentManager::get_document_data_by_id( |
||
493 | $directoryId, |
||
494 | $this->course->getCode(), |
||
495 | false, |
||
496 | $sessionId |
||
497 | ); |
||
498 | |||
499 | if (!$directory) { |
||
500 | throw new Exception('NoDataAvailable'); |
||
501 | } |
||
502 | |||
503 | $path = $directory['path']; |
||
504 | } |
||
505 | |||
506 | $courseInfo = api_get_course_info_by_id($this->course->getId()); |
||
507 | $documents = DocumentManager::getAllDocumentData( |
||
508 | $courseInfo, |
||
509 | $path, |
||
510 | 0, |
||
511 | null, |
||
512 | false, |
||
513 | false, |
||
514 | $sessionId |
||
515 | ); |
||
516 | $results = []; |
||
517 | |||
518 | if (!empty($documents)) { |
||
519 | $webPath = api_get_path(WEB_CODE_PATH).'document/document.php?'; |
||
520 | |||
521 | /** @var array $document */ |
||
522 | foreach ($documents as $document) { |
||
523 | if ($document['visibility'] != '1') { |
||
524 | continue; |
||
525 | } |
||
526 | |||
527 | $icon = $document['filetype'] == 'file' |
||
528 | ? choose_image($document['path']) |
||
529 | : chooseFolderIcon($document['path']); |
||
530 | |||
531 | $results[] = [ |
||
532 | 'id' => $document['id'], |
||
533 | 'type' => $document['filetype'], |
||
534 | 'title' => $document['title'], |
||
535 | 'path' => $document['path'], |
||
536 | 'url' => $webPath.http_build_query( |
||
537 | [ |
||
538 | 'username' => $this->user->getUsername(), |
||
539 | 'api_key' => $this->apiKey, |
||
540 | 'cidReq' => $this->course->getCode(), |
||
541 | 'id_session' => $sessionId, |
||
542 | 'gidReq' => 0, |
||
543 | 'gradebook' => 0, |
||
544 | 'origin' => '', |
||
545 | 'action' => 'download', |
||
546 | 'id' => $document['id'], |
||
547 | ] |
||
548 | ), |
||
549 | 'icon' => $icon, |
||
550 | 'size' => format_file_size($document['size']), |
||
551 | ]; |
||
552 | } |
||
553 | } |
||
554 | |||
555 | return $results; |
||
556 | } |
||
557 | |||
558 | /** |
||
559 | * @throws Exception |
||
560 | * |
||
561 | * @return array |
||
562 | */ |
||
563 | public function getCourseAnnouncements() |
||
564 | { |
||
565 | Event::event_access_tool(TOOL_ANNOUNCEMENT); |
||
566 | |||
567 | $sessionId = $this->session ? $this->session->getId() : 0; |
||
568 | |||
569 | $announcements = AnnouncementManager::getAnnouncements( |
||
570 | null, |
||
571 | null, |
||
572 | false, |
||
573 | null, |
||
574 | null, |
||
575 | null, |
||
576 | null, |
||
577 | null, |
||
578 | 0, |
||
579 | $this->user->getId(), |
||
580 | $this->course->getId(), |
||
581 | $sessionId |
||
582 | ); |
||
583 | |||
584 | $announcements = array_map( |
||
585 | function ($announcement) { |
||
586 | return [ |
||
587 | 'id' => (int) $announcement['id'], |
||
588 | 'title' => strip_tags($announcement['title']), |
||
589 | 'creatorName' => strip_tags($announcement['username']), |
||
590 | 'date' => strip_tags($announcement['insert_date']), |
||
591 | ]; |
||
592 | }, |
||
593 | $announcements |
||
594 | ); |
||
595 | |||
596 | return $announcements; |
||
597 | } |
||
598 | |||
599 | /** |
||
600 | * @param int $announcementId |
||
601 | * |
||
602 | * @throws Exception |
||
603 | * |
||
604 | * @return array |
||
605 | */ |
||
606 | public function getCourseAnnouncement($announcementId) |
||
607 | { |
||
608 | Event::event_access_tool(TOOL_ANNOUNCEMENT); |
||
609 | |||
610 | $sessionId = $this->session ? $this->session->getId() : 0; |
||
611 | $announcement = AnnouncementManager::getAnnouncementInfoById( |
||
612 | $announcementId, |
||
613 | $this->course->getId(), |
||
614 | $this->user->getId() |
||
615 | ); |
||
616 | |||
617 | if (!$announcement) { |
||
618 | throw new Exception(get_lang('NoAnnouncement')); |
||
619 | } |
||
620 | |||
621 | return [ |
||
622 | 'id' => $announcement['announcement']->getIid(), |
||
623 | 'title' => $announcement['announcement']->getTitle(), |
||
624 | 'creatorName' => UserManager::formatUserFullName($announcement['item_property']->getInsertUser()), |
||
625 | 'date' => api_convert_and_format_date( |
||
626 | $announcement['item_property']->getInsertDate(), |
||
627 | DATE_TIME_FORMAT_LONG_24H |
||
628 | ), |
||
629 | 'content' => AnnouncementManager::parseContent( |
||
630 | $this->user->getId(), |
||
631 | $announcement['announcement']->getContent(), |
||
632 | $this->course->getCode(), |
||
633 | $sessionId |
||
634 | ), |
||
635 | ]; |
||
636 | } |
||
637 | |||
638 | /** |
||
639 | * @throws Exception |
||
640 | * |
||
641 | * @return array |
||
642 | */ |
||
643 | public function getCourseAgenda() |
||
694 | ); |
||
695 | } |
||
696 | |||
697 | /** |
||
698 | * @throws Exception |
||
699 | * |
||
700 | * @return array |
||
701 | */ |
||
702 | public function getCourseNotebooks() |
||
703 | { |
||
704 | Event::event_access_tool(TOOL_NOTEBOOK); |
||
705 | |||
706 | $em = Database::getManager(); |
||
707 | /** @var CNotebookRepository $notebooksRepo */ |
||
708 | $notebooksRepo = $em->getRepository('ChamiloCourseBundle:CNotebook'); |
||
709 | $notebooks = $notebooksRepo->findByUser($this->user, $this->course, $this->session); |
||
710 | |||
711 | return array_map( |
||
712 | function (CNotebook $notebook) { |
||
713 | return [ |
||
714 | 'id' => $notebook->getIid(), |
||
715 | 'title' => $notebook->getTitle(), |
||
716 | 'description' => $notebook->getDescription(), |
||
717 | 'creationDate' => api_format_date( |
||
718 | $notebook->getCreationDate()->getTimestamp() |
||
719 | ), |
||
720 | 'updateDate' => api_format_date( |
||
721 | $notebook->getUpdateDate()->getTimestamp() |
||
722 | ), |
||
723 | ]; |
||
724 | }, |
||
725 | $notebooks |
||
726 | ); |
||
727 | } |
||
728 | |||
729 | /** |
||
730 | * @throws Exception |
||
731 | * |
||
732 | * @return array |
||
733 | */ |
||
734 | public function getCourseForumCategories() |
||
735 | { |
||
736 | Event::event_access_tool(TOOL_FORUM); |
||
737 | |||
738 | $sessionId = $this->session ? $this->session->getId() : 0; |
||
739 | $webCoursePath = api_get_path(WEB_COURSE_PATH).$this->course->getDirectory().'/upload/forum/images/'; |
||
740 | |||
741 | require_once api_get_path(SYS_CODE_PATH).'forum/forumfunction.inc.php'; |
||
742 | |||
743 | $categoriesFullData = get_forum_categories('', $this->course->getId(), $sessionId); |
||
744 | $categories = []; |
||
745 | $includeGroupsForums = api_get_setting('display_groups_forum_in_general_tool') === 'true'; |
||
746 | $forumsFullData = get_forums('', $this->course->getCode(), $includeGroupsForums, $sessionId); |
||
747 | $forums = []; |
||
748 | |||
749 | foreach ($forumsFullData as $forumId => $forumInfo) { |
||
750 | $forum = [ |
||
751 | 'id' => (int) $forumInfo['iid'], |
||
752 | 'catId' => (int) $forumInfo['forum_category'], |
||
753 | 'title' => $forumInfo['forum_title'], |
||
754 | 'description' => $forumInfo['forum_comment'], |
||
755 | 'image' => $forumInfo['forum_image'] ? ($webCoursePath.$forumInfo['forum_image']) : '', |
||
756 | 'numberOfThreads' => isset($forumInfo['number_of_threads']) ? intval( |
||
757 | $forumInfo['number_of_threads'] |
||
758 | ) : 0, |
||
759 | 'lastPost' => null, |
||
760 | ]; |
||
761 | |||
762 | $lastPostInfo = get_last_post_information($forumId, false, $this->course->getId(), $sessionId); |
||
763 | |||
764 | if ($lastPostInfo) { |
||
765 | $forum['lastPost'] = [ |
||
766 | 'date' => api_convert_and_format_date($lastPostInfo['last_post_date']), |
||
767 | 'user' => api_get_person_name( |
||
768 | $lastPostInfo['last_poster_firstname'], |
||
769 | $lastPostInfo['last_poster_lastname'] |
||
770 | ), |
||
771 | ]; |
||
772 | } |
||
773 | |||
774 | $forums[] = $forum; |
||
775 | } |
||
776 | |||
777 | foreach ($categoriesFullData as $category) { |
||
778 | $categoryForums = array_filter( |
||
779 | $forums, |
||
780 | function (array $forum) use ($category) { |
||
781 | if ($forum['catId'] != $category['cat_id']) { |
||
782 | return false; |
||
783 | } |
||
784 | |||
785 | return true; |
||
786 | } |
||
787 | ); |
||
788 | |||
789 | $categories[] = [ |
||
790 | 'id' => (int) $category['iid'], |
||
791 | 'title' => $category['cat_title'], |
||
792 | 'catId' => (int) $category['cat_id'], |
||
793 | 'description' => $category['cat_comment'], |
||
794 | 'forums' => $categoryForums, |
||
795 | 'courseId' => $this->course->getId(), |
||
796 | ]; |
||
797 | } |
||
798 | |||
799 | return $categories; |
||
800 | } |
||
801 | |||
802 | /** |
||
803 | * @param int $forumId |
||
804 | * |
||
805 | * @throws Exception |
||
806 | * |
||
807 | * @return array |
||
808 | */ |
||
809 | public function getCourseForum($forumId) |
||
810 | { |
||
811 | Event::event_access_tool(TOOL_FORUM); |
||
812 | |||
813 | require_once api_get_path(SYS_CODE_PATH).'forum/forumfunction.inc.php'; |
||
814 | |||
815 | $sessionId = $this->session ? $this->session->getId() : 0; |
||
816 | $forumInfo = get_forums($forumId, $this->course->getCode(), true, $sessionId); |
||
817 | |||
818 | if (!isset($forumInfo['iid'])) { |
||
819 | throw new Exception(get_lang('NoForum')); |
||
820 | } |
||
821 | |||
822 | $webCoursePath = api_get_path(WEB_COURSE_PATH).$this->course->getDirectory().'/upload/forum/images/'; |
||
823 | $forum = [ |
||
824 | 'id' => $forumInfo['iid'], |
||
825 | 'title' => $forumInfo['forum_title'], |
||
826 | 'description' => $forumInfo['forum_comment'], |
||
827 | 'image' => $forumInfo['forum_image'] ? ($webCoursePath.$forumInfo['forum_image']) : '', |
||
828 | 'threads' => [], |
||
829 | ]; |
||
830 | |||
831 | $threads = get_threads($forumInfo['iid'], $this->course->getId(), $sessionId); |
||
832 | |||
833 | foreach ($threads as $thread) { |
||
834 | $forum['threads'][] = [ |
||
835 | 'id' => $thread['iid'], |
||
836 | 'title' => $thread['thread_title'], |
||
837 | 'lastEditDate' => api_convert_and_format_date($thread['lastedit_date'], DATE_TIME_FORMAT_LONG_24H), |
||
838 | 'numberOfReplies' => $thread['thread_replies'], |
||
839 | 'numberOfViews' => $thread['thread_views'], |
||
840 | 'author' => api_get_person_name($thread['firstname'], $thread['lastname']), |
||
841 | ]; |
||
842 | } |
||
843 | |||
844 | return $forum; |
||
845 | } |
||
846 | |||
847 | /** |
||
848 | * @param int $forumId |
||
849 | * @param int $threadId |
||
850 | * |
||
851 | * @return array |
||
852 | */ |
||
853 | public function getCourseForumThread($forumId, $threadId) |
||
854 | { |
||
855 | Event::event_access_tool(TOOL_FORUM); |
||
856 | |||
857 | require_once api_get_path(SYS_CODE_PATH).'forum/forumfunction.inc.php'; |
||
858 | |||
859 | $sessionId = $this->session ? $this->session->getId() : 0; |
||
860 | $threadInfo = get_thread_information($forumId, $threadId, $sessionId); |
||
861 | |||
862 | $thread = [ |
||
863 | 'id' => intval($threadInfo['iid']), |
||
864 | 'cId' => intval($threadInfo['c_id']), |
||
865 | 'title' => $threadInfo['thread_title'], |
||
866 | 'forumId' => intval($threadInfo['forum_id']), |
||
867 | 'posts' => [], |
||
868 | ]; |
||
869 | |||
870 | $forumInfo = get_forums($threadInfo['forum_id'], $this->course->getCode(), true, $sessionId); |
||
871 | $postsInfo = getPosts($forumInfo, $threadInfo['iid'], 'ASC'); |
||
872 | |||
873 | foreach ($postsInfo as $postInfo) { |
||
874 | $thread['posts'][] = [ |
||
875 | 'id' => $postInfo['iid'], |
||
876 | 'title' => $postInfo['post_title'], |
||
877 | 'text' => $postInfo['post_text'], |
||
878 | 'author' => api_get_person_name($postInfo['firstname'], $postInfo['lastname']), |
||
879 | 'date' => api_convert_and_format_date($postInfo['post_date'], DATE_TIME_FORMAT_LONG_24H), |
||
880 | 'parentId' => $postInfo['post_parent_id'], |
||
881 | ]; |
||
882 | } |
||
883 | |||
884 | return $thread; |
||
885 | } |
||
886 | |||
887 | public function getCourseLinks(): array |
||
888 | { |
||
889 | Event::event_access_tool(TOOL_LINK); |
||
890 | |||
891 | $courseId = $this->course->getId(); |
||
892 | $sessionId = $this->session ? $this->session->getId() : 0; |
||
893 | |||
894 | $webCodePath = api_get_path(WEB_CODE_PATH); |
||
895 | $cidReq = api_get_cidreq(); |
||
896 | |||
897 | $categories = array_merge( |
||
898 | [ |
||
899 | [ |
||
900 | 'iid' => 0, |
||
901 | 'c_id' => $courseId, |
||
902 | 'id' => 0, |
||
903 | 'category_title' => get_lang('NoCategory'), |
||
904 | 'description' => '', |
||
905 | 'display_order' => 0, |
||
906 | 'session_id' => $sessionId, |
||
907 | 'visibility' => 1, |
||
908 | ], |
||
909 | ], |
||
910 | Link::getLinkCategories($courseId, $sessionId) |
||
911 | ); |
||
912 | |||
913 | $categories = array_filter( |
||
914 | $categories, |
||
915 | function (array $category) { |
||
916 | return $category['visibility'] != 0; |
||
917 | } |
||
918 | ); |
||
919 | |||
920 | return array_map( |
||
921 | function (array $category) use ($webCodePath, $cidReq, $courseId, $sessionId) { |
||
922 | $links = array_filter( |
||
923 | Link::getLinksPerCategory($category['iid'], $courseId, $sessionId), |
||
924 | function (array $link) { |
||
925 | return $link['visibility'] != 0; |
||
926 | } |
||
927 | ); |
||
928 | |||
929 | $links = array_map( |
||
930 | function (array $link) use ($webCodePath, $cidReq) { |
||
931 | return [ |
||
932 | 'id' => (int) $link['id'], |
||
933 | 'title' => Security::remove_XSS($link['title']), |
||
934 | 'description' => Security::remove_XSS($link['description']), |
||
935 | 'visibility' => (int) $link['visibility'], |
||
936 | 'url' => $webCodePath."link/link_goto.php?$cidReq&link_id=".$link['id'], |
||
937 | ]; |
||
938 | }, |
||
939 | $links |
||
940 | ); |
||
941 | |||
942 | return [ |
||
943 | 'id' => (int) $category['iid'], |
||
944 | 'title' => Security::remove_XSS($category['category_title']), |
||
945 | 'description' => Security::remove_XSS($category['description']), |
||
946 | 'visibility' => (int) $category['visibility'], |
||
947 | 'links' => $links, |
||
948 | ]; |
||
949 | }, |
||
950 | $categories |
||
951 | ); |
||
952 | } |
||
953 | |||
954 | /** |
||
955 | * @return array |
||
956 | */ |
||
957 | public function getUserProfile() |
||
958 | { |
||
959 | $pictureInfo = UserManager::get_user_picture_path_by_id($this->user->getId(), 'web'); |
||
960 | |||
961 | $result = [ |
||
962 | 'pictureUri' => $pictureInfo['dir'].$pictureInfo['file'], |
||
963 | 'id' => $this->user->getId(), |
||
964 | 'status' => $this->user->getStatus(), |
||
965 | 'fullName' => UserManager::formatUserFullName($this->user), |
||
966 | 'username' => $this->user->getUsername(), |
||
967 | 'officialCode' => $this->user->getOfficialCode(), |
||
968 | 'phone' => $this->user->getPhone(), |
||
969 | 'extra' => [], |
||
970 | ]; |
||
971 | |||
972 | $fieldValue = new ExtraFieldValue('user'); |
||
973 | $extraInfo = $fieldValue->getAllValuesForAnItem($this->user->getId(), true); |
||
974 | |||
975 | foreach ($extraInfo as $extra) { |
||
976 | /** @var ExtraFieldValues $extraValue */ |
||
977 | $extraValue = $extra['value']; |
||
978 | $result['extra'][] = [ |
||
979 | 'title' => $extraValue->getField()->getDisplayText(true), |
||
980 | 'value' => $extraValue->getValue(), |
||
981 | ]; |
||
982 | } |
||
983 | |||
984 | return $result; |
||
985 | } |
||
986 | |||
987 | public function getCourseLpProgress() |
||
988 | { |
||
989 | $sessionId = $this->session ? $this->session->getId() : 0; |
||
990 | $userId = $this->user->getId(); |
||
991 | |||
992 | /*$sessionId = $this->session ? $this->session->getId() : 0; |
||
993 | $courseId = $this->course->getId();*/ |
||
994 | |||
995 | $result = Tracking::getCourseLpProgress($userId, $sessionId); |
||
996 | |||
997 | return [$result]; |
||
998 | } |
||
999 | |||
1000 | /** |
||
1001 | * @throws Exception |
||
1002 | * |
||
1003 | * @return array |
||
1004 | */ |
||
1005 | public function getCourseLearnPaths() |
||
1006 | { |
||
1007 | Event::event_access_tool(TOOL_LEARNPATH); |
||
1008 | |||
1009 | $sessionId = $this->session ? $this->session->getId() : 0; |
||
1010 | $categoriesTempList = learnpath::getCategories($this->course->getId()); |
||
1011 | |||
1012 | $categoryNone = new CLpCategory(); |
||
1013 | $categoryNone->setId(0); |
||
1014 | $categoryNone->setName(get_lang('WithOutCategory')); |
||
1015 | $categoryNone->setPosition(0); |
||
1016 | |||
1017 | $categories = array_merge([$categoryNone], $categoriesTempList); |
||
1018 | $categoryData = []; |
||
1019 | |||
1020 | /** @var CLpCategory $category */ |
||
1021 | foreach ($categories as $category) { |
||
1022 | $learnPathList = new LearnpathList( |
||
1023 | $this->user->getId(), |
||
1024 | api_get_course_info($this->course->getCode()), |
||
1025 | $sessionId, |
||
1026 | null, |
||
1027 | false, |
||
1028 | $category->getId() |
||
1029 | ); |
||
1030 | |||
1031 | $flatLpList = $learnPathList->get_flat_list(); |
||
1032 | |||
1033 | if (empty($flatLpList)) { |
||
1034 | continue; |
||
1035 | } |
||
1036 | |||
1037 | $listData = []; |
||
1038 | |||
1039 | foreach ($flatLpList as $lpId => $lpDetails) { |
||
1040 | if ($lpDetails['lp_visibility'] == 0) { |
||
1041 | continue; |
||
1042 | } |
||
1043 | |||
1044 | if (!learnpath::is_lp_visible_for_student( |
||
1045 | $lpId, |
||
1046 | $this->user->getId(), |
||
1047 | api_get_course_info($this->course->getCode()), |
||
1048 | $sessionId |
||
1049 | )) { |
||
1050 | continue; |
||
1051 | } |
||
1052 | |||
1053 | $timeLimits = false; |
||
1054 | |||
1055 | // This is an old LP (from a migration 1.8.7) so we do nothing |
||
1056 | if (empty($lpDetails['created_on']) && empty($lpDetails['modified_on'])) { |
||
1057 | $timeLimits = false; |
||
1058 | } |
||
1059 | |||
1060 | // Checking if expired_on is ON |
||
1061 | if (!empty($lpDetails['expired_on'])) { |
||
1062 | $timeLimits = true; |
||
1063 | } |
||
1064 | |||
1065 | if ($timeLimits) { |
||
1066 | if (!empty($lpDetails['publicated_on']) && !empty($lpDetails['expired_on'])) { |
||
1067 | $startTime = api_strtotime($lpDetails['publicated_on'], 'UTC'); |
||
1068 | $endTime = api_strtotime($lpDetails['expired_on'], 'UTC'); |
||
1069 | $now = time(); |
||
1070 | $isActiveTime = false; |
||
1071 | |||
1072 | if ($now > $startTime && $endTime > $now) { |
||
1073 | $isActiveTime = true; |
||
1074 | } |
||
1075 | |||
1076 | if (!$isActiveTime) { |
||
1077 | continue; |
||
1078 | } |
||
1079 | } |
||
1080 | } |
||
1081 | |||
1082 | $progress = learnpath::getProgress($lpId, $this->user->getId(), $this->course->getId(), $sessionId); |
||
1083 | |||
1084 | $listData[] = [ |
||
1085 | 'id' => $lpId, |
||
1086 | 'title' => Security::remove_XSS($lpDetails['lp_name']), |
||
1087 | 'progress' => $progress, |
||
1088 | 'url' => api_get_path(WEB_CODE_PATH).'webservices/api/v2.php?'.http_build_query( |
||
1089 | [ |
||
1090 | 'hash' => $this->encodeParams( |
||
1091 | [ |
||
1092 | 'action' => 'course_learnpath', |
||
1093 | 'lp_id' => $lpId, |
||
1094 | 'course' => $this->course->getId(), |
||
1095 | 'session' => $sessionId, |
||
1096 | ] |
||
1097 | ), |
||
1098 | ] |
||
1099 | ), |
||
1100 | ]; |
||
1101 | } |
||
1102 | |||
1103 | if (empty($listData)) { |
||
1104 | continue; |
||
1105 | } |
||
1106 | |||
1107 | $categoryData[] = [ |
||
1108 | 'id' => $category->getId(), |
||
1109 | 'name' => $category->getName(), |
||
1110 | 'learnpaths' => $listData, |
||
1111 | ]; |
||
1112 | } |
||
1113 | |||
1114 | return $categoryData; |
||
1115 | } |
||
1116 | |||
1117 | /** |
||
1118 | * Start login for a user. Then make a redirect to show the learnpath. |
||
1119 | * |
||
1120 | * @param int $lpId |
||
1121 | */ |
||
1122 | public function showLearningPath($lpId) |
||
1123 | { |
||
1124 | $loggedUser['user_id'] = $this->user->getId(); |
||
1125 | $loggedUser['status'] = $this->user->getStatus(); |
||
1126 | $loggedUser['uidReset'] = true; |
||
1127 | $sessionId = $this->session ? $this->session->getId() : 0; |
||
1128 | |||
1129 | ChamiloSession::write('_user', $loggedUser); |
||
1130 | Login::init_user($this->user->getId(), true); |
||
1131 | |||
1132 | $url = api_get_path(WEB_CODE_PATH).'lp/lp_controller.php?'.http_build_query( |
||
1133 | [ |
||
1134 | 'cidReq' => $this->course->getCode(), |
||
1135 | 'id_session' => $sessionId, |
||
1136 | 'gidReq' => 0, |
||
1137 | 'gradebook' => 0, |
||
1138 | 'origin' => '', |
||
1139 | 'action' => 'view', |
||
1140 | 'lp_id' => (int) $lpId, |
||
1141 | 'isStudentView' => 'true', |
||
1142 | ] |
||
1143 | ); |
||
1144 | |||
1145 | header("Location: $url"); |
||
1146 | exit; |
||
1147 | } |
||
1148 | |||
1149 | /** |
||
1150 | * @param int $forumId |
||
1151 | * |
||
1152 | * @return array |
||
1153 | */ |
||
1154 | public function saveForumPost(array $postValues, $forumId) |
||
1155 | { |
||
1156 | Event::event_access_tool(TOOL_FORUM); |
||
1157 | |||
1158 | require_once api_get_path(SYS_CODE_PATH).'forum/forumfunction.inc.php'; |
||
1159 | |||
1160 | $forum = get_forums($forumId, $this->course->getCode()); |
||
1161 | store_reply($forum, $postValues, $this->course->getId(), $this->user->getId()); |
||
1162 | |||
1163 | return [ |
||
1164 | 'registered' => true, |
||
1165 | ]; |
||
1166 | } |
||
1167 | |||
1168 | /** |
||
1169 | * Get the list of sessions for current user. |
||
1170 | * |
||
1171 | * @return array the sessions list |
||
1172 | */ |
||
1173 | public function getUserSessions() |
||
1174 | { |
||
1175 | $data = []; |
||
1176 | $sessionsByCategory = UserManager::get_sessions_by_category($this->user->getId(), false); |
||
1177 | |||
1178 | foreach ($sessionsByCategory as $category) { |
||
1179 | $categorySessions = []; |
||
1180 | |||
1181 | foreach ($category['sessions'] as $sessions) { |
||
1182 | $sessionCourses = []; |
||
1183 | |||
1184 | foreach ($sessions['courses'] as $course) { |
||
1185 | $courseInfo = api_get_course_info_by_id($course['real_id']); |
||
1186 | $teachers = SessionManager::getCoachesByCourseSessionToString( |
||
1187 | $sessions['session_id'], |
||
1188 | $course['real_id'] |
||
1189 | ); |
||
1190 | |||
1191 | $sessionCourses[] = [ |
||
1192 | 'id' => $courseInfo['real_id'], |
||
1193 | 'title' => $courseInfo['title'], |
||
1194 | 'code' => $courseInfo['code'], |
||
1195 | 'directory' => $courseInfo['directory'], |
||
1196 | 'pictureUrl' => $courseInfo['course_image_large'], |
||
1197 | 'urlPicture' => $courseInfo['course_image_large'], |
||
1198 | 'teachers' => $teachers, |
||
1199 | ]; |
||
1200 | } |
||
1201 | |||
1202 | $sessionBox = Display::getSessionTitleBox($sessions['session_id']); |
||
1203 | |||
1204 | $categorySessions[] = [ |
||
1205 | 'name' => $sessionBox['title'], |
||
1206 | 'id' => $sessions['session_id'], |
||
1207 | 'date' => $sessionBox['dates'], |
||
1208 | 'duration' => isset($sessionBox['duration']) ? $sessionBox['duration'] : null, |
||
1209 | 'courses' => $sessionCourses, |
||
1210 | ]; |
||
1211 | } |
||
1212 | |||
1213 | $data[] = [ |
||
1214 | 'id' => $category['session_category']['id'], |
||
1215 | 'name' => $category['session_category']['name'], |
||
1216 | 'sessions' => $categorySessions, |
||
1217 | ]; |
||
1218 | } |
||
1219 | |||
1220 | return $data; |
||
1221 | } |
||
1222 | |||
1223 | public function getUsersSubscribedToCourse() |
||
1224 | { |
||
1225 | $users = CourseManager::get_user_list_from_course_code($this->course->getCode()); |
||
1226 | |||
1227 | $userList = []; |
||
1228 | foreach ($users as $user) { |
||
1229 | $userList[] = [ |
||
1230 | 'user_id' => $user['user_id'], |
||
1231 | 'username' => $user['username'], |
||
1232 | 'firstname' => $user['firstname'], |
||
1233 | 'lastname' => $user['lastname'], |
||
1234 | 'status_rel' => $user['status_rel'], |
||
1235 | ]; |
||
1236 | } |
||
1237 | |||
1238 | return $userList; |
||
1239 | } |
||
1240 | |||
1241 | /** |
||
1242 | * @param string $subject |
||
1243 | * @param string $text |
||
1244 | * |
||
1245 | * @return array |
||
1246 | */ |
||
1247 | public function saveUserMessage($subject, $text, array $receivers) |
||
1248 | { |
||
1249 | foreach ($receivers as $userId) { |
||
1250 | MessageManager::send_message($userId, $subject, $text); |
||
1251 | } |
||
1252 | |||
1253 | return [ |
||
1254 | 'sent' => true, |
||
1255 | ]; |
||
1256 | } |
||
1257 | |||
1258 | /** |
||
1259 | * @param string $search |
||
1260 | * |
||
1261 | * @return array |
||
1262 | */ |
||
1263 | public function getMessageUsers($search) |
||
1264 | { |
||
1265 | $repo = UserManager::getRepository(); |
||
1266 | |||
1267 | $users = $repo->findUsersToSendMessage($this->user->getId(), $search); |
||
1268 | $showEmail = api_get_setting('show_email_addresses') === 'true'; |
||
1269 | $data = []; |
||
1270 | |||
1271 | /** @var User $user */ |
||
1272 | foreach ($users as $user) { |
||
1273 | $userName = UserManager::formatUserFullName($user); |
||
1274 | |||
1275 | if ($showEmail) { |
||
1276 | $userName .= " ({$user->getEmail()})"; |
||
1277 | } |
||
1278 | |||
1279 | $data[] = [ |
||
1280 | 'id' => $user->getId(), |
||
1281 | 'name' => $userName, |
||
1282 | ]; |
||
1283 | } |
||
1284 | |||
1285 | return $data; |
||
1286 | } |
||
1287 | |||
1288 | /** |
||
1289 | * @param string $title |
||
1290 | * @param string $text |
||
1291 | * |
||
1292 | * @return array |
||
1293 | */ |
||
1294 | public function saveCourseNotebook($title, $text) |
||
1295 | { |
||
1296 | Event::event_access_tool(TOOL_NOTEBOOK); |
||
1297 | |||
1298 | $values = ['note_title' => $title, 'note_comment' => $text]; |
||
1299 | $sessionId = $this->session ? $this->session->getId() : 0; |
||
1300 | |||
1301 | $noteBookId = NotebookManager::save_note( |
||
1302 | $values, |
||
1303 | $this->user->getId(), |
||
1304 | $this->course->getId(), |
||
1305 | $sessionId |
||
1306 | ); |
||
1307 | |||
1308 | return [ |
||
1309 | 'registered' => $noteBookId, |
||
1310 | ]; |
||
1311 | } |
||
1312 | |||
1313 | /** |
||
1314 | * @param int $forumId |
||
1315 | * |
||
1316 | * @return array |
||
1317 | */ |
||
1318 | public function saveForumThread(array $values, $forumId) |
||
1319 | { |
||
1320 | Event::event_access_tool(TOOL_FORUM); |
||
1321 | |||
1322 | require_once api_get_path(SYS_CODE_PATH).'forum/forumfunction.inc.php'; |
||
1323 | |||
1324 | $sessionId = $this->session ? $this->session->getId() : 0; |
||
1325 | $forum = get_forums($forumId, $this->course->getCode(), true, $sessionId); |
||
1326 | $courseInfo = api_get_course_info($this->course->getCode()); |
||
1327 | $thread = store_thread($forum, $values, $courseInfo, false, $this->user->getId(), $sessionId); |
||
1328 | |||
1329 | return [ |
||
1330 | 'registered' => $thread->getIid(), |
||
1331 | ]; |
||
1332 | } |
||
1333 | |||
1334 | /** |
||
1335 | * @return array |
||
1336 | */ |
||
1337 | public function getUsersCampus(array $params) |
||
1338 | { |
||
1339 | $conditions = [ |
||
1340 | 'status' => $params['status'], |
||
1341 | ]; |
||
1342 | $idCampus = $params['id_campus']; |
||
1343 | $users = UserManager::get_user_list($conditions, ['firstname'], false, false, $idCampus); |
||
1344 | $list = []; |
||
1345 | foreach ($users as $item) { |
||
1346 | $listTemp = [ |
||
1347 | 'id' => $item['user_id'], |
||
1348 | 'firstname' => $item['firstname'], |
||
1349 | 'lastname' => $item['lastname'], |
||
1350 | 'email' => $item['email'], |
||
1351 | ]; |
||
1352 | $list[] = $listTemp; |
||
1353 | } |
||
1354 | |||
1355 | return $list; |
||
1356 | } |
||
1357 | |||
1358 | /** |
||
1359 | * @return array |
||
1360 | */ |
||
1361 | public function getCoursesCampus(array $params) |
||
1374 | ); |
||
1375 | } |
||
1376 | |||
1377 | /** |
||
1378 | * @return array |
||
1379 | */ |
||
1380 | public function addSession(array $params) |
||
1381 | { |
||
1382 | $name = $params['name']; |
||
1383 | $coach_username = (int) $params['coach_username']; |
||
1384 | $startDate = $params['access_start_date']; |
||
1385 | $endDate = $params['access_end_date']; |
||
1386 | $displayStartDate = $startDate; |
||
1387 | $displayEndDate = $endDate; |
||
1388 | $description = $params['description']; |
||
1389 | $idUrlCampus = $params['id_campus']; |
||
1390 | $extraFields = isset($params['extra']) ? $params['extra'] : []; |
||
1391 | |||
1392 | $return = SessionManager::create_session( |
||
1393 | $name, |
||
1394 | $startDate, |
||
1395 | $endDate, |
||
1396 | $displayStartDate, |
||
1397 | $displayEndDate, |
||
1398 | null, |
||
1399 | null, |
||
1400 | $coach_username, |
||
1401 | null, |
||
1402 | 1, |
||
1403 | false, |
||
1404 | null, |
||
1405 | $description, |
||
1406 | 1, |
||
1407 | $extraFields, |
||
1408 | null, |
||
1409 | false, |
||
1410 | $idUrlCampus |
||
1411 | ); |
||
1412 | |||
1413 | if ($return) { |
||
1414 | $out = [ |
||
1415 | 'status' => true, |
||
1416 | 'message' => get_lang('ANewSessionWasCreated'), |
||
1417 | 'id_session' => $return, |
||
1418 | ]; |
||
1419 | } else { |
||
1420 | $out = [ |
||
1421 | 'status' => false, |
||
1422 | 'message' => get_lang('ErrorOccurred'), |
||
1423 | ]; |
||
1424 | } |
||
1425 | |||
1426 | return $out; |
||
1427 | } |
||
1428 | |||
1429 | public function addCourse(array $courseParam): array |
||
1430 | { |
||
1431 | $idCampus = isset($courseParam['id_campus']) ? $courseParam['id_campus'] : 1; |
||
1432 | $title = isset($courseParam['title']) ? $courseParam['title'] : ''; |
||
1433 | $wantedCode = isset($courseParam['wanted_code']) ? $courseParam['wanted_code'] : null; |
||
1434 | $diskQuota = isset($courseParam['disk_quota']) ? $courseParam['disk_quota'] : '100'; |
||
1435 | $visibility = isset($courseParam['visibility']) ? (int) $courseParam['visibility'] : null; |
||
1436 | $removeCampusId = $courseParam['remove_campus_id_from_wanted_code'] ?? 0; |
||
1437 | $language = $courseParam['language'] ?? ''; |
||
1438 | |||
1439 | if (isset($courseParam['visibility'])) { |
||
1440 | if ($courseParam['visibility'] && |
||
1441 | $courseParam['visibility'] >= 0 && |
||
1442 | $courseParam['visibility'] <= 3 |
||
1443 | ) { |
||
1444 | $visibility = (int) $courseParam['visibility']; |
||
1445 | } |
||
1446 | } |
||
1447 | |||
1448 | $params = []; |
||
1449 | $params['title'] = $title; |
||
1450 | $params['wanted_code'] = 'CAMPUS_'.$idCampus.'_'.$wantedCode; |
||
1451 | if (1 === (int) $removeCampusId) { |
||
1452 | $params['wanted_code'] = $wantedCode; |
||
1453 | } |
||
1454 | $params['user_id'] = $this->user->getId(); |
||
1455 | $params['visibility'] = $visibility; |
||
1456 | $params['disk_quota'] = $diskQuota; |
||
1457 | $params['course_language'] = $language; |
||
1458 | |||
1459 | foreach ($courseParam as $key => $value) { |
||
1460 | if (substr($key, 0, 6) === 'extra_') { //an extra field |
||
1461 | $params[$key] = $value; |
||
1462 | } |
||
1463 | } |
||
1464 | |||
1465 | $courseInfo = CourseManager::create_course($params, $params['user_id'], $idCampus); |
||
1466 | $results = []; |
||
1467 | if (!empty($courseInfo)) { |
||
1468 | $results['status'] = true; |
||
1469 | $results['code_course'] = $courseInfo['code']; |
||
1470 | $results['title_course'] = $courseInfo['title']; |
||
1471 | $extraFieldValues = new ExtraFieldValue('course'); |
||
1472 | $extraFields = $extraFieldValues->getAllValuesByItem($courseInfo['real_id']); |
||
1473 | $results['extra_fields'] = $extraFields; |
||
1474 | $results['message'] = sprintf(get_lang('CourseXAdded'), $courseInfo['code']); |
||
1475 | } else { |
||
1476 | $results['status'] = false; |
||
1477 | $results['message'] = get_lang('CourseCreationFailed'); |
||
1478 | } |
||
1479 | |||
1480 | return $results; |
||
1481 | } |
||
1482 | |||
1483 | /** |
||
1484 | * @param $userParam |
||
1485 | * |
||
1486 | * @throws Exception |
||
1487 | * |
||
1488 | * @return array |
||
1489 | */ |
||
1490 | public function addUser($userParam) |
||
1491 | { |
||
1492 | $firstName = $userParam['firstname']; |
||
1493 | $lastName = $userParam['lastname']; |
||
1494 | $status = $userParam['status']; |
||
1495 | $email = $userParam['email']; |
||
1496 | $loginName = $userParam['loginname']; |
||
1497 | $password = $userParam['password']; |
||
1498 | |||
1499 | $official_code = ''; |
||
1500 | $language = ''; |
||
1501 | $phone = ''; |
||
1502 | $picture_uri = ''; |
||
1503 | $auth_source = $userParam['auth_source'] ?? PLATFORM_AUTH_SOURCE; |
||
1504 | $expiration_date = ''; |
||
1505 | $active = 1; |
||
1506 | $hr_dept_id = 0; |
||
1507 | $original_user_id_name = $userParam['original_user_id_name']; |
||
1508 | $original_user_id_value = $userParam['original_user_id_value']; |
||
1509 | |||
1510 | $extra_list = isset($userParam['extra']) ? $userParam['extra'] : []; |
||
1511 | if (isset($userParam['language'])) { |
||
1512 | $language = $userParam['language']; |
||
1513 | } |
||
1514 | if (isset($userParam['phone'])) { |
||
1515 | $phone = $userParam['phone']; |
||
1516 | } |
||
1517 | if (isset($userParam['expiration_date'])) { |
||
1518 | $expiration_date = $userParam['expiration_date']; |
||
1519 | } |
||
1520 | |||
1521 | // Default language. |
||
1522 | if (empty($language)) { |
||
1523 | $language = api_get_setting('platformLanguage'); |
||
1524 | } |
||
1525 | |||
1526 | // First check wether the login already exists. |
||
1527 | if (!UserManager::is_username_available($loginName)) { |
||
1528 | throw new Exception(get_lang('UserNameNotAvailable')); |
||
1529 | } |
||
1530 | |||
1531 | $userId = UserManager::create_user( |
||
1532 | $firstName, |
||
1533 | $lastName, |
||
1534 | $status, |
||
1535 | $email, |
||
1536 | $loginName, |
||
1537 | $password, |
||
1538 | $official_code, |
||
1539 | $language, |
||
1540 | $phone, |
||
1541 | $picture_uri, |
||
1542 | $auth_source, |
||
1543 | $expiration_date, |
||
1544 | $active, |
||
1545 | $hr_dept_id |
||
1546 | ); |
||
1547 | |||
1548 | if (empty($userId)) { |
||
1549 | throw new Exception(get_lang('UserNotRegistered')); |
||
1550 | } |
||
1551 | |||
1552 | if (api_is_multiple_url_enabled()) { |
||
1553 | if (api_get_current_access_url_id() != -1) { |
||
1554 | UrlManager::add_user_to_url( |
||
1555 | $userId, |
||
1556 | api_get_current_access_url_id() |
||
1557 | ); |
||
1558 | } else { |
||
1559 | UrlManager::add_user_to_url($userId, 1); |
||
1560 | } |
||
1561 | } else { |
||
1562 | // We add by default the access_url_user table with access_url_id = 1 |
||
1563 | UrlManager::add_user_to_url($userId, 1); |
||
1564 | } |
||
1565 | |||
1566 | // Save new field label into user_field table. |
||
1567 | UserManager::create_extra_field( |
||
1568 | $original_user_id_name, |
||
1569 | 1, |
||
1570 | $original_user_id_name, |
||
1571 | '' |
||
1572 | ); |
||
1573 | // Save the external system's id into user_field_value table. |
||
1574 | UserManager::update_extra_field_value( |
||
1575 | $userId, |
||
1576 | $original_user_id_name, |
||
1577 | $original_user_id_value |
||
1578 | ); |
||
1579 | |||
1580 | if (is_array($extra_list) && count($extra_list) > 0) { |
||
1581 | foreach ($extra_list as $extra) { |
||
1582 | $extra_field_name = $extra['field_name']; |
||
1583 | $extra_field_value = $extra['field_value']; |
||
1584 | // Save new field label into user_field table. |
||
1585 | UserManager::create_extra_field( |
||
1586 | $extra_field_name, |
||
1587 | 1, |
||
1588 | $extra_field_name, |
||
1589 | '' |
||
1590 | ); |
||
1591 | // Save the external system's id into user_field_value table. |
||
1592 | UserManager::update_extra_field_value( |
||
1593 | $userId, |
||
1594 | $extra_field_name, |
||
1595 | $extra_field_value |
||
1596 | ); |
||
1597 | } |
||
1598 | } |
||
1599 | |||
1600 | return [$userId]; |
||
1601 | } |
||
1602 | |||
1603 | /** |
||
1604 | * Subscribe User to Course. |
||
1605 | * |
||
1606 | * @param array $params |
||
1607 | * |
||
1608 | * @return array |
||
1609 | */ |
||
1610 | public function subscribeUserToCourse($params) |
||
1611 | { |
||
1612 | $course_id = $params['course_id']; |
||
1613 | $course_code = $params['course_code']; |
||
1614 | $user_id = $params['user_id']; |
||
1615 | $status = $params['status'] ?? STUDENT; |
||
1616 | |||
1617 | if (!$course_id && !$course_code) { |
||
1618 | return [false]; |
||
1619 | } |
||
1620 | if (!$course_code) { |
||
1621 | $course_code = CourseManager::get_course_code_from_course_id($course_id); |
||
1622 | } |
||
1623 | |||
1624 | if (CourseManager::subscribeUser($user_id, $course_code, $status, 0, 0, false)) { |
||
1625 | return [true]; |
||
1626 | } |
||
1627 | |||
1628 | return [false]; |
||
1629 | } |
||
1630 | |||
1631 | /** |
||
1632 | * @throws Exception |
||
1633 | */ |
||
1634 | public function subscribeUserToCoursePassword($courseCode, $password) |
||
1635 | { |
||
1636 | $courseInfo = api_get_course_info($courseCode); |
||
1637 | |||
1638 | if (empty($courseInfo)) { |
||
1639 | throw new Exception(get_lang('NoCourse')); |
||
1640 | } |
||
1641 | |||
1642 | if (sha1($password) === $courseInfo['registration_code']) { |
||
1643 | CourseManager::processAutoSubscribeToCourse($courseCode); |
||
1644 | |||
1645 | return; |
||
1646 | } |
||
1647 | |||
1648 | throw new Exception(get_lang('CourseRegistrationCodeIncorrect')); |
||
1649 | } |
||
1650 | |||
1651 | public function unSubscribeUserToCourse(array $params): array |
||
1652 | { |
||
1653 | $courseId = $params['course_id']; |
||
1654 | $courseCode = $params['course_code']; |
||
1655 | $userId = $params['user_id']; |
||
1656 | |||
1657 | if (!$courseId && !$courseCode) { |
||
1658 | return [false]; |
||
1659 | } |
||
1660 | |||
1661 | if (!$courseCode) { |
||
1662 | $courseCode = CourseManager::get_course_code_from_course_id($courseId); |
||
1663 | } |
||
1664 | |||
1665 | if (CourseManager::unsubscribe_user($userId, $courseCode)) { |
||
1666 | return [true]; |
||
1667 | } |
||
1668 | |||
1669 | return [false]; |
||
1670 | } |
||
1671 | |||
1672 | public function deleteUserMessage($messageId, $messageType) |
||
1673 | { |
||
1674 | if ($messageType === 'sent') { |
||
1675 | return MessageManager::delete_message_by_user_sender($this->user->getId(), $messageId); |
||
1676 | } else { |
||
1677 | return MessageManager::delete_message_by_user_receiver($this->user->getId(), $messageId); |
||
1678 | } |
||
1679 | } |
||
1680 | |||
1681 | public function setMessageRead($messageId) |
||
1682 | { |
||
1683 | MessageManager::update_message($this->user->getId(), $messageId); |
||
1684 | } |
||
1685 | |||
1686 | /** |
||
1687 | * Add Campus Virtual. |
||
1688 | * |
||
1689 | * @param array Params Campus |
||
1690 | * |
||
1691 | * @return array |
||
1692 | */ |
||
1693 | public function createCampusURL($params) |
||
1694 | { |
||
1695 | $urlCampus = Security::remove_XSS($params['url']); |
||
1696 | $description = Security::remove_XSS($params['description']); |
||
1697 | |||
1698 | $active = isset($params['active']) ? intval($params['active']) : 0; |
||
1699 | $num = UrlManager::url_exist($urlCampus); |
||
1700 | if ($num == 0) { |
||
1701 | // checking url |
||
1702 | if (substr($urlCampus, strlen($urlCampus) - 1, strlen($urlCampus)) == '/') { |
||
1703 | $idCampus = UrlManager::add($urlCampus, $description, $active, true); |
||
1704 | } else { |
||
1705 | //create |
||
1706 | $idCampus = UrlManager::add($urlCampus.'/', $description, $active, true); |
||
1707 | } |
||
1708 | |||
1709 | return [ |
||
1710 | 'status' => true, |
||
1711 | 'id_campus' => $idCampus, |
||
1712 | ]; |
||
1713 | } |
||
1714 | |||
1715 | return [ |
||
1716 | 'status' => false, |
||
1717 | 'id_campus' => 0, |
||
1718 | ]; |
||
1719 | } |
||
1720 | |||
1721 | /** |
||
1722 | * Edit Campus Virtual. |
||
1723 | * |
||
1724 | * @param array Params Campus |
||
1725 | * |
||
1726 | * @return array |
||
1727 | */ |
||
1728 | public function editCampusURL($params) |
||
1729 | { |
||
1730 | $urlCampus = Security::remove_XSS($params['url']); |
||
1731 | $description = Security::remove_XSS($params['description']); |
||
1732 | |||
1733 | $active = isset($params['active']) ? intval($params['active']) : 0; |
||
1734 | $url_id = isset($params['id']) ? intval($params['id']) : 0; |
||
1735 | |||
1736 | if (!empty($url_id)) { |
||
1737 | //we can't change the status of the url with id=1 |
||
1738 | if ($url_id == 1) { |
||
1739 | $active = 1; |
||
1740 | } |
||
1741 | //checking url |
||
1742 | if (substr($urlCampus, strlen($urlCampus) - 1, strlen($urlCampus)) == '/') { |
||
1743 | UrlManager::update($url_id, $urlCampus, $description, $active); |
||
1744 | } else { |
||
1745 | UrlManager::update($url_id, $urlCampus.'/', $description, $active); |
||
1746 | } |
||
1747 | |||
1748 | return [true]; |
||
1749 | } |
||
1750 | |||
1751 | return [false]; |
||
1752 | } |
||
1753 | |||
1754 | /** |
||
1755 | * Delete Campus Virtual. |
||
1756 | * |
||
1757 | * @param array Params Campus |
||
1758 | * |
||
1759 | * @return array |
||
1760 | */ |
||
1761 | public function deleteCampusURL($params) |
||
1762 | { |
||
1763 | $url_id = isset($params['id']) ? intval($params['id']) : 0; |
||
1764 | |||
1765 | $result = UrlManager::delete($url_id); |
||
1766 | if ($result) { |
||
1767 | return [ |
||
1768 | 'status' => true, |
||
1769 | 'message' => get_lang('URLDeleted'), |
||
1770 | ]; |
||
1771 | } else { |
||
1772 | return [ |
||
1773 | 'status' => false, |
||
1774 | 'message' => get_lang('Error'), |
||
1775 | ]; |
||
1776 | } |
||
1777 | } |
||
1778 | |||
1779 | /** |
||
1780 | * @throws Exception |
||
1781 | * |
||
1782 | * @return array |
||
1783 | */ |
||
1784 | public function addCoursesSession(array $params) |
||
1809 | ]; |
||
1810 | } |
||
1811 | |||
1812 | /** |
||
1813 | * @return array |
||
1814 | */ |
||
1815 | public function addUsersSession(array $params) |
||
1816 | { |
||
1817 | $sessionId = $params['id_session']; |
||
1818 | $userList = $params['list_users']; |
||
1819 | |||
1820 | if (!is_array($userList)) { |
||
1821 | $userList = []; |
||
1822 | } |
||
1823 | |||
1824 | SessionManager::subscribeUsersToSession( |
||
1825 | $sessionId, |
||
1826 | $userList, |
||
1827 | null, |
||
1828 | false |
||
1829 | ); |
||
1830 | |||
1831 | return [ |
||
1832 | 'status' => true, |
||
1833 | 'message' => get_lang('UsersAdded'), |
||
1834 | ]; |
||
1835 | } |
||
1836 | |||
1837 | /** |
||
1838 | * Creates a session from a model session. |
||
1839 | * |
||
1840 | * @param $modelSessionId |
||
1841 | * @param $sessionName |
||
1842 | * @param $startDate |
||
1843 | * @param $endDate |
||
1844 | * |
||
1845 | * @throws Exception |
||
1846 | * |
||
1847 | * @return int, the id of the new session |
||
1848 | */ |
||
1849 | public function createSessionFromModel($modelSessionId, $sessionName, $startDate, $endDate, array $extraFields = []) |
||
1850 | { |
||
1851 | if (empty($modelSessionId) || empty($sessionName) || empty($startDate) || empty($endDate)) { |
||
1852 | throw new Exception(get_lang('NoData')); |
||
1853 | } |
||
1854 | |||
1855 | if (!SessionManager::isValidId($modelSessionId)) { |
||
1856 | throw new Exception(get_lang('ModelSessionDoesNotExist')); |
||
1857 | } |
||
1858 | |||
1859 | $modelSession = SessionManager::fetch($modelSessionId); |
||
1860 | |||
1861 | $modelSession['accessUrlId'] = 1; |
||
1862 | if (api_is_multiple_url_enabled()) { |
||
1863 | if (api_get_current_access_url_id() != -1) { |
||
1864 | $modelSession['accessUrlId'] = api_get_current_access_url_id(); |
||
1865 | } |
||
1866 | } |
||
1867 | |||
1868 | $newSessionId = SessionManager::create_session( |
||
1869 | $sessionName, |
||
1870 | $startDate, |
||
1871 | $endDate, |
||
1872 | $startDate, |
||
1873 | $endDate, |
||
1874 | $startDate, |
||
1875 | $endDate, |
||
1876 | $modelSession['id_coach'], |
||
1877 | $modelSession['session_category_id'], |
||
1878 | $modelSession['visibility'], |
||
1879 | false, |
||
1880 | $modelSession['duration'], |
||
1881 | $modelSession['description'], |
||
1882 | $modelSession['show_description'], |
||
1883 | $extraFields, |
||
1884 | $modelSession['session_admin_id'], |
||
1885 | $modelSession['send_subscription_notification'], |
||
1886 | $modelSession['accessUrlId'] |
||
1887 | ); |
||
1888 | |||
1889 | if (empty($newSessionId)) { |
||
1890 | throw new Exception(get_lang('SessionNotRegistered')); |
||
1891 | } |
||
1892 | |||
1893 | if (is_string($newSessionId)) { |
||
1894 | throw new Exception($newSessionId); |
||
1895 | } |
||
1896 | |||
1897 | $promotionId = $modelSession['promotion_id']; |
||
1898 | if ($promotionId) { |
||
1899 | $sessionList = array_keys(SessionManager::get_all_sessions_by_promotion($promotionId)); |
||
1900 | $sessionList[] = $newSessionId; |
||
1901 | SessionManager::subscribe_sessions_to_promotion($modelSession['promotion_id'], $sessionList); |
||
1902 | } |
||
1903 | |||
1904 | $modelExtraFields = []; |
||
1905 | $fields = SessionManager::getFilteredExtraFields($modelSessionId); |
||
1906 | if (is_array($fields) and !empty($fields)) { |
||
1907 | foreach ($fields as $field) { |
||
1908 | $modelExtraFields[$field['variable']] = $field['value']; |
||
1909 | } |
||
1910 | } |
||
1911 | $allExtraFields = array_merge($modelExtraFields, $extraFields); |
||
1912 | foreach ($allExtraFields as $name => $value) { |
||
1913 | // SessionManager::update_session_extra_field_value returns false when no row is changed, |
||
1914 | // which can happen since extra field values are initialized by SessionManager::create_session |
||
1915 | // therefore we do not throw an exception when false is returned |
||
1916 | SessionManager::update_session_extra_field_value($newSessionId, $name, $value); |
||
1917 | } |
||
1918 | |||
1919 | $courseList = array_keys(SessionManager::get_course_list_by_session_id($modelSessionId)); |
||
1920 | if (is_array($courseList) |
||
1921 | && !empty($courseList) |
||
1922 | && !SessionManager::add_courses_to_session($newSessionId, $courseList)) { |
||
1923 | throw new Exception(get_lang('CoursesNotAddedToSession')); |
||
1924 | } |
||
1925 | |||
1926 | if (api_is_multiple_url_enabled()) { |
||
1927 | if (api_get_current_access_url_id() != -1) { |
||
1928 | UrlManager::add_session_to_url( |
||
1929 | $newSessionId, |
||
1930 | api_get_current_access_url_id() |
||
1931 | ); |
||
1932 | } else { |
||
1933 | UrlManager::add_session_to_url($newSessionId, 1); |
||
1934 | } |
||
1935 | } else { |
||
1936 | UrlManager::add_session_to_url($newSessionId, 1); |
||
1937 | } |
||
1938 | |||
1939 | return $newSessionId; |
||
1940 | } |
||
1941 | |||
1942 | /** |
||
1943 | * subscribes a user to a session. |
||
1944 | * |
||
1945 | * @param int $sessionId the session id |
||
1946 | * @param string $loginName the user's login name |
||
1947 | * |
||
1948 | * @throws Exception |
||
1949 | * |
||
1950 | * @return boolean, whether it worked |
||
1951 | */ |
||
1952 | public function subscribeUserToSessionFromUsername($sessionId, $loginName) |
||
1953 | { |
||
1954 | if (!SessionManager::isValidId($sessionId)) { |
||
1955 | throw new Exception(get_lang('SessionNotFound')); |
||
1956 | } |
||
1957 | |||
1958 | $userId = UserManager::get_user_id_from_username($loginName); |
||
1959 | if (false === $userId) { |
||
1960 | throw new Exception(get_lang('UserNotFound')); |
||
1961 | } |
||
1962 | |||
1963 | $subscribed = SessionManager::subscribeUsersToSession( |
||
1964 | $sessionId, |
||
1965 | [$userId], |
||
1966 | SESSION_VISIBLE_READ_ONLY, |
||
1967 | false |
||
1968 | ); |
||
1969 | if (!$subscribed) { |
||
1970 | throw new Exception(get_lang('UserNotSubscribed')); |
||
1971 | } |
||
1972 | |||
1973 | return true; |
||
1974 | } |
||
1975 | |||
1976 | /** |
||
1977 | * finds the session which has a specific value in a specific extra field. |
||
1978 | * |
||
1979 | * @param $fieldName |
||
1980 | * @param $fieldValue |
||
1981 | * |
||
1982 | * @throws Exception when no session matched or more than one session matched |
||
1983 | * |
||
1984 | * @return int, the matching session id |
||
1985 | */ |
||
1986 | public function getSessionFromExtraField($fieldName, $fieldValue) |
||
1987 | { |
||
1988 | // find sessions that that have value in field |
||
1989 | $valueModel = new ExtraFieldValue('session'); |
||
1990 | $sessionIdList = $valueModel->get_item_id_from_field_variable_and_field_value( |
||
1991 | $fieldName, |
||
1992 | $fieldValue, |
||
1993 | false, |
||
1994 | false, |
||
1995 | true |
||
1996 | ); |
||
1997 | |||
1998 | // throw if none found |
||
1999 | if (empty($sessionIdList)) { |
||
2000 | throw new Exception(get_lang('NoSessionMatched')); |
||
2001 | } |
||
2002 | |||
2003 | // throw if more than one found |
||
2004 | if (count($sessionIdList) > 1) { |
||
2005 | throw new Exception(get_lang('MoreThanOneSessionMatched')); |
||
2006 | } |
||
2007 | |||
2008 | // return sessionId |
||
2009 | return intval($sessionIdList[0]['item_id']); |
||
2010 | } |
||
2011 | |||
2012 | /** |
||
2013 | * updates a user identified by its login name. |
||
2014 | * |
||
2015 | * @param array $parameters |
||
2016 | * |
||
2017 | * @throws Exception on failure |
||
2018 | * |
||
2019 | * @return boolean, true on success |
||
2020 | */ |
||
2021 | public function updateUserFromUserName($parameters) |
||
2022 | { |
||
2023 | // find user |
||
2024 | $userId = null; |
||
2025 | if (!is_array($parameters) || empty($parameters)) { |
||
2026 | throw new Exception('NoData'); |
||
2027 | } |
||
2028 | foreach ($parameters as $name => $value) { |
||
2029 | if (strtolower($name) === 'loginname') { |
||
2030 | $userId = UserManager::get_user_id_from_username($value); |
||
2031 | if (false === $userId) { |
||
2032 | throw new Exception(get_lang('UserNotFound')); |
||
2033 | } |
||
2034 | break; |
||
2035 | } |
||
2036 | } |
||
2037 | if (is_null($userId)) { |
||
2038 | throw new Exception(get_lang('NoData')); |
||
2039 | } |
||
2040 | /** @var User $user */ |
||
2041 | $user = UserManager::getRepository()->find($userId); |
||
2042 | if (empty($user)) { |
||
2043 | throw new Exception(get_lang('CouldNotLoadUser')); |
||
2044 | } |
||
2045 | |||
2046 | // tell the world we are about to update a user |
||
2047 | $hook = HookUpdateUser::create(); |
||
2048 | if (!empty($hook)) { |
||
2049 | $hook->notifyUpdateUser(HOOK_EVENT_TYPE_PRE); |
||
2050 | } |
||
2051 | |||
2052 | // apply submitted modifications |
||
2053 | foreach ($parameters as $name => $value) { |
||
2054 | switch (strtolower($name)) { |
||
2055 | case 'email': |
||
2056 | $user->setEmail($value); |
||
2057 | break; |
||
2058 | case 'enabled': |
||
2059 | $user->setEnabled($value); |
||
2060 | break; |
||
2061 | case 'lastname': |
||
2062 | $user->setLastname($value); |
||
2063 | break; |
||
2064 | case 'firstname': |
||
2065 | $user->setFirstname($value); |
||
2066 | break; |
||
2067 | case 'phone': |
||
2068 | $user->setPhone($value); |
||
2069 | break; |
||
2070 | case 'address': |
||
2071 | $user->setAddress($value); |
||
2072 | break; |
||
2073 | case 'roles': |
||
2074 | $user->setRoles($value); |
||
2075 | break; |
||
2076 | case 'profile_completed': |
||
2077 | $user->setProfileCompleted($value); |
||
2078 | break; |
||
2079 | case 'auth_source': |
||
2080 | $user->setAuthSource($value); |
||
2081 | break; |
||
2082 | case 'status': |
||
2083 | $user->setStatus($value); |
||
2084 | break; |
||
2085 | case 'official_code': |
||
2086 | $user->setOfficialCode($value); |
||
2087 | break; |
||
2088 | case 'picture_uri': |
||
2089 | $user->setPictureUri($value); |
||
2090 | break; |
||
2091 | case 'creator_id': |
||
2092 | $user->setCreatorId($value); |
||
2093 | break; |
||
2094 | case 'competences': |
||
2095 | $user->setCompetences($value); |
||
2096 | break; |
||
2097 | case 'diplomas': |
||
2098 | $user->setDiplomas($value); |
||
2099 | break; |
||
2100 | case 'openarea': |
||
2101 | $user->setOpenArea($value); |
||
2102 | break; |
||
2103 | case 'teach': |
||
2104 | $user->setTeach($value); |
||
2105 | break; |
||
2106 | case 'productions': |
||
2107 | $user->setProductions($value); |
||
2108 | break; |
||
2109 | case 'language': |
||
2110 | $languages = api_get_languages(); |
||
2111 | if (!in_array($value, $languages['folder'])) { |
||
2112 | throw new Exception(get_lang('LanguageUnavailable')); |
||
2113 | } |
||
2114 | $user->setLanguage($value); |
||
2115 | break; |
||
2116 | case 'registration_date': |
||
2117 | $user->setRegistrationDate($value); |
||
2118 | break; |
||
2119 | case 'expiration_date': |
||
2120 | $user->setExpirationDate( |
||
2121 | new DateTime( |
||
2122 | api_get_utc_datetime($value), |
||
2123 | new DateTimeZone('UTC') |
||
2124 | ) |
||
2125 | ); |
||
2126 | break; |
||
2127 | case 'active': |
||
2128 | // see UserManager::update_user() usermanager.lib.php:1205 |
||
2129 | if ($user->getActive() != $value) { |
||
2130 | $user->setActive($value); |
||
2131 | Event::addEvent($value ? LOG_USER_ENABLE : LOG_USER_DISABLE, LOG_USER_ID, $userId); |
||
2132 | } |
||
2133 | break; |
||
2134 | case 'openid': |
||
2135 | $user->setOpenId($value); |
||
2136 | break; |
||
2137 | case 'theme': |
||
2138 | $user->setTheme($value); |
||
2139 | break; |
||
2140 | case 'hr_dept_id': |
||
2141 | $user->setHrDeptId($value); |
||
2142 | break; |
||
2143 | case 'extra': |
||
2144 | if (is_array($value)) { |
||
2145 | if (count($value) > 0) { |
||
2146 | if (is_array($value[0])) { |
||
2147 | foreach ($value as $field) { |
||
2148 | $fieldName = $field['field_name']; |
||
2149 | $fieldValue = $field['field_value']; |
||
2150 | if (!isset($fieldName) || !isset($fieldValue) || |
||
2151 | !UserManager::update_extra_field_value($userId, $fieldName, $fieldValue)) { |
||
2152 | throw new Exception(get_lang('CouldNotUpdateExtraFieldValue').': '.print_r($field, true)); |
||
2153 | } |
||
2154 | } |
||
2155 | } else { |
||
2156 | foreach ($value as $fieldName => $fieldValue) { |
||
2157 | if (!UserManager::update_extra_field_value($userId, $fieldName, $fieldValue)) { |
||
2158 | throw new Exception(get_lang('CouldNotUpdateExtraFieldValue').': '.$fieldName); |
||
2159 | } |
||
2160 | } |
||
2161 | } |
||
2162 | } |
||
2163 | } |
||
2164 | break; |
||
2165 | case 'username': |
||
2166 | case 'api_key': |
||
2167 | case 'action': |
||
2168 | case 'loginname': |
||
2169 | break; |
||
2170 | case 'email_canonical': |
||
2171 | case 'locked': |
||
2172 | case 'expired': |
||
2173 | case 'credentials_expired': |
||
2174 | case 'credentials_expire_at': |
||
2175 | case 'expires_at': |
||
2176 | case 'salt': |
||
2177 | case 'last_login': |
||
2178 | case 'created_at': |
||
2179 | case 'updated_at': |
||
2180 | case 'confirmation_token': |
||
2181 | case 'password_requested_at': |
||
2182 | case 'password': // see UserManager::update_user usermanager.lib.php:1182 |
||
2183 | case 'username_canonical': |
||
2184 | default: |
||
2185 | throw new Exception(get_lang('UnsupportedUpdate')." '$name'"); |
||
2186 | } |
||
2187 | } |
||
2188 | |||
2189 | // save modifications |
||
2190 | UserManager::getManager()->updateUser($user, true); |
||
2191 | |||
2192 | // tell the world we just updated this user |
||
2193 | if (!empty($hook)) { |
||
2194 | $hook->setEventData(['user' => $user]); |
||
2195 | $hook->notifyUpdateUser(HOOK_EVENT_TYPE_POST); |
||
2196 | } |
||
2197 | |||
2198 | // invalidate cache for this user |
||
2199 | $cacheAvailable = api_get_configuration_value('apc'); |
||
2200 | if ($cacheAvailable === true) { |
||
2201 | $apcVar = api_get_configuration_value('apc_prefix').'userinfo_'.$userId; |
||
2202 | if (apcu_exists($apcVar)) { |
||
2203 | apcu_delete($apcVar); |
||
2204 | } |
||
2205 | } |
||
2206 | |||
2207 | return true; |
||
2208 | } |
||
2209 | |||
2210 | /** |
||
2211 | * Returns whether a user login name exists. |
||
2212 | * |
||
2213 | * @param string $loginname the user login name |
||
2214 | * |
||
2215 | * @return bool whether the user login name exists |
||
2216 | */ |
||
2217 | public function usernameExist($loginname) |
||
2218 | { |
||
2219 | return false !== api_get_user_info_from_username($loginname); |
||
2220 | } |
||
2221 | |||
2222 | /** |
||
2223 | * This service roughly matches what the call to MDL's API core_course_get_contents function returns. |
||
2224 | * |
||
2225 | * @return array |
||
2226 | */ |
||
2227 | public function getCourseQuizMdlCompat() |
||
2228 | { |
||
2229 | $userId = $this->user->getId(); |
||
2230 | $courseId = $this->course->getId(); |
||
2231 | $sessionId = $this->session ? $this->session->getId() : 0; |
||
2232 | |||
2233 | $toolVisibility = CourseHome::getToolVisibility(TOOL_QUIZ, $courseId, $sessionId); |
||
2234 | |||
2235 | $json = [ |
||
2236 | "id" => $this->course->getId(), |
||
2237 | "name" => get_lang('Exercises'), |
||
2238 | "visible" => (int) $toolVisibility, |
||
2239 | "summary" => '', |
||
2240 | "summaryformat" => 1, |
||
2241 | "section" => 1, |
||
2242 | "hiddenbynumsections" => 0, |
||
2243 | "uservisible" => $toolVisibility, |
||
2244 | "modules" => [], |
||
2245 | ]; |
||
2246 | |||
2247 | $quizIcon = Display::return_icon('quiz.png', '', [], ICON_SIZE_SMALL, false, true); |
||
2248 | |||
2249 | $json['modules'] = array_map( |
||
2250 | function (array $exercise) use ($quizIcon) { |
||
2251 | return [ |
||
2252 | 'id' => $exercise['id'], |
||
2253 | 'url' => $exercise['url'], |
||
2254 | 'name' => $exercise['name'], |
||
2255 | 'instance' => 1, |
||
2256 | 'visible' => 1, |
||
2257 | 'uservisible' => true, |
||
2258 | 'visibleoncoursepage' => 0, |
||
2259 | 'modicon' => $quizIcon, |
||
2260 | 'modname' => 'quiz', |
||
2261 | 'modplural' => get_lang('Exercises'), |
||
2262 | 'availability' => null, |
||
2263 | 'indent' => 0, |
||
2264 | 'onclick' => '', |
||
2265 | 'afterlink' => null, |
||
2266 | 'customdata' => "", |
||
2267 | 'noviewlink' => false, |
||
2268 | 'completion' => (int) ($exercise[1] > 0), |
||
2269 | ]; |
||
2270 | }, |
||
2271 | Exercise::exerciseGrid(0, '', $userId, $courseId, $sessionId, true) |
||
2272 | ); |
||
2273 | |||
2274 | return [$json]; |
||
2275 | } |
||
2276 | |||
2277 | /** |
||
2278 | * @throws Exception |
||
2279 | */ |
||
2280 | public function updateSession(array $params): array |
||
2281 | { |
||
2282 | $id = $params['session_id']; |
||
2283 | $reset = $params['reset'] ?? null; |
||
2284 | $name = $params['name'] ?? null; |
||
2285 | $coachId = isset($params['id_coach']) ? (int) $params['id_coach'] : null; |
||
2286 | $sessionCategoryId = isset($params['session_category_id']) ? (int) $params['session_category_id'] : null; |
||
2287 | $description = $params['description'] ?? null; |
||
2288 | $showDescription = $params['show_description'] ?? null; |
||
2289 | $duration = $params['duration'] ?? null; |
||
2290 | $visibility = $params['visibility'] ?? null; |
||
2291 | $promotionId = $params['promotion_id'] ?? null; |
||
2292 | $displayStartDate = $params['display_start_date'] ?? null; |
||
2293 | $displayEndDate = $params['display_end_date'] ?? null; |
||
2294 | $accessStartDate = $params['access_start_date'] ?? null; |
||
2295 | $accessEndDate = $params['access_end_date'] ?? null; |
||
2296 | $coachStartDate = $params['coach_access_start_date'] ?? null; |
||
2297 | $coachEndDate = $params['coach_access_end_date'] ?? null; |
||
2298 | $sendSubscriptionNotification = $params['send_subscription_notification'] ?? null; |
||
2299 | $extraFields = $params['extra'] ?? []; |
||
2300 | |||
2301 | $reset = (bool) $reset; |
||
2302 | $visibility = (int) $visibility; |
||
2303 | $tblSession = Database::get_main_table(TABLE_MAIN_SESSION); |
||
2304 | |||
2305 | if (!SessionManager::isValidId($id)) { |
||
2306 | throw new Exception(get_lang('NoData')); |
||
2307 | } |
||
2308 | |||
2309 | if (!empty($accessStartDate) && !api_is_valid_date($accessStartDate, 'Y-m-d H:i') && |
||
2310 | !api_is_valid_date($accessStartDate, 'Y-m-d H:i:s') |
||
2311 | ) { |
||
2312 | throw new Exception(get_lang('InvalidDate')); |
||
2313 | } |
||
2314 | |||
2315 | if (!empty($accessEndDate) && !api_is_valid_date($accessEndDate, 'Y-m-d H:i') && |
||
2316 | !api_is_valid_date($accessEndDate, 'Y-m-d H:i:s') |
||
2317 | ) { |
||
2318 | throw new Exception(get_lang('InvalidDate')); |
||
2319 | } |
||
2320 | |||
2321 | if (!empty($accessStartDate) && !empty($accessEndDate) && $accessStartDate >= $accessEndDate) { |
||
2322 | throw new Exception(get_lang('InvalidDate')); |
||
2323 | } |
||
2324 | |||
2325 | $values = []; |
||
2326 | |||
2327 | if ($reset) { |
||
2328 | $values['name'] = $name; |
||
2329 | $values['id_coach'] = $coachId; |
||
2330 | $values['session_category_id'] = $sessionCategoryId; |
||
2331 | $values['description'] = $description; |
||
2332 | $values['show_description'] = $showDescription; |
||
2333 | $values['duration'] = $duration; |
||
2334 | $values['visibility'] = $visibility; |
||
2335 | $values['promotion_id'] = $promotionId; |
||
2336 | $values['display_start_date'] = !empty($displayStartDate) ? api_get_utc_datetime($displayStartDate) : null; |
||
2337 | $values['display_end_date'] = !empty($displayEndDate) ? api_get_utc_datetime($displayEndDate) : null; |
||
2338 | $values['access_start_date'] = !empty($accessStartDate) ? api_get_utc_datetime($accessStartDate) : null; |
||
2339 | $values['access_end_date'] = !empty($accessEndDate) ? api_get_utc_datetime($accessEndDate) : null; |
||
2340 | $values['coach_access_start_date'] = !empty($coachStartDate) ? api_get_utc_datetime($coachStartDate) : null; |
||
2341 | $values['coach_access_end_date'] = !empty($coachEndDate) ? api_get_utc_datetime($coachEndDate) : null; |
||
2342 | $values['send_subscription_notification'] = $sendSubscriptionNotification; |
||
2343 | } else { |
||
2344 | if (!empty($name)) { |
||
2345 | $values['name'] = $name; |
||
2346 | } |
||
2347 | |||
2348 | if (!empty($coachId)) { |
||
2349 | $values['id_coach'] = $coachId; |
||
2350 | } |
||
2351 | |||
2352 | if (!empty($sessionCategoryId)) { |
||
2353 | $values['session_category_id'] = $sessionCategoryId; |
||
2354 | } |
||
2355 | |||
2356 | if (!empty($description)) { |
||
2357 | $values['description'] = $description; |
||
2358 | } |
||
2359 | |||
2360 | if (!empty($showDescription)) { |
||
2361 | $values['show_description'] = $showDescription; |
||
2362 | } |
||
2363 | |||
2364 | if (!empty($duration)) { |
||
2365 | $values['duration'] = $duration; |
||
2366 | } |
||
2367 | |||
2368 | if (!empty($visibility)) { |
||
2369 | $values['visibility'] = $visibility; |
||
2370 | } |
||
2371 | |||
2372 | if (!empty($promotionId)) { |
||
2373 | $values['promotion_id'] = $promotionId; |
||
2374 | } |
||
2375 | |||
2376 | if (!empty($displayStartDate)) { |
||
2377 | $values['display_start_date'] = api_get_utc_datetime($displayStartDate); |
||
2378 | } |
||
2379 | |||
2380 | if (!empty($displayEndDate)) { |
||
2381 | $values['display_end_date'] = api_get_utc_datetime($displayEndDate); |
||
2382 | } |
||
2383 | |||
2384 | if (!empty($accessStartDate)) { |
||
2385 | $values['access_start_date'] = api_get_utc_datetime($accessStartDate); |
||
2386 | } |
||
2387 | |||
2388 | if (!empty($accessEndDate)) { |
||
2389 | $values['access_end_date'] = api_get_utc_datetime($accessEndDate); |
||
2390 | } |
||
2391 | |||
2392 | if (!empty($coachStartDate)) { |
||
2393 | $values['coach_access_start_date'] = api_get_utc_datetime($coachStartDate); |
||
2394 | } |
||
2395 | |||
2396 | if (!empty($coachEndDate)) { |
||
2397 | $values['coach_access_end_date'] = api_get_utc_datetime($coachEndDate); |
||
2398 | } |
||
2399 | |||
2400 | if (!empty($sendSubscriptionNotification)) { |
||
2401 | $values['send_subscription_notification'] = $sendSubscriptionNotification; |
||
2402 | } |
||
2403 | } |
||
2404 | |||
2405 | Database::update( |
||
2406 | $tblSession, |
||
2407 | $values, |
||
2408 | ['id = ?' => $id] |
||
2409 | ); |
||
2410 | |||
2411 | if (!empty($extraFields)) { |
||
2412 | $extraFields['item_id'] = $id; |
||
2413 | $sessionFieldValue = new ExtraFieldValue('session'); |
||
2414 | $sessionFieldValue->saveFieldValues($extraFields); |
||
2415 | } |
||
2416 | |||
2417 | return [ |
||
2418 | 'status' => true, |
||
2419 | 'message' => get_lang('Updated'), |
||
2420 | 'id_session' => $id, |
||
2421 | ]; |
||
2422 | } |
||
2423 | |||
2424 | public function checkConditionalLogin(): bool |
||
2425 | { |
||
2426 | $file = api_get_path(SYS_CODE_PATH).'auth/conditional_login/conditional_login.php'; |
||
2427 | |||
2428 | if (!file_exists($file)) { |
||
2429 | return true; |
||
2430 | } |
||
2431 | |||
2432 | include_once $file; |
||
2433 | |||
2434 | if (!isset($login_conditions)) { |
||
2435 | return true; |
||
2436 | } |
||
2437 | |||
2438 | foreach ($login_conditions as $condition) { |
||
2439 | //If condition fails we redirect to the URL defined by the condition |
||
2440 | if (!isset($condition['conditional_function'])) { |
||
2441 | continue; |
||
2442 | } |
||
2443 | |||
2444 | $function = $condition['conditional_function']; |
||
2445 | $result = $function(['user_id' => $this->user->getId()]); |
||
2446 | |||
2447 | if ($result == false) { |
||
2448 | return false; |
||
2449 | } |
||
2450 | } |
||
2451 | |||
2452 | return true; |
||
2453 | } |
||
2454 | |||
2455 | public function getLegalConditions(): array |
||
2456 | { |
||
2457 | $language = api_get_language_id( |
||
2458 | api_get_interface_language() |
||
2459 | ); |
||
2460 | |||
2461 | $termPreview = LegalManager::get_last_condition($language); |
||
2462 | |||
2463 | if ($termPreview) { |
||
2464 | return $termPreview; |
||
2465 | } |
||
2466 | |||
2467 | $language = api_get_language_id( |
||
2468 | api_get_setting('platformLanguage') |
||
2469 | ); |
||
2470 | |||
2471 | $termPreview = LegalManager::get_last_condition($language); |
||
2472 | |||
2473 | if ($termPreview) { |
||
2474 | return $termPreview; |
||
2475 | } |
||
2476 | |||
2477 | $language = api_get_language_id('english'); |
||
2478 | |||
2479 | return LegalManager::get_last_condition($language); |
||
2480 | } |
||
2481 | |||
2482 | public function updateConditionAccepted() |
||
2483 | { |
||
2484 | $legalAcceptType = $_POST['legal_accept_type'] ?? null; |
||
2485 | |||
2486 | $condArray = explode(':', $legalAcceptType); |
||
2487 | $condArray = array_map('intval', $condArray); |
||
2488 | |||
2489 | if (empty($condArray[0]) || empty($condArray[1])) { |
||
2490 | return; |
||
2491 | } |
||
2492 | |||
2493 | $conditionToSave = intval($condArray[0]).':'.intval($condArray[1]).':'.time(); |
||
2494 | |||
2495 | LegalManager::sendEmailToUserBoss( |
||
2496 | $this->user->getId(), |
||
2497 | $conditionToSave |
||
2498 | ); |
||
2499 | } |
||
2500 | |||
2501 | public function logout() |
||
2502 | { |
||
2503 | online_logout($this->user->getId()); |
||
2504 | |||
2505 | Event::courseLogout( |
||
2506 | [ |
||
2507 | 'uid' => $this->user->getId(), |
||
2508 | 'cid' => $this->course ? $this->course->getId() : 0, |
||
2509 | 'sid' => $this->session ? $this->session->getId() : 0, |
||
2510 | ] |
||
2511 | ); |
||
2512 | } |
||
2513 | |||
2514 | /** |
||
2515 | * @throws Exception |
||
2516 | */ |
||
2517 | public function setThreadNotify(int $threadId): string |
||
2518 | { |
||
2519 | require_once api_get_path(SYS_CODE_PATH).'forum/forumfunction.inc.php'; |
||
2520 | |||
2521 | $result = set_notification( |
||
2522 | 'thread', |
||
2523 | $threadId, |
||
2524 | false, |
||
2525 | api_get_user_info($this->user->getId()), |
||
2526 | api_get_course_info($this->course->getCode()) |
||
2527 | ); |
||
2528 | |||
2529 | if (false === $result) { |
||
2530 | throw new Exception(get_lang('NotAllowed')); |
||
2531 | } |
||
2532 | |||
2533 | return $result; |
||
2534 | } |
||
2535 | |||
2536 | public function getCourseWorks(): array |
||
2537 | { |
||
2538 | Event::event_access_tool(TOOL_STUDENTPUBLICATION); |
||
2539 | |||
2540 | require_once api_get_path(SYS_CODE_PATH).'work/work.lib.php'; |
||
2541 | |||
2542 | $isAllowedToEdit = $this->user->getStatus() !== STUDENT; |
||
2543 | |||
2544 | $courseId = $this->course->getId(); |
||
2545 | $sessionId = $this->session ? $this->session->getId() : 0; |
||
2546 | |||
2547 | $courseInfo = api_get_course_info_by_id($this->course->getId()); |
||
2548 | |||
2549 | $works = array_filter( |
||
2550 | getWorkListTeacherData($courseId, $sessionId, 0, 0, 0, 'title', 'ASC', ''), |
||
2551 | function (array $work) use ($isAllowedToEdit, $courseInfo, $courseId, $sessionId) { |
||
2552 | if (!$isAllowedToEdit |
||
2553 | && !userIsSubscribedToWork($this->user->getId(), $work['id'], $courseId) |
||
2554 | ) { |
||
2555 | return false; |
||
2556 | } |
||
2557 | |||
2558 | $visibility = api_get_item_visibility($courseInfo, 'work', $work['id'], $sessionId); |
||
2559 | |||
2560 | if (!$isAllowedToEdit && $visibility != 1) { |
||
2561 | return false; |
||
2562 | } |
||
2563 | |||
2564 | return true; |
||
2565 | } |
||
2566 | ); |
||
2567 | |||
2568 | return array_map( |
||
2569 | function (array $work) use ($isAllowedToEdit, $courseInfo) { |
||
2570 | $work['type'] = 'work.png'; |
||
2571 | |||
2572 | if (!$isAllowedToEdit) { |
||
2573 | $workList = get_work_user_list( |
||
2574 | 0, |
||
2575 | 1000, |
||
2576 | null, |
||
2577 | null, |
||
2578 | $work['id'], |
||
2579 | ' AND u.id = '.$this->user->getId() |
||
2580 | ); |
||
2581 | |||
2582 | $count = getTotalWorkComment($workList, $courseInfo); |
||
2583 | $lastWork = getLastWorkStudentFromParentByUser($this->user->getId(), $work, $courseInfo); |
||
2584 | |||
2585 | $work['feedback'] = ' '.Display::label('0 '.get_lang('Feedback'), 'warning'); |
||
2586 | |||
2587 | if (!empty($count)) { |
||
2588 | $work['feedback'] = ' '.Display::label($count.' '.get_lang('Feedback'), 'info'); |
||
2589 | } |
||
2590 | |||
2591 | $work['last_upload'] = ''; |
||
2592 | |||
2593 | if (!empty($lastWork)) { |
||
2594 | $work['last_upload'] = !empty($lastWork['qualification']) |
||
2595 | ? $lastWork['qualification_rounded'].' - ' |
||
2596 | : ''; |
||
2597 | $work['last_upload'] .= api_get_local_time($lastWork['sent_date']); |
||
2598 | } |
||
2599 | } |
||
2600 | |||
2601 | return $work; |
||
2602 | }, |
||
2603 | $works |
||
2604 | ); |
||
2605 | } |
||
2606 | |||
2607 | /** |
||
2608 | * @throws Exception |
||
2609 | */ |
||
2610 | public function putCourseWorkVisibility(int $workId, int $status): bool |
||
2611 | { |
||
2612 | Event::event_access_tool(TOOL_STUDENTPUBLICATION); |
||
2613 | |||
2614 | $courseInfo = api_get_course_info_by_id($this->course->getId()); |
||
2615 | |||
2616 | require_once api_get_path(SYS_CODE_PATH).'work/work.lib.php'; |
||
2617 | |||
2618 | switch ($status) { |
||
2619 | case 1: |
||
2620 | return makeVisible($workId, $courseInfo); |
||
2621 | case 0: |
||
2622 | return makeInvisible($workId, $courseInfo); |
||
2623 | default: |
||
2624 | throw new Exception(get_lang('ActionNotAllowed')); |
||
2625 | } |
||
2626 | } |
||
2627 | |||
2628 | public function deleteWorkStudentItem(int $workId): string |
||
2629 | { |
||
2630 | Event::event_access_tool(TOOL_STUDENTPUBLICATION); |
||
2631 | |||
2632 | require_once api_get_path(SYS_CODE_PATH).'work/work.lib.php'; |
||
2633 | |||
2634 | $courseInfo = api_get_course_info_by_id($this->course->getId()); |
||
2635 | |||
2636 | $fileDeleted = deleteWorkItem($workId, $courseInfo); |
||
2637 | |||
2638 | if ($fileDeleted) { |
||
2639 | return get_lang('TheDocumentHasBeenDeleted'); |
||
2640 | } |
||
2641 | |||
2642 | return get_lang('YouAreNotAllowedToDeleteThisDocument'); |
||
2643 | } |
||
2644 | |||
2645 | public function deleteWorkCorrections(int $workId): string |
||
2646 | { |
||
2647 | Event::event_access_tool(TOOL_STUDENTPUBLICATION); |
||
2648 | |||
2649 | require_once api_get_path(SYS_CODE_PATH).'work/work.lib.php'; |
||
2650 | |||
2651 | $courseInfo = api_get_course_info_by_id($this->course->getId()); |
||
2652 | |||
2653 | $result = get_work_user_list(null, null, null, null, $workId); |
||
2654 | |||
2655 | if ($result) { |
||
2656 | foreach ($result as $item) { |
||
2657 | $workInfo = get_work_data_by_id($item['id']); |
||
2658 | |||
2659 | deleteCorrection($courseInfo, $workInfo); |
||
2660 | } |
||
2661 | } |
||
2662 | |||
2663 | return get_lang('Deleted'); |
||
2664 | } |
||
2665 | |||
2666 | public function getWorkList(int $workId): array |
||
2667 | { |
||
2668 | $isAllowedToEdit = api_is_allowed_to_edit(); |
||
2669 | |||
2670 | Event::event_access_tool(TOOL_STUDENTPUBLICATION); |
||
2671 | |||
2672 | require_once api_get_path(SYS_CODE_PATH).'work/work.lib.php'; |
||
2673 | |||
2674 | $userId = $this->user->getId(); |
||
2675 | $courseId = $this->course->getId(); |
||
2676 | $sessionId = $this->session ? $this->session->getId() : 0; |
||
2677 | |||
2678 | $courseInfo = api_get_course_info_by_id($courseId); |
||
2679 | $webPath = api_get_path(WEB_PATH); |
||
2680 | |||
2681 | $whereCondition = !$isAllowedToEdit ? " AND u.id = $userId" : ''; |
||
2682 | |||
2683 | $works = get_work_user_list( |
||
2684 | 0, |
||
2685 | 0, |
||
2686 | 'title', |
||
2687 | 'asc', |
||
2688 | $workId, |
||
2689 | $whereCondition, |
||
2690 | null, |
||
2691 | false, |
||
2692 | $courseId, |
||
2693 | $sessionId |
||
2694 | ); |
||
2695 | |||
2696 | return array_map( |
||
2697 | function (array $work) use ($courseInfo, $webPath) { |
||
2698 | $itemId = $work['id']; |
||
2699 | $count = getWorkCommentCount($itemId, $courseInfo); |
||
2700 | |||
2701 | $work['feedback'] = $count.' '.Display::returnFontAwesomeIcon('comments-o'); |
||
2702 | $work['feedback_clean'] = $count; |
||
2703 | |||
2704 | $workInfo = get_work_data_by_id($itemId); |
||
2705 | $commentsTmp = getWorkComments($workInfo); |
||
2706 | $comments = []; |
||
2707 | |||
2708 | foreach ($commentsTmp as $comment) { |
||
2709 | $comment['comment'] = str_replace('src="/', 'src="'.$webPath.'app/', $comment['comment']); |
||
2710 | $comments[] = $comment; |
||
2711 | } |
||
2712 | |||
2713 | $work['comments'] = $comments; |
||
2714 | |||
2715 | if (empty($workInfo['qualificator_id'])) { |
||
2716 | $qualificator_id = Display::label(get_lang('NotRevised'), 'warning'); |
||
2717 | } else { |
||
2718 | $qualificator_id = Display::label(get_lang('Revised'), 'success'); |
||
2719 | } |
||
2720 | |||
2721 | $work['qualificator_id'] = $qualificator_id; |
||
2722 | |||
2723 | return $work; |
||
2724 | }, |
||
2725 | $works |
||
2726 | ); |
||
2727 | } |
||
2728 | |||
2729 | public function getWorkStudentsWithoutPublications(int $workId): array |
||
2730 | { |
||
2731 | Event::event_access_tool(TOOL_STUDENTPUBLICATION); |
||
2732 | |||
2733 | require_once api_get_path(SYS_CODE_PATH).'work/work.lib.php'; |
||
2734 | |||
2735 | return get_list_users_without_publication($workId); |
||
2736 | } |
||
2737 | |||
2738 | public function getWorkUsers(int $workId): array |
||
2739 | { |
||
2740 | Event::event_access_tool(TOOL_STUDENTPUBLICATION); |
||
2741 | |||
2742 | require_once api_get_path(SYS_CODE_PATH).'work/work.lib.php'; |
||
2743 | |||
2744 | $courseId = $this->course->getId(); |
||
2745 | $sessionId = $this->session ? $this->session->getId() : 0; |
||
2746 | $courseInfo = api_get_course_info_by_id($courseId); |
||
2747 | |||
2748 | $items = getAllUserToWork($workId, $courseId); |
||
2749 | $usersAdded = []; |
||
2750 | $result = [ |
||
2751 | 'users_added' => [], |
||
2752 | 'users_to_add' => [], |
||
2753 | ]; |
||
2754 | |||
2755 | if (!empty($items)) { |
||
2756 | foreach ($items as $data) { |
||
2757 | $usersAdded[] = $data['user_id']; |
||
2758 | |||
2759 | $userInfo = api_get_user_info($data['user_id']); |
||
2760 | |||
2761 | $result['users_added'][] = [ |
||
2762 | 'user_id' => (int) $data['user_id'], |
||
2763 | 'complete_name_with_username' => $userInfo['complete_name_with_username'], |
||
2764 | ]; |
||
2765 | } |
||
2766 | } |
||
2767 | |||
2768 | if (empty($sessionId)) { |
||
2769 | $status = STUDENT; |
||
2770 | } else { |
||
2771 | $status = 0; |
||
2772 | } |
||
2773 | |||
2774 | $userList = CourseManager::get_user_list_from_course_code( |
||
2775 | $courseInfo['code'], |
||
2776 | $sessionId, |
||
2777 | null, |
||
2778 | null, |
||
2779 | $status |
||
2780 | ); |
||
2781 | |||
2782 | $userToAddList = []; |
||
2783 | foreach ($userList as $user) { |
||
2784 | if (!in_array($user['user_id'], $usersAdded)) { |
||
2785 | $userToAddList[] = $user; |
||
2786 | } |
||
2787 | } |
||
2788 | |||
2789 | if (!empty($userToAddList)) { |
||
2790 | foreach ($userToAddList as $user) { |
||
2791 | $userName = api_get_person_name($user['firstname'], $user['lastname']).' ('.$user['username'].') '; |
||
2792 | |||
2793 | $result['users_to_add'][] = [ |
||
2794 | 'user_id' => (int) $user['user_id'], |
||
2795 | 'complete_name_with_username' => $userName, |
||
2796 | ]; |
||
2797 | } |
||
2798 | } |
||
2799 | |||
2800 | return $result; |
||
2801 | } |
||
2802 | |||
2803 | public function getWorkStudentList(int $workId): array |
||
2804 | { |
||
2805 | Event::event_access_tool(TOOL_STUDENTPUBLICATION); |
||
2806 | |||
2807 | require_once api_get_path(SYS_CODE_PATH).'work/work.lib.php'; |
||
2808 | |||
2809 | $courseId = $this->course->getId(); |
||
2810 | $courseCode = $this->course->getCode(); |
||
2811 | $sessionId = $this->session ? $this->session->getId() : 0; |
||
2812 | |||
2813 | $myFolderData = get_work_data_by_id($workId); |
||
2814 | |||
2815 | $workParents = []; |
||
2816 | |||
2817 | if (empty($myFolderData)) { |
||
2818 | $workParents = getWorkList($workId, $myFolderData); |
||
2819 | } |
||
2820 | |||
2821 | $workIdList = []; |
||
2822 | |||
2823 | if (!empty($workParents)) { |
||
2824 | foreach ($workParents as $work) { |
||
2825 | $workIdList[] = $work->id; |
||
2826 | } |
||
2827 | } |
||
2828 | |||
2829 | $userList = getWorkUserList( |
||
2830 | $courseCode, |
||
2831 | $sessionId, |
||
2832 | 0, |
||
2833 | 0, |
||
2834 | null, |
||
2835 | null, |
||
2836 | null |
||
2837 | ); |
||
2838 | |||
2839 | return array_map( |
||
2840 | function ($userId) use ($courseId, $sessionId, $workParents, $workIdList) { |
||
2841 | $user = api_get_user_info($userId); |
||
2842 | |||
2843 | $userWorks = 0; |
||
2844 | |||
2845 | if (!empty($workIdList)) { |
||
2846 | $userWorks = getUniqueStudentAttempts( |
||
2847 | $workIdList, |
||
2848 | 0, |
||
2849 | $courseId, |
||
2850 | $sessionId, |
||
2851 | $user['user_id'] |
||
2852 | ); |
||
2853 | } |
||
2854 | |||
2855 | $works = $userWorks." / ".count($workParents); |
||
2856 | |||
2857 | return [ |
||
2858 | 'id' => $userId, |
||
2859 | 'complete_name' => api_get_person_name($user['firstname'], $user['lastname']), |
||
2860 | 'works' => $works, |
||
2861 | ]; |
||
2862 | }, |
||
2863 | $userList |
||
2864 | ); |
||
2865 | } |
||
2866 | |||
2867 | public function viewDocumentInFrame(int $documentId) |
||
2868 | { |
||
2869 | $courseCode = $this->course->getCode(); |
||
2870 | $sessionId = $this->session ? $this->session->getId() : 0; |
||
2871 | |||
2872 | $url = api_get_path(WEB_CODE_PATH).'document/showinframes.php?' |
||
2873 | .http_build_query( |
||
2874 | [ |
||
2875 | 'cidReq' => $courseCode, |
||
2876 | 'id_session' => $sessionId, |
||
2877 | 'gidReq' => 0, |
||
2878 | 'gradebook' => 0, |
||
2879 | 'origin' => self::SERVICE_NAME, |
||
2880 | 'id' => $documentId, |
||
2881 | ] |
||
2882 | ); |
||
2883 | |||
2884 | header("Location: $url"); |
||
2885 | exit; |
||
2886 | } |
||
2887 | |||
2888 | public function viewQuizTool() |
||
2889 | { |
||
2890 | $courseCode = $this->course->getCode(); |
||
2891 | $sessionId = $this->session ? $this->session->getId() : 0; |
||
2892 | |||
2893 | $url = api_get_path(WEB_CODE_PATH).'exercise/exercise.php?' |
||
2894 | .http_build_query( |
||
2895 | [ |
||
2896 | 'cidReq' => $courseCode, |
||
2897 | 'id_session' => $sessionId, |
||
2898 | 'gidReq' => 0, |
||
2899 | 'gradebook' => 0, |
||
2900 | 'origin' => self::SERVICE_NAME, |
||
2901 | ] |
||
2902 | ); |
||
2903 | |||
2904 | header("Location: $url"); |
||
2905 | exit; |
||
2906 | } |
||
2907 | |||
2908 | public function viewSurveyTool() |
||
2909 | { |
||
2910 | $courseCode = $this->course->getCode(); |
||
2911 | $sessionId = $this->session ? $this->session->getId() : 0; |
||
2912 | |||
2913 | $url = api_get_path(WEB_CODE_PATH).'survey/survey_list.php?' |
||
2914 | .http_build_query( |
||
2915 | [ |
||
2916 | 'cidReq' => $courseCode, |
||
2917 | 'id_session' => $sessionId, |
||
2918 | 'gidReq' => 0, |
||
2919 | 'gradebook' => 0, |
||
2920 | 'origin' => self::SERVICE_NAME, |
||
2921 | ] |
||
2922 | ); |
||
2923 | |||
2924 | header("Location: $url"); |
||
2925 | exit; |
||
2926 | } |
||
2927 | |||
2928 | public static function isAllowedByRequest(bool $inpersonate = false): bool |
||
2948 | } |
||
2949 | |||
2950 | /** |
||
2951 | * @param array $additionalParams Optional |
||
2952 | * |
||
2953 | * @return string |
||
2954 | */ |
||
2955 | private function encodeParams(array $additionalParams = []) |
||
2956 | { |
||
2957 | $params = array_merge( |
||
2958 | $additionalParams, |
||
2959 | [ |
||
2960 | 'api_key' => $this->apiKey, |
||
2961 | 'username' => $this->user->getUsername(), |
||
2962 | ] |
||
2963 | ); |
||
2964 | |||
2965 | return json_encode($params); |
||
2966 | } |
||
2967 | } |
||
2968 |