Source for file sl_generallib.php

Documentation is available at sl_generallib.php

  1. <?php
  2.     
  3.     /**
  4.     * Sloodle general library.
  5.     *
  6.     * Provides various utility functionality for general Sloodle purposes.
  7.     *
  8.     * @package sloodle
  9.     * @copyright Copyright (c) 2007-8 Sloodle (various contributors)
  10.     * @license http://www.gnu.org/licenses/gpl-3.0.html GNU GPL v3
  11.     *
  12.     * @contributor Edmund Edgar
  13.     * @contributor Peter R. Bloomfield
  14.     *
  15.     */
  16.     
  17.     // This library expects that the Sloodle config file has already been included
  18.     //  (along with the Moodle libraries)
  19.     
  20.     
  21.     /** Include the IO library. */
  22.     require_once(SLOODLE_DIRROOT.'/lib/sl_iolib.php');
  23.     
  24.     
  25.     /**
  26.     * Sets a Sloodle configuration value.
  27.     * This data will be stored in Moodle's "config" table, so it will persist even after Sloodle is uninstalled.
  28.     * After being set, it will be available (read-only) as a named member of Moodle's $CFG variable.
  29.     * <b>NOTE:</b> in Sloodle debug mode, this function will terminate the script with an error if the name is not prefixed with "sloodle_".
  30.     * @param string $name The name of the value to be stored (should be prefixed with "sloodle_")
  31.     * @param string $value The string representation of the value to be stored
  32.     * @return bool True on success, or false on failure (may fail if database query encountered an error)
  33.     * @see sloodle_get_config()
  34.     */
  35.     function sloodle_set_config($name$value)
  36.     {
  37.         // If in debug mode, ensure the name is prefixed appropriately for Sloodle
  38.         if (defined('SLOODLE_DEBUG'&& SLOODLE_DEBUG{
  39.             if (substr_count($name'sloodle_'1{
  40.                 exit ("ERROR: sloodle_set_config(..) called with invalid value name \"$name\". Expected \"sloodle_\" prefix.");
  41.             }
  42.         }
  43.         // Use the standard Moodle config function, ignoring the 3rd parameter ("plugin", which defaults to NULL)
  44.         return set_config(strtolower($name)$value);
  45.     }
  46.  
  47.     /**
  48.     * Gets a Sloodle configuration value from Moodle's "config" table.
  49.     * This function does not necessarily need to be used.
  50.     * All configuration data is available as named members of Moodle's $CFG global variable.
  51.     * <b>NOTE:</b> in Sloodle debug mode, this function will terminate the script with an error if the name is not prefixed with "sloodle_".
  52.     * @param string $name The name of the value to be stored (should be prefixed with "sloodle_")
  53.     * @return mixed A string containing the configuration value, or false if the query failed (e.g. if the named value didn't exist)
  54.     * @see sloodle_set_config()
  55.     */
  56.     function sloodle_get_config($name)
  57.     {
  58.         // If in debug mode, ensure the name is prefixed appropriately for Sloodle
  59.         if (defined('SLOODLE_DEBUG'&& SLOODLE_DEBUG{
  60.             if (substr_count($name'sloodle_'1{
  61.                 exit ("ERROR: sloodle_get_config(..) called with invalid value name \"$name\". Expected \"sloodle_\" prefix.");
  62.             }
  63.         }
  64.         // Use the Moodle config function, ignoring the plugin parameter
  65.         $val get_config(NULLstrtolower($name));
  66.         // Older Moodle versions return a database record object instead of the value itself
  67.         // Workaround:
  68.         if (is_object($val)) return $val->value;
  69.         return $val;
  70.     }
  71.  
  72.     /**
  73.     * Gets the site-wide Sloodle prim password from the configuration table.
  74.     * @return mixed A string containing the prim password, or FALSE if no password has yet been specified
  75.     * @see sloodle_set_prim_password()
  76.     * @see sloodle_get_config()
  77.     */
  78.     function sloodle_get_prim_password()
  79.     {
  80.         return sloodle_get_config('sloodle_prim_password');
  81.     }
  82.     
  83.     /**
  84.     * Sets the site-wide Sloodle prim password from the configuration table.
  85.     * <b>Note:</b> this functio peforms no validation on the input value, except to determine that it is a string.
  86.     * @param string $password A string containing the prim password
  87.     * @return bool True if the database query was successful, or false otherwise
  88.     * @see sloodle_get_prim_password()
  89.     * @see sloodle_set_config()
  90.     */
  91.     function sloodle_set_prim_password($password)
  92.     {
  93.         // Make sure it's a string
  94.         if (!is_string($password)) return FALSE;
  95.         return sloodle_set_config('sloodle_prim_password'$password);
  96.     }
  97.     
  98.     /**
  99.     * Old form of the {@link: sloodle_set_prim_password()} function.
  100.     * Now deliberately terminates the script if called.
  101.     * <b>DO NOT USE!</b>
  102.     * @deprecated
  103.     * @return void 
  104.     * @see sloodle_get_prim_password()
  105.     */
  106.     function sloodle_prim_password()
  107.     {
  108.         exit("***** Old sloodle_prim_password() function called from: ".$_SERVER['PHP_SELF']." *****");
  109.     }
  110.     
  111.     /**
  112.     * Determines whether or not automatic registration is enabled.
  113.     * @return bool True if automatic registration is enabled, or false otherwise.
  114.     * @see sloodle_get_auth_method()
  115.     * @see sloodle_set_auth_method()
  116.     * @see sloodle_get_config()
  117.     */
  118.     {
  119.         // Get the auth method from the config table
  120.         $method sloodle_get_config('sloodle_auth_method');
  121.         // Is it autoreg?
  122.         return ($method === 'autoregister');
  123.     }
  124.     
  125.     /**
  126.     * Gets the site-wide authentication from the configuration table.
  127.     * @return mixed A string containing the authentication method, or FALSE if none has yet been specified
  128.     * @see sloodle_is_automatic_registration_on()
  129.     * @see sloodle_set_auth_method()
  130.     * @see sloodle_get_config()
  131.     */
  132.     function sloodle_get_auth_method()
  133.     {
  134.         return sloodle_get_config('sloodle_auth_method');
  135.     }
  136.     
  137.     /**
  138.     * Sets the site-wide authentication method in the Moodle configuration table.
  139.     * <b>Note:</b> no validation is performed on the parameter except to establish that it is a string
  140.     * @param string $auth A string containing the authentication method, "web" for web-based, or "autoregister" for automatic registration
  141.     * @return bool True if successful, or false if the query failed
  142.     * @see sloodle_is_automatic_registration_on()
  143.     * @see sloodle_get_auth_method()
  144.     * @see sloodle_set_config()
  145.     */
  146.     function sloodle_set_auth_method($auth)
  147.     {
  148.         // Make sure it's a string
  149.         if (!is_string($auth)) return FALSE;
  150.         return sloodle_set_config('sloodle_auth_method'$auth);
  151.     }
  152.     
  153.     /**
  154.     * Gets the position of the site-wide LoginZone object.
  155.     * @return mixed A string containing the position of the LoginZone object ("<x,y,z>"), or FALSE if no LoginZone data has yet been stored
  156.     * @see sloodle_set_loginzone_pos()
  157.     * @see sloodle_get_config()
  158.     */
  159.     function sloodle_get_loginzone_pos()
  160.     {
  161.         return sloodle_get_config('sloodle_loginzone_pos');
  162.     }
  163.     
  164.     /**
  165.     * Sets the position of the site-wide LoginZone object (stored in the Moodle configuration table).
  166.     * @param mixed $pos Either a string vector "<x,y,z>" or an associative vector array {x,y,z}
  167.     * @return bool True if successful, or false otherwise
  168.     * @see sloodle_get_loginzone_pos()
  169.     * @see sloodle_set_config()
  170.     */
  171.     function sloodle_set_loginzone_pos($pos)
  172.     {
  173.         // If it's an array, make it a string
  174.         if (is_array($pos)) $pos sloodle_array_to_vector($pos);
  175.         else if (!is_string($pos)) return FALSE;
  176.         return sloodle_set_config('sloodle_loginzone_pos'$pos);
  177.     }
  178.     
  179.     /**
  180.     * Gets the size of the site-wide LoginZone object.
  181.     * @return mixed A string containing the dimensions of the LoginZone object ("<x,y,z>"), or FALSE if no LoginZone data has yet been stored
  182.     * @see sloodle_set_loginzone_size()
  183.     * @see sloodle_get_config()
  184.     */
  185.     function sloodle_get_loginzone_size()
  186.     {
  187.         return sloodle_get_config('sloodle_loginzone_size');
  188.     }
  189.     
  190.     /**
  191.     * Sets the size of the site-wide LoginZone object (stored in the Moodle configuration table).
  192.     * @param mixed $size Either a string vector "<x,y,z>" or an associative vector array {x,y,z}
  193.     * @return bool True if successful, or false otherwise
  194.     * @see sloodle_get_loginzone_size()
  195.     * @see sloodle_set_config()
  196.     */
  197.     function sloodle_set_loginzone_size($size)
  198.     {
  199.         // If it's an array, make it a string
  200.         if (is_array($size)) $size sloodle_array_to_vector($size);
  201.         else if (!is_string($size)) return FALSE;
  202.         return sloodle_set_config('sloodle_loginzone_size'$size);
  203.     }
  204.     
  205.     /**
  206.     * Gets the name of the region where the site-wide LoginZone object was most recently rezzed.
  207.     * @return mixed A string containing the name of a region (e.g. "virtuALBA"), or FALSE if no LoginZone data has yet been stored
  208.     * @see sloodle_set_loginzone_region()
  209.     * @see sloodle_get_config()
  210.     */
  211.     function sloodle_get_loginzone_region()
  212.     {
  213.         return sloodle_get_config('sloodle_loginzone_region');
  214.     }
  215.     
  216.     /**
  217.     * Sets the name of the region where the site-wide LoginZone object was most recently rezzed.
  218.     * @param string $region A string containing the name of a region (e.g. "virtuALBA")
  219.     * @see sloodle_get_loginzone_region()
  220.     * @see sloodle_set_config()
  221.     */
  222.     function sloodle_set_loginzone_region($region)
  223.     {
  224.         // Make sure it's a string
  225.         if (!is_string($region)) return FALSE;
  226.         return sloodle_set_config('sloodle_loginzone_region'$region);
  227.     }
  228.     
  229.     /**
  230.     * Gets an array of the names of all objects in the Object Distributor.
  231.     * <b>NOTE:</b> Stored in the Moodle configuration table as value "sloodle_distrib_objects".
  232.     * Must be set manually as a pipe-delimited list, e.g. "object1|object2|object3".
  233.     * @return array A numeric array of strings (will be an empty array if no objects are available)
  234.     * @see sloodle_get_config()
  235.     */
  236.     function sloodle_get_distribution_list()
  237.     {
  238.         // Get the data from the configuration system
  239.         $str sloodle_get_config('sloodle_distrib_objects');
  240.         if (!is_string($str|| empty($str)) return array();
  241.         // Split it at pipe-characters |
  242.         return explode('|'$str);
  243.     }
  244.     
  245.     /**
  246.     * Sends an XMLRPC message into Second Life.
  247.     * @param string $channel A string containing a UUID identifying the XMLRPC channel in SL to be used
  248.     * @param int $intval An integer value to be sent in the message
  249.     * @param string $strval A string value to be sent in the message
  250.     * @return bool True if successful, or false if an error occurs
  251.     */
  252.     function sloodle_send_xmlrpc_message($channel,$intval,$strval)
  253.     {
  254.         // Include our XMLRPC library
  255.         require_once(SLOODLE_DIRROOT.'/lib/xmlrpc.inc');
  256.         // Instantiate a new client object for communicating with Second Life
  257.         $client new xmlrpc_client("http://xmlrpc.secondlife.com/cgi-bin/xmlrpc.cgi");
  258.         // Construct the content of the RPC
  259.         $content '<?xml version="1.0"?><methodCall><methodName>llRemoteData</methodName><params><param><value><struct><member><name>Channel</name><value><string>'.$channel.'</string></value></member><member><name>IntValue</name><value><int>'.$intval.'</int></value></member><member><name>StringValue</name><value><string>'.$strval.'</string></value></member></struct></value></param></params></methodCall>';
  260.         
  261.         // Attempt to send the data via http
  262.         $response $client->send(
  263.             $content,
  264.             60,
  265.             'http'
  266.         );
  267.         
  268.         //var_dump($response); // Debug output
  269.         // Make sure we got a response value
  270.         if (!isset($response->val|| empty($response->val|| is_null($response->val)) {
  271.             // Report an error if we are in debug mode
  272.             if (defined('SLOODLE_DEBUG'&& SLOODLE_DEBUG{
  273.                 print '<p align="left">Not getting the expected XMLRPC response. Is Second Life broken again?<br/>';
  274.                 if (isset($response->errstr)) print "XMLRPC Error - ".$response->errstr;
  275.                 print '</p>';
  276.             }
  277.             return FALSE;
  278.         }
  279.         
  280.         // Check the contents of the response value
  281.         //if (defined('SLOODLE_DEBUG') && SLOODLE_DEBUG) {
  282.         //    print_r($response->val);
  283.         //}
  284.         
  285.         //TODO: Check the details of the response to see if this was successful or not...
  286.         return TRUE;
  287.     
  288.     }
  289.  
  290.     /**
  291.     * Old logging function
  292.     * @todo <b>May require update?</b>
  293.     */
  294.     function sloodle_add_to_log($courseid null$module null$action null$url null$cmid null$info null)
  295.     {
  296.  
  297.        global $CFG;
  298.  
  299.        // TODO: Make sure we set this in the calling function, then remove this bit
  300.        if ($courseid == null{
  301.           $courseid optional_param('sloodle_courseid',0,PARAM_RAW);
  302.        }
  303.  
  304.        // if no action is specified, use the object name
  305.        if ($action == null{
  306.           $action $_SERVER['X-SecondLife-Object-Name'];
  307.        }
  308.  
  309.        $region $_SERVER['X-SecondLife-Region'];
  310.        if ($info == null{
  311.           $info $region;
  312.        }
  313.  
  314.        $slurl '';
  315.        if (preg_match('/^(.*)\(.*?\)$/',$region,$matches)) // strip the coordinates, eg. Cicero (123,123)
  316.           $region $matches[1];
  317.        }
  318.  
  319.        $xyz $_SERVER['X-SecondLife-Local-Position'];
  320.        if (preg_match('/^\((.*?),(.*?),(.*?)\)$/',$xyz,$matches)) {
  321.           $xyz $matches[1].'/'.$matches[2].'/'.$matches[3];
  322.        }
  323.  
  324.        return add_to_log($courseidnull$action$CFG->wwwroot.'/mod/sloodle/toslurl.php?region='.urlencode($region).'&xyz='.$xyz$userid$info );
  325.        //return add_to_log($courseid, null, "ok", "ok", $userid, "ok");
  326.  
  327.     }
  328.  
  329.     /**
  330.     * Determines whether or not Sloodle is installed.
  331.     * Queries Moodle's modules table for a Sloodle entry.
  332.     * <b>NOTE:</b> does not check for the presence of the Sloodle files.
  333.     * @return bool True if Sloodle is installed, or false otherwise.
  334.     */
  335.     function sloodle_is_installed()
  336.     {
  337.         // Is there a Sloodle entry in the modules table?
  338.         return record_exists('modules''name''sloodle');
  339.     }
  340.     
  341.     /**
  342.     * Generates a random login security token.
  343.     * Uses mixed-case letters and numbers to generate a random 16-character string.
  344.     * @return string 
  345.     * @see sloodle_random_web_password()
  346.     */
  347.     function sloodle_random_security_token()
  348.     {
  349.         // Define the characters we can use in our token, and get the length of it
  350.         $str "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
  351.         $strlen strlen($str1;
  352.         // Prepare the token variable
  353.         $token '';
  354.         // Loop once for each output character
  355.         for($length 0$length 16$length++{
  356.             // Shuffle the string, then pick and store a random character
  357.             $str str_shuffle($str);
  358.             $char mt_rand(0$strlen);
  359.             $token .= $str[$char];
  360.         }
  361.         
  362.         return $token;
  363.     }
  364.     
  365.     /**
  366.     * Generates a random web password
  367.     * Uses mixed-case letters and numbers to generate a random 8-character string.
  368.     * @return string 
  369.     * @see sloodle_random_security_token()
  370.     */
  371.     function sloodle_random_web_password()
  372.     {
  373.         // Define the characters we can use in our token, and get the length of it
  374.         $str "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
  375.         $strlen strlen($str1;
  376.         // Prepare the password string
  377.         $pwd '';
  378.         // Loop once for each output character
  379.         for($length 0$length 8$length++{
  380.             // Shuffle the string, then pick and store a random character
  381.             $str str_shuffle($str);
  382.             $char mt_rand(0$strlen);
  383.             $pwd .= $str[$char];
  384.         }
  385.         
  386.         return $pwd;
  387.     }
  388.     
  389.     /**
  390.     * Converts a string vector to an array vector.
  391.     * String vector should be of format "<x,y,z>".
  392.     * Converts to associative array with members 'x', 'y', and 'z'.
  393.     * Returns false if input parameter was not of correct format.
  394.     * @param string $vector A string vector of format "<x,y,z>".
  395.     * @return mixed 
  396.     * @see sloodle_array_to_vector()
  397.     * @see sloodle_round_vector()
  398.     */
  399.     function sloodle_vector_to_array($vector)
  400.     {
  401.         if (preg_match('/<(.*?),(.*?),(.*?)>/',$vector,$vectorbits)) {
  402.             $arr array();
  403.             $arr['x'$vectorbits[1];
  404.             $arr['y'$vectorbits[2];
  405.             $arr['z'$vectorbits[3];
  406.             return $arr;
  407.         }
  408.         return false;
  409.     }
  410.     
  411.     /**
  412.     * Converts an array vector to a string vector.
  413.     * Array vector should be associative, containing elements 'x', 'y', and 'z'.
  414.     * Converts to a string vector of format "<x,y,z>".
  415.     * @return string 
  416.     * @see sloodle_vector_to_array()
  417.     * @see sloodle_round_vector()
  418.     */
  419.     function sloodle_array_to_vector($arr)
  420.     {
  421.         $ret '<'.$arr['x'].','.$arr['y'].','.$arr['z'].'>';
  422.         return $ret;
  423.     }
  424.     
  425.     /**
  426.     * Obtains the identified course module instance database record.
  427.     * @param int $id The integer ID of a course module instance
  428.     * @return mixed  A database record if successful, or false if it could not be found
  429.     */
  430.     function sloodle_get_course_module_instance($id)
  431.     {
  432.         return get_record('course_modules''id'$id);
  433.     }
  434.     
  435.     /**
  436.     * Determines whether or not the specified course module instance is visible.
  437.     * Checks that the instance itself and the course section are both valid.
  438.     * @param int $id The integer ID of a course module instance.
  439.     * @return bool True if visible, or false if invisible or not found
  440.     */
  441.     {
  442.         // Get the course module instance record, whether directly from the parameter, or from the database
  443.         if (is_object($id)) {
  444.             $course_module_instance $id;
  445.         else if (is_int($id)) {
  446.             if (!($course_module_instance get_record('course_modules''id'$id))) return FALSE;
  447.         else return FALSE;
  448.         
  449.         // Make sure the instance itself is visible
  450.         if ((int)$course_module_instance->visible == 0return FALSE;
  451.         // Find out which section it is in, and if that section is valid
  452.         if (!($section get_record('course_sections''id'$course_module_instance->section))) return FALSE;
  453.         if ((int)$section->visible == 0return FALSE;
  454.         
  455.         // Looks like the module is visible
  456.         return TRUE;
  457.     }
  458.     
  459.     /**
  460.     * Determines if the specified course module instance is of the named type.
  461.     * For example, this can check if a particular instance is a "forum" or a "chat".
  462.     * @param int $id The integer ID of a course module instance
  463.     * @param string $module_name Module type to check (must be the exact name of an installed module, e.g. 'sloodle' or 'quiz')
  464.     * @return bool True if the module is of the specified type, or false otherwise
  465.     */
  466.     function sloodle_check_course_module_instance_type($id$module_name)
  467.     {
  468.         // Get the record for the module type
  469.         if (!($module_record get_record('modules''name'$module_name))) return FALSE;
  470.  
  471.         // Get the course module instance record, whether directly from the parameter, or from the database
  472.         if (is_object($id)) {
  473.             $course_module_instance $id;
  474.         else if (is_int($id)) {
  475.             if (!($course_module_instance get_record('course_modules''id'$id))) return FALSE;
  476.         else return FALSE;
  477.         
  478.         // Check the type of the instance
  479.         return ($course_module_instance->module == $module_record->id);
  480.     }
  481.     
  482.     /**
  483.     * Obtains the ID number of the specified module (type not instance).
  484.     * @param string $name The name of the module type to check, e.g. 'sloodle' or 'forum'
  485.     * @return mixed Integer containing module ID, or false if it is not installed
  486.     */
  487.     function sloodle_get_module_id($name)
  488.     {
  489.         // Ensure the name is a non-empty string
  490.         if (!is_string($name|| empty($name)) return FALSE;
  491.         // Obtain the module record
  492.         if (!($module_record get_record('modules''name'$module_name))) return FALSE;
  493.         
  494.         return $module_record->id;
  495.     }
  496.     
  497.     /**
  498.     * Checks if the specified position is in the current (site-wide) loginzone.
  499.     * @param mixed $pos A string vector or an associated array vector
  500.     * @return bool True if position is in LoginZone, or false if not
  501.     * @see sloodle_login_zone_coordinates()
  502.     */
  503.     function sloodle_position_is_in_login_zone($pos)
  504.     {
  505.         // Get a position array from the parameter
  506.         $posarr NULL;
  507.         if (is_array($pos&& count($pos== 3{
  508.             $posarr $pos;
  509.         else if (is_string($pos)) {
  510.             $posarr sloodle_vector_to_array($pos);
  511.         else {
  512.             return FALSE;
  513.         }
  514.         // Fetch the loginzone boundaries
  515.         list($maxarr,$minarrsloodle_login_zone_coordinates();
  516.  
  517.         // Make sure the position is not past the maximum bounds
  518.         if ( ($posarr['x'$maxarr['x']|| ($posarr['y'$maxarr['y']|| ($posarr['z'$maxarr['z']) ) {
  519.             return FALSE;
  520.         }
  521.         // Make sure the position is not past the minimum bounds
  522.         if ( ($posarr['x'$minarr['x']|| ($posarr['y'$minarr['y']|| ($posarr['z'$minarr['z']) ) {
  523.             return FALSE;
  524.         }
  525.  
  526.         return TRUE;
  527.     }
  528.     
  529.     /**
  530.     * Generates teleport coordinates for a user who has already finished the LoginZone process.
  531.     * @return array An associated array vector
  532.     */
  533.     {
  534.         // Get the size and position of the loginzone
  535.         $pos sloodle_get_loginzone_pos();
  536.         $size sloodle_get_loginzone_size();
  537.         // Make sure we retrieved both OK
  538.         if (!is_string($pos|| !is_string($size)) {
  539.             return FALSE;
  540.         }
  541.         // Convert both to arrays
  542.         $posarr sloodle_vector_to_array($pos);
  543.         $sizearr sloodle_vector_to_array($size);
  544.         // Calculate a position just below the loginzone
  545.         $coord array();
  546.         $coord['x'round($posarr['x'],0);
  547.         $coord['y'round($posarr['y'],0);
  548.         $coord['z'round(($posarr['z']-(($sizearr['z'])/2)-2),0);
  549.         return $coord;
  550.     }
  551.     
  552.     /**
  553.     * Generates a random position within the specified cubic zone.
  554.     * @param array $zonemax Associative array vector specifying the maximum boundary of the cubic zone
  555.     * @param array $zonemin Associative array vector specifying the minimum boundary of the cubic zone
  556.     * @return array An associative vector array
  557.     */
  558.     function sloodle_random_position_in_zone($zonemax,$zonemin)
  559.     {
  560.         $pos array();
  561.         $pos['x'rand($zonemin['x'],$zonemax['x']);    
  562.         $pos['y'rand($zonemin['y'],$zonemax['y']);    
  563.         $pos['z'rand($zonemin['z'],$zonemax['z']);
  564.         return $pos;
  565.     }
  566.  
  567.     // Round the specified 3d vector to integer values
  568.     // $pos should be a vector string "<x,y,z>" or an associative array {x,y,z}
  569.     // Return is the same as the type passed-in
  570.     // If the input type is unrecognised, it simply returns it back out unchanged
  571.     /**
  572.     * Rounds the specified 3d vector integer values.
  573.     * Can handle/return a string vector, or an array vector.
  574.     * (Output type matches input type).
  575.     * @param mixed $pos Either a string vector or an array vector
  576.     * @return mixed 
  577.     */
  578.     function sloodle_round_vector($pos)
  579.     {
  580.         // We will work with an array, but allow for conversion to/from string
  581.         $arrayvec $pos;
  582.         $returnstring FALSE;
  583.         // Is it a string?
  584.         if (is_string($pos)) {
  585.             $arrayvec sloodle_vector_to_array($pos);
  586.             $returnstring TRUE;
  587.         else if (!is_array($pos)) {
  588.             return $pos;
  589.         }
  590.     
  591.         // Construct an output array
  592.         $output array();
  593.         foreach ($arrayvec as $key => $val{
  594.             $output[$keyround($val0);
  595.         }
  596.         
  597.         // If we need to convert it back to a string, then do so
  598.         if ($returnstring{
  599.             return sloodle_array_to_vector($output);
  600.         }
  601.         
  602.         return $output;
  603.     }
  604.     
  605.     /**
  606.     * Calculates the maximum and minimum bounds of the site-wide LoginZone.
  607.     * Returns the bounds as a numeric array of two associate array vectors: ($max, $min).
  608.     * (Or returns false if no LoginZone position/size could be found in the Moodle configuration table).
  609.     * @return array 
  610.     */
  611.     function sloodle_login_zone_coordinates()
  612.     {
  613.         // Get the position and size of the loginzone
  614.         $pos sloodle_get_loginzone_pos();
  615.         $size sloodle_get_loginzone_size();
  616.         // Make sure both we retrieved successfully
  617.         if (($pos == FALSE|| ($size == FALSE)) {
  618.             return FALSE;
  619.         }
  620.         // Convert both to arrays
  621.         $posarr sloodle_vector_to_array($pos);
  622.         $sizearr sloodle_vector_to_array($size);
  623.         // Calculate the bounds
  624.         $max array();
  625.         $max['x'$posarr['x']+(($sizearr['x'])/2)-2;
  626.         $max['y'$posarr['y']+(($sizearr['y'])/2)-2;
  627.         $max['z'$posarr['z']+(($sizearr['z'])/2)-2;
  628.         $min array();
  629.         $min['x'$posarr['x']-(($sizearr['x'])/2)+2;
  630.         $min['y'$posarr['y']-(($sizearr['y'])/2)+2;
  631.         $min['z'$posarr['z']-(($sizearr['z'])/2)+2;
  632.         
  633.         return array($max,$min);
  634.     }
  635.  
  636. ?>

Documentation generated on Tue, 04 Mar 2008 15:08:41 +0000 by phpDocumentor 1.4.0