auth.lib.php 34 KB

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