auth.lib.php 28 KB

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