generate_layout_entries.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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(SLOODLE_LIBROOT.'/object_configs.php');
  14. require_once '../../../lib/json/json_encoding.inc.php';
  15. include('index.template.php');
  16. ini_set('display_errors', 1);
  17. error_reporting(E_ALL);
  18. require_once(SLOODLE_LIBROOT.'/layout_recipe/layout_recipe_base.php');
  19. $layoutid = optional_param('layoutid', 0, PARAM_INT);
  20. $recipe = optional_param('layoutrecipe', 'SloodleSimpleLayoutRecipe', PARAM_TEXT);
  21. if (!$layoutid) {
  22. error_output( 'Layout ID missing');
  23. }
  24. if (!class_exists( $recipe ) ){
  25. error_output( 'Recipe definition not found');
  26. }
  27. $layout = new SloodleLayout();
  28. if (!$layout->load($layoutid)) {
  29. error_output( 'Could not load layout');
  30. }
  31. $recipe = new $recipe();
  32. if (!$recipe->generate()) {
  33. error_output( 'Could not generate recipe');
  34. }
  35. if (!$recipe->saveToLayoutWithID( $layoutid )) {
  36. error_output( 'Could not save generated recipe to layout');
  37. }
  38. $courseid = $layout->course;
  39. $controller_context = get_context_instance( CONTEXT_MODULE, $layout->controllerid);
  40. if (!has_capability('mod/sloodle:uselayouts', $controller_context)) {
  41. error_output( 'Access denied');
  42. }
  43. $controllerid = $layout->controllerid;
  44. $rezzeruuid = $_REQUEST['rezzeruuid'];
  45. $addedentries = array();
  46. foreach($layout->get_entries() as $layoutentry) {
  47. $config = SloodleObjectConfig::ForObjectName($layoutentry->name);
  48. $modtitle = $layoutentry->get_course_module_title();
  49. if (!$modtitle) {
  50. $modtitle = '';
  51. }
  52. ob_start();
  53. $element_id = print_rezzable_item_li( $layoutentry, $courseid, $controllerid, $layout, false);
  54. $html_list_item = ob_get_clean();
  55. ob_start();
  56. $element_id = print_config_form( $layoutentry, $config, $courseid, $controllerid, $layoutid, $config->group, $rezzeruuid);
  57. $edit_object_form = ob_get_clean();
  58. $addedentries[] = array(
  59. 'objectgroup' => $config->group,
  60. 'objectgrouptext' => get_string('objectgroup:'.$config->group, 'sloodle'),
  61. 'objecttypelinkable' => $config->type_for_link(),
  62. 'objectname' => preg_replace('/SLOODLE\s/', '', $layoutentry->name),
  63. 'moduletitle' => $modtitle,
  64. 'layoutid' => $layoutid,
  65. 'layoutentryid' => $layoutentry->id,
  66. 'html_list_item' => $html_list_item,
  67. 'edit_object_form' => $edit_object_form
  68. );
  69. };
  70. $content = array(
  71. 'result' => 'generated',
  72. 'layoutid' => $layoutid,
  73. 'addedentries' => $addedentries,
  74. );
  75. print json_encode($content);
  76. exit;
  77. function error_output($error) {
  78. $content = array(
  79. 'result' => 'failed',
  80. 'error' => $error,
  81. );
  82. print json_encode($content);
  83. exit;
  84. }
  85. ?>