glossary_ajax_request.php 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /* @todo move this file in the inc/ajax/ folder */
  4. /**
  5. * Glossary ajax request code.
  6. *
  7. * @package chamilo.glossary
  8. */
  9. require_once __DIR__.'/../inc/global.inc.php';
  10. api_protect_course_script(true);
  11. /**
  12. * Search a term and return description from a glossary.
  13. */
  14. $charset = api_get_system_encoding();
  15. // Replace image path
  16. $path_image = api_get_path(WEB_COURSE_PATH).api_get_course_path();
  17. $path_image_search = '../..'.api_get_path(REL_COURSE_PATH).api_get_course_path();
  18. $glossaryId = isset($_POST['glossary_id']) ? (int) $_POST['glossary_id'] : 0;
  19. $description = get_lang('No results found');
  20. if (!empty($glossaryId)) {
  21. $description = GlossaryManager::get_glossary_term_by_glossary_id($glossaryId);
  22. $description = str_replace($path_image_search, $path_image, $description);
  23. } elseif (isset($_POST['glossary_data']) && $_POST['glossary_data'] == 'true') {
  24. // get_glossary_terms
  25. $glossary_data = GlossaryManager::get_glossary_terms();
  26. $glossary_all_data = [];
  27. if (count($glossary_data) > 0) {
  28. foreach ($glossary_data as $glossary_index => $glossary_value) {
  29. $glossary_all_data[] = $glossary_value['id'].'__|__|'.$glossary_value['name'];
  30. }
  31. $description = implode('[|.|_|.|-|.|]', $glossary_all_data);
  32. }
  33. } elseif (isset($_POST['glossary_name'])) {
  34. $glossaryName = Security::remove_XSS($_POST['glossary_name']);
  35. $glossaryName = api_convert_encoding($glossaryName, $charset, 'UTF-8');
  36. $glossaryName = trim($glossaryName);
  37. if (api_get_configuration_value('save_titles_as_html')) {
  38. $glossaryName = "%$glossaryName%";
  39. }
  40. $glossaryInfo = GlossaryManager::get_glossary_term_by_glossary_name($glossaryName);
  41. if (!empty($glossaryInfo)) {
  42. $description = str_replace(
  43. $path_image_search,
  44. $path_image,
  45. $glossaryInfo['description']
  46. );
  47. if (is_null($description) || strlen(trim($description)) == 0) {
  48. $description = get_lang('No results found');
  49. } else {
  50. $description = str_replace('class="glossary"', '', $description);
  51. }
  52. }
  53. }
  54. echo api_xml_http_response_encode($description);