Source for file sl_avilister_linker.php

Documentation is available at sl_avilister_linker.php

  1. <?php
  2.     /**
  3.     * Sloodle AviLister linker script.
  4.     *
  5.     * Allows a tool in-world to fetch avatars' associated Moodle names.
  6.     *
  7.     * @package sloodle
  8.     * @copyright Copyright (c) 2007-8 Sloodle (various contributors)
  9.     * @license http://www.gnu.org/licenses/gpl-3.0.html GNU GPL v3
  10.     *
  11.     * @contributor Jeremy Kemp (and others?)
  12.     * @contributor Peter R. Bloomfield - rewritten and expanded for Sloodle 0.2 (using new API/comms. format, and added list mode)
  13.     *
  14.     */
  15.     
  16.     // This script is expected to be called by in-world Sloodle objects.
  17.     // The following parameters are required:
  18.     //
  19.     //   sloodlepwd = prim password for authentication
  20.     //
  21.     // The following parameters are required for certain modes:
  22.     //
  23.     //   sloodlecourseid = the course for which users are being retrieved
  24.     //   sloodleuuid = UUID of an avatar
  25.     //   sloodleavname = name of an avatar
  26.     //   sloodleavnamelist = a list of avatar names, separated by pipe characters
  27.     //
  28.     //
  29.     // The script operates in 3 modes: single-lookup, multi-lookup, and list
  30.     // If 'sloodleuuid' or 'sloodleavname' is specified, then single-lookup mode is assumed.
  31.     // In this mode, the script will fetch the data for that user only.
  32.     //
  33.     // Alternatively, if 'sloodleavnamelist' is specified, then multi-lookup mode is assumed.
  34.     // It will attempt to fetch data for each user in the list (but will ignore unrecognised names).
  35.     //
  36.     // In list mode, the script will fetch data for all users in a particular course.
  37.     // This requires that 'sloodlecourseid' is specified.
  38.     
  39.     // In either mode, the script will return status code 1 on success, and the following on each data line:
  40.     //  "<avatar_name>|<moodle_name>"
  41.     // (The <online> parameter is either 1 or 0, indicating whether or not that user is currently online in SL)
  42.     // Single lookup mode returns 1 entry, whereas multi-lookup and list modes may return many
  43.     //
  44.     // Note: data will *not* be given for Moodle users who have no avatar, nor avatars not linked to a Moodle user
  45.     
  46.     require_once('../../config.php');
  47.     require_once(SLOODLE_DIRROOT.'/sl_debug.php');
  48.     require_once(SLOODLE_DIRROOT.'/lib/sl_lsllib.php');
  49.     
  50.     sloodle_debug_output('<br/>');
  51.     
  52.     // Create an LSL handler and process the basic request data
  53.     sloodle_debug_output('Creating LSL handler...<br/>');
  54.     $lsl new SloodleLSLHandler();
  55.     sloodle_debug_output('Processing basic request data...<br/>');
  56.     $lsl->request->process_request_data();
  57.     
  58.     // Ensure the request is authenticated
  59.     sloodle_debug_output('Authenticating request...<br/>');
  60.     $lsl->request->authenticate_request();
  61.     
  62.     // Obtain additional parameters
  63.     sloodle_debug_output('Obtaining additional parameters...<br/>');
  64.     $sloodleavnamelist optional_param('sloodleavnamelist'NULLPARAM_RAW);
  65.     
  66.     // Are we in single lookup mode?
  67.     if (!(is_null($lsl->request->get_avatar_name()) && is_null($lsl->request->get_avatar_uuid()))) {
  68.         // We are in single lookup mode
  69.         sloodle_debug_output('***** Single lookup Mode *****<br/>');
  70.         // Attempt to login the Sloodle user to get their info (but suppress auto-registration)
  71.         $lsl->login_by_request(TRUETRUE);
  72.         
  73.         // Add the user data to the response
  74.         $lsl->response->set_status_code(1);
  75.         $lsl->response->set_status_descriptor('OK');
  76.         $lsl->response->add_data_line(array($lsl->user->sloodle_user_cache->avname$lsl->user->moodle_user_cache->firstname.' '.$lsl->user->moodle_user_cache->lastname));
  77.         
  78.     else if (!is_null($sloodleavnamelist&& !empty($sloodleavnamelist)) {
  79.         // Multi-lookup mode
  80.         sloodle_debug_output('***** Multi lookup Mode *****<br/>');
  81.         // Split up the list by pipe characters
  82.         $avnames explode('|'$sloodleavnamelist);
  83.         if (is_array($avnames)) {
  84.             // Setup our response
  85.             $lsl->response->set_status_code(1);
  86.             $lsl->response->set_status_descriptor('OK');
  87.             
  88.             $numnames count($avnames);
  89.             
  90.             // Go through each name to construct the "WHERE" part of a query
  91.             $wherequery "";
  92.             $isfirst TRUE;
  93.             foreach ($avnames as $av{
  94.                 if ($isfirst$isfirst FALSE;
  95.                 else $wherequery .= " OR";
  96.                 $wherequery .= " `avname` = '$av'";
  97.             }
  98.             // Construct the full query
  99.             $query =    "
  100.                             SELECT `avname`, `firstname`, `lastname`
  101.                             FROM `{$CFG->prefix}sloodle_users`
  102.                             LEFT JOIN `{$CFG->prefix}user`
  103.                              ON `{$CFG->prefix}sloodle_users`.`userid` = `{$CFG->prefix}user`.`id`
  104.                             WHERE $wherequery
  105.                             LIMIT 0,$numnames
  106.                         ";
  107.             $recs get_records_sql($query);
  108.             // Go through each returned record
  109.             if (is_array($recs)) {
  110.                 foreach ($recs as $r{
  111.                     $lsl->response->add_data_line(array($r->avname$r->firstname.' '.$r->lastname));
  112.                 }
  113.             }            
  114.             
  115.         else {
  116.             // Nothing to parse
  117.             $lsl->response->set_status_code(-811);
  118.             $lsl->response->set_status_descriptor('REQUEST');
  119.             $lsl->response->add_data_line('No list of avatar names.');
  120.         }
  121.     
  122.     else {
  123.     
  124.         // List mode
  125.         sloodle_debug_output('***** List Mode *****<br/>');
  126.         // Make sure the course ID was specified
  127.         if ($lsl->request->get_course_id(== NULL{
  128.             sloodle_debug_output('ERROR: no parameters specified.<br/>');
  129.             $lsl->response->set_status_code(-811);
  130.             $lsl->response->set_status_descriptor('REQUEST');
  131.             $lsl->response->add_data_line('Expected avatar UUID or name, avatar name list, or course ID.');
  132.         else {
  133.             // Make sure the course exists
  134.             if (!record_exists('course''id'$lsl->request->get_course_id())) {
  135.                 $lsl->response->quick_output(-512'COURSE''The specified course does not exist.'FALSE);
  136.                 exit();
  137.             }
  138.             // Everything seems OK... fetch a list of users in the course
  139.             sloodle_debug_output('Fetching list of users in course...<br/>');
  140.             $courseusers get_course_users($lsl->request->get_course_id());
  141.             if (!is_array($courseusers)) $courseusers array();
  142.             // Construct the response header
  143.             $lsl->response->set_status_code(1);
  144.             $lsl->response->set_status_descriptor('OK');
  145.             // Go through each one
  146.             $user new SloodleUser();
  147.             sloodle_debug_output('Iterating through list of users...<br/>');
  148.             foreach ($courseusers as $u{
  149.                 // Attempt to fetch the Sloodle data for this Moodle user
  150.                 $user->set_moodle_user_id($u->id);
  151.                 if ($user->find_linked_sloodle_user(!== TRUEcontinue;
  152.                 // Add the user to the response
  153.                 $lsl->response->add_data_line(array($user->sloodle_user_cache->avname$u->firstname.' '.$u->lastname));
  154.             }
  155.         }
  156.         
  157.     }
  158.     
  159.     // Output the response
  160.     sloodle_debug_output('Outputting response...<br/>');
  161.     sloodle_debug_output('<pre>');
  162.     $lsl->response->render_to_output();
  163.     sloodle_debug_output('</pre>');
  164.     
  165.     exit();
  166. ?>

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