access_url_edit_courses_to_url_functions.lib.php 2.2 KB

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