toggle_mark.php 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. <?php
  2. /**
  3. * This action marks an answer as the correct answer for a question.
  4. *
  5. */
  6. $guid = (int) get_input('guid');
  7. if (empty($guid)) {
  8. register_error(elgg_echo('error:missing_data'));
  9. forward(REFERER);
  10. }
  11. elgg_entity_gatekeeper($guid, 'object', 'answer');
  12. $entity = get_entity($guid);
  13. // are you allowed to mark answers as correct
  14. if (!questions_can_mark_answer($entity)) {
  15. register_error(elgg_echo('questions:action:answer:toggle_mark:error:not_allowed'));
  16. forward(REFERER);
  17. }
  18. $question = $entity->getContainerEntity();
  19. $answer = $question->getMarkedAnswer();
  20. if (empty($answer)) {
  21. // no answer yet, so mark this one
  22. $entity->markAsCorrect();
  23. system_message(elgg_echo('questions:action:answer:toggle_mark:success:mark'));
  24. } elseif ($answer->getGUID() == $entity->getGUID()) {
  25. // the marked answer is this answer, so unmark
  26. $entity->undoMarkAsCorrect();
  27. system_message(elgg_echo('questions:action:answer:toggle_mark:success:unmark'));
  28. } else {
  29. register_error(elgg_echo('questions:action:answer:toggle_mark:error:duplicate'));
  30. }
  31. forward(REFERER);