QnA_Answer.php 9.4 KB

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