qnareviseanswer.php 7.4 KB

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