Source for file sl_lsllib.php
Documentation is available at sl_lsllib.php
* Sloodle LSL handling library.
* Provides the central API functionality, automatically combining several other API elements.
* @copyright Copyright (c) 2007-8 Sloodle (various contributors)
* @license http://www.gnu.org/licenses/gpl-3.0.html GNU GPL v3
* @contributor Peter R. Bloomfield
// This library expects that the Sloodle config file has already been included
// (along with the Moodle libraries)
/** Include the general Sloodle functionality. */
require_once(SLOODLE_DIRROOT.
'/lib/sl_generallib.php');
/** Include the Sloodle IO library. */
require_once(SLOODLE_DIRROOT.
'/lib/sl_iolib.php');
/** Include the Sloodle user management library. */
require_once(SLOODLE_DIRROOT.
'/lib/sl_userlib.php');
* A helper class to automate many API features.
* Most interaction with the object-oriented parts of the Sloodle API should occur via this class.
* It brings together the other classes, and automatically handles various features.
* Instantiated to the {@link: SloodleUser} type in the class constructor.
* No accessors - should be accessed directly.
* A Sloodle request handling object.
* Instantiated to the {@link: SloodleLSLRequest} type in the class constructor.
* No accessors - should be accessed directly.
* A Sloodle response handling object.
* Instantiated to the {@link: SloodleLSLResponse} type in the class constructor.
* No accessors - should be accessed directly.
* Instantiates member objects.
// Instantiate and link our components
* Performs an internal 'login' based on user identified in request.
* Checks for avatar UUID and/or name request parameters, and checks for an associated Moodle account.
* If found, the user is logged-in (details stored in Moodle's global $USER variable).
* @param bool $require If true (default), the function will terminate the script with an error message if login fails. (Error message will be LSL-friendly).
* @param bool $suppress_autoreg If true (not default), the function will always suppress auto-registration of new users. Note: this parameter will be ignored if auto-registration is disabled.
* @return bool True if login was successful. False if login failed, but parameter $require was false.
// Make sure the request is processed and authenticated
// Make sure the items of data were specified
$this->response->set_status_descriptor('USER_AUTH');
$this->response->add_data_line('User not identified in request.');
// Is the user already fully registered?
// Yes - attempt to login
$this->response->set_status_descriptor('USER_AUTH');
$this->response->add_data_line('Failed to login Moodle user for unkown reason.');
// Is auto-registration disabled or suppressed?
// We cannot do auto-registration
$this->response->set_status_descriptor('USER_AUTH');
$this->response->add_data_line('User not registered, and auto registration was not permitted.');
if ($suppress_autoreg) $this->response->add_data_line('Note: auto registration was explicitly suppressed.');
// Make sure we have both the avatar name and UUID
// [Note: if the user was fully registered with Sloodle and not Moodle, and only name OR UUID was provided,
// then the earlier data processing will have retrieved the missing value... cunning, eh? :-)]
$this->response->set_status_descriptor('USER_AUTO_REG');
$this->response->add_data_line('User auto registration not possible - avatar UUID was not provided or found in the database.');
$this->response->set_status_descriptor('USER_AUTO_REG');
$this->response->add_data_line('User auto registration not possible - avatar name was not provided or found in the database.');
// Do we need to register a Moodle account?
// Extract the first and last name parts
// Expecting that all SL names are first last, with a space in between
if (preg_match('/^(.*)\s(.*?)$/', $avname, $avbits)) {
// Something wasn't quite right about the name
$this->response->set_status_descriptor('USER_AUTO_REG');
$this->response->add_data_line('User auto registration failed - could not extract first and last names from avatar name "'.
$avname.
'".');
// Register the new Moodle account
// Did something go wrong?
$this->response->set_status_descriptor('USER_AUTO_REG');
$this->response->add_data_line('Moodle user creation failed.');
// Do we need to register a Sloodle entry?
// Create the Sloodle entry
); // Other parameters can be default
if ($newsloodle !==
TRUE) {
$this->response->set_status_descriptor('USER_AUTO_REG');
$this->response->add_data_line('Sloodle user creation failed.');
// We already have a Sloodle user
// Just link the users together
$this->response->set_status_descriptor('USER_AUTO_REG');
$this->response->add_data_line('Failed to link new Sloodle and Moodle users together.');
// User has been auto-registered, so add a side-effect code to our response
// Finally, attempt to login the new user
* Authenticates the avatar identified in the request by the login security token.
* Uses the avatar UUID and/or name, and the security token request to parameters to
* check against the details in the database.
* @param bool $use_cache If true (not default) then the function will use the user data stored in Sloodle user cache of the {@link: $user} member object (instead of querying the database for new data).
* @return bool True if authentication was successful, or false if not.
// Was a login security token specified in the request? Do nothing if not
// Are we using the cache?
// No - fetch fresh data and ensure it worked
if ($this->update_sloodle_user_cache_from_db() !==
TRUE) return FALSE;
// Make sure the login security token is specified in the cache
// Compare the security tokens
* Checks that a user is enrolled in a course, based on request data.
* Fetches user and course identification from the standard request parameters.
* @param bool $require If true (default) then the function will terminate with an LSL-friendly error message if user is not enrolled, or not all data was present.
* @param bool $use_cache If true (not default) then the courses cache in the {@link: $user} member will be used, instead of querying the database for fresh data.
* @return bool True if the user is enrolled, or false otherwise.
// Make sure the course exists
$this->response->set_status_descriptor('USER_ENROL');
$this->response->add_data_line('User not enroled in course, and auto-enrolment is not yet implemented.');
Documentation generated on Tue, 04 Mar 2008 15:08:48 +0000 by phpDocumentor 1.4.0