linker.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. <?php
  2. /**
  3. * Sloodle demo linker (for Sloodle 0.4).
  4. * Allows a demo object to link to Moodle.
  5. *
  6. * @package sloodle
  7. * @copyright Copyright (c) 2009 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. // Common parameters for linker scripts
  13. // sloodlecontrollerid = ID of a Sloodle Controller through which to access Moodle
  14. // sloodlepwd = the prim password or object-specific session key to authenticate the request
  15. // sloodlemoduleid = ID of a module to load (corresponds to a course module instance ID, i.e. the 'id' field of the 'course_modules' db table).
  16. // sloodleuuid = UUID of the avatar
  17. // sloodleavname = name of the avatar
  18. //
  19. // Less common parameters:
  20. // sloodleserveraccesslevel = how strict should access be? Handled entirely by the SloodleSession object
  21. //
  22. //
  23. // The following parameter is optional:
  24. // sloodledebug = if 'true', then Sloodle debugging mode is activated -- should only be used from a web-browser as it outputs lots of extra info
  25. /** Lets Sloodle know we are in a linker script. */
  26. define('SLOODLE_LINKER_SCRIPT', true);
  27. /** Grab the Sloodle/Moodle configuration. */
  28. require_once('../../init.php');
  29. /** Include the Sloodle PHP API. */
  30. require_once(SLOODLE_LIBROOT.'/sloodle_session.php');
  31. ///// SETTING UP THE SESSION //////
  32. // Construct a SloodleSession to handle this request.
  33. // This provides a framework to handle all the common SLOODLE-related things.
  34. // By default, this immediately processes important incoming data, but you can suppress that with optional parameters. (See API documentation.)
  35. $sloodle = new SloodleSession();
  36. // Check that the object is allowed to access Moodle.
  37. // This checks that a SLOODLE Controller has been identified, and that a password has been given too.
  38. // If it fails, it will normally terminate the script with an error, but you can suppress that using optional parameters.
  39. $sloodle->authenticate_request();
  40. // At this stage, you are guaranteed that the request is secure... or at least as secure as we can make it. :-)
  41. // Note:
  42. // There are two types of authentication - prim password, and object-specific password.
  43. // In both cases, a SLOODLE Controller must be identified in the "sloodlecontrollerid" HTTP parameter, and the password in the "sloodlepwd" HTTP parameter.
  44. // A prim-password is just a 5 to 9 digit number, and it is the same for any object using the Controller.
  45. // An object-specific password is a 9-digit number PLUS the object's UUID, and it is unique for every object.
  46. // * See the SLOODLE PHP API documentation for more information about the "SloodleSession" class *
  47. ///// HANDLING THE USER /////
  48. // Many requests either deal with or are originated by a particular avatar.
  49. // For example, the WebIntercom needs to know who is chatting a message.
  50. // This information is passed-in using two parameters: sloodleavname, sloodleuuid.
  51. // It is good practice to provide both in your script, but just one will usually suffice (unless you want to auto-register a new avatar).
  52. // You can use the following method to check that an avatar has been specified.
  53. // This will also check to see if they are registered in Moodle, and enrolled on the course.
  54. // If it fails, it will normally terminate the script with an error, but you can suppress that using optional parameters.
  55. // Note that this function will auto-register and/or auto-enrol avatars if necessary and if the Moodle site/course allow it.
  56. // An important side-effect of this function is that it forces Moodle to think the registered Moodle user is actually logged-in for the duration of this script.
  57. $sloodle->validate_user();
  58. // At this stage, you are guaranteed to have a valid avatar AND Moodle user.
  59. // You can now access the user information like this: $sloodle->user->...
  60. // For example, let's get the names of the avatar and Moodle user.
  61. $avatarname = $sloodle->user->get_avatar_name();
  62. $moodlename = $sloodle->user->get_user_firstname() .' '. $sloodle->user->get_user_lastname();
  63. // * See the SLOODLE PHP API documentation for more information about the SloodleUser class. *
  64. // SloodleUser is defined in: sloodle/lib/user.php
  65. ///// LOADING MODULES /////
  66. // In order to improve portability and code-reusability, a modules system was created.
  67. // It is not essential, but can help keep linker scripts clean and free of Moodle-specific database code.
  68. // The SloodleModule base class provides a common base for any SLOODLE code which 'wraps' a Moodle module.
  69. // For example, the SloodleModuleChat class provides simple functions to handle a Moodle chatroom.
  70. // Similarly, the SloodleModuleBlog class provides simple functions to handle the Moodle blog.
  71. // Note that these will NOT necessarily correspond to actual Moodle modules (although they usually do).
  72. // The following method can be used to load a module wrapper for a chatroom.
  73. // The first argument names the module, and the second indicates whether or not there is database data to be loaded.
  74. // Note that this will load the module identified by HTTP parameter 'sloodlemoduleid'.
  75. // If that parameter is not provided in the request, then this method will terminate the script with an error message.
  76. // If the module loading fails (e.g. due to a database error) then the script will also be terminated, although that behaviour can be suppressed by addition optional arguments.
  77. $sloodle->load_module('chat', true);
  78. // You can now access the module like this: $sloodle->module->...
  79. // There are some common functions, such as getting the name of the module, and various functions specific to the module class that was loaded.
  80. $modulename = $sloodle->module->get_name();
  81. $chatmessages = $sloodle->module->get_chat_history();
  82. // * See the SLOODLE PHP API documentation for more information about the "SloodleModule" class *
  83. // SloodleModule is defined in: sloodle/lib/modules/module_base.php
  84. // The sub-classes are defined in: sloodle/lib/modules
  85. // The module loading code is define in: sloodle/lib/modules.php
  86. ///// GETTING INPUT /////
  87. // You will often want to pass data from SL to Moodle as HTTP parameters. This can be done using GET or POST parameters.
  88. // You can fetch either type of data easily using the "SloodleRequest" object within the SloodleSession.
  89. // You access it using: $sloodle->request->...
  90. // If there is a parameter called 'message' that you absolutely MUST receive for the script to work properly, you can fetch it like this:
  91. $requiredmessage = $sloodle->request->required_param('message');
  92. // If the parameter was not given in the HTTP request, then the script is immediately terminate with an error message.
  93. // The argument gives the name of the parameter to fetch.
  94. // If the parameter was specified, then it is returned as a raw string (you MUST do proper data cleanup before putting it into a database query, otherwise you'll be vulnerable to SQL injections).
  95. // If a particular parameter is optional (i.e. you may or may not receive it), then you can use the following method:
  96. $optionalmessage = $sloodle->request->optional_param('anothermessage', '');
  97. // The first argument gave the name of the paramter.
  98. // The second argument gave the default value that would be returned if that parameter was not passed to this script.
  99. // If the parameter *was* provided for this script, then the raw string value is returned.
  100. // * See the SLOODLE PHP API documentation for more information about the "SloodleRequest" class *
  101. // SloodleRequest is defined in: sloodle/lib/io.php
  102. ///// RETURNING A RESPONSE /////
  103. // After you have done some processing, you have to provide a response.
  104. // There is information in the developer documentation on the SLOODLE wiki about the appropriate communications specification.
  105. // Note that *anything* you output, e.g. using "echo" or "print" becomes part of the response.
  106. // The SloodleResponse object within SloodleSession provides some assistance.
  107. // You can access the response object like this: $sloodle->response->...
  108. // You must always provide a status code (see the wiki for a list of these):
  109. // http://slisweb.sjsu.edu/sl/index.php/Sloodle_status_codes
  110. // If you need to create your own status codes, pick a range of unused numbers.
  111. // Document the status codes on the wiki before you start using them in your script.
  112. $sloodle->response->set_status_code(1); // Positive means OK, negative means an error
  113. // You must also provide a simple, generic, status 'descriptor' (a short human-readable string):
  114. $sloodle->response->set_status_descriptor('OK');
  115. // You can optionally add data, line by line. Each line can have several fields, usually separated by | characters.
  116. // There are several ways to add the data.
  117. $sloodle->response->add_data_line("Avatar name is: {$avatarname}"); // Just add a string on each line
  118. $sloodle->response->add_data_line(array('Moodle name', $moodlename)); // Add several fields on a single line
  119. $sloodle->response->add_data_line(array('module', 'name', $modulename));
  120. // Just some more usage of input data
  121. $sloodle->response->add_data_line("Required message: {$requiredmessage}");
  122. if (empty($optionalmessage)) $sloodle->response->add_data_line("No optional message provided");
  123. else $sloodle->response->add_data_line("Optional message: {$optionalmessage}");
  124. // Finally, you MUST remember to render the response!
  125. $sloodle->response->render_to_output();
  126. // Alternatively, you can render the output to a string, like this:
  127. $output = '';
  128. //$sloodle->response->render_to_output($output); // Passed in by reference
  129. // You can then output that string yourself, or send it by XMLRPC or similar.
  130. // NOTE: for XMLRPC into SL, you need to replace newlines (\n) with \\n before sending.
  131. // * See the SLOODLE PHP API documentation for more information about the "SloodleResponse" class *
  132. // SloodleResponse is defined in: sloodle/lib/io.php
  133. ?>