courses_controller.php 33 KB

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