Rest.php 44 KB

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