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.             // Get the glossary ID
  102.             $glossaryid = (int)$this->moodle_glossary_instance->id;
  103.             
  104.             // Construct a query
  105.             $sql_like sql_ilike();
  106.             $termquery "$sql_like '$term'";
  107.             if ($matchPartial$termquery "$sql_like '%$term%'";
  108.             
  109.             // Search concepts
  110.             $recs get_records_select('glossary_entries'"glossaryid = $glossaryid AND concept $termquery"'concept');
  111.             if (is_array($recs)) {
  112.                 foreach ($recs as $r{
  113.                     $entries[$r->idnew SloodleGlossaryEntry($r->id$r->concept$r->definition);
  114.                 }
  115.             }
  116.             
  117.             // Search aliases
  118.             if ($searchAliases{
  119.                 $recs get_records_select('glossary_alias'"alias $termquery");
  120.                 // Go through each alias found
  121.                 if (is_array($recs)) {
  122.                     foreach ($recs as $r{
  123.                         // First check if we already had this entry
  124.                         if (isset($entries[$r->entryid])) continue;
  125.                         // Check if this alias refers to an entry in our desired glossary
  126.                         $entry get_record('glossary_entries''glossaryid'$glossaryid'entryid'$r->entryid);
  127.                         if (!$entrycontinue;
  128.                         // Store the entry
  129.                         $entries[$entry->idnew SloodleGlossaryEntry($entry->id$entry->concept$entry->definition);
  130.                     }
  131.                 }
  132.             }
  133.             
  134.             // Search definitions
  135.             if ($searchDefinitions{
  136.                 $recs get_records_select('glossary_entries'"glossaryid = $glossaryid AND definition $sql_like '%$term%'"'concept');
  137.                 if (is_array($recs)) {
  138.                     foreach ($recs as $r{
  139.                         if (!isset($entries[$r->id])) $entries[$r->idnew SloodleGlossaryEntry($r->id$r->concept$r->definition);
  140.                     }
  141.                 }
  142.             }
  143.             
  144.             return $entries;
  145.         }
  146.         
  147.         
  148.     // ACCESSORS //
  149.     
  150.         /**
  151.         * Gets the name of this module instance.
  152.         * @return string The name of this controller
  153.         */
  154.         function get_name()
  155.         {
  156.             return $this->moodle_glossary_instance->name;
  157.         }
  158.         
  159.         /**
  160.         * Gets the intro description of this module instance, if available.
  161.         * @return string The intro description of this controller
  162.         */
  163.         function get_intro()
  164.         {
  165.             return $this->moodle_glossary_instance->intro;
  166.         }
  167.         
  168.         /**
  169.         * Gets the identifier of the course this controller belongs to.
  170.         * @return mixed Course identifier. Type depends on VLE. (In Moodle, it will be an integer).
  171.         */
  172.         function get_course_id()
  173.         {
  174.             return (int)$this->moodle_glossary_instance->course;
  175.         }
  176.         
  177.         /**
  178.         * Gets the time at which this instance was created, or 0 if unknown.
  179.         * @return int Timestamp
  180.         */
  181.         function get_creation_time()
  182.         {
  183.             return (int)$this->moodle_glossary_instance->timecreated;
  184.         }
  185.         
  186.         /**
  187.         * Gets the time at which this instance was last modified, or 0 if unknown.
  188.         * @return int Timestamp
  189.         */
  190.         function get_modification_time()
  191.         {
  192.             return (int)$this->moodle_glossary_instance->timemodified;
  193.         }
  194.         
  195.         
  196.         /**
  197.         * Gets the short type name of this instance.
  198.         * @return string 
  199.         */
  200.         function get_type()
  201.         {
  202.             return 'glossary';
  203.         }
  204.  
  205.         /**
  206.         * Gets the full type name of this instance, according to the current language pack, if available.
  207.         * Note: should be overridden by sub-classes.
  208.         * @return string Full type name if possible, or the short name otherwise.
  209.         */
  210.         function get_type_full()
  211.         {
  212.             return get_string('modulename''glossary');
  213.         }
  214.  
  215.     }
  216.     
  217.     
  218.     /**
  219.     * Represents a single glossary entry
  220.     * @package sloodle
  221.     */
  222.     class SloodleGlossaryEntry
  223.     {
  224.         /**
  225.         * Constructor - initialises members.
  226.         * @param mixed $id The ID of this message - type depends on VLE, but is typically an integer
  227.         * @param string $concept The name of this entry
  228.         * @param string $definition The definition of this entry
  229.         */
  230.         function SloodleGlossaryEntry($id$concept$definition)
  231.         {
  232.             $this->id = $id;
  233.             $this->concept = $concept;
  234.             $this->definition = $definition;
  235.         }
  236.         
  237.         /**
  238.         * Accessor - set all members in a single call.
  239.         * @param mixed $id The ID of this message - type depends on VLE, but is typically an integer
  240.         * @param string $concept The name of this entry
  241.         * @param string $definition The definition of this entry
  242.         */
  243.         function set($id$concept$definition)
  244.         {
  245.             $this->id = $id;
  246.             $this->concept = $concept;
  247.             $this->definition = $definition;
  248.         }
  249.         
  250.         /**
  251.         * The ID of the entry.
  252.         * The type depends on the VLE, but typically is an integer.
  253.         * @var mixed 
  254.         * @access public
  255.         */
  256.         var $id = 0;
  257.     
  258.         /**
  259.         * The name of this entry.
  260.         * @var string 
  261.         * @access public
  262.         */
  263.         var $concept = '';
  264.         
  265.         /**
  266.         * The definition of this entry.
  267.         * @var string 
  268.         * @access public
  269.         */
  270.         var $definition = '';
  271.     }
  272.  
  273.  
  274. ?>

Documentation generated on Fri, 17 Jul 2009 11:02:00 +0100 by phpDocumentor 1.4.0