access_url_edit_courses_to_url_functions.lib.php 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * Definition of the Accessurleditcoursestourl class
  5. * @package chamilo.library
  6. */
  7. /**
  8. * Init
  9. */
  10. require_once ('xajax/xajax.inc.php');
  11. /**
  12. * Access_url_edit_courses_to_url class
  13. * Contains several functions dealing with displaying,
  14. * editing,... of a Access_url_edit_courses_to_url_functions
  15. *
  16. * @version 1.0
  17. * @author Toon Keppens <toon@vi-host.net>
  18. * @author Julio Montoya - Cleaning code
  19. * @author Ricardo Rodriguez - Separated the function and code
  20. */
  21. class Accessurleditcoursestourl
  22. {
  23. /**
  24. * Search for a list of available courses by title or code, based on
  25. * a given string
  26. * @param string String to search for
  27. * @param int Deprecated param
  28. * @return string A formatted, xajax answer block
  29. * @assert () === false
  30. */
  31. function search_courses($needle, $id)
  32. {
  33. global $tbl_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('ajax_list_courses','innerHTML',api_utf8_encode($return));
  60. return $xajax_response;
  61. }
  62. }