ElggAnswer.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. <?php
  2. class ElggAnswer extends ElggObject {
  3. const SUBTYPE = 'answer';
  4. /**
  5. * (non-PHPdoc)
  6. * @see ElggObject::initializeAttributes()
  7. */
  8. function initializeAttributes() {
  9. parent::initializeAttributes();
  10. $this->attributes['subtype'] = self::SUBTYPE;
  11. }
  12. /**
  13. * (non-PHPdoc)
  14. * @see ElggEntity::getURL()
  15. */
  16. public function getURL() {
  17. $container_entity = $this->getContainerEntity();
  18. $url = $container_entity->getURL() . "#elgg-object-{$this->getGUID()}";
  19. return $url;
  20. }
  21. /**
  22. * (non-PHPdoc)
  23. * @see ElggObject::canComment()
  24. */
  25. public function canComment($user_guid = 0) {
  26. return $this->getContainerEntity()->canComment($user_guid);
  27. }
  28. /**
  29. * (non-PHPdoc)
  30. * @see ElggEntity::__get()
  31. */
  32. public function __get($name) {
  33. if ($name === 'title') {
  34. $question = $this->getContainerEntity();
  35. return elgg_echo('questions:object:answer:title', [$question->title]);
  36. }
  37. return parent::__get($name);
  38. }
  39. /**
  40. * Get the metadata object for the correct answer
  41. *
  42. * @return false|ElggMetadata
  43. */
  44. public function getCorrectAnswerMetadata() {
  45. $result = false;
  46. $options = [
  47. 'metadata_name' => 'correct_answer',
  48. 'guid' => $this->getGUID(),
  49. ];
  50. $metadata = elgg_get_metadata($options);
  51. if ($metadata) {
  52. $result = $metadata[0];
  53. }
  54. return $result;
  55. }
  56. /**
  57. * Mark an answer as the correct answer for this question
  58. *
  59. * @return void
  60. */
  61. public function markAsCorrect() {
  62. // first set the mark
  63. $this->correct_answer = true;
  64. // trigger event for notifications
  65. elgg_trigger_event('correct', 'object', $this);
  66. // depending of the plugin settings, we also need to close the question
  67. if (questions_close_on_marked_answer()) {
  68. $question = $this->getContainerEntity();
  69. $question->close();
  70. }
  71. }
  72. /**
  73. * This answer is no longer the correct answer for this question
  74. *
  75. * @return void
  76. */
  77. public function undoMarkAsCorrect() {
  78. unset($this->correct_answer);
  79. // don't forget to reopen the question
  80. $question = $this->getContainerEntity();
  81. $question->reopen();
  82. }
  83. /**
  84. * Check if we can auto mark this as the correct answer
  85. *
  86. * @param bool $creating new answer or editing (default: editing)
  87. *
  88. * @return void
  89. */
  90. public function checkAutoMarkCorrect($creating = false) {
  91. $creating = (bool) $creating;
  92. if (empty($creating)) {
  93. // only on new entities
  94. return;
  95. }
  96. $question = $this->getContainerEntity();
  97. $container = $question->getContainerEntity();
  98. $user = $this->getOwnerEntity();
  99. if (questions_auto_mark_answer_correct($container, $user)) {
  100. $this->markAsCorrect();
  101. }
  102. }
  103. }