get_terms.php 2.9 KB

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