auth.lib.php 32 KB

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