| Total Complexity | 143 |
| Total Lines | 1491 |
| Duplicated Lines | 0 % |
| Changes | 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 |
||
| 15 | */ |
||
| 16 | class Rest extends WebService |
||
| 17 | { |
||
| 18 | const SERVICE_NAME = 'MsgREST'; |
||
| 19 | const EXTRA_FIELD_GCM_REGISTRATION = 'gcm_registration_id'; |
||
| 20 | |||
| 21 | const GET_AUTH = 'authenticate'; |
||
| 22 | const GET_USER_MESSAGES = 'user_messages'; |
||
| 23 | const POST_USER_MESSAGE_READ = 'user_message_read'; |
||
| 24 | const POST_USER_MESSAGE_UNREAD = 'user_message_unread'; |
||
| 25 | const SAVE_GCM_ID = 'gcm_id'; |
||
| 26 | const GET_USER_COURSES = 'user_courses'; |
||
| 27 | const GET_PROFILE = 'user_profile'; |
||
| 28 | const GET_COURSE_INFO = 'course_info'; |
||
| 29 | const GET_COURSE_DESCRIPTIONS = 'course_descriptions'; |
||
| 30 | const GET_COURSE_DOCUMENTS = 'course_documents'; |
||
| 31 | const GET_COURSE_ANNOUNCEMENTS = 'course_announcements'; |
||
| 32 | const GET_COURSE_ANNOUNCEMENT = 'course_announcement'; |
||
| 33 | const GET_COURSE_AGENDA = 'course_agenda'; |
||
| 34 | const GET_COURSE_NOTEBOOKS = 'course_notebooks'; |
||
| 35 | const GET_COURSE_FORUM_CATEGORIES = 'course_forumcategories'; |
||
| 36 | const GET_COURSE_FORUM = 'course_forum'; |
||
| 37 | const GET_COURSE_FORUM_THREAD = 'course_forumthread'; |
||
| 38 | const GET_COURSE_LEARNPATHS = 'course_learnpaths'; |
||
| 39 | const GET_COURSE_LEARNPATH = 'course_learnpath'; |
||
| 40 | const SAVE_FORUM_POST = 'save_forum_post'; |
||
| 41 | const GET_USER_SESSIONS = 'user_sessions'; |
||
| 42 | const SAVE_USER_MESSAGE = 'save_user_message'; |
||
| 43 | const GET_MESSAGE_USERS = 'message_users'; |
||
| 44 | const SAVE_COURSE_NOTEBOOK = 'save_course_notebook'; |
||
| 45 | const SAVE_FORUM_THREAD = 'save_forum_thread'; |
||
| 46 | const SAVE_COURSE = 'save_course'; |
||
| 47 | const SAVE_USER = 'save_user'; |
||
| 48 | const SUBSCRIBE_USER_TO_COURSE = 'subscribe_user_to_course'; |
||
| 49 | const EXTRAFIELD_GCM_ID = 'gcm_registration_id'; |
||
| 50 | const CREATE_CAMPUS = 'add_campus'; |
||
| 51 | const EDIT_CAMPUS = 'edit_campus'; |
||
| 52 | const DELETE_CAMPUS = 'delete_campus'; |
||
| 53 | const SAVE_SESSION = 'save_session'; |
||
| 54 | const GET_USERS = 'get_users'; |
||
| 55 | const GET_COURSE = 'get_courses'; |
||
| 56 | const ADD_COURSES_SESSION = 'add_courses_session'; |
||
| 57 | const ADD_USER_SESSION = 'add_users_session'; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * @var Session |
||
| 61 | */ |
||
| 62 | private $session; |
||
| 63 | /** |
||
| 64 | * @var Course |
||
| 65 | */ |
||
| 66 | private $course; |
||
| 67 | |||
| 68 | /** |
||
| 69 | * Rest constructor. |
||
| 70 | * |
||
| 71 | * @param string $username |
||
| 72 | * @param string $apiKey |
||
| 73 | */ |
||
| 74 | public function __construct($username, $apiKey) |
||
| 75 | { |
||
| 76 | parent::__construct($username, $apiKey); |
||
| 77 | } |
||
| 78 | |||
| 79 | /** |
||
| 80 | * Set the current course. |
||
| 81 | * |
||
| 82 | * @param int $id |
||
| 83 | * |
||
| 84 | * @throws Exception |
||
| 85 | */ |
||
| 86 | public function setCourse($id) |
||
| 87 | { |
||
| 88 | if (!$id) { |
||
| 89 | $this->course = null; |
||
| 90 | |||
| 91 | return; |
||
| 92 | } |
||
| 93 | |||
| 94 | $em = Database::getManager(); |
||
| 95 | /** @var Course $course */ |
||
| 96 | $course = $em->find('ChamiloCoreBundle:Course', $id); |
||
| 97 | |||
| 98 | if (!$course) { |
||
| 99 | throw new Exception(get_lang('This course could not be found')); |
||
| 100 | } |
||
| 101 | |||
| 102 | $this->course = $course; |
||
| 103 | } |
||
| 104 | |||
| 105 | /** Set the current session |
||
| 106 | * @param int $id |
||
| 107 | * |
||
| 108 | * @throws Exception |
||
| 109 | */ |
||
| 110 | public function setSession($id) |
||
| 111 | { |
||
| 112 | if (!$id) { |
||
| 113 | $this->session = null; |
||
| 114 | |||
| 115 | return; |
||
| 116 | } |
||
| 117 | |||
| 118 | $em = Database::getManager(); |
||
| 119 | /** @var Session $session */ |
||
| 120 | $session = $em->find('ChamiloCoreBundle:Session', $id); |
||
| 121 | |||
| 122 | if (!$session) { |
||
| 123 | throw new Exception(get_lang('The session could not be found')); |
||
| 124 | } |
||
| 125 | |||
| 126 | $this->session = $session; |
||
| 127 | } |
||
| 128 | |||
| 129 | /** |
||
| 130 | * @param string $username |
||
| 131 | * @param string $apiKeyToValidate |
||
| 132 | * |
||
| 133 | * @throws Exception |
||
| 134 | * |
||
| 135 | * @return Rest |
||
| 136 | */ |
||
| 137 | public static function validate($username, $apiKeyToValidate) |
||
| 138 | { |
||
| 139 | $apiKey = self::findUserApiKey($username, self::SERVICE_NAME); |
||
| 140 | |||
| 141 | if ($apiKey != $apiKeyToValidate) { |
||
| 142 | throw new Exception(get_lang('Invalid API key')); |
||
| 143 | } |
||
| 144 | |||
| 145 | return new self($username, $apiKey); |
||
| 146 | } |
||
| 147 | |||
| 148 | /** |
||
| 149 | * Create the gcm_registration_id extra field for users. |
||
| 150 | */ |
||
| 151 | public static function init() |
||
| 152 | { |
||
| 153 | $extraField = new ExtraField('user'); |
||
| 154 | $fieldInfo = $extraField->get_handler_field_info_by_field_variable(self::EXTRA_FIELD_GCM_REGISTRATION); |
||
| 155 | |||
| 156 | if (empty($fieldInfo)) { |
||
| 157 | $extraField->save([ |
||
| 158 | 'variable' => self::EXTRA_FIELD_GCM_REGISTRATION, |
||
| 159 | 'field_type' => ExtraField::FIELD_TYPE_TEXT, |
||
| 160 | 'display_text' => self::EXTRA_FIELD_GCM_REGISTRATION, |
||
| 161 | ]); |
||
| 162 | } |
||
| 163 | } |
||
| 164 | |||
| 165 | /** |
||
| 166 | * @param string $registrationId |
||
| 167 | * |
||
| 168 | * @return bool |
||
| 169 | */ |
||
| 170 | public function setGcmId($registrationId) |
||
| 171 | { |
||
| 172 | $registrationId = Security::remove_XSS($registrationId); |
||
| 173 | $extraFieldValue = new ExtraFieldValue('user'); |
||
| 174 | |||
| 175 | return $extraFieldValue->save([ |
||
| 176 | 'variable' => self::EXTRA_FIELD_GCM_REGISTRATION, |
||
| 177 | 'value' => $registrationId, |
||
| 178 | 'item_id' => $this->user->getId(), |
||
| 179 | ]); |
||
| 180 | } |
||
| 181 | |||
| 182 | /** |
||
| 183 | * @param int $lastMessageId |
||
| 184 | * |
||
| 185 | * @return array |
||
| 186 | */ |
||
| 187 | public function getUserMessages($lastMessageId = 0) |
||
| 188 | { |
||
| 189 | $lastMessages = MessageManager::getMessagesFromLastReceivedMessage($this->user->getId(), $lastMessageId); |
||
| 190 | $messages = []; |
||
| 191 | |||
| 192 | foreach ($lastMessages as $message) { |
||
| 193 | $hasAttachments = MessageManager::hasAttachments($message['id']); |
||
| 194 | |||
| 195 | $messages[] = [ |
||
| 196 | 'id' => $message['id'], |
||
| 197 | 'title' => $message['title'], |
||
| 198 | 'sender' => [ |
||
| 199 | 'id' => $message['user_id'], |
||
| 200 | 'lastname' => $message['lastname'], |
||
| 201 | 'firstname' => $message['firstname'], |
||
| 202 | 'completeName' => api_get_person_name($message['firstname'], $message['lastname']), |
||
| 203 | ], |
||
| 204 | 'sendDate' => $message['send_date'], |
||
| 205 | 'content' => $message['content'], |
||
| 206 | 'hasAttachments' => $hasAttachments, |
||
| 207 | 'url' => api_get_path(WEB_CODE_PATH).'messages/view_message.php?' |
||
| 208 | .http_build_query(['type' => 1, 'id' => $message['id']]), |
||
| 209 | ]; |
||
| 210 | } |
||
| 211 | |||
| 212 | return $messages; |
||
| 213 | } |
||
| 214 | |||
| 215 | /** |
||
| 216 | * Get the user courses. |
||
| 217 | * |
||
| 218 | * @throws \Doctrine\ORM\OptimisticLockException |
||
| 219 | * @throws \Doctrine\ORM\TransactionRequiredException |
||
| 220 | * @throws \Doctrine\ORM\ORMException |
||
| 221 | * |
||
| 222 | * @return array |
||
| 223 | */ |
||
| 224 | public function getUserCourses() |
||
| 225 | { |
||
| 226 | $courses = CourseManager::get_courses_list_by_user_id($this->user->getId()); |
||
| 227 | $data = []; |
||
| 228 | |||
| 229 | foreach ($courses as $courseInfo) { |
||
| 230 | /** @var Course $course */ |
||
| 231 | $course = Database::getManager()->find('ChamiloCoreBundle:Course', $courseInfo['real_id']); |
||
| 232 | $teachers = CourseManager::getTeacherListFromCourseCodeToString($course->getCode()); |
||
| 233 | $picturePath = Container::getIllustrationRepository()->getIllustrationUrl($course); |
||
| 234 | |||
| 235 | $data[] = [ |
||
| 236 | 'id' => $course->getId(), |
||
| 237 | 'title' => $course->getTitle(), |
||
| 238 | 'code' => $course->getCode(), |
||
| 239 | 'directory' => $course->getDirectory(), |
||
| 240 | 'urlPicture' => $picturePath, |
||
| 241 | 'teachers' => $teachers, |
||
| 242 | 'isSpecial' => !empty($courseInfo['special_course']), |
||
| 243 | ]; |
||
| 244 | } |
||
| 245 | |||
| 246 | return $data; |
||
| 247 | } |
||
| 248 | |||
| 249 | /** |
||
| 250 | * @throws Exception |
||
| 251 | * |
||
| 252 | * @return array |
||
| 253 | */ |
||
| 254 | public function getCourseInfo() |
||
| 255 | { |
||
| 256 | $teachers = CourseManager::getTeacherListFromCourseCodeToString($this->course->getCode()); |
||
| 257 | $tools = CourseHome::get_tools_category( |
||
| 258 | TOOL_STUDENT_VIEW, |
||
| 259 | $this->course->getId(), |
||
| 260 | $this->session ? $this->session->getId() : 0 |
||
| 261 | ); |
||
| 262 | |||
| 263 | return [ |
||
| 264 | 'id' => $this->course->getId(), |
||
| 265 | 'title' => $this->course->getTitle(), |
||
| 266 | 'code' => $this->course->getCode(), |
||
| 267 | 'directory' => $this->course->getDirectory(), |
||
| 268 | 'urlPicture' => Container::getIllustrationRepository()->getIllustrationUrl($this->course); |
||
|
|
|||
| 269 | 'teachers' => $teachers, |
||
| 270 | 'tools' => array_map( |
||
| 271 | function ($tool) { |
||
| 272 | return ['type' => $tool['name']]; |
||
| 273 | }, |
||
| 274 | $tools |
||
| 275 | ), |
||
| 276 | ]; |
||
| 277 | } |
||
| 278 | |||
| 279 | /** |
||
| 280 | * Get the course descriptions. |
||
| 281 | * |
||
| 282 | * @throws Exception |
||
| 283 | * |
||
| 284 | * @return array |
||
| 285 | */ |
||
| 286 | public function getCourseDescriptions() |
||
| 287 | { |
||
| 288 | $descriptions = CourseDescription::get_descriptions($this->course->getId()); |
||
| 289 | $results = []; |
||
| 290 | |||
| 291 | /** @var CourseDescription $description */ |
||
| 292 | foreach ($descriptions as $description) { |
||
| 293 | $results[] = [ |
||
| 294 | 'id' => $description->get_description_type(), |
||
| 295 | 'title' => $description->get_title(), |
||
| 296 | 'content' => str_replace('src="/', 'src="'.api_get_path(WEB_PATH), $description->get_content()), |
||
| 297 | ]; |
||
| 298 | } |
||
| 299 | |||
| 300 | return $results; |
||
| 301 | } |
||
| 302 | |||
| 303 | /** |
||
| 304 | * @param int $directoryId |
||
| 305 | * |
||
| 306 | * @throws Exception |
||
| 307 | * |
||
| 308 | * @return array |
||
| 309 | */ |
||
| 310 | public function getCourseDocuments($directoryId = 0) |
||
| 311 | { |
||
| 312 | /** @var string $path */ |
||
| 313 | $path = '/'; |
||
| 314 | $sessionId = $this->session ? $this->session->getId() : 0; |
||
| 315 | |||
| 316 | if ($directoryId) { |
||
| 317 | $directory = DocumentManager::get_document_data_by_id( |
||
| 318 | $directoryId, |
||
| 319 | $this->course->getCode(), |
||
| 320 | false, |
||
| 321 | $sessionId |
||
| 322 | ); |
||
| 323 | |||
| 324 | if (!$directory) { |
||
| 325 | throw new Exception('NoDataAvailable'); |
||
| 326 | } |
||
| 327 | |||
| 328 | $path = $directory['path']; |
||
| 329 | } |
||
| 330 | |||
| 331 | $courseInfo = api_get_course_info_by_id($this->course->getId()); |
||
| 332 | $documents = DocumentManager::getAllDocumentData( |
||
| 333 | $courseInfo, |
||
| 334 | $path, |
||
| 335 | 0, |
||
| 336 | null, |
||
| 337 | false, |
||
| 338 | false, |
||
| 339 | $sessionId |
||
| 340 | ); |
||
| 341 | $results = []; |
||
| 342 | |||
| 343 | if (!empty($documents)) { |
||
| 344 | $webPath = api_get_path(WEB_CODE_PATH).'document/document.php?'; |
||
| 345 | |||
| 346 | /** @var array $document */ |
||
| 347 | foreach ($documents as $document) { |
||
| 348 | if ($document['visibility'] != '1') { |
||
| 349 | continue; |
||
| 350 | } |
||
| 351 | |||
| 352 | $icon = $document['filetype'] == 'file' |
||
| 353 | ? choose_image($document['path']) |
||
| 354 | : chooseFolderIcon($document['path']); |
||
| 355 | |||
| 356 | $results[] = [ |
||
| 357 | 'id' => $document['id'], |
||
| 358 | 'type' => $document['filetype'], |
||
| 359 | 'title' => $document['title'], |
||
| 360 | 'path' => $document['path'], |
||
| 361 | 'url' => $webPath.http_build_query([ |
||
| 362 | 'username' => $this->user->getUsername(), |
||
| 363 | 'api_key' => $this->apiKey, |
||
| 364 | 'cidReq' => $this->course->getCode(), |
||
| 365 | 'id_session' => $sessionId, |
||
| 366 | 'gidReq' => 0, |
||
| 367 | 'gradebook' => 0, |
||
| 368 | 'origin' => '', |
||
| 369 | 'action' => 'download', |
||
| 370 | 'id' => $document['id'], |
||
| 371 | ]), |
||
| 372 | 'icon' => $icon, |
||
| 373 | 'size' => format_file_size($document['size']), |
||
| 374 | ]; |
||
| 375 | } |
||
| 376 | } |
||
| 377 | |||
| 378 | return $results; |
||
| 379 | } |
||
| 380 | |||
| 381 | /** |
||
| 382 | * @throws Exception |
||
| 383 | * |
||
| 384 | * @return array |
||
| 385 | */ |
||
| 386 | public function getCourseAnnouncements() |
||
| 387 | { |
||
| 388 | $sessionId = $this->session ? $this->session->getId() : 0; |
||
| 389 | |||
| 390 | $announcements = AnnouncementManager::getAnnouncements( |
||
| 391 | null, |
||
| 392 | null, |
||
| 393 | false, |
||
| 394 | null, |
||
| 395 | null, |
||
| 396 | null, |
||
| 397 | null, |
||
| 398 | null, |
||
| 399 | 0, |
||
| 400 | $this->user->getId(), |
||
| 401 | $this->course->getId(), |
||
| 402 | $sessionId |
||
| 403 | ); |
||
| 404 | |||
| 405 | $announcements = array_map( |
||
| 406 | function ($announcement) { |
||
| 407 | return [ |
||
| 408 | 'id' => (int) $announcement['id'], |
||
| 409 | 'title' => strip_tags($announcement['title']), |
||
| 410 | 'creatorName' => strip_tags($announcement['username']), |
||
| 411 | 'date' => strip_tags($announcement['insert_date']), |
||
| 412 | ]; |
||
| 413 | }, |
||
| 414 | $announcements |
||
| 415 | ); |
||
| 416 | |||
| 417 | return $announcements; |
||
| 418 | } |
||
| 419 | |||
| 420 | /** |
||
| 421 | * @param int $announcementId |
||
| 422 | * |
||
| 423 | * @throws Exception |
||
| 424 | * |
||
| 425 | * @return array |
||
| 426 | */ |
||
| 427 | public function getCourseAnnouncement($announcementId) |
||
| 428 | { |
||
| 429 | $sessionId = $this->session ? $this->session->getId() : 0; |
||
| 430 | $announcement = AnnouncementManager::getAnnouncementInfoById( |
||
| 431 | $announcementId, |
||
| 432 | $this->course->getId(), |
||
| 433 | $this->user->getId() |
||
| 434 | ); |
||
| 435 | |||
| 436 | if (!$announcement) { |
||
| 437 | throw new Exception(get_lang('No announcement')); |
||
| 438 | } |
||
| 439 | |||
| 440 | return [ |
||
| 441 | 'id' => $announcement['announcement']->getIid(), |
||
| 442 | 'title' => $announcement['announcement']->getTitle(), |
||
| 443 | 'creatorName' => UserManager::formatUserFullName($announcement['item_property']->getInsertUser()), |
||
| 444 | 'date' => api_convert_and_format_date( |
||
| 445 | $announcement['item_property']->getInsertDate(), |
||
| 446 | DATE_TIME_FORMAT_LONG_24H |
||
| 447 | ), |
||
| 448 | 'content' => AnnouncementManager::parseContent( |
||
| 449 | $this->user->getId(), |
||
| 450 | $announcement['announcement']->getContent(), |
||
| 451 | $this->course->getCode(), |
||
| 452 | $sessionId |
||
| 453 | ), |
||
| 454 | ]; |
||
| 455 | } |
||
| 456 | |||
| 457 | /** |
||
| 458 | * @throws Exception |
||
| 459 | * |
||
| 460 | * @return array |
||
| 461 | */ |
||
| 462 | public function getCourseAgenda() |
||
| 463 | { |
||
| 464 | $sessionId = $this->session ? $this->session->getId() : 0; |
||
| 465 | |||
| 466 | $agenda = new Agenda( |
||
| 467 | 'course', |
||
| 468 | $this->user->getId(), |
||
| 469 | $this->course->getId(), |
||
| 470 | $sessionId |
||
| 471 | ); |
||
| 472 | $result = $agenda->parseAgendaFilter(null); |
||
| 473 | |||
| 474 | $start = new DateTime(api_get_utc_datetime(), new DateTimeZone('UTC')); |
||
| 475 | $start->modify('first day of this month'); |
||
| 476 | $start->setTime(0, 0, 0); |
||
| 477 | $end = new DateTime(api_get_utc_datetime(), new DateTimeZone('UTC')); |
||
| 478 | $end->modify('last day of this month'); |
||
| 479 | $end->setTime(23, 59, 59); |
||
| 480 | |||
| 481 | $groupId = current($result['groups']); |
||
| 482 | $userId = current($result['users']); |
||
| 483 | |||
| 484 | $events = $agenda->getEvents( |
||
| 485 | $start->getTimestamp(), |
||
| 486 | $end->getTimestamp(), |
||
| 487 | $this->course->getId(), |
||
| 488 | $groupId, |
||
| 489 | $userId, |
||
| 490 | 'array' |
||
| 491 | ); |
||
| 492 | |||
| 493 | if (!is_array($events)) { |
||
| 494 | return []; |
||
| 495 | } |
||
| 496 | |||
| 497 | $webPath = api_get_path(WEB_PATH); |
||
| 498 | |||
| 499 | return array_map( |
||
| 500 | function ($event) use ($webPath) { |
||
| 501 | return [ |
||
| 502 | 'id' => (int) $event['unique_id'], |
||
| 503 | 'title' => $event['title'], |
||
| 504 | 'content' => str_replace('src="/', 'src="'.$webPath, $event['description']), |
||
| 505 | 'startDate' => $event['start_date_localtime'], |
||
| 506 | 'endDate' => $event['end_date_localtime'], |
||
| 507 | 'isAllDay' => $event['allDay'] ? true : false, |
||
| 508 | ]; |
||
| 509 | }, |
||
| 510 | $events |
||
| 511 | ); |
||
| 512 | } |
||
| 513 | |||
| 514 | /** |
||
| 515 | * @throws Exception |
||
| 516 | * |
||
| 517 | * @return array |
||
| 518 | */ |
||
| 519 | public function getCourseNotebooks() |
||
| 520 | { |
||
| 521 | $em = Database::getManager(); |
||
| 522 | /** @var CNotebookRepository $notebooksRepo */ |
||
| 523 | $notebooksRepo = $em->getRepository('ChamiloCourseBundle:CNotebook'); |
||
| 524 | $notebooks = $notebooksRepo->findByUser($this->user, $this->course, $this->session); |
||
| 525 | |||
| 526 | return array_map( |
||
| 527 | function (CNotebook $notebook) { |
||
| 528 | return [ |
||
| 529 | 'id' => $notebook->getIid(), |
||
| 530 | 'title' => $notebook->getTitle(), |
||
| 531 | 'description' => $notebook->getDescription(), |
||
| 532 | 'creationDate' => api_format_date( |
||
| 533 | $notebook->getCreationDate()->getTimestamp() |
||
| 534 | ), |
||
| 535 | 'updateDate' => api_format_date( |
||
| 536 | $notebook->getUpdateDate()->getTimestamp() |
||
| 537 | ), |
||
| 538 | ]; |
||
| 539 | }, |
||
| 540 | $notebooks |
||
| 541 | ); |
||
| 542 | } |
||
| 543 | |||
| 544 | /** |
||
| 545 | * @throws Exception |
||
| 546 | * |
||
| 547 | * @return array |
||
| 548 | */ |
||
| 549 | public function getCourseForumCategories() |
||
| 550 | { |
||
| 551 | $sessionId = $this->session ? $this->session->getId() : 0; |
||
| 552 | $webCoursePath = api_get_path(WEB_COURSE_PATH).$this->course->getDirectory().'/upload/forum/images/'; |
||
| 553 | |||
| 554 | require_once api_get_path(SYS_CODE_PATH).'forum/forumfunction.inc.php'; |
||
| 555 | |||
| 556 | $categoriesFullData = get_forum_categories('', $this->course->getId(), $sessionId); |
||
| 557 | $categories = []; |
||
| 558 | $includeGroupsForums = api_get_setting('display_groups_forum_in_general_tool') === 'true'; |
||
| 559 | $forumsFullData = get_forums('', $this->course->getCode(), $includeGroupsForums, $sessionId); |
||
| 560 | $forums = []; |
||
| 561 | |||
| 562 | foreach ($forumsFullData as $forumId => $forumInfo) { |
||
| 563 | $forum = [ |
||
| 564 | 'id' => intval($forumInfo['iid']), |
||
| 565 | 'catId' => intval($forumInfo['forum_category']), |
||
| 566 | 'title' => $forumInfo['forum_title'], |
||
| 567 | 'description' => $forumInfo['forum_comment'], |
||
| 568 | 'image' => $forumInfo['forum_image'] ? ($webCoursePath.$forumInfo['forum_image']) : '', |
||
| 569 | 'numberOfThreads' => isset($forumInfo['number_of_threads']) ? intval($forumInfo['number_of_threads']) : 0, |
||
| 570 | 'lastPost' => null, |
||
| 571 | ]; |
||
| 572 | |||
| 573 | $lastPostInfo = get_last_post_information($forumId, false, $this->course->getId(), $sessionId); |
||
| 574 | |||
| 575 | if ($lastPostInfo) { |
||
| 576 | $forum['lastPost'] = [ |
||
| 577 | 'date' => api_convert_and_format_date($lastPostInfo['last_post_date']), |
||
| 578 | 'user' => api_get_person_name( |
||
| 579 | $lastPostInfo['last_poster_firstname'], |
||
| 580 | $lastPostInfo['last_poster_lastname'] |
||
| 581 | ), |
||
| 582 | ]; |
||
| 583 | } |
||
| 584 | |||
| 585 | $forums[] = $forum; |
||
| 586 | } |
||
| 587 | |||
| 588 | foreach ($categoriesFullData as $category) { |
||
| 589 | $categoryForums = array_filter( |
||
| 590 | $forums, |
||
| 591 | function (array $forum) use ($category) { |
||
| 592 | if ($forum['catId'] != $category['cat_id']) { |
||
| 593 | return false; |
||
| 594 | } |
||
| 595 | |||
| 596 | return true; |
||
| 597 | } |
||
| 598 | ); |
||
| 599 | |||
| 600 | $categories[] = [ |
||
| 601 | 'id' => intval($category['iid']), |
||
| 602 | 'title' => $category['cat_title'], |
||
| 603 | 'catId' => intval($category['cat_id']), |
||
| 604 | 'description' => $category['cat_comment'], |
||
| 605 | 'forums' => $categoryForums, |
||
| 606 | 'courseId' => $this->course->getId(), |
||
| 607 | ]; |
||
| 608 | } |
||
| 609 | |||
| 610 | return $categories; |
||
| 611 | } |
||
| 612 | |||
| 613 | /** |
||
| 614 | * @param int $forumId |
||
| 615 | * |
||
| 616 | * @throws Exception |
||
| 617 | * |
||
| 618 | * @return array |
||
| 619 | */ |
||
| 620 | public function getCourseForum($forumId) |
||
| 621 | { |
||
| 622 | require_once api_get_path(SYS_CODE_PATH).'forum/forumfunction.inc.php'; |
||
| 623 | |||
| 624 | $sessionId = $this->session ? $this->session->getId() : 0; |
||
| 625 | $forumInfo = get_forums($forumId, $this->course->getCode(), true, $sessionId); |
||
| 626 | |||
| 627 | if (!isset($forumInfo['iid'])) { |
||
| 628 | throw new Exception(get_lang('No forum')); |
||
| 629 | } |
||
| 630 | |||
| 631 | $webCoursePath = api_get_path(WEB_COURSE_PATH).$this->course->getDirectory().'/upload/forum/images/'; |
||
| 632 | $forum = [ |
||
| 633 | 'id' => $forumInfo['iid'], |
||
| 634 | 'title' => $forumInfo['forum_title'], |
||
| 635 | 'description' => $forumInfo['forum_comment'], |
||
| 636 | 'image' => $forumInfo['forum_image'] ? ($webCoursePath.$forumInfo['forum_image']) : '', |
||
| 637 | 'threads' => [], |
||
| 638 | ]; |
||
| 639 | |||
| 640 | $threads = get_threads($forumInfo['iid'], $this->course->getId(), $sessionId); |
||
| 641 | |||
| 642 | foreach ($threads as $thread) { |
||
| 643 | $forum['threads'][] = [ |
||
| 644 | 'id' => $thread['iid'], |
||
| 645 | 'title' => $thread['thread_title'], |
||
| 646 | 'lastEditDate' => api_convert_and_format_date($thread['lastedit_date'], DATE_TIME_FORMAT_LONG_24H), |
||
| 647 | 'numberOfReplies' => $thread['thread_replies'], |
||
| 648 | 'numberOfViews' => $thread['thread_views'], |
||
| 649 | 'author' => api_get_person_name($thread['firstname'], $thread['lastname']), |
||
| 650 | ]; |
||
| 651 | } |
||
| 652 | |||
| 653 | return $forum; |
||
| 654 | } |
||
| 655 | |||
| 656 | /** |
||
| 657 | * @param int $forumId |
||
| 658 | * @param int $threadId |
||
| 659 | * |
||
| 660 | * @return array |
||
| 661 | */ |
||
| 662 | public function getCourseForumThread($forumId, $threadId) |
||
| 663 | { |
||
| 664 | require_once api_get_path(SYS_CODE_PATH).'forum/forumfunction.inc.php'; |
||
| 665 | |||
| 666 | $sessionId = $this->session ? $this->session->getId() : 0; |
||
| 667 | $threadInfo = get_thread_information($forumId, $threadId, $sessionId); |
||
| 668 | |||
| 669 | $thread = [ |
||
| 670 | 'id' => intval($threadInfo['iid']), |
||
| 671 | 'cId' => intval($threadInfo['c_id']), |
||
| 672 | 'title' => $threadInfo['thread_title'], |
||
| 673 | 'forumId' => intval($threadInfo['forum_id']), |
||
| 674 | 'posts' => [], |
||
| 675 | ]; |
||
| 676 | |||
| 677 | $forumInfo = get_forums($threadInfo['forum_id'], $this->course->getCode(), true, $sessionId); |
||
| 678 | $postsInfo = getPosts($forumInfo, $threadInfo['iid'], 'ASC'); |
||
| 679 | |||
| 680 | foreach ($postsInfo as $postInfo) { |
||
| 681 | $thread['posts'][] = [ |
||
| 682 | 'id' => $postInfo['iid'], |
||
| 683 | 'title' => $postInfo['post_title'], |
||
| 684 | 'text' => $postInfo['post_text'], |
||
| 685 | 'author' => api_get_person_name($postInfo['firstname'], $postInfo['lastname']), |
||
| 686 | 'date' => api_convert_and_format_date($postInfo['post_date'], DATE_TIME_FORMAT_LONG_24H), |
||
| 687 | 'parentId' => $postInfo['post_parent_id'], |
||
| 688 | ]; |
||
| 689 | } |
||
| 690 | |||
| 691 | return $thread; |
||
| 692 | } |
||
| 693 | |||
| 694 | /** |
||
| 695 | * @return array |
||
| 696 | */ |
||
| 697 | public function getUserProfile() |
||
| 698 | { |
||
| 699 | $pictureInfo = UserManager::get_user_picture_path_by_id($this->user->getId(), 'web'); |
||
| 700 | |||
| 701 | $result = [ |
||
| 702 | 'pictureUri' => $pictureInfo['dir'].$pictureInfo['file'], |
||
| 703 | 'id' => $this->user->getId(), |
||
| 704 | 'status' => $this->user->getStatus(), |
||
| 705 | 'fullName' => UserManager::formatUserFullName($this->user), |
||
| 706 | 'username' => $this->user->getUsername(), |
||
| 707 | 'officialCode' => $this->user->getOfficialCode(), |
||
| 708 | 'phone' => $this->user->getPhone(), |
||
| 709 | 'extra' => [], |
||
| 710 | ]; |
||
| 711 | |||
| 712 | $fieldValue = new ExtraFieldValue('user'); |
||
| 713 | $extraInfo = $fieldValue->getAllValuesForAnItem($this->user->getId(), true); |
||
| 714 | |||
| 715 | foreach ($extraInfo as $extra) { |
||
| 716 | /** @var ExtraFieldValues $extraValue */ |
||
| 717 | $extraValue = $extra['value']; |
||
| 718 | |||
| 719 | $result['extra'][] = [ |
||
| 720 | 'title' => $extraValue->getField()->getDisplayText(true), |
||
| 721 | 'value' => $extraValue->getValue(), |
||
| 722 | ]; |
||
| 723 | } |
||
| 724 | |||
| 725 | return $result; |
||
| 726 | } |
||
| 727 | |||
| 728 | /** |
||
| 729 | * @throws Exception |
||
| 730 | * |
||
| 731 | * @return array |
||
| 732 | */ |
||
| 733 | public function getCourseLearnPaths() |
||
| 734 | { |
||
| 735 | $sessionId = $this->session ? $this->session->getId() : 0; |
||
| 736 | $categoriesTempList = learnpath::getCategories($this->course->getId()); |
||
| 737 | |||
| 738 | $categoryNone = new CLpCategory(); |
||
| 739 | $categoryNone->setId(0); |
||
| 740 | $categoryNone->setName(get_lang('Without category')); |
||
| 741 | $categoryNone->setPosition(0); |
||
| 742 | |||
| 743 | $categories = array_merge([$categoryNone], $categoriesTempList); |
||
| 744 | $categoryData = []; |
||
| 745 | |||
| 746 | /** @var CLpCategory $category */ |
||
| 747 | foreach ($categories as $category) { |
||
| 748 | $learnPathList = new LearnpathList( |
||
| 749 | $this->user->getId(), |
||
| 750 | api_get_course_info($this->course->getCode()), |
||
| 751 | $sessionId, |
||
| 752 | null, |
||
| 753 | false, |
||
| 754 | $category->getId() |
||
| 755 | ); |
||
| 756 | |||
| 757 | $flatLpList = $learnPathList->get_flat_list(); |
||
| 758 | |||
| 759 | if (empty($flatLpList)) { |
||
| 760 | continue; |
||
| 761 | } |
||
| 762 | |||
| 763 | $listData = []; |
||
| 764 | |||
| 765 | foreach ($flatLpList as $lpId => $lpDetails) { |
||
| 766 | if ($lpDetails['lp_visibility'] == 0) { |
||
| 767 | continue; |
||
| 768 | } |
||
| 769 | |||
| 770 | if (!learnpath::is_lp_visible_for_student( |
||
| 771 | $lpId, |
||
| 772 | $this->user->getId(), |
||
| 773 | api_get_course_info($this->course->getCode()), |
||
| 774 | $sessionId |
||
| 775 | )) { |
||
| 776 | continue; |
||
| 777 | } |
||
| 778 | |||
| 779 | $timeLimits = false; |
||
| 780 | |||
| 781 | // This is an old LP (from a migration 1.8.7) so we do nothing |
||
| 782 | if (empty($lpDetails['created_on']) && empty($lpDetails['modified_on'])) { |
||
| 783 | $timeLimits = false; |
||
| 784 | } |
||
| 785 | |||
| 786 | // Checking if expired_on is ON |
||
| 787 | if (!empty($lpDetails['expired_on'])) { |
||
| 788 | $timeLimits = true; |
||
| 789 | } |
||
| 790 | |||
| 791 | if ($timeLimits) { |
||
| 792 | if (!empty($lpDetails['publicated_on']) && !empty($lpDetails['expired_on'])) { |
||
| 793 | $startTime = api_strtotime($lpDetails['publicated_on'], 'UTC'); |
||
| 794 | $endTime = api_strtotime($lpDetails['expired_on'], 'UTC'); |
||
| 795 | $now = time(); |
||
| 796 | $isActivedTime = false; |
||
| 797 | |||
| 798 | if ($now > $startTime && $endTime > $now) { |
||
| 799 | $isActivedTime = true; |
||
| 800 | } |
||
| 801 | |||
| 802 | if (!$isActivedTime) { |
||
| 803 | continue; |
||
| 804 | } |
||
| 805 | } |
||
| 806 | } |
||
| 807 | |||
| 808 | $progress = learnpath::getProgress($lpId, $this->user->getId(), $this->course->getId(), $sessionId); |
||
| 809 | |||
| 810 | $listData[] = [ |
||
| 811 | 'id' => $lpId, |
||
| 812 | 'title' => Security::remove_XSS($lpDetails['lp_name']), |
||
| 813 | 'progress' => intval($progress), |
||
| 814 | 'url' => api_get_path(WEB_CODE_PATH).'webservices/api/v2.php?'.http_build_query([ |
||
| 815 | 'hash' => $this->encodeParams([ |
||
| 816 | 'action' => 'course_learnpath', |
||
| 817 | 'lp_id' => $lpId, |
||
| 818 | 'course' => $this->course->getId(), |
||
| 819 | 'session' => $sessionId, |
||
| 820 | ]), |
||
| 821 | ]), |
||
| 822 | ]; |
||
| 823 | } |
||
| 824 | |||
| 825 | if (empty($listData)) { |
||
| 826 | continue; |
||
| 827 | } |
||
| 828 | |||
| 829 | $categoryData[] = [ |
||
| 830 | 'id' => $category->getId(), |
||
| 831 | 'name' => $category->getName(), |
||
| 832 | 'learnpaths' => $listData, |
||
| 833 | ]; |
||
| 834 | } |
||
| 835 | |||
| 836 | return $categoryData; |
||
| 837 | } |
||
| 838 | |||
| 839 | /** |
||
| 840 | * @param string $encoded |
||
| 841 | * |
||
| 842 | * @return array |
||
| 843 | */ |
||
| 844 | public static function decodeParams($encoded) |
||
| 845 | { |
||
| 846 | $decoded = json_decode($encoded); |
||
| 847 | |||
| 848 | return $decoded; |
||
| 849 | } |
||
| 850 | |||
| 851 | /** |
||
| 852 | * Start login for a user. Then make a redirect to show the learnpath. |
||
| 853 | * |
||
| 854 | * @param int $lpId |
||
| 855 | */ |
||
| 856 | public function showLearningPath($lpId) |
||
| 857 | { |
||
| 858 | $loggedUser['user_id'] = $this->user->getId(); |
||
| 859 | $loggedUser['status'] = $this->user->getStatus(); |
||
| 860 | $loggedUser['uidReset'] = true; |
||
| 861 | $sessionId = $this->session ? $this->session->getId() : 0; |
||
| 862 | |||
| 863 | ChamiloSession::write('_user', $loggedUser); |
||
| 864 | Login::init_user($this->user->getId(), true); |
||
| 865 | |||
| 866 | $url = api_get_path(WEB_CODE_PATH).'lp/lp_controller.php?'.http_build_query([ |
||
| 867 | 'cidReq' => $this->course->getCode(), |
||
| 868 | 'id_session' => $sessionId, |
||
| 869 | 'gidReq' => 0, |
||
| 870 | 'gradebook' => 0, |
||
| 871 | 'origin' => '', |
||
| 872 | 'action' => 'view', |
||
| 873 | 'lp_id' => intval($lpId), |
||
| 874 | 'isStudentView' => 'true', |
||
| 875 | ]); |
||
| 876 | |||
| 877 | header("Location: $url"); |
||
| 878 | exit; |
||
| 879 | } |
||
| 880 | |||
| 881 | /** |
||
| 882 | * @param array $postValues |
||
| 883 | * @param int $forumId |
||
| 884 | * |
||
| 885 | * @return array |
||
| 886 | */ |
||
| 887 | public function saveForumPost(array $postValues, $forumId) |
||
| 888 | { |
||
| 889 | require_once api_get_path(SYS_CODE_PATH).'forum/forumfunction.inc.php'; |
||
| 890 | |||
| 891 | $forum = get_forums($forumId, $this->course->getCode()); |
||
| 892 | store_reply($forum, $postValues, $this->course->getId(), $this->user->getId()); |
||
| 893 | |||
| 894 | return [ |
||
| 895 | 'registered' => true, |
||
| 896 | ]; |
||
| 897 | } |
||
| 898 | |||
| 899 | /** |
||
| 900 | * Get the list of sessions for current user. |
||
| 901 | * |
||
| 902 | * @return array the sessions list |
||
| 903 | */ |
||
| 904 | public function getUserSessions() |
||
| 905 | { |
||
| 906 | $data = []; |
||
| 907 | $sessionsByCategory = UserManager::get_sessions_by_category($this->user->getId(), false); |
||
| 908 | |||
| 909 | foreach ($sessionsByCategory as $category) { |
||
| 910 | $categorySessions = []; |
||
| 911 | |||
| 912 | foreach ($category['sessions'] as $sessions) { |
||
| 913 | $sessionCourses = []; |
||
| 914 | |||
| 915 | foreach ($sessions['courses'] as $course) { |
||
| 916 | $courseInfo = api_get_course_info_by_id($course['real_id']); |
||
| 917 | $teachers = SessionManager::getCoachesByCourseSessionToString( |
||
| 918 | $sessions['session_id'], |
||
| 919 | $course['real_id'] |
||
| 920 | ); |
||
| 921 | |||
| 922 | $sessionCourses[] = [ |
||
| 923 | 'id' => $courseInfo['real_id'], |
||
| 924 | 'title' => $courseInfo['title'], |
||
| 925 | 'code' => $courseInfo['code'], |
||
| 926 | 'directory' => $courseInfo['directory'], |
||
| 927 | 'pictureUrl' => $courseInfo['course_image_large'], |
||
| 928 | 'urlPicture' => $courseInfo['course_image_large'], |
||
| 929 | 'teachers' => $teachers, |
||
| 930 | ]; |
||
| 931 | } |
||
| 932 | |||
| 933 | $sessionBox = Display::getSessionTitleBox($sessions['session_id']); |
||
| 934 | |||
| 935 | $categorySessions[] = [ |
||
| 936 | 'name' => $sessionBox['title'], |
||
| 937 | 'id' => $sessions['session_id'], |
||
| 938 | 'date' => $sessionBox['dates'], |
||
| 939 | 'duration' => isset($sessionBox['duration']) ? $sessionBox['duration'] : null, |
||
| 940 | 'courses' => $sessionCourses, |
||
| 941 | ]; |
||
| 942 | } |
||
| 943 | |||
| 944 | $data[] = [ |
||
| 945 | 'id' => $category['session_category']['id'], |
||
| 946 | 'name' => $category['session_category']['name'], |
||
| 947 | 'sessions' => $categorySessions, |
||
| 948 | ]; |
||
| 949 | } |
||
| 950 | |||
| 951 | return $data; |
||
| 952 | } |
||
| 953 | |||
| 954 | /** |
||
| 955 | * @param string $subject |
||
| 956 | * @param string $text |
||
| 957 | * @param array $receivers |
||
| 958 | * |
||
| 959 | * @return array |
||
| 960 | */ |
||
| 961 | public function saveUserMessage($subject, $text, array $receivers) |
||
| 962 | { |
||
| 963 | foreach ($receivers as $userId) { |
||
| 964 | MessageManager::send_message($userId, $subject, $text); |
||
| 965 | } |
||
| 966 | |||
| 967 | return [ |
||
| 968 | 'sent' => true, |
||
| 969 | ]; |
||
| 970 | } |
||
| 971 | |||
| 972 | /** |
||
| 973 | * @param string $search |
||
| 974 | * |
||
| 975 | * @return array |
||
| 976 | */ |
||
| 977 | public function getMessageUsers($search) |
||
| 978 | { |
||
| 979 | $repo = UserManager::getRepository(); |
||
| 980 | |||
| 981 | $users = $repo->findUsersToSendMessage($this->user->getId(), $search); |
||
| 982 | $showEmail = api_get_setting('show_email_addresses') === 'true'; |
||
| 983 | $data = []; |
||
| 984 | |||
| 985 | /** @var User $user */ |
||
| 986 | foreach ($users as $user) { |
||
| 987 | $userName = UserManager::formatUserFullName($user); |
||
| 988 | |||
| 989 | if ($showEmail) { |
||
| 990 | $userName .= " ({$user->getEmail()})"; |
||
| 991 | } |
||
| 992 | |||
| 993 | $data[] = [ |
||
| 994 | 'id' => $user->getId(), |
||
| 995 | 'name' => $userName, |
||
| 996 | ]; |
||
| 997 | } |
||
| 998 | |||
| 999 | return $data; |
||
| 1000 | } |
||
| 1001 | |||
| 1002 | /** |
||
| 1003 | * @param string $title |
||
| 1004 | * @param string $text |
||
| 1005 | * |
||
| 1006 | * @return array |
||
| 1007 | */ |
||
| 1008 | public function saveCourseNotebook($title, $text) |
||
| 1009 | { |
||
| 1010 | $values = ['note_title' => $title, 'note_comment' => $text]; |
||
| 1011 | $sessionId = $this->session ? $this->session->getId() : 0; |
||
| 1012 | |||
| 1013 | $noteBookId = NotebookManager::save_note( |
||
| 1014 | $values, |
||
| 1015 | $this->user->getId(), |
||
| 1016 | $this->course->getId(), |
||
| 1017 | $sessionId |
||
| 1018 | ); |
||
| 1019 | |||
| 1020 | return [ |
||
| 1021 | 'registered' => $noteBookId, |
||
| 1022 | ]; |
||
| 1023 | } |
||
| 1024 | |||
| 1025 | /** |
||
| 1026 | * @param array $values |
||
| 1027 | * @param int $forumId |
||
| 1028 | * |
||
| 1029 | * @return array |
||
| 1030 | */ |
||
| 1031 | public function saveForumThread(array $values, $forumId) |
||
| 1032 | { |
||
| 1033 | require_once api_get_path(SYS_CODE_PATH).'forum/forumfunction.inc.php'; |
||
| 1034 | |||
| 1035 | $sessionId = $this->session ? $this->session->getId() : 0; |
||
| 1036 | $forum = get_forums($forumId, $this->course->getCode(), true, $sessionId); |
||
| 1037 | $courseInfo = api_get_course_info($this->course->getCode()); |
||
| 1038 | $thread = store_thread($forum, $values, $courseInfo, false, $this->user->getId(), $sessionId); |
||
| 1039 | |||
| 1040 | return [ |
||
| 1041 | 'registered' => $thread->getIid(), |
||
| 1042 | ]; |
||
| 1043 | } |
||
| 1044 | |||
| 1045 | /** |
||
| 1046 | * @param array $params |
||
| 1047 | * |
||
| 1048 | * @return array |
||
| 1049 | */ |
||
| 1050 | public function getUsersCampus(array $params) |
||
| 1051 | { |
||
| 1052 | $conditions = [ |
||
| 1053 | 'status' => $params['status'], |
||
| 1054 | ]; |
||
| 1055 | $idCampus = $params['id_campus']; |
||
| 1056 | $users = UserManager::get_user_list($conditions, ['firstname'], false, false, $idCampus); |
||
| 1057 | $list = []; |
||
| 1058 | foreach ($users as $item) { |
||
| 1059 | $listTemp = [ |
||
| 1060 | 'id' => $item['user_id'], |
||
| 1061 | 'firstname' => $item['firstname'], |
||
| 1062 | 'lastname' => $item['lastname'], |
||
| 1063 | 'email' => $item['email'], |
||
| 1064 | ]; |
||
| 1065 | $list[] = $listTemp; |
||
| 1066 | } |
||
| 1067 | |||
| 1068 | return $list; |
||
| 1069 | } |
||
| 1070 | |||
| 1071 | /** |
||
| 1072 | * @param array $params |
||
| 1073 | * |
||
| 1074 | * @return array |
||
| 1075 | */ |
||
| 1076 | public function getCoursesCampus(array $params) |
||
| 1077 | { |
||
| 1078 | $idCampus = $params['id_campus']; |
||
| 1079 | |||
| 1080 | $courseList = CourseManager::get_courses_list( |
||
| 1081 | 0, //offset |
||
| 1082 | 0, //howMany |
||
| 1083 | 1, //$orderby = 1 |
||
| 1084 | 'ASC', |
||
| 1085 | -1, //visibility |
||
| 1086 | null, |
||
| 1087 | $idCampus, //$urlId |
||
| 1088 | true //AlsoSearchCode |
||
| 1089 | ); |
||
| 1090 | |||
| 1091 | return $courseList; |
||
| 1092 | } |
||
| 1093 | |||
| 1094 | /** |
||
| 1095 | * @param array $params |
||
| 1096 | * |
||
| 1097 | * @return array |
||
| 1098 | */ |
||
| 1099 | public function addSession(array $params) |
||
| 1100 | { |
||
| 1101 | $name = $params['name']; |
||
| 1102 | $coach_username = intval($params['coach_username']); |
||
| 1103 | $startDate = $params['access_start_date']; |
||
| 1104 | $endDate = $params['access_end_date']; |
||
| 1105 | $displayStartDate = $startDate; |
||
| 1106 | $displayEndDate = $endDate; |
||
| 1107 | $description = $params['description']; |
||
| 1108 | $idUrlCampus = $params['id_campus']; |
||
| 1109 | |||
| 1110 | $return = SessionManager::create_session( |
||
| 1111 | $name, |
||
| 1112 | $startDate, |
||
| 1113 | $endDate, |
||
| 1114 | $displayStartDate, |
||
| 1115 | $displayEndDate, |
||
| 1116 | null, |
||
| 1117 | null, |
||
| 1118 | $coach_username, |
||
| 1119 | null, |
||
| 1120 | 1, |
||
| 1121 | false, |
||
| 1122 | null, |
||
| 1123 | $description, |
||
| 1124 | 1, |
||
| 1125 | [], |
||
| 1126 | null, |
||
| 1127 | false, |
||
| 1128 | $idUrlCampus |
||
| 1129 | ); |
||
| 1130 | |||
| 1131 | if ($return) { |
||
| 1132 | $out = [ |
||
| 1133 | 'status' => true, |
||
| 1134 | 'message' => 'Sesión creada correctamente', |
||
| 1135 | 'id_session' => $return, |
||
| 1136 | ]; |
||
| 1137 | } else { |
||
| 1138 | $out = [ |
||
| 1139 | 'status' => false, |
||
| 1140 | 'message' => 'Error al crear la sesión', |
||
| 1141 | ]; |
||
| 1142 | } |
||
| 1143 | |||
| 1144 | return $out; |
||
| 1145 | } |
||
| 1146 | |||
| 1147 | /** |
||
| 1148 | * @param array $courseParam |
||
| 1149 | * |
||
| 1150 | * @return array |
||
| 1151 | */ |
||
| 1152 | public function addCourse(array $courseParam) |
||
| 1153 | { |
||
| 1154 | $results = []; |
||
| 1155 | $idCampus = isset($courseParam['id_campus']) ? $courseParam['id_campus'] : 1; |
||
| 1156 | $title = isset($courseParam['title']) ? $courseParam['title'] : ''; |
||
| 1157 | $wantedCode = isset($courseParam['wanted_code']) ? $courseParam['wanted_code'] : null; |
||
| 1158 | $diskQuota = isset($courseParam['disk_quota']) ? $courseParam['disk_quota'] : '100'; |
||
| 1159 | $visibility = isset($courseParam['visibility']) ? (int) $courseParam['visibility'] : null; |
||
| 1160 | |||
| 1161 | if (isset($courseParam['visibility'])) { |
||
| 1162 | if ($courseParam['visibility'] && |
||
| 1163 | $courseParam['visibility'] >= 0 && |
||
| 1164 | $courseParam['visibility'] <= 3 |
||
| 1165 | ) { |
||
| 1166 | $visibility = (int) $courseParam['visibility']; |
||
| 1167 | } |
||
| 1168 | } |
||
| 1169 | |||
| 1170 | $params = []; |
||
| 1171 | $params['title'] = $title; |
||
| 1172 | $params['wanted_code'] = 'CAMPUS_'.$idCampus.'_'.$wantedCode; |
||
| 1173 | $params['user_id'] = $this->user->getId(); |
||
| 1174 | $params['visibility'] = $visibility; |
||
| 1175 | $params['disk_quota'] = $diskQuota; |
||
| 1176 | |||
| 1177 | $courseInfo = CourseManager::create_course($params, $params['user_id'], $idCampus); |
||
| 1178 | |||
| 1179 | if (!empty($courseInfo)) { |
||
| 1180 | $results['status'] = true; |
||
| 1181 | $results['code_course'] = $courseInfo['code']; |
||
| 1182 | $results['title_course'] = $courseInfo['title']; |
||
| 1183 | $results['message'] = 'Curso registrado con exito'; |
||
| 1184 | } else { |
||
| 1185 | $results['status'] = false; |
||
| 1186 | $results['message'] = 'Error al registrar el curso'; |
||
| 1187 | } |
||
| 1188 | |||
| 1189 | return $results; |
||
| 1190 | } |
||
| 1191 | |||
| 1192 | /** |
||
| 1193 | * @param $user_param |
||
| 1194 | * |
||
| 1195 | * @return array |
||
| 1196 | */ |
||
| 1197 | public function addUser($user_param) |
||
| 1198 | { |
||
| 1199 | $results = []; |
||
| 1200 | $orig_user_id_value = []; |
||
| 1201 | $firstName = $user_param['firstname']; |
||
| 1202 | $lastName = $user_param['lastname']; |
||
| 1203 | $status = $user_param['status']; |
||
| 1204 | $email = $user_param['email']; |
||
| 1205 | $loginName = $user_param['loginname']; |
||
| 1206 | $password = $user_param['password']; |
||
| 1207 | $official_code = ''; |
||
| 1208 | $language = ''; |
||
| 1209 | $phone = ''; |
||
| 1210 | $picture_uri = ''; |
||
| 1211 | $auth_source = PLATFORM_AUTH_SOURCE; |
||
| 1212 | $expiration_date = ''; |
||
| 1213 | $active = 1; |
||
| 1214 | $hr_dept_id = 0; |
||
| 1215 | $extra = null; |
||
| 1216 | $original_user_id_name = $user_param['original_user_id_name']; |
||
| 1217 | $original_user_id_value = $user_param['original_user_id_value']; |
||
| 1218 | $orig_user_id_value[] = $user_param['original_user_id_value']; |
||
| 1219 | $extra_list = $user_param['extra']; |
||
| 1220 | if (!empty($user_param['language'])) { |
||
| 1221 | $language = $user_param['language']; |
||
| 1222 | } |
||
| 1223 | if (!empty($user_param['phone'])) { |
||
| 1224 | $phone = $user_param['phone']; |
||
| 1225 | } |
||
| 1226 | if (!empty($user_param['expiration_date'])) { |
||
| 1227 | $expiration_date = $user_param['expiration_date']; |
||
| 1228 | } |
||
| 1229 | |||
| 1230 | // Default language. |
||
| 1231 | if (empty($language)) { |
||
| 1232 | $language = api_get_setting('platformLanguage'); |
||
| 1233 | } |
||
| 1234 | |||
| 1235 | // First check wether the login already exists. |
||
| 1236 | if (!UserManager::is_username_available($loginName)) { |
||
| 1237 | $results[] = 0; |
||
| 1238 | } |
||
| 1239 | |||
| 1240 | $userId = UserManager::create_user( |
||
| 1241 | $firstName, |
||
| 1242 | $lastName, |
||
| 1243 | $status, |
||
| 1244 | $email, |
||
| 1245 | $loginName, |
||
| 1246 | $password, |
||
| 1247 | $official_code, |
||
| 1248 | $language, |
||
| 1249 | $phone, |
||
| 1250 | $picture_uri, |
||
| 1251 | $auth_source, |
||
| 1252 | $expiration_date, |
||
| 1253 | $active, |
||
| 1254 | $hr_dept_id |
||
| 1255 | ); |
||
| 1256 | |||
| 1257 | if ($userId) { |
||
| 1258 | if (api_is_multiple_url_enabled()) { |
||
| 1259 | if (api_get_current_access_url_id() != -1) { |
||
| 1260 | UrlManager::add_user_to_url( |
||
| 1261 | $userId, |
||
| 1262 | api_get_current_access_url_id() |
||
| 1263 | ); |
||
| 1264 | } else { |
||
| 1265 | UrlManager::add_user_to_url($userId, 1); |
||
| 1266 | } |
||
| 1267 | } else { |
||
| 1268 | // We add by default the access_url_user table with access_url_id = 1 |
||
| 1269 | UrlManager::add_user_to_url($userId, 1); |
||
| 1270 | } |
||
| 1271 | |||
| 1272 | // Save new field label into user_field table. |
||
| 1273 | UserManager::create_extra_field( |
||
| 1274 | $original_user_id_name, |
||
| 1275 | 1, |
||
| 1276 | $original_user_id_name, |
||
| 1277 | '' |
||
| 1278 | ); |
||
| 1279 | // Save the external system's id into user_field_value table. |
||
| 1280 | UserManager::update_extra_field_value( |
||
| 1281 | $userId, |
||
| 1282 | $original_user_id_name, |
||
| 1283 | $original_user_id_value |
||
| 1284 | ); |
||
| 1285 | |||
| 1286 | if (is_array($extra_list) && count($extra_list) > 0) { |
||
| 1287 | foreach ($extra_list as $extra) { |
||
| 1288 | $extra_field_name = $extra['field_name']; |
||
| 1289 | $extra_field_value = $extra['field_value']; |
||
| 1290 | // Save new field label into user_field table. |
||
| 1291 | UserManager::create_extra_field( |
||
| 1292 | $extra_field_name, |
||
| 1293 | 1, |
||
| 1294 | $extra_field_name, |
||
| 1295 | '' |
||
| 1296 | ); |
||
| 1297 | // Save the external system's id into user_field_value table. |
||
| 1298 | UserManager::update_extra_field_value( |
||
| 1299 | $userId, |
||
| 1300 | $extra_field_name, |
||
| 1301 | $extra_field_value |
||
| 1302 | ); |
||
| 1303 | } |
||
| 1304 | } |
||
| 1305 | $results[] = $userId; |
||
| 1306 | } else { |
||
| 1307 | $results[] = 0; |
||
| 1308 | } |
||
| 1309 | |||
| 1310 | return $results; |
||
| 1311 | } |
||
| 1312 | |||
| 1313 | /** |
||
| 1314 | * Subscribe User to Course. |
||
| 1315 | * |
||
| 1316 | * @param array $params |
||
| 1317 | * |
||
| 1318 | * @return array |
||
| 1319 | */ |
||
| 1320 | public function subscribeUserToCourse($params) |
||
| 1321 | { |
||
| 1322 | $course_id = $params['course_id']; |
||
| 1323 | $course_code = $params['course_code']; |
||
| 1324 | $user_id = $params['user_id']; |
||
| 1325 | if (!$course_id && !$course_code) { |
||
| 1326 | return [false]; |
||
| 1327 | } |
||
| 1328 | if (!$course_code) { |
||
| 1329 | $course_code = CourseManager::get_course_code_from_course_id($course_id); |
||
| 1330 | } |
||
| 1331 | if (CourseManager::subscribeUser($user_id, $course_code)) { |
||
| 1332 | return [true]; |
||
| 1333 | } else { |
||
| 1334 | return [false]; |
||
| 1335 | } |
||
| 1336 | |||
| 1337 | return [true]; |
||
| 1338 | } |
||
| 1339 | |||
| 1340 | /** |
||
| 1341 | * Add Campus Virtual. |
||
| 1342 | * |
||
| 1343 | * @param array Params Campus |
||
| 1344 | * |
||
| 1345 | * @return array |
||
| 1346 | */ |
||
| 1347 | public function createCampusURL($params) |
||
| 1348 | { |
||
| 1349 | $urlCampus = Security::remove_XSS($params['url']); |
||
| 1350 | $description = Security::remove_XSS($params['description']); |
||
| 1351 | |||
| 1352 | $active = isset($params['active']) ? intval($params['active']) : 0; |
||
| 1353 | $num = UrlManager::url_exist($urlCampus); |
||
| 1354 | if ($num == 0) { |
||
| 1355 | // checking url |
||
| 1356 | if (substr($urlCampus, strlen($urlCampus) - 1, strlen($urlCampus)) == '/') { |
||
| 1357 | $idCampus = UrlManager::add($urlCampus, $description, $active, true); |
||
| 1358 | } else { |
||
| 1359 | //create |
||
| 1360 | $idCampus = UrlManager::add($urlCampus.'/', $description, $active, true); |
||
| 1361 | } |
||
| 1362 | |||
| 1363 | return [ |
||
| 1364 | 'status' => true, |
||
| 1365 | 'id_campus' => $idCampus, |
||
| 1366 | ]; |
||
| 1367 | } |
||
| 1368 | |||
| 1369 | return [ |
||
| 1370 | 'status' => false, |
||
| 1371 | 'id_campus' => 0, |
||
| 1372 | ]; |
||
| 1373 | } |
||
| 1374 | |||
| 1375 | /** |
||
| 1376 | * Edit Campus Virtual. |
||
| 1377 | * |
||
| 1378 | * @param array Params Campus |
||
| 1379 | * |
||
| 1380 | * @return array |
||
| 1381 | */ |
||
| 1382 | public function editCampusURL($params) |
||
| 1383 | { |
||
| 1384 | $urlCampus = Security::remove_XSS($params['url']); |
||
| 1385 | $description = Security::remove_XSS($params['description']); |
||
| 1386 | |||
| 1387 | $active = isset($params['active']) ? intval($params['active']) : 0; |
||
| 1388 | $url_id = isset($params['id']) ? intval($params['id']) : 0; |
||
| 1389 | |||
| 1390 | if (!empty($url_id)) { |
||
| 1391 | //we can't change the status of the url with id=1 |
||
| 1392 | if ($url_id == 1) { |
||
| 1393 | $active = 1; |
||
| 1394 | } |
||
| 1395 | //checking url |
||
| 1396 | if (substr($urlCampus, strlen($urlCampus) - 1, strlen($urlCampus)) == '/') { |
||
| 1397 | UrlManager::update($url_id, $urlCampus, $description, $active); |
||
| 1398 | } else { |
||
| 1399 | UrlManager::update($url_id, $urlCampus.'/', $description, $active); |
||
| 1400 | } |
||
| 1401 | |||
| 1402 | return [true]; |
||
| 1403 | } |
||
| 1404 | |||
| 1405 | return [false]; |
||
| 1406 | } |
||
| 1407 | |||
| 1408 | /** |
||
| 1409 | * Delete Campus Virtual. |
||
| 1410 | * |
||
| 1411 | * @param array Params Campus |
||
| 1412 | * |
||
| 1413 | * @return array |
||
| 1414 | */ |
||
| 1415 | public function deleteCampusURL($params) |
||
| 1416 | { |
||
| 1417 | $url_id = isset($params['id']) ? intval($params['id']) : 0; |
||
| 1418 | |||
| 1419 | $result = UrlManager::delete($url_id); |
||
| 1420 | if ($result) { |
||
| 1421 | return [ |
||
| 1422 | 'status' => true, |
||
| 1423 | 'message' => get_lang('URL deleted.'), |
||
| 1424 | ]; |
||
| 1425 | } else { |
||
| 1426 | return [ |
||
| 1427 | 'status' => false, |
||
| 1428 | 'message' => get_lang('Error'), |
||
| 1429 | ]; |
||
| 1430 | } |
||
| 1431 | } |
||
| 1432 | |||
| 1433 | /** |
||
| 1434 | * @param array $params |
||
| 1435 | * |
||
| 1436 | * @throws Exception |
||
| 1437 | * |
||
| 1438 | * @return array |
||
| 1439 | */ |
||
| 1440 | public function addCoursesSession(array $params) |
||
| 1441 | { |
||
| 1442 | $sessionId = $params['id_session']; |
||
| 1443 | $courseList = $params['list_courses']; |
||
| 1444 | |||
| 1445 | $result = SessionManager::add_courses_to_session( |
||
| 1446 | $sessionId, |
||
| 1447 | $courseList, |
||
| 1448 | true, |
||
| 1449 | false |
||
| 1450 | ); |
||
| 1451 | |||
| 1452 | if ($result) { |
||
| 1453 | return [ |
||
| 1454 | 'status' => $result, |
||
| 1455 | 'message' => 'Los cursos fueron añadidos a la sessión', |
||
| 1456 | ]; |
||
| 1457 | } else { |
||
| 1458 | return [ |
||
| 1459 | 'status' => $result, |
||
| 1460 | 'message' => 'Error al añadir cursos a la sessión', |
||
| 1461 | ]; |
||
| 1462 | } |
||
| 1463 | } |
||
| 1464 | |||
| 1465 | /** |
||
| 1466 | * @param array $params |
||
| 1467 | * |
||
| 1468 | * @return array |
||
| 1469 | */ |
||
| 1470 | public function addUsersSession(array $params) |
||
| 1471 | { |
||
| 1472 | $sessionId = $params['id_session']; |
||
| 1473 | $userList = $params['list_users']; |
||
| 1474 | |||
| 1475 | if (!is_array($userList)) { |
||
| 1476 | $userList = []; |
||
| 1477 | } |
||
| 1478 | |||
| 1479 | SessionManager::subscribeUsersToSession( |
||
| 1480 | $sessionId, |
||
| 1481 | $userList, |
||
| 1482 | null, |
||
| 1483 | false |
||
| 1484 | ); |
||
| 1485 | |||
| 1486 | return [ |
||
| 1487 | 'status' => true, |
||
| 1488 | 'message' => 'Error al añadir usuarios a la sessión', |
||
| 1489 | ]; |
||
| 1490 | } |
||
| 1491 | |||
| 1492 | /** |
||
| 1493 | * @param array $additionalParams Optional |
||
| 1494 | * |
||
| 1495 | * @return string |
||
| 1496 | */ |
||
| 1497 | private function encodeParams(array $additionalParams = []) |
||
| 1498 | { |
||
| 1499 | $params = array_merge($additionalParams, [ |
||
| 1500 | 'api_key' => $this->apiKey, |
||
| 1501 | 'username' => $this->user->getUsername(), |
||
| 1502 | ]); |
||
| 1503 | $encoded = json_encode($params); |
||
| 1504 | |||
| 1505 | return $encoded; |
||
| 1506 | } |
||
| 1508 |