save.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. <?php
  2. elgg_make_sticky_form('question');
  3. $guid = (int) get_input('guid');
  4. if (empty($guid)) {
  5. $question = new ElggQuestion();
  6. } else {
  7. elgg_entity_gatekeeper($guid, 'object', 'question');
  8. $question = get_entity($guid);
  9. }
  10. $adding = !$question->getGUID();
  11. $editing = !$adding;
  12. $moving = false;
  13. if ($editing && !$question->canEdit()) {
  14. register_error(elgg_echo('actionunauthorized'));
  15. forward(REFERER);
  16. }
  17. $container_guid = (int) get_input('container_guid');
  18. if (empty($container_guid)) {
  19. $container_guid = (int) $question->getOwnerGUID();
  20. }
  21. if ($editing && ($container_guid != $question->getContainerGUID())) {
  22. $moving = true;
  23. }
  24. $container = get_entity($container_guid);
  25. if ($adding && !questions_can_ask_question($container)) {
  26. register_error(elgg_echo('questions:action:question:save:error:container'));
  27. forward(REFERER);
  28. }
  29. if (questions_limited_to_groups() && ($container_guid == $question->getOwnerGUID())) {
  30. register_error(elgg_echo('questions:action:question:save:error:limited_to_groups'));
  31. forward(REFERER);
  32. }
  33. $title = get_input('title');
  34. $description = get_input('description');
  35. $tags = string_to_tag_array(get_input('tags', ''));
  36. $access_id = (int) get_input('access_id');
  37. $comments_enabled = get_input('comments_enabled');
  38. if (empty($container_guid) || empty($title)) {
  39. register_error(elgg_echo('questions:action:question:save:error:body', [$container_guid, $title]));
  40. forward(REFERER);
  41. }
  42. // make sure we have a valid access_id
  43. $access_id = questions_validate_access_id($access_id, $container_guid);
  44. $question->title = $title;
  45. $question->description = $description;
  46. $question->access_id = $access_id;
  47. $question->container_guid = $container_guid;
  48. $question->tags = $tags;
  49. $question->comments_enabled = $comments_enabled;
  50. try {
  51. $question->save();
  52. if ($adding) {
  53. // add river event
  54. elgg_create_river_item([
  55. 'view' => 'river/object/question/create',
  56. 'action_type' => 'create',
  57. 'subject_guid' => elgg_get_logged_in_user_guid(),
  58. 'object_guid' => $question->getGUID(),
  59. 'access_id' => $question->access_id,
  60. ]);
  61. // check for a solution time limit
  62. $solution_time = questions_get_solution_time($question->getContainerEntity());
  63. if ($solution_time) {
  64. // add x number of days when the question should be solved
  65. $question->solution_time = (time() + ($solution_time * 24 * 60 * 60));
  66. }
  67. } elseif ($moving) {
  68. elgg_trigger_event('move', 'object', $question);
  69. }
  70. } catch (Exception $e) {
  71. register_error(elgg_echo('questions:action:question:save:error:save'));
  72. register_error($e->getMessage());
  73. forward(REFERER);
  74. }
  75. elgg_clear_sticky_form('question');
  76. if (!$adding) {
  77. $forward_url = $question->getURL();
  78. } elseif ($container instanceof ElggUser) {
  79. $forward_url = "questions/owner/{$container->username}";
  80. } else {
  81. $forward_url = "questions/group/{$container->getGUID()}/all";
  82. }
  83. forward($forward_url);