Source for file general.php

Documentation is available at general.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.     /** Include our email functionality. */
  21.     require_once(SLOODLE_LIBROOT.'/mail.php');
  22.     
  23.     
  24.     /**
  25.     * Sets a Sloodle configuration value.
  26.     * This data will be stored in Moodle's "config" table, so it will persist even after Sloodle is uninstalled.
  27.     * After being set, it will be available (read-only) as a named member of Moodle's $CFG variable.
  28.     * <b>NOTE:</b> in Sloodle debug mode, this function will terminate the script with an error if the name is not prefixed with "sloodle_".
  29.     * @param string $name The name of the value to be stored (should be prefixed with "sloodle_")
  30.     * @param string $value The string representation of the value to be stored
  31.     * @return bool True on success, or false on failure (may fail if database query encountered an error)
  32.     * @see sloodle_get_config()
  33.     */
  34.     function sloodle_set_config($name$value)
  35.     {
  36.         // If in debug mode, ensure the name is prefixed appropriately for Sloodle
  37.         if (defined('SLOODLE_DEBUG'&& SLOODLE_DEBUG{
  38.             if (substr_count($name'sloodle_'1{
  39.                 exit ("ERROR: sloodle_set_config(..) called with invalid value name \"$name\". Expected \"sloodle_\" prefix.");
  40.             }
  41.         }
  42.         // Use the standard Moodle config function, ignoring the 3rd parameter ("plugin", which defaults to NULL)
  43.         return set_config(strtolower($name)$value);
  44.     }
  45.  
  46.     /**
  47.     * Gets a Sloodle configuration value from Moodle's "config" table.
  48.     * This function does not necessarily need to be used.
  49.     * All configuration data is available as named members of Moodle's $CFG global variable.
  50.     * <b>NOTE:</b> in Sloodle debug mode, this function will terminate the script with an error if the name is not prefixed with "sloodle_".
  51.     * @param string $name The name of the value to be stored (should be prefixed with "sloodle_")
  52.     * @return mixed A string containing the configuration value, or false if the query failed (e.g. if the named value didn't exist)
  53.     * @see sloodle_set_config()
  54.     */
  55.     function sloodle_get_config($name)
  56.     {
  57.         // If in debug mode, ensure the name is prefixed appropriately for Sloodle
  58.         if (defined('SLOODLE_DEBUG'&& SLOODLE_DEBUG{
  59.             if (substr_count($name'sloodle_'1{
  60.                 exit ("ERROR: sloodle_get_config(..) called with invalid value name \"$name\". Expected \"sloodle_\" prefix.");
  61.             }
  62.         }
  63.         // Use the Moodle config function, ignoring the plugin parameter
  64.         $val get_config(NULLstrtolower($name));
  65.         // Older Moodle versions return a database record object instead of the value itself
  66.         // Workaround:
  67.         if (is_object($val)) return $val->value;
  68.         return $val;
  69.     }
  70.     
  71.     /**
  72.     * Determines whether or not auto-registration is allowed for the site.
  73.     * @return bool True if auto-reg is allowed on the site, or false otherwise.
  74.     */
  75.     function sloodle_autoreg_enabled_site()
  76.     {
  77.         return (bool)sloodle_get_config('sloodle_allow_autoreg');
  78.     }
  79.     
  80.     /**
  81.     * Determines whether or not auto-enrolment is allowed for the site.
  82.     * @return bool True if auto-enrolment is allowed on the site, or false otherwise.
  83.     */
  84.     function sloodle_autoenrol_enabled_site()
  85.     {
  86.         return (bool)sloodle_get_config('sloodle_allow_autoenrol');
  87.     }
  88.  
  89.     /**
  90.     * Sends an XMLRPC message into Second Life.
  91.     * @param string $channel A string containing a UUID identifying the XMLRPC channel in SL to be used
  92.     * @param int $intval An integer value to be sent in the message
  93.     * @param string $strval A string value to be sent in the message
  94.     * @return bool True if successful, or false if an error occurs
  95.     */
  96.     function sloodle_send_xmlrpc_message($channel,$intval,$strval)
  97.     {
  98.         // Include our XMLRPC library
  99.         require_once(SLOODLE_DIRROOT.'/lib/xmlrpc.inc');
  100.         // Instantiate a new client object for communicating with Second Life
  101.         $client new xmlrpc_client("http://xmlrpc.secondlife.com/cgi-bin/xmlrpc.cgi");
  102.         // Construct the content of the RPC
  103.         $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>';
  104.         
  105.         // Attempt to send the data via http
  106.         $response $client->send(
  107.             $content,
  108.             60,
  109.             'http'
  110.         );
  111.         
  112.         //var_dump($response); // Debug output
  113.         // Make sure we got a response value
  114.         if (!isset($response->val|| empty($response->val|| is_null($response->val)) {
  115.             // Report an error if we are in debug mode
  116.             if (defined('SLOODLE_DEBUG'&& SLOODLE_DEBUG{
  117.                 print '<p align="left">Not getting the expected XMLRPC response. Is Second Life broken again?<br/>';
  118.                 if (isset($response->errstr)) print "XMLRPC Error - ".$response->errstr;
  119.                 print '</p>';
  120.             }
  121.             return FALSE;
  122.         }
  123.         
  124.         // Check the contents of the response value
  125.         //if (defined('SLOODLE_DEBUG') && SLOODLE_DEBUG) {
  126.         //    print_r($response->val);
  127.         //}
  128.         
  129.         //TODO: Check the details of the response to see if this was successful or not...
  130.         return TRUE;
  131.     
  132.     }
  133.  
  134.     /**
  135.     * Old logging function
  136.     * @todo <b>May require update?</b>
  137.     */
  138.     function sloodle_add_to_log($courseid null$module null$action null$url null$cmid null$info null)
  139.     {
  140.  
  141.        global $CFG;
  142.  
  143.        // TODO: Make sure we set this in the calling function, then remove this bit
  144.        if ($courseid == null{
  145.           $courseid optional_param('sloodle_courseid',0,PARAM_RAW);
  146.        }
  147.  
  148.        // if no action is specified, use the object name
  149.        if ($action == null{
  150.           $action $_SERVER['X-SecondLife-Object-Name'];
  151.        }
  152.  
  153.        $region $_SERVER['X-SecondLife-Region'];
  154.        if ($info == null{
  155.           $info $region;
  156.        }
  157.  
  158.        $slurl '';
  159.        if (preg_match('/^(.*)\(.*?\)$/',$region,$matches)) // strip the coordinates, eg. Cicero (123,123)
  160.           $region $matches[1];
  161.        }
  162.  
  163.        $xyz $_SERVER['X-SecondLife-Local-Position'];
  164.        if (preg_match('/^\((.*?),(.*?),(.*?)\)$/',$xyz,$matches)) {
  165.           $xyz $matches[1].'/'.$matches[2].'/'.$matches[3];
  166.        }
  167.  
  168.        return add_to_log($courseidnull$action$CFG->wwwroot.'/mod/sloodle/toslurl.php?region='.urlencode($region).'&xyz='.$xyz$userid$info );
  169.        //return add_to_log($courseid, null, "ok", "ok", $userid, "ok");
  170.  
  171.     }
  172.  
  173.     /**
  174.     * Determines whether or not Sloodle is installed.
  175.     * Queries Moodle's modules table for a Sloodle entry.
  176.     * <b>NOTE:</b> does not check for the presence of the Sloodle files.
  177.     * @return bool True if Sloodle is installed, or false otherwise.
  178.     */
  179.     function sloodle_is_installed()
  180.     {
  181.         // Is there a Sloodle entry in the modules table?
  182.         return record_exists('modules''name''sloodle');
  183.     }
  184.     
  185.     /**
  186.     * Generates a random login security token.
  187.     * Uses mixed-case letters and numbers to generate a random 16-character string.
  188.     * @return string 
  189.     * @see sloodle_random_web_password()
  190.     */
  191.     function sloodle_random_security_token()
  192.     {
  193.         // Define the characters we can use in our token, and get the length of it
  194.         $str "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
  195.         $strlen strlen($str1;
  196.         // Prepare the token variable
  197.         $token '';
  198.         // Loop once for each output character
  199.         for($length 0$length 16$length++{
  200.             // Shuffle the string, then pick and store a random character
  201.             $str str_shuffle($str);
  202.             $char mt_rand(0$strlen);
  203.             $token .= $str[$char];
  204.         }
  205.         
  206.         return $token;
  207.     }
  208.     
  209.     /**
  210.     * Generates a random web password
  211.     * Uses mixed-case letters and numbers to generate a random 8-character string.
  212.     * @return string 
  213.     * @see sloodle_random_security_token()
  214.     */
  215.     function sloodle_random_web_password()
  216.     {
  217.         // Define the characters we can use in our token, and get the length of it
  218.         $str "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
  219.         $strlen strlen($str1;
  220.         // Prepare the password string
  221.         $pwd '';
  222.         // Loop once for each output character
  223.         for($length 0$length 8$length++{
  224.             // Shuffle the string, then pick and store a random character
  225.             $str str_shuffle($str);
  226.             $char mt_rand(0$strlen);
  227.             $pwd .= $str[$char];
  228.         }
  229.         
  230.         return $pwd;
  231.     }
  232.     
  233.     /**
  234.     * Generates a random prim password (7 to 9 digit number).
  235.     * @return string The password as a string
  236.     */
  237.     function sloodle_random_prim_password()
  238.     {
  239.         return (string)mt_rand(1000000999999999);
  240.     }
  241.     
  242.     /**
  243.     * Converts a string vector to an array vector.
  244.     * String vector should be of format "<x,y,z>".
  245.     * Converts to associative array with members 'x', 'y', and 'z'.
  246.     * Returns false if input parameter was not of correct format.
  247.     * @param string $vector A string vector of format "<x,y,z>".
  248.     * @return mixed 
  249.     * @see sloodle_array_to_vector()
  250.     * @see sloodle_round_vector()
  251.     */
  252.     function sloodle_vector_to_array($vector)
  253.     {
  254.         if (preg_match('/<(.*?),(.*?),(.*?)>/',$vector,$vectorbits)) {
  255.             $arr array();
  256.             $arr['x'$vectorbits[1];
  257.             $arr['y'$vectorbits[2];
  258.             $arr['z'$vectorbits[3];
  259.             return $arr;
  260.         }
  261.         return false;
  262.     }
  263.     
  264.     /**
  265.     * Converts an array vector to a string vector.
  266.     * Array vector should be associative, containing elements 'x', 'y', and 'z'.
  267.     * Converts to a string vector of format "<x,y,z>".
  268.     * @return string 
  269.     * @see sloodle_vector_to_array()
  270.     * @see sloodle_round_vector()
  271.     */
  272.     function sloodle_array_to_vector($arr)
  273.     {
  274.         $ret '<'.$arr['x'].','.$arr['y'].','.$arr['z'].'>';
  275.         return $ret;
  276.     }
  277.     
  278.     /**
  279.     * Obtains the identified course module instance database record.
  280.     * @param int $id The integer ID of a course module instance
  281.     * @return mixed  A database record if successful, or false if it could not be found
  282.     */
  283.     function sloodle_get_course_module_instance($id)
  284.     {
  285.         return get_record('course_modules''id'$id);
  286.     }
  287.     
  288.     /**
  289.     * Determines whether or not the specified course module instance is visible.
  290.     * Checks that the instance itself and the course section are both valid.
  291.     * @param int $id The integer ID of a course module instance.
  292.     * @return bool True if visible, or false if invisible or not found
  293.     */
  294.     {
  295.         // Get the course module instance record, whether directly from the parameter, or from the database
  296.         if (is_object($id)) {
  297.             $course_module_instance $id;
  298.         else if (is_int($id)) {
  299.             if (!($course_module_instance get_record('course_modules''id'$id))) return FALSE;
  300.         else return FALSE;
  301.         
  302.         // Make sure the instance itself is visible
  303.         if ((int)$course_module_instance->visible == 0return FALSE;
  304.         // Find out which section it is in, and if that section is valid
  305.         if (!($section get_record('course_sections''id'$course_module_instance->section))) return FALSE;
  306.         if ((int)$section->visible == 0return FALSE;
  307.         
  308.         // Looks like the module is visible
  309.         return TRUE;
  310.     }
  311.     
  312.     /**
  313.     * Determines if the specified course module instance is of the named type.
  314.     * For example, this can check if a particular instance is a "forum" or a "chat".
  315.     * @param int $id The integer ID of a course module instance
  316.     * @param string $module_name Module type to check (must be the exact name of an installed module, e.g. 'sloodle' or 'quiz')
  317.     * @return bool True if the module is of the specified type, or false otherwise
  318.     */
  319.     function sloodle_check_course_module_instance_type($id$module_name)
  320.     {
  321.         // Get the record for the module type
  322.         if (!($module_record get_record('modules''name'$module_name))) return FALSE;
  323.  
  324.         // Get the course module instance record, whether directly from the parameter, or from the database
  325.         if (is_object($id)) {
  326.             $course_module_instance $id;
  327.         else if (is_int($id)) {
  328.             if (!($course_module_instance get_record('course_modules''id'$id))) return FALSE;
  329.         else return FALSE;
  330.         
  331.         // Check the type of the instance
  332.         return ($course_module_instance->module == $module_record->id);
  333.     }
  334.     
  335.     /**
  336.     * Obtains the ID number of the specified module (type not instance).
  337.     * @param string $name The name of the module type to check, e.g. 'sloodle' or 'forum'
  338.     * @return mixed Integer containing module ID, or false if it is not installed
  339.     */
  340.     function sloodle_get_module_id($name)
  341.     {
  342.         // Ensure the name is a non-empty string
  343.         if (!is_string($name|| empty($name)) return FALSE;
  344.         // Obtain the module record
  345.         if (!($module_record get_record('modules''name'$module_name))) return FALSE;
  346.         
  347.         return $module_record->id;
  348.     }
  349.     
  350.     /**
  351.     * Checks if the specified position is in the current (site-wide) loginzone.
  352.     * @param mixed $pos A string vector or an associated array vector
  353.     * @return bool True if position is in LoginZone, or false if not
  354.     * @see sloodle_login_zone_coordinates()
  355.     * @todo Update or remove... no longer valid
  356.     */
  357.     function sloodle_position_is_in_login_zone($pos)
  358.     {
  359.         // Get a position array from the parameter
  360.         $posarr NULL;
  361.         if (is_array($pos&& count($pos== 3{
  362.             $posarr $pos;
  363.         else if (is_string($pos)) {
  364.             $posarr sloodle_vector_to_array($pos);
  365.         else {
  366.             return FALSE;
  367.         }
  368.         // Fetch the loginzone boundaries
  369.         list($maxarr,$minarrsloodle_login_zone_coordinates();
  370.  
  371.         // Make sure the position is not past the maximum bounds
  372.         if ( ($posarr['x'$maxarr['x']|| ($posarr['y'$maxarr['y']|| ($posarr['z'$maxarr['z']) ) {
  373.             return FALSE;
  374.         }
  375.         // Make sure the position is not past the minimum bounds
  376.         if ( ($posarr['x'$minarr['x']|| ($posarr['y'$minarr['y']|| ($posarr['z'$minarr['z']) ) {
  377.             return FALSE;
  378.         }
  379.  
  380.         return TRUE;
  381.     }
  382.     
  383.     /**
  384.     * Generates teleport coordinates for a user who has already finished the LoginZone process.
  385.     * @param string $pos A string vector giving the position of the LoginZone
  386.     * @param string $size A string vector giving the size of the LoginZone
  387.     * @return array, bool An associative array vector containing a teleport location, or false if the operation fails.
  388.     */
  389.     function sloodle_finished_login_coordinates($pos$size)
  390.     {
  391.         // Make sure the parameters are valid types
  392.         if (!is_string($pos|| !is_string($size)) {
  393.             return FALSE;
  394.         }
  395.         // Convert both to arrays
  396.         $posarr sloodle_vector_to_array($pos);
  397.         $sizearr sloodle_vector_to_array($size);
  398.         // Calculate a position just below the loginzone
  399.         $coord array();
  400.         $coord['x'round($posarr['x'],0);
  401.         $coord['y'round($posarr['y'],0);
  402.         $coord['z'round(($posarr['z']-(($sizearr['z'])/2)-2),0);
  403.         return $coord;
  404.     }
  405.     
  406.     /**
  407.     * Generates a random position within a cuboid zone of the specified size.
  408.     * (Note: leaves a 2 metre margin round the outside)
  409.     * @param array $size Associative array giving the size of the zone
  410.     * @return array An associative vector array
  411.     */
  412.     function sloodle_random_position_in_zone($size)
  413.     {
  414.         // Construct the half-size array
  415.         $halfsize array('x'=>($size['x'2.02.0'y'=>($size['y'2.02.0'z'=>($size['z'2.02.0);
  416.     
  417.         $pos array();
  418.         $pos['x'mt_rand(0.0$size['x'4.0$halfsize['x'];
  419.         $pos['y'mt_rand(0.0$size['y'4.0$halfsize['y'];
  420.         $pos['z'mt_rand(0.0$size['z'4.0$halfsize['z'];
  421.         return $pos;
  422.     }
  423.  
  424.     // Round the specified 3d vector to integer values
  425.     // $pos should be a vector string "<x,y,z>" or an associative array {x,y,z}
  426.     // Return is the same as the type passed-in
  427.     // If the input type is unrecognised, it simply returns it back out unchanged
  428.     /**
  429.     * Rounds the specified 3d vector integer values.
  430.     * Can handle/return a string vector, or an array vector.
  431.     * (Output type matches input type).
  432.     * @param mixed $pos Either a string vector or an array vector
  433.     * @return mixed 
  434.     */
  435.     function sloodle_round_vector($pos)
  436.     {
  437.         // We will work with an array, but allow for conversion to/from string
  438.         $arrayvec $pos;
  439.         $returnstring FALSE;
  440.         // Is it a string?
  441.         if (is_string($pos)) {
  442.             $arrayvec sloodle_vector_to_array($pos);
  443.             $returnstring TRUE;
  444.         else if (!is_array($pos)) {
  445.             return $pos;
  446.         }
  447.     
  448.         // Construct an output array
  449.         $output array();
  450.         foreach ($arrayvec as $key => $val{
  451.             $output[$keyround($val0);
  452.         }
  453.         
  454.         // If we need to convert it back to a string, then do so
  455.         if ($returnstring{
  456.             return sloodle_array_to_vector($output);
  457.         }
  458.         
  459.         return $output;
  460.     }
  461.     
  462.     /**
  463.     * Calculates the maximum and minimum bounds of the specified LoginZone
  464.     * Returns the bounds as a numeric array of two associate array vectors: ($max, $min).
  465.     * (Or returns false if no LoginZone position/size could be found in the Moodle configuration table).
  466.     * @param string $pos A string vector giving the position of the LoginZone
  467.     * @param string $size A string vector giving the size of the LoginZone
  468.     * @return array 
  469.     */
  470.     function sloodle_login_zone_bounds($pos$size)
  471.     {
  472.         // Make sure the parameters are valid types
  473.         if (($pos == FALSE|| ($size == FALSE)) {
  474.             return FALSE;
  475.         }
  476.         // Convert both to arrays
  477.         $posarr sloodle_vector_to_array($pos);
  478.         $sizearr sloodle_vector_to_array($size);
  479.         // Calculate the bounds
  480.         $max array();
  481.         $max['x'$posarr['x']+(($sizearr['x'])/2)-2;
  482.         $max['y'$posarr['y']+(($sizearr['y'])/2)-2;
  483.         $max['z'$posarr['z']+(($sizearr['z'])/2)-2;
  484.         $min array();
  485.         $min['x'$posarr['x']-(($sizearr['x'])/2)+2;
  486.         $min['y'$posarr['y']-(($sizearr['y'])/2)+2;
  487.         $min['z'$posarr['z']-(($sizearr['z'])/2)+2;
  488.         
  489.         return array($max,$min);
  490.     }
  491.     
  492.     
  493.     /**
  494.     * Checks if the given prim password is valid.
  495.     * @param string $password The password string to check
  496.     * @return bool True if it is valid, or false otherwise.
  497.     */
  498.     function sloodle_validate_prim_password($password)
  499.     {
  500.         // Check that it's a string
  501.         if (!is_string($password)) return false;
  502.         // Check the length
  503.         $len strlen($password);
  504.         if ($len || $len 9return false;
  505.         // Check that it's all numbers
  506.         if (!ctype_digit($password)) return false;
  507.         // Check that it doesn't start with a 0
  508.         if ($password[0== '0'return false;
  509.         
  510.         // It all seems fine
  511.         return true;
  512.     }
  513.     
  514.     /**
  515.     * Checks if the given prim password is valid, and provides feedback.
  516.     * An array is written to by reference, each element containing error codes.
  517.     * Each error code is a word. The full text of the error message may be obtained
  518.     *  from the string file by looking for "primpass:errorcode".
  519.     *
  520.     * @param string $password The password to validate
  521.     * @param array &$errors An array (passed by reference) which will contain any error messages
  522.     * @return bool True if the prim password is valid, or false otherwise
  523.     */
  524.     function sloodle_validate_prim_password_verbose($password&$errors)
  525.     {
  526.         // Initialise variables
  527.         $errors array();
  528.         $result true;
  529.         
  530.         // Check that it's a string
  531.         if (!is_string($password)) {
  532.             $errors['invalidtype';
  533.             $result false;
  534.         }
  535.         // Check the length
  536.         $len strlen($password);
  537.         if ($len 5{
  538.             $errors['tooshort';
  539.             $result false;
  540.         }
  541.         if ($len 9{
  542.             $errors['toolong';
  543.             $result false;
  544.         }
  545.         
  546.         // Check that it's all numbers
  547.         if (!ctype_digit($password)) {
  548.             $errors['numonly';
  549.             $result false;
  550.         }
  551.         
  552.         // Check that it doesn't start with a 0
  553.         if ($password[0== '0'{
  554.             $errors['leadingzero';
  555.             $result false;
  556.         }
  557.         
  558.         return $result;
  559.     }
  560.     
  561.     
  562.     /**
  563.     * Stores a pending login notification for an auto-registered user.
  564.     * A cron job will process the pending notification queue.
  565.     * @param string $destination Identifies the destination of the notification (for SL, this will be the object UUID. The send function will construct the email address)
  566.     * @param string $avatar Identifier for the avatar being notified
  567.     * @param string $username The username to notify the user of
  568.     * @param string $password The (plaintext) password to notify the user of
  569.     * @return bool True if successful, or false otherwise
  570.     */
  571.     function sloodle_login_notification($destination$avatar$username$password)
  572.     {
  573.         // If another pending notification already exists for the same username, then delete it
  574.         delete_records('sloodle_login_notifications''username'$username);
  575.         
  576.         // Add the new details
  577.         $notification new stdClass();
  578.         $notification->destination $destination;
  579.         $notification->avatar $avatar;
  580.         $notification->username $username;
  581.         $notification->password $password;
  582.  
  583.         if (!insert_record('sloodle_login_notifications'$notification)) {
  584.         echo "failed\n";
  585.     }
  586.     echo "succeeded\n";
  587.     }
  588.     
  589.     /**
  590.     * Send a login notification.
  591.     * @param string $destination Identifies the destination of the notification (for SL, this will be the object UUID. The target email address will be constructed)
  592.     * @param string $avatar Identifier for the avatar being notified
  593.     * @param string $username The username to notify the user of
  594.     * @param string $password The (plaintext) password to notify the user of
  595.     * @return bool True if successful, or false otherwise
  596.     */
  597.     function sloodle_send_login_notification($destination$avatar$username$password)
  598.     {
  599.         global $CFG;
  600.         return sloodle_text_email_sl($destination'SLOODLE_LOGIN'"$avatar|{$CFG->wwwroot}|$username|$password");
  601.     }
  602.     
  603.     /**
  604.     * Processes pending login notifications, up to a certain limit.
  605.     * Retrieves the requests one-at-a-time for processing.
  606.     * This is slower, but ensures minimal damage if the process is terminated, e.g. due to server timeout.
  607.     * @param int $limit The maximum number of pending requests to process.
  608.     * @return void 
  609.     */
  610.     function sloodle_process_login_notifications($limit 25)
  611.     {
  612.         global $CFG;
  613.         
  614.         // Validate the limit
  615.         $limit = (int)$limit;
  616.         if ($limit 1return;
  617.         
  618.         // Go through each one
  619.         for ($i 0$i $limit$i++{
  620.             // Obtain the first record
  621.             $recs get_records('sloodle_login_notifications''''''id''*'0$limit);
  622.             if (!$recsreturn false;
  623.             reset($recs);
  624.             $rec current($recs);
  625.             
  626.             // Determine the user ID of the person who requested this
  627.             $userid 0;
  628.             if (!($sloodleuser get_record('sloodle_users''uuid'$rec->avatar))) {
  629.                 // Failed to the user - get the guest user instead
  630.                 $guestdata guest_user();
  631.                 $userid $guestdata->id;
  632.             else {
  633.                 // Got the data - store the user ID
  634.                 $userid $sloodleuser->userid;
  635.             }
  636.             
  637.             // Send the notification
  638.             if (sloodle_send_login_notification($rec->destination$rec->avatar$rec->username$rec->password)) {
  639.                 // Log the notification
  640.                  add_to_log(SITEID'sloodle''view''''Sent login details by email to avatar in-world'0$userid);
  641.             else {
  642.                 // Log the failed notification (but don't keep trying the same one)
  643.                 add_to_log(SITEID'sloodle''view failed''''Failed to send login details by email to avatar in-world'0$userid);
  644.             }
  645.             
  646.             // Delete the record from the data
  647.             delete_records('sloodle_login_notifications''id'$rec->id);
  648.         }
  649.     }
  650.     
  651.     
  652.     /**
  653.     * Extracts a value from a name-value associative array if it is set.
  654.     * (The array should associate name to value).
  655.     * @param array $settings The array of names and values
  656.     * @param string $name The name of the value to retrieve
  657.     * @param mixed $default The default value to return if the specified value was not found
  658.     * @return mixed The value from the input array, or the $default parameter
  659.     */
  660.     function sloodle_get_value($settings$name$default null)
  661.     {
  662.         if (is_array($settings&& isset($settings[$name])) return $settings[$name];
  663.         return $default;
  664.     }
  665.     
  666.     
  667.     /**
  668.     * Outputs the standard form elements for access levels in object configuration.
  669.     * Each part can be optionally hidden, and default values can be provided.
  670.     * (Note: the server access level must be communicated from the object back to Moodle... rubbish implementation, but it works!)
  671.     * @param array $current_config An associative array of setting names to values, containing defaults. (Ignored if null).
  672.     * @param bool $show_use_object Determines whether or not the "Use Object" setting is shown
  673.     * @param bool $show_control_object Determines whether or not the "Control Object" setting is shown
  674.     * @param bool $show_server Determines whether or not the server access setting is shown
  675.     * @return void 
  676.     */
  677.     function sloodle_print_access_level_options($current_config$show_use_object true$show_control_object true$show_server true)
  678.     {
  679.         // Quick-escape: if everything is being suppressed, then do nothing
  680.         if (!($show_use_object || $show_control_object || $show_server)) return;
  681.         
  682.         // Fetch default values from the configuration, if possible
  683.         $sloodleobjectaccessleveluse sloodle_get_value($current_config'sloodleobjectaccessleveluse'SLOODLE_OBJECT_ACCESS_LEVEL_PUBLIC);
  684.         $sloodleobjectaccesslevelctrl sloodle_get_value($current_config'sloodleobjectaccesslevelctrl'SLOODLE_OBJECT_ACCESS_LEVEL_OWNER);
  685.         $sloodleserveraccesslevel sloodle_get_value($current_config'sloodleserveraccesslevel'SLOODLE_SERVER_ACCESS_LEVEL_PUBLIC);
  686.         
  687.         // Define our object access level array
  688.         $object_access_levels array(  SLOODLE_OBJECT_ACCESS_LEVEL_PUBLIC => get_string('accesslevel:public','sloodle'),
  689.                                         SLOODLE_OBJECT_ACCESS_LEVEL_GROUP => get_string('accesslevel:group','sloodle'),
  690.                                         SLOODLE_OBJECT_ACCESS_LEVEL_OWNER => get_string('accesslevel:owner','sloodle') );
  691.         // Define our server access level array
  692.         $server_access_levels array(  SLOODLE_SERVER_ACCESS_LEVEL_PUBLIC => get_string('accesslevel:public','sloodle'),
  693.                                         SLOODLE_SERVER_ACCESS_LEVEL_COURSE => get_string('accesslevel:course','sloodle'),
  694.                                         SLOODLE_SERVER_ACCESS_LEVEL_SITE => get_string('accesslevel:site','sloodle'),
  695.                                         SLOODLE_SERVER_ACCESS_LEVEL_STAFF => get_string('accesslevel:staff','sloodle') );
  696.     
  697.         // Display box and a heading
  698.         print_box_start('generalbox boxaligncenter');
  699.         echo '<h3>'.get_string('accesslevel','sloodle').'</h3>';
  700.     
  701.         // Print the object settings
  702.         if ($show_use_object || $show_control_object{
  703.             
  704.             // Object access
  705.             echo '<b>'.get_string('accesslevelobject','sloodle').'</b><br><i>'.get_string('accesslevelobject:desc','sloodle').'</i><br><br>';
  706.             // Use object
  707.             if ($show_use_object{
  708.                 echo get_string('accesslevelobject:use','sloodle').': ';
  709.                 choose_from_menu($object_access_levels'sloodleobjectaccessleveluse'$sloodleobjectaccessleveluse'');
  710.                 echo '<br><br>';
  711.             }
  712.             // Control object
  713.             if ($show_control_object{
  714.                 echo get_string('accesslevelobject:control','sloodle').': ';
  715.                 choose_from_menu($object_access_levels'sloodleobjectaccesslevelctrl'$sloodleobjectaccesslevelctrl'');
  716.                 echo '<br><br>';
  717.             }
  718.         }
  719.         
  720.         // Print the server settings
  721.         if ($show_server{
  722.             // Server access
  723.             echo '<b>'.get_string('accesslevelserver','sloodle').'</b><br><i>'.get_string('accesslevelserver:desc','sloodle').'</i><br><br>';
  724.             echo get_string('accesslevel','sloodle').': ';
  725.             choose_from_menu($server_access_levels'sloodleserveraccesslevel'$sloodleserveraccesslevel'');
  726.             echo '<br>';
  727.         }        
  728.         
  729.         print_box_end();
  730.     }
  731.     
  732.     
  733.  
  734. ?>

Documentation generated on Mon, 16 Jun 2008 15:56:21 +0100 by phpDocumentor 1.4.0