auth.lib.php 29 KB

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