QnA_Answer.php 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. <?php
  2. // This file is part of GNU social - https://www.gnu.org/software/social
  3. //
  4. // GNU social is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Affero General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // GNU social is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Affero General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Affero General Public License
  15. // along with GNU social. If not, see <http://www.gnu.org/licenses/>.
  16. /**
  17. * Data class to save answers to questions
  18. *
  19. * @category QnA
  20. * @package GNUsocial
  21. * @author Zach Copley <zach@status.net>
  22. * @copyright 2011 StatusNet, Inc.
  23. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
  24. */
  25. defined('STATUSNET') || die();
  26. /**
  27. * For storing answers
  28. *
  29. * @copyright 2011 StatusNet, Inc.
  30. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
  31. *
  32. * @see DB_DataObject
  33. */
  34. class QnA_Answer extends Managed_DataObject
  35. {
  36. const OBJECT_TYPE = 'http://activityschema.org/object/answer';
  37. public $__table = 'qna_answer'; // table name
  38. public $id; // char(36) primary key not null -> UUID
  39. public $uri; // varchar(191) not 255 because utf8mb4 takes more space
  40. public $question_id; // char(36) -> question.id UUID
  41. public $profile_id; // int -> question.id
  42. public $best; // bool -> whether the question asker has marked this as the best answer
  43. public $revisions; // int -> count of revisions to this answer
  44. public $content; // text -> response text
  45. public $created; // datetime
  46. /**
  47. * The One True Thingy that must be defined and declared.
  48. */
  49. public static function schemaDef()
  50. {
  51. return array(
  52. 'description' => 'Record of answers to questions',
  53. 'fields' => array(
  54. 'id' => array(
  55. 'type' => 'char',
  56. 'length' => 36,
  57. 'not null' => true, 'description' => 'UUID of the response',
  58. ),
  59. 'uri' => array(
  60. 'type' => 'varchar',
  61. 'length' => 191,
  62. 'not null' => true,
  63. 'description' => 'UUID to the answer notice',
  64. ),
  65. 'question_id' => array(
  66. 'type' => 'char',
  67. 'length' => 36,
  68. 'not null' => true,
  69. 'description' => 'UUID of question being responded to',
  70. ),
  71. 'content' => array('type' => 'text'), // got a better name?
  72. 'best' => array('type' => 'bool'),
  73. 'revisions' => array('type' => 'int'),
  74. 'profile_id' => array('type' => 'int'),
  75. 'created' => array('type' => 'datetime', 'not null' => true),
  76. ),
  77. 'primary key' => array('id'),
  78. 'unique keys' => array(
  79. 'qna_answer_uri_key' => array('uri'),
  80. 'qna_answer_question_id_profile_id_key' => array('question_id', 'profile_id'),
  81. ),
  82. 'indexes' => array(
  83. 'qna_answer_profile_id_question_id_idx' => array('profile_id', 'question_id'),
  84. )
  85. );
  86. }
  87. /**
  88. * Get an answer based on a notice
  89. *
  90. * @param Notice $notice Notice to check for
  91. *
  92. * @return QnA_Answer found response or null
  93. */
  94. public static function getByNotice($notice)
  95. {
  96. $answer = self::getKV('uri', $notice->uri);
  97. if (empty($answer)) {
  98. throw new Exception("No answer with URI {$notice->uri}");
  99. }
  100. return $answer;
  101. }
  102. /**
  103. * Get the notice that belongs to this answer
  104. *
  105. * @return Notice
  106. */
  107. public function getNotice()
  108. {
  109. return Notice::getKV('uri', $this->uri);
  110. }
  111. public static function fromNotice($notice)
  112. {
  113. return QnA_Answer::getKV('uri', $notice->uri);
  114. }
  115. public function getUrl()
  116. {
  117. return $this->getNotice()->getUrl();
  118. }
  119. /**
  120. * Get the Question this is an answer to
  121. *
  122. * @return QnA_Question
  123. */
  124. public function getQuestion()
  125. {
  126. $question = QnA_Question::getKV('id', $this->question_id);
  127. if (empty($question)) {
  128. // TRANS: Exception thown when getting a question with a non-existing ID.
  129. // TRANS: %s is the non-existing question ID.
  130. throw new Exception(sprintf(_m('No question with ID %s'), $this->question_id));
  131. }
  132. return $question;
  133. }
  134. public function getProfile()
  135. {
  136. $profile = Profile::getKV('id', $this->profile_id);
  137. if (empty($profile)) {
  138. // TRANS: Exception thown when getting a profile with a non-existing ID.
  139. // TRANS: %s is the non-existing profile ID.
  140. throw new Exception(sprintf(_m('No profile with ID %s'), $this->profile_id));
  141. }
  142. return $profile;
  143. }
  144. public function asHTML()
  145. {
  146. return self::toHTML(
  147. $this->getProfile(),
  148. $this->getQuestion(),
  149. $this
  150. );
  151. }
  152. public function asString()
  153. {
  154. return self::toString(
  155. $this->getProfile(),
  156. $this->getQuestion(),
  157. $this
  158. );
  159. }
  160. public static function toHTML($profile, $question, $answer)
  161. {
  162. $notice = $question->getNotice();
  163. $out = new XMLStringer();
  164. $cls = array('qna_answer');
  165. if (!empty($answer->best)) {
  166. $cls[] = 'best';
  167. }
  168. $out->elementStart('p', array('class' => implode(' ', $cls)));
  169. $out->elementStart('span', 'answer-content');
  170. $out->raw(common_render_text($answer->content));
  171. $out->elementEnd('span');
  172. if (!empty($answer->revisions)) {
  173. $out->elementstart('span', 'answer-revisions');
  174. $out->text(
  175. htmlspecialchars(
  176. // Notification of how often an answer was revised.
  177. // TRANS: %s is the number of answer revisions.
  178. sprintf(_m('%s revision', '%s revisions', $answer->revisions), $answer->revisions)
  179. )
  180. );
  181. $out->elementEnd('span');
  182. }
  183. $out->elementEnd('p');
  184. return $out->getString();
  185. }
  186. public static function toString($profile, $question, $answer)
  187. {
  188. // @todo FIXME: unused variable?
  189. $notice = $question->getNotice();
  190. return sprintf(
  191. // TRANS: Text for a question that was answered.
  192. // TRANS: %1$s is the user that answered, %2$s is the question title,
  193. // TRANS: %2$s is the answer content.
  194. _m('%1$s answered the question "%2$s": %3$s'),
  195. htmlspecialchars($profile->getBestName()),
  196. htmlspecialchars($question->title),
  197. htmlspecialchars($answer->content)
  198. );
  199. }
  200. /**
  201. * Save a new answer notice
  202. *
  203. * @param Profile $profile
  204. * @param Question $Question the question being answered
  205. * @param array
  206. *
  207. * @return Notice saved notice
  208. */
  209. public static function saveNew($profile, $question, $text, $options = null)
  210. {
  211. if (empty($options)) {
  212. $options = array();
  213. }
  214. $answer = new QnA_Answer();
  215. $answer->id = UUID::gen();
  216. $answer->profile_id = $profile->id;
  217. $answer->question_id = $question->id;
  218. $answer->revisions = 0;
  219. $answer->best = false;
  220. $answer->content = $text;
  221. $answer->created = common_sql_now();
  222. $answer->uri = common_local_url(
  223. 'qnashowanswer',
  224. array('id' => $answer->id)
  225. );
  226. common_log(LOG_DEBUG, "Saving answer: $answer->id, $answer->uri");
  227. $answer->insert();
  228. $content = sprintf(
  229. // TRANS: Text for a question that was answered.
  230. // TRANS: %s is the question title.
  231. _m('answered "%s"'),
  232. $question->title
  233. );
  234. $link = '<a href="' . htmlspecialchars($answer->uri) . '">' . htmlspecialchars($question->title) . '</a>';
  235. // TRANS: Rendered version of the notice content answering a question.
  236. // TRANS: %s a link to the question with question title as the link content.
  237. $rendered = sprintf(_m('answered "%s"'), $link);
  238. $tags = array();
  239. $replies = array();
  240. $options = array_merge(
  241. array(
  242. 'urls' => array(),
  243. 'content' => $content,
  244. 'rendered' => $rendered,
  245. 'tags' => $tags,
  246. 'replies' => $replies,
  247. 'reply_to' => $question->getNotice()->id,
  248. 'object_type' => self::OBJECT_TYPE
  249. ),
  250. $options
  251. );
  252. if (!array_key_exists('uri', $options)) {
  253. $options['uri'] = $answer->uri;
  254. }
  255. $saved = Notice::saveNew(
  256. $profile->id,
  257. $content,
  258. array_key_exists('source', $options) ?
  259. $options['source'] : 'web',
  260. $options
  261. );
  262. return $saved;
  263. }
  264. }