auth.lib.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * Class Auth
  5. * Auth can be used to instantiate objects or as a library to manage courses
  6. * This file contains a class used like library provides functions for auth tool.
  7. * It's also used like model to courses_controller (MVC pattern).
  8. *
  9. * @author Christian Fasanando <christian1827@gmail.com>
  10. *
  11. * @package chamilo.auth
  12. */
  13. class Auth
  14. {
  15. /**
  16. * Constructor.
  17. */
  18. public function __construct()
  19. {
  20. }
  21. /**
  22. * retrieves all the courses that the user has already subscribed to.
  23. *
  24. * @param int $user_id
  25. *
  26. * @return array an array containing all the information of the courses of the given user
  27. */
  28. public function get_courses_of_user($user_id)
  29. {
  30. $TABLECOURS = Database::get_main_table(TABLE_MAIN_COURSE);
  31. $TABLECOURSUSER = Database::get_main_table(TABLE_MAIN_COURSE_USER);
  32. $avoidCoursesCondition = CoursesAndSessionsCatalog::getAvoidCourseCondition();
  33. $visibilityCondition = CourseManager::getCourseVisibilitySQLCondition('course', true);
  34. // Secondly we select the courses that are in a category (user_course_cat<>0) and
  35. // sort these according to the sort of the category
  36. $user_id = (int) $user_id;
  37. $sql = "SELECT
  38. course.code k,
  39. course.visual_code vc,
  40. course.subscribe subscr,
  41. course.unsubscribe unsubscr,
  42. course.title i,
  43. course.tutor_name t,
  44. course.category_code cat,
  45. course.directory dir,
  46. course_rel_user.status status,
  47. course_rel_user.sort sort,
  48. course_rel_user.user_course_cat user_course_cat
  49. FROM $TABLECOURS course, $TABLECOURSUSER course_rel_user
  50. WHERE
  51. course.id = course_rel_user.c_id AND
  52. course_rel_user.relation_type <> ".COURSE_RELATION_TYPE_RRHH." AND
  53. course_rel_user.user_id = '".$user_id."'
  54. $avoidCoursesCondition
  55. $visibilityCondition
  56. ORDER BY course_rel_user.sort ASC";
  57. $result = Database::query($sql);
  58. $courses = [];
  59. while ($row = Database::fetch_array($result)) {
  60. //we only need the database name of the course
  61. $courses[] = [
  62. 'code' => $row['k'],
  63. 'visual_code' => $row['vc'],
  64. 'title' => $row['i'],
  65. 'directory' => $row['dir'],
  66. 'status' => $row['status'],
  67. 'tutor' => $row['t'],
  68. 'subscribe' => $row['subscr'],
  69. 'category' => $row['cat'],
  70. 'unsubscribe' => $row['unsubscr'],
  71. 'sort' => $row['sort'],
  72. 'user_course_category' => $row['user_course_cat'],
  73. ];
  74. }
  75. return $courses;
  76. }
  77. /**
  78. * This function get all the courses in the particular user category.
  79. *
  80. * @return array
  81. */
  82. public function get_courses_in_category()
  83. {
  84. $user_id = api_get_user_id();
  85. // table definitions
  86. $TABLECOURS = Database::get_main_table(TABLE_MAIN_COURSE);
  87. $TABLECOURSUSER = Database::get_main_table(TABLE_MAIN_COURSE_USER);
  88. $avoidCoursesCondition = CoursesAndSessionsCatalog::getAvoidCourseCondition();
  89. $visibilityCondition = CourseManager::getCourseVisibilitySQLCondition('course', true);
  90. $sql = "SELECT
  91. course.id as real_id,
  92. course.code, course.visual_code, course.subscribe subscr, course.unsubscribe unsubscr,
  93. course.title title, course.tutor_name tutor, course.directory, course_rel_user.status status,
  94. course_rel_user.sort sort, course_rel_user.user_course_cat user_course_cat
  95. FROM $TABLECOURS course,
  96. $TABLECOURSUSER course_rel_user
  97. WHERE
  98. course.id = course_rel_user.c_id AND
  99. course_rel_user.user_id = '".$user_id."' AND
  100. course_rel_user.relation_type <> ".COURSE_RELATION_TYPE_RRHH."
  101. $avoidCoursesCondition
  102. $visibilityCondition
  103. ORDER BY course_rel_user.user_course_cat, course_rel_user.sort ASC";
  104. $result = Database::query($sql);
  105. $data = [];
  106. while ($course = Database::fetch_array($result)) {
  107. $data[$course['user_course_cat']][] = $course;
  108. }
  109. return $data;
  110. }
  111. /**
  112. * stores the changes in a course category
  113. * (moving a course to a different course category).
  114. *
  115. * @param int $courseId
  116. * @param int Category id
  117. *
  118. * @return bool True if it success
  119. */
  120. public function updateCourseCategory($courseId, $newcategory)
  121. {
  122. $courseId = (int) $courseId;
  123. $newcategory = (int) $newcategory;
  124. $current_user = api_get_user_id();
  125. $table = Database::get_main_table(TABLE_MAIN_COURSE_USER);
  126. $max_sort_value = api_max_sort_value($newcategory, $current_user);
  127. $sql = "UPDATE $table SET
  128. user_course_cat='".$newcategory."',
  129. sort='".($max_sort_value + 1)."'
  130. WHERE
  131. c_id ='".$courseId."' AND
  132. user_id='".$current_user."' AND
  133. relation_type<>".COURSE_RELATION_TYPE_RRHH;
  134. $resultQuery = Database::query($sql);
  135. $result = false;
  136. if (Database::affected_rows($resultQuery)) {
  137. $result = true;
  138. }
  139. return $result;
  140. }
  141. /**
  142. * moves the course one place up or down.
  143. *
  144. * @param string Direction (up/down)
  145. * @param string Course code
  146. * @param int Category id
  147. *
  148. * @return bool True if it success
  149. */
  150. public function move_course($direction, $course2move, $category)
  151. {
  152. // definition of tables
  153. $table = Database::get_main_table(TABLE_MAIN_COURSE_USER);
  154. $current_user_id = api_get_user_id();
  155. $all_user_courses = $this->get_courses_of_user($current_user_id);
  156. // we need only the courses of the category we are moving in
  157. $user_courses = [];
  158. foreach ($all_user_courses as $key => $course) {
  159. if ($course['user_course_category'] == $category) {
  160. $user_courses[] = $course;
  161. }
  162. }
  163. $target_course = [];
  164. foreach ($user_courses as $count => $course) {
  165. if ($course2move == $course['code']) {
  166. // source_course is the course where we clicked the up or down icon
  167. $source_course = $course;
  168. // target_course is the course before/after the source_course (depending on the up/down icon)
  169. if ($direction == 'up') {
  170. $target_course = $user_courses[$count - 1];
  171. } else {
  172. $target_course = $user_courses[$count + 1];
  173. }
  174. break;
  175. }
  176. }
  177. $result = false;
  178. if (count($target_course) > 0 && count($source_course) > 0) {
  179. $courseInfo = api_get_course_info($source_course['code']);
  180. $courseId = $courseInfo['real_id'];
  181. $targetCourseInfo = api_get_course_info($target_course['code']);
  182. $targetCourseId = $targetCourseInfo['real_id'];
  183. $sql = "UPDATE $table
  184. SET sort='".$target_course['sort']."'
  185. WHERE
  186. c_id = '".$courseId."' AND
  187. user_id = '".$current_user_id."' AND
  188. relation_type<>".COURSE_RELATION_TYPE_RRHH;
  189. $result1 = Database::query($sql);
  190. $sql = "UPDATE $table SET sort='".$source_course['sort']."'
  191. WHERE
  192. c_id ='".$targetCourseId."' AND
  193. user_id='".$current_user_id."' AND
  194. relation_type<>".COURSE_RELATION_TYPE_RRHH;
  195. $result2 = Database::query($sql);
  196. if (Database::affected_rows($result1) && Database::affected_rows($result2)) {
  197. $result = true;
  198. }
  199. }
  200. return $result;
  201. }
  202. /**
  203. * Moves the course one place up or down.
  204. *
  205. * @param string $direction Direction up/down
  206. * @param string $category2move Category id
  207. *
  208. * @return bool True If it success
  209. */
  210. public function move_category($direction, $category2move)
  211. {
  212. $userId = api_get_user_id();
  213. $userCategories = CourseManager::get_user_course_categories(api_get_user_id());
  214. $categories = array_values($userCategories);
  215. $previous = null;
  216. $target_category = [];
  217. foreach ($categories as $key => $category) {
  218. $category_id = $category['id'];
  219. if ($category2move == $category_id) {
  220. // source_course is the course where we clicked the up or down icon
  221. $source_category = $userCategories[$category2move];
  222. // target_course is the course before/after the source_course (depending on the up/down icon)
  223. if ($direction == 'up') {
  224. if (isset($categories[$key - 1])) {
  225. $target_category = $userCategories[$categories[$key - 1]['id']];
  226. }
  227. } else {
  228. if (isset($categories[$key + 1])) {
  229. $target_category = $userCategories[$categories[$key + 1]['id']];
  230. }
  231. }
  232. }
  233. }
  234. $result = false;
  235. if (count($target_category) > 0 && count($source_category) > 0) {
  236. $table = Database::get_main_table(TABLE_USER_COURSE_CATEGORY);
  237. $sql = "UPDATE $table SET
  238. sort = '".Database::escape_string($target_category['sort'])."'
  239. WHERE id='".intval($source_category['id'])."' AND user_id='".$userId."'";
  240. $resultFirst = Database::query($sql);
  241. $sql = "UPDATE $table SET
  242. sort = '".Database::escape_string($source_category['sort'])."'
  243. WHERE id='".intval($target_category['id'])."' AND user_id='".$userId."'";
  244. $resultSecond = Database::query($sql);
  245. if (Database::affected_rows($resultFirst) && Database::affected_rows($resultSecond)) {
  246. $result = true;
  247. }
  248. }
  249. return $result;
  250. }
  251. /**
  252. * Updates the user course category in the chamilo_user database.
  253. *
  254. * @param string Category title
  255. * @param int Category id
  256. *
  257. * @return bool True if it success
  258. */
  259. public function store_edit_course_category($title, $category_id)
  260. {
  261. // protect data
  262. $title = Database::escape_string($title);
  263. $category_id = (int) $category_id;
  264. $result = false;
  265. $table = Database::get_main_table(TABLE_USER_COURSE_CATEGORY);
  266. $sql = "UPDATE $table
  267. SET title='".api_htmlentities($title, ENT_QUOTES, api_get_system_encoding())."'
  268. WHERE id='".$category_id."'";
  269. $resultQuery = Database::query($sql);
  270. if (Database::affected_rows($resultQuery)) {
  271. $result = true;
  272. }
  273. return $result;
  274. }
  275. /**
  276. * deletes a course category and moves all the courses that were in this category to main category.
  277. *
  278. * @param int Category id
  279. *
  280. * @return bool True if it success
  281. */
  282. public function delete_course_category($category_id)
  283. {
  284. $current_user_id = api_get_user_id();
  285. $tucc = Database::get_main_table(TABLE_USER_COURSE_CATEGORY);
  286. $TABLECOURSUSER = Database::get_main_table(TABLE_MAIN_COURSE_USER);
  287. $category_id = (int) $category_id;
  288. $result = false;
  289. $sql = "DELETE FROM $tucc
  290. WHERE
  291. id='".$category_id."' AND
  292. user_id='".$current_user_id."'";
  293. $resultQuery = Database::query($sql);
  294. if (Database::affected_rows($resultQuery)) {
  295. $result = true;
  296. }
  297. $sql = "UPDATE $TABLECOURSUSER
  298. SET user_course_cat='0'
  299. WHERE
  300. user_course_cat='".$category_id."' AND
  301. user_id='".$current_user_id."' AND
  302. relation_type<>".COURSE_RELATION_TYPE_RRHH." ";
  303. Database::query($sql);
  304. return $result;
  305. }
  306. /**
  307. * @param int $categoryId
  308. *
  309. * @return array|mixed
  310. */
  311. public function getUserCourseCategory($categoryId)
  312. {
  313. $userId = api_get_user_id();
  314. $tucc = Database::get_main_table(TABLE_USER_COURSE_CATEGORY);
  315. $categoryId = (int) $categoryId;
  316. $sql = "SELECT * FROM $tucc
  317. WHERE
  318. id= $categoryId AND
  319. user_id= $userId";
  320. $resultQuery = Database::query($sql);
  321. $result = Database::fetch_array($resultQuery, 'ASSOC');
  322. return $result;
  323. }
  324. /**
  325. * unsubscribe the user from a given course.
  326. *
  327. * @param string $course_code
  328. *
  329. * @return bool True if it success
  330. */
  331. public function remove_user_from_course($course_code)
  332. {
  333. $tbl_course_user = Database::get_main_table(TABLE_MAIN_COURSE_USER);
  334. // protect variables
  335. $current_user_id = api_get_user_id();
  336. $course_code = Database::escape_string($course_code);
  337. $result = true;
  338. $courseInfo = api_get_course_info($course_code);
  339. $courseId = $courseInfo['real_id'];
  340. // we check (once again) if the user is not course administrator
  341. // because the course administrator cannot unsubscribe himself
  342. // (s)he can only delete the course
  343. $sql = "SELECT * FROM $tbl_course_user
  344. WHERE
  345. user_id='".$current_user_id."' AND
  346. c_id ='".$courseId."' AND
  347. status='1' ";
  348. $result_check = Database::query($sql);
  349. $number_of_rows = Database::num_rows($result_check);
  350. if ($number_of_rows > 0) {
  351. $result = false;
  352. }
  353. CourseManager::unsubscribe_user($current_user_id, $course_code);
  354. return $result;
  355. }
  356. /**
  357. * stores the user course category in the chamilo_user database.
  358. *
  359. * @param string Category title
  360. *
  361. * @return bool True if it success
  362. */
  363. public function store_course_category($category_title)
  364. {
  365. $table = Database::get_main_table(TABLE_USER_COURSE_CATEGORY);
  366. // protect data
  367. $current_user_id = api_get_user_id();
  368. $category_title = Database::escape_string($category_title);
  369. $result = false;
  370. // step 1: we determine the max value of the user defined course categories
  371. $sql = "SELECT sort FROM $table
  372. WHERE user_id='".$current_user_id."'
  373. ORDER BY sort DESC";
  374. $rs_sort = Database::query($sql);
  375. $maxsort = Database::fetch_array($rs_sort);
  376. $nextsort = $maxsort['sort'] + 1;
  377. // step 2: we check if there is already a category with this name,
  378. // if not we store it, else we give an error.
  379. $sql = "SELECT * FROM $table
  380. WHERE
  381. user_id='".$current_user_id."' AND
  382. title='".$category_title."'
  383. ORDER BY sort DESC";
  384. $rs = Database::query($sql);
  385. if (Database::num_rows($rs) == 0) {
  386. $sql = "INSERT INTO $table (user_id, title,sort)
  387. VALUES ('".$current_user_id."', '".api_htmlentities($category_title, ENT_QUOTES, api_get_system_encoding())."', '".$nextsort."')";
  388. $resultQuery = Database::query($sql);
  389. if (Database::affected_rows($resultQuery)) {
  390. $result = true;
  391. }
  392. } else {
  393. $result = false;
  394. }
  395. return $result;
  396. }
  397. }