QnA - Allow closing questions

This commit is contained in:
Zach Copley 2011-04-04 14:33:20 -07:00
parent e6fd3fa038
commit 5193afb8bf
7 changed files with 395 additions and 67 deletions

View File

@ -81,18 +81,19 @@ class QnAPlugin extends MicroAppPlugin
case 'QnanewquestionAction': case 'QnanewquestionAction':
case 'QnanewanswerAction': case 'QnanewanswerAction':
case 'QnashowquestionAction': case 'QnashowquestionAction':
case 'QnaclosequestionAction':
case 'QnashowanswerAction': case 'QnashowanswerAction':
case 'QnareviseanswerAction': case 'QnareviseanswerAction':
case 'QnavoteAction': case 'QnavoteAction':
include_once $dir . '/actions/' include_once $dir . '/actions/'
. strtolower(mb_substr($cls, 0, -6)) . '.php'; . strtolower(mb_substr($cls, 0, -6)) . '.php';
return false; return false;
case 'QnaquestionForm': case 'QnanewquestionForm':
case 'QnashowanswerForm': case 'QnashowquestionForm':
case 'QnanewanswerForm': case 'QnanewanswerForm':
case 'QnashowanswerForm':
case 'QnareviseanswerForm': case 'QnareviseanswerForm':
case 'QnavoteForm': case 'QnavoteForm':
case 'AnswerNoticeListItem':
include_once $dir . '/lib/' . strtolower($cls).'.php'; include_once $dir . '/lib/' . strtolower($cls).'.php';
break; break;
case 'QnA_Question': case 'QnA_Question':
@ -122,6 +123,10 @@ class QnAPlugin extends MicroAppPlugin
'main/qna/newquestion', 'main/qna/newquestion',
array('action' => 'qnanewquestion') array('action' => 'qnanewquestion')
); );
$m->connect(
'answer/qna/closequestion',
array('action' => 'qnaclosequestion')
);
$m->connect( $m->connect(
'main/qna/newanswer', 'main/qna/newanswer',
array('action' => 'qnanewanswer') array('action' => 'qnanewanswer')
@ -384,24 +389,20 @@ class QnAPlugin extends MicroAppPlugin
$question = QnA_Question::getByNotice($notice); $question = QnA_Question::getByNotice($notice);
if (!empty($question)) { if (!empty($question)) {
if (empty($user)) {
$short = $this->shorten($question->description, $notice); $form = new QnashowquestionForm($out, $question);
$out->raw($short); $form->show();
} else {
// Don't prompt user for an answer if the question is closed or
// the current user posed the question in the first place
if (empty($question->closed)) {
if (!empty($user)) {
$profile = $user->getProfile(); $profile = $user->getProfile();
$answer = $question->getAnswer($profile); $answer = $question->getAnswer($profile);
if (!$answer) { if (empty($answer)) {
$form = new QnanewanswerForm($question, $out); $form = new QnanewanswerForm($out, $question);
$form->show();
} else {
$form = new QnashowquestionForm($out, $question);
$form->show(); $form->show();
} }
} }
} else {
$out->element('span', 'closed', _m('This question is closed.'));
}
} else { } else {
$out->text(_m('Question data is missing.')); $out->text(_m('Question data is missing.'));
} }
@ -465,7 +466,7 @@ class QnAPlugin extends MicroAppPlugin
function entryForm($out) function entryForm($out)
{ {
return new QnaquestionForm($out); return new QnanewquestionForm($out);
} }
/** /**

View File

@ -0,0 +1,200 @@
<?php
/**
* StatusNet - the distributed open-source microblogging tool
* Copyright (C) 2011, StatusNet, Inc.
*
* Close a question to further answers
*
* PHP version 5
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* @category QnA
* @package StatusNet
* @author Zach Copley <zach@status.net>
* @copyright 2011 StatusNet, Inc.
* @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
* @link http://status.net/
*/
if (!defined('STATUSNET')) {
// This check helps protect against security problems;
// your code file can't be executed directly from the web.
exit(1);
}
/**
* Close a question to new answers
*
* @category QnA
* @package StatusNet
* @author Zach Copley <zach@status.net>
* @copyright 2010 StatusNet, Inc.
* @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
* @link http://status.net/
*/
class QnaclosequestionAction extends Action
{
protected $user = null;
protected $error = null;
protected $complete = null;
protected $question = null;
protected $answer = null;
/**
* Returns the title of the action
*
* @return string Action title
*/
function title()
{
// TRANS: Page title for close a question
return _m('Close question');
}
/**
* For initializing members of the class.
*
* @param array $argarray misc. arguments
*
* @return boolean true
*/
function prepare($argarray)
{
parent::prepare($argarray);
if ($this->boolean('ajax')) {
StatusNet::setApi(true);
}
$this->user = common_current_user();
if (empty($this->user)) {
// TRANS: Client exception thrown trying to close a question when not logged in
throw new ClientException(
_m("You must be logged in to close a question."),
403
);
}
if ($this->isPost()) {
$this->checkSessionToken();
}
$id = substr($this->trimmed('id'), 9);
$this->question = QnA_Question::staticGet('id', $id);
if (empty($this->question)) {
// TRANS: Client exception thrown trying to respond to a non-existing question.
throw new ClientException(_m('Invalid or missing question.'), 404);
}
return true;
}
/**
* Handler method
*
* @param array $argarray is ignored since it's now passed in in prepare()
*
* @return void
*/
function handle($argarray=null)
{
parent::handle($argarray);
if ($this->isPost()) {
$this->closeQuestion();
} else {
$this->showPage();
}
return;
}
/**
* Close a question
*
* @return void
*/
function closeQuestion()
{
$user = common_current_user();
try {
if ($user->id != $this->question->profile_id) {
throw new Exception(_m('You didn\'t ask this question.'));
}
$orig = clone($this->question);
$this->question->closed = 1;
$this->question->update($orig);
} catch (ClientException $ce) {
$this->error = $ce->getMessage();
$this->showPage();
return;
}
if ($this->boolean('ajax')) {
header('Content-Type: text/xml;charset=utf-8');
$this->xw->startDocument('1.0', 'UTF-8');
$this->elementStart('html');
$this->elementStart('head');
// TRANS: Page title after sending an answer.
$this->element('title', null, _m('Answers'));
$this->elementEnd('head');
$this->elementStart('body');
$form = new QnashowquestionForm($this, $this->question);
$form->show();
$this->elementEnd('body');
$this->elementEnd('html');
} else {
common_redirect($this->question->bestUrl(), 303);
}
}
/**
* Show the close question form
*
* @return void
*/
function showContent()
{
if (!empty($this->error)) {
$this->element('p', 'error', $this->error);
}
// blar
}
/**
* Return true if read only.
*
* MAY override
*
* @param array $args other arguments
*
* @return boolean is read only action?
*/
function isReadOnly($args)
{
if ($_SERVER['REQUEST_METHOD'] == 'GET' ||
$_SERVER['REQUEST_METHOD'] == 'HEAD') {
return true;
} else {
return false;
}
}
}

View File

@ -201,66 +201,31 @@ class QnA_Question extends Managed_DataObject
function asHTML() function asHTML()
{ {
return self::toHTML( return self::toHTML($this->getProfile(), $this);
$this->getProfile(),
$this,
$this->getAnswers()
);
} }
function asString() function asString()
{ {
return self::toString( return self::toString($this->getProfile(), $this);
$this->getProfile(),
$this,
$this->getAnswers()
);
} }
static function toHTML($profile, $question, $answer) static function toHTML($profile, $question)
{ {
$notice = $question->getNotice(); $notice = $question->getNotice();
$fmt = '<p class="qna_question">'; $fmt = '<span class="question_description">%s</span>';
$fmt .= '<span class="question_title"><a href="%1$s">%2$s</a></span>';
$fmt .= '<span class="question_description">%3$s</span>';
$fmt .= '<span class="question_author">asked by <a href="%4$s">%5$s</a></span>';
$fmt .= '</p>';
$q = sprintf( $q = sprintf(
$fmt, $fmt,
htmlspecialchars($notice->bestUrl()), htmlspecialchars($question->description)
htmlspecialchars($question->title),
htmlspecialchars($question->description),
htmlspecialchars($profile->profileurl),
htmlspecialchars($profile->getBestName())
); );
$ans = array(); return $q;
$ans[] = '<div class="qna_answers">';
while($answer->fetch()) {
$ans[] = $answer->asHTML();
}
$ans[] .= '</div>';
return $q . implode($ans);
} }
static function toString($profile, $question, $answers) static function toString($profile, $question, $answers)
{ {
$fmt = _m( return sprintf(htmlspecialchars($question->description));
'%1$s asked the question "%2$s": %3$s'
);
return sprintf(
$fmt,
htmlspecialchars($profile->getBestName()),
htmlspecialchars($question->title),
htmlspecialchars($question->description)
);
} }
/** /**

View File

@ -56,7 +56,7 @@ class QnanewanswerForm extends Form
* *
* @return void * @return void
*/ */
function __construct(QnA_Question $question, HTMLOutputter $out) function __construct(HTMLOutputter $out, QnA_Question $question)
{ {
parent::__construct($out); parent::__construct($out);
$this->question = $question; $this->question = $question;
@ -103,6 +103,8 @@ class QnanewanswerForm extends Form
$out = $this->out; $out = $this->out;
$id = "question-" . $question->id; $id = "question-" . $question->id;
$out->raw($this->question->asHTML());
$out->element('p', 'answer', 'Your answer'); $out->element('p', 'answer', 'Your answer');
$out->hidden('id', $id); $out->hidden('id', $id);
$out->textarea('answer', 'answer'); $out->textarea('answer', 'answer');

View File

@ -44,7 +44,7 @@ if (!defined('STATUSNET')) {
* @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
* @link http://status.net/ * @link http://status.net/
*/ */
class QnaquestionForm extends Form class QnanewquestionForm extends Form
{ {
protected $title; protected $title;
protected $description; protected $description;

View File

@ -124,6 +124,7 @@ class QnashowanswerForm extends Form
'id', 'id',
'revise-' . $this->answer->id 'revise-' . $this->answer->id
); );
$this->out->raw($this->answer->asHTML()); $this->out->raw($this->answer->asHTML());
} }
@ -141,7 +142,6 @@ class QnashowanswerForm extends Form
if (empty($this->question->closed)) { if (empty($this->question->closed)) {
if ($user->id == $this->question->profile_id) { if ($user->id == $this->question->profile_id) {
common_debug("I am the question asker!");
if (empty($this->answer->best)) { if (empty($this->answer->best)) {
$this->out->submit( $this->out->submit(
'best', 'best',

View File

@ -0,0 +1,160 @@
<?php
/**
* StatusNet, the distributed open-source microblogging tool
*
* Form for showing / revising an answer
*
* PHP version 5
*
* LICENCE: This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* @category Form
* @package StatusNet
* @author Zach Copley <zach@status.net>
* @copyright 2011 StatusNet, Inc.
* @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
* @link http://status.net/
*/
if (!defined('STATUSNET')) {
exit(1);
}
require_once INSTALLDIR . '/lib/form.php';
/**
* Form for showing a question
*
* @category Form
* @package StatusNet
* @author Zach Copley <zach@status.net>
* @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
* @link http://status.net/
*
*/
class QnashowquestionForm extends Form
{
/**
* The question to show
*/
var $question = null;
/**
* Constructor
*
* @param HTMLOutputter $out output channel
* @param QnA_Question $question the question to show
*/
function __construct($out = null, $question = null)
{
parent::__construct($out);
$this->question = $question;
}
/**
* ID of the form
*
* @return int ID of the form
*/
function id()
{
return 'question-' . $this->question->id;
}
/**
* Action of the form
*
* @return string URL of the action
*/
function action()
{
return common_local_url('qnaclosequestion');
}
/**
* Include a session token for CSRF protection
*
* @return void
*/
function sessionToken()
{
$this->out->hidden(
'token',
common_session_token()
);
}
/**
* Legend of the Form
*
* @return void
*/
function formLegend()
{
// TRANS: Form legend for revising the answer.
$this->out->element('legend', null, _('Question'));
}
/**
* Data elements
*
* @return void
*/
function formData()
{
$this->out->hidden(
'id',
'question-' . $this->question->id
);
$this->out->raw($this->question->asHTML());
}
/**
* Action elements
*
* @return void
*/
function formActions()
{
$user = common_current_user();
if (empty($user)) {
return;
}
if (empty($this->question->closed)) {
if ($user->id == $this->question->profile_id) {
$this->out->submit(
'close',
// TRANS: Button text for closing a question
_m('BUTTON', 'Close'),
'submit',
null,
// TRANS: Title for button text for closing a question
_('Close the question')
);
}
}
}
/**
* Class of the form.
*
* @return string the form's class
*/
function formClass()
{
return 'form_close ajax';
}
}