2011-03-18 09:43:13 +09:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Data class to mark a notice as a question
|
|
|
|
*
|
|
|
|
* PHP version 5
|
|
|
|
*
|
2011-03-21 11:24:35 +09:00
|
|
|
* @category QnA
|
2011-03-18 09:43:13 +09:00
|
|
|
* @package StatusNet
|
|
|
|
* @author Zach Copley <zach@status.net>
|
|
|
|
* @license http://www.fsf.org/licensing/licenses/agpl.html AGPLv3
|
|
|
|
* @link http://status.net/
|
|
|
|
*
|
|
|
|
* StatusNet - the distributed open-source microblogging tool
|
|
|
|
* Copyright (C) 2011, StatusNet, Inc.
|
|
|
|
*
|
|
|
|
* 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/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (!defined('STATUSNET')) {
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* For storing a question
|
|
|
|
*
|
2011-03-21 11:24:35 +09:00
|
|
|
* @category QnA
|
2011-03-18 09:43:13 +09:00
|
|
|
* @package StatusNet
|
|
|
|
* @author Zach Copley <zach@status.net>
|
|
|
|
* @license http://www.fsf.org/licensing/licenses/agpl.html AGPLv3
|
|
|
|
* @link http://status.net/
|
|
|
|
*
|
|
|
|
* @see DB_DataObject
|
|
|
|
*/
|
|
|
|
|
2011-03-21 11:24:35 +09:00
|
|
|
class QnA_Question extends Managed_DataObject
|
2011-03-18 09:43:13 +09:00
|
|
|
{
|
2011-03-22 07:50:36 +09:00
|
|
|
const OBJECT_TYPE = 'http://activityschema.org/object/question';
|
|
|
|
|
2011-03-21 11:24:35 +09:00
|
|
|
public $__table = 'qna_question'; // table name
|
2011-03-18 09:43:13 +09:00
|
|
|
public $id; // char(36) primary key not null -> UUID
|
|
|
|
public $uri;
|
|
|
|
public $profile_id; // int -> profile.id
|
|
|
|
public $title; // text
|
|
|
|
public $description; // text
|
2011-03-21 11:24:35 +09:00
|
|
|
public $closed; // int (boolean) whether a question is closed
|
2011-03-18 09:43:13 +09:00
|
|
|
public $created; // datetime
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get an instance by key
|
|
|
|
*
|
|
|
|
* This is a utility method to get a single instance with a given key value.
|
|
|
|
*
|
2011-03-21 11:24:35 +09:00
|
|
|
* @param string $k Key to use to lookup
|
2011-03-18 09:43:13 +09:00
|
|
|
* @param mixed $v Value to lookup
|
|
|
|
*
|
2011-03-21 11:24:35 +09:00
|
|
|
* @return QnA_Question object found, or null for no hits
|
2011-03-18 09:43:13 +09:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
function staticGet($k, $v=null)
|
|
|
|
{
|
2011-03-21 11:24:35 +09:00
|
|
|
return Memcached_DataObject::staticGet('QnA_Question', $k, $v);
|
2011-03-18 09:43:13 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get an instance by compound key
|
|
|
|
*
|
|
|
|
* This is a utility method to get a single instance with a given set of
|
|
|
|
* key-value pairs. Usually used for the primary key for a compound key; thus
|
|
|
|
* the name.
|
|
|
|
*
|
|
|
|
* @param array $kv array of key-value mappings
|
|
|
|
*
|
|
|
|
* @return Bookmark object found, or null for no hits
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
function pkeyGet($kv)
|
|
|
|
{
|
2011-03-21 11:24:35 +09:00
|
|
|
return Memcached_DataObject::pkeyGet('QnA_Question', $kv);
|
2011-03-18 09:43:13 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The One True Thingy that must be defined and declared.
|
|
|
|
*/
|
|
|
|
public static function schemaDef()
|
|
|
|
{
|
|
|
|
return array(
|
2011-03-21 11:24:35 +09:00
|
|
|
'description' => 'Per-notice question data for QNA plugin',
|
2011-03-18 09:43:13 +09:00
|
|
|
'fields' => array(
|
2011-03-21 11:24:35 +09:00
|
|
|
'id' => array(
|
2011-03-22 07:50:36 +09:00
|
|
|
'type' => 'char',
|
|
|
|
'length' => 36,
|
|
|
|
'not null' => true,
|
2011-03-21 11:24:35 +09:00
|
|
|
'description' => 'UUID'
|
|
|
|
),
|
|
|
|
'uri' => array(
|
2011-03-22 07:50:36 +09:00
|
|
|
'type' => 'varchar',
|
|
|
|
'length' => 255,
|
2011-03-21 11:24:35 +09:00
|
|
|
'not null' => true
|
|
|
|
),
|
|
|
|
'profile_id' => array('type' => 'int'),
|
|
|
|
'title' => array('type' => 'text'),
|
2011-03-22 07:50:36 +09:00
|
|
|
'closed' => array('type' => 'int', 'size' => 'tiny'),
|
2011-03-18 09:43:13 +09:00
|
|
|
'description' => array('type' => 'text'),
|
2011-03-21 11:24:35 +09:00
|
|
|
'created' => array(
|
2011-03-22 12:57:19 +09:00
|
|
|
'type' => 'datetime',
|
2011-03-21 11:24:35 +09:00
|
|
|
'not null' => true
|
|
|
|
),
|
2011-03-18 09:43:13 +09:00
|
|
|
),
|
|
|
|
'primary key' => array('id'),
|
|
|
|
'unique keys' => array(
|
|
|
|
'question_uri_key' => array('uri'),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get a question based on a notice
|
|
|
|
*
|
|
|
|
* @param Notice $notice Notice to check for
|
|
|
|
*
|
|
|
|
* @return Question found question or null
|
|
|
|
*/
|
|
|
|
function getByNotice($notice)
|
|
|
|
{
|
|
|
|
return self::staticGet('uri', $notice->uri);
|
|
|
|
}
|
|
|
|
|
|
|
|
function getNotice()
|
|
|
|
{
|
|
|
|
return Notice::staticGet('uri', $this->uri);
|
|
|
|
}
|
|
|
|
|
|
|
|
function bestUrl()
|
|
|
|
{
|
|
|
|
return $this->getNotice()->bestUrl();
|
|
|
|
}
|
|
|
|
|
2011-03-31 02:33:15 +09:00
|
|
|
function getProfile()
|
|
|
|
{
|
|
|
|
$profile = Profile::staticGet('id', $this->profile_id);
|
|
|
|
if (empty($profile)) {
|
|
|
|
throw new Exception("No profile with ID {$this->profile_id}");
|
|
|
|
}
|
|
|
|
return $profile;
|
|
|
|
}
|
|
|
|
|
2011-03-18 09:43:13 +09:00
|
|
|
/**
|
|
|
|
* Get the answer from a particular user to this question, if any.
|
|
|
|
*
|
|
|
|
* @param Profile $profile
|
|
|
|
*
|
|
|
|
* @return Answer object or null
|
|
|
|
*/
|
|
|
|
function getAnswer(Profile $profile)
|
|
|
|
{
|
2011-03-21 11:24:35 +09:00
|
|
|
$a = new QnA_Answer();
|
2011-03-18 09:43:13 +09:00
|
|
|
$a->question_id = $this->id;
|
|
|
|
$a->profile_id = $profile->id;
|
|
|
|
$a->find();
|
|
|
|
if ($a->fetch()) {
|
|
|
|
return $a;
|
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-03-31 02:33:15 +09:00
|
|
|
function getAnswers()
|
|
|
|
{
|
|
|
|
$a = new QnA_Answer();
|
|
|
|
$a->question_id = $this->id;
|
|
|
|
$cnt = $a->find();
|
|
|
|
if (!empty($cnt)) {
|
|
|
|
return $a;
|
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-03-18 09:43:13 +09:00
|
|
|
function countAnswers()
|
|
|
|
{
|
2011-04-05 14:04:36 +09:00
|
|
|
$a = new QnA_Answer();
|
|
|
|
|
2011-03-18 09:43:13 +09:00
|
|
|
$a->question_id = $this->id;
|
2011-04-05 14:04:36 +09:00
|
|
|
|
|
|
|
return $a->count();
|
2011-03-18 09:43:13 +09:00
|
|
|
}
|
|
|
|
|
2011-03-22 07:50:36 +09:00
|
|
|
static function fromNotice($notice)
|
|
|
|
{
|
|
|
|
return QnA_Question::staticGet('uri', $notice->uri);
|
|
|
|
}
|
|
|
|
|
2011-03-31 02:33:15 +09:00
|
|
|
function asHTML()
|
|
|
|
{
|
2011-04-05 06:33:20 +09:00
|
|
|
return self::toHTML($this->getProfile(), $this);
|
2011-03-31 02:33:15 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
function asString()
|
|
|
|
{
|
2011-04-05 06:33:20 +09:00
|
|
|
return self::toString($this->getProfile(), $this);
|
2011-03-31 02:33:15 +09:00
|
|
|
}
|
|
|
|
|
2011-04-05 06:33:20 +09:00
|
|
|
static function toHTML($profile, $question)
|
2011-03-31 02:33:15 +09:00
|
|
|
{
|
|
|
|
$notice = $question->getNotice();
|
|
|
|
|
2011-04-05 07:08:47 +09:00
|
|
|
$out = new XMLStringer();
|
2011-04-05 07:28:28 +09:00
|
|
|
|
|
|
|
if (!empty($question->description)) {
|
|
|
|
$out->elementStart('span', 'question-description');
|
|
|
|
$out->raw(QnAPlugin::shorten($question->description, $notice));
|
|
|
|
$out->elementEnd('span');
|
|
|
|
}
|
|
|
|
|
2011-04-05 14:04:36 +09:00
|
|
|
$cnt = $question->countAnswers();
|
|
|
|
|
|
|
|
if (!empty($cnt)) {
|
|
|
|
$out->elementStart('span', 'answer-count');
|
|
|
|
$out->text(sprintf(_m('%s answers'), $cnt));
|
|
|
|
$out->elementEnd('span');
|
|
|
|
}
|
|
|
|
|
2011-04-05 07:28:28 +09:00
|
|
|
if (!empty($question->closed)) {
|
|
|
|
$out->elementStart('span', 'question-closed');
|
|
|
|
$out->text(_m('This question is closed.'));
|
|
|
|
$out->elementEnd('span');
|
|
|
|
}
|
2011-03-31 02:33:15 +09:00
|
|
|
|
2011-04-05 07:08:47 +09:00
|
|
|
return $out->getString();
|
2011-03-31 02:33:15 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
static function toString($profile, $question, $answers)
|
|
|
|
{
|
2011-04-05 06:33:20 +09:00
|
|
|
return sprintf(htmlspecialchars($question->description));
|
2011-03-31 02:33:15 +09:00
|
|
|
}
|
|
|
|
|
2011-03-18 09:43:13 +09:00
|
|
|
/**
|
|
|
|
* Save a new question notice
|
|
|
|
*
|
|
|
|
* @param Profile $profile
|
|
|
|
* @param string $question
|
|
|
|
* @param string $title
|
|
|
|
* @param string $description
|
|
|
|
* @param array $option // and whatnot
|
|
|
|
*
|
|
|
|
* @return Notice saved notice
|
|
|
|
*/
|
2011-03-22 07:50:36 +09:00
|
|
|
static function saveNew($profile, $title, $description, $options = array())
|
2011-03-18 09:43:13 +09:00
|
|
|
{
|
2011-03-21 11:24:35 +09:00
|
|
|
$q = new QnA_Question();
|
2011-03-18 09:43:13 +09:00
|
|
|
|
|
|
|
$q->id = UUID::gen();
|
|
|
|
$q->profile_id = $profile->id;
|
|
|
|
$q->title = $title;
|
|
|
|
$q->description = $description;
|
|
|
|
|
|
|
|
if (array_key_exists('created', $options)) {
|
|
|
|
$q->created = $options['created'];
|
|
|
|
} else {
|
|
|
|
$q->created = common_sql_now();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (array_key_exists('uri', $options)) {
|
|
|
|
$q->uri = $options['uri'];
|
|
|
|
} else {
|
|
|
|
$q->uri = common_local_url(
|
2011-03-22 12:57:19 +09:00
|
|
|
'qnashowquestion',
|
2011-03-18 09:43:13 +09:00
|
|
|
array('id' => $q->id)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
common_log(LOG_DEBUG, "Saving question: $q->id $q->uri");
|
|
|
|
$q->insert();
|
|
|
|
|
2011-04-05 09:04:13 +09:00
|
|
|
if (Notice::contentTooLong($q->title . ' ' . $q->uri)) {
|
|
|
|
$max = Notice::maxContent();
|
|
|
|
$uriLen = mb_strlen($q->uri);
|
|
|
|
$targetLen = $max - ($uriLen + 15);
|
|
|
|
$title = mb_substr($q->title, 0, $targetLen) . '…';
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
$content = $title . ' ' . $q->uri;
|
2011-03-22 07:50:36 +09:00
|
|
|
|
2011-04-05 09:04:13 +09:00
|
|
|
$link = '<a href="' . htmlspecialchars($q->uri) . '">' . htmlspecialchars($q->title) . '</a>';
|
2011-03-18 09:43:13 +09:00
|
|
|
// TRANS: Rendered version of the notice content creating a question.
|
|
|
|
// TRANS: %s a link to the question as link description.
|
|
|
|
$rendered = sprintf(_m('Question: %s'), $link);
|
|
|
|
|
|
|
|
$tags = array('question');
|
|
|
|
$replies = array();
|
|
|
|
|
|
|
|
$options = array_merge(
|
|
|
|
array(
|
|
|
|
'urls' => array(),
|
|
|
|
'rendered' => $rendered,
|
|
|
|
'tags' => $tags,
|
|
|
|
'replies' => $replies,
|
2011-03-22 07:50:36 +09:00
|
|
|
'object_type' => self::OBJECT_TYPE
|
2011-03-18 09:43:13 +09:00
|
|
|
),
|
|
|
|
$options
|
|
|
|
);
|
|
|
|
|
|
|
|
if (!array_key_exists('uri', $options)) {
|
2011-03-22 07:50:36 +09:00
|
|
|
$options['uri'] = $q->uri;
|
2011-03-18 09:43:13 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
$saved = Notice::saveNew(
|
|
|
|
$profile->id,
|
|
|
|
$content,
|
|
|
|
array_key_exists('source', $options) ?
|
|
|
|
$options['source'] : 'web',
|
|
|
|
$options
|
|
|
|
);
|
|
|
|
|
|
|
|
return $saved;
|
|
|
|
}
|
|
|
|
}
|