courses_controller.php 31 KB

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