Source for file sl_send_object.php

Documentation is available at sl_send_object.php

  1. <?php
  2.     /**
  3.     * Sloodle object distribution interface page.
  4.     *
  5.     * Can be accessed by Moodle users in a web-browser to request the distribution of in-world objects.
  6.     *
  7.     * @package sloodledistrib
  8.     * @copyright Copyright (c) 2008 Sloodle (various contributors)
  9.     * @license http://www.gnu.org/licenses/gpl-3.0.html GNU GPL v3
  10.     *
  11.     * @contributor Edmund Edgar
  12.     * @contributor Peter R. Bloomfield
  13.     *
  14.     */
  15.     
  16.     // This script should only be accessed by a browser (a script linker version can be found at "sl_send_object_linker.php")
  17.     // The following parameters are optional, but are typically provided by this script submitting a form to itself:
  18.     //
  19.     //   cmd = indicates what action should be carried out (can be 'sendobject' to send an object, or ommitted completely to select the users)
  20.     //   fname = first name of the avatar to whom to send an object (required if 'lname' is specified)
  21.     //   lname = last name of the avatar to whom to send an object (required if 'fname' is specified)
  22.     //   uuid = UUID of an avatar to whom to send an object
  23.     //   object = name of the object to send (required if 'cmd' is 'sendobject')
  24.     //
  25.     
  26.  
  27.     $sendToArbitraryAvatars false// set this to true to make it possible to enter an avatar name for someone not already registered on the site. This needs a way of getting the UUID for the avatar name - I've implemented this with a web service at webservices.socialminds.jp using LibSL, but I can't guarantee this will be running and available, so for now it's turned off and you can only send objects to avatars already registered in the system.
  28.  
  29.     require_once('../config.php');
  30.     
  31.     require_login();
  32.     
  33.     require_once(SLOODLE_DIRROOT.'/lib/sl_lsllib.php');
  34.     
  35.     // See if we can find a Sloodle user for this account
  36.     $sl_user new SloodleUser();
  37.     $sl_user->set_moodle_user_id($USER->id);    $sl_user->find_linked_sloodle_user();
  38.     $avname '';
  39.     if ($sl_user->get_sloodle_user_id(0$avname $sl_user->sloodle_user_cache->avname;
  40.     // Anybody with teacher status or higher can send objects to anybody on the system
  41.     // (Everybody else can just send them to their own avatar)
  42.     $show_all_users isteacherinanycourse()// This includes admins
  43.  
  44.     // Construct our bread-crumbs navigation links
  45.     $nav array();
  46.     $nav "<a href=\"{$CFG->wwwroot}/admin/module.php?module=sloodle\" title=\"Sloodle\">Sloodle</a> ->";
  47.     $nav .= "<a href=\"".SLOODLE_WWWROOT."/sl_distrib/sl_send_object.php\">".get_string('sloodleobjectdistributor''sloodle')."</a>";
  48.     print_header(get_string('sloodleobjectdistributor''sloodle')get_string('sloodleobjectdistributor''sloodle')$nav''''false);
  49.  
  50.     // Fetch the key for the XMLRPC channel
  51.     $distribchannel sloodle_get_config('sloodle_distrib_channel');
  52.     if (($distribchannel == NULL|| ($distribchannel == ''|| ($distribchannel == FALSE)) {
  53.         error(get_string('sloodleobjectdistributor:nochannel''sloodle'));
  54.         exit();
  55.     }
  56.     
  57.     // Fetch the list of objects
  58.     $distribobjects sloodle_get_distribution_list();
  59.     if (!is_array($distribobjects|| count($distribobjects== 0{
  60.         error(get_string('sloodleobjectdistributor:noobjects''sloodle'));
  61.         exit();
  62.     }
  63.     sort($distribobjectsSORT_STRING);
  64.  
  65.     // Fetch the additional parameters
  66.     $cmd optional_param('cmd',null,PARAM_RAW);
  67.     $fname optional_param('fname'''PARAM_RAW);
  68.     $lname optional_param('lname'''PARAM_RAW);
  69.     $uuid optional_param('uuid'''PARAM_RAW);
  70.     $object optional_param('object'''PARAM_RAW);
  71.     
  72.     
  73.     
  74.     // What command was specified?
  75.     switch ($cmd{
  76.     case NULL:
  77.         // No command - just show the page of options
  78.     
  79.         // Construct a list of all users to whom data may be sent
  80.         $allSloodleUsers get_records('sloodle_users');
  81.         $keysToNames array();
  82.         if (is_array($allSloodleUsers)) {
  83.             foreach($allSloodleUsers as $su{
  84.                 if ( ($su->uuid != ''&& ($su->avname != '') ) {
  85.                     $keysToNames[$su->uuid$su->avname;
  86.                 }
  87.             }
  88.             asort($keysToNames);
  89.         }
  90.         
  91.         // Construct a selection box of objects available for distribution
  92.         $objselect '<select name="object">';
  93.         foreach ($distribobjects as $obj{
  94.             // Make sure it isn't empty
  95.             if (is_string($obj&& !empty($obj)) {
  96.                 $objselect .= "<option value=\"$obj\">$obj</option>";
  97.             }
  98.         }
  99.         $objselect .= '</select>';
  100.         
  101.         // This value will determine whether to disable the "send to me" form submission button
  102.         $disable_send_to_me ($sl_user->get_sloodle_user_id(<= 0);
  103.         
  104.         ?>
  105.         
  106.         
  107.         <div style="text-align:center;width:100%;">
  108.         <center>
  109.         <h3 style="width:100%;text-align:center;">
  110.         <?php
  111.          print_string('sloodleobjectdistributor''sloodle');
  112.          helpbutton('object_distributor'get_string('sloodleobjectdistributor''sloodle')'sloodle');
  113.         ?>
  114.         </h3>
  115.         
  116.         <!-- Sending to self -->
  117.         <form   method="GET" action="<?php print $_SERVER['PHP_SELF']?>">
  118.          <input type="hidden" name="cmd" value="sendobject" />
  119.         
  120.          <?php
  121.           if ($sl_user->get_sloodle_user_id(0{
  122.             print '<input type="hidden" name="uuid" value="'.$sl_user->sloodle_user_cache->uuid.'"/>';
  123.             print '<table style="border:solid 1px black;width:450px;">';
  124.           else {
  125.             print '<table style="border:solid 1px #555555; background-color:#cccccc; width:450px;">';
  126.           }
  127.          ?>
  128.          
  129.           <tr><th colspan="2" style="padding-bottom:8px;"><?php print_string('sloodleobjectdistributor:sendtomyavatar','sloodle')?></th></tr>
  130.  
  131.           <tr>
  132.            <td style="text-align:right;width:40%;"><?php print_string('selectobject','sloodle')?>: </td>
  133.            <td style="text-align:left; width:60%;"><?php print $objselect?></td>
  134.           </tr>
  135.          
  136.           <tr>
  137.            <td colspan="2" style="text-align:center; padding:20px;">
  138.             <input  type="submit"
  139.                     value="<?php
  140.                             print_string('sloodleobjectdistributor:sendtomyavatar','sloodle');
  141.                             if (!$disable_send_to_meprint ' ('.$sl_user->sloodle_user_cache->avname.')';
  142.                            ?>"
  143.                     <?php if ($disable_send_to_meprint 'disabled="true"'?> />
  144.             <?php
  145.              if ($disable_send_to_me{
  146.               print '<br/><span style="color:red;">';
  147.               print_string('avatarnotlinked','sloodle');
  148.               print '</span>';
  149.              }
  150.             ?>
  151.            </tr>
  152.           </td>
  153.          
  154.          </table>
  155.         </form>
  156.         
  157.         <!-- Sending to a custom avatar -->
  158.         <?php
  159.         if ($show_all_users{
  160.             print '<form method="get" action="'.$_SERVER['PHP_SELF'].'">';
  161.             print '<table style="width:450px; border:solid 1px black; margin-top:28px;">';
  162.             
  163.             print '<tr><th colspan="3" style="text-align:center; padding-bottom:8px;">';
  164.             print_string('sloodleobjectdistributor:sendtoanotheravatar','sloodle');
  165.             print '</th></tr>';
  166.            
  167.             // Show the avatar name input boxes
  168.             if ($sendToArbitraryAvatars{
  169.                 print '<tr><td style="text-align:right; width:40%;">';
  170.                 print_string('enteravatarname','sloodle');
  171.                 print ': </td><td>';
  172.                 print '<input type="text" name="fname" value="'.$fname.'" />';
  173.                 print '</td><td>';
  174.                 print '<input type="text" name="lname" value="'.$lname.'" />';
  175.                 print '</td></tr>';
  176.             }
  177.             
  178.             // Show the select users combo box
  179.             print '<tr><td style="text-align:right; width:40%;">';
  180.             if ($sendToArbitraryAvatars{
  181.                 print_string('or','sloodle');
  182.                 print '&nbsp;&nbsp;';
  183.             }
  184.             
  185.             print_string('selectuser','sloodle');
  186.             print ': </td><td colspan="2" style="text-align:left;">';
  187.             if (count($keysToNames0{
  188.                 asort($keysToNames);
  189.                 print '<select name="uuid">';
  190.                 foreach($keysToNames as $k=>$n{
  191.                     print '<option value="'.$k.'">'.$n.'</option>';
  192.                 }
  193.                 print '</select>';
  194.             else {
  195.                 print '<span style="color:red;">('.get_string('nosloodleusers','sloodle').')</span>';
  196.             }
  197.             print '</td></tr>';
  198.             
  199.             // Show the object selection box
  200.             print '<tr><td style="text-align:right;">';
  201.             print_string('selectobject''sloodle');
  202.             print ': </td><td colspan="2">';
  203.             print $objselect;
  204.             print '</td></tr>';
  205.         
  206.             print '<tr><td align="center" colspan="3" style="padding:20px;">';
  207.             print '<input type="hidden" name="cmd" value="sendobject" />';
  208.             print '<input type="submit" value="'.get_string('sendobject','sloodle').'"/>';
  209.             print '</td></tr>';
  210.             print '</table>';
  211.             print '</form>';
  212.         }
  213.         ?>
  214.         </center>
  215.         </div>
  216.         <?php
  217.         
  218.        
  219.         break;
  220.         
  221.      case 'sendobject':
  222.         // Send an object to the specified user
  223.         // Has the UUID been omitted?
  224.         if ($uuid == ''{
  225.             // We need to find an avatar key
  226.             // Look them up in the Sloodle user table
  227.             $sl_user new SloodleUser();
  228.             if ($sl_user->find_sloodle_user(''$fname.' '.$lname)) {
  229.                 $uuid $sl_user->sloodle_user_cache->uuid;
  230.             }
  231.         
  232.             // If that failed, are we allowed to use the name2key system?
  233.             if (empty($uuid&& $sendToArbitraryAvatars{
  234.                 // Make sure the full name was specified
  235.                 if ( ($fname != ''&& ($lname != '') ) {
  236.                     // Attempt to retrieve the key
  237.                     $uuid keyForName($fname,$lname);
  238.                 }
  239.             }
  240.             
  241.             // Do we have a UUID yet?
  242.             if (empty($uuid)) {
  243.                 // No - we can't go any futher
  244.                 error(get_string('sloodleobjectdistributor:usernotfound','sloodle'));
  245.                 break;
  246.             }
  247.         }
  248.         
  249.         // Construct our data response
  250.         $response new SloodleLSLResponse();
  251.         $response->set_status_code(1);
  252.         $response->set_status_descriptor('OK');
  253.         $response->add_data_line(array('SENDOBJECT',$uuid,$object));
  254.         // Render it to a string so we can send it
  255.         $str '';
  256.         $response->render_to_string($str);
  257.         $str str_replace("\n""\\n"$str)// Ugly hack to stop the newline character disappearing before it reaches SL
  258.         
  259.         // Send the XMLRPC
  260.         $ok sloodle_send_xmlrpc_message($distribchannel,0,$str);
  261.         
  262.         // What was the result?
  263.         if ($ok{
  264.             print '<h3 style="color:green;text-align:center;">'.get_string('sloodleobjectdistributor:successful','sloodle').'</h3>';
  265.             print '<p style="text-align:center;">';
  266.             print get_string('Object','sloodle').': '.$object.'<br/>';
  267.             print get_string('uuid','sloodle').': '.$uuid.'<br/>';
  268.             print get_string('xmlrpc:channel','sloodle').': '.$distribchannel.'<br/>';
  269.             print '</p>';
  270.         else {
  271.             print '<h3 style="color:red;text-align:center;">'.get_string('sloodleobjectdistributor:failed','sloodle').'</h3>';
  272.             print '<p style="text-align:center;">';
  273.             print get_string('Object','sloodle').': '.$object.'<br/>';
  274.             print get_string('uuid','sloodle').': '.$uuid.'<br/>';
  275.             print get_string('xmlrpc:channel','sloodle').': '.$distribchannel.'<br/>';
  276.             print '</p>';
  277.         }
  278.         break;
  279.         
  280.     default:
  281.         // Command unknown - issue an error
  282.         error(get_string('sloodleobjectdistributor:unknowncommand','sloodle'));
  283.         exit();
  284.     }
  285.     
  286.     
  287.     // Display the page footer if this is a browser request
  288.     print_footer();
  289.  
  290. // Uses a web service lookup to fetch the key for an arbitrary name - not currently using this here.
  291. function keyForName($fname,$lname{
  292.  
  293.     $url "http://webservices.socialminds.jp/name2key.php?fname=$fname&lname=$lname";
  294.     $handle fopen($url"r");
  295.     $contents fread($handle,8192);
  296.     fclose($handle);
  297.  
  298.     return $contents;
  299.  
  300. }
  301.  
  302. ?>

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