Rest.php 45 KB

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