courses_controller.php 33 KB

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