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 sloodle
  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.     * Update mode will add or replace entries in a layout.
  37.     * To activate this mode, "sloodlelayoutname" must be specified, as well as the following parameter:
  38.     *  sloodlelayoutentries = a pipe and line-separated list of entries to save against the profile
  39.     * The following parameter can also be specified to alter the behaviour (defaults to 'false' if not specified):
  40.     *  sloodleadd = if 'true', then the given entries are added to the layout instead of replacing them
  41.     * 
  42.     *
  43.     * The format for the "sloodlelayoutentries" parameter should be the same as the response from the query mode.
  44.     * Due to the potentially large quantity of information this type of query may generate, it is advisable to use POST instead of GET.
  45.     */
  46.     
  47.     /** Lets Sloodle know we are in a linker script. */
  48.     define('SLOODLE_LINKER_SCRIPT'true);
  49.     
  50.     /** Grab the Sloodle/Moodle configuration. */
  51.     require_once('../../sl_config.php');
  52.     /** Include the Sloodle PHP API. */
  53.     require_once(SLOODLE_LIBROOT.'/sloodle_session.php');
  54.     
  55.     // Authenticate the request and user, but do not allow auto-registration and enrolment
  56.     $sloodle new SloodleSession();
  57.     $sloodle->authenticate_request();
  58.     $sloodle->validate_user(truetruetrue);
  59.     
  60.     // We need to check certain capabilities
  61.     $can_use_layouts false;
  62.     $can_edit_layouts false;
  63.     
  64. ///// MOODLE-SPECIFIC /////
  65.     $course_context get_context_instance(CONTEXT_COURSE$sloodle->course->get_course_id());
  66.     $can_use_layouts has_capability('mod/sloodle:uselayouts'$course_context);
  67.     $can_edit_layouts has_capability('mod/sloodle:editlayouts'$course_context);
  68. ///// END MOODLE-SPECIFIC /////
  69.  
  70.     // If the user cannot use layouts at all, then we cannot do anything
  71.     if (!$can_use_layouts{
  72.         $sloodle->response->quick_output(-301'USER_AUTH''User does not have permission to use layout profiles.'false);
  73.         exit();
  74.     }
  75.     
  76.     // Get the optional parameters
  77.     $sloodlelayoutname $sloodle->request->optional_param('sloodlelayoutname');
  78.     $sloodlelayoutentries $sloodle->request->optional_param('sloodlelayoutentries');
  79.     $sloodleadd $sloodle->request->optional_param('sloodleadd''false');
  80.     if (strcasecmp($sloodleadd'true'== || $sloodleadd == '1'$sloodleadd true;
  81.     else $sloodleadd false;
  82.     // Determine which mode we're in (0 = browse, 1 = query, 2 = update)
  83.     $mode 0;
  84.     if ($sloodlelayoutname == null$mode 0;
  85.     else if ($sloodlelayoutentries == null$mode 1;
  86.     else $mode 2;
  87.     
  88.     
  89.     // Enter the appropriate mode
  90.     switch ($mode{
  91.     case 0:
  92.         // BROWSE MODE //
  93.         // Fetch the layouts in this course
  94.         $layouts $sloodle->course->get_layout_names();
  95.         // Add one data line per layout
  96.         $sloodle->response->set_status_code(1);
  97.         $sloodle->response->set_status_descriptor('OK');
  98.         foreach ($layouts as $id => $name{
  99.             $sloodle->response->add_data_line(array($id$name));
  100.         }
  101.         
  102.         break;
  103.         
  104.     case 1:
  105.         // QUERY MODE //
  106.         // Attempt to load the specified profile
  107.         $layout_entries $sloodle->course->get_layout_entries($sloodlelayoutname);
  108.         if ($layout_entries === false{
  109.             // Profile not found
  110.             $sloodle->response->set_status_code(-902);
  111.             $sloodle->response->set_status_descriptor('LAYOUT');
  112.             $sloodle->response->add_data_line('Named profile does not exist');
  113.         else {
  114.             // Output one entry per line
  115.             $sloodle->response->set_status_code(1);
  116.             $sloodle->response->set_status_descriptor('OK');
  117.             foreach ($layout_entries as $le{
  118.                 $sloodle->response->add_data_line(array($le->name$le->position$le->rotation));
  119.             }
  120.         }
  121.         
  122.         break;
  123.         
  124.     case 2:
  125.         // UPDATE MODE //
  126.         // Make sure the user has permission to edit profiles
  127.         if ($can_edit_layouts{
  128.         else {
  129.             $sloodle->response->set_status_code(-301);
  130.             $sloodle->response->set_status_descriptor('LAYOUT_PROFILE');
  131.             $sloodle->response->add_data_line('User does not have permission to edit layout profiles');
  132.         }
  133.         
  134.         // This array will store the new entries for our layout
  135.         $entries array();
  136.         // Go through each line of incoming data
  137.         $lines explode("\n"$sloodlelayoutentries);
  138.         foreach ($lines as $l{
  139.             // Split the data into separate fields, and check that we have enough in this entry
  140.             $fields explode("|"$l);
  141.             if (count($fields3continue;
  142.             // Construct an entry object
  143.             $entryobj new SloodleLayoutEntry();
  144.             $entryobj->name $fields[0];
  145.             $entryobj->position $fields[1];
  146.             $entryobj->rotation $fields[2];
  147.             $entries[$entryobj;
  148.         }
  149.         
  150.         // Udpate the layout
  151.         if ($sloodle->course->save_layout($sloodlelayoutname$entries$sloodleadd)) {
  152.             $sloodle->response->set_status_code(1);
  153.             $sloodle->response->set_status_descriptor('OK');
  154.         else {
  155.             $sloodle->response->set_status_code(-901);
  156.             $sloodle->response->set_status_descriptor('LAYOUT_PROFILE');
  157.             $sloodle->response->add_data_line('Failed to save new layout');
  158.         }
  159.         
  160.         break;
  161.         
  162.     default:
  163.         // Unknown mode
  164.         $sloodle->response->set_status_code(-904);
  165.         $sloodle->response->set_status_descriptor('LAYOUT_PROFILE');
  166.         $sloodle->response->add_data_line('Error determining layout operation');
  167.         break;
  168.     }
  169.     
  170.     
  171.     // Render our output
  172.     sloodle_debug('<pre>')// <- to help visualising output in a browser when debugging
  173.     $sloodle->response->render_to_output();
  174.     sloodle_debug('</pre>');
  175.     
  176. ?>

Documentation generated on Mon, 16 Jun 2008 15:56:27 +0100 by phpDocumentor 1.4.0