access_url_edit_users_to_url_functions.lib.php 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * AccessUrlEditUsersToUrl class definition
  5. * Contains several functions dealing with displaying,
  6. * editing,... of a Access_url_edit_users_to_url_functions.
  7. *
  8. * @version 1.0
  9. *
  10. * @author Toon Keppens <toon@vi-host.net>
  11. * @author Julio Montoya - Cleaning code
  12. * @author Ricardo Rodriguez - Separated the function and code
  13. *
  14. * @package chamilo.library
  15. */
  16. class AccessUrlEditUsersToUrl
  17. {
  18. /**
  19. * Search users by username, firstname or lastname, based on the given
  20. * search string.
  21. *
  22. * @param string Search string
  23. * @param int Deprecated param
  24. *
  25. * @return xajaxResponse Xajax response block
  26. * @assert () === false
  27. */
  28. public static function search_users($needle, $id)
  29. {
  30. $tbl_user = Database::get_main_table(TABLE_MAIN_USER);
  31. $xajax_response = new xajaxResponse();
  32. $return = '';
  33. if (!empty($needle)) {
  34. // xajax send utf8 datas... datas in db can be non-utf8 datas
  35. $charset = api_get_system_encoding();
  36. $needle = api_convert_encoding($needle, $charset, 'utf-8');
  37. $needle = Database::escape_string($needle);
  38. // search users where username or firstname or lastname begins likes $needle
  39. $order_clause = api_sort_by_first_name() ? ' ORDER BY firstname, lastname, username' : ' ORDER BY lastname, firstname, username';
  40. $sql = 'SELECT u.user_id, username, lastname, firstname FROM '.$tbl_user.' u '.
  41. ' WHERE (username LIKE "'.$needle.'%" '.
  42. ' OR firstname LIKE "'.$needle.'%" '.
  43. ' OR lastname LIKE "'.$needle.'%") '.
  44. $order_clause.
  45. ' LIMIT 11';
  46. $rs = Database::query($sql);
  47. $i = 0;
  48. while ($user = Database::fetch_array($rs)) {
  49. $i++;
  50. if ($i <= 10) {
  51. $return .= '<a href="javascript: void(0);" onclick="javascript: add_user_to_url(\''.addslashes($user['user_id']).'\',\''.api_get_person_name(addslashes($user['firstname']), addslashes($user['lastname'])).' ('.addslashes($user['username']).')'.'\')">'.api_get_person_name($user['firstname'], $user['lastname']).' ('.$user['username'].')</a><br />';
  52. } else {
  53. $return .= '...<br />';
  54. }
  55. }
  56. }
  57. $xajax_response->addAssign(
  58. 'ajax_list_users',
  59. 'innerHTML',
  60. api_utf8_encode($return)
  61. );
  62. return $xajax_response;
  63. }
  64. }