courses_controller.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * This file contains class used like controller, it should be included inside a dispatcher file (e.g: index.php)
  5. * @author Christian Fasanando <christian1827@gmail.com> - BeezNest
  6. * @package chamilo.auth
  7. */
  8. /**
  9. * Code
  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 AuthLib();
  26. }
  27. /**
  28. * It's used for listing courses,
  29. * render to courses_list view
  30. * @param string action
  31. * @param string confirmation message(optional)
  32. */
  33. public function courses_list($action, $message = '')
  34. {
  35. $data = array();
  36. $user_id = api_get_user_id();
  37. $data['user_courses'] = $this->model->get_courses_of_user($user_id);
  38. $data['user_course_categories'] = $this->model->get_user_course_categories();
  39. $data['courses_in_category'] = $this->model->get_courses_in_category();
  40. $data['all_user_categories'] = $this->model->get_user_course_categories();
  41. $data['action'] = $action;
  42. $data['message'] = $message;
  43. // render to the view
  44. $this->view->set_data($data);
  45. $this->view->set_layout('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 confirmation message(optional)
  54. * @param string 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('layout');
  66. $this->view->set_template('categories_list');
  67. $this->view->render();
  68. }
  69. /**
  70. * @return FormValidator
  71. */
  72. private function getSearchForm()
  73. {
  74. $form = new FormValidator('form-search', 'post', api_get_self().'?action=subscribe&amp;hidden_links=0', null, array('class' => 'form-search'));
  75. $form->addElement('hidden', 'search_course', '1');
  76. $form->addElement('text', 'search_term');
  77. $form->addElement('button', 'submit', get_lang('Search'));
  78. return $form;
  79. }
  80. /**
  81. * It's used for listing courses with categories,
  82. * render to courses_categories view
  83. * @param string action
  84. * @param string Category code (optional)
  85. */
  86. public function courses_categories($action, $category_code = null, $message = '', $error = '', $content = null)
  87. {
  88. $data = array();
  89. $browse_course_categories = $this->model->browse_course_categories();
  90. if ($action == 'display_random_courses') {
  91. $data['browse_courses_in_category'] = $this->model->browse_courses_in_category(null, 10);
  92. } else {
  93. if (!isset($category_code)) {
  94. $category_code = $browse_course_categories[0][1]['code']; // by default first category
  95. }
  96. $data['browse_courses_in_category'] = $this->model->browse_courses_in_category($category_code);
  97. }
  98. $data['search_form'] = $this->getSearchForm();
  99. $data['browse_course_categories'] = $browse_course_categories;
  100. $data['code'] = Security::remove_XSS($category_code);
  101. // getting all the courses to which the user is subscribed to
  102. $curr_user_id = api_get_user_id();
  103. $user_courses = $this->model->get_courses_of_user($curr_user_id);
  104. $user_coursecodes = array();
  105. // we need only the course codes as these will be used to match against the courses of the category
  106. if ($user_courses != '') {
  107. foreach($user_courses as $key => $value) {
  108. $user_coursecodes[] = $value['code'];
  109. }
  110. }
  111. if (api_is_drh()) {
  112. $courses = CourseManager::get_courses_followed_by_drh(api_get_user_id());
  113. foreach ($courses as $course) {
  114. $user_coursecodes[] = $course['code'];
  115. }
  116. }
  117. $data['user_coursecodes'] = $user_coursecodes;
  118. $data['action'] = $action;
  119. $data['message'] = $message;
  120. $data['content'] = $content;
  121. $data['error'] = $error;
  122. // render to the view
  123. $this->view->set_data($data);
  124. $this->view->set_layout('layout');
  125. $this->view->set_template('courses_categories');
  126. $this->view->render();
  127. }
  128. /**
  129. * Search courses
  130. */
  131. public function search_courses($search_term, $message = '', $error = '')
  132. {
  133. $data = array();
  134. $browse_course_categories = $this->model->browse_course_categories();
  135. $data['browse_courses_in_category'] = $this->model->search_courses($search_term);
  136. $data['browse_course_categories'] = $browse_course_categories;
  137. $data['search_form'] = $this->getSearchForm();
  138. $data['search_term'] = Security::remove_XSS($search_term); //filter before showing in template
  139. // getting all the courses to which the user is subscribed to
  140. $curr_user_id = api_get_user_id();
  141. $user_courses = $this->model->get_courses_of_user($curr_user_id);
  142. $user_coursecodes = array();
  143. // we need only the course codes as these will be used to match against the courses of the category
  144. if ($user_courses != '') {
  145. foreach ($user_courses as $key => $value) {
  146. $user_coursecodes[] = $value['code'];
  147. }
  148. }
  149. $data['user_coursecodes'] = $user_coursecodes;
  150. $data['message'] = $message;
  151. $data['error'] = $error;
  152. $data['action'] = 'display_courses';
  153. // render to the view
  154. $this->view->set_data($data);
  155. $this->view->set_layout('layout');
  156. $this->view->set_template('courses_categories');
  157. $this->view->render();
  158. }
  159. /**
  160. * Auto user subcription to a course
  161. */
  162. public function subscribe_user($course_code, $search_term, $category_code) {
  163. $data = array();
  164. $courseInfo = api_get_course_info($course_code);
  165. // The course must be open in order to access the auto subscription
  166. if (in_array($courseInfo['visibility'], array(COURSE_VISIBILITY_CLOSED, COURSE_VISIBILITY_REGISTERED, COURSE_VISIBILITY_HIDDEN))) {
  167. $error = get_lang('SubscribingNotAllowed');
  168. //$message = get_lang('SubscribingNotAllowed');
  169. } else {
  170. $result = $this->model->subscribe_user($course_code);
  171. if (!$result) {
  172. $error = get_lang('CourseRegistrationCodeIncorrect');
  173. } else {
  174. //Redirect directly to the course after subscription
  175. $message = $result['message'];
  176. $content = $result['content'];
  177. }
  178. }
  179. if (!empty($search_term)) {
  180. $this->search_courses($search_term, $message, $error);
  181. } else {
  182. $this->courses_categories('subcribe', $category_code, $message, $error, $content);
  183. }
  184. return $result;
  185. }
  186. /**
  187. * Create a category
  188. * render to listing view
  189. * @param string Category title
  190. */
  191. public function add_course_category($category_title) {
  192. $result = $this->model->store_course_category($category_title);
  193. $message = '';
  194. if ($result) { $message = get_lang("CourseCategoryStored"); }
  195. else { $error = get_lang('ACourseCategoryWithThisNameAlreadyExists');}
  196. $action = 'sortmycourses';
  197. $this->courses_list($action, $message);
  198. }
  199. /**
  200. * Change course category
  201. * render to listing view
  202. * @param string Course code
  203. * @param int Category id
  204. */
  205. public function change_course_category($courseId, $category_id) {
  206. $result = $this->model->store_changecoursecategory($courseId, $category_id);
  207. $message = '';
  208. if ($result) { $message = get_lang('EditCourseCategorySucces'); }
  209. $action = 'sortmycourses';
  210. $this->courses_list($action, $message);
  211. }
  212. /**
  213. * Move up/down courses inside a category
  214. * render to listing view
  215. * @param string move to up or down
  216. * @param string Course code
  217. * @param int Category id
  218. */
  219. public function move_course($move, $course_code, $category_id) {
  220. $result = $this->model->move_course($move, $course_code, $category_id);
  221. $message = '';
  222. if ($result) { $message = get_lang('CourseSortingDone'); }
  223. $action = 'sortmycourses';
  224. $this->courses_list($action, $message);
  225. }
  226. /**
  227. * Move up/down categories
  228. * render to listing view
  229. * @param string move to up or down
  230. * @param int Category id
  231. */
  232. public function move_category($move, $category_id) {
  233. $result = $this->model->move_category($move, $category_id);
  234. $message = '';
  235. if ($result) { $message = get_lang('CategorySortingDone'); }
  236. $action = 'sortmycourses';
  237. $this->courses_list($action, $message);
  238. }
  239. /**
  240. * Edit course category
  241. * render to listing view
  242. * @param string Category title
  243. * @param int Category id
  244. */
  245. public function edit_course_category($title, $category) {
  246. $result = $this->model->store_edit_course_category($title, $category);
  247. $message = '';
  248. if ($result) { $message = get_lang('CourseCategoryEditStored'); }
  249. $action = 'sortmycourses';
  250. $this->courses_list($action, $message);
  251. }
  252. /**
  253. * Delete a course category
  254. * render to listing view
  255. * @param int Category id
  256. */
  257. public function delete_course_category($category_id) {
  258. $result = $this->model->delete_course_category($category_id);
  259. $message = '';
  260. if ($result) { $message = get_lang('CourseCategoryDeleted'); }
  261. $action = 'sortmycourses';
  262. $this->courses_list($action, $message);
  263. }
  264. /**
  265. * Unsubscribe user from a course
  266. * render to listing view
  267. * @param string Course code
  268. */
  269. public function unsubscribe_user_from_course($courseCode, $search_term = null, $category_code = null) {
  270. $courseInfo = api_get_course_info($courseCode);
  271. $result = $this->model->remove_user_from_course($courseInfo['real_id']);
  272. $message = '';
  273. if ($result) { $message = get_lang('YouAreNowUnsubscribed'); }
  274. $action = 'sortmycourses';
  275. if (!empty($search_term)) {
  276. $this->search_courses($search_term, $message, $error);
  277. } else {
  278. $this->courses_categories('subcribe', $category_code, $message, $error);
  279. }
  280. }
  281. }