ElggQuestion.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. <?php
  2. class ElggQuestion extends ElggObject {
  3. const SUBTYPE = 'question';
  4. /**
  5. * (non-PHPdoc)
  6. * @see ElggObject::initializeAttributes()
  7. */
  8. protected 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. $url = "questions/view/{$this->getGUID()}/" . elgg_get_friendly_title($this->title);
  18. return elgg_normalize_url($url);
  19. }
  20. /**
  21. * (non-PHPdoc)
  22. * @see ElggObject::canComment()
  23. */
  24. public function canComment($user_guid = 0) {
  25. if ($this->comments_enabled === 'off') {
  26. return false;
  27. }
  28. return parent::canComment($user_guid);
  29. }
  30. /**
  31. * Get the answers on this question
  32. *
  33. * @param array $options accepts all elgg_get_entities options
  34. *
  35. * @return false|int|ElggAnswer[]
  36. */
  37. public function getAnswers(array $options = array()) {
  38. $defaults = [
  39. 'order_by' => 'time_created asc',
  40. ];
  41. $overrides = [
  42. 'type' => 'object',
  43. 'subtype' => 'answer',
  44. 'container_guid' => $this->getGUID(),
  45. ];
  46. $options = array_merge($defaults, $options, $overrides);
  47. return elgg_get_entities($options);
  48. }
  49. /**
  50. * List the answers on this question
  51. *
  52. * @param array $options accepts all elgg_list_entities options
  53. *
  54. * @return string
  55. */
  56. public function listAnswers(array $options = []) {
  57. return elgg_list_entities($options, [$this, 'getAnswers']);
  58. }
  59. /**
  60. * Get the answer that was marked as the correct answer.
  61. *
  62. * @return fasle|ElggAnswer
  63. */
  64. public function getMarkedAnswer() {
  65. $result = false;
  66. $options = [
  67. 'type' => 'object',
  68. 'subtype' => ElggAnswer::SUBTYPE,
  69. 'limit' => 1,
  70. 'container_guid' => $this->getGUID(),
  71. 'metadata_name_value_pairs' => array(
  72. 'name' => 'correct_answer',
  73. 'value' => true
  74. )
  75. ];
  76. $answers = elgg_get_entities_from_metadata($options);
  77. if (!empty($answers)) {
  78. $result = $answers[0];
  79. }
  80. return $result;
  81. }
  82. /**
  83. * Helper function to close a question from further answers.
  84. *
  85. * @return void
  86. */
  87. public function close() {
  88. $this->status = 'closed';
  89. }
  90. /**
  91. * Reopen the question for more answers.
  92. *
  93. * @return void
  94. */
  95. public function reopen() {
  96. $this->status = 'open';
  97. }
  98. /**
  99. * Get the current status of the question.
  100. *
  101. * This can be
  102. * - 'open'
  103. * - 'closed'
  104. *
  105. * @return string the current status
  106. */
  107. public function getStatus() {
  108. $result = 'open';
  109. // do we even support status
  110. if (questions_close_on_marked_answer()) {
  111. // make sure the status is correct
  112. switch ($this->status) {
  113. case 'open':
  114. // is it still open, so no marked answer
  115. if ($this->getMarkedAnswer()) {
  116. $result = 'closed';
  117. }
  118. break;
  119. case 'closed':
  120. $result = 'closed';
  121. // is it still open, so no marked answer
  122. if (!$this->getMarkedAnswer()) {
  123. $result = 'open';
  124. }
  125. break;
  126. default:
  127. // no setting yet
  128. if ($this->getMarkedAnswer()) {
  129. $result = 'closed';
  130. }
  131. break;
  132. }
  133. }
  134. return $result;
  135. }
  136. }