qnareviseanswer.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. <?php
  2. /**
  3. * StatusNet - the distributed open-source microblogging tool
  4. * Copyright (C) 2011, StatusNet, Inc.
  5. *
  6. * Revise an answer
  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. * Revise an answer
  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 QnareviseanswerAction extends Action
  46. {
  47. protected $user = null;
  48. protected $error = null;
  49. protected $question = null;
  50. protected $answer = 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 revising a question
  60. return _m('Revise 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. $this->user = common_current_user();
  77. if (empty($this->user)) {
  78. throw new ClientException(
  79. // TRANS: Client exception thrown trying to answer a question while not logged in.
  80. _m("You must be logged in to answer to a question."),
  81. 403
  82. );
  83. }
  84. $id = substr($this->trimmed('id'), 7);
  85. $this->answer = QnA_Answer::getKV('id', $id);
  86. $this->question = $this->answer->getQuestion();
  87. if (empty($this->answer) || empty($this->question)) {
  88. throw new ClientException(
  89. // TRANS: Client exception thrown trying to respond to a non-existing question.
  90. _m('Invalid or missing answer.'),
  91. 404
  92. );
  93. }
  94. $this->answerText = $this->trimmed('answer');
  95. return true;
  96. }
  97. /**
  98. * Handler method
  99. *
  100. * @return void
  101. */
  102. function handle()
  103. {
  104. parent::handle();
  105. if ($this->isPost()) {
  106. $this->checkSessionToken();
  107. if ($this->arg('revise')) {
  108. $this->showContent();
  109. return;
  110. } else if ($this->arg('best')) {
  111. if ($this->user->id == $this->question->profile_id) {
  112. $this->markBest();
  113. return;
  114. }
  115. } else {
  116. $this->reviseAnswer();
  117. return;
  118. }
  119. }
  120. $this->showPage();
  121. }
  122. /**
  123. * Show the revise answer form
  124. *
  125. * @return void
  126. */
  127. function showContent()
  128. {
  129. if (!empty($this->error)) {
  130. $this->element('p', 'error', $this->error);
  131. }
  132. if ($this->boolean('ajax')) {
  133. $this->showAjaxReviseForm();
  134. } else {
  135. $form = new QnareviseanswerForm($this->answer, $this);
  136. $form->show();
  137. }
  138. return;
  139. }
  140. function showAjaxReviseForm()
  141. {
  142. $this->startHTML('text/xml;charset=utf-8');
  143. $this->elementStart('head');
  144. // TRANS: Form title for sending an answer.
  145. $this->element('title', null, _m('TITLE', 'Answer'));
  146. $this->elementEnd('head');
  147. $this->elementStart('body');
  148. $form = new QnareviseanswerForm($this->answer, $this);
  149. $form->show();
  150. $this->elementEnd('body');
  151. $this->endHTML();
  152. }
  153. /**
  154. * Mark the answer as the "best" answer
  155. *
  156. * @return void
  157. */
  158. function markBest()
  159. {
  160. $question = $this->question;
  161. $answer = $this->answer;
  162. try {
  163. // close the question to further answers
  164. $orig = clone($question);
  165. $question->closed = 1;
  166. $result = $question->update($orig);
  167. // mark this answer an the best answer
  168. $orig = clone($answer);
  169. $answer->best = 1;
  170. $result = $answer->update($orig);
  171. } catch (ClientException $ce) {
  172. $this->error = $ce->getMessage();
  173. $this->showPage();
  174. return;
  175. }
  176. if ($this->boolean('ajax')) {
  177. common_debug("ajaxy part");
  178. $this->startHTML('text/xml;charset=utf-8');
  179. $this->elementStart('head');
  180. // TRANS: Page title after sending an answer.
  181. $this->element('title', null, _m('Answer'));
  182. $this->elementEnd('head');
  183. $this->elementStart('body');
  184. $form = new QnashowanswerForm($this, $answer);
  185. $form->show();
  186. $this->elementEnd('body');
  187. $this->endHTML();
  188. } else {
  189. common_redirect($this->answer->getUrl(), 303);
  190. }
  191. }
  192. /**
  193. * Revise the answer
  194. *
  195. * @return void
  196. */
  197. function reviseAnswer()
  198. {
  199. $answer = $this->answer;
  200. try {
  201. $orig = clone($answer);
  202. $answer->content = $this->answerText;
  203. $answer->revisions++;
  204. $result = $answer->update($orig);
  205. } catch (ClientException $ce) {
  206. $this->error = $ce->getMessage();
  207. $this->showPage();
  208. return;
  209. }
  210. if ($this->boolean('ajax')) {
  211. common_debug("ajaxy part");
  212. $this->startHTML('text/xml;charset=utf-8');
  213. $this->elementStart('head');
  214. // TRANS: Page title after sending an answer.
  215. $this->element('title', null, _m('Answer'));
  216. $this->elementEnd('head');
  217. $this->elementStart('body');
  218. $form = new QnashowanswerForm($this, $answer);
  219. $form->show();
  220. $this->elementEnd('body');
  221. $this->endHTML();
  222. } else {
  223. common_redirect($this->answer->getUrl(), 303);
  224. }
  225. }
  226. /**
  227. * Return true if read only.
  228. *
  229. * MAY override
  230. *
  231. * @param array $args other arguments
  232. *
  233. * @return boolean is read only action?
  234. */
  235. function isReadOnly($args)
  236. {
  237. if ($_SERVER['REQUEST_METHOD'] == 'GET' ||
  238. $_SERVER['REQUEST_METHOD'] == 'HEAD') {
  239. return true;
  240. } else {
  241. return false;
  242. }
  243. }
  244. }