Source for file avilister_linker.php

Documentation is available at 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
  13.     *
  14.     */
  15.     
  16.     // Access to this script can be authenticated in two ways: user-centric, and course-centric.
  17.     // Both modes require the "sloodlepwd" parameter.
  18.     // User-centric mode requires the avatar's UUID as "sloodleuuid".
  19.     // Course-centric mode requires a Controller ID as "sloodlecontrollerid".
  20.     //
  21.     // NOTE: course-centric mode will be assumed if "sloodlecontrollerid" is present. User-centric mode otherwise.
  22.     //
  23.     // If using course-centric authentication, and no other parameters are provided, then a list of all
  24.     //  avatar/Moodle names in the given course are returned. Attempting this from user-centric
  25.     //  authentication will fail, returning status code -331.
  26.     //
  27.     //
  28.     // In either authentication mode, the Moodle name of a specific avatar can be checked by specifying
  29.     //  the following parameter:
  30.     //
  31.     //   sloodlelookupavname = name of an avatar lookup
  32.     //
  33.     //
  34.     // Alternatively, in either authentication mode, the Moodle names of several avatars can be checked by
  35.     //  specifying the following parameter:
  36.     //
  37.     //   sloodleavnamelist = a list of avatar names, separated by pipe characters
  38.     //
  39.     
  40.     // In any mode, the script will return status code 1 on success, and the following on each data line:
  41.     //  "<avatar_name>|<real_name>"
  42.     // Single lookup mode returns 1 entry, whereas multi-lookup and list modes may return many.
  43.     // If the single lookup fails, then error code -103 is returned.
  44.     // If the other modes fail (i.e. no matching avatars found), then there are simply no data lines -- the status code is still 1.
  45.     //
  46.     // Note: data will *not* be given for Moodle users who have no avatar, nor avatars not linked to a Moodle user
  47.     
  48.     
  49.     /** Lets Sloodle know we are in a linker script. */
  50.     define('SLOODLE_LINKER_SCRIPT'true);
  51.     
  52.     /** Grab the Sloodle/Moodle configuration. */
  53.     require_once('../sl_config.php');
  54.     /** Include the Sloodle PHP API. */
  55.     require_once(SLOODLE_LIBROOT.'/sloodle_session.php');
  56.     
  57.     // Process incoming data
  58.     $sloodle new SloodleSession();
  59.     
  60.     // Check what type of authentication we are using
  61.     $course_centric false;
  62.     if ($sloodle->request->get_controller_id(false== null{
  63.         // Attempt user-centric authentication
  64.         $sloodle->authenticate_user_request();
  65.         $sloodle->validate_avatar();
  66.     else {
  67.         // Attempt course-centric authentication
  68.         $sloodle->authenticate_request();
  69.         $course_centric true;
  70.     }
  71.     
  72.     // Load the AviLister module
  73.     $sloodle->load_module('avilister'false)// No database data required
  74.     
  75.     // Check for other parameters
  76.     $sloodlelookupavname $sloodle->request->optional_param('sloodlelookupavname');
  77.     $sloodleavnamelist $sloodle->request->optional_param('sloodleavnamelist');
  78.     
  79.     // Check what mode we are in (0 = course names, 1 = single-lookup, 2 = multi-lookup)
  80.     $mode 0;
  81.     if (!empty($sloodlelookupavname)) $mode 1;
  82.     else if (!empty($sloodleavnamelist)) $mode 2;
  83.     switch ($mode{
  84.     case 0:
  85.         // Course names - not valid with user-centric authentication
  86.         if (!$course_centric{
  87.             $sloodle->response->quick_output(-331'USER_AUTH''Cannot access this information via user-centric authentication'false);
  88.             exit();
  89.         }
  90.         // Get the array of names
  91.         $names $sloodle->module->find_real_names_course();
  92.         if (!$names || count($names== 0{
  93.             $sloodle->response->quick_output(-512'COURSE''Failed to access course to retrieve enrolled user data'false);
  94.             exit();
  95.         }
  96.         // Output the list of names
  97.         natcasesort($names);
  98.         foreach ($names as $avatar_name => $real_name{
  99.             $sloodle->response->add_data_line(array($avatar_name$real_name));
  100.         }
  101.         break;
  102.         
  103.     case 1:
  104.         // Single lookup mode
  105.         // Attempt to get the real name
  106.         $sloodlelookupavname addslashes(strip_tags($sloodlelookupavname));
  107.         $real_name $sloodle->module->find_real_name(''$sloodlelookupavname);
  108.         if (!$real_name{
  109.             $sloodle->response->quick_output(-103'MISC''Could not locate requested avatar name'false);
  110.             exit();
  111.         }
  112.         $sloodle->response->add_data_line(array($sloodlelookupavname$real_name));
  113.         break;
  114.         
  115.     case 2:
  116.         // Split up the input array
  117.         $sloodleavnamelist addslashes(strip_tags($sloodleavnamelist));
  118.         $avnames explode('|'$sloodleavnamelist);
  119.         // Get and output the array of names
  120.         $names $sloodle->module->find_real_names($avnames);
  121.         if ($names && count($names0{
  122.             natcasesort($names);
  123.             foreach ($names as $avatar_name => $real_name{
  124.                 $sloodle->response->add_data_line(array($avatar_name$real_name));
  125.             }
  126.         }
  127.         break;
  128.     }
  129.     
  130.     
  131.     // Output our response
  132.     $sloodle->response->set_status_code(1);
  133.     $sloodle->response->set_status_descriptor('OK');
  134.     $sloodle->response->render_to_output();
  135.     
  136.     exit();
  137. ?>

Documentation generated on Mon, 07 Jul 2008 12:32:15 +0100 by phpDocumentor 1.4.0