linker.php 3.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. <?php
  2. /**
  3. * Sloodle chat linker (for Sloodle 0.3).
  4. * Allows a Sloodle WebIntercom tool link into a Moodle chatroom.
  5. * Fetches a recent chat history, and optionally inserts a new message.
  6. *
  7. * @package sloodlechat
  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. * @todo Implement capabilities to make sure users can write to the chatroom (will need special handling to let guest users be permitted if desired)
  12. *
  13. * @contributor (various)
  14. * @contributor Peter R. Bloomfield
  15. */
  16. // This script should be called with the following parameters:
  17. // sloodlecontrollerid = ID of a Sloodle Controller through which to access Moodle
  18. // sloodlepwd = the prim password or object-specific session key to authenticate the request
  19. // sloodlemoduleid = ID of a chatroom
  20. //
  21. // If adding a new message, the following parameters should be provided:
  22. // sloodleuuid = UUID of the avatar
  23. // sloodleavname = name of the avatar
  24. // message = the body of the message
  25. //
  26. // The following parameter is optional:
  27. // sloodledebug = if 'true', then Sloodle debugging mode is activated
  28. /** Lets Sloodle know we are in a linker script. */
  29. define('SLOODLE_LINKER_SCRIPT', true);
  30. /** Grab the Sloodle/Moodle configuration. */
  31. require_once('../../init.php');
  32. /** Include the Sloodle PHP API. */
  33. require_once(SLOODLE_LIBROOT.'/sloodle_session.php');
  34. // Authenticate the request, and load a chat module
  35. $sloodle = new SloodleSession();
  36. $sloodle->authenticate_request();
  37. $sloodle->load_module('chat', true);
  38. // Attempt to validate the user
  39. // (this will auto-register/enrol users where necessary and allowed)
  40. // If server access level is public, then validation is not essential... otherwise, it is
  41. $sloodleserveraccesslevel = $sloodle->request->get_server_access_level(false);
  42. if ($sloodleserveraccesslevel == 0) $sloodle->validate_user(false);
  43. else $sloodle->validate_user(true);
  44. $sloodle->validate_requirements();
  45. // Has an incoming message been provided?
  46. $message = sloodle_clean_for_db($sloodle->request->optional_param('message', null));
  47. if ($message != null) {
  48. // Add it to the chatroom - if it fails add a negative side effect code to our response.
  49. // The positive side effect will be added by the function if successful.
  50. if (!$sloodle->module->add_message($message)) {
  51. $sloodle->response->add_side_effect(-10101);
  52. add_to_log($sloodle->course->get_course_id(), 'sloodle', 'add message', '', 'Failed to add chat message to chatroom', $sloodle->request->get_module_id());
  53. } else {
  54. add_to_log($sloodle->course->get_course_id(), 'sloodle', 'add message', '', 'Added chat message to chatroom', $sloodle->request->get_module_id());
  55. }
  56. }
  57. // Start preparing the response
  58. $sloodle->response->set_status_code(1);
  59. $sloodle->response->set_status_descriptor('OK');
  60. // Fetch a chat history
  61. $messages = $sloodle->module->get_chat_history();
  62. foreach ($messages as $m) {
  63. $author = sloodle_clean_for_output($m->user->get_user_firstname().' '.$m->user->get_user_lastname());
  64. $sloodle->response->add_data_line(array($m->id, $author, sloodle_clean_for_output($m->message)));
  65. }
  66. // Output our response
  67. sloodle_debug('<pre>'); // For debug mode, lets us see the response in a browser
  68. $sloodle->response->render_to_output();
  69. sloodle_debug('</pre>');
  70. ?>