IndexableChunk.class.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. <?php
  2. // some constants to avoid serialize string keys on serialized data array
  3. define('SE_COURSE_ID', 0);
  4. define('SE_TOOL_ID', 1);
  5. define('SE_DATA', 2);
  6. define('SE_USER', 3);
  7. // in some cases we need top differenciate xapian documents of the same tool
  8. define('SE_DOCTYPE_EXERCISE_EXERCISE', 0);
  9. define('SE_DOCTYPE_EXERCISE_QUESTION', 1);
  10. // xapian prefixes
  11. define('XAPIAN_PREFIX_COURSEID','C');
  12. define('XAPIAN_PREFIX_TOOLID','O');
  13. abstract class _IndexableChunk
  14. {
  15. /* struct (array)
  16. * {
  17. * string title; <- nombre de archivo/elemento
  18. * string content; <- texto a indexar
  19. * string ids; <- los flags a guardar "cidReq:lp_id:path"
  20. * }
  21. */
  22. public $data;
  23. /**
  24. * array (
  25. * 'SE_COURSE_ID' => string <- course id from course table on main db
  26. * 'SE_TOOL_ID' => string <- tool id from mainapi lib constants
  27. * 'SE_DATA' => mixed <- extra information, depends on SE_TOOL_ID
  28. * 'SE_USER' => id <- user id from user table in main db
  29. * )
  30. */
  31. public $xapian_data;
  32. /**
  33. * array(
  34. * name => string
  35. * flag => char
  36. * )
  37. */
  38. public $terms;
  39. /**
  40. * Add a value to the indexed item
  41. * @param string Key
  42. * @param string Value
  43. * @return void
  44. */
  45. function addValue($key, $value) {
  46. $this->data[$key] = $value;
  47. }
  48. /**
  49. * Add a term (like xapian definition)
  50. * @param string Term
  51. * @param string Flag (one character)
  52. */
  53. public function addTerm($term, $flag) {
  54. global $charset;
  55. if (strlen($flag) == 1) {
  56. $this->terms[] = array('name' => api_convert_encoding(stripslashes($term),'UTF-8',$charset), 'flag' => $flag);
  57. }
  58. }
  59. /**
  60. * Class constructor. Just generates an empty 'data' array attribute
  61. */
  62. function __construct() {
  63. $this->data = array();
  64. }
  65. /**
  66. * Class desctructor. Unsets attributes.
  67. */
  68. function __destruct() {
  69. unset($this->data);
  70. unset($this->terms);
  71. }
  72. }
  73. /**
  74. * Extension of the _IndexableChunk class to make IndexableChunk extensible.
  75. */
  76. class IndexableChunk extends _IndexableChunk
  77. {
  78. /**
  79. * Let add course id term
  80. */
  81. public function addCourseId($course_id) {
  82. $this->addTerm($course_id, XAPIAN_PREFIX_COURSEID);
  83. }
  84. /**
  85. * Let add tool id term
  86. */
  87. public function addToolId($tool_id) {
  88. $this->addTerm($tool_id, XAPIAN_PREFIX_TOOLID);
  89. }
  90. }
  91. ?>