Total Complexity | 320 |
Total Lines | 2410 |
Duplicated Lines | 0 % |
Changes | 5 | ||
Bugs | 0 | 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 GET_USER_MESSAGES = 'user_messages'; |
||
23 | const GET_USER_COURSES = 'user_courses'; |
||
24 | const GET_USER_SESSIONS = 'user_sessions'; |
||
25 | const GET_USERS_SUBSCRIBED_TO_COURSE = 'get_users_subscribed_to_course'; |
||
26 | const GET_USER_MESSAGES_RECEIVED = 'user_messages_received'; |
||
27 | const GET_USER_MESSAGES_SENT = 'user_messages_sent'; |
||
28 | const POST_USER_MESSAGE_READ = 'user_message_read'; |
||
29 | const POST_USER_MESSAGE_UNREAD = 'user_message_unread'; |
||
30 | const SAVE_GCM_ID = 'gcm_id'; |
||
31 | const GET_PROFILE = 'user_profile'; |
||
32 | const GET_COURSE_INFO = 'course_info'; |
||
33 | const GET_COURSE_DESCRIPTIONS = 'course_descriptions'; |
||
34 | const GET_COURSE_DOCUMENTS = 'course_documents'; |
||
35 | const GET_COURSE_ANNOUNCEMENTS = 'course_announcements'; |
||
36 | const GET_COURSE_ANNOUNCEMENT = 'course_announcement'; |
||
37 | const GET_COURSE_AGENDA = 'course_agenda'; |
||
38 | const GET_COURSE_NOTEBOOKS = 'course_notebooks'; |
||
39 | const GET_COURSE_FORUM_CATEGORIES = 'course_forumcategories'; |
||
40 | const GET_COURSE_FORUM = 'course_forum'; |
||
41 | const GET_COURSE_FORUM_THREAD = 'course_forumthread'; |
||
42 | const GET_COURSE_LEARNPATHS = 'course_learnpaths'; |
||
43 | const GET_COURSE_LEARNPATH = 'course_learnpath'; |
||
44 | const GET_COURSE_LP_PROGRESS = 'course_lp_progress'; |
||
45 | const SAVE_FORUM_POST = 'save_forum_post'; |
||
46 | const SAVE_USER_MESSAGE = 'save_user_message'; |
||
47 | const GET_MESSAGE_USERS = 'message_users'; |
||
48 | const SAVE_COURSE_NOTEBOOK = 'save_course_notebook'; |
||
49 | const SAVE_FORUM_THREAD = 'save_forum_thread'; |
||
50 | const SAVE_COURSE = 'save_course'; |
||
51 | const SAVE_USER = 'save_user'; |
||
52 | const SAVE_USER_JSON = 'save_user_json'; |
||
53 | const SUBSCRIBE_USER_TO_COURSE = 'subscribe_user_to_course'; |
||
54 | const SUBSCRIBE_USER_TO_COURSE_PASSWORD = 'subscribe_user_to_course_password'; |
||
55 | const UNSUBSCRIBE_USER_FROM_COURSE = 'unsubscribe_user_from_course'; |
||
56 | const EXTRAFIELD_GCM_ID = 'gcm_registration_id'; |
||
57 | const DELETE_USER_MESSAGE = 'delete_user_message'; |
||
58 | const SET_MESSAGE_READ = 'set_message_read'; |
||
59 | const CREATE_CAMPUS = 'add_campus'; |
||
60 | const EDIT_CAMPUS = 'edit_campus'; |
||
61 | const DELETE_CAMPUS = 'delete_campus'; |
||
62 | const SAVE_SESSION = 'save_session'; |
||
63 | const UPDATE_SESSION = 'update_session'; |
||
64 | const GET_USERS = 'get_users'; |
||
65 | const GET_COURSES = 'get_courses'; |
||
66 | const GET_COURSES_FROM_EXTRA_FIELD = 'get_courses_from_extra_field'; |
||
67 | const ADD_COURSES_SESSION = 'add_courses_session'; |
||
68 | const ADD_USERS_SESSION = 'add_users_session'; |
||
69 | const CREATE_SESSION_FROM_MODEL = 'create_session_from_model'; |
||
70 | const SUBSCRIBE_USER_TO_SESSION_FROM_USERNAME = 'subscribe_user_to_session_from_username'; |
||
71 | const GET_SESSION_FROM_EXTRA_FIELD = 'get_session_from_extra_field'; |
||
72 | const UPDATE_USER_FROM_USERNAME = 'update_user_from_username'; |
||
73 | const USERNAME_EXIST = 'username_exist'; |
||
74 | const GET_COURSE_QUIZ_MDL_COMPAT = 'get_course_quiz_mdl_compat'; |
||
75 | const UPDATE_USER_PAUSE_TRAINING = 'update_user_pause_training'; |
||
76 | const DELETE_COURSE = 'delete_course'; |
||
77 | const CHECK_CONDITIONAL_LOGIN = 'check_conditional_login'; |
||
78 | const GET_LEGAL_CONDITIONS = 'get_legal_conditions'; |
||
79 | const UPDATE_CONDITION_ACCEPTED = 'update_condition_accepted'; |
||
80 | const LOGOUT = 'logout'; |
||
81 | const DELETE_USER = 'delete_user'; |
||
82 | |||
83 | /** |
||
84 | * @var Session |
||
85 | */ |
||
86 | private $session; |
||
87 | |||
88 | /** |
||
89 | * @var Course |
||
90 | */ |
||
91 | private $course; |
||
92 | |||
93 | /** |
||
94 | * Rest constructor. |
||
95 | * |
||
96 | * @param string $username |
||
97 | * @param string $apiKey |
||
98 | */ |
||
99 | public function __construct($username, $apiKey) |
||
100 | { |
||
101 | parent::__construct($username, $apiKey); |
||
102 | } |
||
103 | |||
104 | /** |
||
105 | * @param string $username |
||
106 | * @param string $apiKeyToValidate |
||
107 | * |
||
108 | * @throws Exception |
||
109 | * |
||
110 | * @return Rest |
||
111 | */ |
||
112 | public static function validate($username, $apiKeyToValidate) |
||
113 | { |
||
114 | $apiKey = self::findUserApiKey($username, self::SERVICE_NAME); |
||
115 | |||
116 | if ($apiKey != $apiKeyToValidate) { |
||
117 | throw new Exception(get_lang('InvalidApiKey')); |
||
118 | } |
||
119 | |||
120 | return new self($username, $apiKey); |
||
121 | } |
||
122 | |||
123 | /** |
||
124 | * Create the gcm_registration_id extra field for users. |
||
125 | */ |
||
126 | public static function init() |
||
127 | { |
||
128 | $extraField = new ExtraField('user'); |
||
129 | $fieldInfo = $extraField->get_handler_field_info_by_field_variable(self::EXTRA_FIELD_GCM_REGISTRATION); |
||
130 | |||
131 | if (empty($fieldInfo)) { |
||
132 | $extraField->save( |
||
133 | [ |
||
134 | 'variable' => self::EXTRA_FIELD_GCM_REGISTRATION, |
||
135 | 'field_type' => ExtraField::FIELD_TYPE_TEXT, |
||
136 | 'display_text' => self::EXTRA_FIELD_GCM_REGISTRATION, |
||
137 | ] |
||
138 | ); |
||
139 | } |
||
140 | } |
||
141 | |||
142 | /** |
||
143 | * @param string $encoded |
||
144 | * |
||
145 | * @return array |
||
146 | */ |
||
147 | public static function decodeParams($encoded) |
||
148 | { |
||
149 | return json_decode($encoded); |
||
150 | } |
||
151 | |||
152 | /** |
||
153 | * Set the current course. |
||
154 | * |
||
155 | * @param int $id |
||
156 | * |
||
157 | * @throws Exception |
||
158 | */ |
||
159 | public function setCourse($id) |
||
160 | { |
||
161 | if (!$id) { |
||
162 | $this->course = null; |
||
163 | |||
164 | ChamiloSession::erase('_real_cid'); |
||
165 | ChamiloSession::erase('_cid'); |
||
166 | ChamiloSession::erase('_course'); |
||
167 | |||
168 | return; |
||
169 | } |
||
170 | |||
171 | $em = Database::getManager(); |
||
172 | /** @var Course $course */ |
||
173 | $course = $em->find('ChamiloCoreBundle:Course', $id); |
||
174 | |||
175 | if (!$course) { |
||
|
|||
176 | throw new Exception(get_lang('NoCourse')); |
||
177 | } |
||
178 | |||
179 | $this->course = $course; |
||
180 | |||
181 | ChamiloSession::write('_real_cid', $course->getId()); |
||
182 | ChamiloSession::write('_cid', $course->getCode()); |
||
183 | ChamiloSession::write('_course', api_get_course_info($course->getCode())); |
||
184 | } |
||
185 | |||
186 | /** |
||
187 | * Set the current session. |
||
188 | * |
||
189 | * @param int $id |
||
190 | * |
||
191 | * @throws Exception |
||
192 | */ |
||
193 | public function setSession($id) |
||
194 | { |
||
195 | if (!$id) { |
||
196 | $this->session = null; |
||
197 | |||
198 | ChamiloSession::erase('session_name'); |
||
199 | ChamiloSession::erase('id_session'); |
||
200 | |||
201 | return; |
||
202 | } |
||
203 | |||
204 | $em = Database::getManager(); |
||
205 | /** @var Session $session */ |
||
206 | $session = $em->find('ChamiloCoreBundle:Session', $id); |
||
207 | |||
208 | if (!$session) { |
||
209 | throw new Exception(get_lang('NoSession')); |
||
210 | } |
||
211 | |||
212 | $this->session = $session; |
||
213 | |||
214 | ChamiloSession::write('session_name', $session->getName()); |
||
215 | ChamiloSession::write('id_session', $session->getId()); |
||
216 | } |
||
217 | |||
218 | /** |
||
219 | * @param string $registrationId |
||
220 | * |
||
221 | * @return bool |
||
222 | */ |
||
223 | public function setGcmId($registrationId) |
||
224 | { |
||
225 | $registrationId = Security::remove_XSS($registrationId); |
||
226 | $extraFieldValue = new ExtraFieldValue('user'); |
||
227 | |||
228 | return $extraFieldValue->save( |
||
229 | [ |
||
230 | 'variable' => self::EXTRA_FIELD_GCM_REGISTRATION, |
||
231 | 'value' => $registrationId, |
||
232 | 'item_id' => $this->user->getId(), |
||
233 | ] |
||
234 | ); |
||
235 | } |
||
236 | |||
237 | /** |
||
238 | * @param int $lastMessageId |
||
239 | * |
||
240 | * @return array |
||
241 | */ |
||
242 | public function getUserMessages($lastMessageId = 0) |
||
243 | { |
||
244 | $lastMessages = MessageManager::getMessagesFromLastReceivedMessage($this->user->getId(), $lastMessageId); |
||
245 | $messages = []; |
||
246 | |||
247 | foreach ($lastMessages as $message) { |
||
248 | $hasAttachments = MessageManager::hasAttachments($message['id']); |
||
249 | |||
250 | $messages[] = [ |
||
251 | 'id' => $message['id'], |
||
252 | 'title' => $message['title'], |
||
253 | 'sender' => [ |
||
254 | 'id' => $message['user_id'], |
||
255 | 'lastname' => $message['lastname'], |
||
256 | 'firstname' => $message['firstname'], |
||
257 | 'completeName' => api_get_person_name($message['firstname'], $message['lastname']), |
||
258 | ], |
||
259 | 'sendDate' => $message['send_date'], |
||
260 | 'content' => $message['content'], |
||
261 | 'hasAttachments' => $hasAttachments, |
||
262 | 'url' => api_get_path(WEB_CODE_PATH).'messages/view_message.php?' |
||
263 | .http_build_query(['type' => 1, 'id' => $message['id']]), |
||
264 | ]; |
||
265 | } |
||
266 | |||
267 | return $messages; |
||
268 | } |
||
269 | |||
270 | /** |
||
271 | * @return array |
||
272 | */ |
||
273 | public function getUserReceivedMessages() |
||
274 | { |
||
275 | $lastMessages = MessageManager::getReceivedMessages($this->user->getId(), 0); |
||
276 | $messages = []; |
||
277 | |||
278 | $webPath = api_get_path(WEB_PATH); |
||
279 | |||
280 | foreach ($lastMessages as $message) { |
||
281 | $hasAttachments = MessageManager::hasAttachments($message['id']); |
||
282 | $attachmentList = []; |
||
283 | if ($hasAttachments) { |
||
284 | $attachmentList = MessageManager::getAttachmentList($message['id']); |
||
285 | } |
||
286 | $messages[] = [ |
||
287 | 'id' => $message['id'], |
||
288 | 'title' => $message['title'], |
||
289 | 'msgStatus' => $message['msg_status'], |
||
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 | 'pictureUri' => $message['pictureUri'], |
||
296 | ], |
||
297 | 'sendDate' => $message['send_date'], |
||
298 | 'content' => str_replace('src="/"', $webPath, $message['content']), |
||
299 | 'hasAttachments' => $hasAttachments, |
||
300 | 'attachmentList' => $attachmentList, |
||
301 | 'url' => '', |
||
302 | ]; |
||
303 | } |
||
304 | |||
305 | return $messages; |
||
306 | } |
||
307 | |||
308 | /** |
||
309 | * @return array |
||
310 | */ |
||
311 | public function getUserSentMessages() |
||
312 | { |
||
313 | $lastMessages = MessageManager::getSentMessages($this->user->getId(), 0); |
||
314 | $messages = []; |
||
315 | |||
316 | foreach ($lastMessages as $message) { |
||
317 | $hasAttachments = MessageManager::hasAttachments($message['id']); |
||
318 | |||
319 | $messages[] = [ |
||
320 | 'id' => $message['id'], |
||
321 | 'title' => $message['title'], |
||
322 | 'msgStatus' => $message['msg_status'], |
||
323 | 'receiver' => [ |
||
324 | 'id' => $message['user_id'], |
||
325 | 'lastname' => $message['lastname'], |
||
326 | 'firstname' => $message['firstname'], |
||
327 | 'completeName' => api_get_person_name($message['firstname'], $message['lastname']), |
||
328 | 'pictureUri' => $message['pictureUri'], |
||
329 | ], |
||
330 | 'sendDate' => $message['send_date'], |
||
331 | 'content' => $message['content'], |
||
332 | 'hasAttachments' => $hasAttachments, |
||
333 | 'url' => '', |
||
334 | ]; |
||
335 | } |
||
336 | |||
337 | return $messages; |
||
338 | } |
||
339 | |||
340 | /** |
||
341 | * Get the user courses. |
||
342 | */ |
||
343 | public function getUserCourses($userId = 0): array |
||
344 | { |
||
345 | if (empty($userId)) { |
||
346 | $userId = $this->user->getId(); |
||
347 | } |
||
348 | |||
349 | Event::courseLogout( |
||
350 | [ |
||
351 | 'uid' => $userId, |
||
352 | 'cid' => api_get_course_id(), |
||
353 | 'sid' => api_get_session_id(), |
||
354 | ] |
||
355 | ); |
||
356 | |||
357 | $courses = CourseManager::get_courses_list_by_user_id($userId); |
||
358 | $data = []; |
||
359 | |||
360 | foreach ($courses as $courseInfo) { |
||
361 | /** @var Course $course */ |
||
362 | $course = Database::getManager()->find('ChamiloCoreBundle:Course', $courseInfo['real_id']); |
||
363 | $teachers = CourseManager::getTeacherListFromCourseCodeToString($course->getCode()); |
||
364 | $picturePath = CourseManager::getPicturePath($course, true) |
||
365 | ?: Display::return_icon('session_default.png', null, null, null, null, true); |
||
366 | |||
367 | $data[] = [ |
||
368 | 'id' => $course->getId(), |
||
369 | 'title' => $course->getTitle(), |
||
370 | 'code' => $course->getCode(), |
||
371 | 'directory' => $course->getDirectory(), |
||
372 | 'urlPicture' => $picturePath, |
||
373 | 'teachers' => $teachers, |
||
374 | 'isSpecial' => !empty($courseInfo['special_course']), |
||
375 | ]; |
||
376 | } |
||
377 | |||
378 | return $data; |
||
379 | } |
||
380 | |||
381 | /** |
||
382 | * @throws Exception |
||
383 | * |
||
384 | * @return array |
||
385 | */ |
||
386 | public function getCourseInfo() |
||
387 | { |
||
388 | $teachers = CourseManager::getTeacherListFromCourseCodeToString($this->course->getCode()); |
||
389 | $tools = CourseHome::get_tools_category( |
||
390 | TOOL_STUDENT_VIEW, |
||
391 | $this->course->getId(), |
||
392 | $this->session ? $this->session->getId() : 0 |
||
393 | ); |
||
394 | |||
395 | return [ |
||
396 | 'id' => $this->course->getId(), |
||
397 | 'title' => $this->course->getTitle(), |
||
398 | 'code' => $this->course->getCode(), |
||
399 | 'directory' => $this->course->getDirectory(), |
||
400 | 'urlPicture' => CourseManager::getPicturePath($this->course, true), |
||
401 | 'teachers' => $teachers, |
||
402 | 'tools' => array_map( |
||
403 | function ($tool) { |
||
404 | return ['type' => $tool['name']]; |
||
405 | }, |
||
406 | $tools |
||
407 | ), |
||
408 | ]; |
||
409 | } |
||
410 | |||
411 | /** |
||
412 | * Get the course descriptions. |
||
413 | * |
||
414 | * @throws Exception |
||
415 | * |
||
416 | * @return array |
||
417 | */ |
||
418 | public function getCourseDescriptions() |
||
419 | { |
||
420 | Event::event_access_tool(TOOL_COURSE_DESCRIPTION); |
||
421 | |||
422 | $descriptions = CourseDescription::get_descriptions($this->course->getId()); |
||
423 | $results = []; |
||
424 | |||
425 | $webPath = api_get_path(WEB_PATH); |
||
426 | |||
427 | /** @var CourseDescription $description */ |
||
428 | foreach ($descriptions as $description) { |
||
429 | $results[] = [ |
||
430 | 'id' => $description->get_description_type(), |
||
431 | 'title' => $description->get_title(), |
||
432 | 'content' => str_replace('src="/', 'src="'.$webPath, $description->get_content()), |
||
433 | ]; |
||
434 | } |
||
435 | |||
436 | return $results; |
||
437 | } |
||
438 | |||
439 | /** |
||
440 | * @param int $directoryId |
||
441 | * |
||
442 | * @throws Exception |
||
443 | * |
||
444 | * @return array |
||
445 | */ |
||
446 | public function getCourseDocuments($directoryId = 0) |
||
447 | { |
||
448 | Event::event_access_tool(TOOL_DOCUMENT); |
||
449 | |||
450 | /** @var string $path */ |
||
451 | $path = '/'; |
||
452 | $sessionId = $this->session ? $this->session->getId() : 0; |
||
453 | |||
454 | if ($directoryId) { |
||
455 | $directory = DocumentManager::get_document_data_by_id( |
||
456 | $directoryId, |
||
457 | $this->course->getCode(), |
||
458 | false, |
||
459 | $sessionId |
||
460 | ); |
||
461 | |||
462 | if (!$directory) { |
||
463 | throw new Exception('NoDataAvailable'); |
||
464 | } |
||
465 | |||
466 | $path = $directory['path']; |
||
467 | } |
||
468 | |||
469 | $courseInfo = api_get_course_info_by_id($this->course->getId()); |
||
470 | $documents = DocumentManager::getAllDocumentData( |
||
471 | $courseInfo, |
||
472 | $path, |
||
473 | 0, |
||
474 | null, |
||
475 | false, |
||
476 | false, |
||
477 | $sessionId |
||
478 | ); |
||
479 | $results = []; |
||
480 | |||
481 | if (!empty($documents)) { |
||
482 | $webPath = api_get_path(WEB_CODE_PATH).'document/document.php?'; |
||
483 | |||
484 | /** @var array $document */ |
||
485 | foreach ($documents as $document) { |
||
486 | if ($document['visibility'] != '1') { |
||
487 | continue; |
||
488 | } |
||
489 | |||
490 | $icon = $document['filetype'] == 'file' |
||
491 | ? choose_image($document['path']) |
||
492 | : chooseFolderIcon($document['path']); |
||
493 | |||
494 | $results[] = [ |
||
495 | 'id' => $document['id'], |
||
496 | 'type' => $document['filetype'], |
||
497 | 'title' => $document['title'], |
||
498 | 'path' => $document['path'], |
||
499 | 'url' => $webPath.http_build_query( |
||
500 | [ |
||
501 | 'username' => $this->user->getUsername(), |
||
502 | 'api_key' => $this->apiKey, |
||
503 | 'cidReq' => $this->course->getCode(), |
||
504 | 'id_session' => $sessionId, |
||
505 | 'gidReq' => 0, |
||
506 | 'gradebook' => 0, |
||
507 | 'origin' => '', |
||
508 | 'action' => 'download', |
||
509 | 'id' => $document['id'], |
||
510 | ] |
||
511 | ), |
||
512 | 'icon' => $icon, |
||
513 | 'size' => format_file_size($document['size']), |
||
514 | ]; |
||
515 | } |
||
516 | } |
||
517 | |||
518 | return $results; |
||
519 | } |
||
520 | |||
521 | /** |
||
522 | * @throws Exception |
||
523 | * |
||
524 | * @return array |
||
525 | */ |
||
526 | public function getCourseAnnouncements() |
||
527 | { |
||
528 | Event::event_access_tool(TOOL_ANNOUNCEMENT); |
||
529 | |||
530 | $sessionId = $this->session ? $this->session->getId() : 0; |
||
531 | |||
532 | $announcements = AnnouncementManager::getAnnouncements( |
||
533 | null, |
||
534 | null, |
||
535 | false, |
||
536 | null, |
||
537 | null, |
||
538 | null, |
||
539 | null, |
||
540 | null, |
||
541 | 0, |
||
542 | $this->user->getId(), |
||
543 | $this->course->getId(), |
||
544 | $sessionId |
||
545 | ); |
||
546 | |||
547 | $announcements = array_map( |
||
548 | function ($announcement) { |
||
549 | return [ |
||
550 | 'id' => (int) $announcement['id'], |
||
551 | 'title' => strip_tags($announcement['title']), |
||
552 | 'creatorName' => strip_tags($announcement['username']), |
||
553 | 'date' => strip_tags($announcement['insert_date']), |
||
554 | ]; |
||
555 | }, |
||
556 | $announcements |
||
557 | ); |
||
558 | |||
559 | return $announcements; |
||
560 | } |
||
561 | |||
562 | /** |
||
563 | * @param int $announcementId |
||
564 | * |
||
565 | * @throws Exception |
||
566 | * |
||
567 | * @return array |
||
568 | */ |
||
569 | public function getCourseAnnouncement($announcementId) |
||
570 | { |
||
571 | Event::event_access_tool(TOOL_ANNOUNCEMENT); |
||
572 | |||
573 | $sessionId = $this->session ? $this->session->getId() : 0; |
||
574 | $announcement = AnnouncementManager::getAnnouncementInfoById( |
||
575 | $announcementId, |
||
576 | $this->course->getId(), |
||
577 | $this->user->getId() |
||
578 | ); |
||
579 | |||
580 | if (!$announcement) { |
||
581 | throw new Exception(get_lang('NoAnnouncement')); |
||
582 | } |
||
583 | |||
584 | return [ |
||
585 | 'id' => $announcement['announcement']->getIid(), |
||
586 | 'title' => $announcement['announcement']->getTitle(), |
||
587 | 'creatorName' => UserManager::formatUserFullName($announcement['item_property']->getInsertUser()), |
||
588 | 'date' => api_convert_and_format_date( |
||
589 | $announcement['item_property']->getInsertDate(), |
||
590 | DATE_TIME_FORMAT_LONG_24H |
||
591 | ), |
||
592 | 'content' => AnnouncementManager::parseContent( |
||
593 | $this->user->getId(), |
||
594 | $announcement['announcement']->getContent(), |
||
595 | $this->course->getCode(), |
||
596 | $sessionId |
||
597 | ), |
||
598 | ]; |
||
599 | } |
||
600 | |||
601 | /** |
||
602 | * @throws Exception |
||
603 | * |
||
604 | * @return array |
||
605 | */ |
||
606 | public function getCourseAgenda() |
||
657 | ); |
||
658 | } |
||
659 | |||
660 | /** |
||
661 | * @throws Exception |
||
662 | * |
||
663 | * @return array |
||
664 | */ |
||
665 | public function getCourseNotebooks() |
||
666 | { |
||
667 | Event::event_access_tool(TOOL_NOTEBOOK); |
||
668 | |||
669 | $em = Database::getManager(); |
||
670 | /** @var CNotebookRepository $notebooksRepo */ |
||
671 | $notebooksRepo = $em->getRepository('ChamiloCourseBundle:CNotebook'); |
||
672 | $notebooks = $notebooksRepo->findByUser($this->user, $this->course, $this->session); |
||
673 | |||
674 | return array_map( |
||
675 | function (CNotebook $notebook) { |
||
676 | return [ |
||
677 | 'id' => $notebook->getIid(), |
||
678 | 'title' => $notebook->getTitle(), |
||
679 | 'description' => $notebook->getDescription(), |
||
680 | 'creationDate' => api_format_date( |
||
681 | $notebook->getCreationDate()->getTimestamp() |
||
682 | ), |
||
683 | 'updateDate' => api_format_date( |
||
684 | $notebook->getUpdateDate()->getTimestamp() |
||
685 | ), |
||
686 | ]; |
||
687 | }, |
||
688 | $notebooks |
||
689 | ); |
||
690 | } |
||
691 | |||
692 | /** |
||
693 | * @throws Exception |
||
694 | * |
||
695 | * @return array |
||
696 | */ |
||
697 | public function getCourseForumCategories() |
||
698 | { |
||
699 | Event::event_access_tool(TOOL_FORUM); |
||
700 | |||
701 | $sessionId = $this->session ? $this->session->getId() : 0; |
||
702 | $webCoursePath = api_get_path(WEB_COURSE_PATH).$this->course->getDirectory().'/upload/forum/images/'; |
||
703 | |||
704 | require_once api_get_path(SYS_CODE_PATH).'forum/forumfunction.inc.php'; |
||
705 | |||
706 | $categoriesFullData = get_forum_categories('', $this->course->getId(), $sessionId); |
||
707 | $categories = []; |
||
708 | $includeGroupsForums = api_get_setting('display_groups_forum_in_general_tool') === 'true'; |
||
709 | $forumsFullData = get_forums('', $this->course->getCode(), $includeGroupsForums, $sessionId); |
||
710 | $forums = []; |
||
711 | |||
712 | foreach ($forumsFullData as $forumId => $forumInfo) { |
||
713 | $forum = [ |
||
714 | 'id' => (int) $forumInfo['iid'], |
||
715 | 'catId' => (int) $forumInfo['forum_category'], |
||
716 | 'title' => $forumInfo['forum_title'], |
||
717 | 'description' => $forumInfo['forum_comment'], |
||
718 | 'image' => $forumInfo['forum_image'] ? ($webCoursePath.$forumInfo['forum_image']) : '', |
||
719 | 'numberOfThreads' => isset($forumInfo['number_of_threads']) ? intval( |
||
720 | $forumInfo['number_of_threads'] |
||
721 | ) : 0, |
||
722 | 'lastPost' => null, |
||
723 | ]; |
||
724 | |||
725 | $lastPostInfo = get_last_post_information($forumId, false, $this->course->getId(), $sessionId); |
||
726 | |||
727 | if ($lastPostInfo) { |
||
728 | $forum['lastPost'] = [ |
||
729 | 'date' => api_convert_and_format_date($lastPostInfo['last_post_date']), |
||
730 | 'user' => api_get_person_name( |
||
731 | $lastPostInfo['last_poster_firstname'], |
||
732 | $lastPostInfo['last_poster_lastname'] |
||
733 | ), |
||
734 | ]; |
||
735 | } |
||
736 | |||
737 | $forums[] = $forum; |
||
738 | } |
||
739 | |||
740 | foreach ($categoriesFullData as $category) { |
||
741 | $categoryForums = array_filter( |
||
742 | $forums, |
||
743 | function (array $forum) use ($category) { |
||
744 | if ($forum['catId'] != $category['cat_id']) { |
||
745 | return false; |
||
746 | } |
||
747 | |||
748 | return true; |
||
749 | } |
||
750 | ); |
||
751 | |||
752 | $categories[] = [ |
||
753 | 'id' => (int) $category['iid'], |
||
754 | 'title' => $category['cat_title'], |
||
755 | 'catId' => (int) $category['cat_id'], |
||
756 | 'description' => $category['cat_comment'], |
||
757 | 'forums' => $categoryForums, |
||
758 | 'courseId' => $this->course->getId(), |
||
759 | ]; |
||
760 | } |
||
761 | |||
762 | return $categories; |
||
763 | } |
||
764 | |||
765 | /** |
||
766 | * @param int $forumId |
||
767 | * |
||
768 | * @throws Exception |
||
769 | * |
||
770 | * @return array |
||
771 | */ |
||
772 | public function getCourseForum($forumId) |
||
773 | { |
||
774 | Event::event_access_tool(TOOL_FORUM); |
||
775 | |||
776 | require_once api_get_path(SYS_CODE_PATH).'forum/forumfunction.inc.php'; |
||
777 | |||
778 | $sessionId = $this->session ? $this->session->getId() : 0; |
||
779 | $forumInfo = get_forums($forumId, $this->course->getCode(), true, $sessionId); |
||
780 | |||
781 | if (!isset($forumInfo['iid'])) { |
||
782 | throw new Exception(get_lang('NoForum')); |
||
783 | } |
||
784 | |||
785 | $webCoursePath = api_get_path(WEB_COURSE_PATH).$this->course->getDirectory().'/upload/forum/images/'; |
||
786 | $forum = [ |
||
787 | 'id' => $forumInfo['iid'], |
||
788 | 'title' => $forumInfo['forum_title'], |
||
789 | 'description' => $forumInfo['forum_comment'], |
||
790 | 'image' => $forumInfo['forum_image'] ? ($webCoursePath.$forumInfo['forum_image']) : '', |
||
791 | 'threads' => [], |
||
792 | ]; |
||
793 | |||
794 | $threads = get_threads($forumInfo['iid'], $this->course->getId(), $sessionId); |
||
795 | |||
796 | foreach ($threads as $thread) { |
||
797 | $forum['threads'][] = [ |
||
798 | 'id' => $thread['iid'], |
||
799 | 'title' => $thread['thread_title'], |
||
800 | 'lastEditDate' => api_convert_and_format_date($thread['lastedit_date'], DATE_TIME_FORMAT_LONG_24H), |
||
801 | 'numberOfReplies' => $thread['thread_replies'], |
||
802 | 'numberOfViews' => $thread['thread_views'], |
||
803 | 'author' => api_get_person_name($thread['firstname'], $thread['lastname']), |
||
804 | ]; |
||
805 | } |
||
806 | |||
807 | return $forum; |
||
808 | } |
||
809 | |||
810 | /** |
||
811 | * @param int $forumId |
||
812 | * @param int $threadId |
||
813 | * |
||
814 | * @return array |
||
815 | */ |
||
816 | public function getCourseForumThread($forumId, $threadId) |
||
817 | { |
||
818 | Event::event_access_tool(TOOL_FORUM); |
||
819 | |||
820 | require_once api_get_path(SYS_CODE_PATH).'forum/forumfunction.inc.php'; |
||
821 | |||
822 | $sessionId = $this->session ? $this->session->getId() : 0; |
||
823 | $threadInfo = get_thread_information($forumId, $threadId, $sessionId); |
||
824 | |||
825 | $thread = [ |
||
826 | 'id' => intval($threadInfo['iid']), |
||
827 | 'cId' => intval($threadInfo['c_id']), |
||
828 | 'title' => $threadInfo['thread_title'], |
||
829 | 'forumId' => intval($threadInfo['forum_id']), |
||
830 | 'posts' => [], |
||
831 | ]; |
||
832 | |||
833 | $forumInfo = get_forums($threadInfo['forum_id'], $this->course->getCode(), true, $sessionId); |
||
834 | $postsInfo = getPosts($forumInfo, $threadInfo['iid'], 'ASC'); |
||
835 | |||
836 | foreach ($postsInfo as $postInfo) { |
||
837 | $thread['posts'][] = [ |
||
838 | 'id' => $postInfo['iid'], |
||
839 | 'title' => $postInfo['post_title'], |
||
840 | 'text' => $postInfo['post_text'], |
||
841 | 'author' => api_get_person_name($postInfo['firstname'], $postInfo['lastname']), |
||
842 | 'date' => api_convert_and_format_date($postInfo['post_date'], DATE_TIME_FORMAT_LONG_24H), |
||
843 | 'parentId' => $postInfo['post_parent_id'], |
||
844 | ]; |
||
845 | } |
||
846 | |||
847 | return $thread; |
||
848 | } |
||
849 | |||
850 | /** |
||
851 | * @return array |
||
852 | */ |
||
853 | public function getUserProfile() |
||
854 | { |
||
855 | $pictureInfo = UserManager::get_user_picture_path_by_id($this->user->getId(), 'web'); |
||
856 | |||
857 | $result = [ |
||
858 | 'pictureUri' => $pictureInfo['dir'].$pictureInfo['file'], |
||
859 | 'id' => $this->user->getId(), |
||
860 | 'status' => $this->user->getStatus(), |
||
861 | 'fullName' => UserManager::formatUserFullName($this->user), |
||
862 | 'username' => $this->user->getUsername(), |
||
863 | 'officialCode' => $this->user->getOfficialCode(), |
||
864 | 'phone' => $this->user->getPhone(), |
||
865 | 'extra' => [], |
||
866 | ]; |
||
867 | |||
868 | $fieldValue = new ExtraFieldValue('user'); |
||
869 | $extraInfo = $fieldValue->getAllValuesForAnItem($this->user->getId(), true); |
||
870 | |||
871 | foreach ($extraInfo as $extra) { |
||
872 | /** @var ExtraFieldValues $extraValue */ |
||
873 | $extraValue = $extra['value']; |
||
874 | $result['extra'][] = [ |
||
875 | 'title' => $extraValue->getField()->getDisplayText(true), |
||
876 | 'value' => $extraValue->getValue(), |
||
877 | ]; |
||
878 | } |
||
879 | |||
880 | return $result; |
||
881 | } |
||
882 | |||
883 | public function getCourseLpProgress() |
||
884 | { |
||
885 | $sessionId = $this->session ? $this->session->getId() : 0; |
||
886 | $userId = $this->user->getId(); |
||
887 | |||
888 | /*$sessionId = $this->session ? $this->session->getId() : 0; |
||
889 | $courseId = $this->course->getId();*/ |
||
890 | |||
891 | $result = Tracking::getCourseLpProgress($userId, $sessionId); |
||
892 | |||
893 | return [$result]; |
||
894 | } |
||
895 | |||
896 | /** |
||
897 | * @throws Exception |
||
898 | * |
||
899 | * @return array |
||
900 | */ |
||
901 | public function getCourseLearnPaths() |
||
902 | { |
||
903 | Event::event_access_tool(TOOL_LEARNPATH); |
||
904 | |||
905 | $sessionId = $this->session ? $this->session->getId() : 0; |
||
906 | $categoriesTempList = learnpath::getCategories($this->course->getId()); |
||
907 | |||
908 | $categoryNone = new CLpCategory(); |
||
909 | $categoryNone->setId(0); |
||
910 | $categoryNone->setName(get_lang('WithOutCategory')); |
||
911 | $categoryNone->setPosition(0); |
||
912 | |||
913 | $categories = array_merge([$categoryNone], $categoriesTempList); |
||
914 | $categoryData = []; |
||
915 | |||
916 | /** @var CLpCategory $category */ |
||
917 | foreach ($categories as $category) { |
||
918 | $learnPathList = new LearnpathList( |
||
919 | $this->user->getId(), |
||
920 | api_get_course_info($this->course->getCode()), |
||
921 | $sessionId, |
||
922 | null, |
||
923 | false, |
||
924 | $category->getId() |
||
925 | ); |
||
926 | |||
927 | $flatLpList = $learnPathList->get_flat_list(); |
||
928 | |||
929 | if (empty($flatLpList)) { |
||
930 | continue; |
||
931 | } |
||
932 | |||
933 | $listData = []; |
||
934 | |||
935 | foreach ($flatLpList as $lpId => $lpDetails) { |
||
936 | if ($lpDetails['lp_visibility'] == 0) { |
||
937 | continue; |
||
938 | } |
||
939 | |||
940 | if (!learnpath::is_lp_visible_for_student( |
||
941 | $lpId, |
||
942 | $this->user->getId(), |
||
943 | api_get_course_info($this->course->getCode()), |
||
944 | $sessionId |
||
945 | )) { |
||
946 | continue; |
||
947 | } |
||
948 | |||
949 | $timeLimits = false; |
||
950 | |||
951 | // This is an old LP (from a migration 1.8.7) so we do nothing |
||
952 | if (empty($lpDetails['created_on']) && empty($lpDetails['modified_on'])) { |
||
953 | $timeLimits = false; |
||
954 | } |
||
955 | |||
956 | // Checking if expired_on is ON |
||
957 | if (!empty($lpDetails['expired_on'])) { |
||
958 | $timeLimits = true; |
||
959 | } |
||
960 | |||
961 | if ($timeLimits) { |
||
962 | if (!empty($lpDetails['publicated_on']) && !empty($lpDetails['expired_on'])) { |
||
963 | $startTime = api_strtotime($lpDetails['publicated_on'], 'UTC'); |
||
964 | $endTime = api_strtotime($lpDetails['expired_on'], 'UTC'); |
||
965 | $now = time(); |
||
966 | $isActiveTime = false; |
||
967 | |||
968 | if ($now > $startTime && $endTime > $now) { |
||
969 | $isActiveTime = true; |
||
970 | } |
||
971 | |||
972 | if (!$isActiveTime) { |
||
973 | continue; |
||
974 | } |
||
975 | } |
||
976 | } |
||
977 | |||
978 | $progress = learnpath::getProgress($lpId, $this->user->getId(), $this->course->getId(), $sessionId); |
||
979 | |||
980 | $listData[] = [ |
||
981 | 'id' => $lpId, |
||
982 | 'title' => Security::remove_XSS($lpDetails['lp_name']), |
||
983 | 'progress' => $progress, |
||
984 | 'url' => api_get_path(WEB_CODE_PATH).'webservices/api/v2.php?'.http_build_query( |
||
985 | [ |
||
986 | 'hash' => $this->encodeParams( |
||
987 | [ |
||
988 | 'action' => 'course_learnpath', |
||
989 | 'lp_id' => $lpId, |
||
990 | 'course' => $this->course->getId(), |
||
991 | 'session' => $sessionId, |
||
992 | ] |
||
993 | ), |
||
994 | ] |
||
995 | ), |
||
996 | ]; |
||
997 | } |
||
998 | |||
999 | if (empty($listData)) { |
||
1000 | continue; |
||
1001 | } |
||
1002 | |||
1003 | $categoryData[] = [ |
||
1004 | 'id' => $category->getId(), |
||
1005 | 'name' => $category->getName(), |
||
1006 | 'learnpaths' => $listData, |
||
1007 | ]; |
||
1008 | } |
||
1009 | |||
1010 | return $categoryData; |
||
1011 | } |
||
1012 | |||
1013 | /** |
||
1014 | * Start login for a user. Then make a redirect to show the learnpath. |
||
1015 | * |
||
1016 | * @param int $lpId |
||
1017 | */ |
||
1018 | public function showLearningPath($lpId) |
||
1019 | { |
||
1020 | $loggedUser['user_id'] = $this->user->getId(); |
||
1021 | $loggedUser['status'] = $this->user->getStatus(); |
||
1022 | $loggedUser['uidReset'] = true; |
||
1023 | $sessionId = $this->session ? $this->session->getId() : 0; |
||
1024 | |||
1025 | ChamiloSession::write('_user', $loggedUser); |
||
1026 | Login::init_user($this->user->getId(), true); |
||
1027 | |||
1028 | $url = api_get_path(WEB_CODE_PATH).'lp/lp_controller.php?'.http_build_query( |
||
1029 | [ |
||
1030 | 'cidReq' => $this->course->getCode(), |
||
1031 | 'id_session' => $sessionId, |
||
1032 | 'gidReq' => 0, |
||
1033 | 'gradebook' => 0, |
||
1034 | 'origin' => '', |
||
1035 | 'action' => 'view', |
||
1036 | 'lp_id' => (int) $lpId, |
||
1037 | 'isStudentView' => 'true', |
||
1038 | ] |
||
1039 | ); |
||
1040 | |||
1041 | header("Location: $url"); |
||
1042 | exit; |
||
1043 | } |
||
1044 | |||
1045 | /** |
||
1046 | * @param int $forumId |
||
1047 | * |
||
1048 | * @return array |
||
1049 | */ |
||
1050 | public function saveForumPost(array $postValues, $forumId) |
||
1051 | { |
||
1052 | Event::event_access_tool(TOOL_FORUM); |
||
1053 | |||
1054 | require_once api_get_path(SYS_CODE_PATH).'forum/forumfunction.inc.php'; |
||
1055 | |||
1056 | $forum = get_forums($forumId, $this->course->getCode()); |
||
1057 | store_reply($forum, $postValues, $this->course->getId(), $this->user->getId()); |
||
1058 | |||
1059 | return [ |
||
1060 | 'registered' => true, |
||
1061 | ]; |
||
1062 | } |
||
1063 | |||
1064 | /** |
||
1065 | * Get the list of sessions for current user. |
||
1066 | * |
||
1067 | * @return array the sessions list |
||
1068 | */ |
||
1069 | public function getUserSessions() |
||
1070 | { |
||
1071 | $data = []; |
||
1072 | $sessionsByCategory = UserManager::get_sessions_by_category($this->user->getId(), false); |
||
1073 | |||
1074 | foreach ($sessionsByCategory as $category) { |
||
1075 | $categorySessions = []; |
||
1076 | |||
1077 | foreach ($category['sessions'] as $sessions) { |
||
1078 | $sessionCourses = []; |
||
1079 | |||
1080 | foreach ($sessions['courses'] as $course) { |
||
1081 | $courseInfo = api_get_course_info_by_id($course['real_id']); |
||
1082 | $teachers = SessionManager::getCoachesByCourseSessionToString( |
||
1083 | $sessions['session_id'], |
||
1084 | $course['real_id'] |
||
1085 | ); |
||
1086 | |||
1087 | $sessionCourses[] = [ |
||
1088 | 'id' => $courseInfo['real_id'], |
||
1089 | 'title' => $courseInfo['title'], |
||
1090 | 'code' => $courseInfo['code'], |
||
1091 | 'directory' => $courseInfo['directory'], |
||
1092 | 'pictureUrl' => $courseInfo['course_image_large'], |
||
1093 | 'urlPicture' => $courseInfo['course_image_large'], |
||
1094 | 'teachers' => $teachers, |
||
1095 | ]; |
||
1096 | } |
||
1097 | |||
1098 | $sessionBox = Display::getSessionTitleBox($sessions['session_id']); |
||
1099 | |||
1100 | $categorySessions[] = [ |
||
1101 | 'name' => $sessionBox['title'], |
||
1102 | 'id' => $sessions['session_id'], |
||
1103 | 'date' => $sessionBox['dates'], |
||
1104 | 'duration' => isset($sessionBox['duration']) ? $sessionBox['duration'] : null, |
||
1105 | 'courses' => $sessionCourses, |
||
1106 | ]; |
||
1107 | } |
||
1108 | |||
1109 | $data[] = [ |
||
1110 | 'id' => $category['session_category']['id'], |
||
1111 | 'name' => $category['session_category']['name'], |
||
1112 | 'sessions' => $categorySessions, |
||
1113 | ]; |
||
1114 | } |
||
1115 | |||
1116 | return $data; |
||
1117 | } |
||
1118 | |||
1119 | public function getUsersSubscribedToCourse() |
||
1120 | { |
||
1121 | $users = CourseManager::get_user_list_from_course_code($this->course->getCode()); |
||
1122 | |||
1123 | $userList = []; |
||
1124 | foreach ($users as $user) { |
||
1125 | $userList[] = [ |
||
1126 | 'user_id' => $user['user_id'], |
||
1127 | 'username' => $user['username'], |
||
1128 | 'firstname' => $user['firstname'], |
||
1129 | 'lastname' => $user['lastname'], |
||
1130 | 'status_rel' => $user['status_rel'], |
||
1131 | ]; |
||
1132 | } |
||
1133 | |||
1134 | return $userList; |
||
1135 | } |
||
1136 | |||
1137 | /** |
||
1138 | * @param string $subject |
||
1139 | * @param string $text |
||
1140 | * |
||
1141 | * @return array |
||
1142 | */ |
||
1143 | public function saveUserMessage($subject, $text, array $receivers) |
||
1144 | { |
||
1145 | foreach ($receivers as $userId) { |
||
1146 | MessageManager::send_message($userId, $subject, $text); |
||
1147 | } |
||
1148 | |||
1149 | return [ |
||
1150 | 'sent' => true, |
||
1151 | ]; |
||
1152 | } |
||
1153 | |||
1154 | /** |
||
1155 | * @param string $search |
||
1156 | * |
||
1157 | * @return array |
||
1158 | */ |
||
1159 | public function getMessageUsers($search) |
||
1160 | { |
||
1161 | $repo = UserManager::getRepository(); |
||
1162 | |||
1163 | $users = $repo->findUsersToSendMessage($this->user->getId(), $search); |
||
1164 | $showEmail = api_get_setting('show_email_addresses') === 'true'; |
||
1165 | $data = []; |
||
1166 | |||
1167 | /** @var User $user */ |
||
1168 | foreach ($users as $user) { |
||
1169 | $userName = UserManager::formatUserFullName($user); |
||
1170 | |||
1171 | if ($showEmail) { |
||
1172 | $userName .= " ({$user->getEmail()})"; |
||
1173 | } |
||
1174 | |||
1175 | $data[] = [ |
||
1176 | 'id' => $user->getId(), |
||
1177 | 'name' => $userName, |
||
1178 | ]; |
||
1179 | } |
||
1180 | |||
1181 | return $data; |
||
1182 | } |
||
1183 | |||
1184 | /** |
||
1185 | * @param string $title |
||
1186 | * @param string $text |
||
1187 | * |
||
1188 | * @return array |
||
1189 | */ |
||
1190 | public function saveCourseNotebook($title, $text) |
||
1191 | { |
||
1192 | Event::event_access_tool(TOOL_NOTEBOOK); |
||
1193 | |||
1194 | $values = ['note_title' => $title, 'note_comment' => $text]; |
||
1195 | $sessionId = $this->session ? $this->session->getId() : 0; |
||
1196 | |||
1197 | $noteBookId = NotebookManager::save_note( |
||
1198 | $values, |
||
1199 | $this->user->getId(), |
||
1200 | $this->course->getId(), |
||
1201 | $sessionId |
||
1202 | ); |
||
1203 | |||
1204 | return [ |
||
1205 | 'registered' => $noteBookId, |
||
1206 | ]; |
||
1207 | } |
||
1208 | |||
1209 | /** |
||
1210 | * @param int $forumId |
||
1211 | * |
||
1212 | * @return array |
||
1213 | */ |
||
1214 | public function saveForumThread(array $values, $forumId) |
||
1215 | { |
||
1216 | Event::event_access_tool(TOOL_FORUM); |
||
1217 | |||
1218 | require_once api_get_path(SYS_CODE_PATH).'forum/forumfunction.inc.php'; |
||
1219 | |||
1220 | $sessionId = $this->session ? $this->session->getId() : 0; |
||
1221 | $forum = get_forums($forumId, $this->course->getCode(), true, $sessionId); |
||
1222 | $courseInfo = api_get_course_info($this->course->getCode()); |
||
1223 | $thread = store_thread($forum, $values, $courseInfo, false, $this->user->getId(), $sessionId); |
||
1224 | |||
1225 | return [ |
||
1226 | 'registered' => $thread->getIid(), |
||
1227 | ]; |
||
1228 | } |
||
1229 | |||
1230 | /** |
||
1231 | * @return array |
||
1232 | */ |
||
1233 | public function getUsersCampus(array $params) |
||
1234 | { |
||
1235 | $conditions = [ |
||
1236 | 'status' => $params['status'], |
||
1237 | ]; |
||
1238 | $idCampus = $params['id_campus']; |
||
1239 | $users = UserManager::get_user_list($conditions, ['firstname'], false, false, $idCampus); |
||
1240 | $list = []; |
||
1241 | foreach ($users as $item) { |
||
1242 | $listTemp = [ |
||
1243 | 'id' => $item['user_id'], |
||
1244 | 'firstname' => $item['firstname'], |
||
1245 | 'lastname' => $item['lastname'], |
||
1246 | 'email' => $item['email'], |
||
1247 | ]; |
||
1248 | $list[] = $listTemp; |
||
1249 | } |
||
1250 | |||
1251 | return $list; |
||
1252 | } |
||
1253 | |||
1254 | /** |
||
1255 | * @return array |
||
1256 | */ |
||
1257 | public function getCoursesCampus(array $params) |
||
1270 | ); |
||
1271 | } |
||
1272 | |||
1273 | /** |
||
1274 | * @return array |
||
1275 | */ |
||
1276 | public function addSession(array $params) |
||
1277 | { |
||
1278 | $name = $params['name']; |
||
1279 | $coach_username = (int) $params['coach_username']; |
||
1280 | $startDate = $params['access_start_date']; |
||
1281 | $endDate = $params['access_end_date']; |
||
1282 | $displayStartDate = $startDate; |
||
1283 | $displayEndDate = $endDate; |
||
1284 | $description = $params['description']; |
||
1285 | $idUrlCampus = $params['id_campus']; |
||
1286 | $extraFields = isset($params['extra']) ? $params['extra'] : []; |
||
1287 | |||
1288 | $return = SessionManager::create_session( |
||
1289 | $name, |
||
1290 | $startDate, |
||
1291 | $endDate, |
||
1292 | $displayStartDate, |
||
1293 | $displayEndDate, |
||
1294 | null, |
||
1295 | null, |
||
1296 | $coach_username, |
||
1297 | null, |
||
1298 | 1, |
||
1299 | false, |
||
1300 | null, |
||
1301 | $description, |
||
1302 | 1, |
||
1303 | $extraFields, |
||
1304 | null, |
||
1305 | false, |
||
1306 | $idUrlCampus |
||
1307 | ); |
||
1308 | |||
1309 | if ($return) { |
||
1310 | $out = [ |
||
1311 | 'status' => true, |
||
1312 | 'message' => get_lang('ANewSessionWasCreated'), |
||
1313 | 'id_session' => $return, |
||
1314 | ]; |
||
1315 | } else { |
||
1316 | $out = [ |
||
1317 | 'status' => false, |
||
1318 | 'message' => get_lang('ErrorOccurred'), |
||
1319 | ]; |
||
1320 | } |
||
1321 | |||
1322 | return $out; |
||
1323 | } |
||
1324 | |||
1325 | public function addCourse(array $courseParam): array |
||
1326 | { |
||
1327 | $idCampus = isset($courseParam['id_campus']) ? $courseParam['id_campus'] : 1; |
||
1328 | $title = isset($courseParam['title']) ? $courseParam['title'] : ''; |
||
1329 | $wantedCode = isset($courseParam['wanted_code']) ? $courseParam['wanted_code'] : null; |
||
1330 | $diskQuota = isset($courseParam['disk_quota']) ? $courseParam['disk_quota'] : '100'; |
||
1331 | $visibility = isset($courseParam['visibility']) ? (int) $courseParam['visibility'] : null; |
||
1332 | $removeCampusId = $courseParam['remove_campus_id_from_wanted_code'] ?? 0; |
||
1333 | $language = $courseParam['language'] ?? ''; |
||
1334 | |||
1335 | if (isset($courseParam['visibility'])) { |
||
1336 | if ($courseParam['visibility'] && |
||
1337 | $courseParam['visibility'] >= 0 && |
||
1338 | $courseParam['visibility'] <= 3 |
||
1339 | ) { |
||
1340 | $visibility = (int) $courseParam['visibility']; |
||
1341 | } |
||
1342 | } |
||
1343 | |||
1344 | $params = []; |
||
1345 | $params['title'] = $title; |
||
1346 | $params['wanted_code'] = 'CAMPUS_'.$idCampus.'_'.$wantedCode; |
||
1347 | if (1 === (int) $removeCampusId) { |
||
1348 | $params['wanted_code'] = $wantedCode; |
||
1349 | } |
||
1350 | $params['user_id'] = $this->user->getId(); |
||
1351 | $params['visibility'] = $visibility; |
||
1352 | $params['disk_quota'] = $diskQuota; |
||
1353 | $params['course_language'] = $language; |
||
1354 | |||
1355 | foreach ($courseParam as $key => $value) { |
||
1356 | if (substr($key, 0, 6) === 'extra_') { //an extra field |
||
1357 | $params[$key] = $value; |
||
1358 | } |
||
1359 | } |
||
1360 | |||
1361 | $courseInfo = CourseManager::create_course($params, $params['user_id'], $idCampus); |
||
1362 | $results = []; |
||
1363 | if (!empty($courseInfo)) { |
||
1364 | $results['status'] = true; |
||
1365 | $results['code_course'] = $courseInfo['code']; |
||
1366 | $results['title_course'] = $courseInfo['title']; |
||
1367 | $extraFieldValues = new ExtraFieldValue('course'); |
||
1368 | $extraFields = $extraFieldValues->getAllValuesByItem($courseInfo['real_id']); |
||
1369 | $results['extra_fields'] = $extraFields; |
||
1370 | $results['message'] = sprintf(get_lang('CourseXAdded'), $courseInfo['code']); |
||
1371 | } else { |
||
1372 | $results['status'] = false; |
||
1373 | $results['message'] = get_lang('CourseCreationFailed'); |
||
1374 | } |
||
1375 | |||
1376 | return $results; |
||
1377 | } |
||
1378 | |||
1379 | /** |
||
1380 | * @param $userParam |
||
1381 | * |
||
1382 | * @throws Exception |
||
1383 | * |
||
1384 | * @return array |
||
1385 | */ |
||
1386 | public function addUser($userParam) |
||
1387 | { |
||
1388 | $firstName = $userParam['firstname']; |
||
1389 | $lastName = $userParam['lastname']; |
||
1390 | $status = $userParam['status']; |
||
1391 | $email = $userParam['email']; |
||
1392 | $loginName = $userParam['loginname']; |
||
1393 | $password = $userParam['password']; |
||
1394 | |||
1395 | $official_code = ''; |
||
1396 | $language = ''; |
||
1397 | $phone = ''; |
||
1398 | $picture_uri = ''; |
||
1399 | $auth_source = $userParam['auth_source'] ?? PLATFORM_AUTH_SOURCE; |
||
1400 | $expiration_date = ''; |
||
1401 | $active = 1; |
||
1402 | $hr_dept_id = 0; |
||
1403 | $original_user_id_name = $userParam['original_user_id_name']; |
||
1404 | $original_user_id_value = $userParam['original_user_id_value']; |
||
1405 | |||
1406 | $extra_list = isset($userParam['extra']) ? $userParam['extra'] : []; |
||
1407 | if (isset($userParam['language'])) { |
||
1408 | $language = $userParam['language']; |
||
1409 | } |
||
1410 | if (isset($userParam['phone'])) { |
||
1411 | $phone = $userParam['phone']; |
||
1412 | } |
||
1413 | if (isset($userParam['expiration_date'])) { |
||
1414 | $expiration_date = $userParam['expiration_date']; |
||
1415 | } |
||
1416 | |||
1417 | // Default language. |
||
1418 | if (empty($language)) { |
||
1419 | $language = api_get_setting('platformLanguage'); |
||
1420 | } |
||
1421 | |||
1422 | // First check wether the login already exists. |
||
1423 | if (!UserManager::is_username_available($loginName)) { |
||
1424 | throw new Exception(get_lang('UserNameNotAvailable')); |
||
1425 | } |
||
1426 | |||
1427 | $userId = UserManager::create_user( |
||
1428 | $firstName, |
||
1429 | $lastName, |
||
1430 | $status, |
||
1431 | $email, |
||
1432 | $loginName, |
||
1433 | $password, |
||
1434 | $official_code, |
||
1435 | $language, |
||
1436 | $phone, |
||
1437 | $picture_uri, |
||
1438 | $auth_source, |
||
1439 | $expiration_date, |
||
1440 | $active, |
||
1441 | $hr_dept_id |
||
1442 | ); |
||
1443 | |||
1444 | if (empty($userId)) { |
||
1445 | throw new Exception(get_lang('UserNotRegistered')); |
||
1446 | } |
||
1447 | |||
1448 | if (api_is_multiple_url_enabled()) { |
||
1449 | if (api_get_current_access_url_id() != -1) { |
||
1450 | UrlManager::add_user_to_url( |
||
1451 | $userId, |
||
1452 | api_get_current_access_url_id() |
||
1453 | ); |
||
1454 | } else { |
||
1455 | UrlManager::add_user_to_url($userId, 1); |
||
1456 | } |
||
1457 | } else { |
||
1458 | // We add by default the access_url_user table with access_url_id = 1 |
||
1459 | UrlManager::add_user_to_url($userId, 1); |
||
1460 | } |
||
1461 | |||
1462 | // Save new field label into user_field table. |
||
1463 | UserManager::create_extra_field( |
||
1464 | $original_user_id_name, |
||
1465 | 1, |
||
1466 | $original_user_id_name, |
||
1467 | '' |
||
1468 | ); |
||
1469 | // Save the external system's id into user_field_value table. |
||
1470 | UserManager::update_extra_field_value( |
||
1471 | $userId, |
||
1472 | $original_user_id_name, |
||
1473 | $original_user_id_value |
||
1474 | ); |
||
1475 | |||
1476 | if (is_array($extra_list) && count($extra_list) > 0) { |
||
1477 | foreach ($extra_list as $extra) { |
||
1478 | $extra_field_name = $extra['field_name']; |
||
1479 | $extra_field_value = $extra['field_value']; |
||
1480 | // Save new field label into user_field table. |
||
1481 | UserManager::create_extra_field( |
||
1482 | $extra_field_name, |
||
1483 | 1, |
||
1484 | $extra_field_name, |
||
1485 | '' |
||
1486 | ); |
||
1487 | // Save the external system's id into user_field_value table. |
||
1488 | UserManager::update_extra_field_value( |
||
1489 | $userId, |
||
1490 | $extra_field_name, |
||
1491 | $extra_field_value |
||
1492 | ); |
||
1493 | } |
||
1494 | } |
||
1495 | |||
1496 | return [$userId]; |
||
1497 | } |
||
1498 | |||
1499 | /** |
||
1500 | * Subscribe User to Course. |
||
1501 | * |
||
1502 | * @param array $params |
||
1503 | * |
||
1504 | * @return array |
||
1505 | */ |
||
1506 | public function subscribeUserToCourse($params) |
||
1507 | { |
||
1508 | $course_id = $params['course_id']; |
||
1509 | $course_code = $params['course_code']; |
||
1510 | $user_id = $params['user_id']; |
||
1511 | $status = $params['status'] ?? STUDENT; |
||
1512 | |||
1513 | if (!$course_id && !$course_code) { |
||
1514 | return [false]; |
||
1515 | } |
||
1516 | if (!$course_code) { |
||
1517 | $course_code = CourseManager::get_course_code_from_course_id($course_id); |
||
1518 | } |
||
1519 | |||
1520 | if (CourseManager::subscribeUser($user_id, $course_code, $status, 0, 0, false)) { |
||
1521 | return [true]; |
||
1522 | } |
||
1523 | |||
1524 | return [false]; |
||
1525 | } |
||
1526 | |||
1527 | /** |
||
1528 | * @throws Exception |
||
1529 | */ |
||
1530 | public function subscribeUserToCoursePassword($courseCode, $password) |
||
1531 | { |
||
1532 | $courseInfo = api_get_course_info($courseCode); |
||
1533 | |||
1534 | if (empty($courseInfo)) { |
||
1535 | throw new Exception(get_lang('NoCourse')); |
||
1536 | } |
||
1537 | |||
1538 | if (sha1($password) === $courseInfo['registration_code']) { |
||
1539 | CourseManager::processAutoSubscribeToCourse($courseCode); |
||
1540 | |||
1541 | return; |
||
1542 | } |
||
1543 | |||
1544 | throw new Exception(get_lang('CourseRegistrationCodeIncorrect')); |
||
1545 | } |
||
1546 | |||
1547 | public function unSubscribeUserToCourse(array $params): array |
||
1548 | { |
||
1549 | $courseId = $params['course_id']; |
||
1550 | $courseCode = $params['course_code']; |
||
1551 | $userId = $params['user_id']; |
||
1552 | |||
1553 | if (!$courseId && !$courseCode) { |
||
1554 | return [false]; |
||
1555 | } |
||
1556 | |||
1557 | if (!$courseCode) { |
||
1558 | $courseCode = CourseManager::get_course_code_from_course_id($courseId); |
||
1559 | } |
||
1560 | |||
1561 | if (CourseManager::unsubscribe_user($userId, $courseCode)) { |
||
1562 | return [true]; |
||
1563 | } |
||
1564 | |||
1565 | return [false]; |
||
1566 | } |
||
1567 | |||
1568 | public function deleteUserMessage($messageId, $messageType) |
||
1569 | { |
||
1570 | if ($messageType === 'sent') { |
||
1571 | return MessageManager::delete_message_by_user_sender($this->user->getId(), $messageId); |
||
1572 | } else { |
||
1573 | return MessageManager::delete_message_by_user_receiver($this->user->getId(), $messageId); |
||
1574 | } |
||
1575 | } |
||
1576 | |||
1577 | public function setMessageRead($messageId) |
||
1578 | { |
||
1579 | MessageManager::update_message($this->user->getId(), $messageId); |
||
1580 | } |
||
1581 | |||
1582 | /** |
||
1583 | * Add Campus Virtual. |
||
1584 | * |
||
1585 | * @param array Params Campus |
||
1586 | * |
||
1587 | * @return array |
||
1588 | */ |
||
1589 | public function createCampusURL($params) |
||
1590 | { |
||
1591 | $urlCampus = Security::remove_XSS($params['url']); |
||
1592 | $description = Security::remove_XSS($params['description']); |
||
1593 | |||
1594 | $active = isset($params['active']) ? intval($params['active']) : 0; |
||
1595 | $num = UrlManager::url_exist($urlCampus); |
||
1596 | if ($num == 0) { |
||
1597 | // checking url |
||
1598 | if (substr($urlCampus, strlen($urlCampus) - 1, strlen($urlCampus)) == '/') { |
||
1599 | $idCampus = UrlManager::add($urlCampus, $description, $active, true); |
||
1600 | } else { |
||
1601 | //create |
||
1602 | $idCampus = UrlManager::add($urlCampus.'/', $description, $active, true); |
||
1603 | } |
||
1604 | |||
1605 | return [ |
||
1606 | 'status' => true, |
||
1607 | 'id_campus' => $idCampus, |
||
1608 | ]; |
||
1609 | } |
||
1610 | |||
1611 | return [ |
||
1612 | 'status' => false, |
||
1613 | 'id_campus' => 0, |
||
1614 | ]; |
||
1615 | } |
||
1616 | |||
1617 | /** |
||
1618 | * Edit Campus Virtual. |
||
1619 | * |
||
1620 | * @param array Params Campus |
||
1621 | * |
||
1622 | * @return array |
||
1623 | */ |
||
1624 | public function editCampusURL($params) |
||
1625 | { |
||
1626 | $urlCampus = Security::remove_XSS($params['url']); |
||
1627 | $description = Security::remove_XSS($params['description']); |
||
1628 | |||
1629 | $active = isset($params['active']) ? intval($params['active']) : 0; |
||
1630 | $url_id = isset($params['id']) ? intval($params['id']) : 0; |
||
1631 | |||
1632 | if (!empty($url_id)) { |
||
1633 | //we can't change the status of the url with id=1 |
||
1634 | if ($url_id == 1) { |
||
1635 | $active = 1; |
||
1636 | } |
||
1637 | //checking url |
||
1638 | if (substr($urlCampus, strlen($urlCampus) - 1, strlen($urlCampus)) == '/') { |
||
1639 | UrlManager::update($url_id, $urlCampus, $description, $active); |
||
1640 | } else { |
||
1641 | UrlManager::update($url_id, $urlCampus.'/', $description, $active); |
||
1642 | } |
||
1643 | |||
1644 | return [true]; |
||
1645 | } |
||
1646 | |||
1647 | return [false]; |
||
1648 | } |
||
1649 | |||
1650 | /** |
||
1651 | * Delete Campus Virtual. |
||
1652 | * |
||
1653 | * @param array Params Campus |
||
1654 | * |
||
1655 | * @return array |
||
1656 | */ |
||
1657 | public function deleteCampusURL($params) |
||
1658 | { |
||
1659 | $url_id = isset($params['id']) ? intval($params['id']) : 0; |
||
1660 | |||
1661 | $result = UrlManager::delete($url_id); |
||
1662 | if ($result) { |
||
1663 | return [ |
||
1664 | 'status' => true, |
||
1665 | 'message' => get_lang('URLDeleted'), |
||
1666 | ]; |
||
1667 | } else { |
||
1668 | return [ |
||
1669 | 'status' => false, |
||
1670 | 'message' => get_lang('Error'), |
||
1671 | ]; |
||
1672 | } |
||
1673 | } |
||
1674 | |||
1675 | /** |
||
1676 | * @throws Exception |
||
1677 | * |
||
1678 | * @return array |
||
1679 | */ |
||
1680 | public function addCoursesSession(array $params) |
||
1705 | ]; |
||
1706 | } |
||
1707 | |||
1708 | /** |
||
1709 | * @return array |
||
1710 | */ |
||
1711 | public function addUsersSession(array $params) |
||
1712 | { |
||
1713 | $sessionId = $params['id_session']; |
||
1714 | $userList = $params['list_users']; |
||
1715 | |||
1716 | if (!is_array($userList)) { |
||
1717 | $userList = []; |
||
1718 | } |
||
1719 | |||
1720 | SessionManager::subscribeUsersToSession( |
||
1721 | $sessionId, |
||
1722 | $userList, |
||
1723 | null, |
||
1724 | false |
||
1725 | ); |
||
1726 | |||
1727 | return [ |
||
1728 | 'status' => true, |
||
1729 | 'message' => get_lang('UsersAdded'), |
||
1730 | ]; |
||
1731 | } |
||
1732 | |||
1733 | /** |
||
1734 | * Creates a session from a model session. |
||
1735 | * |
||
1736 | * @param $modelSessionId |
||
1737 | * @param $sessionName |
||
1738 | * @param $startDate |
||
1739 | * @param $endDate |
||
1740 | * |
||
1741 | * @throws Exception |
||
1742 | * |
||
1743 | * @return int, the id of the new session |
||
1744 | */ |
||
1745 | public function createSessionFromModel($modelSessionId, $sessionName, $startDate, $endDate, array $extraFields = []) |
||
1746 | { |
||
1747 | if (empty($modelSessionId) || empty($sessionName) || empty($startDate) || empty($endDate)) { |
||
1748 | throw new Exception(get_lang('NoData')); |
||
1749 | } |
||
1750 | |||
1751 | if (!SessionManager::isValidId($modelSessionId)) { |
||
1752 | throw new Exception(get_lang('ModelSessionDoesNotExist')); |
||
1753 | } |
||
1754 | |||
1755 | $modelSession = SessionManager::fetch($modelSessionId); |
||
1756 | |||
1757 | $modelSession['accessUrlId'] = 1; |
||
1758 | if (api_is_multiple_url_enabled()) { |
||
1759 | if (api_get_current_access_url_id() != -1) { |
||
1760 | $modelSession['accessUrlId'] = api_get_current_access_url_id(); |
||
1761 | } |
||
1762 | } |
||
1763 | |||
1764 | $newSessionId = SessionManager::create_session( |
||
1765 | $sessionName, |
||
1766 | $startDate, |
||
1767 | $endDate, |
||
1768 | $startDate, |
||
1769 | $endDate, |
||
1770 | $startDate, |
||
1771 | $endDate, |
||
1772 | $modelSession['id_coach'], |
||
1773 | $modelSession['session_category_id'], |
||
1774 | $modelSession['visibility'], |
||
1775 | false, |
||
1776 | $modelSession['duration'], |
||
1777 | $modelSession['description'], |
||
1778 | $modelSession['show_description'], |
||
1779 | $extraFields, |
||
1780 | $modelSession['session_admin_id'], |
||
1781 | $modelSession['send_subscription_notification'], |
||
1782 | $modelSession['accessUrlId'] |
||
1783 | ); |
||
1784 | |||
1785 | if (empty($newSessionId)) { |
||
1786 | throw new Exception(get_lang('SessionNotRegistered')); |
||
1787 | } |
||
1788 | |||
1789 | if (is_string($newSessionId)) { |
||
1790 | throw new Exception($newSessionId); |
||
1791 | } |
||
1792 | |||
1793 | $promotionId = $modelSession['promotion_id']; |
||
1794 | if ($promotionId) { |
||
1795 | $sessionList = array_keys(SessionManager::get_all_sessions_by_promotion($promotionId)); |
||
1796 | $sessionList[] = $newSessionId; |
||
1797 | SessionManager::subscribe_sessions_to_promotion($modelSession['promotion_id'], $sessionList); |
||
1798 | } |
||
1799 | |||
1800 | $modelExtraFields = []; |
||
1801 | $fields = SessionManager::getFilteredExtraFields($modelSessionId); |
||
1802 | if (is_array($fields) and !empty($fields)) { |
||
1803 | foreach ($fields as $field) { |
||
1804 | $modelExtraFields[$field['variable']] = $field['value']; |
||
1805 | } |
||
1806 | } |
||
1807 | $allExtraFields = array_merge($modelExtraFields, $extraFields); |
||
1808 | foreach ($allExtraFields as $name => $value) { |
||
1809 | // SessionManager::update_session_extra_field_value returns false when no row is changed, |
||
1810 | // which can happen since extra field values are initialized by SessionManager::create_session |
||
1811 | // therefore we do not throw an exception when false is returned |
||
1812 | SessionManager::update_session_extra_field_value($newSessionId, $name, $value); |
||
1813 | } |
||
1814 | |||
1815 | $courseList = array_keys(SessionManager::get_course_list_by_session_id($modelSessionId)); |
||
1816 | if (is_array($courseList) |
||
1817 | && !empty($courseList) |
||
1818 | && !SessionManager::add_courses_to_session($newSessionId, $courseList)) { |
||
1819 | throw new Exception(get_lang('CoursesNotAddedToSession')); |
||
1820 | } |
||
1821 | |||
1822 | if (api_is_multiple_url_enabled()) { |
||
1823 | if (api_get_current_access_url_id() != -1) { |
||
1824 | UrlManager::add_session_to_url( |
||
1825 | $newSessionId, |
||
1826 | api_get_current_access_url_id() |
||
1827 | ); |
||
1828 | } else { |
||
1829 | UrlManager::add_session_to_url($newSessionId, 1); |
||
1830 | } |
||
1831 | } else { |
||
1832 | UrlManager::add_session_to_url($newSessionId, 1); |
||
1833 | } |
||
1834 | |||
1835 | return $newSessionId; |
||
1836 | } |
||
1837 | |||
1838 | /** |
||
1839 | * subscribes a user to a session. |
||
1840 | * |
||
1841 | * @param int $sessionId the session id |
||
1842 | * @param string $loginName the user's login name |
||
1843 | * |
||
1844 | * @throws Exception |
||
1845 | * |
||
1846 | * @return boolean, whether it worked |
||
1847 | */ |
||
1848 | public function subscribeUserToSessionFromUsername($sessionId, $loginName) |
||
1849 | { |
||
1850 | if (!SessionManager::isValidId($sessionId)) { |
||
1851 | throw new Exception(get_lang('SessionNotFound')); |
||
1852 | } |
||
1853 | |||
1854 | $userId = UserManager::get_user_id_from_username($loginName); |
||
1855 | if (false === $userId) { |
||
1856 | throw new Exception(get_lang('UserNotFound')); |
||
1857 | } |
||
1858 | |||
1859 | $subscribed = SessionManager::subscribeUsersToSession( |
||
1860 | $sessionId, |
||
1861 | [$userId], |
||
1862 | SESSION_VISIBLE_READ_ONLY, |
||
1863 | false |
||
1864 | ); |
||
1865 | if (!$subscribed) { |
||
1866 | throw new Exception(get_lang('UserNotSubscribed')); |
||
1867 | } |
||
1868 | |||
1869 | return true; |
||
1870 | } |
||
1871 | |||
1872 | /** |
||
1873 | * finds the session which has a specific value in a specific extra field. |
||
1874 | * |
||
1875 | * @param $fieldName |
||
1876 | * @param $fieldValue |
||
1877 | * |
||
1878 | * @throws Exception when no session matched or more than one session matched |
||
1879 | * |
||
1880 | * @return int, the matching session id |
||
1881 | */ |
||
1882 | public function getSessionFromExtraField($fieldName, $fieldValue) |
||
1883 | { |
||
1884 | // find sessions that that have value in field |
||
1885 | $valueModel = new ExtraFieldValue('session'); |
||
1886 | $sessionIdList = $valueModel->get_item_id_from_field_variable_and_field_value( |
||
1887 | $fieldName, |
||
1888 | $fieldValue, |
||
1889 | false, |
||
1890 | false, |
||
1891 | true |
||
1892 | ); |
||
1893 | |||
1894 | // throw if none found |
||
1895 | if (empty($sessionIdList)) { |
||
1896 | throw new Exception(get_lang('NoSessionMatched')); |
||
1897 | } |
||
1898 | |||
1899 | // throw if more than one found |
||
1900 | if (count($sessionIdList) > 1) { |
||
1901 | throw new Exception(get_lang('MoreThanOneSessionMatched')); |
||
1902 | } |
||
1903 | |||
1904 | // return sessionId |
||
1905 | return intval($sessionIdList[0]['item_id']); |
||
1906 | } |
||
1907 | |||
1908 | /** |
||
1909 | * updates a user identified by its login name. |
||
1910 | * |
||
1911 | * @param array $parameters |
||
1912 | * |
||
1913 | * @throws Exception on failure |
||
1914 | * |
||
1915 | * @return boolean, true on success |
||
1916 | */ |
||
1917 | public function updateUserFromUserName($parameters) |
||
1918 | { |
||
1919 | // find user |
||
1920 | $userId = null; |
||
1921 | if (!is_array($parameters) || empty($parameters)) { |
||
1922 | throw new Exception('NoData'); |
||
1923 | } |
||
1924 | foreach ($parameters as $name => $value) { |
||
1925 | if (strtolower($name) === 'loginname') { |
||
1926 | $userId = UserManager::get_user_id_from_username($value); |
||
1927 | if (false === $userId) { |
||
1928 | throw new Exception(get_lang('UserNotFound')); |
||
1929 | } |
||
1930 | break; |
||
1931 | } |
||
1932 | } |
||
1933 | if (is_null($userId)) { |
||
1934 | throw new Exception(get_lang('NoData')); |
||
1935 | } |
||
1936 | /** @var User $user */ |
||
1937 | $user = UserManager::getRepository()->find($userId); |
||
1938 | if (empty($user)) { |
||
1939 | throw new Exception(get_lang('CouldNotLoadUser')); |
||
1940 | } |
||
1941 | |||
1942 | // tell the world we are about to update a user |
||
1943 | $hook = HookUpdateUser::create(); |
||
1944 | if (!empty($hook)) { |
||
1945 | $hook->notifyUpdateUser(HOOK_EVENT_TYPE_PRE); |
||
1946 | } |
||
1947 | |||
1948 | // apply submitted modifications |
||
1949 | foreach ($parameters as $name => $value) { |
||
1950 | switch (strtolower($name)) { |
||
1951 | case 'email': |
||
1952 | $user->setEmail($value); |
||
1953 | break; |
||
1954 | case 'enabled': |
||
1955 | $user->setEnabled($value); |
||
1956 | break; |
||
1957 | case 'lastname': |
||
1958 | $user->setLastname($value); |
||
1959 | break; |
||
1960 | case 'firstname': |
||
1961 | $user->setFirstname($value); |
||
1962 | break; |
||
1963 | case 'phone': |
||
1964 | $user->setPhone($value); |
||
1965 | break; |
||
1966 | case 'address': |
||
1967 | $user->setAddress($value); |
||
1968 | break; |
||
1969 | case 'roles': |
||
1970 | $user->setRoles($value); |
||
1971 | break; |
||
1972 | case 'profile_completed': |
||
1973 | $user->setProfileCompleted($value); |
||
1974 | break; |
||
1975 | case 'auth_source': |
||
1976 | $user->setAuthSource($value); |
||
1977 | break; |
||
1978 | case 'status': |
||
1979 | $user->setStatus($value); |
||
1980 | break; |
||
1981 | case 'official_code': |
||
1982 | $user->setOfficialCode($value); |
||
1983 | break; |
||
1984 | case 'picture_uri': |
||
1985 | $user->setPictureUri($value); |
||
1986 | break; |
||
1987 | case 'creator_id': |
||
1988 | $user->setCreatorId($value); |
||
1989 | break; |
||
1990 | case 'competences': |
||
1991 | $user->setCompetences($value); |
||
1992 | break; |
||
1993 | case 'diplomas': |
||
1994 | $user->setDiplomas($value); |
||
1995 | break; |
||
1996 | case 'openarea': |
||
1997 | $user->setOpenArea($value); |
||
1998 | break; |
||
1999 | case 'teach': |
||
2000 | $user->setTeach($value); |
||
2001 | break; |
||
2002 | case 'productions': |
||
2003 | $user->setProductions($value); |
||
2004 | break; |
||
2005 | case 'language': |
||
2006 | $languages = api_get_languages(); |
||
2007 | if (!in_array($value, $languages['folder'])) { |
||
2008 | throw new Exception(get_lang('LanguageUnavailable')); |
||
2009 | } |
||
2010 | $user->setLanguage($value); |
||
2011 | break; |
||
2012 | case 'registration_date': |
||
2013 | $user->setRegistrationDate($value); |
||
2014 | break; |
||
2015 | case 'expiration_date': |
||
2016 | $user->setExpirationDate( |
||
2017 | new DateTime( |
||
2018 | api_get_utc_datetime($value), |
||
2019 | new DateTimeZone('UTC') |
||
2020 | ) |
||
2021 | ); |
||
2022 | break; |
||
2023 | case 'active': |
||
2024 | // see UserManager::update_user() usermanager.lib.php:1205 |
||
2025 | if ($user->getActive() != $value) { |
||
2026 | $user->setActive($value); |
||
2027 | Event::addEvent($value ? LOG_USER_ENABLE : LOG_USER_DISABLE, LOG_USER_ID, $userId); |
||
2028 | } |
||
2029 | break; |
||
2030 | case 'openid': |
||
2031 | $user->setOpenId($value); |
||
2032 | break; |
||
2033 | case 'theme': |
||
2034 | $user->setTheme($value); |
||
2035 | break; |
||
2036 | case 'hr_dept_id': |
||
2037 | $user->setHrDeptId($value); |
||
2038 | break; |
||
2039 | case 'extra': |
||
2040 | if (is_array($value)) { |
||
2041 | if (count($value) > 0) { |
||
2042 | if (is_array($value[0])) { |
||
2043 | foreach ($value as $field) { |
||
2044 | $fieldName = $field['field_name']; |
||
2045 | $fieldValue = $field['field_value']; |
||
2046 | if (!isset($fieldName) || !isset($fieldValue) || |
||
2047 | !UserManager::update_extra_field_value($userId, $fieldName, $fieldValue)) { |
||
2048 | throw new Exception(get_lang('CouldNotUpdateExtraFieldValue').': '.print_r($field, true)); |
||
2049 | } |
||
2050 | } |
||
2051 | } else { |
||
2052 | foreach ($value as $fieldName => $fieldValue) { |
||
2053 | if (!UserManager::update_extra_field_value($userId, $fieldName, $fieldValue)) { |
||
2054 | throw new Exception(get_lang('CouldNotUpdateExtraFieldValue').': '.$fieldName); |
||
2055 | } |
||
2056 | } |
||
2057 | } |
||
2058 | } |
||
2059 | } |
||
2060 | break; |
||
2061 | case 'username': |
||
2062 | case 'api_key': |
||
2063 | case 'action': |
||
2064 | case 'loginname': |
||
2065 | break; |
||
2066 | case 'email_canonical': |
||
2067 | case 'locked': |
||
2068 | case 'expired': |
||
2069 | case 'credentials_expired': |
||
2070 | case 'credentials_expire_at': |
||
2071 | case 'expires_at': |
||
2072 | case 'salt': |
||
2073 | case 'last_login': |
||
2074 | case 'created_at': |
||
2075 | case 'updated_at': |
||
2076 | case 'confirmation_token': |
||
2077 | case 'password_requested_at': |
||
2078 | case 'password': // see UserManager::update_user usermanager.lib.php:1182 |
||
2079 | case 'username_canonical': |
||
2080 | default: |
||
2081 | throw new Exception(get_lang('UnsupportedUpdate')." '$name'"); |
||
2082 | } |
||
2083 | } |
||
2084 | |||
2085 | // save modifications |
||
2086 | UserManager::getManager()->updateUser($user, true); |
||
2087 | |||
2088 | // tell the world we just updated this user |
||
2089 | if (!empty($hook)) { |
||
2090 | $hook->setEventData(['user' => $user]); |
||
2091 | $hook->notifyUpdateUser(HOOK_EVENT_TYPE_POST); |
||
2092 | } |
||
2093 | |||
2094 | // invalidate cache for this user |
||
2095 | $cacheAvailable = api_get_configuration_value('apc'); |
||
2096 | if ($cacheAvailable === true) { |
||
2097 | $apcVar = api_get_configuration_value('apc_prefix').'userinfo_'.$userId; |
||
2098 | if (apcu_exists($apcVar)) { |
||
2099 | apcu_delete($apcVar); |
||
2100 | } |
||
2101 | } |
||
2102 | |||
2103 | return true; |
||
2104 | } |
||
2105 | |||
2106 | /** |
||
2107 | * Returns whether a user login name exists. |
||
2108 | * |
||
2109 | * @param string $loginname the user login name |
||
2110 | * |
||
2111 | * @return bool whether the user login name exists |
||
2112 | */ |
||
2113 | public function usernameExist($loginname) |
||
2114 | { |
||
2115 | return false !== api_get_user_info_from_username($loginname); |
||
2116 | } |
||
2117 | |||
2118 | /** |
||
2119 | * This service roughly matches what the call to MDL's API core_course_get_contents function returns. |
||
2120 | * |
||
2121 | * @return array |
||
2122 | */ |
||
2123 | public function getCourseQuizMdlCompat() |
||
2124 | { |
||
2125 | $userId = $this->user->getId(); |
||
2126 | $courseId = $this->course->getId(); |
||
2127 | $sessionId = $this->session ? $this->session->getId() : 0; |
||
2128 | |||
2129 | $toolVisibility = CourseHome::getToolVisibility(TOOL_QUIZ, $courseId, $sessionId); |
||
2130 | |||
2131 | $json = [ |
||
2132 | "id" => $this->course->getId(), |
||
2133 | "name" => get_lang('Exercises'), |
||
2134 | "visible" => (int) $toolVisibility, |
||
2135 | "summary" => '', |
||
2136 | "summaryformat" => 1, |
||
2137 | "section" => 1, |
||
2138 | "hiddenbynumsections" => 0, |
||
2139 | "uservisible" => $toolVisibility, |
||
2140 | "modules" => [], |
||
2141 | ]; |
||
2142 | |||
2143 | $quizIcon = Display::return_icon('quiz.png', '', [], ICON_SIZE_SMALL, false, true); |
||
2144 | |||
2145 | $json['modules'] = array_map( |
||
2146 | function (array $exercise) use ($quizIcon) { |
||
2147 | return [ |
||
2148 | 'id' => $exercise['id'], |
||
2149 | 'url' => $exercise['url'], |
||
2150 | 'name' => $exercise['name'], |
||
2151 | 'instance' => 1, |
||
2152 | 'visible' => 1, |
||
2153 | 'uservisible' => true, |
||
2154 | 'visibleoncoursepage' => 0, |
||
2155 | 'modicon' => $quizIcon, |
||
2156 | 'modname' => 'quiz', |
||
2157 | 'modplural' => get_lang('Exercises'), |
||
2158 | 'availability' => null, |
||
2159 | 'indent' => 0, |
||
2160 | 'onclick' => '', |
||
2161 | 'afterlink' => null, |
||
2162 | 'customdata' => "", |
||
2163 | 'noviewlink' => false, |
||
2164 | 'completion' => (int) ($exercise[1] > 0), |
||
2165 | ]; |
||
2166 | }, |
||
2167 | Exercise::exerciseGrid(0, '', $userId, $courseId, $sessionId, true) |
||
2168 | ); |
||
2169 | |||
2170 | return [$json]; |
||
2171 | } |
||
2172 | |||
2173 | /** |
||
2174 | * @throws Exception |
||
2175 | */ |
||
2176 | public function updateSession(array $params): array |
||
2317 | ]; |
||
2318 | } |
||
2319 | |||
2320 | public function checkConditionalLogin(): bool |
||
2321 | { |
||
2322 | $file = api_get_path(SYS_CODE_PATH).'auth/conditional_login/conditional_login.php'; |
||
2323 | |||
2324 | if (!file_exists($file)) { |
||
2325 | return true; |
||
2326 | } |
||
2327 | |||
2328 | include_once $file; |
||
2329 | |||
2330 | if (!isset($login_conditions)) { |
||
2331 | return true; |
||
2332 | } |
||
2333 | |||
2334 | foreach ($login_conditions as $condition) { |
||
2335 | //If condition fails we redirect to the URL defined by the condition |
||
2336 | if (!isset($condition['conditional_function'])) { |
||
2337 | continue; |
||
2338 | } |
||
2339 | |||
2340 | $function = $condition['conditional_function']; |
||
2341 | $result = $function(['user_id' => $this->user->getId()]); |
||
2342 | |||
2343 | if ($result == false) { |
||
2344 | return false; |
||
2345 | } |
||
2346 | } |
||
2347 | |||
2348 | return true; |
||
2349 | } |
||
2350 | |||
2351 | public function getLegalConditions(): array |
||
2376 | } |
||
2377 | |||
2378 | public function updateConditionAccepted() |
||
2394 | ); |
||
2395 | } |
||
2396 | |||
2397 | public function logout() |
||
2398 | { |
||
2399 | online_logout($this->user->getId()); |
||
2400 | |||
2401 | Event::courseLogout( |
||
2402 | [ |
||
2403 | 'uid' => $this->user->getId(), |
||
2404 | 'cid' => $this->course ? $this->course->getId() : 0, |
||
2405 | 'sid' => $this->session ? $this->session->getId() : 0, |
||
2406 | ] |
||
2407 | ); |
||
2408 | } |
||
2409 | |||
2410 | /** |
||
2411 | * @param array $additionalParams Optional |
||
2412 | * |
||
2413 | * @return string |
||
2414 | */ |
||
2415 | private function encodeParams(array $additionalParams = []) |
||
2426 | } |
||
2427 | } |
||
2428 |