auth.lib.php 29 KB

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