Source for file sl_glossary_linker.php

Documentation is available at sl_glossary_linker.php

  1. <?php    
  2.     /**
  3.     * Sloodle glossary linker.
  4.     *
  5.     * Allows in-world 'MetaGloss' tools to query Moodle glossaries.
  6.     *
  7.     * @package sloodleglossary
  8.     * @copyright Copyright (c) 2007-8 Sloodle (various contributors)
  9.     * @license http://www.gnu.org/licenses/gpl-3.0.html GNU GPL v3
  10.     *
  11.     * @contributor Jeremy Kemp
  12.     * @contributor Peter R. Bloomfield
  13.     *
  14.     */
  15.  
  16.     // This script is expected to be accessed from in-world.
  17.     // The following parameters are required:
  18.     //
  19.     //   sloodlepwd = prim password for request authentication
  20.     //   sloodlecourseid = ID of the course this request relates to
  21.     //
  22.     // The following parameters are optional, depending on the mode required:
  23.     //
  24.     //   sloodleconcept = the search string to be looked-up
  25.     //   sloodlemoduleid = course module instance ID of the glossary to be used
  26.     //
  27.     //
  28.     // There are two modes of operation: browse and lookup.
  29.     //
  30.     // In browse mode, neither the sloodleconcept' nor the 'sloodlemoduleid' parameters should be specified.
  31.     // In this case, a list of all available glossarries in the identified course is returned.
  32.     //
  33.     // Look-up mode requires 'sloodleconcept' and 'sloodlemoduleid' to be specified.
  34.     // It will lookup the specified term ('concept') in the specified glossary.
  35.     //
  36.     // NOTE: only available (i.e. visible) glossaries may be searched
  37.     
  38.     // When returning a list of glossaries (browse mode), the status code returned will be 1 if successful.
  39.     // Each line will define a glossary, like this:
  40.     //
  41.     // "id|name"
  42.     //
  43.     // The "id" is the course module instance ID.
  44.     
  45.     // When returning a definition (lookup mode), the status code returned will be 1 if succesful.
  46.     // (Other status codes may be returned if the glossary is not available).
  47.     //
  48.     // Each line of the response will give a definition as raw text:
  49.     //
  50.     // "concept|definition"
  51.     //
  52.     // Note that some glossaries  may contain multiple definitions of a single term.
  53.     // Also note that the search process will find partial matches (e.g. "pri" will find the definition for "prim").
  54.  
  55.     header ('Content-Type: text/plain; charset=UTF-8');
  56.  
  57.     require_once('../../config.php');
  58.     require_once(SLOODLE_DIRROOT.'/sl_debug.php');
  59.     require_once(SLOODLE_DIRROOT.'/lib/sl_lsllib.php');
  60.     require_once($CFG->dirroot.'/mod/glossary/lib.php');
  61.     require_once($CFG->libdir.'/filelib.php');
  62.     require_once('sl_glossary_lib.php');
  63.     
  64.     // Create an LSL handler and process the basic request data
  65.     sloodle_debug_output('Creating LSL handler...<br/>');
  66.     $lsl new SloodleLSLHandler();
  67.     sloodle_debug_output('Processing basic request data...<br/>');
  68.     $lsl->request->process_request_data();
  69.     
  70.     // Ensure the request is authenticated
  71.     sloodle_debug_output('Authenticating request...<br/>');
  72.     $lsl->request->authenticate_request();
  73.     
  74.     // Obtain the additional parameters
  75.     sloodle_debug_output('Obtaining additional parameters...<br/>');
  76.     $sloodleconcept optional_param('sloodleconcept'NULLPARAM_RAW);
  77.     
  78.     
  79.     // Attempt to get the requested course
  80.     sloodle_debug_output('Fetching course data...<br/>');
  81.     $course $lsl->request->get_course_record();
  82.     
  83.     // Was a module ID specified?
  84.     $cmi NULL;
  85.     $glossary NULL;
  86.     if ($lsl->request->get_module_id(!= NULL{
  87.         // Yes - get the module instance
  88.         sloodle_debug_output('Fetching course module instance...<br/>');
  89.         $cmi $lsl->request->get_course_module_instance('glossary');
  90.         // Attempt to fetch the glossary module itself
  91.         sloodle_debug_output('Fetching activity module...<br/>');
  92.         if (!$glossary sloodle_get_glossary_activity_module($cmi)) {
  93.             sloodle_debug_output('-&gt; Failed.<br/>');
  94.             $lsl->response->set_status_code(-101);
  95.             $lsl->response->set_status_descriptor('SYSTEM');
  96.             $lsl->response->add_data_line('Failed to obtain activity module instance.');
  97.             $lsl->render_to_output();
  98.             exit();
  99.         }
  100.     }
  101.     
  102.     //  Make sure that if we have a CMI, we also have a concept, and vice versa
  103.     if ($cmi != NULL$lsl->request->required_param('sloodleconcept'PARAM_RAW);
  104.     else if ($sloodleconcept != NULL$lsl->request->required_param('sloodlemoduleid'PARAM_RAW);
  105.     
  106.     // Which mode are we in?
  107.     sloodle_debug_output('Checking mode...<br/>');
  108.     if ($cmi == NULL{
  109.         // Browse mode
  110.         sloodle_debug_output('***** BROWSE MODE *****<br/>');
  111.         // Fetch a list of all available glossaries in the course
  112.         sloodle_debug_output('Searching for available glossaries in course...<br/>');
  113.         $glossaries sloodle_get_visible_glossaries_in_course($lsl->request->get_course_id());
  114.         // If the search failed, then there are probably no glossaries to select
  115.         if (!is_array($glossaries)) $glossaries array();
  116.         // Construct the response header
  117.         $lsl->response->set_status_code(1);
  118.         $lsl->response->set_status_descriptor('OK');
  119.         
  120.         // Go through each glossary
  121.         sloodle_debug_output('Iterating through all glossaries...<br/>');
  122.         foreach ($glossaries as $id=>$g{
  123.             // Add this glossary to the response
  124.             $lsl->response->add_data_line(array($id$g->name));
  125.         }
  126.         
  127.     else {
  128.         // Lookup mode
  129.         sloodle_debug_output('***** LOOKUP MODE *****<br/>');
  130.         // Lookup the concept in the glossary
  131.         sloodle_debug_output('Searching glossary...<br/>');
  132.         $defs sloodle_lookup_glossary($glossary$sloodleconcept);
  133.         
  134.         // Construct the response header
  135.         $lsl->response->set_status_code(1);
  136.         $lsl->response->set_status_descriptor('OK');
  137.         
  138.         // Did an error occur?
  139.         if (!is_array($defs)) $defs array();
  140.  
  141.         // Go through each entry
  142.         sloodle_debug_output('Iterating through all entries...<br/>');
  143.         foreach ($defs as $d{
  144.             // Make sure the concept is clean
  145.             $cleanconcept clean_text($d->concept);
  146.             // Strip the definition of tags etc.                
  147.             $cleandef $d->definition;
  148.             $cleandef str_replace("\n"" "$cleandef);
  149.             $cleandef str_replace("\r"" "$cleandef);
  150.             $cleandef stripslashes($cleandef);
  151.             $cleandef strip_tags($cleandef);
  152.             // Add this entry to the response
  153.             $lsl->response->add_data_line(array($cleanconcept$cleandef));            
  154.         }
  155.     }    
  156.     
  157.     // Render the output
  158.     sloodle_debug_output('Outputting response...<br/>');
  159.     sloodle_debug_output('<pre>');
  160.     $lsl->response->render_to_output();
  161.     sloodle_debug_output('</pre>');
  162.     
  163.     exit();
  164.     
  165. /////OLD CODE:
  166.  
  167.     //////////////////////////////////////////////////////////////////////////
  168.     // MetaGloss linker script
  169.     // Provides a link for the LSL MetaGloss script in-world to query the Moodle glossary
  170.     // For more information, visit Sloodle.org
  171.     //
  172.     // Original by Jeremy Kemp, San Jose State University
  173.     // Updated by Peter R. Bloomfield, University of Paisley, October 2007
  174.     //
  175.     // Script expects 2 parameters (via GET or POST).
  176.     // Parameter "concept" is necessary, and defines the search string to be looked-up in the glossary.
  177.     // Either parameter "glossaryid" OR "courseid" must also be specified.
  178.     // The "glossaryid" is the ID number of the glossary to be searched.
  179.     // If that is not specified, but "courseid" is given, then all glossaries in that course are searched.
  180.     // All results are returned as plain text.
  181.     //////////////////////////////////////////////////////////////////////////    
  182.  
  183.     require_once('../../../../config.php');
  184.     require_once($CFG->dirroot .'/mod/glossary/lib.php');
  185.     require_once($CFG->libdir.'/filelib.php');
  186.     
  187.     // Get the necessary parameters
  188.     $courseid optional_param('courseid'NULLPARAM_INT);
  189.     $concept optional_param('concept'NULLPARAM_RAW);
  190.     $glossaryid optional_param('glossaryid'NULLPARAM_INT);
  191.     
  192.     // Initialise a results variable
  193.     $result NULL;
  194.     
  195.     // We *must* have a concept (search term) parameter
  196.     if ($concept === NULLexit("ERROR: no glossary search term specified");
  197.     
  198.     // We either need a glossary ID or a course ID -- if glossary ID is specified, then search that glossary only and ignore the course ID
  199.     // If only the course ID is specified, then search all glossaries in the course
  200.     if (is_null($glossaryid== FALSE{
  201.        // Attempt to find the glossary
  202.        $glossary get_record("glossary""id"$glossaryid);
  203.        if ($glossary === FALSEexit("ERROR: unrecognised glossary ID.");
  204.        
  205.        // Search the glossary
  206.        echo "Searching glossary \"$glossary->name\" for term \"$concept\". ";
  207.        $result glossary_search_entries(array($concept)$glossary0);
  208.     
  209.     else {
  210.     
  211.         // Make sure the course ID was specified
  212.         if ($courseid === NULLexit("ERROR: no glossary or course specified");
  213.         // Get the course object
  214.         if ($course get_record("course""id"$courseid)) {
  215.             exit("ERROR: unrecognised course ID.");
  216.         }
  217.     
  218.         // Search all glossaries in the course
  219.         echo "Searching all glossaries in \"$course->shortname\" for term \"$concept\". ";
  220.         $result glossary_search($coursearray($concept));
  221.     }    
  222.     
  223.     // Check that we have some results
  224.     if (is_array($result&& ($numresults count($result)) 0{
  225.         if ($numresults == 1echo " 1 result found:";
  226.         else echo "$numresults results found:";
  227.         // Display each result
  228.         foreach ($result as $entry{
  229.             // Not very efficient, but look up the name of the glossary each result is from
  230.             $curgloss get_record("glossary""id"$entry->glossaryid);
  231.             echo "\n\n\"$entry->concept\" ($curgloss->name): $entry->definition";
  232.         }
  233.     else {
  234.         echo "No matching entries found for search term \"$concept\".";
  235.     }
  236.     
  237. ?>

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