add_layout_entry.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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. require_once(SLOODLE_LIBROOT.'/object_configs.php');
  16. require_once '../../../lib/json/json_encoding.inc.php';
  17. include('index.template.php');
  18. $configVars = array();
  19. $skipvars = array(
  20. 'controllerid', // Should be in the layout entry
  21. 'courseid', // Should come from the controller
  22. 'layoutentryid', // Layout entry
  23. 'rezzeruuid' // Doesn't belong in layout
  24. );
  25. $layoutid = null;
  26. foreach($_POST as $n => $v) {
  27. if (in_array($n, $skipvars)) {
  28. continue;
  29. }
  30. if ($n == 'layoutid') {
  31. $layoutid = intval($v);
  32. } else if ($n == 'objectname') {
  33. $objectname = $v;
  34. } else if ($n == 'objectgroup') {
  35. $objectgroup= $v;
  36. } else {
  37. $configVars[$n] = $v;
  38. }
  39. }
  40. /*
  41. if (preg_match('/[^A-Za-z0-9_-]/', $objectname)) {
  42. error_output( 'Illegal characters in object name');
  43. }
  44. */
  45. if (!$layoutid) {
  46. error_output( 'Layout ID missing');
  47. }
  48. $layout = new SloodleLayout();
  49. if (!$layout->load($layoutid)) {
  50. error_output( 'Layout not found');
  51. }
  52. $controller_context = get_context_instance( CONTEXT_MODULE, $layout->controllerid);
  53. if (!has_capability('mod/sloodle:editlayouts', $controller_context)) {
  54. error_output( 'Access denied');
  55. }
  56. $layoutentry = new SloodleLayoutEntry();
  57. $layoutentry->name = $objectname;
  58. $layoutentry->layout = $layoutid;
  59. $layoutentry->position = "<0,-1,1.5>"; // default: behind and above the set where it's easy to see
  60. $layoutentry->rotation = "<0.0,0.0,0.0,0.0>";
  61. foreach($configVars as $n=>$v) {
  62. /*
  63. if (preg_match('/[^A-Za-z0-9_-/', $v)) {
  64. error_output( 'Illegal characters in config value');
  65. }
  66. */
  67. if (preg_match('/[^A-Za-z0-9_-]/', $n)) {
  68. error_output( 'Illegal characters in config name');
  69. }
  70. $layoutentry->set_config( $n, $v );
  71. }
  72. //var_dump($layoutentry);
  73. if (!$layoutentryid = $layoutentry->insert()) {
  74. error_output( 'Layout entry creation failed');
  75. }
  76. if (!$moduletitle = $layoutentry->get_course_module_title()) {
  77. $moduletitle = '';
  78. }
  79. if (!$config = $layoutentry->get_object_config()) {
  80. error_output( 'Could not create config for object');
  81. }
  82. if (!$courseid = $layout->course) {
  83. error_output('Could not get courseidfrom layout');
  84. }
  85. if (!$controllerid = $layout->controllerid) {
  86. error_output('Could not get controllerid from layout');
  87. }
  88. $edit_object_form = '';
  89. $html_list_item = '';
  90. //var_dump($objectname);
  91. //var_dump($config);
  92. //var_dump($courseid);
  93. //var_dump($controllerid);
  94. ob_start();
  95. $element_id = print_rezzable_item_li( $layoutentry, $courseid, $controllerid, $layout, false);
  96. $html_list_item = ob_get_clean();
  97. ob_start();
  98. $element_id = print_config_form( $layoutentry, $config, $courseid, $controllerid, $layoutid, $config->group, $rezzer->uuid);
  99. $edit_object_form = ob_get_clean();
  100. $content = array(
  101. 'result' => 'added',
  102. 'objectgroup' => $objectgroup, // TODO: Get this from the object_configs
  103. 'objectgrouptext' => get_string('objectgroup:'.$objectgroup, 'sloodle'), // TODO: Get this from the object_configs
  104. 'objectname' => preg_replace('/SLOODLE\s/', '', $objectname),
  105. 'objecttypelinkable' => $layoutentry->objectDefinition()->type_for_link(),
  106. 'moduletitle' => $moduletitle,
  107. 'layoutid' => $layoutid,
  108. 'layoutentryid' => $layoutentry->id,
  109. 'edit_object_form' => $edit_object_form,
  110. 'html_list_item' => $html_list_item
  111. );
  112. $rand = rand(0,10);
  113. //sleep($rand);
  114. print json_encode($content);
  115. exit;
  116. function error_output($error) {
  117. $content = array(
  118. 'result' => 'failed',
  119. 'error' => $error,
  120. );
  121. print json_encode($content);
  122. exit;
  123. }
  124. ?>