linker.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. <?php
  2. /**
  3. * Sloodle glossary linker (for Sloodle 0.3).
  4. * Allows a Sloodle MetaGloss to search a Moodle glossary.
  5. *
  6. * @package sloodleglossary
  7. * @copyright Copyright (c) 2007-8 Sloodle (various contributors)
  8. * @license http://www.gnu.org/licenses/gpl-3.0.html GNU GPL v3
  9. *
  10. * @todo Implement ability to add new entries?
  11. *
  12. * @contributor (various)
  13. * @contributor Peter R. Bloomfield
  14. */
  15. // This script should be called with the following parameters:
  16. // sloodlecontrollerid = ID of a Sloodle Controller through which to access Moodle
  17. // sloodlepwd = the prim password or object-specific session key to authenticate the request
  18. // sloodlemoduleid = ID of a glossary
  19. //
  20. // If requested with only the above parameters, then the glossary name will be returned on the first data line (status code 1).
  21. // The 'intro' (description), if available, will be provided on the second line, without any HTML markup.
  22. //
  23. // In order to search the glossary, the following parameters must also be specified:
  24. // sloodleterm = a string to search by
  25. // sloodleuuid = UUID of the avatar searching
  26. // sloodleavname = name of the avatar searching
  27. //
  28. // The following parameters are optional:
  29. // sloodlepartialmatches = if 'true' or '1' then searches will show partial matches too (defaults to true)
  30. // sloodlesearchaliases = if 'true' or '1' then searches will search by aliases too (defaults to false)
  31. // sloodlesearchdefinitions = if 'true' or '1' then searches will search by definitions too (defaults to false)
  32. //
  33. // sloodleserveraccesslevel = defines the access level to the resource (ignored if unspecified)
  34. //
  35. // The following parameter is optional:
  36. // sloodledebug = if 'true', then Sloodle debugging mode is activated
  37. // If successful, status code 1 will be returned.
  38. // Each data line will contain a concept and definition separated by a pipe: <concept>|<definition>
  39. /** Lets Sloodle know we are in a linker script. */
  40. define('SLOODLE_LINKER_SCRIPT', true);
  41. /** Grab the Sloodle/Moodle configuration. */
  42. require_once('../../init.php');
  43. /** Include the Sloodle PHP API. */
  44. require_once(SLOODLE_LIBROOT.'/sloodle_session.php');
  45. // Authenticate the request, and load a glossary module
  46. $sloodle = new SloodleSession();
  47. $sloodle->authenticate_request();
  48. $sloodle->load_module('glossary', true);
  49. // Has a search term been specified?
  50. $sloodleterm = $sloodle->request->optional_param('sloodleterm', null);
  51. if (empty($sloodleterm)) {
  52. // Just fetch the name of the glossary
  53. $sloodle->response->set_status_code(1);
  54. $sloodle->response->set_status_descriptor('OK');
  55. $sloodle->response->add_data_line($sloodle->module->get_name());
  56. $sloodle->response->add_data_line(strip_tags($sloodle->module->get_intro()));
  57. $sloodle->response->render_to_output();
  58. exit();
  59. }
  60. // If the server access level has been specified, then validate the user
  61. $serveraccesslevel = $sloodle->request->get_server_access_level(false);
  62. if ($serveraccesslevel !== null && $serveraccesslevel != 0) $sloodle->validate_user();
  63. else $sloodle->validate_user(false);
  64. // Get our other parameters
  65. $sloodleterm = addslashes($sloodleterm);
  66. $sloodlepartialmatches = $sloodle->request->optional_param('sloodlepartialmatches', 'true');
  67. $sloodlesearchaliases = $sloodle->request->optional_param('sloodlesearchaliases', 'false');
  68. $sloodlesearchdefinitions = $sloodle->request->optional_param('sloodlesearchdefinitions', 'false');
  69. // Convert our incoming parameters as necessary
  70. if (strcasecmp($sloodlepartialmatches, 'true') == 0 || $sloodlepartialmatches == '1') $sloodlepartialmatches = true;
  71. else $sloodlepartialmatches = false;
  72. if (strcasecmp($sloodlesearchaliases, 'true') == 0 || $sloodlesearchaliases == '1') $sloodlesearchaliases = true;
  73. else $sloodlesearchaliases = false;
  74. if (strcasecmp($sloodlesearchdefinitions, 'true') == 0 || $sloodlesearchdefinitions == '1') $sloodlesearchdefinitions = true;
  75. else $sloodlesearchdefinitions = false;
  76. // Search the glossary
  77. $results = $sloodle->module->search($sloodleterm, $sloodlepartialmatches, $sloodlesearchaliases, $sloodlesearchdefinitions);
  78. if (is_array($results)) {
  79. $sloodle->response->set_status_code(1);
  80. $sloodle->response->set_status_descriptor('OK');
  81. // Go through each result
  82. foreach ($results as $r) {
  83. $concept = sloodle_clean_for_output($r->concept);
  84. $def = sloodle_clean_for_output($r->definition);
  85. $sloodle->response->add_data_line(array($concept, $def));
  86. }
  87. } else {
  88. $sloodle->response->set_status_code(-103);
  89. $sloodle->response->set_status_descriptor('SYSTEM');
  90. $sloodle->response->add_data_line('Failed to search glossary');
  91. }
  92. // Output our response
  93. sloodle_debug('<pre>'); // For debug mode, lets us see the response in a browser
  94. $sloodle->response->render_to_output();
  95. sloodle_debug('</pre>');
  96. ?>