3 * StatusNet - the distributed open-source microblogging tool
4 * Copyright (C) 2011, StatusNet, Inc.
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.
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.
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/>.
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/
30 if (!defined('STATUSNET')) {
31 // This check helps protect against security problems;
32 // your code file can't be executed directly from the web.
41 * @author Zach Copley <zach@status.net>
42 * @copyright 2010 StatusNet, Inc.
43 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
44 * @link http://status.net/
46 class QnareviseanswerAction extends Action
48 protected $user = null;
49 protected $error = null;
50 protected $question = null;
51 protected $answer = null;
52 protected $content = null;
55 * Returns the title of the action
57 * @return string Action title
61 // TRANS: Page title for revising a question
62 return _m('Revise answer');
66 * For initializing members of the class.
68 * @param array $argarray misc. arguments
70 * @return boolean true
72 function prepare($argarray)
74 parent::prepare($argarray);
75 if ($this->boolean('ajax')) {
76 StatusNet::setApi(true);
79 $this->user = common_current_user();
81 if (empty($this->user)) {
82 throw new ClientException(
83 // TRANS: Client exception thrown trying to answer a question while not logged in.
84 _m("You must be logged in to answer to a question."),
89 $id = substr($this->trimmed('id'), 7);
91 $this->answer = QnA_Answer::getKV('id', $id);
92 $this->question = $this->answer->getQuestion();
94 if (empty($this->answer) || empty($this->question)) {
95 throw new ClientException(
96 // TRANS: Client exception thrown trying to respond to a non-existing question.
97 _m('Invalid or missing answer.'),
102 $this->answerText = $this->trimmed('answer');
110 * @param array $argarray is ignored since it's now passed in in prepare()
114 function handle($argarray=null)
116 parent::handle($argarray);
118 if ($this->isPost()) {
119 $this->checkSessionToken();
120 if ($this->arg('revise')) {
121 $this->showContent();
123 } else if ($this->arg('best')) {
124 if ($this->user->id == $this->question->profile_id) {
129 $this->reviseAnswer();
142 function reviseAnswer()
144 $answer = $this->answer;
147 $orig = clone($answer);
148 $answer->content = $this->answerText;
149 $answer->revisions++;
150 $result = $answer->update($orig);
151 } catch (ClientException $ce) {
152 $this->error = $ce->getMessage();
156 if ($this->boolean('ajax')) {
157 common_debug("ajaxy part");
158 $this->startHTML('text/xml;charset=utf-8');
159 $this->elementStart('head');
160 // TRANS: Page title after sending an answer.
161 $this->element('title', null, _m('Answer'));
162 $this->elementEnd('head');
163 $this->elementStart('body');
164 $form = new QnashowanswerForm($this, $answer);
166 $this->elementEnd('body');
169 common_redirect($this->answer->getUrl(), 303);
174 * Mark the answer as the "best" answer
180 $question = $this->question;
181 $answer = $this->answer;
184 // close the question to further answers
185 $orig = clone($question);
186 $question->closed = 1;
187 $result = $question->update($orig);
189 // mark this answer an the best answer
190 $orig = clone($answer);
192 $result = $answer->update($orig);
193 } catch (ClientException $ce) {
194 $this->error = $ce->getMessage();
198 if ($this->boolean('ajax')) {
199 common_debug("ajaxy part");
200 $this->startHTML('text/xml;charset=utf-8');
201 $this->elementStart('head');
202 // TRANS: Page title after sending an answer.
203 $this->element('title', null, _m('Answer'));
204 $this->elementEnd('head');
205 $this->elementStart('body');
206 $form = new QnashowanswerForm($this, $answer);
208 $this->elementEnd('body');
211 common_redirect($this->answer->getUrl(), 303);
216 * Show the revise answer form
220 function showContent()
222 if (!empty($this->error)) {
223 $this->element('p', 'error', $this->error);
226 if ($this->boolean('ajax')) {
227 $this->showAjaxReviseForm();
229 $form = new QnareviseanswerForm($this->answer, $this);
236 function showAjaxReviseForm()
238 $this->startHTML('text/xml;charset=utf-8');
239 $this->elementStart('head');
240 // TRANS: Form title for sending an answer.
241 $this->element('title', null, _m('TITLE','Answer'));
242 $this->elementEnd('head');
243 $this->elementStart('body');
244 $form = new QnareviseanswerForm($this->answer, $this);
246 $this->elementEnd('body');
251 * Return true if read only.
255 * @param array $args other arguments
257 * @return boolean is read only action?
259 function isReadOnly($args)
261 if ($_SERVER['REQUEST_METHOD'] == 'GET' ||
262 $_SERVER['REQUEST_METHOD'] == 'HEAD') {