Source for file module_avilister.php

Documentation is available at module_avilister.php

  1. <?php
  2.     // This file is part of the Sloodle project (www.sloodle.org)
  3.     
  4.     /**
  5.     * This file defines an AviLister 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.     /**
  21.     * The Sloodle AviLister module class.
  22.     * Search for real/avatar name associations.
  23.     * @package sloodle
  24.     */
  25.     class SloodleModuleAviLister extends SloodleModule
  26.     {
  27.     // DATA //
  28.     
  29.         //... None...
  30.                 
  31.         
  32.     // FUNCTIONS //
  33.     
  34.         /**
  35.         * Constructor
  36.         */
  37.         function SloodleModuleAviLister(&$_session)
  38.         {
  39.             $constructor get_parent_class($this);
  40.             parent::$constructor($_session);
  41.         }
  42.         
  43.         
  44.         /**
  45.         * Fetch the real name to match the given avatar.
  46.         * Either parameter can be used. If the UUID is empty, then the name is used.
  47.         * If both are specified, then the UUID is used first, but with the name as a fall-back.
  48.         * @param string $avatar_uuid Avatar UUID
  49.         * @param string $avatar_name Full avatar name
  50.         * @return string|boolThe full real name of the user if found, or FALSE otherwise.
  51.         */
  52.         function find_real_name($avatar_uuid$avatar_name)
  53.         {
  54.             // Attempt to find an avatar
  55.             $user new SloodleUser($this->_session);
  56.             if (!$user->load_avatar($avatar_uuid$avatar_name)) return false;
  57.             // Attempt to load a linked Moodle user
  58.             if (!$user->load_linked_user()) return false;
  59.             return ($user->get_user_firstname(.' '$user->get_user_lastname());
  60.         }
  61.         
  62.         
  63.         /**
  64.         * Lookup the realnames for all the given avatar names.
  65.         * Note: the output array may not correspond to the input array, as avatars/users who are not found will not be returned.
  66.         * @param array $avatar_names An array of avatar names
  67.         * @return array An associative array of avatar names to (full) real names
  68.         */
  69.         function find_real_names($avatar_names)
  70.         {
  71.             // Make sure the input is an array
  72.             if (!is_array($avatar_names)) return array();
  73.             $user new SloodleUser($this->_session);            
  74.             $output array();
  75.             
  76.             // Go through each input name
  77.             foreach ($avatar_names as $avname{
  78.                 // Attempt to load the given avatar and the linked user
  79.                 if (!$user->load_avatar(null$avname)) continue;
  80.                 if (!$user->load_linked_user()) continue;
  81.                 // Store the name association
  82.                 $output[$avname($user->get_user_firstname(.' '$user->get_user_lastname());
  83.             }
  84.             
  85.             return $output;
  86.         }
  87.         
  88.         
  89.         /**
  90.         * Lookup the avatar/real name associations for everybody in the current course.
  91.         * NOTE: fails if a course is not loaded in the current {@link SloodleSession}
  92.         * @return array|boolAn associative array of avatar names to (full) real names if successful, or false if a course is not available
  93.         */
  94.         function find_real_names_course()
  95.         {
  96.             // Make sure we have a course loaded
  97.             if (!isset($this->_session)) return false;
  98.             if (!$this->_session->course->is_loaded()) return false;
  99.             $user new SloodleUser($this->_session);            
  100.             // Get a list of users of the course
  101.             $courseusers get_course_users($this->_session->course->get_course_id());
  102.             if (!is_array($courseusers)) $courseusers array();
  103.             
  104.             // Go through each user
  105.             $output array();
  106.             foreach ($courseusers as $u{
  107.                 // Attempt to fetch the avatar data for this Moodle user
  108.                 $user->load_user($u->id);
  109.                 if (!$user->load_linked_avatar()) continue;
  110.                 // Store the name association
  111.                 $output[$user->get_avatar_name()($u->firstname.' '.$u->lastname);
  112.             }
  113.             
  114.             return $output;
  115.         }
  116.         
  117.         
  118.     // ACCESSORS //
  119.     
  120.         /**
  121.         * Gets the name of this module instance.
  122.         * @return string The name of this instance
  123.         */
  124.         function get_name()
  125.         {
  126.             return 'AviLister';
  127.         }
  128.         
  129.         /**
  130.         * Gets the short type name of this instance.
  131.         * @return string 
  132.         */
  133.         function get_type()
  134.         {
  135.             return 'avilister';
  136.         }
  137.  
  138.         /**
  139.         * Gets the full type name of this instance, according to the current language pack, if available.
  140.         * Note: should be overridden by sub-classes.
  141.         * @return string Full type name if possible, or the short name otherwise.
  142.         */
  143.         function get_type_full()
  144.         {
  145.             return 'AviLister';
  146.         }
  147.  
  148.     }
  149.     
  150.     
  151.     /**
  152.     * Represents a single avatar list name association
  153.     * @package sloodle
  154.     */
  155.     class SloodleAviListName
  156.     {
  157.         /**
  158.         * Constructor - initialises members.
  159.         * @param string $avatar_name The user's full avatar name
  160.         * @param string $real_first_name The user's real first name
  161.         * @param string $real_last_name The user's real last name
  162.         */
  163.         function SloodleChatMessage($avatar_name=''$real_first_name=''$real_last_name='')
  164.         {
  165.             $this->avatar_name = $avatar_name;
  166.             $this->real_first_name = $real_first_name;
  167.             $this->real_last_name = $real_last_name;
  168.         }
  169.         
  170.         /**
  171.         * Accessor - set all members in a single call.
  172.         * @param string $avatar_name The user's full avatar name
  173.         * @param string $real_first_name The user's real first name
  174.         * @param string $real_last_name The user's real last name
  175.         * @return void 
  176.         */
  177.         function set($avatar_name$real_first_name$real_last_name)
  178.         {
  179.             $this->avatar_name = $avatar_name;
  180.             $this->real_first_name = $real_first_name;
  181.             $this->real_last_name = $real_last_name;
  182.         }
  183.         
  184.         /**
  185.         * Gets the full real name of the user ("firstname lastname")
  186.         * @return string 
  187.         */
  188.         function get_full_real_name()
  189.         {
  190.             return $this->real_first_name .' '$this->real_last_name;
  191.         }
  192.         
  193.                 
  194.         /**
  195.         * Avatar name ("firstname lastname")
  196.         * @var string 
  197.         * @access public
  198.         */
  199.         var $avatar_name = '';
  200.     
  201.         /**
  202.         * Real first name
  203.         * @var string 
  204.         * @access public
  205.         */
  206.         var $real_first_name = '';
  207.         
  208.         /**
  209.         * Real last name
  210.         * @var string 
  211.         * @access public
  212.         */
  213.         var $real_last_name = '';
  214.     }
  215.  
  216.  
  217. ?>

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