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