Source for file layout_linker.php

Documentation is available at layout_linker.php

  1. <?php
  2.     // This script is part of the Sloodle project
  3.  
  4.     /**
  5.     * Layout profile linker script.
  6.     * Allows the use and management of layout profiles from within SL.
  7.     * See comments in source file for further information.
  8.     *
  9.     * @package sloodleset
  10.     * @copyright Copyright (c) 2008 Sloodle (various contributors)
  11.     * @license http://www.gnu.org/licenses/gpl-3.0.html GNU GPL v3
  12.     *
  13.     * @contributor Peter R. Bloomfield
  14.     *
  15.     */
  16.     
  17.     /*
  18.     * This script should always be called with the following parameters:
  19.     *  sloodlecontrollerid = ID of a Sloodle Controller through which to access Moodle
  20.     *  sloodlepwd = the prim password or object-specific session key to authenticate the request
  21.     *  sloodleuuid = the UUID of a user agent making the layout profile request
  22.     *  sloodleavname = the name of an avatar identified by sloodleuuid
  23.     *
  24.     * There are 3 modes of operation: browse, query, update
  25.     * If called with no parameters other than the above, then it adopts browse mode.
  26.     * This mode will return a list of all layout profiles in the current course (whichever course the sloodlecontrollerid is part of).
  27.     * If successful, the stats code will be 1, and each data line will identify a single layout by name.
  28.     *
  29.     * Query mode will fetch all entries pertaining to a specific layout.
  30.     * To activate this mode the following additional parameter must be specified:
  31.     *  sloodlelayoutname = the name of a layout profile
  32.     *
  33.     * If query mode is succesful, it will return status code 1, with one entry per data line, as follows:
  34.     *  name|position|rotation
  35.     *
  36.     * The rotation should be a rotation cast to a string... NOT an Euler-angle vector!
  37.     *
  38.     * Update mode will add or replace entries in a layout.
  39.     * To activate this mode, "sloodlelayoutname" must be specified, as well as the following parameter:
  40.     *  sloodlelayoutentries = a pipe and line-separated list of entries to save against the profile
  41.     * The following parameter can also be specified to alter the behaviour (defaults to 'false' if not specified):
  42.     *  sloodleadd = if 'true', then the given entries are added to the layout instead of replacing them
  43.     * 
  44.     *
  45.     * The format for the "sloodlelayoutentries" parameter should be the same as the response from the query mode.
  46.     * Due to the potentially large quantity of information this type of query may generate, it is advisable to use POST instead of GET.
  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.     // Authenticate the request and user, but do not allow auto-registration and enrolment
  58.     $sloodle new SloodleSession();
  59.     $sloodle->authenticate_request();
  60.     $sloodle->validate_user(truetruetrue);
  61.     
  62.     // We need to check certain capabilities
  63.     $can_use_layouts false;
  64.     $can_edit_layouts false;
  65.     
  66. ///// MOODLE-SPECIFIC /////
  67.     $course_context get_context_instance(CONTEXT_COURSE$sloodle->course->get_course_id());
  68.     $can_use_layouts has_capability('mod/sloodle:uselayouts'$course_context);
  69.     $can_edit_layouts has_capability('mod/sloodle:editlayouts'$course_context);
  70. ///// END MOODLE-SPECIFIC /////
  71.  
  72.     // If the user cannot use layouts at all, then we cannot do anything
  73.     if (!$can_use_layouts{
  74.         $sloodle->response->quick_output(-301'USER_AUTH''User does not have permission to use layout profiles.'false);
  75.         exit();
  76.     }
  77.     
  78.     // Get the optional parameters
  79.     $sloodlelayoutname $sloodle->request->optional_param('sloodlelayoutname');
  80.     $sloodlelayoutentries $sloodle->request->optional_param('sloodlelayoutentries');
  81.     $sloodleadd $sloodle->request->optional_param('sloodleadd''false');
  82.     if (strcasecmp($sloodleadd'true'== || $sloodleadd == '1'$sloodleadd true;
  83.     else $sloodleadd false;
  84.     // Determine which mode we're in (0 = browse, 1 = query, 2 = update)
  85.     $mode 0;
  86.     if ($sloodlelayoutname === null$mode 0;
  87.     else if ($sloodlelayoutentries === null$mode 1;
  88.     else $mode 2;
  89.     
  90.     
  91.     // Enter the appropriate mode
  92.     switch ($mode{
  93.     case 0:
  94.         // BROWSE MODE //
  95.         // Fetch the layouts in this course
  96.         $layouts $sloodle->course->get_layout_names();
  97.         // Add one data line per layout
  98.         $sloodle->response->set_status_code(1);
  99.         $sloodle->response->set_status_descriptor('OK');
  100.         foreach ($layouts as $id => $name{
  101.             $sloodle->response->add_data_line($name);
  102.         }
  103.         
  104.         break;
  105.         
  106.     case 1:
  107.         // QUERY MODE //
  108.         // Attempt to load the specified profile
  109.         $layout_entries $sloodle->course->get_layout_entries($sloodlelayoutname);
  110.         if ($layout_entries === false{
  111.             // Profile not found
  112.             $sloodle->response->set_status_code(-902);
  113.             $sloodle->response->set_status_descriptor('LAYOUT');
  114.             $sloodle->response->add_data_line('Named profile does not exist');
  115.         else {
  116.             // Output one entry per line
  117.             $sloodle->response->set_status_code(1);
  118.             $sloodle->response->set_status_descriptor('OK');
  119.             foreach ($layout_entries as $le{
  120.                 $sloodle->response->add_data_line(array($le->name$le->position$le->rotation));
  121.             }
  122.         }
  123.         
  124.         break;
  125.         
  126.     case 2:
  127.         // UPDATE MODE //
  128.         // Make sure the user has permission to edit profiles
  129.         if ($can_edit_layouts{
  130.         else {
  131.             $sloodle->response->set_status_code(-301);
  132.             $sloodle->response->set_status_descriptor('LAYOUT_PROFILE');
  133.             $sloodle->response->add_data_line('User does not have permission to edit layout profiles');
  134.         }
  135.         
  136.         // This array will store the new entries for our layout
  137.         $entries array();
  138.         // Go through each line of incoming data
  139.         $lines explode("\n"$sloodlelayoutentries);
  140.         foreach ($lines as $l{
  141.             // Split the data into separate fields, and check that we have enough in this entry
  142.             $fields explode("|"$l);
  143.             if (count($fields3continue;
  144.             // Construct an entry object
  145.             $entryobj new SloodleLayoutEntry();
  146.             $entryobj->name $fields[0];
  147.             $entryobj->position $fields[1];
  148.             $entryobj->rotation $fields[2];
  149.             $entries[$entryobj;
  150.         }
  151.         
  152.         // Udpate the layout
  153.         if ($sloodle->course->save_layout($sloodlelayoutname$entries$sloodleadd)) {
  154.             $sloodle->response->set_status_code(1);
  155.             $sloodle->response->set_status_descriptor('OK');
  156.         else {
  157.             $sloodle->response->set_status_code(-901);
  158.             $sloodle->response->set_status_descriptor('LAYOUT_PROFILE');
  159.             $sloodle->response->add_data_line('Failed to save new layout');
  160.         }
  161.         
  162.         break;
  163.         
  164.     default:
  165.         // Unknown mode
  166.         $sloodle->response->set_status_code(-904);
  167.         $sloodle->response->set_status_descriptor('LAYOUT_PROFILE');
  168.         $sloodle->response->add_data_line('Error determining layout operation');
  169.         break;
  170.     }
  171.     
  172.     
  173.     // Render our output
  174.     sloodle_debug('<pre>')// <- to help visualising output in a browser when debugging
  175.     $sloodle->response->render_to_output();
  176.     sloodle_debug('</pre>');
  177.     
  178. ?>

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