auth.lib.php 28 KB

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