QnA_Question.php 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. <?php
  2. /**
  3. * Data class to mark a notice as a question
  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 a question
  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_Question extends Managed_DataObject
  44. {
  45. const OBJECT_TYPE = 'http://activityschema.org/object/question';
  46. public $__table = 'qna_question'; // 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 $profile_id; // int -> profile.id
  50. public $title; // text
  51. public $description; // text
  52. public $closed; // int (boolean) whether a question is closed
  53. public $created; // datetime
  54. /**
  55. * The One True Thingy that must be defined and declared.
  56. */
  57. public static function schemaDef()
  58. {
  59. return array(
  60. 'description' => 'Per-notice question data for QNA plugin',
  61. 'fields' => array(
  62. 'id' => array(
  63. 'type' => 'char',
  64. 'length' => 36,
  65. 'not null' => true,
  66. 'description' => 'UUID'
  67. ),
  68. 'uri' => array(
  69. 'type' => 'varchar',
  70. 'length' => 191,
  71. 'not null' => true
  72. ),
  73. 'profile_id' => array('type' => 'int'),
  74. 'title' => array('type' => 'text'),
  75. 'closed' => array('type' => 'int', 'size' => 'tiny'),
  76. 'description' => array('type' => 'text'),
  77. 'created' => array(
  78. 'type' => 'datetime',
  79. 'not null' => true
  80. ),
  81. ),
  82. 'primary key' => array('id'),
  83. 'unique keys' => array(
  84. 'question_uri_key' => array('uri'),
  85. ),
  86. );
  87. }
  88. /**
  89. * Get a question based on a notice
  90. *
  91. * @param Notice $notice Notice to check for
  92. *
  93. * @return Question found question or null
  94. */
  95. static function getByNotice($notice)
  96. {
  97. return self::getKV('uri', $notice->uri);
  98. }
  99. function getNotice()
  100. {
  101. return Notice::getKV('uri', $this->uri);
  102. }
  103. function getUrl()
  104. {
  105. return $this->getNotice()->getUrl();
  106. }
  107. function getProfile()
  108. {
  109. $profile = Profile::getKV('id', $this->profile_id);
  110. if (empty($profile)) {
  111. // TRANS: Exception trown when getting a profile for a non-existing ID.
  112. // TRANS: %s is the provided profile ID.
  113. throw new Exception(sprintf(_m('No profile with ID %s'),$this->profile_id));
  114. }
  115. return $profile;
  116. }
  117. /**
  118. * Get the answer from a particular user to this question, if any.
  119. *
  120. * @param Profile $profile
  121. *
  122. * @return Answer object or null
  123. */
  124. function getAnswer(Profile $profile)
  125. {
  126. $a = new QnA_Answer();
  127. $a->question_id = $this->id;
  128. $a->profile_id = $profile->id;
  129. $a->find();
  130. if ($a->fetch()) {
  131. return $a;
  132. } else {
  133. return null;
  134. }
  135. }
  136. function getAnswers()
  137. {
  138. $a = new QnA_Answer();
  139. $a->question_id = $this->id;
  140. $cnt = $a->find();
  141. if (!empty($cnt)) {
  142. return $a;
  143. } else {
  144. return null;
  145. }
  146. }
  147. function countAnswers()
  148. {
  149. $a = new QnA_Answer();
  150. $a->question_id = $this->id;
  151. return $a->count();
  152. }
  153. static function fromNotice($notice)
  154. {
  155. return QnA_Question::getKV('uri', $notice->uri);
  156. }
  157. function asHTML()
  158. {
  159. return self::toHTML($this->getProfile(), $this);
  160. }
  161. function asString()
  162. {
  163. return self::toString($this->getProfile(), $this);
  164. }
  165. static function toHTML($profile, $question)
  166. {
  167. $notice = $question->getNotice();
  168. $out = new XMLStringer();
  169. $cls = array('qna_question');
  170. if (!empty($question->closed)) {
  171. $cls[] = 'closed';
  172. }
  173. $out->elementStart('p', array('class' => implode(' ', $cls)));
  174. if (!empty($question->description)) {
  175. $out->elementStart('span', 'question-description');
  176. $out->raw(common_render_text($question->description));
  177. $out->elementEnd('span');
  178. }
  179. $cnt = $question->countAnswers();
  180. if (!empty($cnt)) {
  181. $out->elementStart('span', 'answer-count');
  182. // TRANS: Number of given answers to a question.
  183. // TRANS: %s is the number of given answers.
  184. $out->text(sprintf(_m('%s answer','%s answers',$cnt), $cnt));
  185. $out->elementEnd('span');
  186. }
  187. if (!empty($question->closed)) {
  188. $out->elementStart('span', 'question-closed');
  189. // TRANS: Notification that a question cannot be answered anymore because it is closed.
  190. $out->text(_m('This question is closed.'));
  191. $out->elementEnd('span');
  192. }
  193. $out->elementEnd('p');
  194. return $out->getString();
  195. }
  196. static function toString($profile, $question, $answers)
  197. {
  198. return sprintf(htmlspecialchars($question->description));
  199. }
  200. /**
  201. * Save a new question notice
  202. *
  203. * @param Profile $profile
  204. * @param string $question
  205. * @param string $title
  206. * @param string $description
  207. * @param array $option // and whatnot
  208. *
  209. * @return Notice saved notice
  210. */
  211. static function saveNew($profile, $title, $description, $options = array())
  212. {
  213. $q = new QnA_Question();
  214. $q->id = UUID::gen();
  215. $q->profile_id = $profile->id;
  216. $q->title = $title;
  217. $q->description = $description;
  218. if (array_key_exists('created', $options)) {
  219. $q->created = $options['created'];
  220. } else {
  221. $q->created = common_sql_now();
  222. }
  223. if (array_key_exists('uri', $options)) {
  224. $q->uri = $options['uri'];
  225. } else {
  226. $q->uri = common_local_url(
  227. 'qnashowquestion',
  228. array('id' => $q->id)
  229. );
  230. }
  231. common_log(LOG_DEBUG, "Saving question: $q->id $q->uri");
  232. $q->insert();
  233. if (Notice::contentTooLong($q->title . ' ' . $q->uri)) {
  234. $max = Notice::maxContent();
  235. $uriLen = mb_strlen($q->uri);
  236. $targetLen = $max - ($uriLen + 15);
  237. $title = mb_substr($q->title, 0, $targetLen) . '…';
  238. }
  239. $content = $title . ' ' . $q->uri;
  240. $link = '<a href="' . htmlspecialchars($q->uri) . '">' . htmlspecialchars($q->title) . '</a>';
  241. // TRANS: Rendered version of the notice content creating a question.
  242. // TRANS: %s a link to the question as link description.
  243. $rendered = sprintf(_m('Question: %s'), $link);
  244. $tags = array('question');
  245. $replies = array();
  246. $options = array_merge(
  247. array(
  248. 'urls' => array(),
  249. 'rendered' => $rendered,
  250. 'tags' => $tags,
  251. 'replies' => $replies,
  252. 'object_type' => self::OBJECT_TYPE
  253. ),
  254. $options
  255. );
  256. if (!array_key_exists('uri', $options)) {
  257. $options['uri'] = $q->uri;
  258. }
  259. $saved = Notice::saveNew(
  260. $profile->id,
  261. $content,
  262. array_key_exists('source', $options) ?
  263. $options['source'] : 'web',
  264. $options
  265. );
  266. return $saved;
  267. }
  268. }