123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305 |
- <?php
- if (!defined('STATUSNET')) {
- exit(1);
- }
- class QnA_Answer extends Managed_DataObject
- {
- const OBJECT_TYPE = 'http://activityschema.org/object/answer';
- public $__table = 'qna_answer';
- public $id;
- public $uri;
- public $question_id;
- public $profile_id;
- public $best;
- public $revisions;
- public $content;
- public $created;
-
- public static function schemaDef()
- {
- return array(
- 'description' => 'Record of answers to questions',
- 'fields' => array(
- 'id' => array(
- 'type' => 'char',
- 'length' => 36,
- 'not null' => true, 'description' => 'UUID of the response',
- ),
- 'uri' => array(
- 'type' => 'varchar',
- 'length' => 191,
- 'not null' => true,
- 'description' => 'UUID to the answer notice',
- ),
- 'question_id' => array(
- 'type' => 'char',
- 'length' => 36,
- 'not null' => true,
- 'description' => 'UUID of question being responded to',
- ),
- 'content' => array('type' => 'text'),
- 'best' => array('type' => 'int', 'size' => 'tiny'),
- 'revisions' => array('type' => 'int'),
- 'profile_id' => array('type' => 'int'),
- 'created' => array('type' => 'datetime', 'not null' => true),
- ),
- 'primary key' => array('id'),
- 'unique keys' => array(
- 'question_uri_key' => array('uri'),
- 'question_id_profile_id_key' => array('question_id', 'profile_id'),
- ),
- 'indexes' => array(
- 'profile_id_question_id_index' => array('profile_id', 'question_id'),
- )
- );
- }
-
- static function getByNotice($notice)
- {
- $answer = self::getKV('uri', $notice->uri);
- if (empty($answer)) {
- throw new Exception("No answer with URI {$notice->uri}");
- }
- return $answer;
- }
-
- function getNotice()
- {
- return Notice::getKV('uri', $this->uri);
- }
- static function fromNotice($notice)
- {
- return QnA_Answer::getKV('uri', $notice->uri);
- }
- function getUrl()
- {
- return $this->getNotice()->getUrl();
- }
-
- function getQuestion()
- {
- $question = QnA_Question::getKV('id', $this->question_id);
- if (empty($question)) {
-
-
- throw new Exception(sprintf(_m('No question with ID %s'),$this->question_id));
- }
- return $question;
- }
- function getProfile()
- {
- $profile = Profile::getKV('id', $this->profile_id);
- if (empty($profile)) {
-
-
- throw new Exception(sprintf(_m('No profile with ID %s'),$this->profile_id));
- }
- return $profile;
- }
- function asHTML()
- {
- return self::toHTML(
- $this->getProfile(),
- $this->getQuestion(),
- $this
- );
- }
- function asString()
- {
- return self::toString(
- $this->getProfile(),
- $this->getQuestion(),
- $this
- );
- }
- static function toHTML($profile, $question, $answer)
- {
- $notice = $question->getNotice();
- $out = new XMLStringer();
- $cls = array('qna_answer');
- if (!empty($answer->best)) {
- $cls[] = 'best';
- }
- $out->elementStart('p', array('class' => implode(' ', $cls)));
- $out->elementStart('span', 'answer-content');
- $out->raw(common_render_text($answer->content));
- $out->elementEnd('span');
- if (!empty($answer->revisions)) {
- $out->elementstart('span', 'answer-revisions');
- $out->text(
- htmlspecialchars(
-
-
- sprintf(_m('%s revision','%s revisions',$answer->revisions), $answer->revisions)
- )
- );
- $out->elementEnd('span');
- }
- $out->elementEnd('p');
- return $out->getString();
- }
- static function toString($profile, $question, $answer)
- {
-
- $notice = $question->getNotice();
- return sprintf(
-
-
-
- _m('%1$s answered the question "%2$s": %3$s'),
- htmlspecialchars($profile->getBestName()),
- htmlspecialchars($question->title),
- htmlspecialchars($answer->content)
- );
- }
-
- static function saveNew($profile, $question, $text, $options = null)
- {
- if (empty($options)) {
- $options = array();
- }
- $answer = new QnA_Answer();
- $answer->id = UUID::gen();
- $answer->profile_id = $profile->id;
- $answer->question_id = $question->id;
- $answer->revisions = 0;
- $answer->best = 0;
- $answer->content = $text;
- $answer->created = common_sql_now();
- $answer->uri = common_local_url(
- 'qnashowanswer',
- array('id' => $answer->id)
- );
- common_log(LOG_DEBUG, "Saving answer: $answer->id, $answer->uri");
- $answer->insert();
- $content = sprintf(
-
-
- _m('answered "%s"'),
- $question->title
- );
- $link = '<a href="' . htmlspecialchars($answer->uri) . '">' . htmlspecialchars($question->title) . '</a>';
-
-
- $rendered = sprintf(_m('answered "%s"'), $link);
- $tags = array();
- $replies = array();
- $options = array_merge(
- array(
- 'urls' => array(),
- 'content' => $content,
- 'rendered' => $rendered,
- 'tags' => $tags,
- 'replies' => $replies,
- 'reply_to' => $question->getNotice()->id,
- 'object_type' => self::OBJECT_TYPE
- ),
- $options
- );
- if (!array_key_exists('uri', $options)) {
- $options['uri'] = $answer->uri;
- }
- $saved = Notice::saveNew(
- $profile->id,
- $content,
- array_key_exists('source', $options) ?
- $options['source'] : 'web',
- $options
- );
- return $saved;
- }
- }
|