Work on QnA notice display -- in progress

This commit is contained in:
Zach Copley 2011-03-21 20:57:19 -07:00
parent b0ed4cb89a
commit 7f4bd6b69f
7 changed files with 298 additions and 146 deletions

View File

@ -88,7 +88,8 @@ class QnAPlugin extends MicroAppPlugin
return false; return false;
case 'QnaquestionForm': case 'QnaquestionForm':
case 'QnaanswerForm': case 'QnaanswerForm':
case 'QnavoteForm'; case 'QnaansweredForm':
case 'QnavoteForm':
include_once $dir . '/lib/' . strtolower($cls).'.php'; include_once $dir . '/lib/' . strtolower($cls).'.php';
break; break;
case 'QnA_Question': case 'QnA_Question':
@ -201,24 +202,23 @@ class QnAPlugin extends MicroAppPlugin
switch ($activity->verb) { switch ($activity->verb) {
case ActivityVerb::POST: case ActivityVerb::POST:
$notice = Question::saveNew( $notice = QnA_Question::saveNew(
$actor, $actor,
$questionObj->title $questionObj->title,
// null, $questionObj->summary,
// $questionObj->summary, $options
// $options
); );
break; break;
case Answer::NORMAL: case Answer::ObjectType:
$question = QnA_Question::staticGet('uri', $questionObj->id); $question = QnA_Question::staticGet('uri', $questionObj->id);
if (empty($question)) { if (empty($question)) {
// FIXME: save the question // FIXME: save the question
throw new Exception("Answer to unknown question."); throw new Exception("Answer to unknown question.");
} }
$notice = QnA_Answer::saveNew($actor, $question, $activity->verb, $options); $notice = QnA_Answer::saveNew($actor, $question, $options);
break; break;
default: default:
throw new Exception("Unknown verb for question"); throw new Exception("Unknown object type received by QnA Plugin");
} }
return $notice; return $notice;
@ -292,127 +292,67 @@ class QnAPlugin extends MicroAppPlugin
* @param Notice $notice * @param Notice $notice
* @param HTMLOutputter $out * @param HTMLOutputter $out
*/ */
function showNotice($notice, $out) function showNotice($notice, $out)
{ {
switch ($notice->object_type) { switch ($notice->object_type) {
case QnA_Question::OBJECT_TYPE: case QnA_Question::OBJECT_TYPE:
$this->showQuestionNotice($notice, $out); return $this->showNoticeQuestion($notice, $out);
break;
case QnA_Answer::OBJECT_TYPE: case QnA_Answer::OBJECT_TYPE:
$this->showAnswerNotice($notice, $out); return $this->showNoticeAnswer($notice, $out);
break; default:
} // TRANS: Exception thrown when performing an unexpected action on a question.
// TRANS: %s is the unpexpected object type.
// bad craziness throw new Exception(
$out->elementStart('div', array('class' => 'question')); sprintf(
_m('Unexpected type for QnA plugin: %s.'),
$profile = $notice->getProfile(); $notice->object_type
$avatar = $profile->getAvatar(AVATAR_MINI_SIZE);
$out->element(
'img',
array(
'src' => ($avatar)
? $avatar->displayUrl()
: Avatar::defaultImage(AVATAR_MINI_SIZE),
'class' => 'avatar photo question-avatar',
'width' => AVATAR_MINI_SIZE,
'height' => AVATAR_MINI_SIZE,
'alt' => $profile->getBestName()
) )
); );
}
$out->raw(' '); // avoid   for AJAX XML compatibility
// hack for belongsOnTimeline; JS needs to be able to find the author
$out->elementStart('span', 'vcard author');
$out->element(
'a',
array(
'class' => 'url',
'href' => $profile->profileurl,
'title' => $profile->getBestName()
),
$profile->nickname
);
$out->elementEnd('span');
} }
function showAnswerNotice($notice, $out) function showNoticeQuestion($notice, $out)
{ {
$answer = QnA_Answer::fromNotice($notice);
assert(!empty($answer));
$out->elementStart('div', 'answer');
$out->raw($answer->asHTML());
$out->elementEnd('div');
}
function showQuestionNotice($notice, $out)
{
$profile = $notice->getProfile();
$question = QnA_Question::fromNotice($notice);
assert(!empty($question));
assert(!empty($profile));
$out->elementStart('div', 'question-notice');
$out->elementStart('h3');
if (!empty($question->url)) {
$out->element(
'a',
array(
'href' => $question->url,
'class' => 'question-title'
),
$question->title
);
} else {
$out->text($question->title);
}
if (!empty($question->location)) {
$out->elementStart('div', 'question-location');
$out->element('strong', null, _('Location: '));
$out->element('span', 'location', $question->location);
$out->elementEnd('div');
}
if (!empty($question->description)) {
$out->elementStart('div', 'question-description');
$out->element('strong', null, _('Description: '));
$out->element('span', 'description', $question->description);
$out->elementEnd('div');
}
//$answers = $question->getAnswers();
$out->elementStart('div', 'question-answers');
$out->element('strong', null, _('Answer: '));
$out->element('span', 'question-answer');
$out->elementEnd('div');
$user = common_current_user(); $user = common_current_user();
if (!empty($user)) { // @hack we want regular rendering, then just add stuff after that
$nli = new NoticeListItem($notice, $out);
$nli->showNotice();
$answer = $question->getAnswer($user->getProfile()); $out->elementStart('div', array('class' => 'entry-content question-content'));
$question = QnA_Question::getByNotice($notice);
if (empty($answer)) { if ($question) {
if ($user) {
$profile = $user->getProfile();
$answer = $question->getAnswer($profile);
if ($answer) {
// User has already answer; show the results.
$form = new QnaansweredForm($answer, $out);
} else {
$form = new QnaanswerForm($question, $out); $form = new QnaanswerForm($question, $out);
}
$form->show(); $form->show();
} }
} else {
$out->text(_m('Question data is missing'));
}
$out->elementEnd('div');
// @fixme
$out->elementStart('div', array('class' => 'entry-content'));
} }
$out->elementEnd('div'); function showNoticeAnswer($notice, $out)
{
$user = common_current_user();
// @hack we want regular rendering, then just add stuff after that
$nli = new NoticeListItem($notice, $out);
$nli->showNotice();
// @fixme
$out->elementStart('div', array('class' => 'entry-content'));
} }
/** /**

View File

@ -63,7 +63,7 @@ class QnashowanswerAction extends ShownoticeAction
$this->id = $this->trimmed('id'); $this->id = $this->trimmed('id');
$this->answer = Answer::staticGet('id', $this->id); $this->answer = QnA_Answer::staticGet('id', $this->id);
if (empty($this->answer)) { if (empty($this->answer)) {
throw new ClientException(_('No such answer.'), 404); throw new ClientException(_('No such answer.'), 404);
@ -117,9 +117,11 @@ class QnashowanswerAction extends ShownoticeAction
function showPageTitle() function showPageTitle()
{ {
$this->elementStart('h1'); $this->elementStart('h1');
$this->element('a', $this->element(
'a',
array('href' => $this->answer->url), array('href' => $this->answer->url),
$this->asnwer->title); $this->answer->title
);
$this->elementEnd('h1'); $this->elementEnd('h1');
} }
} }

View File

@ -61,7 +61,7 @@ class QnashowquestionAction extends ShownoticeAction
$this->id = $this->trimmed('id'); $this->id = $this->trimmed('id');
$this->question = Question::staticGet('id', $this->id); $this->question = QnA_Question::staticGet('id', $this->id);
if (empty($this->question)) { if (empty($this->question)) {
// TRANS: Client exception thrown trying to view a non-existing question. // TRANS: Client exception thrown trying to view a non-existing question.
@ -108,7 +108,7 @@ class QnashowquestionAction extends ShownoticeAction
// TRANS: %1$s is the nickname of the user who asked the question, %2$s is the question. // TRANS: %1$s is the nickname of the user who asked the question, %2$s is the question.
return sprintf(_m('%1$s\'s question: %2$s'), return sprintf(_m('%1$s\'s question: %2$s'),
$this->user->nickname, $this->user->nickname,
$this->question->question); $this->question->title);
} }
/** /**

View File

@ -90,7 +90,7 @@ class Qnavote extends Action
} }
$id = $this->trimmed('id'); $id = $this->trimmed('id');
$this->question = Question::staticGet('id', $id); $this->question = QnA_Question::staticGet('id', $id);
if (empty($this->question)) { if (empty($this->question)) {
// TRANS: Client exception thrown trying to respond to a non-existing question. // TRANS: Client exception thrown trying to respond to a non-existing question.
throw new ClientException(_m('Invalid or missing question.'), 404); throw new ClientException(_m('Invalid or missing question.'), 404);

View File

@ -50,7 +50,9 @@ class QnA_Answer extends Managed_DataObject
public $id; // char(36) primary key not null -> UUID public $id; // char(36) primary key not null -> UUID
public $question_id; // char(36) -> question.id UUID public $question_id; // char(36) -> question.id UUID
public $profile_id; // int -> question.id public $profile_id; // int -> question.id
public $best; // (int) boolean -> whether the question asker has marked this as the best answer public $best; // (boolean) int -> whether the question asker has marked this as the best answer
public $revisions; // int -> count of revisions to this answer
public $text; // text -> response text
public $created; // datetime public $created; // datetime
/** /**
@ -111,6 +113,7 @@ class QnA_Answer extends Managed_DataObject
'description' => 'UUID of question being responded to' 'description' => 'UUID of question being responded to'
), ),
'best' => array('type' => 'int', 'size' => 'tiny'), 'best' => array('type' => 'int', 'size' => 'tiny'),
'revisions' => array('type' => 'int'),
'profile_id' => array('type' => 'int'), 'profile_id' => array('type' => 'int'),
'created' => array('type' => 'datetime', 'not null' => true), 'created' => array('type' => 'datetime', 'not null' => true),
), ),
@ -134,7 +137,11 @@ class QnA_Answer extends Managed_DataObject
*/ */
function getByNotice($notice) function getByNotice($notice)
{ {
return self::staticGet('uri', $notice->uri); $answer = self::staticGet('uri', $notice->uri);
if (empty($answer)) {
throw new Exception("No answer with URI {$this->notice->uri}");
}
return $answer;
} }
/** /**
@ -159,13 +166,92 @@ class QnA_Answer extends Managed_DataObject
*/ */
function getQuestion() function getQuestion()
{ {
return Question::staticGet('id', $this->question_id); $question = self::staticGet('id', $this->question_id);
if (empty($question)) {
throw new Exception("No question with ID {$this->question_id}");
}
return question;
} }
static function fromNotice($notice) function getProfile()
{ {
return self::staticGet('uri', $notice->uri); $profile = Profile::staticGet('id', $this->profile_id);
if (empty($profile)) {
throw new Exception("No profile with ID {$this->profile_id}");
} }
return $profile;
}
function asHTML()
{
return self::toHTML(
$this->getProfile(),
$this->getQuestion()
);
}
function asString()
{
return self::toString(
$this->getProfile(),
$this->getQuestion()
);
}
static function toHTML($profile, $event, $response)
{
$fmt = null;
$notice = $event->getNotice();
switch ($response) {
case 'Y':
$fmt = _("<span class='automatic event-rsvp'><a href='%1s'>%2s</a> is attending <a href='%3s'>%4s</a>.</span>");
break;
case 'N':
$fmt = _("<span class='automatic event-rsvp'><a href='%1s'>%2s</a> is not attending <a href='%3s'>%4s</a>.</span>");
break;
case '?':
$fmt = _("<span class='automatic event-rsvp'><a href='%1s'>%2s</a> might attend <a href='%3s'>%4s</a>.</span>");
break;
default:
throw new Exception("Unknown response code {$response}");
break;
}
return sprintf($fmt,
htmlspecialchars($profile->profileurl),
htmlspecialchars($profile->getBestName()),
htmlspecialchars($notice->bestUrl()),
htmlspecialchars($event->title));
}
static function toString($profile, $event, $response)
{
$fmt = null;
$notice = $event->getNotice();
switch ($response) {
case 'Y':
$fmt = _("%1s is attending %2s.");
break;
case 'N':
$fmt = _("%1s is not attending %2s.");
break;
case '?':
$fmt = _("%1s might attend %2s.>");
break;
default:
throw new Exception("Unknown response code {$response}");
break;
}
return sprintf($fmt,
$profile->getBestName(),
$event->title);
}
/** /**
* Save a new answer notice * Save a new answer notice
@ -176,7 +262,7 @@ class QnA_Answer extends Managed_DataObject
* *
* @return Notice saved notice * @return Notice saved notice
*/ */
static function saveNew($profile, $question, $options = null) static function saveNew($profile, $question, $text, $options = null)
{ {
if (empty($options)) { if (empty($options)) {
$options = array(); $options = array();
@ -186,23 +272,24 @@ class QnA_Answer extends Managed_DataObject
$answer->id = UUID::gen(); $answer->id = UUID::gen();
$answer->profile_id = $profile->id; $answer->profile_id = $profile->id;
$answer->question_id = $question->id; $answer->question_id = $question->id;
$answer->revisions = 0;
$answer->best = 0;
$answer->text = $text;
$answer->created = common_sql_now(); $answer->created = common_sql_now();
$answer->uri = common_local_url( $answer->uri = common_local_url(
'showanswer', 'qnashowanswer',
array('id' => $answer->id) array('id' => $answer->id)
); );
common_log(LOG_DEBUG, "Saving answer: $answer->id, $answer->uri"); common_log(LOG_DEBUG, "Saving answer: $answer->id, $answer->uri");
$answer->insert(); $answer->insert();
// TRANS: Notice content answering a question.
// TRANS: %s is the answer
$content = sprintf( $content = sprintf(
_m('answered "%s"'), _m('answered "%s"'),
$answer->uri $question->title
); );
$link = '<a href="' . htmlspecialchars($answer->uri) . '">' . htmlspecialchars($answer) . '</a>'; $link = '<a href="' . htmlspecialchars($answer->uri) . '">' . htmlspecialchars($question->title) . '</a>';
// TRANS: Rendered version of the notice content answering a question. // TRANS: Rendered version of the notice content answering a question.
// TRANS: %s a link to the question with question title as the link content. // TRANS: %s a link to the question with question title as the link content.
$rendered = sprintf(_m('answered "%s"'), $link); $rendered = sprintf(_m('answered "%s"'), $link);
@ -213,11 +300,13 @@ class QnA_Answer extends Managed_DataObject
$options = array_merge( $options = array_merge(
array( array(
'urls' => array(), 'urls' => array(),
'content' => $content,
'rendered' => $rendered, 'rendered' => $rendered,
'tags' => $tags, 'tags' => $tags,
'replies' => $replies, 'replies' => $replies,
'reply_to' => $question->getNotice()->id, 'reply_to' => $question->getNotice()->id,
'object_type' => self::OBJECT_TYPE), 'object_type' => self::OBJECT_TYPE
),
$options $options
); );

View File

@ -175,7 +175,6 @@ class QnA_Question extends Managed_DataObject
static function fromNotice($notice) static function fromNotice($notice)
{ {
common_debug('xxxxxxxxxxxxxxx notice-uri = ' . $notice->uri);
return QnA_Question::staticGet('uri', $notice->uri); return QnA_Question::staticGet('uri', $notice->uri);
} }
@ -209,7 +208,7 @@ class QnA_Question extends Managed_DataObject
$q->uri = $options['uri']; $q->uri = $options['uri'];
} else { } else {
$q->uri = common_local_url( $q->uri = common_local_url(
'showquestion', 'qnashowquestion',
array('id' => $q->id) array('id' => $q->id)
); );
} }

View File

@ -0,0 +1,122 @@
<?php
/**
* StatusNet - the distributed open-source microblogging tool
* Copyright (C) 2011, StatusNet, Inc.
*
* Form for answering a question
*
* 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);
}
/**
* Form to add a new answer to a question
*
* @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/
*/
class QnaansweredForm extends Form
{
protected $question;
protected $answer;
/**
* Construct a new answer form
*
* @param QnA_Answer $answer
* @param HTMLOutputter $out output channel
*
* @return void
*/
function __construct(QnA_Answer $answer, HTMLOutputter $out)
{
parent::__construct($out);
$this->question = $answer->getQuestion();
$this->answer = $answer;
}
/**
* ID of the form
*
* @return int ID of the form
*/
function id()
{
return 'answered-form';
}
/**
* class of the form
*
* @return string class of the form
*/
function formClass()
{
return 'form_settings ajax';
}
/**
* Action of the form
*
* @return string URL of the action
*/
function action()
{
return common_local_url('qnareviseanswer', array('id' => $this->question->id));
}
/**
* Data elements of the form
*
* @return void
*/
function formData()
{
$question = $this->question;
$out = $this->out;
$id = "question-" . $question->id;
$out->element('p', 'Your answer to:', $question->title);
$out->element('input', array('type' => 'text', 'name' => 'answer'));
}
/**
* Action elements
*
* @return void
*/
function formActions()
{
// TRANS: Button text for submitting a poll response.
$this->out->submit('submit', _m('BUTTON', 'Submit'));
}
}