Source for file module_distributor.php

Documentation is available at module_distributor.php

  1. <?php
  2.     // This file is part of the Sloodle project (www.sloodle.org)
  3.     
  4.     /**
  5.     * This file defines the Sloodle Distributor module.
  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 Distributor module class.
  21.     * @package sloodle
  22.     */
  23.     class SloodleModuleDistributor 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 - Sloodle module instance database object.
  37.         * Corresponds to one record from the Moodle 'sloodle' table.
  38.         * @var object 
  39.         * @access private
  40.         */
  41.         var $sloodle_module_instance = null;
  42.         
  43.         /**
  44.         * Internal only - Sloodle Distributor instance database object.
  45.         * Corresponds to one record from the Moodle 'sloodle_distributor' table.
  46.         * @var object 
  47.         * @access private
  48.         */
  49.         var $sloodle_distributor_instance = null;
  50.                 
  51.         
  52.     // FUNCTIONS //
  53.     
  54.         /**
  55.         * Constructor
  56.         */
  57.         function SloodleModuleDistributor(&$_session)
  58.         {
  59.             $constructor get_parent_class($this);
  60.             parent::$constructor($_session);
  61.         }
  62.         
  63.         /**
  64.         * Loads data from the database.
  65.         * Note: even if the function fails, it may still have overwritten some or all existing data in the object.
  66.         * @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)
  67.         * @return bool True if successful, or false otherwise
  68.         */
  69.         function load($id)
  70.         {
  71.             // Make sure the ID is valid
  72.             if (!is_int($id|| $id <= 0return false;
  73.             
  74.             // Fetch the course module data
  75.             if (!($this->cm = get_coursemodule_from_id('sloodle'$id))) return false;
  76.             // Load from the primary table: Sloodle instance
  77.             if (!($this->sloodle_module_instance = get_record('sloodle''id'$this->cm->instance))) return false;
  78.             // Check that it is the correct type
  79.             if ($this->sloodle_module_instance->type != SLOODLE_TYPE_DISTRIBreturn false;
  80.             
  81.             // Load from the secondary table: Distributor instance
  82.             if (!($this->sloodle_distributor_instance = get_record('sloodle_distributor''sloodleid'$this->cm->instance))) return false;
  83.             
  84.             return true;
  85.         }
  86.         
  87.         
  88.         /**
  89.         * Gets a list of all objects for this Distributor.
  90.         * @return array An array of strings, each string containing the name of an object in this Distributor.
  91.         */
  92.         function get_objects()
  93.         {
  94.             // Get all distributor record entries for this distributor, sorted alphabetically
  95.             $recs get_records('sloodle_distributor_entry''distributorid'$this->sloodle_distributor_instance->id'name');
  96.             if (!$recsreturn array();
  97.             // Convert it to an array of strings
  98.             $entries array();
  99.             foreach ($recs as $r{
  100.                 $entries[$recs->name;
  101.             }
  102.             
  103.             return $entries;
  104.         }
  105.         
  106.         
  107.         /**
  108.         * Sets the list of objects in this Distributor
  109.         * @param array $objects An array of strings, each string containing the name of an object in the Distributor.
  110.         * @return bool True if successful, or false if not
  111.         */
  112.         function set_objects($objects)
  113.         {
  114.             // Delete all existing records for this Distributor
  115.             delete_records('sloodle_distributor_entry''distributorid'$this->sloodle_distributor_instance->id);
  116.             
  117.             // Go through each new entry
  118.             $result true;
  119.             foreach ($objects as $o{
  120.                 // Construct the new record
  121.                 $rec new stdClass();
  122.                 $rec->distributorid $this->sloodle_distributor_instance->id;
  123.                 $rec->name sloodle_clean_for_db($o);
  124.                 // Insert it
  125.                 if (!insert_record('sloodle_distributor_entry'$rec)) $result false;
  126.             }
  127.         
  128.             return $result;
  129.         }
  130.         
  131.         /**
  132.         * Sets the UUID of the XMLRPC channel used for requests.
  133.         * @param string $uuid The UUID of an XMLRPC channel
  134.         * @return bool True if successful, or false if not
  135.         */
  136.         function set_channel($uuid)
  137.         {
  138.             // Update the values
  139.             $this->sloodle_distributor_instance->channel $uuid;
  140.             $this->sloodle_distributor_instance->timeupdated time();
  141.             // Update the database
  142.             return update_record('sloodle_distributor'$this->sloodle_distributor_instance);
  143.         }
  144.         
  145.         
  146.         /**
  147.         * Request that the specified object be sent to the specified avatar.
  148.         * @param string $objname Name of the object to send
  149.         * @param string $uuid UUID of the avatar to send the object to
  150.         * @return bool True if successful, or false if not.
  151.         */
  152.         function send_object($objname$uuid)
  153.         {
  154.             // Check that the object exists in this distributor
  155.             if (!record_exists('sloodle_distributor_entry''distributorid'$this->distrib_id'name'addslashes($objname))) return false;
  156.             // Send the XMLRPC request
  157.             return sloodle_send_xmlrpc_message($this->sloodle_distributor_instance->channel0"1|OK\\nSENDOBJECT|$uuid|$objname");
  158.         }
  159.         
  160.         
  161.     // ACCESSORS //
  162.     
  163.         /**
  164.         * Gets the name of this module instance.
  165.         * @return string The name of this controller
  166.         */
  167.         function get_name()
  168.         {
  169.             return $this->sloodle_module_instance->name;
  170.         }
  171.         
  172.         /**
  173.         * Gets the intro description of this module instance, if available.
  174.         * @return string The intro description of this controller
  175.         */
  176.         function get_intro()
  177.         {
  178.             return $this->sloodle_module_instance->intro;
  179.         }
  180.         
  181.         /**
  182.         * Gets the identifier of the course this controller belongs to.
  183.         * @return mixed Course identifier. Type depends on VLE. (In Moodle, it will be an integer).
  184.         */
  185.         function get_course_id()
  186.         {
  187.             return (int)$this->sloodle_module_instance->course;
  188.         }
  189.         
  190.         /**
  191.         * Gets the time at which this instance was created, or 0 if unknown.
  192.         * @return int Timestamp
  193.         */
  194.         function get_creation_time()
  195.         {
  196.             return $this->sloodle_module_instance->timecreated;
  197.         }
  198.         
  199.         /**
  200.         * Gets the time at which this instance was last modified, or 0 if unknown.
  201.         * @return int Timestamp
  202.         */
  203.         function get_modification_time()
  204.         {
  205.             return $this->sloodle_module_instance->timemodified;
  206.         }
  207.         
  208.         
  209.         /**
  210.         * Gets the short type name of this instance.
  211.         * @return string 
  212.         */
  213.         function get_type()
  214.         {
  215.             return SLOODLE_TYPE_DISTRIB;
  216.         }
  217.  
  218.         /**
  219.         * Gets the full type name of this instance, according to the current language pack, if available.
  220.         * Note: should be overridden by sub-classes.
  221.         * @return string Full type name if possible, or the short name otherwise.
  222.         */
  223.         function get_type_full()
  224.         {
  225.             return get_string('moduletype:'.SLOODLE_TYPE_DISTRIB'sloodle');
  226.         }
  227.  
  228.     }
  229.  
  230.  
  231. ?>

Documentation generated on Fri, 17 Jul 2009 11:01:59 +0100 by phpDocumentor 1.4.0