sync_layout_position.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <?php
  2. // Simulates an ajax object-rezzing request
  3. require_once '../../../lib/json/json_encoding.inc.php';
  4. /** Grab the Sloodle/Moodle configuration. */
  5. require_once('../../../init.php');
  6. /** Include the Sloodle PHP API. */
  7. /** Sloodle core library functionality */
  8. require_once(SLOODLE_DIRROOT.'/lib.php');
  9. /** General Sloodle functions. */
  10. require_once(SLOODLE_LIBROOT.'/io.php');
  11. /** Sloodle course data. */
  12. require_once(SLOODLE_LIBROOT.'/course.php');
  13. require_once(SLOODLE_LIBROOT.'/layout_profile.php');
  14. require_once(SLOODLE_LIBROOT.'/active_object.php');
  15. require_once(SLOODLE_LIBROOT.'/user.php');
  16. $error = '';
  17. if (!$USER->id) {
  18. output( 'User not logged in' );
  19. exit;
  20. }
  21. $rezzer = new SloodleActiveObject();
  22. $sloodleuser = new SloodleUser();
  23. $sloodleuser->user_data = $USER;
  24. if (!$layoutentryid = optional_param( 'layoutentryid', 0, PARAM_INT) ) {
  25. error_output( 'Layout ID missing' );
  26. }
  27. if (!$controllerid = optional_param( 'controllerid', 0, PARAM_INT) ) {
  28. error_output( 'Controller ID missing' );
  29. }
  30. if ( !$rezzeruuid = optional_param( 'rezzeruuid', null, PARAM_SAFEDIR ) ) {
  31. error_output('Could not load rezzer');
  32. }
  33. $controller_context = get_context_instance( CONTEXT_MODULE, $controllerid);
  34. if (!has_capability('mod/sloodle:editlayouts', $controller_context)) {
  35. error_output( 'Access denied');
  36. }
  37. $layoutentry = new SloodleLayoutEntry();
  38. if (!$layoutentry->load($layoutentryid)) {
  39. error_output( 'Could not load layout entry' );
  40. }
  41. $controller = new SloodleController();
  42. if (!$controller->load( $controllerid )) {
  43. error_output('Could not load controller');
  44. }
  45. $failures = array();
  46. $active_objects = $controller->get_active_objects( $rezzeruuid, $layoutentryid );
  47. foreach($active_objects as $ao) {
  48. //build response string
  49. $response = new SloodleResponse();
  50. $response->set_status_code(1);
  51. $response->set_status_descriptor('SYSTEM');
  52. $response->set_request_descriptor('REPORT_POSITION');
  53. $response->set_http_in_password($ao->httpinpassword);
  54. //create message - NB for some reason render_to_string changes the string by reference instead of just returning it.
  55. $renderStr="";
  56. $response->render_to_string($renderStr);
  57. //$response = $ao->sendMessage('do:reportposition');
  58. $response = $ao->sendMessage($renderStr);
  59. //var_dump($response['result']);
  60. if (preg_match('/^(<.*?>)\|(<.*?>)\|(.*?)$/', $response['result'], $matches)) {
  61. $layoutentry->position = $matches[1];
  62. $layoutentry->rotation = $matches[2];
  63. $saved = $layoutentry->update();
  64. $ao->position = $matches[1];
  65. $ao->rotation = $matches[2];
  66. $saved = $ao->save();
  67. }
  68. //$rezzed_object_uuid = $reply['result'];
  69. }
  70. // TODO: If we get responses from multiple objects, pick the closest...
  71. // TODO: Handle failures properly...
  72. $result = 'synced';
  73. $content = array(
  74. 'result' => $result,
  75. 'error' => $error,
  76. );
  77. print json_encode($content);
  78. function error_output($error) {
  79. $content = array(
  80. 'result' => 'failed',
  81. 'error' => $error,
  82. );
  83. print json_encode($content);
  84. exit;
  85. }
  86. ?>