courses_controller.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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. $this->toolname = 'auth';
  22. $actived_theme_path = api_get_template();
  23. $this->view = new View($this->toolname, $actived_theme_path);
  24. $this->model = new AuthLib();
  25. }
  26. /**
  27. * It's used for listing courses,
  28. * render to courses_list view
  29. * @param string action
  30. * @param string confirmation message(optional)
  31. */
  32. public function courses_list($action, $message = '') {
  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 confirmation message(optional)
  52. * @param string error message(optional)
  53. */
  54. public function categories_list($action, $message='', $error='') {
  55. $data = array();
  56. $data['user_course_categories'] = $this->model->get_user_course_categories();
  57. $data['action'] = $action;
  58. $data['message'] = $message;
  59. $data['error'] = $error;
  60. // render to the view
  61. $this->view->set_data($data);
  62. $this->view->set_layout('layout');
  63. $this->view->set_template('categories_list');
  64. $this->view->render();
  65. }
  66. /**
  67. * It's used for listing courses with categories,
  68. * render to courses_categories view
  69. * @param string action
  70. * @param string Category code (optional)
  71. */
  72. public function courses_categories($action, $category_code = null, $message = '', $error = '', $content = null) {
  73. $data = array();
  74. $browse_course_categories = $this->model->browse_course_categories();
  75. if ($action == 'display_random_courses') {
  76. $data['browse_courses_in_category'] = $this->model->browse_courses_in_category(null, 10);
  77. } else {
  78. if (!isset($category_code)) {
  79. $category_code = $browse_course_categories[0][1]['code']; // by default first category
  80. }
  81. $data['browse_courses_in_category'] = $this->model->browse_courses_in_category($category_code);
  82. }
  83. $data['browse_course_categories'] = $browse_course_categories;
  84. $data['code'] = Security::remove_XSS($category_code);
  85. // getting all the courses to which the user is subscribed to
  86. $curr_user_id = api_get_user_id();
  87. $user_courses = $this->model->get_courses_of_user($curr_user_id);
  88. $user_coursecodes = array();
  89. // we need only the course codes as these will be used to match against the courses of the category
  90. if ($user_courses != '') {
  91. foreach($user_courses as $key => $value) {
  92. $user_coursecodes[] = $value['code'];
  93. }
  94. }
  95. if (api_is_drh()) {
  96. $courses = CourseManager::get_courses_followed_by_drh(api_get_user_id());
  97. foreach ($courses as $course) {
  98. $user_coursecodes[] = $course['code'];
  99. }
  100. }
  101. $data['user_coursecodes'] = $user_coursecodes;
  102. $data['action'] = $action;
  103. $data['message'] = $message;
  104. $data['content'] = $content;
  105. $data['error'] = $error;
  106. // render to the view
  107. $this->view->set_data($data);
  108. $this->view->set_layout('layout');
  109. $this->view->set_template('courses_categories');
  110. $this->view->render();
  111. }
  112. /**
  113. * Search courses
  114. */
  115. public function search_courses($search_term, $message = '', $error = '') {
  116. $data = array();
  117. $browse_course_categories = $this->model->browse_course_categories();
  118. $data['browse_courses_in_category'] = $this->model->search_courses($search_term);
  119. $data['browse_course_categories'] = $browse_course_categories;
  120. $data['search_term'] = Security::remove_XSS($search_term); //filter before showing in template
  121. // getting all the courses to which the user is subscribed to
  122. $curr_user_id = api_get_user_id();
  123. $user_courses = $this->model->get_courses_of_user($curr_user_id);
  124. $user_coursecodes = array();
  125. // we need only the course codes as these will be used to match against the courses of the category
  126. if ($user_courses != '') {
  127. foreach ($user_courses as $key => $value) {
  128. $user_coursecodes[] = $value['code'];
  129. }
  130. }
  131. $data['user_coursecodes'] = $user_coursecodes;
  132. $data['message'] = $message;
  133. $data['error'] = $error;
  134. $data['action'] = 'display_courses';
  135. // render to the view
  136. $this->view->set_data($data);
  137. $this->view->set_layout('layout');
  138. $this->view->set_template('courses_categories');
  139. $this->view->render();
  140. }
  141. /**
  142. * Auto user subcription to a course
  143. */
  144. public function subscribe_user($course_code, $search_term, $category_code) {
  145. $data = array();
  146. $courseInfo = api_get_course_info($course_code);
  147. // The course must be open in order to access the auto subscription
  148. if (in_array($courseInfo['visibility'], array(COURSE_VISIBILITY_CLOSED, COURSE_VISIBILITY_REGISTERED, COURSE_VISIBILITY_HIDDEN))) {
  149. $error = get_lang('SubscribingNotAllowed');
  150. //$message = get_lang('SubscribingNotAllowed');
  151. } else {
  152. $result = $this->model->subscribe_user($course_code);
  153. if (!$result) {
  154. $error = get_lang('CourseRegistrationCodeIncorrect');
  155. } else {
  156. //Redirect directly to the course after subscription
  157. $message = $result['message'];
  158. $content = $result['content'];
  159. }
  160. }
  161. if (!empty($search_term)) {
  162. $this->search_courses($search_term, $message, $error);
  163. } else {
  164. $this->courses_categories('subcribe', $category_code, $message, $error, $content);
  165. }
  166. return $result;
  167. }
  168. /**
  169. * Create a category
  170. * render to listing view
  171. * @param string Category title
  172. */
  173. public function add_course_category($category_title) {
  174. $result = $this->model->store_course_category($category_title);
  175. $message = '';
  176. if ($result) { $message = get_lang("CourseCategoryStored"); }
  177. else { $error = get_lang('ACourseCategoryWithThisNameAlreadyExists');}
  178. $action = 'sortmycourses';
  179. $this->courses_list($action, $message);
  180. }
  181. /**
  182. * Change course category
  183. * render to listing view
  184. * @param string Course code
  185. * @param int Category id
  186. */
  187. public function change_course_category($courseId, $category_id) {
  188. $result = $this->model->store_changecoursecategory($courseId, $category_id);
  189. $message = '';
  190. if ($result) { $message = get_lang('EditCourseCategorySucces'); }
  191. $action = 'sortmycourses';
  192. $this->courses_list($action, $message);
  193. }
  194. /**
  195. * Move up/down courses inside a category
  196. * render to listing view
  197. * @param string move to up or down
  198. * @param string Course code
  199. * @param int Category id
  200. */
  201. public function move_course($move, $course_code, $category_id) {
  202. $result = $this->model->move_course($move, $course_code, $category_id);
  203. $message = '';
  204. if ($result) { $message = get_lang('CourseSortingDone'); }
  205. $action = 'sortmycourses';
  206. $this->courses_list($action, $message);
  207. }
  208. /**
  209. * Move up/down categories
  210. * render to listing view
  211. * @param string move to up or down
  212. * @param int Category id
  213. */
  214. public function move_category($move, $category_id) {
  215. $result = $this->model->move_category($move, $category_id);
  216. $message = '';
  217. if ($result) { $message = get_lang('CategorySortingDone'); }
  218. $action = 'sortmycourses';
  219. $this->courses_list($action, $message);
  220. }
  221. /**
  222. * Edit course category
  223. * render to listing view
  224. * @param string Category title
  225. * @param int Category id
  226. */
  227. public function edit_course_category($title, $category) {
  228. $result = $this->model->store_edit_course_category($title, $category);
  229. $message = '';
  230. if ($result) { $message = get_lang('CourseCategoryEditStored'); }
  231. $action = 'sortmycourses';
  232. $this->courses_list($action, $message);
  233. }
  234. /**
  235. * Delete a course category
  236. * render to listing view
  237. * @param int Category id
  238. */
  239. public function delete_course_category($category_id) {
  240. $result = $this->model->delete_course_category($category_id);
  241. $message = '';
  242. if ($result) { $message = get_lang('CourseCategoryDeleted'); }
  243. $action = 'sortmycourses';
  244. $this->courses_list($action, $message);
  245. }
  246. /**
  247. * Unsubscribe user from a course
  248. * render to listing view
  249. * @param string Course code
  250. */
  251. public function unsubscribe_user_from_course($courseCode, $search_term = null, $category_code = null) {
  252. $courseInfo = api_get_course_info($courseCode);
  253. $result = $this->model->remove_user_from_course($courseInfo['real_id']);
  254. $message = '';
  255. if ($result) { $message = get_lang('YouAreNowUnsubscribed'); }
  256. $action = 'sortmycourses';
  257. if (!empty($search_term)) {
  258. $this->search_courses($search_term, $message, $error);
  259. } else {
  260. $this->courses_categories('subcribe', $category_code, $message, $error);
  261. }
  262. }
  263. }