question.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. <?php
  2. /**
  3. * Question entity view
  4. *
  5. * @package Questions
  6. */
  7. $question = elgg_extract('entity', $vars, false);
  8. if (!($question instanceof ElggQuestion)) {
  9. return true;
  10. }
  11. $full = (bool) elgg_extract('full_view', $vars, false);
  12. $subtitle = [];
  13. $poster = $question->getOwnerEntity();
  14. $poster_icon = elgg_view_entity_icon($poster, 'small');
  15. $poster_link = elgg_view('output/url', [
  16. 'text' => $poster->name,
  17. 'href' => $poster->getURL(),
  18. 'is_trusted' => true
  19. ]);
  20. $subtitle[] = elgg_echo('questions:asked', [$poster_link]);
  21. $container = $question->getContainerEntity();
  22. if (($container instanceof ElggGroup) && (elgg_get_page_owner_guid() !== $container->getGUID())) {
  23. $group_link = elgg_view('output/url', [
  24. 'text' => $container->name,
  25. 'href' => "questions/group/{$container->getGUID()}/all",
  26. 'is_trusted' => true
  27. ]);
  28. $subtitle[] = elgg_echo('river:ingroup', [$group_link]);
  29. }
  30. $tags = elgg_view('output/tags', ['tags' => $question->tags]);
  31. $subtitle[] = elgg_view_friendly_time($question->time_created);
  32. $answer_options = [
  33. 'type' => 'object',
  34. 'subtype' => 'answer',
  35. 'container_guid' => $question->getGUID(),
  36. 'count' => true,
  37. ];
  38. $num_answers = elgg_get_entities($answer_options);
  39. $answer_text = '';
  40. if ($num_answers != 0) {
  41. $answer_options['limit'] = 1;
  42. $answer_options['count'] = false;
  43. $correct_answer = $question->getMarkedAnswer();
  44. if ($correct_answer) {
  45. $poster = $correct_answer->getOwnerEntity();
  46. $answer_time = elgg_view_friendly_time($correct_answer->time_created);
  47. $answer_link = elgg_view('output/url', ['href' => $poster->getURL(), 'text' => $poster->name]);
  48. $answer_text = elgg_echo('questions:answered:correct', [$answer_link, $answer_time]);
  49. } else {
  50. $last_answer = elgg_get_entities($answer_options);
  51. $poster = $last_answer[0]->getOwnerEntity();
  52. $answer_time = elgg_view_friendly_time($last_answer[0]->time_created);
  53. $answer_link = elgg_view('output/url', ['href' => $poster->getURL(), 'text' => $poster->name]);
  54. $answer_text = elgg_echo('questions:answered', [$answer_link, $answer_time]);
  55. }
  56. $subtitle[] = elgg_view('output/url', [
  57. 'href' => "{$question->getURL()}#question-answers",
  58. 'text' => elgg_echo('answers') . " ({$num_answers})",
  59. ]);
  60. }
  61. $metadata = '';
  62. // do not show the metadata and controls in widget view
  63. if (!elgg_in_context('widgets')) {
  64. $metadata = elgg_view_menu('entity', [
  65. 'entity' => $question,
  66. 'handler' => 'questions',
  67. 'sort_by' => 'priority',
  68. 'class' => 'elgg-menu-hz',
  69. 'full_view' => $full,
  70. ]);
  71. }
  72. $solution_time = (int) $question->solution_time;
  73. if ($solution_time && !$question->getMarkedAnswer()) {
  74. $solution_class = [
  75. 'question-solution-time',
  76. 'float-alt'
  77. ];
  78. if ($solution_time < time()) {
  79. $solution_class[] = ' question-solution-time-late';
  80. } elseif ($solution_time < (time() + (24 * 60 * 60))) {
  81. $solution_class[] = ' question-solution-time-due';
  82. }
  83. $solution_date = elgg_view('output/date', ['value' => $question->solution_time]);
  84. $answer_text .= elgg_format_element('span', ['class' => $solution_class], $solution_date);
  85. }
  86. $subtitle[] = elgg_view('output/categories', $vars);
  87. if ($full) {
  88. $params = [
  89. 'entity' => $question,
  90. 'title' => false,
  91. 'metadata' => $metadata,
  92. 'subtitle' => implode(' ', $subtitle),
  93. 'tags' => $tags,
  94. ];
  95. $list_body = elgg_view('object/elements/summary', $params);
  96. $list_body .= elgg_view('output/longtext', ['value' => $question->description]);
  97. // show comments?
  98. if ($question->comments_enabled !== 'off') {
  99. $comment_count = $question->countComments();
  100. if ($comment_count) {
  101. $comment_options = [
  102. 'type' => 'object',
  103. 'subtype' => 'comment',
  104. 'container_guid' => $question->getGUID(),
  105. 'limit' => false,
  106. 'list_class' => 'elgg-river-comments',
  107. 'distinct' => false,
  108. 'full_view' => true,
  109. ];
  110. $list_body .= elgg_format_element('h3', [
  111. 'class' => ['elgg-river-comments-tab', 'mtm']
  112. ], elgg_echo('comments')
  113. );
  114. $list_body .= elgg_list_entities($comment_options);
  115. }
  116. if ($question->canComment()) {
  117. // show a comment form like in the river
  118. $body_vars = [
  119. 'entity' => $question,
  120. 'inline' => true,
  121. ];
  122. $form = elgg_view_form('comment/save', [], $body_vars);
  123. $list_body .= elgg_format_element('div', [
  124. 'class' => ['elgg-river-item', 'hidden'],
  125. 'id' => "comments-add-{$question->getGUID()}"
  126. ], $form
  127. );
  128. }
  129. }
  130. echo elgg_view_image_block($poster_icon, $list_body);
  131. } else {
  132. // brief view
  133. $title_text = '';
  134. if ($question->getMarkedAnswer()) {
  135. $title_text = elgg_view_icon('checkmark', ['class' => 'mrs question-listing-checkmark']);
  136. }
  137. $title_text .= elgg_get_excerpt($question->title, 100);
  138. $title = elgg_view('output/url', [
  139. 'text' => $title_text,
  140. 'href' => $question->getURL(),
  141. 'is_trusted' => true,
  142. ]);
  143. $excerpt = '';
  144. if (!empty($question->description)) {
  145. $excerpt = elgg_format_element('div', ['class' => 'mbm'], elgg_get_excerpt($question->description));
  146. }
  147. if (!empty($answer_text)) {
  148. $answer_text = elgg_format_element('div', ['class' => 'elgg-subtext'], $answer_text);
  149. }
  150. $params = [
  151. 'entity' => $question,
  152. 'title' => $title,
  153. 'metadata' => $metadata,
  154. 'subtitle' => implode(' ', $subtitle),
  155. 'tags' => $tags,
  156. 'content' => $excerpt . $answer_text,
  157. ];
  158. $list_body = elgg_view('object/elements/summary', $params);
  159. echo elgg_view_image_block($poster_icon, $list_body);
  160. }