access_url_edit_courses_to_url_functions.lib.php 2.3 KB

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