Source for file sl_webintercom_linker.php

Documentation is available at sl_webintercom_linker.php

  1. <?php
  2.     // Sloodle WebIntercom linker script
  3.     // Allows a Sloodle WebIntercom in-world to communicate with a web-based Moodle chat-room
  4.     // Part of the Sloodle project (www.sloodle.org)
  5.     //
  6.     // Copyright (c) 2007 Sloodle
  7.     // Released under the GNU GPL
  8.     //
  9.     // Contributors:
  10.     //   various - original design and implementation
  11.     //   Peter R. Bloomfield - rewritten for Sloodle 0.2 (using new API/comms. format, and resolved outstanding issues)
  12.     //
  13.     
  14.     /**
  15.     * Sloodle WebIntercom linker script.
  16.     *
  17.     * Allows a Sloodle WebIntercom in-world to communicate with a web-based Moodle chatroom.
  18.     *
  19.     * @package sloodlechat
  20.     * @copyright Copyright (c) 2007-8 Sloodle (various contributors)
  21.     * @license http://www.gnu.org/licenses/gpl-3.0.html GNU GPL v3
  22.     *
  23.     * @contributor (various)
  24.     * @contributor Peter R. Bloomfield
  25.     *
  26.     */
  27.     
  28.     // This script is expected to be requested by in-world objects.
  29.     // The following parameters are required:
  30.     //
  31.     //   sloodlepwd = prim password for authentication
  32.     //   sloodlecourseid = ID of the course which the chat-room is in
  33.     //
  34.     // The following parameters may be required for certain modes of operation:
  35.     //
  36.     //   sloodlemoduleid = course module instance ID of the chatroom we are connecting to
  37.     //   sloodlechatmsg = the body of a chat message to be sent to the chatroom
  38.     //   sloodleuuid = UUID of the avatar who entered the chat message (optional if 'sloodleavname' is specified)
  39.     //   sloodleavname = name of the avatar who entered the chat message (optional if 'sloodleuuid' is specified)
  40.     //
  41.     // The script has 3 modes of operation.
  42.     // If only the basic required parameters are specified, then it is in selection mode (i.e. selecting your chat-room).
  43.     // The script will return a list of available chat-rooms in the data lines, with each line taking this format:
  44.     //  "<moduleid>|<chatroomname>"
  45.     // Note: success return code is 1, although will return -601 if no chat-rooms are found in course.
  46.     //
  47.     // The other mode of operation is chat mode (which interacts with a specific chat-room).
  48.     // It requires that the 'sloodlemoduleid' parameter is specified.
  49.     // At the basic level, this mode queries the list of messages in the chat-room, and returns the most recent ones.
  50.     // However, if 'sloodlechatmsg', and 'sloodleuuid' and/or 'sloodleavname' are specified, then
  51.     //  the script will attempt to add the chat message to the chat-room, attributing it to the specified user.
  52.     //
  53.     // In chat mode, if all goes well (i.e. no errors adding a chat message to the database), then each line of data
  54.     //  following the status line will contain a recent chat message. The format is as follows:
  55.     //  "<msgid>|<chattername>|<msg>"
  56.     // The 'msgid' is the ID number of the chat message (unique across a whole site).
  57.     // 'chattername' is the Moodle name of the person who chatted the message.
  58.     // 'msg' is the chat message text.
  59.     //
  60.     
  61.     header ('Content-Type: text/plain; charset=UTF-8');
  62.  
  63.     require_once('../../config.php');
  64.     require_once(SLOODLE_DIRROOT.'/sl_debug.php');
  65.     require_once(SLOODLE_DIRROOT.'/lib/sl_lsllib.php');
  66.     require_once('sl_webintercom_lib.php');
  67.     
  68.     sloodle_debug_output('<br/>');
  69.     
  70.     // Create an LSL handler and process the basic request data
  71.     sloodle_debug_output('Creating LSL handler...<br/>');
  72.     $lsl new SloodleLSLHandler();
  73.     sloodle_debug_output('Processing basic request data...<br/>');
  74.     $lsl->request->process_request_data();
  75.     
  76.     // Ensure the request is authenticated
  77.     sloodle_debug_output('Authenticating request...<br/>');
  78.     $lsl->request->authenticate_request();
  79.     
  80.     // Obtain the additional parameters
  81.     sloodle_debug_output('Obtaining additional parameters...<br/>');
  82.     $sloodlechatmsg optional_param('sloodlechatmsg'NULLPARAM_RAW);
  83.     
  84.     // Try to find the specified course
  85.     // (this will fail and terminate if the course is not visible)
  86.     sloodle_debug_output('Finding requested course...<br/>');
  87.     $course $lsl->request->get_course_record();
  88.     
  89.     // Are we in select mode?
  90.     if ($lsl->request->get_module_id(== NULL{
  91.         // Get a list of all visible chatrooms module instances in the specified course
  92.         sloodle_debug_output('Fetching all chatrooms in course...<br/>');
  93.         $chatrooms sloodle_get_visible_chatrooms_in_course($lsl->request->get_course_id());
  94.         // Did we fail to retrieve any?
  95.         if (!is_array($chatrooms|| count($chatrooms== 0{
  96.             $lsl->response->set_status_code(-601);
  97.             $lsl->response->set_status_descriptor('MODULE');
  98.             $lsl->response->add_data_line('No chatrooms found in course.');
  99.             $lsl->response->render_to_output();
  100.             exit();
  101.         }
  102.         
  103.         // Start building the response
  104.         sloodle_debug_output('Constructing response...<br/>');
  105.         $lsl->response->set_status_code(1);
  106.         $lsl->response->set_status_descriptor('OK');
  107.         // Add the whole list of chatrooms
  108.         sloodle_debug_output('Adding list of chatrooms...<br/>');
  109.         foreach ($chatrooms as $id=>$chat{
  110.             $lsl->response->add_data_line(array($id$chat->name));
  111.         }
  112.         
  113.         // Output the response and finish
  114.         sloodle_debug_output('Outputting response...<br/>');
  115.         sloodle_debug_output('<pre>');
  116.         $lsl->response->render_to_output();
  117.         sloodle_debug_output('</pre>');
  118.         exit();
  119.     }
  120.     
  121.     
  122.     // Fetch the course module instance for the chatroom (terminates with LSL error message on failure)
  123.     sloodle_debug_output('Fetching course module instance...<br/>');
  124.     $cmi $lsl->request->get_course_module_instance('chat');
  125.     // Now get the chatroom module itself
  126.     sloodle_debug_output('Fetching activity module...<br/>');
  127.     if (!$chatroom get_record('chat''id'$cmi->instance)) {
  128.         $lsl->response->set_status_code(-712);
  129.         $lsl->response->set_status_descriptor('MODULE_INSTANCE');
  130.         $lsl->response->add_data_line('Failed to retrieve chatroom record from database.');
  131.         $lsl->render_to_output();
  132.         exit();
  133.     }
  134.     
  135.     
  136.     // Has a chat message been specified?
  137.     if ($sloodlechatmsg != NULL{
  138.         sloodle_debug_output('There is a chat message to add...<br/>');
  139.         // Yes - attempt to login the user. Allow auto-registration, but do not terminate on failure
  140.         sloodle_debug_output('Attempting to login user...<br/>');
  141.         if (!$lsl->login_by_request(FALSE)) {
  142.             // Login as the guest account
  143.             sloodle_debug_output('Login failed... attempting to login as a guest...<br/>');
  144.             $guestinfo get_guest();
  145.             $USER get_complete_user_data('id',$guestinfo->id);
  146.         }
  147.         
  148.         // Make sure the chat message is 'safe'
  149.         sloodle_debug_output('Constructing chatroom message...<br/>');
  150.         $sloodlechatmsg addslashes(clean_text(stripslashes($sloodlechatmsg)FORMAT_MOODLE));
  151.         
  152.         // Format our message data for a database record
  153.         $message->chatid $chatroom->id// Pass the ID of chat room into the database.
  154.         $message->userid $USER->id//Pass the ID of the chat user into the database.
  155.         $message->message $sloodlechatmsg// Pass the message in.
  156.         $message->timestamp time()// Add the unix timestamp
  157.         
  158.         // Insert the chat message into the database
  159.         sloodle_debug_output('Adding message to database...<br/>');
  160.         if (!insert_record('chat_messages'$message)) {
  161.             // Something went wrong
  162.             sloodle_debug_output('-&gt; Failed.<br/>');
  163.             $lsl->response->set_status_code(-10101);
  164.             $lsl->response->set_status_descriptor('MODULE_INSTANCE');
  165.             $lsl->response->add_data_line('Failed to add chat message to database.');
  166.             $lsl->render_to_output();
  167.             exit();
  168.         }
  169.         
  170.         // We successfully added a chat message... make a note of it as a side-effect
  171.         $lsl->response->add_side_effect(10101);
  172.     }
  173.     
  174.     
  175.     // OK... *finally* we can get around to querying the database for chat messages.... hurrah!
  176.     
  177.     // Construct an SQL query to get all recent chat messages from the database
  178.     sloodle_debug_output('Querying for recent chat messages...<br/>');
  179.     $time_delay time(1*60;
  180.     $query "  SELECT {$CFG->prefix}chat_messages.id, {$CFG->prefix}chat_messages.chatid, {$CFG->prefix}chat_messages.userid, {$CFG->prefix}user.firstname, {$CFG->prefix}user.lastname, {$CFG->prefix}chat_messages.message, {$CFG->prefix}chat_messages.groupid, {$CFG->prefix}chat_messages.system, {$CFG->prefix}chat_messages.timestamp
  181.                 FROM {$CFG->prefix}chat_messages
  182.                 LEFT JOIN {$CFG->prefix}user ON {$CFG->prefix}chat_messages.userid = {$CFG->prefix}user.id
  183.                 WHERE ((({$CFG->prefix}chat_messages.chatid)={$chatroom->id}) AND(({$CFG->prefix}chat_messages.timestamp)>=$time_delay))
  184.                 ORDER BY {$CFG->prefix}chat_messages.timestamp DESC";
  185.     $messages get_records_sql($query);
  186.  
  187.  
  188.     // Make sure we got some results
  189.     sloodle_debug_output('Iterating through recent chat messages...<br/>');
  190.     // Make sure we got some chat messages
  191.     if (is_array($messages)) {
  192.         // Go through each chat message
  193.         foreach($messages as $m{
  194.             // On each line of the data response, output one message in this format: "<id>|<name>|<text>"
  195.             $lsl->response->add_data_line(array($m->id$m->firstname.' '.$m->lastname$m->message));
  196.         }
  197.     }
  198.     
  199.     // Construct and output the rest of the response
  200.     sloodle_debug_output('Outputting response...<br/>');
  201.     $lsl->response->set_status_code(1);
  202.     $lsl->response->set_status_descriptor('OK');
  203.     sloodle_debug_output('<pre>');
  204.     $lsl->response->render_to_output();
  205.     sloodle_debug_output('</pre>');
  206.  
  207.     exit();
  208.  
  209. ?>

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