Source for file module_glossary.php

Documentation is available at module_glossary.php

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

Documentation generated on Mon, 07 Jul 2008 12:32:58 +0100 by phpDocumentor 1.4.0