2011-03-18 00:43:13 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Data class to mark a notice as a question
|
|
|
|
*
|
|
|
|
* PHP version 5
|
|
|
|
*
|
2011-03-21 02:24:35 +00:00
|
|
|
* @category QnA
|
2011-03-18 00:43:13 +00: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 02:24:35 +00:00
|
|
|
* @category QnA
|
2011-03-18 00:43:13 +00: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 02:24:35 +00:00
|
|
|
class QnA_Question extends Managed_DataObject
|
2011-03-18 00:43:13 +00:00
|
|
|
{
|
2011-03-21 22:50:36 +00:00
|
|
|
const OBJECT_TYPE = 'http://activityschema.org/object/question';
|
|
|
|
|
2011-03-21 02:24:35 +00:00
|
|
|
public $__table = 'qna_question'; // table name
|
2011-03-18 00:43:13 +00: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 02:24:35 +00:00
|
|
|
public $closed; // int (boolean) whether a question is closed
|
2011-03-18 00:43:13 +00:00
|
|
|
public $created; // datetime
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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 02:24:35 +00:00
|
|
|
return Memcached_DataObject::pkeyGet('QnA_Question', $kv);
|
2011-03-18 00:43:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The One True Thingy that must be defined and declared.
|
|
|
|
*/
|
|
|
|
public static function schemaDef()
|
|
|
|
{
|
|
|
|
return array(
|
2011-03-21 02:24:35 +00:00
|
|
|
'description' => 'Per-notice question data for QNA plugin',
|
2011-03-18 00:43:13 +00:00
|
|
|
'fields' => array(
|
2011-03-21 02:24:35 +00:00
|
|
|
'id' => array(
|
2011-03-21 22:50:36 +00:00
|
|
|
'type' => 'char',
|
|
|
|
'length' => 36,
|
|
|
|
'not null' => true,
|
2011-03-21 02:24:35 +00:00
|
|
|
'description' => 'UUID'
|
|
|
|
),
|
|
|
|
'uri' => array(
|
2011-03-21 22:50:36 +00:00
|
|
|
'type' => 'varchar',
|
|
|
|
'length' => 255,
|
2011-03-21 02:24:35 +00:00
|
|
|
'not null' => true
|
|
|
|
),
|
|
|
|
'profile_id' => array('type' => 'int'),
|
|
|
|
'title' => array('type' => 'text'),
|
2011-03-21 22:50:36 +00:00
|
|
|
'closed' => array('type' => 'int', 'size' => 'tiny'),
|
2011-03-18 00:43:13 +00:00
|
|
|
'description' => array('type' => 'text'),
|
2011-03-21 02:24:35 +00:00
|
|
|
'created' => array(
|
2011-03-22 03:57:19 +00:00
|
|
|
'type' => 'datetime',
|
2011-03-21 02:24:35 +00:00
|
|
|
'not null' => true
|
|
|
|
),
|
2011-03-18 00:43:13 +00: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-30 18:33:15 +01:00
|
|
|
function getProfile()
|
|
|
|
{
|
|
|
|
$profile = Profile::staticGet('id', $this->profile_id);
|
|
|
|
if (empty($profile)) {
|
2011-06-05 10:06:08 +01:00
|
|
|
// TRANS: Exception trown when getting a profile for a non-existing ID.
|
|
|
|
// TRANS: %s is the provided profile ID.
|
|
|
|
throw new Exception(sprintf(_m('No profile with ID %s'),$this->profile_id));
|
2011-03-30 18:33:15 +01:00
|
|
|
}
|
|
|
|
return $profile;
|
|
|
|
}
|
|
|
|
|
2011-03-18 00:43:13 +00: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 02:24:35 +00:00
|
|
|
$a = new QnA_Answer();
|
2011-03-18 00:43:13 +00:00
|
|
|
$a->question_id = $this->id;
|
|
|
|
$a->profile_id = $profile->id;
|
|
|
|
$a->find();
|
|
|
|
if ($a->fetch()) {
|
|
|
|
return $a;
|
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-03-30 18:33:15 +01: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 00:43:13 +00:00
|
|
|
function countAnswers()
|
|
|
|
{
|
2011-04-05 06:04:36 +01:00
|
|
|
$a = new QnA_Answer();
|
|
|
|
|
2011-03-18 00:43:13 +00:00
|
|
|
$a->question_id = $this->id;
|
2011-04-05 06:04:36 +01:00
|
|
|
|
|
|
|
return $a->count();
|
2011-03-18 00:43:13 +00:00
|
|
|
}
|
|
|
|
|
2011-03-21 22:50:36 +00:00
|
|
|
static function fromNotice($notice)
|
|
|
|
{
|
|
|
|
return QnA_Question::staticGet('uri', $notice->uri);
|
|
|
|
}
|
|
|
|
|
2011-03-30 18:33:15 +01:00
|
|
|
function asHTML()
|
|
|
|
{
|
2011-04-04 22:33:20 +01:00
|
|
|
return self::toHTML($this->getProfile(), $this);
|
2011-03-30 18:33:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
function asString()
|
|
|
|
{
|
2011-04-04 22:33:20 +01:00
|
|
|
return self::toString($this->getProfile(), $this);
|
2011-03-30 18:33:15 +01:00
|
|
|
}
|
|
|
|
|
2011-04-04 22:33:20 +01:00
|
|
|
static function toHTML($profile, $question)
|
2011-03-30 18:33:15 +01:00
|
|
|
{
|
|
|
|
$notice = $question->getNotice();
|
|
|
|
|
2011-04-04 23:08:47 +01:00
|
|
|
$out = new XMLStringer();
|
2011-04-04 23:28:28 +01:00
|
|
|
|
2011-04-18 10:27:56 +01:00
|
|
|
$cls = array('qna_question');
|
|
|
|
|
|
|
|
if (!empty($question->closed)) {
|
|
|
|
$cls[] = 'closed';
|
|
|
|
}
|
|
|
|
|
|
|
|
$out->elementStart('p', array('class' => implode(' ', $cls)));
|
|
|
|
|
2011-04-04 23:28:28 +01:00
|
|
|
if (!empty($question->description)) {
|
|
|
|
$out->elementStart('span', 'question-description');
|
2011-05-23 17:30:34 +01:00
|
|
|
$out->raw(common_render_text($question->description));
|
2011-04-04 23:28:28 +01:00
|
|
|
$out->elementEnd('span');
|
|
|
|
}
|
|
|
|
|
2011-04-05 06:04:36 +01:00
|
|
|
$cnt = $question->countAnswers();
|
|
|
|
|
|
|
|
if (!empty($cnt)) {
|
|
|
|
$out->elementStart('span', 'answer-count');
|
2011-06-05 10:06:08 +01:00
|
|
|
// TRANS: Number of given answers to a question.
|
2011-06-05 18:23:35 +01:00
|
|
|
// TRANS: %s is the number of given answers.
|
|
|
|
$out->text(sprintf(_m('%s answer','%s answers',$cnt), $cnt));
|
2011-04-05 06:04:36 +01:00
|
|
|
$out->elementEnd('span');
|
|
|
|
}
|
|
|
|
|
2011-04-04 23:28:28 +01:00
|
|
|
if (!empty($question->closed)) {
|
|
|
|
$out->elementStart('span', 'question-closed');
|
2011-06-05 10:06:08 +01:00
|
|
|
// TRANS: Notification that a question cannot be answered anymore because it is closed.
|
2011-04-04 23:28:28 +01:00
|
|
|
$out->text(_m('This question is closed.'));
|
|
|
|
$out->elementEnd('span');
|
|
|
|
}
|
2011-03-30 18:33:15 +01:00
|
|
|
|
2011-04-18 10:27:56 +01:00
|
|
|
$out->elementEnd('p');
|
|
|
|
|
2011-04-04 23:08:47 +01:00
|
|
|
return $out->getString();
|
2011-03-30 18:33:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static function toString($profile, $question, $answers)
|
|
|
|
{
|
2011-04-04 22:33:20 +01:00
|
|
|
return sprintf(htmlspecialchars($question->description));
|
2011-03-30 18:33:15 +01:00
|
|
|
}
|
|
|
|
|
2011-03-18 00:43:13 +00: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-21 22:50:36 +00:00
|
|
|
static function saveNew($profile, $title, $description, $options = array())
|
2011-03-18 00:43:13 +00:00
|
|
|
{
|
2011-03-21 02:24:35 +00:00
|
|
|
$q = new QnA_Question();
|
2011-03-18 00:43:13 +00: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 03:57:19 +00:00
|
|
|
'qnashowquestion',
|
2011-03-18 00:43:13 +00:00
|
|
|
array('id' => $q->id)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
common_log(LOG_DEBUG, "Saving question: $q->id $q->uri");
|
|
|
|
$q->insert();
|
|
|
|
|
2011-04-05 01:04:13 +01: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-21 22:50:36 +00:00
|
|
|
|
2011-04-05 01:04:13 +01:00
|
|
|
$link = '<a href="' . htmlspecialchars($q->uri) . '">' . htmlspecialchars($q->title) . '</a>';
|
2011-03-18 00:43:13 +00: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-21 22:50:36 +00:00
|
|
|
'object_type' => self::OBJECT_TYPE
|
2011-03-18 00:43:13 +00:00
|
|
|
),
|
|
|
|
$options
|
|
|
|
);
|
|
|
|
|
|
|
|
if (!array_key_exists('uri', $options)) {
|
2011-03-21 22:50:36 +00:00
|
|
|
$options['uri'] = $q->uri;
|
2011-03-18 00:43:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$saved = Notice::saveNew(
|
|
|
|
$profile->id,
|
|
|
|
$content,
|
|
|
|
array_key_exists('source', $options) ?
|
|
|
|
$options['source'] : 'web',
|
|
|
|
$options
|
|
|
|
);
|
|
|
|
|
|
|
|
return $saved;
|
|
|
|
}
|
|
|
|
}
|