Source for file linker.php

Documentation is available at linker.php

  1. <?php
  2.     /**
  3.     * Sloodle choice linker (for Sloodle 0.3).
  4.     * Allows an SL object to access a Moodle choice instance.
  5.     *
  6.     * @package sloodlechoice
  7.     * @copyright Copyright (c) 2007-8 Sloodle (various contributors)
  8.     * @license http://www.gnu.org/licenses/gpl-3.0.html GNU GPL v3
  9.     *
  10.     * @contributor Peter R. Bloomfield
  11.     */
  12.     
  13.     // This script should be called with the following parameters:
  14.     //  sloodlecontrollerid = ID of a Sloodle Controller through which to access Moodle
  15.     //  sloodlepwd = the prim password or object-specific session key to authenticate the request
  16.     //  sloodlemoduleid = ID of a choice
  17.     //
  18.     // If called with only the above parameters, then summary information about the choice is fetched.
  19.     // Status code 10001 will be returned, with the first few data lines having the following format:
  20.     //   <choice_name>
  21.     //   <choice_text>
  22.     //   <is_available>|<timestamp_open>|<timestamp_close>
  23.     //   <num_unanswered>
  24.     //
  25.     // Following that will be one line for each available option, with the following format:
  26.     //   <option_id>|<option_text>|<num_selected>
  27.     //  
  28.     // The "num_unanswered" and "num_selected" values will be -1 if they are not allowed to be shown.
  29.     // The "is_available" will be 1 if the choice is open and accepting answers, or 0 otherwise.
  30.     // The timestamp values indicate when the choice opens and closes respectively, but will be 0 if there is no opening or closing time.
  31.     //
  32.     //
  33.     // An option can be selected by including the following parameters
  34.     //  sloodleuuid = UUID of the avatar
  35.     //  sloodleavname = name of the avatar
  36.     //  sloodleoptionid = the ID of a particular option (unique site-wide)
  37.     //
  38.     // If successful, the return code will be 10011, 10012, or 10013, depending what has been done. No data.
  39.     // See the status codes list for further information.
  40.     //
  41.     
  42.  
  43.     /** Lets Sloodle know we are in a linker script. */
  44.     define('SLOODLE_LINKER_SCRIPT'true);
  45.     
  46.     /** Grab the Sloodle/Moodle configuration. */
  47.     require_once('../../sl_config.php');
  48.     /** Include the Sloodle PHP API. */
  49.     require_once(SLOODLE_LIBROOT.'/sloodle_session.php');
  50.  
  51.     ///// HORRIBLE HACK! /////
  52.     // I've copied this function from Moodle 1.9.
  53.     // Adding it here makes it work for Moodle 1.8 too.
  54.  
  55.     if (!function_exists('choice_get_response_data')) {
  56. function choice_get_response_data($choice$cm$groupmode{
  57.     global $CFG$USER;
  58.  
  59.     $context get_context_instance(CONTEXT_MODULE$cm->id);
  60.  
  61. /// Get the current group
  62.     if ($groupmode 0{
  63.         $currentgroup groups_get_activity_group($cm);
  64.     else {
  65.         $currentgroup 0;
  66.     }
  67.  
  68. /// Initialise the returned array, which is a matrix:  $allresponses[responseid][userid] = responseobject
  69.     $allresponses array();
  70.  
  71. /// First get all the users who have access here
  72. /// To start with we assume they are all "unanswered" then move them later
  73.     $allresponses[0get_users_by_capability($context'mod/choice:choose''u.id, u.picture, u.firstname, u.lastname, u.idnumber''u.firstname ASC'''''$currentgroup''falsetrue);
  74.  
  75. /// Get all the recorded responses for this choice
  76.     $rawresponses get_records('choice_answers''choiceid'$choice->id);
  77.  
  78. /// Use the responses to move users into the correct column
  79.  
  80.     if ($rawresponses{
  81.         foreach ($rawresponses as $response{
  82.             if (isset($allresponses[0][$response->userid])) {   // This person is enrolled and in correct group
  83.                 $allresponses[0][$response->userid]->timemodified $response->timemodified;
  84.                 $allresponses[$response->optionid][$response->useridclone($allresponses[0][$response->userid]);
  85.                 unset($allresponses[0][$response->userid]);   // Remove from unanswered column
  86.             }
  87.         }
  88.     }
  89.  
  90.     return $allresponses;
  91.  
  92. }
  93. }
  94.  
  95.     ///// END HORRIBLE HACK! /////
  96.     
  97.     // Authenticate the request, and load a choice module
  98.     $sloodle new SloodleSession();
  99.     $sloodle->authenticate_request();
  100.     $sloodle->load_module('choice'true);
  101.  
  102.     // Has an option been specified?
  103.     $sloodleoptionid $sloodle->request->optional_param('sloodleoptionid');
  104.     if ($sloodleoptionid === null{
  105.         // No - we are simply querying for choice data
  106.         $sloodle->response->set_status_code(10001);
  107.         $sloodle->response->set_status_descriptor('CHOICE_QUERY');
  108.         
  109.         // Check availability and results
  110.         $isavailable '0';
  111.         if ($sloodle->module->is_open()) $isavailable '1';
  112.         $canshowresults $sloodle->module->can_show_results();
  113.  
  114.         // Fetch the intro (question) text, but cut everything after the first line
  115.         $qtext trim($sloodle->module->get_intro());
  116.         $qlines explode("<br />"$qtext);
  117.         if (is_array($qlines&& count($qlines0$qtext stripslashes(strip_tags($qlines[0]));
  118.         else $qtext '';
  119.         // Whatever happens, make sure the question text isn't too long
  120.         $maxqtextlen 128;
  121.         if (strlen($qtext$maxqtextlen{
  122.             $qlines str_split($qtext$maxqtextlen);
  123.             if (is_array($qlines&& count($qlines0$qtext trim($qlines[0]).'...';
  124.         }
  125.         
  126.         // Add the data to the response
  127.         $sloodle->response->add_data_line($sloodle->module->get_name());
  128.         $sloodle->response->add_data_line($qtext);
  129.         $sloodle->response->add_data_line(array($isavailable$sloodle->module->get_opening_time()$sloodle->module->get_closing_time()));
  130.         if ($canshowresults$sloodle->response->add_data_line($sloodle->module->get_num_unanswered());
  131.         else $sloodle->response->add_data_line('-1');
  132.         // Go through each option
  133.         foreach ($sloodle->module->options as $optionid => $option{
  134.             // Prepare a data array for this option
  135.             $optiondata array();
  136.             $optiondata[$optionid;
  137.             $optiondata[stripslashes(strip_tags($option->text));
  138.             if ($canshowresults$optiondata[$option->numselections;
  139.             else $optiondata[= -1;
  140.             
  141.             $sloodle->response->add_data_line($optiondata);
  142.         }
  143.         
  144.     else {
  145.         // Yes - validate the user, and permit auto-registration/enrolment
  146.         $sloodle->validate_user();
  147.         $sloodle->response->set_status_descriptor('CHOICE_SELECT');
  148.         
  149.         // Attempt to select the option
  150.         $result $sloodle->module->select_option($sloodleoptionid);
  151.         if (!$result{
  152.             $sloodle->response->set_status_code(-10016);
  153.             $sloodle->response->add_data_line('Unknown error selecting option.');
  154.         else {
  155.             $sloodle->response->set_status_code($result);
  156.         }
  157.     }
  158.     
  159.     // Output our response
  160.     $sloodle->response->render_to_output();
  161.     
  162. ?>

Documentation generated on Fri, 17 Jul 2009 11:01:37 +0100 by phpDocumentor 1.4.0