courses_controller.php 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. use Chamilo\CoreBundle\Entity\Repository\SequenceRepository;
  4. use Chamilo\CoreBundle\Entity\SequenceResource;
  5. use Chamilo\CoreBundle\Entity\SessionRelCourse;
  6. use Chamilo\CoreBundle\Entity\Tag;
  7. /**
  8. * Class CoursesController.
  9. *
  10. * This file contains class used like controller,
  11. * it should be included inside a dispatcher file (e.g: index.php)
  12. *
  13. * @author Christian Fasanando <christian1827@gmail.com> - BeezNest
  14. *
  15. * @package chamilo.auth
  16. */
  17. class CoursesController
  18. {
  19. private $toolname;
  20. private $view;
  21. private $model;
  22. /**
  23. * Constructor.
  24. */
  25. public function __construct()
  26. {
  27. $this->toolname = 'auth';
  28. //$actived_theme_path = api_get_template();
  29. $this->view = new View($this->toolname);
  30. $this->model = new Auth();
  31. }
  32. /**
  33. * It's used for listing courses,
  34. * render to courses_list view.
  35. *
  36. * @param string $action
  37. * @param string $message confirmation message(optional)
  38. */
  39. public function courseList($action, $message = '')
  40. {
  41. $data = [];
  42. $data['user_courses'] = $this->model->get_courses_of_user(api_get_user_id());
  43. $data['user_course_categories'] = CourseManager::get_user_course_categories(api_get_user_id());
  44. $data['courses_in_category'] = $this->model->get_courses_in_category();
  45. $data['action'] = $action;
  46. $data['message'] = $message;
  47. // render to the view
  48. $this->view->set_data($data);
  49. $this->view->set_layout('catalog_layout');
  50. $this->view->set_template('courses_list');
  51. $this->view->render();
  52. }
  53. /**
  54. * It's used for listing categories, render to categories_list view.
  55. */
  56. public function categoryList()
  57. {
  58. api_block_anonymous_users();
  59. $stok = Security::get_token();
  60. $actions = Display::url(
  61. Display::return_icon('back.png', get_lang('Back'), '', '32'),
  62. api_get_path(WEB_CODE_PATH).'auth/courses.php?action=sortmycourses'
  63. );
  64. $actions = Display::toolbarAction('toolbar-forum', [$actions]);
  65. $form = new FormValidator(
  66. 'create_course_category',
  67. 'post',
  68. api_get_path(WEB_CODE_PATH).'auth/courses.php?action=createcoursecategory'
  69. );
  70. $form->addHidden('sec_token', $stok);
  71. $form->addText('title_course_category', get_lang('Name'));
  72. $form->addButtonSave(get_lang('AddCategory'), 'create_course_category');
  73. $tpl = new Template();
  74. $tpl->assign('content', $form->returnForm());
  75. $tpl->assign('actions', $actions);
  76. $tpl->display_one_col_template();
  77. }
  78. /**
  79. * It's used for listing courses with categories,
  80. * render to courses_categories view.
  81. *
  82. * @param string $action
  83. * @param string $category_code
  84. * @param string $message
  85. * @param string $error
  86. * @param string $content
  87. * @param array $limit will be used if $random_value is not set.
  88. * This array should contains 'start' and 'length' keys
  89. *
  90. * @internal param \action $string
  91. * @internal param \Category $string code (optional)
  92. */
  93. public function courses_categories(
  94. $action,
  95. $category_code = null,
  96. $message = '',
  97. $error = '',
  98. $content = null,
  99. $limit = []
  100. ) {
  101. $data = [];
  102. $listCategories = CoursesAndSessionsCatalog::getCourseCategoriesTree();
  103. $data['countCoursesInCategory'] = CourseCategory::countCoursesInCategory($category_code);
  104. if ($action === 'display_random_courses') {
  105. // Random value is used instead limit filter
  106. $data['browse_courses_in_category'] = CoursesAndSessionsCatalog::getCoursesInCategory(null, 12);
  107. $data['countCoursesInCategory'] = count($data['browse_courses_in_category']);
  108. } else {
  109. if (!isset($category_code)) {
  110. $category_code = $listCategories['ALL']['code']; // by default first category
  111. }
  112. $limit = isset($limit) ? $limit : self::getLimitArray();
  113. $listCourses = CoursesAndSessionsCatalog::getCoursesInCategory($category_code, null, $limit);
  114. $data['browse_courses_in_category'] = $listCourses;
  115. }
  116. $data['list_categories'] = $listCategories;
  117. $data['code'] = Security::remove_XSS($category_code);
  118. // getting all the courses to which the user is subscribed to
  119. $curr_user_id = api_get_user_id();
  120. $user_courses = $this->model->get_courses_of_user($curr_user_id);
  121. $user_coursecodes = [];
  122. // we need only the course codes as these will be used to match against the courses of the category
  123. if ($user_courses != '') {
  124. foreach ($user_courses as $key => $value) {
  125. $user_coursecodes[] = $value['code'];
  126. }
  127. }
  128. if (api_is_drh()) {
  129. $courses = CourseManager::get_courses_followed_by_drh(api_get_user_id());
  130. foreach ($courses as $course) {
  131. $user_coursecodes[] = $course['code'];
  132. }
  133. }
  134. $data['user_coursecodes'] = $user_coursecodes;
  135. $data['action'] = $action;
  136. $data['message'] = $message;
  137. $data['content'] = $content;
  138. $data['error'] = $error;
  139. $data['catalogShowCoursesSessions'] = 0;
  140. $showCoursesSessions = (int) api_get_setting('catalog_show_courses_sessions');
  141. if ($showCoursesSessions > 0) {
  142. $data['catalogShowCoursesSessions'] = $showCoursesSessions;
  143. }
  144. // render to the view
  145. $this->view->set_data($data);
  146. $this->view->set_layout('layout');
  147. $this->view->set_template('courses_categories');
  148. $this->view->render();
  149. }
  150. /**
  151. * @param string $search_term
  152. * @param string $message
  153. * @param string $error
  154. * @param string $content
  155. * @param array $limit
  156. * @param bool $justVisible Whether to search only in courses visibles in the catalogue
  157. */
  158. public function search_courses(
  159. $search_term,
  160. $message = '',
  161. $error = '',
  162. $content = null,
  163. $limit = [],
  164. $justVisible = false
  165. ) {
  166. $data = [];
  167. $limit = !empty($limit) ? $limit : self::getLimitArray();
  168. $browse_course_categories = CoursesAndSessionsCatalog::getCourseCategories();
  169. $data['countCoursesInCategory'] = CourseCategory::countCoursesInCategory('ALL', $search_term);
  170. $data['browse_courses_in_category'] = CoursesAndSessionsCatalog::search_courses(
  171. $search_term,
  172. $limit,
  173. $justVisible
  174. );
  175. $data['browse_course_categories'] = $browse_course_categories;
  176. $data['search_term'] = Security::remove_XSS($search_term); //filter before showing in template
  177. // getting all the courses to which the user is subscribed to
  178. $curr_user_id = api_get_user_id();
  179. $user_courses = $this->model->get_courses_of_user($curr_user_id);
  180. $user_coursecodes = [];
  181. // we need only the course codes as these will be used to match against the courses of the category
  182. if ($user_courses != '') {
  183. foreach ($user_courses as $value) {
  184. $user_coursecodes[] = $value['code'];
  185. }
  186. }
  187. $data['user_coursecodes'] = $user_coursecodes;
  188. $data['message'] = $message;
  189. $data['content'] = $content;
  190. $data['error'] = $error;
  191. $data['action'] = 'display_courses';
  192. // render to the view
  193. $this->view->set_data($data);
  194. $this->view->set_layout('catalog_layout');
  195. $this->view->set_template('courses_categories');
  196. $this->view->render();
  197. }
  198. /**
  199. * Create a category
  200. * render to listing view.
  201. *
  202. * @param string $title
  203. */
  204. public function addCourseCategory($title)
  205. {
  206. $result = $this->model->store_course_category($title);
  207. if ($result) {
  208. Display::addFlash(
  209. Display::return_message(get_lang('CourseCategoryStored'))
  210. );
  211. } else {
  212. Display::addFlash(
  213. Display::return_message(
  214. get_lang('ACourseCategoryWithThisNameAlreadyExists'),
  215. 'error'
  216. )
  217. );
  218. }
  219. header('Location: '.api_get_path(WEB_CODE_PATH).'auth/courses.php?action=sortmycourses');
  220. exit;
  221. }
  222. /**
  223. * Change course category
  224. * render to listing view.
  225. *
  226. * @param string $course_code
  227. * @param int $category_id
  228. */
  229. public function change_course_category($course_code, $category_id)
  230. {
  231. $courseInfo = api_get_course_info($course_code);
  232. $courseId = $courseInfo['real_id'];
  233. $result = $this->model->updateCourseCategory($courseId, $category_id);
  234. if ($result) {
  235. Display::addFlash(
  236. Display::return_message(get_lang('EditCourseCategorySucces'))
  237. );
  238. }
  239. $action = 'sortmycourses';
  240. $this->courseList($action);
  241. }
  242. /**
  243. * Move up/down courses inside a category
  244. * render to listing view.
  245. *
  246. * @param string $move move to up or down
  247. * @param string $course_code
  248. * @param int $category_id Category id
  249. */
  250. public function move_course($move, $course_code, $category_id)
  251. {
  252. $result = $this->model->move_course($move, $course_code, $category_id);
  253. if ($result) {
  254. Display::addFlash(
  255. Display::return_message(get_lang('CourseSortingDone'))
  256. );
  257. }
  258. $action = 'sortmycourses';
  259. $this->courseList($action);
  260. }
  261. /**
  262. * Move up/down categories
  263. * render to listing view.
  264. *
  265. * @param string $move move to up or down
  266. * @param int $category_id Category id
  267. */
  268. public function move_category($move, $category_id)
  269. {
  270. $result = $this->model->move_category($move, $category_id);
  271. if ($result) {
  272. Display::addFlash(
  273. Display::return_message(get_lang('CategorySortingDone'))
  274. );
  275. }
  276. $action = 'sortmycourses';
  277. $this->courseList($action);
  278. }
  279. /**
  280. * Edit course category
  281. * render to listing view.
  282. *
  283. * @param string $title Category title
  284. * @param int $category Category id
  285. */
  286. public function edit_course_category($title, $category)
  287. {
  288. $result = $this->model->store_edit_course_category($title, $category);
  289. if ($result) {
  290. Display::addFlash(
  291. Display::return_message(get_lang('CourseCategoryEditStored'))
  292. );
  293. }
  294. $action = 'sortmycourses';
  295. $this->courseList($action);
  296. }
  297. /**
  298. * Delete a course category
  299. * render to listing view.
  300. *
  301. * @param int Category id
  302. */
  303. public function delete_course_category($category_id)
  304. {
  305. $result = $this->model->delete_course_category($category_id);
  306. if ($result) {
  307. Display::addFlash(
  308. Display::return_message(get_lang('CourseCategoryDeleted'))
  309. );
  310. }
  311. $action = 'sortmycourses';
  312. $this->courseList($action);
  313. }
  314. /**
  315. * Unsubscribe user from a course
  316. * render to listing view.
  317. *
  318. * @param string $course_code
  319. * @param string $search_term
  320. * @param string $category_code
  321. */
  322. public function unsubscribe_user_from_course(
  323. $course_code,
  324. $search_term = null,
  325. $category_code = null
  326. ) {
  327. $result = $this->model->remove_user_from_course($course_code);
  328. $message = '';
  329. $error = '';
  330. if ($result) {
  331. Display::addFlash(
  332. Display::return_message(get_lang('YouAreNowUnsubscribed'))
  333. );
  334. }
  335. if (!empty($search_term)) {
  336. CoursesAndSessionsCatalog::search_courses($search_term, $message, $error);
  337. } else {
  338. $this->courses_categories(
  339. 'subcribe',
  340. $category_code,
  341. $message,
  342. $error
  343. );
  344. }
  345. }
  346. /**
  347. * Get the html block for courses categories.
  348. *
  349. * @param string $code Current category code
  350. * @param bool $hiddenLinks Whether hidden links
  351. * @param array $limit
  352. *
  353. * @return string The HTML block
  354. */
  355. public function getCoursesCategoriesBlock(
  356. $code = null,
  357. $hiddenLinks = false,
  358. $limit = null
  359. ) {
  360. $categories = CoursesAndSessionsCatalog::getCourseCategories();
  361. $html = '';
  362. if (!empty($categories)) {
  363. $action = 'display_courses';
  364. foreach ($categories[0] as $category) {
  365. $categoryName = $category['name'];
  366. $categoryCode = $category['code'];
  367. $categoryCourses = $category['count_courses'];
  368. $html .= '<li>';
  369. $categoryLink = CourseCategory::getCourseCategoryUrl(
  370. 1,
  371. $limit['length'],
  372. $categoryCode,
  373. $hiddenLinks,
  374. $action
  375. );
  376. if ($code == $categoryCode) {
  377. $html .= '<strong>';
  378. $html .= "$categoryName ($categoryCourses)";
  379. $html .= '</strong>';
  380. } else {
  381. if (!empty($categoryCourses)) {
  382. $html .= '<a href="'.$categoryLink.'">';
  383. $html .= "$categoryName ($categoryCourses)";
  384. $html .= '</a>';
  385. } else {
  386. $html .= "$categoryName ($categoryCourses)";
  387. }
  388. }
  389. if (!empty($categories[$categoryCode])) {
  390. $html .= '<ul class="nav nav-list">';
  391. foreach ($categories[$categoryCode] as $subCategory1) {
  392. $subCategory1Name = $subCategory1['name'];
  393. $subCategory1Code = $subCategory1['code'];
  394. $subCategory1Courses = $subCategory1['count_courses'];
  395. $html .= '<li>';
  396. if ($code == $subCategory1Code) {
  397. $html .= "<strong>$subCategory1Name ($subCategory1Courses)</strong>";
  398. } else {
  399. $html .= '<a href="'.$categoryLink.'">';
  400. $html .= "$subCategory1Name ($subCategory1Courses)";
  401. $html .= '</a>';
  402. }
  403. if (!empty($categories[$subCategory1Code])) {
  404. $html .= '<ul class="nav nav-list">';
  405. foreach ($categories[$subCategory1Code] as $subCategory2) {
  406. $subCategory2Name = $subCategory2['name'];
  407. $subCategory2Code = $subCategory2['code'];
  408. $subCategory2Courses = $subCategory2['count_courses'];
  409. $html .= '<li>';
  410. if ($code == $subCategory2Code) {
  411. $html .= "<strong>$subCategory2Name ($subCategory2Courses)</strong>";
  412. } else {
  413. $html .= '<a href="'.$categoryLink.'">';
  414. $html .= "$subCategory2Name ($subCategory2Courses)";
  415. $html .= '</a>';
  416. }
  417. if (!empty($categories[$subCategory2Code])) {
  418. $html .= '<ul class="nav nav-list">';
  419. foreach ($categories[$subCategory2Code] as $subCategory3) {
  420. $subCategory3Name = $subCategory3['name'];
  421. $subCategory3Code = $subCategory3['code'];
  422. $subCategory3Courses = $subCategory3['count_courses'];
  423. $html .= '<li>';
  424. if ($code == $subCategory3Code) {
  425. $html .= "<strong>$subCategory3Name ($subCategory3Courses)</strong>";
  426. } else {
  427. $html .= '<a href="'.$categoryLink.'">';
  428. $html .= "$subCategory3Name ($subCategory3Courses)";
  429. $html .= '</a>';
  430. }
  431. $html .= '</li>';
  432. }
  433. $html .= '</ul>';
  434. }
  435. $html .= '</li>';
  436. }
  437. $html .= '</ul>';
  438. }
  439. $html .= '</li>';
  440. }
  441. $html .= '</ul>';
  442. }
  443. $html .= '</li>';
  444. }
  445. }
  446. return $html;
  447. }
  448. /**
  449. * Get a HTML button for subscribe to session.
  450. *
  451. * @param int $sessionId The session ID
  452. * @param string $sessionName The session name
  453. * @param bool $checkRequirements Optional.
  454. * Whether the session has requirement. Default is false
  455. * @param bool $includeText Optional. Whether show the text in button
  456. * @param bool $btnBing
  457. *
  458. * @return string The button HTML
  459. */
  460. public function getRegisteredInSessionButton(
  461. $sessionId,
  462. $sessionName,
  463. $checkRequirements = false,
  464. $includeText = false,
  465. $btnBing = false
  466. ) {
  467. if ($btnBing) {
  468. $btnBing = 'btn-lg btn-block';
  469. } else {
  470. $btnBing = 'btn-sm';
  471. }
  472. if ($checkRequirements) {
  473. $url = api_get_path(WEB_AJAX_PATH);
  474. $url .= 'sequence.ajax.php?';
  475. $url .= http_build_query([
  476. 'a' => 'get_requirements',
  477. 'id' => intval($sessionId),
  478. 'type' => SequenceResource::SESSION_TYPE,
  479. ]);
  480. return Display::toolbarButton(
  481. get_lang('CheckRequirements'),
  482. $url,
  483. 'shield',
  484. 'info',
  485. [
  486. 'class' => $btnBing.' ajax',
  487. 'data-title' => get_lang('CheckRequirements'),
  488. 'data-size' => 'md',
  489. 'title' => get_lang('CheckRequirements'),
  490. ],
  491. $includeText
  492. );
  493. }
  494. $catalogSessionAutoSubscriptionAllowed = false;
  495. if (api_get_setting('catalog_allow_session_auto_subscription') === 'true') {
  496. $catalogSessionAutoSubscriptionAllowed = true;
  497. }
  498. $url = api_get_path(WEB_CODE_PATH);
  499. if ($catalogSessionAutoSubscriptionAllowed) {
  500. $url .= 'auth/courses.php?';
  501. $url .= http_build_query([
  502. 'action' => 'subscribe_to_session',
  503. 'session_id' => intval($sessionId),
  504. ]);
  505. $result = Display::toolbarButton(
  506. get_lang('Subscribe'),
  507. $url,
  508. 'pencil',
  509. 'primary',
  510. [
  511. 'class' => $btnBing.' ajax',
  512. 'data-title' => get_lang('AreYouSureToSubscribe'),
  513. 'data-size' => 'md',
  514. 'title' => get_lang('Subscribe'),
  515. ],
  516. $includeText
  517. );
  518. } else {
  519. $url .= 'inc/email_editor.php?';
  520. $url .= http_build_query([
  521. 'action' => 'subscribe_me_to_session',
  522. 'session' => Security::remove_XSS($sessionName),
  523. ]);
  524. $result = Display::toolbarButton(
  525. get_lang('SubscribeToSessionRequest'),
  526. $url,
  527. 'pencil',
  528. 'primary',
  529. ['class' => $btnBing],
  530. $includeText
  531. );
  532. }
  533. $hook = HookResubscribe::create();
  534. if (!empty($hook)) {
  535. $hook->setEventData([
  536. 'session_id' => intval($sessionId),
  537. ]);
  538. try {
  539. $hook->notifyResubscribe(HOOK_EVENT_TYPE_PRE);
  540. } catch (Exception $exception) {
  541. $result = $exception->getMessage();
  542. }
  543. }
  544. return $result;
  545. }
  546. /**
  547. * Generate a label if the user has been registered in session.
  548. *
  549. * @return string The label
  550. */
  551. public function getAlreadyRegisteredInSessionLabel()
  552. {
  553. $icon = '<em class="fa fa-graduation-cap"></em>';
  554. return Display::div(
  555. $icon,
  556. [
  557. 'class' => 'btn btn-default btn-sm registered',
  558. 'title' => get_lang("AlreadyRegisteredToSession"),
  559. ]
  560. );
  561. }
  562. /**
  563. * Get a icon for a session.
  564. *
  565. * @param string $sessionName The session name
  566. *
  567. * @return string The icon
  568. */
  569. public function getSessionIcon($sessionName)
  570. {
  571. return Display::return_icon(
  572. 'window_list.png',
  573. $sessionName,
  574. null,
  575. ICON_SIZE_MEDIUM
  576. );
  577. }
  578. /**
  579. * Return Session Catalogue rendered view.
  580. *
  581. * @param string $action
  582. * @param string $nameTools
  583. * @param array $limit
  584. */
  585. public function sessionsList($action, $nameTools, $limit = [])
  586. {
  587. $date = isset($_POST['date']) ? $_POST['date'] : date('Y-m-d');
  588. $hiddenLinks = isset($_GET['hidden_links']) ? intval($_GET['hidden_links']) == 1 : false;
  589. $limit = isset($limit) ? $limit : self::getLimitArray();
  590. $countSessions = SessionManager::countSessionsByEndDate($date);
  591. $sessions = CoursesAndSessionsCatalog::browseSessions($date, $limit);
  592. $pageTotal = intval(intval($countSessions) / $limit['length']);
  593. // Do NOT show pagination if only one page or less
  594. $cataloguePagination = $pageTotal > 1 ? CourseCategory::getCatalogPagination($limit['current'], $limit['length'], $pageTotal) : '';
  595. $sessionsBlocks = $this->getFormattedSessionsBlock($sessions);
  596. // Get session search catalogue URL
  597. $courseUrl = CourseCategory::getCourseCategoryUrl(
  598. 1,
  599. $limit['length'],
  600. null,
  601. 0,
  602. 'subscribe'
  603. );
  604. $tpl = new Template();
  605. $tpl->assign('show_courses', CoursesAndSessionsCatalog::showCourses());
  606. $tpl->assign('show_sessions', CoursesAndSessionsCatalog::showSessions());
  607. $tpl->assign('show_tutor', api_get_setting('show_session_coach') === 'true' ? true : false);
  608. $tpl->assign('course_url', $courseUrl);
  609. $tpl->assign('catalog_pagination', $cataloguePagination);
  610. $tpl->assign('hidden_links', $hiddenLinks);
  611. $tpl->assign('search_token', Security::get_token());
  612. $tpl->assign('search_date', $date);
  613. $tpl->assign('web_session_courses_ajax_url', api_get_path(WEB_AJAX_PATH).'course.ajax.php');
  614. $tpl->assign('sessions', $sessionsBlocks);
  615. $tpl->assign('already_subscribed_label', $this->getAlreadyRegisteredInSessionLabel());
  616. $contentTemplate = $tpl->get_template('auth/session_catalog.tpl');
  617. $tpl->display($contentTemplate);
  618. }
  619. /**
  620. * Show the Session Catalogue with filtered session by course tags.
  621. *
  622. * @param array $limit Limit info
  623. */
  624. public function sessionsListByCoursesTag(array $limit)
  625. {
  626. $searchTag = isset($_POST['search_tag']) ? $_POST['search_tag'] : null;
  627. $searchDate = isset($_POST['date']) ? $_POST['date'] : date('Y-m-d');
  628. $hiddenLinks = isset($_GET['hidden_links']) ? intval($_GET['hidden_links']) == 1 : false;
  629. $courseUrl = CourseCategory::getCourseCategoryUrl(
  630. 1,
  631. $limit['length'],
  632. null,
  633. 0,
  634. 'subscribe'
  635. );
  636. $sessions = CoursesAndSessionsCatalog::browseSessionsByTags($searchTag, $limit);
  637. $sessionsBlocks = $this->getFormattedSessionsBlock($sessions);
  638. $tpl = new Template();
  639. $tpl->assign('show_courses', CoursesAndSessionsCatalog::showCourses());
  640. $tpl->assign('show_sessions', CoursesAndSessionsCatalog::showSessions());
  641. $tpl->assign('show_tutor', (api_get_setting('show_session_coach') === 'true' ? true : false));
  642. $tpl->assign('course_url', $courseUrl);
  643. $tpl->assign('already_subscribed_label', $this->getAlreadyRegisteredInSessionLabel());
  644. $tpl->assign('hidden_links', $hiddenLinks);
  645. $tpl->assign('search_token', Security::get_token());
  646. $tpl->assign('search_date', Security::remove_XSS($searchDate));
  647. $tpl->assign('search_tag', Security::remove_XSS($searchTag));
  648. $tpl->assign('sessions', $sessionsBlocks);
  649. $contentTemplate = $tpl->get_template('auth/session_catalog.tpl');
  650. $tpl->display($contentTemplate);
  651. }
  652. /**
  653. * Show the Session Catalogue with filtered session by a query term.
  654. *
  655. * @param array $limit
  656. */
  657. public function sessionListBySearch(array $limit)
  658. {
  659. $q = isset($_REQUEST['q']) ? Security::remove_XSS($_REQUEST['q']) : null;
  660. $hiddenLinks = isset($_GET['hidden_links']) ? (int) $_GET['hidden_links'] == 1 : false;
  661. $courseUrl = CourseCategory::getCourseCategoryUrl(
  662. 1,
  663. $limit['length'],
  664. null,
  665. 0,
  666. 'subscribe'
  667. );
  668. $searchDate = isset($_POST['date']) ? $_POST['date'] : date('Y-m-d');
  669. $sessions = CoursesAndSessionsCatalog::browseSessionsBySearch($q, $limit);
  670. $sessionsBlocks = $this->getFormattedSessionsBlock($sessions);
  671. $tpl = new Template();
  672. $tpl->assign('show_courses', CoursesAndSessionsCatalog::showCourses());
  673. $tpl->assign('show_sessions', CoursesAndSessionsCatalog::showSessions());
  674. $tpl->assign('show_tutor', api_get_setting('show_session_coach') === 'true' ? true : false);
  675. $tpl->assign('course_url', $courseUrl);
  676. $tpl->assign('already_subscribed_label', $this->getAlreadyRegisteredInSessionLabel());
  677. $tpl->assign('hidden_links', $hiddenLinks);
  678. $tpl->assign('search_token', Security::get_token());
  679. $tpl->assign('search_date', Security::remove_XSS($searchDate));
  680. $tpl->assign('search_tag', Security::remove_XSS($q));
  681. $tpl->assign('sessions', $sessionsBlocks);
  682. $contentTemplate = $tpl->get_template('auth/session_catalog.tpl');
  683. $tpl->display($contentTemplate);
  684. }
  685. /**
  686. * @return array
  687. */
  688. public static function getLimitArray()
  689. {
  690. $pageCurrent = isset($_REQUEST['pageCurrent']) ? (int) $_GET['pageCurrent'] : 1;
  691. $pageLength = isset($_REQUEST['pageLength']) ? (int) $_GET['pageLength'] : CoursesAndSessionsCatalog::PAGE_LENGTH;
  692. return [
  693. 'start' => ($pageCurrent - 1) * $pageLength,
  694. 'current' => $pageCurrent,
  695. 'length' => $pageLength,
  696. ];
  697. }
  698. /**
  699. * Get the formatted data for sessions block to be displayed on Session Catalog page.
  700. *
  701. * @param array $sessions The session list
  702. *
  703. * @return array
  704. */
  705. private function getFormattedSessionsBlock(array $sessions)
  706. {
  707. $extraFieldValue = new ExtraFieldValue('session');
  708. $userId = api_get_user_id();
  709. $sessionsBlocks = [];
  710. $entityManager = Database::getManager();
  711. $sessionRelCourseRepo = $entityManager->getRepository('ChamiloCoreBundle:SessionRelCourse');
  712. $extraFieldRepo = $entityManager->getRepository('ChamiloCoreBundle:ExtraField');
  713. $extraFieldRelTagRepo = $entityManager->getRepository('ChamiloCoreBundle:ExtraFieldRelTag');
  714. $tagsField = $extraFieldRepo->findOneBy([
  715. 'extraFieldType' => Chamilo\CoreBundle\Entity\ExtraField::COURSE_FIELD_TYPE,
  716. 'variable' => 'tags',
  717. ]);
  718. /** @var \Chamilo\CoreBundle\Entity\Session $session */
  719. foreach ($sessions as $session) {
  720. $sessionDates = SessionManager::parseSessionDates([
  721. 'display_start_date' => $session->getDisplayStartDate(),
  722. 'display_end_date' => $session->getDisplayEndDate(),
  723. 'access_start_date' => $session->getAccessStartDate(),
  724. 'access_end_date' => $session->getAccessEndDate(),
  725. 'coach_access_start_date' => $session->getCoachAccessStartDate(),
  726. 'coach_access_end_date' => $session->getCoachAccessEndDate(),
  727. ]);
  728. $imageField = $extraFieldValue->get_values_by_handler_and_field_variable(
  729. $session->getId(),
  730. 'image'
  731. );
  732. $sessionCourseTags = [];
  733. if (!is_null($tagsField)) {
  734. $sessionRelCourses = $sessionRelCourseRepo->findBy([
  735. 'session' => $session,
  736. ]);
  737. /** @var SessionRelCourse $sessionRelCourse */
  738. foreach ($sessionRelCourses as $sessionRelCourse) {
  739. $courseTags = $extraFieldRelTagRepo->getTags(
  740. $tagsField,
  741. $sessionRelCourse->getCourse()->getId()
  742. );
  743. /** @var Tag $tag */
  744. foreach ($courseTags as $tag) {
  745. $sessionCourseTags[] = $tag->getTag();
  746. }
  747. }
  748. }
  749. if (!empty($sessionCourseTags)) {
  750. $sessionCourseTags = array_unique($sessionCourseTags);
  751. }
  752. /** @var SequenceRepository $repo */
  753. $repo = $entityManager->getRepository('ChamiloCoreBundle:SequenceResource');
  754. $sequences = $repo->getRequirementsAndDependenciesWithinSequences(
  755. $session->getId(),
  756. SequenceResource::SESSION_TYPE
  757. );
  758. $hasRequirements = false;
  759. foreach ($sequences['sequences'] as $sequence) {
  760. if (count($sequence['requirements']) === 0) {
  761. continue;
  762. }
  763. $hasRequirements = true;
  764. break;
  765. }
  766. $cat = $session->getCategory();
  767. if (empty($cat)) {
  768. $cat = null;
  769. $catName = '';
  770. } else {
  771. $catName = $cat->getName();
  772. }
  773. $generalCoach = $session->getGeneralCoach();
  774. $coachId = $generalCoach ? $generalCoach->getId() : 0;
  775. $coachName = $generalCoach ? $session->getGeneralCoach()->getCompleteName() : '';
  776. $actions = null;
  777. if (api_is_platform_admin()) {
  778. $actions = api_get_path(WEB_CODE_PATH).'session/resume_session.php?id_session='.$session->getId();
  779. }
  780. $plugin = \BuyCoursesPlugin::create();
  781. $isThisSessionOnSale = $plugin->getBuyCoursePluginPrice($session);
  782. $sessionsBlock = [
  783. 'id' => $session->getId(),
  784. 'name' => $session->getName(),
  785. 'image' => isset($imageField['value']) ? $imageField['value'] : null,
  786. 'nbr_courses' => $session->getNbrCourses(),
  787. 'nbr_users' => $session->getNbrUsers(),
  788. 'coach_id' => $coachId,
  789. 'coach_url' => $generalCoach
  790. ? api_get_path(WEB_AJAX_PATH).'user_manager.ajax.php?a=get_user_popup&user_id='.$coachId
  791. : '',
  792. 'coach_name' => $coachName,
  793. 'coach_avatar' => UserManager::getUserPicture(
  794. $coachId,
  795. USER_IMAGE_SIZE_SMALL
  796. ),
  797. 'is_subscribed' => SessionManager::isUserSubscribedAsStudent(
  798. $session->getId(),
  799. $userId
  800. ),
  801. 'icon' => $this->getSessionIcon($session->getName()),
  802. 'date' => $sessionDates['display'],
  803. 'price' => !empty($isThisSessionOnSale['html']) ? $isThisSessionOnSale['html'] : '',
  804. 'subscribe_button' => isset($isThisSessionOnSale['buy_button']) ? $isThisSessionOnSale['buy_button'] : $this->getRegisteredInSessionButton(
  805. $session->getId(),
  806. $session->getName(),
  807. $hasRequirements
  808. ),
  809. 'show_description' => $session->getShowDescription(),
  810. 'description' => $session->getDescription(),
  811. 'category' => $catName,
  812. 'tags' => $sessionCourseTags,
  813. 'edit_actions' => $actions,
  814. 'duration' => SessionManager::getDayLeftInSession(
  815. ['id' => $session->getId(), 'duration' => $session->getDuration()],
  816. $userId
  817. ),
  818. ];
  819. $sessionsBlock = array_merge($sessionsBlock, $sequences);
  820. $sessionsBlocks[] = $sessionsBlock;
  821. }
  822. return $sessionsBlocks;
  823. }
  824. }