Rest.php 45 KB

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