auth.lib.php 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * This file contains a class used like library provides functions for auth tool. It's also used like model to courses_controller (MVC pattern)
  5. * @author Christian Fasanando <christian1827@gmail.com>
  6. * @package chamilo.auth
  7. */
  8. /**
  9. * Code
  10. */
  11. /**
  12. * Auth can be used to instanciate objects or as a library to manage courses
  13. * @package chamilo.auth
  14. */
  15. class AuthLib {
  16. /**
  17. * Constructor
  18. */
  19. public function __construct() {
  20. }
  21. /**
  22. * retrieves all the courses that the user has already subscribed to
  23. * @param int User id
  24. * @return array an array containing all the information of the courses of the given user
  25. */
  26. public function get_courses_of_user($user_id) {
  27. $TABLECOURS = Database::get_main_table(TABLE_MAIN_COURSE);
  28. $TABLECOURSUSER = Database::get_main_table(TABLE_MAIN_COURSE_USER);
  29. $TABLE_COURSE_FIELD = Database::get_main_table(TABLE_MAIN_COURSE_FIELD);
  30. $TABLE_COURSE_FIELD_VALUE = Database::get_main_table(TABLE_MAIN_COURSE_FIELD_VALUES);
  31. // get course list auto-register
  32. $sql = "SELECT course_code FROM $TABLE_COURSE_FIELD_VALUE tcfv INNER JOIN $TABLE_COURSE_FIELD tcf ON " .
  33. " tcfv.field_id = tcf.id WHERE tcf.field_variable = 'special_course' AND tcfv.field_value = 1 ";
  34. $special_course_result = Database::query($sql);
  35. if (Database::num_rows($special_course_result) > 0) {
  36. $special_course_list = array();
  37. while ($result_row = Database::fetch_array($special_course_result)) {
  38. $special_course_list[] = '"' . $result_row['course_code'] . '"';
  39. }
  40. }
  41. $without_special_courses = '';
  42. if (!empty($special_course_list)) {
  43. $without_special_courses = ' AND course.code NOT IN (' . implode(',', $special_course_list) . ')';
  44. }
  45. // Secondly we select the courses that are in a category (user_course_cat<>0) and sort these according to the sort of the category
  46. $user_id = intval($user_id);
  47. $sql_select_courses = "SELECT course.code k,
  48. course.id as real_id,
  49. course.visual_code vc,
  50. course.subscribe subscr,
  51. course.unsubscribe unsubscr,
  52. course.title i,
  53. course.tutor_name t,
  54. course.db_name db,
  55. course.directory dir,
  56. course_rel_user.status status,
  57. course_rel_user.sort sort,
  58. course_rel_user.user_course_cat user_course_cat
  59. FROM $TABLECOURS course, $TABLECOURSUSER course_rel_user
  60. WHERE course.id = course_rel_user.c_id AND
  61. course_rel_user.relation_type<>" . COURSE_RELATION_TYPE_RRHH . " AND
  62. course_rel_user.user_id = '" . $user_id . "' $without_special_courses
  63. ORDER BY course_rel_user.sort ASC";
  64. $result = Database::query($sql_select_courses);
  65. $courses = array();
  66. while ($row = Database::fetch_array($result)) {
  67. //we only need the database name of the course
  68. //@todo this array assignation is insane fix it
  69. $courses[] = array(
  70. 'db' => $row['db'],
  71. 'code' => $row['k'],
  72. 'visual_code' => $row['vc'],
  73. 'title' => $row['i'],
  74. 'directory' => $row['dir'],
  75. 'status' => $row['status'],
  76. 'tutor' => $row['t'],
  77. 'subscribe' => $row['subscr'],
  78. 'unsubscribe' => $row['unsubscr'],
  79. 'sort' => $row['sort'],
  80. 'user_course_category' => $row['user_course_cat'],
  81. 'real_id' => $row['real_id']
  82. );
  83. }
  84. return $courses;
  85. }
  86. /**
  87. * retrieves the user defined course categories
  88. * @return array containing all the IDs of the user defined courses categories, sorted by the "sort" field
  89. */
  90. public function get_user_course_categories() {
  91. $user_id = api_get_user_id();
  92. $table_category = Database::get_main_table(TABLE_USER_COURSE_CATEGORY);
  93. $sql = "SELECT * FROM " . $table_category . " WHERE user_id=$user_id ORDER BY sort ASC";
  94. $result = Database::query($sql);
  95. $output = array();
  96. while ($row = Database::fetch_array($result)) {
  97. $output[] = $row;
  98. }
  99. return $output;
  100. }
  101. /**
  102. * This function get all the courses in the particular user category;
  103. * @param int User category id
  104. * @return string: the name of the user defined course category
  105. */
  106. public function get_courses_in_category() {
  107. $user_id = api_get_user_id();
  108. // table definitions
  109. $TABLECOURS = Database::get_main_table(TABLE_MAIN_COURSE);
  110. $TABLECOURSUSER = Database::get_main_table(TABLE_MAIN_COURSE_USER);
  111. $TABLE_USER_COURSE_CATEGORY = Database::get_main_table(TABLE_USER_COURSE_CATEGORY);
  112. $TABLE_COURSE_FIELD = Database :: get_main_table(TABLE_MAIN_COURSE_FIELD);
  113. $TABLE_COURSE_FIELD_VALUE = Database :: get_main_table(TABLE_MAIN_COURSE_FIELD_VALUES);
  114. // get course list auto-register
  115. $sql = "SELECT course_code FROM $TABLE_COURSE_FIELD_VALUE tcfv INNER JOIN $TABLE_COURSE_FIELD tcf ON " .
  116. " tcfv.field_id = tcf.id WHERE tcf.field_variable = 'special_course' AND tcfv.field_value = 1 ";
  117. $special_course_result = Database::query($sql);
  118. if (Database::num_rows($special_course_result) > 0) {
  119. $special_course_list = array();
  120. while ($result_row = Database::fetch_array($special_course_result)) {
  121. $special_course_list[] = '"' . $result_row['course_code'] . '"';
  122. }
  123. }
  124. $without_special_courses = '';
  125. if (!empty($special_course_list)) {
  126. $without_special_courses = ' AND course.code NOT IN (' . implode(',', $special_course_list) . ')';
  127. }
  128. $sql_select_courses = "SELECT course.code,
  129. course.visual_code,
  130. course.subscribe subscr,
  131. course.unsubscribe unsubscr,
  132. course.title title,
  133. course.tutor_name tutor,
  134. course.db_name,
  135. course.directory,
  136. course_rel_user.status status,
  137. course_rel_user.sort sort,
  138. course_rel_user.user_course_cat user_course_cat
  139. FROM $TABLECOURS course, $TABLECOURSUSER course_rel_user
  140. WHERE course.id = course_rel_user.c_id
  141. AND course_rel_user.user_id = '" . $user_id . "'
  142. AND course_rel_user.relation_type <> " . COURSE_RELATION_TYPE_RRHH . "
  143. $without_special_courses
  144. ORDER BY course_rel_user.user_course_cat, course_rel_user.sort ASC";
  145. $result = Database::query($sql_select_courses);
  146. $number_of_courses = Database::num_rows($result);
  147. $data = array();
  148. while ($course = Database::fetch_array($result)) {
  149. $data[$course['user_course_cat']][] = $course;
  150. }
  151. return $data;
  152. }
  153. /**
  154. * stores the changes in a course category (moving a course to a different course category)
  155. * @param int Course id
  156. * @param int Category id
  157. * @return bool True if it success
  158. */
  159. public function store_changecoursecategory($courseId, $newcategory) {
  160. $TABLECOURSUSER = Database::get_main_table(TABLE_MAIN_COURSE_USER);
  161. $courseId = Database::escape_string($courseId);
  162. $newcategory = intval($newcategory);
  163. $current_user = api_get_user_id();
  164. $max_sort_value = api_max_sort_value($newcategory, $current_user); // max_sort_value($newcategory);
  165. $result = Database::query("UPDATE $TABLECOURSUSER SET user_course_cat='" . $newcategory . "', sort='" . ($max_sort_value + 1) . "'
  166. WHERE c_id ='" . $courseId . "' AND user_id='" . $current_user . "' AND relation_type<>" . COURSE_RELATION_TYPE_RRHH . "");
  167. if (Database::affected_rows($result)) {
  168. $result = true;
  169. }
  170. return $result;
  171. }
  172. /**
  173. * moves the course one place up or down
  174. * @param string Direction (up/down)
  175. * @param string Course code
  176. * @param int Category id
  177. * @return bool True if it success
  178. */
  179. public function move_course($direction, $course2move, $category) {
  180. // definition of tables
  181. $TABLECOURSUSER = Database::get_main_table(TABLE_MAIN_COURSE_USER);
  182. $current_user_id = api_get_user_id();
  183. $all_user_courses = $this->get_courses_of_user($current_user_id);
  184. $result = false;
  185. // we need only the courses of the category we are moving in
  186. $user_courses = array();
  187. foreach ($all_user_courses as $key => $course) {
  188. if ($course['user_course_category'] == $category) {
  189. $user_courses[] = $course;
  190. }
  191. }
  192. $target_course = array();
  193. foreach ($user_courses as $count => $course) {
  194. if ($course2move == $course['code']) {
  195. // source_course is the course where we clicked the up or down icon
  196. $source_course = $course;
  197. // target_course is the course before/after the source_course (depending on the up/down icon)
  198. if ($direction == 'up') {
  199. $target_course = $user_courses[$count - 1];
  200. } else {
  201. $target_course = $user_courses[$count + 1];
  202. }
  203. break;
  204. }
  205. }
  206. if (count($target_course) > 0 && count($source_course) > 0) {
  207. $sql_update1 = "UPDATE $TABLECOURSUSER SET sort='" . $target_course['sort'] . "'
  208. WHERE c_id = '" . $source_course['real_id'] . "' AND user_id='" . $current_user_id . "' AND relation_type<>" . COURSE_RELATION_TYPE_RRHH . " ";
  209. $sql_update2 = "UPDATE $TABLECOURSUSER SET sort='" . $source_course['sort'] . "'
  210. WHERE c_id = '" . $target_course['real_id'] . "' AND user_id='" . $current_user_id . "' AND relation_type<>" . COURSE_RELATION_TYPE_RRHH . " ";
  211. Database::query($sql_update2);
  212. $result = Database::query($sql_update1);
  213. if (Database::affected_rows($result)) {
  214. $result = true;
  215. }
  216. }
  217. return $result;
  218. }
  219. /**
  220. * Moves the course one place up or down
  221. * @param string Direction up/down
  222. * @param string Category id
  223. * @return bool True If it success
  224. */
  225. public function move_category($direction, $category2move) {
  226. // the database definition of the table that stores the user defined course categories
  227. $table_user_defined_category = Database::get_main_table(TABLE_USER_COURSE_CATEGORY);
  228. $current_user_id = api_get_user_id();
  229. $user_coursecategories = $this->get_user_course_categories();
  230. $user_course_categories_info = $this->get_user_course_categories_info();
  231. $result = false;
  232. foreach ($user_coursecategories as $key => $category) {
  233. $category_id = $category['id'];
  234. if ($category2move == $category_id) {
  235. // source_course is the course where we clicked the up or down icon
  236. $source_category = $user_course_categories_info[$category2move];
  237. // target_course is the course before/after the source_course (depending on the up/down icon)
  238. if ($direction == 'up') {
  239. $target_category = $user_course_categories_info[$user_coursecategories[$key - 1]['id']];
  240. } else {
  241. $target_category = $user_course_categories_info[$user_coursecategories[$key + 1]['id']];
  242. }
  243. }
  244. }
  245. if (count($target_category) > 0 && count($source_category) > 0) {
  246. $sql_update1 = "UPDATE $table_user_defined_category SET sort='" . Database::escape_string($target_category['sort']) . "' WHERE id='" . intval($source_category['id']) . "' AND user_id='" . $current_user_id . "'";
  247. $sql_update2 = "UPDATE $table_user_defined_category SET sort='" . Database::escape_string($source_category['sort']) . "' WHERE id='" . intval($target_category['id']) . "' AND user_id='" . $current_user_id . "'";
  248. Database::query($sql_update2);
  249. $result = Database::query($sql_update1);
  250. if (Database::affected_rows($result)) {
  251. $result = true;
  252. }
  253. }
  254. return $result;
  255. }
  256. /**
  257. * Retrieves the user defined course categories and all the info that goes with it
  258. * @return array containing all the info of the user defined courses categories with the id as key of the array
  259. */
  260. public function get_user_course_categories_info() {
  261. $current_user_id = api_get_user_id();
  262. $table_category = Database::get_main_table(TABLE_USER_COURSE_CATEGORY);
  263. $sql = "SELECT * FROM " . $table_category . " WHERE user_id='" . $current_user_id . "' ORDER BY sort ASC";
  264. $result = Database::query($sql);
  265. while ($row = Database::fetch_array($result)) {
  266. $output[$row['id']] = $row;
  267. }
  268. return $output;
  269. }
  270. /**
  271. * Updates the user course category in the chamilo_user database
  272. * @param string Category title
  273. * @param int Category id
  274. * @return bool True if it success
  275. */
  276. public function store_edit_course_category($title, $category_id) {
  277. // protect data
  278. $title = Database::escape_string($title);
  279. $category_id = intval($category_id);
  280. $result = false;
  281. $tucc = Database::get_main_table(TABLE_USER_COURSE_CATEGORY);
  282. $sql_update = "UPDATE $tucc SET title='" . api_htmlentities($title, ENT_QUOTES, api_get_system_encoding()) . "' WHERE id='" . $category_id . "'";
  283. $result = Database::query($sql_update);
  284. if (Database::affected_rows($result)) {
  285. $result = true;
  286. }
  287. return $result;
  288. }
  289. /**
  290. * deletes a course category and moves all the courses that were in this category to main category
  291. * @param int Category id
  292. * @return bool True if it success
  293. */
  294. public function delete_course_category($category_id) {
  295. $current_user_id = api_get_user_id();
  296. $tucc = Database::get_main_table(TABLE_USER_COURSE_CATEGORY);
  297. $TABLECOURSUSER = Database::get_main_table(TABLE_MAIN_COURSE_USER);
  298. $category_id = intval($category_id);
  299. $sql_delete = "DELETE FROM $tucc WHERE id='" . $category_id . "' and user_id='" . $current_user_id . "'";
  300. $result = Database::query($sql_delete);
  301. if (Database::affected_rows($result)) {
  302. $result = true;
  303. }
  304. $sql_update = "UPDATE $TABLECOURSUSER SET user_course_cat='0'
  305. WHERE user_course_cat='" . $category_id . "' AND user_id='" . $current_user_id . "' AND relation_type<>" . COURSE_RELATION_TYPE_RRHH . " ";
  306. Database::query($sql_update);
  307. return $result;
  308. }
  309. /**
  310. * unsubscribe the user from a given course
  311. * @param string Course code
  312. * @return bool True if it success
  313. */
  314. public function remove_user_from_course($courseId)
  315. {
  316. $tbl_course_user = Database::get_main_table(TABLE_MAIN_COURSE_USER);
  317. // protect variables
  318. $current_user_id = api_get_user_id();
  319. $courseId = Database::escape_string($courseId);
  320. $result = true;
  321. // we check (once again) if the user is not course administrator
  322. // because the course administrator cannot unsubscribe himself
  323. // (s)he can only delete the course
  324. $sql_check = "SELECT * FROM $tbl_course_user WHERE user_id='" . $current_user_id . "' AND c_id ='" . $courseId . "' AND status='1' ";
  325. $result_check = Database::query($sql_check);
  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, $courseId);
  331. return $result;
  332. }
  333. /**
  334. * stores the user course category in the chamilo_user database
  335. * @param string Category title
  336. * @return bool True if it success
  337. */
  338. public function store_course_category($category_title) {
  339. $tucc = Database::get_main_table(TABLE_USER_COURSE_CATEGORY);
  340. // protect data
  341. $current_user_id = api_get_user_id();
  342. $category_title = Database::escape_string($category_title);
  343. $result = false;
  344. // step 1: we determine the max value of the user defined course categories
  345. $sql = "SELECT sort FROM $tucc WHERE user_id='" . $current_user_id . "' ORDER BY sort DESC";
  346. $rs_sort = Database::query($sql);
  347. $maxsort = Database::fetch_array($rs_sort);
  348. $nextsort = $maxsort['sort'] + 1;
  349. // step 2: we check if there is already a category with this name, if not we store it, else we give an error.
  350. $sql = "SELECT * FROM $tucc WHERE user_id='" . $current_user_id . "' AND title='" . $category_title . "'ORDER BY sort DESC";
  351. $rs = Database::query($sql);
  352. if (Database::num_rows($rs) == 0) {
  353. $sql_insert = "INSERT INTO $tucc (user_id, title,sort) VALUES ('" . $current_user_id . "', '" . api_htmlentities($category_title, ENT_QUOTES, api_get_system_encoding()) . "', '" . $nextsort . "')";
  354. $result = Database::query($sql_insert);
  355. if (Database::affected_rows($result)) {
  356. $result = true;
  357. }
  358. } else {
  359. $result = false;
  360. }
  361. return $result;
  362. }
  363. /**
  364. * Counts the number of courses in a given course category
  365. * @param string Category code
  366. * @return int Count of courses
  367. */
  368. public function count_courses_in_category($category_code) {
  369. $tbl_course = Database::get_main_table(TABLE_MAIN_COURSE);
  370. $TABLE_COURSE_FIELD = Database :: get_main_table(TABLE_MAIN_COURSE_FIELD);
  371. $TABLE_COURSE_FIELD_VALUE = Database :: get_main_table(TABLE_MAIN_COURSE_FIELD_VALUES);
  372. // get course list auto-register
  373. $sql = "SELECT course_code FROM $TABLE_COURSE_FIELD_VALUE tcfv INNER JOIN $TABLE_COURSE_FIELD tcf ON " .
  374. " tcfv.field_id = tcf.id WHERE tcf.field_variable = 'special_course' AND tcfv.field_value = 1 ";
  375. $special_course_result = Database::query($sql);
  376. if (Database::num_rows($special_course_result) > 0) {
  377. $special_course_list = array();
  378. while ($result_row = Database::fetch_array($special_course_result)) {
  379. $special_course_list[] = '"' . $result_row['course_code'] . '"';
  380. }
  381. }
  382. $without_special_courses = '';
  383. if (!empty($special_course_list)) {
  384. $without_special_courses = ' AND course.code NOT IN (' . implode(',', $special_course_list) . ')';
  385. }
  386. $sql = "SELECT * FROM $tbl_course WHERE category_code" . (empty($category_code) ? " IS NULL" : "='" . $category_code . "'") . $without_special_courses;
  387. if (api_is_multiple_url_enabled()) {
  388. $url_access_id = api_get_current_access_url_id();
  389. if ($url_access_id != -1) {
  390. $tbl_url_rel_course = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_COURSE);
  391. $sql = "SELECT * FROM $tbl_course as course INNER JOIN $tbl_url_rel_course as url_rel_course
  392. ON (url_rel_course.c_id = course.id)
  393. WHERE access_url_id = $url_access_id AND category_code" . (empty($category_code) ? " IS NULL" : "='" . $category_code . "'") . $without_special_courses;
  394. }
  395. }
  396. return Database::num_rows(Database::query($sql));
  397. }
  398. /**
  399. * Get the browsing of the course categories (faculties)
  400. * @return array array containing a list with all the categories and subcategories(if needed)
  401. */
  402. public function browse_course_categories() {
  403. $tbl_courses_nodes = Database::get_main_table(TABLE_MAIN_CATEGORY);
  404. $sql = "SELECT * FROM $tbl_courses_nodes ORDER BY tree_pos ASC";
  405. $result = Database::query($sql);
  406. $categories = array();
  407. while ($row = Database::fetch_array($result)) {
  408. $count_courses = $this->count_courses_in_category($row['code']);
  409. $row['count_courses'] = $count_courses;
  410. if (!isset($row['parent_id'])) {
  411. $categories[0][$row['tree_pos']] = $row;
  412. } else {
  413. $categories[$row['parent_id']][$row['tree_pos']] = $row;
  414. }
  415. }
  416. return $categories;
  417. }
  418. /**
  419. * Display all the courses in the given course category. I could have used a parameter here
  420. * @param string Category code
  421. * @return array Courses data
  422. */
  423. public function browse_courses_in_category($category_code, $random_value = null) {
  424. $tbl_course = Database::get_main_table(TABLE_MAIN_COURSE);
  425. $TABLE_COURSE_FIELD = Database::get_main_table(TABLE_MAIN_COURSE_FIELD);
  426. $TABLE_COURSE_FIELD_VALUE = Database::get_main_table(TABLE_MAIN_COURSE_FIELD_VALUES);
  427. // Get course list auto-register
  428. $sql = "SELECT course_code
  429. FROM $TABLE_COURSE_FIELD_VALUE tcfv
  430. INNER JOIN $TABLE_COURSE_FIELD tcf ON tcfv.field_id = tcf.id
  431. WHERE tcf.field_variable = 'special_course' AND tcfv.field_value = 1 ";
  432. $special_course_result = Database::query($sql);
  433. if (Database::num_rows($special_course_result) > 0) {
  434. $special_course_list = array();
  435. while ($result_row = Database::fetch_array($special_course_result)) {
  436. $special_course_list[] = '"' . $result_row['course_code'] . '"';
  437. }
  438. }
  439. $without_special_courses = '';
  440. if (!empty($special_course_list)) {
  441. $without_special_courses = ' AND course.code NOT IN (' . implode(',', $special_course_list) . ')';
  442. }
  443. if (!empty($random_value)) {
  444. $random_value = intval($random_value);
  445. $sql = "SELECT COUNT(*) FROM $tbl_course";
  446. $result = Database::query($sql);
  447. list($num_records) = Database::fetch_row($result);
  448. if (api_is_multiple_url_enabled()) {
  449. $url_access_id = api_get_current_access_url_id();
  450. $tbl_url_rel_course = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_COURSE);
  451. $sql = "SELECT COUNT(*) FROM $tbl_course course
  452. INNER JOIN $tbl_url_rel_course as url_rel_course ON (url_rel_course.c_id = course.id)
  453. WHERE access_url_id = $url_access_id ";
  454. $result = Database::query($sql);
  455. list($num_records) = Database::fetch_row($result);
  456. $sql = "SELECT course.id FROM $tbl_course course INNER JOIN $tbl_url_rel_course as url_rel_course
  457. ON (url_rel_course.c_id = course.id)
  458. WHERE access_url_id = $url_access_id AND
  459. RAND()*$num_records< $random_value
  460. $without_special_courses ORDER BY RAND() LIMIT 0, $random_value";
  461. } else {
  462. $sql = "SELECT id FROM $tbl_course course WHERE RAND()*$num_records< $random_value $without_special_courses
  463. ORDER BY RAND() LIMIT 0, $random_value";
  464. }
  465. $result = Database::query($sql);
  466. $id_in = null;
  467. while (list($id) = Database::fetch_row($result)) {
  468. if ($id_in) {
  469. $id_in.= ",$id";
  470. } else {
  471. $id_in = "$id";
  472. }
  473. }
  474. $sql = "SELECT * FROM $tbl_course WHERE id IN($id_in)";
  475. } else {
  476. $category_code = Database::escape_string($category_code);
  477. if (empty($category_code)) {
  478. $sql = "SELECT * FROM $tbl_course WHERE 1=1 $without_special_courses ORDER BY title ";
  479. } else {
  480. $sql = "SELECT * FROM $tbl_course WHERE category_code='$category_code' $without_special_courses ORDER BY title ";
  481. }
  482. //showing only the courses of the current Chamilo access_url_id
  483. if (api_is_multiple_url_enabled()) {
  484. $url_access_id = api_get_current_access_url_id();
  485. $tbl_url_rel_course = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_COURSE);
  486. $sql = "SELECT * FROM $tbl_course as course INNER JOIN $tbl_url_rel_course as url_rel_course
  487. ON (url_rel_course.c_id = course.id)
  488. WHERE access_url_id = $url_access_id AND category_code='$category_code' $without_special_courses
  489. ORDER BY title";
  490. }
  491. }
  492. $result = Database::query($sql);
  493. $courses = array();
  494. while ($row = Database::fetch_array($result)) {
  495. $row['registration_code'] = !empty($row['registration_code']);
  496. $count_users = CourseManager::get_users_count_in_course($row['code']);
  497. $count_connections_last_month = Tracking::get_course_connections_count($row['id'], 0, api_get_utc_datetime(time() - (30 * 86400)));
  498. if ($row['tutor_name'] == '0') {
  499. $row['tutor_name'] = get_lang('NoManager');
  500. }
  501. $point_info = CourseManager::get_course_ranking($row['id'], 0);
  502. $courses[] = array(
  503. 'real_id' => $row['id'],
  504. 'point_info' => $point_info,
  505. 'code' => $row['code'],
  506. 'directory' => $row['directory'],
  507. 'db' => $row['db_name'],
  508. 'visual_code' => $row['visual_code'],
  509. 'title' => $row['title'],
  510. 'tutor' => $row['tutor_name'],
  511. 'subscribe' => $row['subscribe'],
  512. 'unsubscribe' => $row['unsubscribe'],
  513. 'registration_code' => $row['registration_code'],
  514. 'creation_date' => $row['creation_date'],
  515. 'visibility' => $row['visibility'],
  516. 'count_users' => $count_users,
  517. 'count_connections' => $count_connections_last_month
  518. );
  519. }
  520. return $courses;
  521. }
  522. /**
  523. * Search the courses database for a course that matches the search term.
  524. * The search is done on the code, title and tutor field of the course table.
  525. * @param string $search_term: the string that the user submitted, what we are looking for
  526. * @return array an array containing a list of all the courses (the code, directory, dabase, visual_code, title, ... ) matching the the search term.
  527. */
  528. public function search_courses($search_term) {
  529. $TABLECOURS = Database::get_main_table(TABLE_MAIN_COURSE);
  530. $TABLE_COURSE_FIELD = Database :: get_main_table(TABLE_MAIN_COURSE_FIELD);
  531. $TABLE_COURSE_FIELD_VALUE = Database :: get_main_table(TABLE_MAIN_COURSE_FIELD_VALUES);
  532. // get course list auto-register
  533. $sql = "SELECT course_code FROM $TABLE_COURSE_FIELD_VALUE tcfv INNER JOIN $TABLE_COURSE_FIELD tcf ON tcfv.field_id = tcf.id
  534. WHERE tcf.field_variable = 'special_course' AND tcfv.field_value = 1 ";
  535. $special_course_result = Database::query($sql);
  536. if (Database::num_rows($special_course_result) > 0) {
  537. $special_course_list = array();
  538. while ($result_row = Database::fetch_array($special_course_result)) {
  539. $special_course_list[] = '"' . $result_row['course_code'] . '"';
  540. }
  541. }
  542. $without_special_courses = '';
  543. if (!empty($special_course_list)) {
  544. $without_special_courses = ' AND course.code NOT IN (' . implode(',', $special_course_list) . ')';
  545. }
  546. $search_term_safe = Database::escape_string($search_term);
  547. $sql_find = "SELECT * FROM $TABLECOURS WHERE (code LIKE '%" . $search_term_safe . "%' OR title LIKE '%" . $search_term_safe . "%' OR tutor_name LIKE '%" . $search_term_safe . "%') $without_special_courses ORDER BY title, visual_code ASC";
  548. if (api_is_multiple_url_enabled()) {
  549. $url_access_id = api_get_current_access_url_id();
  550. if ($url_access_id != -1) {
  551. $tbl_url_rel_course = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_COURSE);
  552. $sql_find = "SELECT * FROM $TABLECOURS as course INNER JOIN $tbl_url_rel_course as url_rel_course
  553. ON (url_rel_course.c_id = course.id)
  554. WHERE access_url_id = $url_access_id AND (code LIKE '%" . $search_term_safe . "%' OR title LIKE '%" . $search_term_safe . "%' OR tutor_name LIKE '%" . $search_term_safe . "%' ) $without_special_courses ORDER BY title, visual_code ASC ";
  555. }
  556. }
  557. $result_find = Database::query($sql_find);
  558. $courses = array();
  559. while ($row = Database::fetch_array($result_find)) {
  560. $row['registration_code'] = !empty($row['registration_code']);
  561. $count_users = count(CourseManager::get_user_list_from_course_code($row['code']));
  562. $count_connections_last_month = Tracking::get_course_connections_count($row['id'], 0, api_get_utc_datetime(time() - (30 * 86400)));
  563. $courses[] = array(
  564. 'code' => $row['code'],
  565. 'real_id' => $row['id'],
  566. 'directory' => $row['directory'],
  567. 'db' => $row['db_name'],
  568. 'visual_code' => $row['visual_code'],
  569. 'title' => $row['title'],
  570. 'tutor' => $row['tutor_name'],
  571. 'subscribe' => $row['subscribe'],
  572. 'unsubscribe' => $row['unsubscribe'],
  573. 'registration_code' => $row['registration_code'],
  574. 'creation_date' => $row['creation_date'],
  575. 'visibility' => $row['visibility'],
  576. 'count_users' => $count_users,
  577. 'count_connections' => $count_connections_last_month
  578. );
  579. }
  580. return $courses;
  581. }
  582. /**
  583. * Subscribe the user to a given course
  584. * @param string Course code
  585. * @return string Message about results
  586. */
  587. public function subscribe_user($course_code) {
  588. $user_id = api_get_user_id();
  589. $all_course_information = CourseManager::get_course_information($course_code);
  590. if ($all_course_information['registration_code'] == '' || $_POST['course_registration_code'] == $all_course_information['registration_code']) {
  591. if (api_is_platform_admin()) {
  592. $status_user_in_new_course = COURSEMANAGER;
  593. } else {
  594. $status_user_in_new_course = null;
  595. }
  596. if (CourseManager::add_user_to_course($user_id, $all_course_information['real_id'], $status_user_in_new_course)) {
  597. $send = api_get_course_setting('email_alert_to_teacher_on_new_user_in_course', $course_code);
  598. if ($send == 1) {
  599. CourseManager::email_to_tutor($user_id, $course_code, $send_to_tutor_also = false);
  600. } else if ($send == 2) {
  601. CourseManager::email_to_tutor($user_id, $course_code, $send_to_tutor_also = true);
  602. }
  603. $url = Display::url($all_course_information['title'], api_get_course_url($course_code));
  604. $message = sprintf(get_lang('EnrollToCourseXSuccessful'), $url);
  605. } else {
  606. $message = get_lang('ErrorContactPlatformAdmin');
  607. }
  608. return array('message' => $message);
  609. } else {
  610. if (isset($_POST['course_registration_code']) && $_POST['course_registration_code'] != $all_course_information['registration_code']) {
  611. return false;
  612. }
  613. $message = get_lang('CourseRequiresPassword') . '<br />';
  614. $message .= $all_course_information['title'].' ('.$all_course_information['visual_code'].') ';
  615. $action = api_get_path(WEB_CODE_PATH) . "auth/courses.php?action=subscribe_user_with_password&sec_token=" . Security::getCurrentToken();
  616. $form = new FormValidator('subscribe_user_with_password', 'post', $action);
  617. $form->addElement('hidden', 'sec_token', Security::getCurrentToken());
  618. $form->addElement('hidden', 'subscribe_user_with_password', $all_course_information['code']);
  619. $form->addElement('text', 'course_registration_code');
  620. $form->addElement('button', 'submit', get_lang('SubmitRegistrationCode'));
  621. $content = $form->return_form();
  622. return array('message' => $message, 'content' => $content);
  623. }
  624. }
  625. }