get_terms.php 3.0 KB

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