update_layout_entry.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. <?php
  2. // Simulates an ajax object-rezzing request
  3. /** Grab the Sloodle/Moodle configuration. */
  4. require_once('../../../init.php');
  5. /** Include the Sloodle PHP API. */
  6. /** Sloodle core library functionality */
  7. require_once(SLOODLE_DIRROOT.'/lib.php');
  8. /** General Sloodle functions. */
  9. require_once(SLOODLE_LIBROOT.'/io.php');
  10. /** Sloodle course data. */
  11. require_once(SLOODLE_LIBROOT.'/course.php');
  12. require_once(SLOODLE_LIBROOT.'/layout_profile.php');
  13. require_once(SLOODLE_LIBROOT.'/active_object.php');
  14. require_once(SLOODLE_LIBROOT.'/user.php');
  15. // TODO: We'll want to manage this info in a different way.
  16. require_once(SLOODLE_LIBROOT.'/object_configs.php');
  17. require_once '../../../lib/json/json_encoding.inc.php';
  18. $configVars = array();
  19. $layoutentryid = null;
  20. $rezzeruuid = null;
  21. foreach($_POST as $n => $v) {
  22. if ($n == 'layoutentryid') {
  23. $layoutentryid = intval($v);
  24. } else if ($n == 'rezzeruuid') {
  25. $rezzeruuid = $v;
  26. } else {
  27. $configVars[$n] = $v;
  28. }
  29. }
  30. if (!$layoutentryid) {
  31. error_output( 'Layout entry ID missing');
  32. }
  33. $layoutentry = new SloodleLayoutEntry();
  34. if (!$layoutentry->load($layoutentryid)) {
  35. error_output( 'Could not load layout entry' );
  36. }
  37. foreach($configVars as $n=>$v) {
  38. /*
  39. if (preg_match('/[^A-Za-z0-9_-]/', $v)) {
  40. error_output( 'Illegal characters in config value');
  41. }
  42. if (preg_match('/[^A-Za-z0-9_-]/', $n)) {
  43. error_output( 'Illegal characters in config name');
  44. }
  45. */
  46. $layoutentry->set_config( $n, $v );
  47. }
  48. // Send a reset to the object
  49. $controller = new SloodleController();
  50. if (!$controller->load( $configVars['controllerid'] )) {
  51. error_output('Could not load controller');
  52. }
  53. $controller_context = get_context_instance( CONTEXT_MODULE, $configVars['controllerid']);
  54. if (!has_capability('mod/sloodle:editlayouts', $controller_context)) {
  55. error_output( 'Access denied');
  56. }
  57. //var_dump($layoutentry);
  58. if (!$layoutentry->update()) {
  59. error_output('Layout entry update failed');
  60. }
  61. $async = ( defined('SLOODLE_ASYNC_SEND_CONFIG') && SLOODLE_ASYNC_SEND_CONFIG );
  62. $failures = array();
  63. $active_objects = $controller->get_active_objects( $rezzeruuid, $layoutentryid );
  64. if (count($active_objects) > 0) {
  65. foreach($active_objects as $ao) {
  66. if ($ao->configure_for_layout() || true) {
  67. $response = $ao->refreshConfig($async);
  68. sleep(1);
  69. $response2 = $ao->sendConfig($async);
  70. }
  71. // No error handling here - if it breaks, just carry on.
  72. //var_dump($response['result']);
  73. /*
  74. if (preg_match('/^(<.*?>)\|(<.*?>)\|(.*?)$/', $response['result'], $matches)) {
  75. $layoutentry->position = $matches[1];
  76. $layoutentry->rotation = $matches[2];
  77. $saved = $layoutentry->update();
  78. }
  79. */
  80. }
  81. }
  82. if (!$moduletitle = $layoutentry->get_course_module_title()) {
  83. $moduletitle = '';
  84. }
  85. $content = array(
  86. 'result' => 'updated',
  87. 'objectname' => preg_replace('/SLOODLE\s/', '', $layoutentry->name),
  88. 'moduletitle' => $moduletitle,
  89. 'layoutid' => $layoutentry->layout,
  90. 'layoutentryid' => $layoutentry->id
  91. );
  92. $rand = rand(0,10);
  93. //sleep($rand);
  94. print json_encode($content);
  95. exit;
  96. function error_output($error) {
  97. $content = array(
  98. 'result' => 'failed',
  99. 'error' => $error,
  100. );
  101. print json_encode($content);
  102. exit;
  103. }
  104. ?>