add_layout.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. <?php
  2. /** Grab the Sloodle/Moodle configuration. */
  3. require_once('../../../init.php');
  4. /** Include the Sloodle PHP API. */
  5. /** Sloodle core library functionality */
  6. require_once(SLOODLE_DIRROOT.'/lib.php');
  7. /** General Sloodle functions. */
  8. require_once(SLOODLE_LIBROOT.'/io.php');
  9. /** Sloodle course data. */
  10. require_once(SLOODLE_LIBROOT.'/course.php');
  11. require_once(SLOODLE_LIBROOT.'/layout_profile.php');
  12. require_once(SLOODLE_LIBROOT.'/user.php');
  13. require_once '../../../lib/json/json_encoding.inc.php';
  14. $configVars = array();
  15. $courseid = optional_param('courseid', 0, PARAM_INT);
  16. $layoutname = optional_param('layoutname', '', PARAM_TEXT);
  17. $rezzeruuid = optional_param('rezzeruuid', '', PARAM_RAW);
  18. $controllerid = optional_param('controllerid', '', PARAM_RAW);
  19. if (!$courseid) {
  20. error_output( 'Course ID missing');
  21. }
  22. $controller_context = get_context_instance( CONTEXT_MODULE, $controllerid);
  23. if (!has_capability('mod/sloodle:editlayouts', $controller_context)) {
  24. error_output( 'Access denied');
  25. }
  26. $layout = new SloodleLayout();
  27. $layout->name = $layoutname;
  28. $layout->course = $courseid;
  29. $layout->controllerid = $controllerid;
  30. if (!$layoutid = $layout->insert()) {
  31. error_output( 'Layout creation failed');
  32. }
  33. // Create a set of divs for the layout forms.
  34. // We do this on the server side to avoid a lot of hairy javascript html generation.
  35. // We'll pass it back in the AJAX response and let the create script slot it into the right places.
  36. // If there are multiple controllers for the same course, we may have multiple of these, so we'll make an array of them
  37. // NB The list item in the scenes list is still generated in the
  38. // TODO: Lots of duplication with index.php - would be good to reduce it somehow.
  39. // REGULAR SLOODLE TODO: This should filter for courses the user has access to.
  40. $courses = get_courses();
  41. $coursesbyid = array();
  42. foreach($courses as $course) {
  43. $id = $course->id;
  44. $coursesbyid[ $id ] = $course;
  45. }
  46. // Get a list of controllers which the user is permitted to authorise objects on
  47. $controllers = array();
  48. $recs = sloodle_get_records('sloodle', 'type', SLOODLE_TYPE_CTRL);
  49. // Make sure we have at least one controller
  50. if ($recs == false || count($recs) == 0) {
  51. error(get_string('objectauthnocontrollers','sloodle'));
  52. exit();
  53. }
  54. foreach ($recs as $r) {
  55. // Fetch the course module
  56. $cm = get_coursemodule_from_instance('sloodle', $r->id);
  57. if ($cm->course != $courseid) {
  58. continue;
  59. }
  60. // Check that the person can authorise objects of this module
  61. if (has_capability('mod/sloodle:objectauth', get_context_instance(CONTEXT_MODULE, $cm->id))) {
  62. // Store this controller
  63. $controllers[$cm->course][$cm->id] = $r;
  64. }
  65. }
  66. $courselayouts = array();
  67. include_once(SLOODLE_LIBROOT.'/object_configs.php');
  68. //$object_configs = SloodleObjectConfig::AllAvailableAsArrayByGroup();
  69. $object_configs = SloodleObjectConfig::AllAvailableAsArray();
  70. $objectconfigsbygroup = SloodleObjectConfig::AllAvailableAsArrayByGroup();
  71. if (!isset($objectconfigsbygroup['misc'])) {
  72. $objectconfigsbygroup['misc'] = array(); // always make sure we have this group so that we can add misc objects added to the rezzer.
  73. }
  74. // include('object_configs.array.php');
  75. // Construct the list of course names
  76. $coursenames = array();
  77. $layoutentries = array();
  78. foreach ($controllers as $cid => $ctrls) {
  79. $sloodle_course = new SloodleCourse();
  80. if (!$sloodle_course->load($cid)) {
  81. continue;
  82. }
  83. $moodle_course = $sloodle_course->get_course_object();
  84. $coursenames[$cid] = $moodle_course->fullname;
  85. $courselayouts[$cid] = array();
  86. foreach($ctrls as $contid => $ctrl) {
  87. $sloodle_course->ensure_at_least_one_layout('Scene ', $contid);
  88. $layouts = $sloodle_course->get_layouts($contid);
  89. $courselayouts[$cid][$contid] = array();
  90. foreach($layouts as $l) {
  91. $entries = $sloodle_course->get_layout_entries_for_layout_id($l->id);
  92. $entriesbygroup = array('communication'=>array(), 'activity'=>array(), 'registration'=>array(), 'misc'=>array() );
  93. foreach($entries as $e) {
  94. $objectname = $e->name;
  95. $grp = 'misc';
  96. if (isset($object_configs[$objectname])) {
  97. $grp = $object_configs[$objectname]->group;
  98. }
  99. if (!isset($entriesbygroup[ $grp ] )) {
  100. $entriesbygroup[ $grp ] = array();
  101. }
  102. $entriesbygroup[ $grp ][] = $e;
  103. }
  104. $layoutentries[$l->id] = $entriesbygroup;
  105. $courselayouts[$cid][$contid][] = $l;
  106. }
  107. }
  108. }
  109. include('index.template.php');
  110. ob_start();
  111. print_layout_lists( $courses, $controllers, $courselayouts, $layoutentries, $rezzeruuid);
  112. $add_layout_lists = ob_get_clean();
  113. ob_start();
  114. print_layout_add_object_groups( $courses, $controllers, $courselayouts, $objectconfigsbygroup );
  115. $add_object_groups = ob_get_clean();
  116. ob_start();
  117. print_add_object_forms($courses, $controllers, $courselayouts, $object_configs, $rezzeruuid );
  118. $add_object_forms = ob_get_clean();
  119. ob_start();
  120. print_edit_object_forms($courses, $controllers, $courselayouts, $object_configs, $layoutentries, $rezzeruuid);
  121. $edit_object_forms = ob_get_clean();
  122. $content = array(
  123. 'result' => 'added',
  124. 'layoutname' => $layoutname, // TODO: Get this from the object_configs
  125. 'layoutid' => $layoutid,
  126. 'courseid' => $courseid,
  127. 'controllerid' => $controllerid,
  128. 'add_layout_lists' => $add_layout_lists,
  129. 'add_object_groups' => $add_object_groups,
  130. 'add_object_forms' => $add_object_forms,
  131. 'edit_object_forms' => $edit_object_forms
  132. );
  133. print json_encode($content);
  134. exit;
  135. function error_output($error) {
  136. $content = array(
  137. 'result' => 'failed',
  138. 'error' => $error,
  139. );
  140. print json_encode($content);
  141. exit;
  142. }
  143. ?>