module_glossary.php 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. <?php
  2. // This file is part of the Sloodle project (www.sloodle.org)
  3. /**
  4. * This file defines a glossary module for Sloodle.
  5. *
  6. * @package sloodle
  7. * @copyright Copyright (c) 2008 Sloodle (various contributors)
  8. * @license http://www.gnu.org/licenses/gpl-3.0.html GNU GPL v3
  9. *
  10. * @contributor Peter R. Bloomfield
  11. */
  12. /** The Sloodle module base. */
  13. require_once(SLOODLE_LIBROOT.'/modules/module_base.php');
  14. /** General Sloodle functions. */
  15. require_once(SLOODLE_LIBROOT.'/general.php');
  16. /**
  17. * The Sloodle glossary module class.
  18. * @package sloodle
  19. */
  20. class SloodleModuleGlossary extends SloodleModule
  21. {
  22. // DATA //
  23. /**
  24. * Internal for Moodle only - course module instance.
  25. * Corresponds to one record from the Moodle 'course_modules' table.
  26. * @var object
  27. * @access private
  28. */
  29. var $cm = null;
  30. /**
  31. * Internal only - Moodle glossary module instance database object.
  32. * Corresponds to one record from the Moodle 'glossary' table.
  33. * @var object
  34. * @access private
  35. */
  36. var $moodle_glossary_instance = null;
  37. // FUNCTIONS //
  38. /**
  39. * Constructor
  40. */
  41. function SloodleModuleGlossary(&$_session)
  42. {
  43. $constructor = get_parent_class($this);
  44. parent::$constructor($_session);
  45. }
  46. /**
  47. * Loads data from the database.
  48. * Note: even if the function fails, it may still have overwritten some or all existing data in the object.
  49. * @param mixed $id The site-wide unique identifier for all modules. Type depends on VLE. On Moodle, it is an integer course module identifier ('id' field of 'course_modules' table)
  50. * @return bool True if successful, or false otherwise
  51. */
  52. function load($id)
  53. {
  54. // Make sure the ID is valid
  55. $id = (int)$id;
  56. if ($id <= 0) return false;
  57. // Fetch the course module data
  58. if (!($this->cm = get_coursemodule_from_id('glossary', $id))) {
  59. sloodle_debug("Failed to load course module instance #$id.<br/>");
  60. return false;
  61. }
  62. // Make sure the module is visible
  63. if ($this->cm->visible == 0) {
  64. sloodle_debug("Error: course module instance #$id not visible.<br/>");
  65. return false;
  66. }
  67. // Load from the primary table: glossary instance
  68. if (!($this->moodle_glossary_instance = sloodle_get_record('glossary', 'id', $this->cm->instance))) {
  69. sloodle_debug("Failed to load glossary with instance ID #{$cm->instance}.<br/>");
  70. return false;
  71. }
  72. return true;
  73. }
  74. /**
  75. * Searches all entries in the current glossary for the given term.
  76. * @param string $term The term to search for
  77. * @param bool $matchPartial If TRUE (default) then partial matches will be returned (e.g. 'vat' would partially match 'avatar')
  78. * @param bool $searchAliases If TRUE (not default) then aliases will be searched as well
  79. * @param bool $searchDefinitions If TRUE (not default) then definitions will be searched as well (can be SLOW, and always gets partial matches)
  80. * @return array An array of {@link SloodleGlossaryEntry} objects
  81. */
  82. function search($term, $matchPartial = true, $searchAliases = false, $searchDefinitions = false)
  83. {
  84. // This array will store the results associatively, ID => SloodleGlossaryDefinition
  85. $entries = array();
  86. // Get the glossary ID
  87. $glossaryid = (int)$this->moodle_glossary_instance->id;
  88. // Construct a query
  89. $sql_like = sloodle_sql_ilike();
  90. $termparam = '';
  91. if ($matchPartial) {
  92. $termparam = "%$term%";
  93. } else {
  94. $termparam = $term;
  95. }
  96. // Search concepts
  97. $recs = sloodle_get_records_select_params('glossary_entries', "glossaryid = ? AND concept $sql_like ?", array($glossaryid, $termparam), 'concept');
  98. if (is_array($recs)) {
  99. foreach ($recs as $r) {
  100. $entries[$r->id] = new SloodleGlossaryEntry($r->id, $r->concept, $r->definition);
  101. }
  102. }
  103. // Search aliases
  104. if ($searchAliases) {
  105. $recs = sloodle_get_records_select_params('glossary_alias', "alias $sql_like ?", array($termparam));
  106. // Go through each alias found
  107. if (is_array($recs)) {
  108. foreach ($recs as $r) {
  109. // First check if we already had this entry
  110. if (isset($entries[$r->entryid])) continue;
  111. // Check if this alias refers to an entry in our desired glossary
  112. $entry = sloodle_get_record('glossary_entries', 'glossaryid', $glossaryid, 'entryid', $r->entryid);
  113. if (!$entry) continue;
  114. // Store the entry
  115. $entries[$entry->id] = new SloodleGlossaryEntry($entry->id, $entry->concept, $entry->definition);
  116. }
  117. }
  118. }
  119. // Search definitions
  120. if ($searchDefinitions) {
  121. $recs = sloodle_get_records_select_params('glossary_entries', "glossaryid = ? AND definition $sql_like ?", array($glossaryid, "%$term%"), 'concept');
  122. if (is_array($recs)) {
  123. foreach ($recs as $r) {
  124. if (!isset($entries[$r->id])) $entries[$r->id] = new SloodleGlossaryEntry($r->id, $r->concept, $r->definition);
  125. }
  126. }
  127. }
  128. return $entries;
  129. }
  130. // ACCESSORS //
  131. /**
  132. * Gets the name of this module instance.
  133. * @return string The name of this controller
  134. */
  135. function get_name()
  136. {
  137. return $this->moodle_glossary_instance->name;
  138. }
  139. /**
  140. * Gets the intro description of this module instance, if available.
  141. * @return string The intro description of this controller
  142. */
  143. function get_intro()
  144. {
  145. return $this->moodle_glossary_instance->intro;
  146. }
  147. /**
  148. * Gets the identifier of the course this controller belongs to.
  149. * @return mixed Course identifier. Type depends on VLE. (In Moodle, it will be an integer).
  150. */
  151. function get_course_id()
  152. {
  153. return (int)$this->moodle_glossary_instance->course;
  154. }
  155. /**
  156. * Gets the time at which this instance was created, or 0 if unknown.
  157. * @return int Timestamp
  158. */
  159. function get_creation_time()
  160. {
  161. return (int)$this->moodle_glossary_instance->timecreated;
  162. }
  163. /**
  164. * Gets the time at which this instance was last modified, or 0 if unknown.
  165. * @return int Timestamp
  166. */
  167. function get_modification_time()
  168. {
  169. return (int)$this->moodle_glossary_instance->timemodified;
  170. }
  171. /**
  172. * Gets the short type name of this instance.
  173. * @return string
  174. */
  175. function get_type()
  176. {
  177. return 'glossary';
  178. }
  179. /**
  180. * Gets the full type name of this instance, according to the current language pack, if available.
  181. * Note: should be overridden by sub-classes.
  182. * @return string Full type name if possible, or the short name otherwise.
  183. */
  184. function get_type_full()
  185. {
  186. return get_string('modulename', 'glossary');
  187. }
  188. }
  189. /**
  190. * Represents a single glossary entry
  191. * @package sloodle
  192. */
  193. class SloodleGlossaryEntry
  194. {
  195. /**
  196. * Constructor - initialises members.
  197. * @param mixed $id The ID of this message - type depends on VLE, but is typically an integer
  198. * @param string $concept The name of this entry
  199. * @param string $definition The definition of this entry
  200. */
  201. function SloodleGlossaryEntry($id, $concept, $definition)
  202. {
  203. $this->id = $id;
  204. $this->concept = $concept;
  205. $this->definition = $definition;
  206. }
  207. /**
  208. * Accessor - set all members in a single call.
  209. * @param mixed $id The ID of this message - type depends on VLE, but is typically an integer
  210. * @param string $concept The name of this entry
  211. * @param string $definition The definition of this entry
  212. */
  213. function set($id, $concept, $definition)
  214. {
  215. $this->id = $id;
  216. $this->concept = $concept;
  217. $this->definition = $definition;
  218. }
  219. /**
  220. * The ID of the entry.
  221. * The type depends on the VLE, but typically is an integer.
  222. * @var mixed
  223. * @access public
  224. */
  225. var $id = 0;
  226. /**
  227. * The name of this entry.
  228. * @var string
  229. * @access public
  230. */
  231. var $concept = '';
  232. /**
  233. * The definition of this entry.
  234. * @var string
  235. * @access public
  236. */
  237. var $definition = '';
  238. }
  239. ?>