add_users_to_session_course.php 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * @package chamilo.admin
  5. */
  6. // resetting the course id
  7. $cidReset = true;
  8. // including some necessary files
  9. require_once __DIR__.'/../inc/global.inc.php';
  10. $xajax = new xajax();
  11. $xajax->registerFunction('search_users');
  12. // setting the section (for the tabs)
  13. $this_section = SECTION_PLATFORM_ADMIN;
  14. $id_session = isset($_GET['id_session']) ? (int) $_GET['id_session'] : 0;
  15. $courseId = isset($_GET['course_id']) ? (int) $_GET['course_id'] : 0;
  16. if (empty($id_session) || empty($courseId)) {
  17. api_not_allowed(true);
  18. }
  19. $addProcess = isset($_GET['add']) ? Security::remove_XSS($_GET['add']) : null;
  20. SessionManager::protectSession($id_session);
  21. $courseInfo = api_get_course_info_by_id($courseId);
  22. // setting breadcrumbs
  23. $interbreadcrumb[] = ['url' => 'session_list.php', 'name' => get_lang('SessionList')];
  24. $interbreadcrumb[] = [
  25. 'url' => "resume_session.php?id_session=".$id_session,
  26. 'name' => get_lang('SessionOverview'),
  27. ];
  28. // Database Table Definitions
  29. $tbl_session = Database::get_main_table(TABLE_MAIN_SESSION);
  30. $tbl_course = Database::get_main_table(TABLE_MAIN_COURSE);
  31. $tbl_user = Database::get_main_table(TABLE_MAIN_USER);
  32. $tbl_session_rel_user = Database::get_main_table(TABLE_MAIN_SESSION_USER);
  33. $tableRelSessionCourseUser = Database::get_main_table(TABLE_MAIN_SESSION_COURSE_USER);
  34. // setting the name of the tool
  35. $tool_name = get_lang('SubscribeUsersToSession');
  36. $add_type = 'unique';
  37. if (isset($_REQUEST['add_type']) && $_REQUEST['add_type'] != '') {
  38. $add_type = Security::remove_XSS($_REQUEST['add_type']);
  39. }
  40. $page = isset($_GET['page']) ? Security::remove_XSS($_GET['page']) : null;
  41. // Checking for extra field with filter on
  42. $extra_field_list = UserManager::get_extra_fields();
  43. $new_field_list = [];
  44. if (is_array($extra_field_list)) {
  45. foreach ($extra_field_list as $extra_field) {
  46. //if is enabled to filter and is a "<select>" field type
  47. if ($extra_field[8] == 1 && $extra_field[2] == ExtraField::FIELD_TYPE_SELECT) {
  48. $new_field_list[] = [
  49. 'name' => $extra_field[3],
  50. 'type' => $extra_field[2],
  51. 'variable' => $extra_field[1],
  52. 'data' => $extra_field[9],
  53. ];
  54. }
  55. if ($extra_field[8] == 1 && $extra_field[2] == ExtraField::FIELD_TYPE_TAG) {
  56. $options = UserManager::get_extra_user_data_for_tags($extra_field[1]);
  57. $new_field_list[] = [
  58. 'name' => $extra_field[3],
  59. 'type' => $extra_field[2],
  60. 'variable' => $extra_field[1],
  61. 'data' => $options['options'],
  62. ];
  63. }
  64. }
  65. }
  66. function search_users($needle, $type)
  67. {
  68. global $id_session, $courseId;
  69. $tbl_user = Database::get_main_table(TABLE_MAIN_USER);
  70. $tbl_session_rel_user = Database::get_main_table(TABLE_MAIN_SESSION_USER);
  71. $tableRelSessionCourseUser = Database::get_main_table(TABLE_MAIN_SESSION_COURSE_USER);
  72. $xajax_response = new xajaxResponse();
  73. $return = '';
  74. if (!empty($needle) && !empty($type)) {
  75. // Normal behaviour
  76. if ($type == 'any_session' && $needle == 'false') {
  77. $type = 'multiple';
  78. $needle = '';
  79. }
  80. $needle = Database::escape_string($needle);
  81. $order_clause = ' ORDER BY lastname, firstname, username';
  82. $showOfficialCode = false;
  83. $orderListByOfficialCode = api_get_setting('order_user_list_by_official_code');
  84. if ($orderListByOfficialCode === 'true') {
  85. $showOfficialCode = true;
  86. $order_clause = ' ORDER BY official_code, lastname, firstname, username';
  87. }
  88. if (api_is_session_admin()
  89. && api_get_setting('prevent_session_admins_to_manage_all_users') === 'true'
  90. ) {
  91. $order_clause = ' AND user.creator_id = '.api_get_user_id().$order_clause;
  92. }
  93. $cond_user_id = '';
  94. // Only for single & multiple
  95. if (in_array($type, ['single', 'multiple'])) {
  96. if (!empty($id_session)) {
  97. $id_session = (int) $id_session;
  98. // check id_user from session_rel_user table
  99. $sql = "
  100. SELECT su.user_id
  101. FROM $tbl_session_rel_user su
  102. INNER JOIN $tableRelSessionCourseUser sc
  103. ON (sc.session_id = su.session_id AND su.user_id = sc.user_id)
  104. WHERE
  105. sc.c_id = $courseId AND
  106. su.session_id = $id_session AND
  107. relation_type <> ".SESSION_RELATION_TYPE_RRHH;
  108. $res = Database::query($sql);
  109. $user_ids = [];
  110. if (Database::num_rows($res) > 0) {
  111. while ($row = Database::fetch_row($res)) {
  112. $user_ids[] = (int) $row[0];
  113. }
  114. }
  115. if (count($user_ids) > 0) {
  116. $cond_user_id = ' AND user.id NOT IN('.implode(",", $user_ids).')';
  117. }
  118. }
  119. }
  120. switch ($type) {
  121. case 'single':
  122. // search users where username or firstname or lastname begins likes $needle
  123. $sql = "
  124. SELECT user.id, username, lastname, firstname, official_code
  125. FROM $tbl_user user
  126. WHERE
  127. (
  128. username LIKE '$needle%'
  129. OR lastname LIKE '$needle%'
  130. OR firstname LIKE '$needle%'
  131. ) AND
  132. user.status <> 6 AND
  133. user.status <> ".DRH."
  134. $order_clause
  135. LIMIT 11
  136. ";
  137. break;
  138. case 'multiple':
  139. $sql = "
  140. SELECT user.id, username, lastname, firstname, official_code
  141. FROM $tbl_user user
  142. WHERE
  143. lastname LIKE '$needle%' AND
  144. user.status <> ".DRH." AND
  145. user.status <> 6 $cond_user_id
  146. $order_clause
  147. ";
  148. break;
  149. case 'any_session':
  150. $sql = "
  151. SELECT DISTINCT user.id, username, lastname, firstname, official_code
  152. FROM $tbl_user user
  153. LEFT OUTER JOIN $tbl_session_rel_user s
  154. ON (s.user_id = user.id)
  155. WHERE
  156. s.user_id IS NULL AND
  157. user.status <> ".DRH." AND
  158. user.status <> 6 $cond_user_id
  159. $order_clause
  160. ";
  161. break;
  162. }
  163. if (api_is_multiple_url_enabled()) {
  164. $tbl_user_rel_access_url = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_USER);
  165. $access_url_id = api_get_current_access_url_id();
  166. if ($access_url_id != -1) {
  167. switch ($type) {
  168. case 'single':
  169. $sql = "
  170. SELECT user.id, username, lastname, firstname, official_code
  171. FROM $tbl_user user
  172. INNER JOIN $tbl_user_rel_access_url url_user
  173. ON (url_user.user_id = user.id)
  174. WHERE
  175. access_url_id = '$access_url_id'
  176. AND (
  177. username LIKE '$needle%'
  178. OR lastname LIKE '$needle%'
  179. OR firstname LIKE '$needle%'
  180. )
  181. AND user.status <> 6
  182. AND user.status <> ".DRH."
  183. $order_clause LIMIT 11
  184. ";
  185. break;
  186. case 'multiple':
  187. $sql = "
  188. SELECT user.id, username, lastname, firstname, official_code
  189. FROM $tbl_user user
  190. INNER JOIN $tbl_user_rel_access_url url_user ON (url_user.user_id=user.id)
  191. WHERE
  192. access_url_id = $access_url_id
  193. AND lastname LIKE '$needle%'
  194. AND user.status <> ".DRH."
  195. AND user.status <> 6 $cond_user_id
  196. $order_clause
  197. ";
  198. break;
  199. case 'any_session':
  200. $sql = "
  201. SELECT DISTINCT user.id, username, lastname, firstname, official_code
  202. FROM $tbl_user user
  203. LEFT OUTER JOIN $tbl_session_rel_user s
  204. ON (s.user_id = user.id)
  205. INNER JOIN $tbl_user_rel_access_url url_user
  206. ON (url_user.user_id = user.id)
  207. WHERE
  208. access_url_id = $access_url_id
  209. AND s.user_id IS null
  210. AND user.status <> ".DRH."
  211. AND user.status <> 6 $cond_user_id
  212. $order_clause
  213. ";
  214. break;
  215. }
  216. }
  217. }
  218. $rs = Database::query($sql);
  219. $i = 0;
  220. if ($type == 'single') {
  221. while ($user = Database:: fetch_array($rs)) {
  222. $i++;
  223. if ($i <= 10) {
  224. $person_name =
  225. $user['lastname'].' '.$user['firstname'].' ('.$user['username'].') '.$user['official_code'];
  226. if ($showOfficialCode) {
  227. $officialCode = !empty($user['official_code']) ? $user['official_code'].' - ' : '? - ';
  228. $person_name =
  229. $officialCode.$user['lastname'].' '.$user['firstname'].' ('.$user['username'].')';
  230. }
  231. $return .= '<a href="javascript: void(0);" onclick="javascript: add_user_to_session(\''.$user['id']
  232. .'\',\''.$person_name.' '.'\')">'.$person_name.' </a><br />';
  233. } else {
  234. $return .= '...<br />';
  235. }
  236. }
  237. $xajax_response->addAssign('ajax_list_users_single', 'innerHTML', api_utf8_encode($return));
  238. } else {
  239. $return .= '<select id="origin_users" name="nosessionUsersList[]" multiple="multiple" size="15" style="width:360px;">';
  240. while ($user = Database:: fetch_array($rs)) {
  241. $person_name =
  242. $user['lastname'].' '.$user['firstname'].' ('.$user['username'].') '.$user['official_code'];
  243. if ($showOfficialCode) {
  244. $officialCode = !empty($user['official_code']) ? $user['official_code'].' - ' : '? - ';
  245. $person_name = $officialCode.$user['lastname'].' '.$user['firstname'].' ('.$user['username'].')';
  246. }
  247. $return .= '<option value="'.$user['id'].'">'.$person_name.' </option>';
  248. }
  249. $return .= '</select>';
  250. $xajax_response->addAssign('ajax_list_users_multiple', 'innerHTML', api_utf8_encode($return));
  251. }
  252. }
  253. return $xajax_response;
  254. }
  255. $xajax->processRequests();
  256. $htmlHeadXtra[] = $xajax->getJavascript('../inc/lib/xajax/');
  257. $htmlHeadXtra[] = '
  258. <script>
  259. function add_user_to_session (code, content) {
  260. document.getElementById("user_to_add").value = "";
  261. document.getElementById("ajax_list_users_single").innerHTML = "";
  262. destination = document.getElementById("destination_users");
  263. for (i=0;i<destination.length;i++) {
  264. if(destination.options[i].text == content) {
  265. return false;
  266. }
  267. }
  268. destination.options[destination.length] = new Option(content,code);
  269. destination.selectedIndex = -1;
  270. sortOptions(destination.options);
  271. }
  272. function remove_item(origin) {
  273. for(var i = 0 ; i<origin.options.length ; i++) {
  274. if(origin.options[i].selected) {
  275. origin.options[i]=null;
  276. i = i-1;
  277. }
  278. }
  279. }
  280. function validate_filter() {
  281. document.formulaire.add_type.value = \''.$add_type.'\';
  282. document.formulaire.form_sent.value=0;
  283. document.formulaire.submit();
  284. }
  285. function checked_in_no_session(checked) {
  286. $("#first_letter_user")
  287. .find("option")
  288. .attr("selected", false);
  289. xajax_search_users(checked, "any_session");
  290. }
  291. function change_select(val) {
  292. $("#user_with_any_session_id").attr("checked", false);
  293. xajax_search_users(val,"multiple");
  294. }
  295. </script>';
  296. $form_sent = 0;
  297. $errorMsg = $firstLetterUser = $firstLetterSession = '';
  298. $UserList = $SessionList = [];
  299. $sessions = [];
  300. if (isset($_POST['form_sent']) && $_POST['form_sent']) {
  301. $form_sent = $_POST['form_sent'];
  302. $firstLetterUser = isset($_POST['firstLetterUser']) ? $_POST['firstLetterUser'] : '';
  303. $firstLetterSession = isset($_POST['firstLetterSession']) ? $_POST['firstLetterSession'] : '';
  304. $UserList = isset($_POST['sessionUsersList']) ? $_POST['sessionUsersList'] : [];
  305. if (!is_array($UserList)) {
  306. $UserList = [];
  307. }
  308. if ($form_sent == 1) {
  309. // Added a parameter to send emails when registering a user
  310. SessionManager::subscribeUsersToSession(
  311. $id_session,
  312. $UserList,
  313. null,
  314. false,
  315. false
  316. );
  317. SessionManager::subscribe_users_to_session_course(
  318. $UserList,
  319. $id_session,
  320. $courseInfo['code']
  321. );
  322. Display::addFlash(Display::return_message(get_lang('Updated')));
  323. header('Location: resume_session.php?id_session='.$id_session);
  324. exit;
  325. }
  326. }
  327. $session_info = SessionManager::fetch($id_session);
  328. Display::display_header($tool_name);
  329. $nosessionUsersList = $sessionUsersList = [];
  330. $where_filter = null;
  331. $ajax_search = $add_type == 'unique' ? true : false;
  332. //$order_clause = api_sort_by_first_name() ? ' ORDER BY firstname, lastname, username' : ' ORDER BY lastname, firstname, username';
  333. // On this screen, it doesn't make sense to order users by firstname. Always use lastname first
  334. // api_get_person_name() calls have been removed because ordering users in a simple list must always
  335. // be done by lastname, even if we like to show user names with the firstname first.
  336. // By simple logic, lastnames are the smallest common denominator
  337. $order_clause = ' ORDER BY lastname, firstname, username';
  338. $showOfficialCode = false;
  339. $orderListByOfficialCode = api_get_setting('order_user_list_by_official_code');
  340. if ($orderListByOfficialCode === 'true') {
  341. $showOfficialCode = true;
  342. $order_clause = ' ORDER BY official_code, lastname, firstname, username';
  343. }
  344. if ($ajax_search) {
  345. $sql = "
  346. SELECT u.id, u.lastname, u.firstname, u.username, su.session_id, u.official_code
  347. FROM $tbl_session_rel_user su
  348. INNER JOIN $tableRelSessionCourseUser sc
  349. ON (sc.session_id = su.session_id AND su.user_id = sc.user_id)
  350. INNER JOIN $tbl_user u
  351. ON su.user_id = u.id
  352. WHERE
  353. su.session_id = ".intval($id_session)." AND
  354. su.relation_type <> ".SESSION_RELATION_TYPE_RRHH." AND
  355. sc.c_id = $courseId AND
  356. u.status<>".DRH." AND
  357. u.status <> 6
  358. $order_clause
  359. ";
  360. if (api_is_multiple_url_enabled()) {
  361. $tbl_user_rel_access_url = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_USER);
  362. $access_url_id = api_get_current_access_url_id();
  363. if ($access_url_id != -1) {
  364. $sql = "
  365. SELECT u.id, u.lastname, u.firstname, u.username, su.session_id, u.official_code
  366. FROM $tbl_user u
  367. INNER JOIN $tbl_session_rel_user su
  368. ON
  369. su.user_id = u.id AND
  370. su.relation_type <> ".SESSION_RELATION_TYPE_RRHH." AND
  371. su.session_id = ".intval($id_session)."
  372. INNER JOIN $tbl_user_rel_access_url url_user
  373. ON (url_user.user_id = u.id)
  374. INNER JOIN $tableRelSessionCourseUser sc
  375. ON (sc.session_id = su.session_id AND su.user_id = sc.user_id)
  376. WHERE
  377. access_url_id = $access_url_id AND
  378. sc.c_id = $courseId AND
  379. u.status <> ".DRH." AND
  380. u.status <> 6
  381. $order_clause
  382. ";
  383. }
  384. }
  385. $result = Database::query($sql);
  386. $users = Database::store_result($result);
  387. foreach ($users as $user) {
  388. $sessionUsersList[$user['id']] = $user;
  389. }
  390. $sessionUserInfo = SessionManager::getTotalUserCoursesInSession($id_session, $courseId);
  391. // Filter the user list in all courses in the session
  392. foreach ($sessionUserInfo as $sessionUser) {
  393. // filter students in session
  394. if ($sessionUser['status_in_session'] != 0) {
  395. continue;
  396. }
  397. if (!array_key_exists($sessionUser['id'], $sessionUsersList)) {
  398. continue;
  399. }
  400. }
  401. unset($users); //clean to free memory
  402. } else {
  403. // Filter by Extra Fields
  404. $extra_field_result = [];
  405. $use_extra_fields = false;
  406. if (is_array($extra_field_list)) {
  407. if (is_array($new_field_list) && count($new_field_list) > 0) {
  408. $result_list = [];
  409. foreach ($new_field_list as $new_field) {
  410. $varname = 'field_'.$new_field['variable'];
  411. $fieldtype = $new_field['type'];
  412. if (UserManager::is_extra_field_available($new_field['variable'])) {
  413. if (isset($_POST[$varname]) && $_POST[$varname] != '0') {
  414. $use_extra_fields = true;
  415. if ($fieldtype == ExtraField::FIELD_TYPE_TAG) {
  416. $extra_field_result[] = UserManager::get_extra_user_data_by_tags(
  417. $_POST['field_id'],
  418. $_POST[$varname]
  419. );
  420. } else {
  421. $extra_field_result[] = UserManager::get_extra_user_data_by_value(
  422. $new_field['variable'],
  423. $_POST[$varname]
  424. );
  425. }
  426. }
  427. }
  428. }
  429. }
  430. }
  431. if ($use_extra_fields) {
  432. $final_result = [];
  433. if (count($extra_field_result) > 1) {
  434. for ($i = 0; $i < count($extra_field_result) - 1; $i++) {
  435. if (is_array($extra_field_result[$i + 1])) {
  436. $final_result = array_intersect(
  437. $extra_field_result[$i],
  438. $extra_field_result[$i + 1]
  439. );
  440. }
  441. }
  442. } else {
  443. $final_result = $extra_field_result[0];
  444. }
  445. if (api_is_multiple_url_enabled()) {
  446. if (is_array($final_result) && count($final_result) > 0) {
  447. $where_filter = " AND u.id IN ('".implode("','", $final_result)."') ";
  448. } else {
  449. //no results
  450. $where_filter = " AND u.id = -1";
  451. }
  452. } else {
  453. if (is_array($final_result) && count($final_result) > 0) {
  454. $where_filter = " WHERE u.id IN ('".implode("','", $final_result)."') ";
  455. } else {
  456. //no results
  457. $where_filter = " WHERE u.id = -1";
  458. }
  459. }
  460. }
  461. if (api_is_session_admin() && api_get_setting('prevent_session_admins_to_manage_all_users') === 'true') {
  462. $order_clause = " AND u.creator_id = ".api_get_user_id().$order_clause;
  463. }
  464. if ($use_extra_fields) {
  465. $sql = "
  466. SELECT u.id, lastname, firstname, username, su.session_id, official_code
  467. FROM $tbl_user u
  468. LEFT JOIN $tbl_session_rel_user su
  469. ON su.user_id = u.id
  470. AND su.session_id = $id_session
  471. AND su.relation_type <> ".SESSION_RELATION_TYPE_RRHH."
  472. INNER JOIN $tableRelSessionCourseUser sc
  473. ON (sc.session_id = su.session_id AND su.user_id = sc.user_id)
  474. $where_filter
  475. AND u.status <> ".DRH."
  476. AND u.status <> 6
  477. AND sc.c_id = $courseId
  478. $order_clause
  479. ";
  480. } else {
  481. $sql = "
  482. SELECT u.id, lastname, firstname, username, su.session_id, official_code
  483. FROM $tbl_user u
  484. LEFT JOIN $tbl_session_rel_user su
  485. ON su.user_id = u.id
  486. AND su.session_id = $id_session
  487. AND su.relation_type <> ".SESSION_RELATION_TYPE_RRHH."
  488. WHERE
  489. u.status <> ".DRH." AND
  490. u.status <> 6
  491. $order_clause
  492. ";
  493. }
  494. if (api_is_multiple_url_enabled()) {
  495. $tbl_user_rel_access_url = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_USER);
  496. $access_url_id = api_get_current_access_url_id();
  497. if ($access_url_id != -1) {
  498. $sql = "
  499. SELECT u.id, lastname, firstname, username, su.session_id, official_code
  500. FROM $tbl_user u
  501. LEFT JOIN $tbl_session_rel_user su
  502. ON su.user_id = u.id
  503. AND su.session_id = $id_session
  504. AND su.relation_type <> ".SESSION_RELATION_TYPE_RRHH."
  505. INNER JOIN $tbl_user_rel_access_url url_user
  506. ON (url_user.user_id = u.id)
  507. WHERE
  508. access_url_id = $access_url_id
  509. $where_filter
  510. AND u.status <> ".DRH."
  511. AND u.status<>6
  512. $order_clause
  513. ";
  514. }
  515. }
  516. $result = Database::query($sql);
  517. $users = Database::store_result($result, 'ASSOC');
  518. foreach ($users as $uid => $user) {
  519. if ($user['session_id'] != $id_session) {
  520. $nosessionUsersList[$user['id']] = [
  521. 'fn' => $user['firstname'],
  522. 'ln' => $user['lastname'],
  523. 'un' => $user['username'],
  524. 'official_code' => $user['official_code'],
  525. ];
  526. unset($users[$uid]);
  527. }
  528. }
  529. unset($users); //clean to free memory
  530. // filling the correct users in list
  531. $sql = "
  532. SELECT u.id, lastname, firstname, username, su.session_id, official_code
  533. FROM $tbl_user u
  534. LEFT JOIN $tbl_session_rel_user su
  535. ON su.user_id = u.id
  536. AND su.session_id = $id_session
  537. AND su.relation_type <> ".SESSION_RELATION_TYPE_RRHH."
  538. INNER JOIN $tableRelSessionCourseUser sc
  539. ON (sc.session_id = su.session_id AND su.user_id = sc.user_id)
  540. WHERE
  541. sc.c_id = $courseId AND
  542. u.status <> ".DRH." AND u.status <> 6
  543. $order_clause
  544. ";
  545. if (api_is_multiple_url_enabled()) {
  546. $tbl_user_rel_access_url = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_USER);
  547. $access_url_id = api_get_current_access_url_id();
  548. if ($access_url_id != -1) {
  549. $sql = "
  550. SELECT u.id, lastname, firstname, username, su.session_id, official_code
  551. FROM $tbl_user u
  552. LEFT JOIN $tbl_session_rel_user su
  553. ON su.user_id = u.id
  554. AND su.session_id = $id_session
  555. AND su.relation_type <> ".SESSION_RELATION_TYPE_RRHH."
  556. INNER JOIN $tbl_user_rel_access_url url_user
  557. ON (url_user.user_id = u.id)
  558. INNER JOIN $tableRelSessionCourseUser sc
  559. ON (sc.session_id = su.session_id AND su.user_id = sc.user_id)
  560. WHERE
  561. sc.c_id = $courseId AND
  562. access_url_id = $access_url_id AND
  563. u.status <> ".DRH." AND
  564. u.status <> 6
  565. $order_clause
  566. ";
  567. }
  568. }
  569. $result = Database::query($sql);
  570. $users = Database::store_result($result, 'ASSOC');
  571. foreach ($users as $uid => $user) {
  572. if ($user['session_id'] == $id_session) {
  573. $sessionUsersList[$user['id']] = $user;
  574. if (array_key_exists($user['id'], $nosessionUsersList)) {
  575. unset($nosessionUsersList[$user['id']]);
  576. }
  577. }
  578. unset($users[$uid]);
  579. }
  580. unset($users); //clean to free memory
  581. }
  582. if ($add_type == 'multiple') {
  583. $link_add_type_unique =
  584. '<a href="'.api_get_self().'?course_id='.$courseId.'&id_session='.$id_session.'&add='.$addProcess.'&add_type=unique">'.
  585. Display::return_icon('single.gif').get_lang('SessionAddTypeUnique').'</a>';
  586. $link_add_type_multiple = Display::url(Display::return_icon('multiple.gif').get_lang('SessionAddTypeMultiple'), '');
  587. } else {
  588. $link_add_type_unique = Display::url(Display::return_icon('single.gif').get_lang('SessionAddTypeUnique'), '');
  589. $link_add_type_multiple =
  590. '<a href="'.api_get_self().'?course_id='.$courseId.'&id_session='.$id_session.'&amp;add='.$addProcess.'&amp;add_type=multiple">'
  591. .Display::return_icon('multiple.gif').get_lang('SessionAddTypeMultiple').'</a>';
  592. }
  593. ?>
  594. <div class="actions">
  595. <?php
  596. echo $link_add_type_unique;
  597. echo $link_add_type_multiple;
  598. ?>
  599. </div>
  600. <form name="formulaire" method="post"
  601. action="<?php echo api_get_self(); ?>?page=<?php echo $page; ?>&course_id=<?php echo $courseId; ?>&id_session=<?php echo $id_session; ?><?php if (!empty($addProcess)) {
  602. echo '&add=true';
  603. } ?>" <?php if ($ajax_search) {
  604. echo ' onsubmit="valide();"';
  605. } ?>>
  606. <?php echo '<legend>'.$tool_name.' ('.$session_info['name'].') - '.$courseInfo['title'].' </legend>'; ?>
  607. <?php
  608. if ($add_type == 'multiple') {
  609. if (is_array($extra_field_list)) {
  610. if (is_array($new_field_list) && count($new_field_list) > 0) {
  611. echo '<h3>'.get_lang('FilterUsers').'</h3>';
  612. foreach ($new_field_list as $new_field) {
  613. echo $new_field['name'];
  614. $varname = 'field_'.$new_field['variable'];
  615. $fieldtype = $new_field['type'];
  616. echo '&nbsp;<select name="'.$varname.'">';
  617. echo '<option value="0">--'.get_lang('Select').'--</option>';
  618. foreach ($new_field['data'] as $option) {
  619. $checked = '';
  620. if ($fieldtype == ExtraField::FIELD_TYPE_TAG) {
  621. if (isset($_POST[$varname])) {
  622. if ($_POST[$varname] == $option['tag']) {
  623. $checked = 'selected="true"';
  624. }
  625. }
  626. echo '<option value="'.$option['tag'].'" '.$checked.'>'.$option['tag'].'</option>';
  627. } else {
  628. if (isset($_POST[$varname])) {
  629. if ($_POST[$varname] == $option[1]) {
  630. $checked = 'selected="true"';
  631. }
  632. }
  633. echo '<option value="'.$option[1].'" '.$checked.'>'.$option[2].'</option>';
  634. }
  635. }
  636. echo '</select>';
  637. $extraHidden =
  638. $fieldtype == ExtraField::FIELD_TYPE_TAG ? '<input type="hidden" name="field_id" value="'
  639. .$option['field_id'].'" />' : '';
  640. echo $extraHidden;
  641. echo '&nbsp;&nbsp;';
  642. }
  643. echo '<input type="button" value="'.get_lang('Filter').'" onclick="validate_filter()" />';
  644. echo '<br /><br />';
  645. }
  646. }
  647. }
  648. ?>
  649. <input type="hidden" name="form_sent" value="1"/>
  650. <input type="hidden" name="add_type"/>
  651. <?php
  652. if (!empty($errorMsg)) {
  653. echo Display::return_message($errorMsg); //main API
  654. }
  655. ?>
  656. <div id="multiple-add-session" class="row">
  657. <div class="col-md-4">
  658. <div class="form-group">
  659. <label><?php echo get_lang('UserListInPlatform'); ?> </label>
  660. <?php
  661. if (!($add_type == 'multiple')) {
  662. ?>
  663. <input type="text" id="user_to_add" onkeyup="xajax_search_users(this.value,'single')"
  664. class="form-control"/>
  665. <div id="ajax_list_users_single" class="select-list-ajax"></div>
  666. <?php
  667. } else {
  668. ?>
  669. <div id="ajax_list_users_multiple">
  670. <select id="origin_users" name="nosessionUsersList[]" multiple="multiple" size="15"
  671. class="form-control">
  672. <?php
  673. foreach ($nosessionUsersList as $uid => $enreg) {
  674. ?>
  675. <option value="<?php echo $uid; ?>" <?php if (in_array($uid, $UserList)) {
  676. echo 'selected="selected"';
  677. } ?>>
  678. <?php
  679. $personName = $enreg['ln'].' '.$enreg['fn'].' ('.$enreg['un'].') '
  680. .$enreg['official_code'];
  681. if ($showOfficialCode) {
  682. $officialCode =
  683. !empty($enreg['official_code']) ? $enreg['official_code'].' - '
  684. : '? - ';
  685. $personName =
  686. $officialCode.$enreg['ln'].' '.$enreg['fn'].' ('.$enreg['un'].')';
  687. }
  688. echo $personName; ?>
  689. </option>
  690. <?php
  691. } ?>
  692. </select>
  693. </div>
  694. <?php
  695. }
  696. unset($nosessionUsersList);
  697. ?>
  698. </div>
  699. </div>
  700. <div class="col-md-4">
  701. <?php if ($add_type == 'multiple') {
  702. ?>
  703. <?php echo get_lang('FirstLetterUser'); ?> :
  704. <select id="first_letter_user" name="firstLetterUser" onchange="change_select(this.value);">
  705. <option value="%">--</option>
  706. <?php
  707. echo Display:: get_alphabet_options(); ?>
  708. </select>
  709. <br/>
  710. <br/>
  711. <?php
  712. } ?>
  713. <div class="control-course">
  714. <?php
  715. if ($ajax_search) {
  716. ?>
  717. <div class="separate-action">
  718. <button name="remove_user" class="btn btn-primary" type="button"
  719. onclick="remove_item(document.getElementById('destination_users'))">
  720. <em class="fa fa-chevron-left"></em>
  721. </button>
  722. </div>
  723. <?php
  724. } else {
  725. ?>
  726. <div class="separate-action">
  727. <button name="add_user" class="btn btn-primary" type="button"
  728. onclick="moveItem(document.getElementById('origin_users'), document.getElementById('destination_users'))"
  729. onclick="moveItem(document.getElementById('origin_users'), document.getElementById('destination_users'))">
  730. <em class="fa fa-chevron-right"></em>
  731. </button>
  732. </div>
  733. <div class="separate-action">
  734. <button name="remove_user" class="btn btn-primary" type="button"
  735. onclick="moveItem(document.getElementById('destination_users'), document.getElementById('origin_users'))"
  736. onclick="moveItem(document.getElementById('destination_users'), document.getElementById('origin_users'))">
  737. <em class="fa fa-chevron-left"></em>
  738. </button>
  739. </div>
  740. <?php
  741. }
  742. if (!empty($addProcess)) {
  743. echo '<button name="next" class="btn btn-success" type="button" value="" onclick="valide()" >'
  744. .get_lang('FinishSessionCreation').'</button>';
  745. } else {
  746. echo '<button name="next" class="btn btn-success" type="button" value="" onclick="valide()" >'
  747. .get_lang('SubscribeUsersToSession').'</button>';
  748. }
  749. ?>
  750. </div>
  751. </div>
  752. <div class="col-md-4">
  753. <label><?php echo get_lang('UserListInSession'); ?> :</label>
  754. <select id="destination_users" name="sessionUsersList[]" multiple="multiple" size="15"
  755. class="form-control">
  756. <?php
  757. foreach ($sessionUsersList as $enreg) {
  758. ?>
  759. <option value="<?php echo $enreg['id']; ?>">
  760. <?php
  761. $personName = $enreg['lastname'].' '.$enreg['firstname'].' ('.$enreg['username'].') '
  762. .$enreg['official_code'];
  763. if ($showOfficialCode) {
  764. $officialCode =
  765. !empty($enreg['official_code']) ? $enreg['official_code'].' - ' : '? - ';
  766. $personName =
  767. $officialCode.$enreg['lastname'].' '.$enreg['firstname'].' ('.$enreg['username']
  768. .')';
  769. }
  770. echo $personName; ?>
  771. </option>
  772. <?php
  773. }
  774. unset($sessionUsersList);
  775. ?>
  776. </select>
  777. </div>
  778. </div>
  779. </form>
  780. <script>
  781. function moveItem(origin, destination) {
  782. for (var i = 0; i < origin.options.length; i++) {
  783. if (origin.options[i].selected) {
  784. destination.options[destination.length] = new Option(origin.options[i].text, origin.options[i].value);
  785. origin.options[i] = null;
  786. i = i - 1;
  787. }
  788. }
  789. destination.selectedIndex = -1;
  790. sortOptions(destination.options);
  791. }
  792. function sortOptions(options) {
  793. newOptions = new Array();
  794. for (i = 0; i < options.length; i++)
  795. newOptions[i] = options[i];
  796. newOptions = newOptions.sort(mysort);
  797. options.length = 0;
  798. for (i = 0; i < newOptions.length; i++)
  799. options[i] = newOptions[i];
  800. }
  801. function mysort(a, b) {
  802. if (a.text.toLowerCase() > b.text.toLowerCase()) {
  803. return 1;
  804. }
  805. if (a.text.toLowerCase() < b.text.toLowerCase()) {
  806. return -1;
  807. }
  808. return 0;
  809. }
  810. function valide() {
  811. var options = document.getElementById('destination_users').options;
  812. for (i = 0; i < options.length; i++)
  813. options[i].selected = true;
  814. document.forms.formulaire.submit();
  815. }
  816. function loadUsersInSelect(select) {
  817. var xhr_object = null;
  818. if (window.XMLHttpRequest) // Firefox
  819. xhr_object = new XMLHttpRequest();
  820. else if (window.ActiveXObject) // Internet Explorer
  821. xhr_object = new ActiveXObject("Microsoft.XMLHTTP");
  822. else // XMLHttpRequest non supporté par le navigateur
  823. alert("Votre navigateur ne supporte pas les objets XMLHTTPRequest...");
  824. xhr_object.open("POST", "loadUsersInSelect.ajax.php");
  825. xhr_object.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
  826. nosessionUsers = makepost(document.getElementById('origin_users'));
  827. sessionUsers = makepost(document.getElementById('destination_users'));
  828. nosessionClasses = makepost(document.getElementById('origin_classes'));
  829. sessionClasses = makepost(document.getElementById('destination_classes'));
  830. xhr_object.send("nosessionusers=" + nosessionUsers + "&sessionusers=" + sessionUsers + "&nosessionclasses=" + nosessionClasses + "&sessionclasses=" + sessionClasses);
  831. xhr_object.onreadystatechange = function () {
  832. if (xhr_object.readyState == 4) {
  833. document.getElementById('content_source').innerHTML = result = xhr_object.responseText;
  834. }
  835. }
  836. }
  837. function makepost(select) {
  838. var options = select.options;
  839. var ret = "";
  840. for (i = 0; i < options.length; i++)
  841. ret = ret + options[i].value + '::' + options[i].text + ";;";
  842. return ret;
  843. }
  844. </script>
  845. <?php
  846. Display::display_footer();