Rest.php 45 KB

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