Source for file sl_profile_linker.php

Documentation is available at sl_profile_linker.php

  1. <?php
  2.     
  3.     /**
  4.     * Sloodle classroom profile linker.
  5.     *
  6.     * Allows in-world objects (typically Sloodle Sets) to interact with Sloodle classroom setup profiles in the Moodle database.
  7.     *
  8.     * @package sloodleclassroom
  9.     * @copyright Copyright (c) 2006-7 Sloodle (various contributors)
  10.     * @license http://www.gnu.org/licenses/gpl-3.0.html GNU GPL v3
  11.     *
  12.     * @contributor Edmund Edgar
  13.     * @contributor Peter R. Bloomfield
  14.     *
  15.     */
  16.     
  17.     // Classroom profiles consist of a series of object entries.
  18.     // Each object entry consists of the object name, and relative position to the Sloodle Set.
  19.     
  20.     // This script is expected to be called from in-world objects.
  21.     // The following parameters are required:
  22.     //
  23.     //   sloodlepwd = prim password for authenticating the request
  24.     //   sloodleuuid = UUID of the user making the request (optional if 'sloodleavname' is specified)
  25.     //   sloodleavname = avatar name of the user making the request (optional if 'sloodleuuid' is specified)
  26.     //   sloodlecourseid = the ID of the course which the profile is in
  27.     //   sloodlecmd = the operation ('command') to be carried out
  28.     //
  29.     // Certain modes also require the following parameters:
  30.     //
  31.     //   sloodleprofilename = the name of the profile being handled
  32.     //   sloodleentries = the entries to be added to the profile
  33.     //
  34.     // The mode of the script is determined by the 'sloodlecmd' parameters.
  35.     //  'newprofile' creates a new profile with the name given in 'sloodleprofilename'.
  36.     //  'saveentries' will erase all existing entries, and add new object entries specified in 'sloodleentries' to the profile named in 'sloodleprofilename'.
  37.     //  'listentries' will list all object entries in the profile named in 'sloodleprofilename'.
  38.     //  'listprofiles' will list all profiles in the identified course.
  39.     //
  40.     
  41.     // When saving entries to a profile, the format of each entry in 'sloodleentries' should be as follows:
  42.     //
  43.     //  "name|position"
  44.     //
  45.     // The position should be given as a string vector "<x,y,z>".
  46.     // Note that object names should be unique within a given profile.
  47.     //
  48.     // Multiple entries should be separate by a double pipe ||.
  49.     // For example, three entries might look like this:
  50.     // 
  51.     // "Sloodle WebIntercom|<1.4,0.9,2.6>||Sloodle Registration Booth|<2.1,3.0,3.5>||Sloodle MetaGloss|<0.0,1.2,2.6>"
  52.     //
  53.     // There is not response data.
  54.     
  55.     // When adding a new profile, the response code will be 1 for success, and the ID and name of the profile will be given on the sole data line, e.g.:
  56.     //
  57.     //  23|My Profile
  58.     
  59.     // When returning the entries in a given profile, each line contain the entry ID, name and position, e.g.:
  60.     //
  61.     //  6|Sloodle WebIntercom|<1.4,0.9,2.6>
  62.     //  7|Sloodle Registration Booth|<2.1,3.0,3.5>
  63.     //  11|Sloodle MetaGloss|<0.0,1.2,2.6>
  64.     
  65.     // When returning the available profiles for the course, each one will be returned by id and name on its own line, e.g.:
  66.     //
  67.     //  2|topic1
  68.     //  15|demo
  69.     //  26|assessment
  70.     
  71.     // Some specific error codes which may be returned:
  72.     //
  73.     //   -901 = unknown profile error
  74.     //   -902 = profile does not exist
  75.     //   -903 = profile already exists (i.e. cannot be created again)
  76.     //   -904 = unknown profile command
  77.     //
  78.     // In some cases, error code -103 may be returned if no data was found.
  79.     
  80.  
  81.     require_once('../config.php');
  82.     require_once(SLOODLE_DIRROOT.'/sl_debug.php');
  83.     require_once(SLOODLE_DIRROOT.'/lib/sl_lsllib.php');
  84.     require_once(SLOODLE_DIRROOT.'/lib/sl_classroomlib.php');
  85.  
  86.     
  87.     sloodle_debug_output('<br/>');
  88.     
  89.     // Create an LSL handler and process the basic request data
  90.     sloodle_debug_output('Creating LSL handler...<br/>');
  91.     $lsl new SloodleLSLHandler();
  92.     sloodle_debug_output('Processing basic request data...<br/>');
  93.     $lsl->request->process_request_data();
  94.     
  95.     // Make sure the course id was specified
  96.     $lsl->request->required_param('sloodlecourseid'PARAM_INT);
  97.     
  98.     // Ensure the request is authenticated
  99.     sloodle_debug_output('Authenticating request...<br/>');
  100.     $lsl->request->authenticate_request();
  101.     // Attempt to login the user
  102.     sloodle_debug_output('Logging-in user...<br/>');
  103.     $lsl->login_by_request();
  104.     
  105.     // Make sure the user is authorized to manage profiles for this course
  106.     sloodle_debug_output('Checking user authority...<br/>');
  107.     if (!(isadmin(|| isteacher($lsl->request->get_course_id()))) {
  108.         // User is not authorized
  109.         sloodle_debug_output('-&gt; ERROR: user not admin or teacher.<br/>');
  110.         $lsl->response->set_status_code(-331);
  111.         $lsl->response->set_status_descriptor('USER_AUTH');
  112.         $lsl->response->add_data_line('User is not authorised to manage Sloodle classroom profiles for this course.');
  113.         $lsl->response->render_to_output();
  114.         exit();
  115.     }    
  116.     
  117.     // Obtain the additional parameters
  118.     sloodle_debug_output('Obtaining additional parameters...<br/>');
  119.     $sloodlecmd strtolower($lsl->request->required_param('sloodlecmd'PARAM_RAW));
  120.     $sloodleprofilename optional_param('sloodleprofilename'NULLPARAM_RAW);
  121.     if ($sloodleprofilename != NULL$sloodleprofilename trim($sloodleprofilename);
  122.     $sloodleentries optional_param('sloodleentries'NULLPARAM_RAW);
  123.         
  124.     
  125.     // Check which mode we're in
  126.     sloodle_debug_output("Command type: '$sloodlecmd'...<br/>");
  127.     switch ($sloodlecmd{
  128.     
  129.     case 'newprofile':
  130.         // We are to create a new profile
  131.         sloodle_debug_output('Creating new profile...<br/>');
  132.         // Make sure the profile name was specified
  133.         $lsl->request->required_param('sloodleprofilename'PARAM_RAW);
  134.         sloodle_debug_output("-&gt; Profile name: '$sloodleprofilename'<br/>");
  135.         
  136.         // Check that the named profile does not already exist for this course
  137.         sloodle_debug_output('Checking if profile already exists...<br/>');
  138.         $profile sloodle_get_classroom_profile_by_name($sloodleprofilename$lsl->request->get_course_id());
  139.         
  140.         if ($profile ===FALSE{
  141.             // Construct a new profile object
  142.             sloodle_debug_output('Constructing new profile object...<br/>');
  143.             $profile new stdClass();
  144.             $profile->name $sloodleprofilename;
  145.             $profile->courseid $lsl->request->get_course_id();
  146.             // Insert it into the database
  147.             sloodle_debug_output('Adding profile to database...<br/>');
  148.             $id sloodle_add_classroom_profile($profile);
  149.             if ($id === FALSE{
  150.                 $lsl->response->set_status_code(-901);
  151.                 $lsl->response->set_status_descriptor('PROFILE');
  152.                 $lsl->response->add_data_line('Failed to add profile to database.');
  153.                 break;
  154.             }
  155.             $lsl->response->set_status_code(1);
  156.         else {
  157.             sloodle_debug_output('-&gt; Profile exists.<br/>');
  158.             $lsl->response->set_status_code(-903);
  159.             $id $profile->id;
  160.         }
  161.         
  162.         // Everything seems fine
  163.         $lsl->response->set_status_descriptor('OK');        
  164.         $lsl->response->add_data_line(array($id$sloodleprofilename));
  165.         break;
  166.         
  167.         
  168.     case 'listprofiles':
  169.         // We are to list the profiles in the current course
  170.         sloodle_debug_output('Fetching list of profiles in course...<br/>');
  171.         $profiles sloodle_get_classroom_profiles($lsl->request->get_course_id());
  172.         // Make sure it was successful
  173.         if (!is_array($profiles)) $profiles array();
  174.         
  175.         // List each one
  176.         sloodle_debug_output('Iterating through all profiles...<br/>');
  177.         foreach ($profiles as $p{
  178.             $lsl->response->add_data_line(array($p->id$p->name));
  179.         }
  180.         
  181.         // Construct the rest of the response
  182.         sloodle_debug_output('Constructing remainder of response...<br/>');
  183.         $lsl->response->set_status_code(1);
  184.         $lsl->response->set_status_descriptor('OK');        
  185.         break;
  186.         
  187.         
  188.     case 'listentries':
  189.         // We are to list all entries in the given profile
  190.         sloodle_debug_output('Listing all entries in profile...<br/>');
  191.         // Make sure the profile name was specified
  192.         $lsl->request->required_param('sloodleprofilename'PARAM_RAW);
  193.         sloodle_debug_output("-&gt; Profile name: '$sloodleprofilename'<br/>");
  194.         
  195.         // Attempt to fetch the named profile
  196.         sloodle_debug_output('Fetching profile...<br/>');
  197.         $profile sloodle_get_classroom_profile_by_name($sloodleprofilename$lsl->request->get_course_id());
  198.         // Make sure it was successful
  199.         if (!is_object($profile)) {
  200.             sloodle_debug_output('-&gt; ERROR: failed to fetch profile<br/>');
  201.             $lsl->response->set_status_code(-103);
  202.             $lsl->response->set_status_descriptor('SYSTEM');
  203.             $lsl->response->add_data_line("Failed to find profile '$sloodleprofilename' in specified course.");
  204.             break;
  205.         }
  206.                
  207.         // Fetch all entries in the profile
  208.         sloodle_debug_output('Fetching all entries in profile...<br/>');
  209.         $entries sloodle_get_classroom_profile_entries($profile->id);
  210.         // Make sure it was successful
  211.         if (!is_array($entries)) $entries array();
  212.         
  213.         // Go through each entry
  214.         sloodle_debug_output('Iterating through list of entries...<br/>');
  215.         foreach ($entries as $e{
  216.             // Add this entry to the response
  217.             $lsl->response->add_data_line(array($e->id$e->name$e->relative_position));
  218.         }
  219.         
  220.         // Construct the rest of the response
  221.         sloodle_debug_output('Constructing remainder of response...<br/>');
  222.         $lsl->response->set_status_code(1);
  223.         $lsl->response->set_status_descriptor('OK');            
  224.         break;
  225.         
  226.         
  227.     case 'saveentries':
  228.         // We are to save a new list of entries in the profile
  229.         sloodle_debug_output('Saving profile entries...<br/>');
  230.         // Make sure the profile name was specified
  231.         $lsl->request->required_param('sloodleprofilename'PARAM_RAW);
  232.         sloodle_debug_output("-&gt; Profile name: '$sloodleprofilename'<br/>");
  233.         // Make sure the entries string was specified
  234.         if ($sloodleentries == NULL || empty($sloodleentries)) {
  235.             sloodle_debug_output('ERROR: entries string not specified.<br/>');
  236.             $lsl->response->set_status_code(-811);
  237.             $lsl->response->set_status_descriptor('REQUEST');
  238.             $lsl->response->add_data_line('Expected parameter \'sloodleentries\' not specified.');
  239.             break;
  240.         }
  241.         
  242.         // Attempt to fetch the named profile
  243.         sloodle_debug_output('Fetching profile...<br/>');
  244.         $profile sloodle_get_classroom_profile_by_name($sloodleprofilename$lsl->request->get_course_id());
  245.         // Make sure it was successful
  246.         if (!is_object($profile)) {
  247.             sloodle_debug_output('-&gt; ERROR: failed to fetch profile<br/>');
  248.             $lsl->response->set_status_code(-103);
  249.             $lsl->response->set_status_descriptor('SYSTEM');
  250.             $lsl->response->add_data_line("Failed to find profile '$sloodleprofilename' in specified course.");
  251.             break;
  252.         }
  253.                
  254.         // Fetch all entries in the profile
  255.         sloodle_debug_output('Fetching all entries in profile...<br/>');
  256.         $raw_entries sloodle_get_classroom_profile_entries($profile->id);
  257.         $entries array();
  258.         // Was it successful?
  259.         if (is_array($raw_entries)) {
  260.             // Yes - convert it to an associative array of name to entry
  261.             sloodle_debug_output('Converting entries array...<br/>');
  262.             foreach ($raw_entries as $e{
  263.                 $entries[$e->name$e;
  264.             }
  265.         else {
  266.             // No - just an empty array
  267.             sloodle_debug_output('-&gt; No entries.<br/>');
  268.         }
  269.         
  270.         // Split the entries list into each individual entry command
  271.         sloodle_debug_output('Processing entries string...<br/>');
  272.         $new_entries_str explode("||"$sloodleentries);
  273.         $new_entries array()// This will store our new entries that we create
  274.         // Go through each one
  275.         foreach ($new_entries_str as $nes{
  276.             // Extract all parts of the entry
  277.             $parts explode("|"$nes);
  278.             // Make sure we have the expected number of parts
  279.             if (count($parts!= 2{
  280.                 sloodle_debug_output("ERROR: incorrect number of parts in entry string '$nes'<br/>");
  281.                 continue;
  282.             }
  283.             
  284.             // Construct a new profile entry object
  285.             $this_entry new stdClass();
  286.             $this_entry->sloodle_classroom_setup_profile_id $profile->id;
  287.             $this_entry->name $parts[0];
  288.             $this_entry->uuid '';
  289.             $this_entry->relative_position $parts[1];
  290.             // Add the entry to the list of entries
  291.             $new_entries[$this_entry;
  292.         }
  293.         
  294.                
  295.         // Save the new entries
  296.         sloodle_debug_output('Saving profile entries...<br/>');
  297.         if (sloodle_save_classroom_profile_entries($profile->id$new_entries)) {
  298.             sloodle_debug_output('-&gt;Success.<br/>');
  299.             $lsl->response->set_status_code(1);
  300.             $lsl->response->set_status_descriptor('OK');
  301.         else {
  302.             sloodle_debug_output('-&gt;Failed.<br/>');
  303.             $lsl->response->set_status_code(-901);
  304.             $lsl->response->set_status_descriptor('PROFILE');
  305.             $lsl->response->add_data_line('Failed to save profile entries.');
  306.         }        
  307.         break;
  308.     
  309.     
  310.     default:
  311.         // Unknown command
  312.         sloodle_debug_output('-&gt; ERROR: unknown command type.<br/>');
  313.         $lsl->response->set_status_code(-904);
  314.         $lsl->response->set_status_descriptor('PROFILE');
  315.         $lsl->response->add_data_line("Unknown profile command: '$sloodleprofilecmd'");
  316.         break;
  317.     }
  318.     
  319.     // Render the output
  320.     sloodle_debug_output('Outputting response...<br/>');
  321.     sloodle_debug_output('<pre>');
  322.     $lsl->response->render_to_output();
  323.     sloodle_debug_output('</pre>');
  324.     
  325.     exit();
  326. ?>

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