auth.lib.php 29 KB

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