qnanewanswer.php 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. <?php
  2. /**
  3. * StatusNet - the distributed open-source microblogging tool
  4. * Copyright (C) 2011, StatusNet, Inc.
  5. *
  6. * Answer a question
  7. *
  8. * PHP version 5
  9. *
  10. * This program is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License as published by
  12. * the Free Software Foundation, either version 3 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. *
  23. * @category QnA
  24. * @package StatusNet
  25. * @author Zach Copley <zach@status.net>
  26. * @copyright 2011 StatusNet, Inc.
  27. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
  28. * @link http://status.net/
  29. */
  30. if (!defined('STATUSNET')) {
  31. // This check helps protect against security problems;
  32. // your code file can't be executed directly from the web.
  33. exit(1);
  34. }
  35. /**
  36. * Answer a question
  37. *
  38. * @category QnA
  39. * @package StatusNet
  40. * @author Zach Copley <zach@status.net>
  41. * @copyright 2010 StatusNet, Inc.
  42. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
  43. * @link http://status.net/
  44. */
  45. class QnanewanswerAction extends Action
  46. {
  47. public $question = null;
  48. protected $user = null;
  49. protected $error = null;
  50. protected $complete = null;
  51. protected $content = null;
  52. /**
  53. * Returns the title of the action
  54. *
  55. * @return string Action title
  56. */
  57. function title()
  58. {
  59. // TRANS: Page title for and answer to a question.
  60. return _m('Answer');
  61. }
  62. /**
  63. * For initializing members of the class.
  64. *
  65. * @param array $args misc. arguments
  66. *
  67. * @return boolean true
  68. * @throws ClientException
  69. */
  70. function prepare(array $args = [])
  71. {
  72. parent::prepare($args);
  73. if ($this->boolean('ajax')) {
  74. GNUsocial::setApi(true);
  75. }
  76. common_debug("in qnanewanswer");
  77. $this->user = common_current_user();
  78. if (empty($this->user)) {
  79. throw new ClientException(
  80. // TRANS: Client exception thrown trying to answer a question while not logged in.
  81. _m("You must be logged in to answer to a question."),
  82. 403
  83. );
  84. }
  85. if ($this->isPost()) {
  86. $this->checkSessionToken();
  87. }
  88. $id = substr($this->trimmed('id'), 9);
  89. $this->question = QnA_Question::getKV('id', $id);
  90. if (empty($this->question)) {
  91. throw new ClientException(
  92. // TRANS: Client exception thrown trying to respond to a non-existing question.
  93. _m('Invalid or missing question.'),
  94. 404
  95. );
  96. }
  97. $this->answerText = $this->trimmed('answer');
  98. return true;
  99. }
  100. /**
  101. * Handler method
  102. *
  103. * @return void
  104. */
  105. function handle()
  106. {
  107. parent::handle();
  108. if ($this->isPost()) {
  109. $this->newAnswer();
  110. } else {
  111. $this->showForm();
  112. }
  113. return;
  114. }
  115. /**
  116. * Add a new answer
  117. *
  118. * @return void
  119. */
  120. function newAnswer()
  121. {
  122. $profile = $this->user->getProfile();
  123. try {
  124. $notice = QnA_Answer::saveNew(
  125. $profile,
  126. $this->question,
  127. $this->answerText
  128. );
  129. } catch (ClientException $ce) {
  130. $this->error = $ce->getMessage();
  131. $this->showForm($this->error);
  132. return;
  133. }
  134. if ($this->boolean('ajax')) {
  135. common_debug("ajaxy part");
  136. $answer = $this->question->getAnswer($profile);
  137. $this->startHTML('text/xml;charset=utf-8');
  138. $this->elementStart('head');
  139. // TRANS: Page title after sending an answer.
  140. $this->element('title', null, _m('Answers'));
  141. $this->elementEnd('head');
  142. $this->elementStart('body');
  143. $nli = new NoticeAnswerListItem($notice, $this, $this->question, $answer);
  144. $nli->show();
  145. $this->elementEnd('body');
  146. $this->endHTML();
  147. } else {
  148. common_debug("not ajax");
  149. common_redirect($this->question->getUrl(), 303);
  150. }
  151. }
  152. /**
  153. * @param string $msg An error message, if any
  154. *
  155. * @return void
  156. */
  157. function showForm($msg = null)
  158. {
  159. common_debug("show form - msg = $msg");
  160. if ($this->boolean('ajax')) {
  161. if ($msg) {
  162. $this->ajaxErrorMsg($msg);
  163. } else {
  164. $this->ajaxShowForm();
  165. }
  166. return;
  167. }
  168. $this->msg = $msg;
  169. $this->showPage();
  170. }
  171. /**
  172. * Show an Ajax-y error message
  173. *
  174. * Goes back to the browser, where it's shown in a popup.
  175. *
  176. * @param string $msg Message to show
  177. *
  178. * @return void
  179. */
  180. function ajaxErrorMsg($msg)
  181. {
  182. $this->startHTML('text/xml;charset=utf-8', true);
  183. $this->elementStart('head');
  184. // TRANS: Page title after an AJAX error occurs on the post answer page.
  185. $this->element('title', null, _m('Ajax Error'));
  186. $this->elementEnd('head');
  187. $this->elementStart('body');
  188. $this->element('p', array('id' => 'error'), $msg);
  189. $this->elementEnd('body');
  190. $this->endHTML();
  191. }
  192. /**
  193. * Show an Ajax-y answer form
  194. *
  195. * Goes back to the browser, where it's shown in a popup.
  196. *
  197. * @param string $msg Message to show
  198. *
  199. * @return void
  200. */
  201. function ajaxShowForm()
  202. {
  203. common_debug('ajaxShowForm()');
  204. $this->startHTML('text/xml;charset=utf-8', true);
  205. $this->elementStart('head');
  206. // TRANS: Title for form to send answer to a question.
  207. $this->element('title', null, _m('TITLE', 'Your answer'));
  208. $this->elementEnd('head');
  209. $this->elementStart('body');
  210. $form = new QnanewanswerForm($this, $this->question);
  211. $form->show();
  212. $this->elementEnd('body');
  213. $this->endHTML();
  214. }
  215. /**
  216. * Show the Answer form
  217. *
  218. * @return void
  219. */
  220. function showContent()
  221. {
  222. if (!empty($this->error)) {
  223. $this->element('p', 'error', $this->error);
  224. }
  225. $form = new QnanewanswerForm($this->question, $this);
  226. $form->show();
  227. return;
  228. }
  229. /**
  230. * Return true if read only.
  231. *
  232. * MAY override
  233. *
  234. * @param array $args other arguments
  235. *
  236. * @return boolean is read only action?
  237. */
  238. function isReadOnly($args)
  239. {
  240. if ($_SERVER['REQUEST_METHOD'] == 'GET' ||
  241. $_SERVER['REQUEST_METHOD'] == 'HEAD') {
  242. return true;
  243. } else {
  244. return false;
  245. }
  246. }
  247. }
  248. class NoticeAnswerListItem extends NoticeListItem
  249. {
  250. protected $question;
  251. protected $answer;
  252. /**
  253. * constructor
  254. *
  255. * Also initializes the profile attribute.
  256. *
  257. * @param Notice $notice The notice we'll display
  258. * @param $out
  259. * @param $question
  260. * @param $answer
  261. */
  262. function __construct($notice, $out, $question, $answer)
  263. {
  264. parent::__construct($notice, $out);
  265. $this->question = $question;
  266. $this->answer = $answer;
  267. }
  268. function show()
  269. {
  270. if (empty($this->notice)) {
  271. common_log(LOG_WARNING, "Trying to show missing notice; skipping.");
  272. return;
  273. } else if (empty($this->profile)) {
  274. common_log(LOG_WARNING, "Trying to show missing profile (" . $this->notice->profile_id . "); skipping.");
  275. return;
  276. }
  277. $this->showStart();
  278. $this->showNotice();
  279. $this->showNoticeInfo();
  280. $notice = $this->question->getNotice();
  281. $this->out->hidden('inreplyto', $notice->id);
  282. $this->showEnd();
  283. }
  284. /**
  285. * show the content of the notice
  286. *
  287. * Shows the content of the notice. This is pre-rendered for efficiency
  288. * at save time. Some very old notices might not be pre-rendered, so
  289. * they're rendered on the spot.
  290. *
  291. * @return void
  292. */
  293. function showContent()
  294. {
  295. $this->out->elementStart('p', array('class' => 'e-content answer-content'));
  296. $this->out->raw($this->notice->getRendered());
  297. if (!empty($this->answer)) {
  298. $form = new QnashowanswerForm($this->out, $this->answer);
  299. $form->show();
  300. } else {
  301. // TRANS: Error message displayed when an answer has no content.
  302. $this->out->text(_m('Answer data is missing.'));
  303. }
  304. $this->out->elementEnd('p');
  305. }
  306. }