Sloodle 0.2 Linker Script Example

From SLIS Second Life Wiki
Jump to: navigation, search
WARNING: this example will only work with Sloodle 0.2 and 0.21.

<?php

// Remember to explain what your script does in comments up here!


///// INCLUDING FILES /////

// Include the Sloodle configuration script
// (this is in the root of the Sloodle folder).
// It also includes all the Moodle libraries.
// Remember to fix this so it works from where your script is!
require_once('config.php');

// For all other scripts, use the constants defined in 'config.php'
// E.g. "SLOODLE_DIRROOT"

// We need the debug script for linker scripts
require_once(SLOODLE_DIRROOT.'/sl_debug.php');
// We need the LSL handling functionality
require_once(SLOODLE_DIRROOT.'/lib/sl_lsllib.php');


///// DEBUGGING /////

// Since 'sl_debug.php' was included, we can use debug mode features
// It is important to use this in some places, to make manual testing easier
sloodle_debug_output('This will only be outputed when we are in debug mode.');

// This is how we check if we are in debug mode:
if (defined('SLOODLE_DEBUG') && SLOODLE_DEBUG) {
echo
'We are in debug mode.';
}


///// INITIAL SETUP /////

// Create an LSL handler object and process all incoming data
// (You must do this):
$lsl = new SloodleLSLHandler();
$lsl->request->process_request_data();


///// PARAMETERS /////

// If there are any special request (GET or POST) parameters for this script, there are two ways to get them
// NOTE: the standard Sloodle parameters (e.g. "sloodleavname", "sloodlepwd" etc.) were all processed and stored
// by the "$lsl->process_request_data()" function above. Use the accessors in $lsl->request (which is of the
// SloodleLSLRequest type) to retrieve the data.

// If it is an optional parameter, then you can just use the Moodle function
$myparam = optional_param('somevalue', NULL, PARAM_INT);

// If it is a required parameter, you *must* use the Sloodle function
// (This will halt with an LSL error message if it fails):
$myparam = $lsl->request->required_param('somevalue', PARAM_INT);



///// PRE-PROCESSING /////

// Authenticate the request (checks prim password)
// (Only do this if you need security, and the object provided a prim password)
// (This function will terminate the script with an LSL error message authentication fails)
// This uses the 'sloodlepwd' request parameter
$lsl->request->authenticate_request();


// If your in-world object refers to a particular user (e.g. when writing a blog)
// and so you need them to be logged-in, use the following function.
// (If auto-registration is active, this is auto-register them).
// This uses the 'sloodleavname' and/or 'sloodleuuid' request parameters
$lsl->login_by_request();


// If the request refers to a specific course, then you can get the course record here.
// It is just a record from the database, and it does not verify that the user is enrolled.
// It just checks that the course exists and is visible.
// In future, it will also check that the course is Sloodle-enabled and that the user is enrolled in it (if applicable).
// (The script will terminate with an LSL error message if something goes wrong)
// This uses the 'sloodlecourseid' request parameter.
$course_record = $lsl->request->get_course_record();


// If the request refers to a specific course module instance (e.g. a forum) then we can get it here
// This will make sure that the instance exists, is of the correct type, and that it is visible.
// You need to specify the name of the type in a string, and you need to be very accurate...
// It should be lower-case, and should match the name of the folder the module is in within the "mod" folder.
// (The script will terminate with an LSL error message if something goes wrong)
// It simply returns a database record from Moodle's 'course_modules' table
$cmi = $lsl->request->get_course_module_instance('forum'); // Could also be 'choice', 'quiz', 'wiki' etc.



///// MAIN PROCESSING /////


// If you logged-in a user up above, you can get their data directly
$lsl->user->sloodle_user_cache;
$lsl->user->moodle_user_cache;
// The user caches are database records, so you could do this:
$avatar_name = $lsl->user->sloodle_user_cache->avname;
$moodle_name = $lsl->user->moodle_user_cache->firstname .' '. $lsl->user->moodle_user_cache->lastname;
// There is lots of other data you can access!


// If you want to alter the Sloodle user data, then simply alter the cache object,
// and then tell the user object to update the database
$lsl->user->sloodle_user_cache->avname = 'Scooby Doo';
$result = $lsl->user->update_sloodle_user_cache_to_db();
// $result will be TRUE if successful, or FALSE or a string if an error occured
if ($result === FALSE) {
// Failed!
exit();
} else if (
is_string($result)) {
// Failed - $result contains an error message
exit();
}



// You can do anything else you need here.
//...
//... your code here!
//...



///// SENDING A RESPONSE /////

// Constructing your response is very important.
// The response will already contain lots of data from the original request,
// such as the user's UUID, the request descriptor, and any side effect codes (such as auto registration)

// You must set a status code and preferably also a status descriptor (see http://slisweb.sjsu.edu/sl/index.php/Sloodle_status_codes)
$lsl->response->set_status_code(1);
$lsl->response->set_status_descriptor('OK');
// See the "SloodleLSLResponse" class in "lib/sl_iolib.php" for more items you can add to the status line

// The main data part of the response is optional.
// You can add each line as a string (or any individual value):
$lsl->response->add_data_line('This is a line');
$lsl->response->add_data_line(42); // Numbers work too
$lsl->response->add_data_line('Another line');

// You also add an array of values, which get display on one line, separated by vertical bars |
$lsl->response->add_data_line( array("one", 2, "three") ); // (This becomes "one|2|three")


// Finally... you need to output your response data
/// ***** THIS IS REQUIRED FOR OUTPUT! *****
$lsl->response->render_to_output(); // Returns FALSE if you've missed any important values

// You can also render to a string, e.g. if you want to use XMLRPC instead
$str = "";
$lsl->response->render_to_string($str); // Returns FALSE if you've missed any important values



///// FINISHED! /////

?>

Personal tools