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