courses_controller.php 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * Class CoursesController
  5. *
  6. * This file contains class used like controller, it should be included inside a dispatcher file (e.g: index.php)
  7. * @author Christian Fasanando <christian1827@gmail.com> - BeezNest
  8. * @package chamilo.auth
  9. */
  10. class CoursesController
  11. {
  12. private $toolname;
  13. private $view;
  14. private $model;
  15. /**
  16. * Constructor
  17. */
  18. public function __construct()
  19. {
  20. $this->toolname = 'auth';
  21. $actived_theme_path = api_get_template();
  22. $this->view = new View($this->toolname, $actived_theme_path);
  23. $this->model = new Auth();
  24. }
  25. /**
  26. * It's used for listing courses,
  27. * render to courses_list view
  28. * @param string action
  29. * @param string confirmation message(optional)
  30. */
  31. public function courses_list($action, $message = '')
  32. {
  33. $data = array();
  34. $user_id = api_get_user_id();
  35. $data['user_courses'] = $this->model->get_courses_of_user($user_id);
  36. $data['user_course_categories'] = $this->model->get_user_course_categories();
  37. $data['courses_in_category'] = $this->model->get_courses_in_category();
  38. $data['all_user_categories'] = $this->model->get_user_course_categories();
  39. $data['action'] = $action;
  40. $data['message'] = $message;
  41. // render to the view
  42. $this->view->set_data($data);
  43. $this->view->set_layout('layout');
  44. $this->view->set_template('courses_list');
  45. $this->view->render();
  46. }
  47. /**
  48. * It's used for listing categories,
  49. * render to categories_list view
  50. * @param string $action
  51. * @param string $message confirmation message(optional)
  52. * @param string $error error message(optional)
  53. */
  54. public function categories_list($action, $message='', $error='')
  55. {
  56. $data = array();
  57. $data['user_course_categories'] = $this->model->get_user_course_categories();
  58. $data['action'] = $action;
  59. $data['message'] = $message;
  60. $data['error'] = $error;
  61. // render to the view
  62. $this->view->set_data($data);
  63. $this->view->set_layout('layout');
  64. $this->view->set_template('categories_list');
  65. $this->view->render();
  66. }
  67. /**
  68. * It's used for listing courses with categories,
  69. * render to courses_categories view
  70. * @param $action
  71. * @param string $category_code
  72. * @param string $message
  73. * @param string $error
  74. * @param string $content
  75. * @param array $limit will be used if $random_value is not set.
  76. * This array should contains 'start' and 'length' keys
  77. * @internal param \action $string
  78. * @internal param \Category $string code (optional)
  79. */
  80. public function courses_categories($action, $category_code = null, $message = '', $error = '', $content = null, $limit = array())
  81. {
  82. $data = array();
  83. $browse_course_categories = $this->model->browse_course_categories();
  84. global $_configuration;
  85. $data['countCoursesInCategory'] = $this->model->count_courses_in_category($category_code);
  86. if ($action == 'display_random_courses') {
  87. // Random value is used instead limit filter
  88. $data['browse_courses_in_category'] = $this->model->browse_courses_in_category(null, 10);
  89. $data['countCoursesInCategory'] = count($data['browse_courses_in_category']);
  90. } else {
  91. if (!isset($category_code)) {
  92. $category_code = $browse_course_categories[0][1]['code']; // by default first category
  93. }
  94. $limit = isset($limit) ? $limit : getLimitArray();
  95. $data['browse_courses_in_category'] = $this->model->browse_courses_in_category($category_code, null, $limit);
  96. }
  97. $data['browse_course_categories'] = $browse_course_categories;
  98. $data['code'] = Security::remove_XSS($category_code);
  99. // getting all the courses to which the user is subscribed to
  100. $curr_user_id = api_get_user_id();
  101. $user_courses = $this->model->get_courses_of_user($curr_user_id);
  102. $user_coursecodes = array();
  103. // we need only the course codes as these will be used to match against the courses of the category
  104. if ($user_courses != '') {
  105. foreach($user_courses as $key => $value) {
  106. $user_coursecodes[] = $value['code'];
  107. }
  108. }
  109. if (api_is_drh()) {
  110. $courses = CourseManager::get_courses_followed_by_drh(api_get_user_id());
  111. foreach ($courses as $course) {
  112. $user_coursecodes[] = $course['code'];
  113. }
  114. }
  115. $data['user_coursecodes'] = $user_coursecodes;
  116. $data['action'] = $action;
  117. $data['message'] = $message;
  118. $data['content'] = $content;
  119. $data['error'] = $error;
  120. $data['catalogShowCoursesSessions'] = 0;
  121. if (isset($_configuration['catalog_show_courses_sessions'])) {
  122. $data['catalogShowCoursesSessions'] = $_configuration['catalog_show_courses_sessions'];
  123. }
  124. // render to the view
  125. $this->view->set_data($data);
  126. $this->view->set_layout('layout');
  127. $this->view->set_template('courses_categories');
  128. $this->view->render();
  129. }
  130. /**
  131. * @param string $search_term
  132. * @param string $message
  133. * @param string $error
  134. * @param string $content
  135. * @param $limit
  136. */
  137. public function search_courses($search_term, $message = '', $error = '', $content = null, $limit = array())
  138. {
  139. $data = array();
  140. $limit = !empty($limit) ? $limit : getLimitArray();
  141. $browse_course_categories = $this->model->browse_course_categories();
  142. $data['countCoursesInCategory'] = $this->model->count_courses_in_category('ALL', $search_term);
  143. $data['browse_courses_in_category'] = $this->model->search_courses($search_term, $limit);
  144. $data['browse_course_categories'] = $browse_course_categories;
  145. $data['search_term'] = Security::remove_XSS($search_term); //filter before showing in template
  146. // getting all the courses to which the user is subscribed to
  147. $curr_user_id = api_get_user_id();
  148. $user_courses = $this->model->get_courses_of_user($curr_user_id);
  149. $user_coursecodes = array();
  150. // we need only the course codes as these will be used to match against the courses of the category
  151. if ($user_courses != '') {
  152. foreach ($user_courses as $value) {
  153. $user_coursecodes[] = $value['code'];
  154. }
  155. }
  156. $data['user_coursecodes'] = $user_coursecodes;
  157. $data['message'] = $message;
  158. $data['content'] = $content;
  159. $data['error'] = $error;
  160. $data['action'] = 'display_courses';
  161. // render to the view
  162. $this->view->set_data($data);
  163. $this->view->set_layout('layout');
  164. $this->view->set_template('courses_categories');
  165. $this->view->render();
  166. }
  167. /**
  168. * Auto user subscription to a course
  169. */
  170. public function subscribe_user($course_code, $search_term, $category_code)
  171. {
  172. $courseInfo = api_get_course_info($course_code);
  173. // The course must be open in order to access the auto subscription
  174. if (in_array($courseInfo['visibility'], array(COURSE_VISIBILITY_CLOSED, COURSE_VISIBILITY_REGISTERED, COURSE_VISIBILITY_HIDDEN))) {
  175. $error = get_lang('SubscribingNotAllowed');
  176. //$message = get_lang('SubscribingNotAllowed');
  177. } else {
  178. $result = $this->model->subscribe_user($course_code);
  179. if (!$result) {
  180. $error = get_lang('CourseRegistrationCodeIncorrect');
  181. } else {
  182. // Redirect directly to the course after subscription
  183. $message = $result['message'];
  184. $content = $result['content'];
  185. }
  186. }
  187. if (!empty($search_term)) {
  188. $this->search_courses($search_term, $message, $error, $content);
  189. } else {
  190. $this->courses_categories('subscribe', $category_code, $message, $error, $content);
  191. }
  192. return $result;
  193. }
  194. /**
  195. * Create a category
  196. * render to listing view
  197. * @param string Category title
  198. */
  199. public function add_course_category($category_title)
  200. {
  201. $result = $this->model->store_course_category($category_title);
  202. $message = '';
  203. if ($result) {
  204. $message = get_lang("CourseCategoryStored");
  205. } else {
  206. $error = get_lang('ACourseCategoryWithThisNameAlreadyExists');
  207. }
  208. $action = 'sortmycourses';
  209. $this->courses_list($action, $message);
  210. }
  211. /**
  212. * Change course category
  213. * render to listing view
  214. * @param string Course code
  215. * @param int Category id
  216. */
  217. public function change_course_category($course_code, $category_id)
  218. {
  219. $result = $this->model->store_changecoursecategory($course_code, $category_id);
  220. $message = '';
  221. if ($result) {
  222. $message = get_lang('EditCourseCategorySucces');
  223. }
  224. $action = 'sortmycourses';
  225. $this->courses_list($action, $message);
  226. }
  227. /**
  228. * Move up/down courses inside a category
  229. * render to listing view
  230. * @param string move to up or down
  231. * @param string Course code
  232. * @param int Category id
  233. */
  234. public function move_course($move, $course_code, $category_id)
  235. {
  236. $result = $this->model->move_course($move, $course_code, $category_id);
  237. $message = '';
  238. if ($result) {
  239. $message = get_lang('CourseSortingDone');
  240. }
  241. $action = 'sortmycourses';
  242. $this->courses_list($action, $message);
  243. }
  244. /**
  245. * Move up/down categories
  246. * render to listing view
  247. * @param string move to up or down
  248. * @param int Category id
  249. */
  250. public function move_category($move, $category_id)
  251. {
  252. $result = $this->model->move_category($move, $category_id);
  253. $message = '';
  254. if ($result) {
  255. $message = get_lang('CategorySortingDone');
  256. }
  257. $action = 'sortmycourses';
  258. $this->courses_list($action, $message);
  259. }
  260. /**
  261. * Edit course category
  262. * render to listing view
  263. * @param string Category title
  264. * @param int Category id
  265. */
  266. public function edit_course_category($title, $category)
  267. {
  268. $result = $this->model->store_edit_course_category($title, $category);
  269. $message = '';
  270. if ($result) {
  271. $message = get_lang('CourseCategoryEditStored');
  272. }
  273. $action = 'sortmycourses';
  274. $this->courses_list($action, $message);
  275. }
  276. /**
  277. * Delete a course category
  278. * render to listing view
  279. * @param int Category id
  280. */
  281. public function delete_course_category($category_id)
  282. {
  283. $result = $this->model->delete_course_category($category_id);
  284. $message = '';
  285. if ($result) {
  286. $message = get_lang('CourseCategoryDeleted');
  287. }
  288. $action = 'sortmycourses';
  289. $this->courses_list($action, $message);
  290. }
  291. /**
  292. * Unsubscribe user from a course
  293. * render to listing view
  294. * @param string Course code
  295. */
  296. public function unsubscribe_user_from_course($course_code, $search_term = null, $category_code = null)
  297. {
  298. $result = $this->model->remove_user_from_course($course_code);
  299. $message = '';
  300. if ($result) {
  301. $message = get_lang('YouAreNowUnsubscribed');
  302. }
  303. $action = 'sortmycourses';
  304. if (!empty($search_term)) {
  305. $this->search_courses($search_term, $message, $error);
  306. } else {
  307. $this->courses_categories('subcribe', $category_code, $message, $error);
  308. }
  309. }
  310. /**
  311. * Get the html block for courses categories
  312. * @param string $code Current category code
  313. * @param boolean $hiddenLinks Whether hidden links
  314. * @param array $limit
  315. * @return string The HTML block
  316. */
  317. public function getCoursesCategoriesBlock($code = null, $hiddenLinks = false, $limit = null)
  318. {
  319. $categories = $this->model->browse_course_categories();
  320. $html = '';
  321. if (!empty($categories)) {
  322. $action = 'display_courses';
  323. foreach ($categories[0] as $category) {
  324. $categoryName = $category['name'];
  325. $categoryCode = $category['code'];
  326. $categoryCourses = $category['count_courses'];
  327. $html .= '<li>';
  328. if ($code == $categoryCode) {
  329. $html .= '<strong>';
  330. $html .= "$categoryName ($categoryCourses)";
  331. $html .= '</strong>';
  332. } else {
  333. if (!empty($categoryCourses)) {
  334. $html .= '<a href="' . getCourseCategoryUrl(
  335. 1,
  336. $limit['length'],
  337. $categoryCode,
  338. $hiddenLinks,
  339. $action
  340. ) . '">';
  341. $html .= "$categoryName ($categoryCourses)";
  342. $html .= '</a>';
  343. } else {
  344. $html .= "$categoryName ($categoryCourses)";
  345. }
  346. }
  347. if (!empty($categories[$categoryCode])) {
  348. $html .= '<ul class="nav nav-list">';
  349. foreach ($categories[$categoryCode] as $subCategory1) {
  350. $subCategory1Name = $subCategory1['name'];
  351. $subCategory1Code = $subCategory1['code'];
  352. $subCategory1Courses = $subCategory1['count_courses'];
  353. $html .= '<li>';
  354. if ($code == $subCategory1Code) {
  355. $html .= "<strong>$subCategory1Name ($subCategory1Courses)</strong>";
  356. } else {
  357. $html .= '<a href="' . getCourseCategoryUrl(
  358. 1,
  359. $limit['length'],
  360. $categoryCode,
  361. $hiddenLinks,
  362. $action
  363. ) . '">';
  364. $html .= "$subCategory1Name ($subCategory1Courses)";
  365. $html .= '</a>';
  366. }
  367. if (!empty($categories[$subCategory1Code])) {
  368. $html .= '<ul class="nav nav-list">';
  369. foreach ($categories[$subCategory1Code] as $subCategory2) {
  370. $subCategory2Name = $subCategory2['name'];
  371. $subCategory2Code = $subCategory2['code'];
  372. $subCategory2Courses = $subCategory2['count_courses'];
  373. $html .= '<li>';
  374. if ($code == $subCategory2Code) {
  375. $html .= "<strong>$subCategory2Name ($subCategory2Courses)</strong>";
  376. } else {
  377. $html .= '<a href="' . getCourseCategoryUrl(
  378. 1,
  379. $limit['length'],
  380. $categoryCode,
  381. $hiddenLinks,
  382. $action
  383. ) . '">';
  384. $html .= "$subCategory2Name ($subCategory2Courses)";
  385. $html .= '</a>';
  386. }
  387. if (!empty($categories[$subCategory2Code])) {
  388. $html .= '<ul class="nav nav-list">';
  389. foreach ($categories[$subCategory2Code] as $subCategory3) {
  390. $subCategory3Name = $subCategory3['name'];
  391. $subCategory3Code = $subCategory3['code'];
  392. $subCategory3Courses = $subCategory3['count_courses'];
  393. $html .= '<li>';
  394. if ($code == $subCategory3Code) {
  395. $html .= "<strong>$subCategory3Name ($subCategory3Courses)</strong>";
  396. } else {
  397. $html .= '<a href="' . getCourseCategoryUrl(
  398. 1,
  399. $limit['length'],
  400. $categoryCode,
  401. $hiddenLinks,
  402. $action
  403. ) . '">';
  404. $html .= "$subCategory3Name ($subCategory3Courses)";
  405. $html .= '</a>';
  406. }
  407. $html .= '</li>';
  408. }
  409. $html .= '</ul>';
  410. }
  411. $html .= '</li>';
  412. }
  413. $html .= '</ul>';
  414. }
  415. $html .= '</li>';
  416. }
  417. $html .= '</ul>';
  418. }
  419. $html .= '</li>';
  420. }
  421. }
  422. return $html;
  423. }
  424. /**
  425. * Get a HTML button for subscribe to session
  426. * @param string $sessionName The session name
  427. * @return string The button
  428. */
  429. public function getRegisterInSessionButton($sessionName)
  430. {
  431. $sessionName = urlencode($sessionName);
  432. $url = api_get_path(WEB_PATH) . "main/inc/email_editor.php?action=subscribe_me_to_session&session=$sessionName";
  433. return Display::url(get_lang('Subscribe'), $url, array(
  434. 'class' => 'btn btn-large btn-primary',
  435. ));
  436. }
  437. /**
  438. * Generate a label if the user has been registered in session
  439. * @return string The label
  440. */
  441. public function getAlreadyRegisterInSessionLabel()
  442. {
  443. $icon = Display::return_icon('students.gif', get_lang('Student'));
  444. return Display::label($icon . ' ' . get_lang("AlreadyRegisteredToSession"), "info");
  445. }
  446. /**
  447. * Get a icon for a session
  448. * @param string $sessionName The session name
  449. * @return string The icon
  450. */
  451. public function getSessionIcon($sessionName)
  452. {
  453. return Display::return_icon('window_list.png', $sessionName, null, ICON_SIZE_BIG);
  454. }
  455. /**
  456. * Return Session Catalogue rendered view
  457. * @param string $action
  458. * @param string $nameTools
  459. * @param array $limit
  460. */
  461. public function sessionsList($action, $nameTools, $limit = array())
  462. {
  463. $date = isset($_POST['date']) ? $_POST['date'] : date('Y-m-d');
  464. $hiddenLinks = isset($_GET['hidden_links']) ? intval($_GET['hidden_links']) == 1 : false;
  465. $limit = isset($limit) ? $limit : getLimitArray();
  466. $countSessions = $this->model->countSessions($date);
  467. $sessions = $this->model->browseSessions($date, $limit);
  468. $pageTotal = intval(ceil(intval($countSessions) / $limit['length']));
  469. // Do NOT show pagination if only one page or less
  470. $cataloguePagination = $pageTotal > 1 ?
  471. getCataloguePagination($limit['current'], $limit['length'], $pageTotal) :
  472. '';
  473. $sessionsBlocks = array();
  474. // Get session list catalogue URL
  475. $sessionUrl = getCourseCategoryUrl(1, $limit['length'], null, 0, 'display_sessions');
  476. // Get session search catalogue URL
  477. $courseUrl = getCourseCategoryUrl(1, $limit['length'], null, 0, 'subscribe');
  478. foreach ($sessions as $session) {
  479. $sessionsBlocks[] = array(
  480. 'id' => $session['id'],
  481. 'name' => $session['name'],
  482. 'nbr_courses' => $session['nbr_courses'],
  483. 'nbr_users' => $session['nbr_users'],
  484. 'coach_name' => $session['coach_name'],
  485. 'is_subscribed' => $session['is_subscribed'],
  486. 'icon' => $this->getSessionIcon($session['name']),
  487. 'date' => SessionManager::getSessionFormattedDate($session),
  488. 'subscribe_button' => $this->getRegisterInSessionButton($session['name'])
  489. );
  490. }
  491. $tpl = new Template();
  492. $tpl->assign('action', $action);
  493. $tpl->assign('showCourses', CoursesAndSessionsCatalog::showCourses());
  494. $tpl->assign('showSessions', CoursesAndSessionsCatalog::showSessions());
  495. $tpl->assign('api_get_self', api_get_self());
  496. $tpl->assign('sessionUrl', $sessionUrl);
  497. $tpl->assign('courseUrl', $courseUrl);
  498. $tpl->assign('nameTools', $nameTools);
  499. $tpl->assign('coursesCategoriesList', $this->getCoursesCategoriesBlock(null, false, $limit));
  500. $tpl->assign('cataloguePagination', $cataloguePagination);
  501. $tpl->assign('hiddenLinks', $hiddenLinks);
  502. $tpl->assign('searchToken', Security::get_token());
  503. $tpl->assign('searchDate', $date);
  504. $tpl->assign('web_session_courses_ajax_url', api_get_path(WEB_AJAX_PATH) . 'course.ajax.php');
  505. $tpl->assign('sessions_blocks', $sessionsBlocks);
  506. $tpl->assign('already_subscribed_label', $this->getAlreadyRegisterInSessionLabel());
  507. $contentTemplate = $tpl->get_template('auth/sessions_catalog.tpl');
  508. $tpl->display($contentTemplate);
  509. }
  510. }