courses_controller.php 33 KB

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