move_to_discussions.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. <?php
  2. elgg_make_sticky_form('question');
  3. $guid = (int) get_input('guid');
  4. $title = get_input('title');
  5. $description = get_input('description');
  6. $tags = string_to_tag_array(get_input('tags', ''));
  7. $access_id = (int) get_input('access_id');
  8. $forward_url = REFERER;
  9. if (empty($guid)) {
  10. register_error(elgg_echo('error:missing_data'));
  11. forward(REFERER);
  12. }
  13. elgg_entity_gatekeeper($guid, 'object', 'question');
  14. $entity = get_entity($guid);
  15. $container = $entity->getContainerEntity();
  16. if (!$entity->canEdit() || !questions_can_move_to_discussions($container)) {
  17. register_error(elgg_echo('questions:action:question:move_to_discussions:error:move'));
  18. forward(REFERER);
  19. }
  20. $access_id = questions_validate_access_id($access_id, $container->getGUID());
  21. // save the latest changes
  22. $entity->title = $title;
  23. $entity->description = $description;
  24. $entity->tags = $tags;
  25. $entity->access_id = $access_id;
  26. $entity->save();
  27. // create new discussion
  28. $topic = new ElggObject();
  29. $topic->subtype = 'groupforumtopic';
  30. $topic->container_guid = $entity->getContainerGUID();
  31. $topic->access_id = $entity->access_id;
  32. $topic->title = $entity->title;
  33. $topic->description = $entity->description;
  34. $topic->tags = $entity->tags;
  35. $topic->status = 'open';
  36. if ($topic->save()) {
  37. // cleanup sticky form
  38. elgg_clear_sticky_form('question');
  39. // make sure we can copy all annotations
  40. $ia = elgg_set_ignore_access(true);
  41. $comment_options = [
  42. 'type' => 'object',
  43. 'subtype' => 'comment',
  44. 'container_guid' => $entity->getGUID(),
  45. 'limit' => false,
  46. ];
  47. $comments = new ElggBatch('elgg_get_entities', $comment_options);
  48. // copy all comments on the question to topic replies
  49. foreach ($comments as $comment) {
  50. $reply = new ElggDiscussionReply();
  51. $reply->owner_guid = $comment->getOwnerGUID();
  52. $reply->container_guid = $topic->getGUID();
  53. $reply->access_id = $topic->access_id;
  54. $reply->description = $comment->description;
  55. if ($reply->save()) {
  56. questions_backdate_entity($reply->getGUID(), $comment->time_created);
  57. }
  58. $comment->delete();
  59. }
  60. $answer_options = [
  61. 'type' => 'object',
  62. 'subtype' => 'answer',
  63. 'container_guid' => $entity->getGUID(),
  64. 'limit' => false,
  65. ];
  66. $answers = new ElggBatch('elgg_get_entities', $answer_options);
  67. // copy all answers on the question to topic replies
  68. foreach ($answers as $answer) {
  69. // move awnser to reply
  70. $reply = new ElggDiscussionReply();
  71. $reply->owner_guid = $answer->getOwnerGUID();
  72. $reply->container_guid = $topic->getGUID();
  73. $reply->access_id = $topic->access_id;
  74. $reply->description = $answer->description;
  75. if ($reply->save()) {
  76. questions_backdate_entity($reply->getGUID(), $answer->time_created);
  77. }
  78. // copy all comments on the answer to topic replies
  79. $comment_options['container_guid'] = $answer->getGUID();
  80. $comments = new ElggBatch('elgg_get_entities', $comment_options);
  81. foreach ($comments as $comment) {
  82. $reply = new ElggDiscussionReply();
  83. $reply->owner_guid = $comment->getOwnerGUID();
  84. $reply->container_guid = $topic->getGUID();
  85. $reply->access_id = $topic->access_id;
  86. $reply->description = $comment->description;
  87. if ($reply->save()) {
  88. questions_backdate_entity($reply->getGUID(), $comment->time_created);
  89. }
  90. $comment->delete();
  91. }
  92. $answer->delete();
  93. }
  94. // last changes to the topic
  95. // backdate the discussion
  96. $topic->time_created = $entity->time_created;
  97. // set correct owner of the topic
  98. $topic->owner_guid = $entity->getOwnerGUID();
  99. $topic->save();
  100. // cleaup the old question
  101. $entity->delete();
  102. // restore access
  103. elgg_set_ignore_access($ia);
  104. // set correct forward url
  105. $forward_url = 'questions/todo/' . $entity->getContainerGUID();
  106. system_message(elgg_echo('questions:action:question:move_to_discussions:success'));
  107. } else {
  108. register_error(elgg_echo('questions:action:question:move_to_discussions:error:topic'));
  109. }
  110. forward($forward_url);