courses_controller.php 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808
  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. // The course must be open in order to access the auto subscription
  177. if (in_array(
  178. $courseInfo['visibility'],
  179. array(COURSE_VISIBILITY_CLOSED, COURSE_VISIBILITY_REGISTERED, COURSE_VISIBILITY_HIDDEN))
  180. ) {
  181. $error = get_lang('SubscribingNotAllowed');
  182. } else {
  183. $result = $this->model->subscribe_user($course_code);
  184. if (!$result) {
  185. $error = get_lang('CourseRegistrationCodeIncorrect');
  186. } else {
  187. // Redirect directly to the course after subscription
  188. $message = $result['message'];
  189. $content = $result['content'];
  190. }
  191. }
  192. if (!empty($search_term)) {
  193. $this->search_courses($search_term, $message, $error, $content);
  194. } else {
  195. $this->courses_categories('subscribe', $category_code, $message, $error, $content);
  196. }
  197. return $result;
  198. }
  199. /**
  200. * Create a category
  201. * render to listing view
  202. * @param string Category title
  203. */
  204. public function add_course_category($category_title)
  205. {
  206. $result = $this->model->store_course_category($category_title);
  207. if ($result) {
  208. Display::addFlash(Display::return_message(get_lang('CourseCategoryStored')));
  209. } else {
  210. Display::addFlash(Display::return_message(get_lang('ACourseCategoryWithThisNameAlreadyExists'), 'error'));
  211. }
  212. $action = 'sortmycourses';
  213. $this->courses_list($action);
  214. }
  215. /**
  216. * Change course category
  217. * render to listing view
  218. * @param string Course code
  219. * @param int Category id
  220. */
  221. public function change_course_category($course_code, $category_id)
  222. {
  223. $courseInfo = api_get_course_info($course_code);
  224. $courseId = $courseInfo['real_id'];
  225. $result = $this->model->updateCourseCategory($courseId, $category_id);
  226. if ($result) {
  227. Display::addFlash(Display::return_message(get_lang('EditCourseCategorySucces')));
  228. }
  229. $action = 'sortmycourses';
  230. $this->courses_list($action);
  231. }
  232. /**
  233. * Move up/down courses inside a category
  234. * render to listing view
  235. * @param string move to up or down
  236. * @param string Course code
  237. * @param int Category id
  238. */
  239. public function move_course($move, $course_code, $category_id)
  240. {
  241. $result = $this->model->move_course($move, $course_code, $category_id);
  242. if ($result) {
  243. Display::addFlash(Display::return_message(get_lang('CourseSortingDone')));
  244. }
  245. $action = 'sortmycourses';
  246. $this->courses_list($action);
  247. }
  248. /**
  249. * Move up/down categories
  250. * render to listing view
  251. * @param string move to up or down
  252. * @param int Category id
  253. */
  254. public function move_category($move, $category_id)
  255. {
  256. $result = $this->model->move_category($move, $category_id);
  257. if ($result) {
  258. Display::addFlash(Display::return_message(get_lang('CategorySortingDone')));
  259. }
  260. $action = 'sortmycourses';
  261. $this->courses_list($action);
  262. }
  263. /**
  264. * Edit course category
  265. * render to listing view
  266. * @param string Category title
  267. * @param int Category id
  268. */
  269. public function edit_course_category($title, $category)
  270. {
  271. $result = $this->model->store_edit_course_category($title, $category);
  272. if ($result) {
  273. Display::addFlash(Display::return_message(get_lang('CourseCategoryEditStored')));
  274. }
  275. $action = 'sortmycourses';
  276. $this->courses_list($action);
  277. }
  278. /**
  279. * Delete a course category
  280. * render to listing view
  281. * @param int Category id
  282. */
  283. public function delete_course_category($category_id)
  284. {
  285. $result = $this->model->delete_course_category($category_id);
  286. if ($result) {
  287. Display::addFlash(Display::return_message(get_lang('CourseCategoryDeleted')));
  288. }
  289. $action = 'sortmycourses';
  290. $this->courses_list($action);
  291. }
  292. /**
  293. * Unsubscribe user from a course
  294. * render to listing view
  295. * @param string Course code
  296. */
  297. public function unsubscribe_user_from_course($course_code, $search_term = null, $category_code = null)
  298. {
  299. $result = $this->model->remove_user_from_course($course_code);
  300. $message = '';
  301. $error = '';
  302. if ($result) {
  303. Display::addFlash(Display::return_message(get_lang('YouAreNowUnsubscribed')));
  304. }
  305. $action = 'sortmycourses';
  306. if (!empty($search_term)) {
  307. $this->search_courses($search_term, $message, $error);
  308. } else {
  309. $this->courses_categories('subcribe', $category_code, $message, $error);
  310. }
  311. }
  312. /**
  313. * Get the html block for courses categories
  314. * @param string $code Current category code
  315. * @param boolean $hiddenLinks Whether hidden links
  316. * @param array $limit
  317. * @return string The HTML block
  318. */
  319. public function getCoursesCategoriesBlock($code = null, $hiddenLinks = false, $limit = null)
  320. {
  321. $categories = $this->model->browse_course_categories();
  322. $html = '';
  323. if (!empty($categories)) {
  324. $action = 'display_courses';
  325. foreach ($categories[0] as $category) {
  326. $categoryName = $category['name'];
  327. $categoryCode = $category['code'];
  328. $categoryCourses = $category['count_courses'];
  329. $html .= '<li>';
  330. if ($code == $categoryCode) {
  331. $html .= '<strong>';
  332. $html .= "$categoryName ($categoryCourses)";
  333. $html .= '</strong>';
  334. } else {
  335. if (!empty($categoryCourses)) {
  336. $html .= '<a href="' . getCourseCategoryUrl(
  337. 1,
  338. $limit['length'],
  339. $categoryCode,
  340. $hiddenLinks,
  341. $action
  342. ) . '">';
  343. $html .= "$categoryName ($categoryCourses)";
  344. $html .= '</a>';
  345. } else {
  346. $html .= "$categoryName ($categoryCourses)";
  347. }
  348. }
  349. if (!empty($categories[$categoryCode])) {
  350. $html .= '<ul class="nav nav-list">';
  351. foreach ($categories[$categoryCode] as $subCategory1) {
  352. $subCategory1Name = $subCategory1['name'];
  353. $subCategory1Code = $subCategory1['code'];
  354. $subCategory1Courses = $subCategory1['count_courses'];
  355. $html .= '<li>';
  356. if ($code == $subCategory1Code) {
  357. $html .= "<strong>$subCategory1Name ($subCategory1Courses)</strong>";
  358. } else {
  359. $html .= '<a href="' . getCourseCategoryUrl(
  360. 1,
  361. $limit['length'],
  362. $categoryCode,
  363. $hiddenLinks,
  364. $action
  365. ) . '">';
  366. $html .= "$subCategory1Name ($subCategory1Courses)";
  367. $html .= '</a>';
  368. }
  369. if (!empty($categories[$subCategory1Code])) {
  370. $html .= '<ul class="nav nav-list">';
  371. foreach ($categories[$subCategory1Code] as $subCategory2) {
  372. $subCategory2Name = $subCategory2['name'];
  373. $subCategory2Code = $subCategory2['code'];
  374. $subCategory2Courses = $subCategory2['count_courses'];
  375. $html .= '<li>';
  376. if ($code == $subCategory2Code) {
  377. $html .= "<strong>$subCategory2Name ($subCategory2Courses)</strong>";
  378. } else {
  379. $html .= '<a href="' . getCourseCategoryUrl(
  380. 1,
  381. $limit['length'],
  382. $categoryCode,
  383. $hiddenLinks,
  384. $action
  385. ) . '">';
  386. $html .= "$subCategory2Name ($subCategory2Courses)";
  387. $html .= '</a>';
  388. }
  389. if (!empty($categories[$subCategory2Code])) {
  390. $html .= '<ul class="nav nav-list">';
  391. foreach ($categories[$subCategory2Code] as $subCategory3) {
  392. $subCategory3Name = $subCategory3['name'];
  393. $subCategory3Code = $subCategory3['code'];
  394. $subCategory3Courses = $subCategory3['count_courses'];
  395. $html .= '<li>';
  396. if ($code == $subCategory3Code) {
  397. $html .= "<strong>$subCategory3Name ($subCategory3Courses)</strong>";
  398. } else {
  399. $html .= '<a href="' . getCourseCategoryUrl(
  400. 1,
  401. $limit['length'],
  402. $categoryCode,
  403. $hiddenLinks,
  404. $action
  405. ) . '">';
  406. $html .= "$subCategory3Name ($subCategory3Courses)";
  407. $html .= '</a>';
  408. }
  409. $html .= '</li>';
  410. }
  411. $html .= '</ul>';
  412. }
  413. $html .= '</li>';
  414. }
  415. $html .= '</ul>';
  416. }
  417. $html .= '</li>';
  418. }
  419. $html .= '</ul>';
  420. }
  421. $html .= '</li>';
  422. }
  423. }
  424. return $html;
  425. }
  426. /**
  427. * Get a HTML button for subscribe to session
  428. * @param int $sessionId The session ID
  429. * @param string $sessionName The session name
  430. * @param boolean $checkRequirements Optional.
  431. * Whether the session has requirement. Default is false
  432. * @return string The button HTML
  433. */
  434. public function getRegisteredInSessionButton(
  435. $sessionId,
  436. $sessionName,
  437. $checkRequirements = false
  438. )
  439. {
  440. if ($checkRequirements) {
  441. $url = api_get_path(WEB_AJAX_PATH);
  442. $url .= 'sequence.ajax.php?';
  443. $url .= http_build_query([
  444. 'a' => 'get_requirements',
  445. 'id' => intval($sessionId),
  446. 'type' => SequenceResource::SESSION_TYPE,
  447. ]);
  448. return Display::toolbarButton(
  449. get_lang('CheckRequirements'),
  450. $url,
  451. 'check-circle',
  452. 'primary',
  453. [
  454. 'class' => 'btn-lg btn-block ajax',
  455. 'data-title' => get_lang('CheckRequirements'),
  456. 'data-size' => 'md'
  457. ]
  458. );
  459. }
  460. $catalogSessionAutoSubscriptionAllowed = false;
  461. if (
  462. api_get_setting('catalog_allow_session_auto_subscription') === 'true'
  463. ) {
  464. $catalogSessionAutoSubscriptionAllowed = true;
  465. }
  466. $url = api_get_path(WEB_CODE_PATH);
  467. if ($catalogSessionAutoSubscriptionAllowed) {
  468. $url .= 'auth/courses.php?';
  469. $url .= http_build_query([
  470. 'action' => 'subscribe_to_session',
  471. 'session_id' => intval($sessionId)
  472. ]);
  473. $result = Display::toolbarButton(
  474. get_lang('Subscribe'),
  475. $url,
  476. 'check-circle',
  477. 'primary',
  478. [
  479. 'class' => 'btn-lg btn-block ajax',
  480. 'data-title' => get_lang('AreYouSureToSubscribe'),
  481. 'data-size' => 'md'
  482. ]
  483. );
  484. } else {
  485. $url .= 'inc/email_editor.php?';
  486. $url .= http_build_query([
  487. 'action' => 'subscribe_me_to_session',
  488. 'session' => Security::remove_XSS($sessionName),
  489. ]);
  490. $result = Display::toolbarButton(
  491. get_lang('Subscribe'),
  492. $url,
  493. 'check-circle',
  494. 'primary',
  495. ['class' => 'btn-lg btn-block']
  496. );
  497. }
  498. $hook = HookResubscribe::create();
  499. if (!empty($hook)) {
  500. $hook->setEventData(array(
  501. 'session_id' => intval($sessionId),
  502. ));
  503. try {
  504. $hook->notifyResubscribe(HOOK_EVENT_TYPE_PRE);
  505. } catch (Exception $exception) {
  506. $result = $exception->getMessage();
  507. }
  508. }
  509. return $result;
  510. }
  511. /**
  512. * Generate a label if the user has been registered in session
  513. * @return string The label
  514. */
  515. public function getAlreadyRegisteredInSessionLabel()
  516. {
  517. $icon = '<i class="fa fa-smile-o"></i>';
  518. return Display::div(
  519. $icon . ' ' . get_lang("AlreadyRegisteredToSession"),
  520. array('class' => 'info-catalog')
  521. );
  522. }
  523. /**
  524. * Get a icon for a session
  525. * @param string $sessionName The session name
  526. * @return string The icon
  527. */
  528. public function getSessionIcon($sessionName)
  529. {
  530. return Display::return_icon('window_list.png', $sessionName, null,ICON_SIZE_MEDIUM);
  531. }
  532. /**
  533. * Return Session Catalogue rendered view
  534. * @param string $action
  535. * @param string $nameTools
  536. * @param array $limit
  537. */
  538. public function sessionsList($action, $nameTools, $limit = array())
  539. {
  540. $date = isset($_POST['date']) ? $_POST['date'] : date('Y-m-d');
  541. $hiddenLinks = isset($_GET['hidden_links']) ? intval($_GET['hidden_links']) == 1 : false;
  542. $limit = isset($limit) ? $limit : getLimitArray();
  543. $countSessions = $this->model->countSessions($date);
  544. $sessions = $this->model->browseSessions($date, $limit);
  545. $pageTotal = intval(ceil(intval($countSessions) / $limit['length']));
  546. // Do NOT show pagination if only one page or less
  547. $cataloguePagination = $pageTotal > 1 ?
  548. getCataloguePagination($limit['current'], $limit['length'], $pageTotal) :
  549. '';
  550. $sessionsBlocks = $this->getFormatedSessionsBlock($sessions);
  551. // Get session list catalogue URL
  552. //$sessionUrl = getCourseCategoryUrl(1, $limit['length'], null, 0, 'display_sessions');
  553. // Get session search catalogue URL
  554. $courseUrl = getCourseCategoryUrl(1, $limit['length'], null, 0, 'subscribe');
  555. $tpl = new Template();
  556. $tpl->assign('show_courses', CoursesAndSessionsCatalog::showCourses());
  557. $tpl->assign('show_sessions', CoursesAndSessionsCatalog::showSessions());
  558. $tpl->assign('show_tutor', (api_get_setting('show_session_coach')==='true' ? true : false));
  559. $tpl->assign('course_url', $courseUrl);
  560. $tpl->assign('catalog_pagination', $cataloguePagination);
  561. $tpl->assign('hidden_links', $hiddenLinks);
  562. $tpl->assign('search_token', Security::get_token());
  563. $tpl->assign('search_date', $date);
  564. $tpl->assign('web_session_courses_ajax_url', api_get_path(WEB_AJAX_PATH) . 'course.ajax.php');
  565. $tpl->assign('sessions', $sessionsBlocks);
  566. $tpl->assign('already_subscribed_label', $this->getAlreadyRegisteredInSessionLabel());
  567. $contentTemplate = $tpl->get_template('auth/session_catalog.tpl');
  568. $tpl->display($contentTemplate);
  569. }
  570. /**
  571. * Show the Session Catalogue with filtered session by course tags
  572. * @param array $limit Limit info
  573. */
  574. public function sessionsListByCoursesTag(array $limit)
  575. {
  576. $searchTag = isset($_POST['search_tag']) ? $_POST['search_tag'] : null;
  577. $searchDate = isset($_POST['date']) ? $_POST['date'] : date('Y-m-d');
  578. $hiddenLinks = isset($_GET['hidden_links']) ? intval($_GET['hidden_links']) == 1 : false;
  579. $courseUrl = getCourseCategoryUrl(1, $limit['length'], null, 0, 'subscribe');
  580. $sessions = $this->model->browseSessionsByTags($searchTag, $limit);
  581. $sessionsBlocks = $this->getFormatedSessionsBlock($sessions);
  582. $tpl = new Template();
  583. $tpl->assign('show_courses', CoursesAndSessionsCatalog::showCourses());
  584. $tpl->assign('show_sessions', CoursesAndSessionsCatalog::showSessions());
  585. $tpl->assign('show_tutor', (api_get_setting('show_session_coach')==='true' ? true : false));
  586. $tpl->assign('course_url', $courseUrl);
  587. $tpl->assign('already_subscribed_label', $this->getAlreadyRegisteredInSessionLabel());
  588. $tpl->assign('hidden_links', $hiddenLinks);
  589. $tpl->assign('search_token', Security::get_token());
  590. $tpl->assign('search_date', Security::remove_XSS($searchDate));
  591. $tpl->assign('search_tag', Security::remove_XSS($searchTag));
  592. $tpl->assign('sessions', $sessionsBlocks);
  593. $contentTemplate = $tpl->get_template('auth/session_catalog.tpl');
  594. $tpl->display($contentTemplate);
  595. }
  596. /**
  597. * Show the Session Catalogue with filtered session by a query term
  598. * @param array $limit
  599. */
  600. public function sessionListBySearch(array $limit)
  601. {
  602. $q = isset($_REQUEST['q']) ? Security::remove_XSS($_REQUEST['q']) : null;
  603. $hiddenLinks = isset($_GET['hidden_links']) ? intval($_GET['hidden_links']) == 1 : false;
  604. $courseUrl = getCourseCategoryUrl(1, $limit['length'], null, 0, 'subscribe');
  605. $searchDate = isset($_POST['date']) ? $_POST['date'] : date('Y-m-d');
  606. $sessions = $this->model->browseSessionsBySearch($q, $limit);
  607. $sessionsBlocks = $this->getFormatedSessionsBlock($sessions);
  608. $tpl = new Template();
  609. $tpl->assign('show_courses', CoursesAndSessionsCatalog::showCourses());
  610. $tpl->assign('show_sessions', CoursesAndSessionsCatalog::showSessions());
  611. $tpl->assign('show_tutor', (api_get_setting('show_session_coach')==='true' ? true : false));
  612. $tpl->assign('course_url', $courseUrl);
  613. $tpl->assign('already_subscribed_label', $this->getAlreadyRegisteredInSessionLabel());
  614. $tpl->assign('hidden_links', $hiddenLinks);
  615. $tpl->assign('search_token', Security::get_token());
  616. $tpl->assign('search_date', Security::remove_XSS($searchDate));
  617. $tpl->assign('search_tag', Security::remove_XSS($q));
  618. $tpl->assign('sessions', $sessionsBlocks);
  619. $contentTemplate = $tpl->get_template('auth/session_catalog.tpl');
  620. $tpl->display($contentTemplate);
  621. }
  622. /**
  623. * Get the formated data for sessions block to be displayed on Session Catalog page
  624. * @param array $sessions The session list
  625. * @return array
  626. */
  627. private function getFormatedSessionsBlock(array $sessions)
  628. {
  629. $extraFieldValue = new ExtraFieldValue('session');
  630. $userId = api_get_user_id();
  631. $sessionsBlocks = [];
  632. $entityManager = Database::getManager();
  633. $sessionRelCourseRepo = $entityManager->getRepository('ChamiloCoreBundle:SessionRelCourse');
  634. $extraFieldRepo = $entityManager->getRepository('ChamiloCoreBundle:ExtraField');
  635. $extraFieldRelTagRepo = $entityManager->getRepository('ChamiloCoreBundle:ExtraFieldRelTag');
  636. $tagsField = $extraFieldRepo->findOneBy([
  637. 'extraFieldType' => Chamilo\CoreBundle\Entity\ExtraField::COURSE_FIELD_TYPE,
  638. 'variable' => 'tags',
  639. ]);
  640. /** @var \Chamilo\CoreBundle\Entity\Session $session */
  641. foreach ($sessions as $session) {
  642. $sessionDates = SessionManager::parseSessionDates([
  643. 'display_start_date' => $session->getDisplayStartDate(),
  644. 'display_end_date' => $session->getDisplayEndDate(),
  645. 'access_start_date' => $session->getAccessStartDate(),
  646. 'access_end_date' => $session->getAccessEndDate(),
  647. 'coach_access_start_date' => $session->getCoachAccessStartDate(),
  648. 'coach_access_end_date' => $session->getCoachAccessEndDate(),
  649. ]);
  650. $imageField = $extraFieldValue->get_values_by_handler_and_field_variable($session->getId(), 'image');
  651. $sessionCourseTags = [];
  652. if (!is_null($tagsField)) {
  653. $sessionRelCourses = $sessionRelCourseRepo->findBy([
  654. 'session' => $session,
  655. ]);
  656. foreach ($sessionRelCourses as $sessionRelCourse) {
  657. $courseTags = $extraFieldRelTagRepo->getTags(
  658. $tagsField,
  659. $sessionRelCourse->getCourse()->getId()
  660. );
  661. foreach ($courseTags as $tag) {
  662. $sessionCourseTags[] = $tag->getTag();
  663. }
  664. }
  665. }
  666. if (!empty($sessionCourseTags)) {
  667. $sessionCourseTags = array_unique($sessionCourseTags);
  668. }
  669. $repo = $entityManager->getRepository('ChamiloCoreBundle:SequenceResource');
  670. $sequences = $repo->getRequirementsAndDependenciesWithinSequences(
  671. $session->getId(),
  672. SequenceResource::SESSION_TYPE
  673. );
  674. $hasRequirements = false;
  675. foreach ($sequences['sequences'] as $sequence) {
  676. if (count($sequence['requirements']) === 0) {
  677. continue;
  678. }
  679. $hasRequirements = true;
  680. break;
  681. }
  682. $sessionsBlock = array(
  683. 'id' => $session->getId(),
  684. 'name' => $session->getName(),
  685. 'image' => isset($imageField['value']) ? $imageField['value'] : null,
  686. 'nbr_courses' => $session->getNbrCourses(),
  687. 'nbr_users' => $session->getNbrUsers(),
  688. 'coach_name' => $session->getGeneralCoach()->getCompleteName(),
  689. 'is_subscribed' => SessionManager::isUserSubscribedAsStudent($session->getId(), $userId),
  690. 'icon' => $this->getSessionIcon($session->getName()),
  691. 'date' => $sessionDates['display'],
  692. 'subscribe_button' => $this->getRegisteredInSessionButton(
  693. $session->getId(),
  694. $session->getName(),
  695. $hasRequirements
  696. ),
  697. 'show_description' => $session->getShowDescription(),
  698. 'tags' => $sessionCourseTags,
  699. );
  700. $sessionsBlock = array_merge($sessionsBlock, $sequences);
  701. $sessionsBlocks[] = $sessionsBlock;
  702. }
  703. return $sessionsBlocks;
  704. }
  705. }