save.php 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. <?php
  2. elgg_make_sticky_form('answer');
  3. $guid = (int) get_input('guid');
  4. $answer = new ElggAnswer($guid);
  5. $adding = !$answer->guid;
  6. $editing = !$adding;
  7. if ($editing && !$answer->canEdit()) {
  8. register_error(elgg_echo('actionunauthorized'));
  9. forward(REFERER);
  10. }
  11. $container_guid = (int) get_input('container_guid');
  12. $description = get_input('description');
  13. if (empty($container_guid) || empty($description)) {
  14. register_error(elgg_echo('questions:action:answer:save:error:body', [$container_guid, $description]));
  15. forward(REFERER);
  16. }
  17. if ($adding && !can_write_to_container(0, $container_guid, 'object', 'answer')) {
  18. register_error(elgg_echo('questions:action:answer:save:error:container'));
  19. forward(REFERER);
  20. }
  21. elgg_entity_gatekeeper($container_guid, 'object', ElggQuestion::SUBTYPE);
  22. $question = get_entity($container_guid);
  23. if ($question->getStatus() != 'open') {
  24. elgg_clear_sticky_form('answer');
  25. register_error(elgg_echo('questions:action:answer:save:error:question_closed'));
  26. forward(REFERER);
  27. }
  28. $answer->description = $description;
  29. $answer->access_id = $question->access_id;
  30. $answer->container_guid = $container_guid;
  31. try {
  32. $answer->save();
  33. if ($adding) {
  34. // check for auto mark as correct
  35. $answer->checkAutoMarkCorrect($adding);
  36. // create river event
  37. elgg_create_river_item([
  38. 'view' => 'river/object/answer/create',
  39. 'action_type' => 'create',
  40. 'subject_guid' => elgg_get_logged_in_user_guid(),
  41. 'object_guid' => $answer->getGUID(),
  42. 'access_id' => $answer->access_id,
  43. ]);
  44. }
  45. } catch (Exception $e) {
  46. register_error(elgg_echo('questions:action:answer:save:error:save'));
  47. register_error($e->getMessage());
  48. }
  49. elgg_clear_sticky_form('answer');
  50. forward(get_input('forward', $answer->getURL()));