get_terms.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * This script retrieves a list of terms that have xapian documents
  5. * related with the term passed.
  6. *
  7. * @package chamilo.include.search
  8. */
  9. $terms_list = [];
  10. // verify parameter and return a right value to avoid problems parsing it
  11. if (empty($_GET['term']) || empty($_GET['prefix']) || !in_array($_GET['operator'], ['or', 'and'])) {
  12. echo json_encode($terms_list);
  13. return;
  14. }
  15. require_once __DIR__.'../../../global.inc.php';
  16. require_once api_get_path(LIBRARY_PATH).'search/ChamiloQuery.php';
  17. /**
  18. * Search with filter and build base array avoiding repeated terms.
  19. *
  20. * @param array $filter XapianQuery array
  21. * @param array $specific_fields
  22. *
  23. * @return array $sf_terms
  24. */
  25. function get_usual_sf_terms($filter, $specific_fields)
  26. {
  27. $sf_terms = [];
  28. $dkterms = chamilo_query_simple_query('', 0, 1000, $filter);
  29. if (is_array($dkterms) && is_array($dkterms[1])) {
  30. foreach ($specific_fields as $specific_field) {
  31. foreach ($dkterms[1] as $obj) {
  32. foreach ($obj['sf-'.$specific_field['code']] as $raw_term) {
  33. if (count($raw_term['name']) > 1) {
  34. $normal_term = substr($raw_term['name'], 1);
  35. $sf_terms[$specific_field['code']][$normal_term] = $normal_term;
  36. }
  37. }
  38. }
  39. }
  40. }
  41. return $sf_terms;
  42. }
  43. $term = $_GET['term'];
  44. $prefix = $_GET['prefix'];
  45. $operator = $_GET['operator'];
  46. $specific_fields = get_specific_field_list();
  47. $sf_terms = [];
  48. if (($cid = api_get_course_id()) != -1) { // with cid
  49. // course filter
  50. $filter[] = chamilo_get_boolean_query(XAPIAN_PREFIX_COURSEID.$cid);
  51. // term filter
  52. if ($term != '__all__') {
  53. $filter[] = chamilo_get_boolean_query($prefix.$term);
  54. // always and between term and courseid
  55. $filter = chamilo_join_queries($filter, null, 'and');
  56. }
  57. $sf_terms = get_usual_sf_terms($filter, $specific_fields);
  58. } else { // without cid
  59. if ($term != '__all__') {
  60. $filter[] = chamilo_get_boolean_query($prefix.$term);
  61. $sf_terms = get_usual_sf_terms($filter, $specific_fields);
  62. } else { // no cid and all/any terms
  63. foreach ($specific_fields as $specific_field) {
  64. foreach (xapian_get_all_terms(1000, $specific_field['code']) as $raw_term) {
  65. if (count($raw_term['name']) > 1) {
  66. $normal_term = substr($raw_term['name'], 1);
  67. $sf_terms[$specific_field['code']][$normal_term] = $normal_term;
  68. }
  69. }
  70. }
  71. }
  72. }
  73. // build array to return
  74. foreach ($sf_terms as $sf_prefix => $term_group) {
  75. //if (count($tem_group) > 0) {
  76. $first_term = ['__all__' => ($operator == 'or' ? '-- Any --' : '-- All -- ')];
  77. //}
  78. if ($sf_prefix != $prefix) {
  79. $terms_list[] = [
  80. 'prefix' => $sf_prefix,
  81. 'terms' => array_merge($first_term, $term_group),
  82. ];
  83. }
  84. }
  85. echo json_encode($terms_list);