access_url_edit_courses_to_url_functions.lib.php 2.3 KB

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