auth.lib.php 15 KB

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