courses_controller.php 31 KB

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