2011-03-18 00:43:13 +00:00
|
|
|
<?php
|
2019-09-11 09:25:39 +01:00
|
|
|
// This file is part of GNU social - https://www.gnu.org/software/social
|
|
|
|
//
|
|
|
|
// GNU social 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.
|
|
|
|
//
|
|
|
|
// GNU social 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 GNU social. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
2011-03-18 00:43:13 +00:00
|
|
|
/**
|
|
|
|
* Data class to mark a notice as a question
|
|
|
|
*
|
2019-09-11 09:25:39 +01:00
|
|
|
* @category QnA
|
|
|
|
* @package GNUsocial
|
|
|
|
* @author Zach Copley <zach@status.net>
|
|
|
|
* @copyright 2011 StatusNet, Inc.
|
|
|
|
* @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
|
2011-03-18 00:43:13 +00:00
|
|
|
*/
|
|
|
|
|
2019-09-11 09:25:39 +01:00
|
|
|
defined('GNUSOCIAL') || die();
|
2011-03-18 00:43:13 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* For storing a question
|
|
|
|
*
|
2019-09-11 09:25:39 +01:00
|
|
|
* @copyright 2011 StatusNet, Inc.
|
|
|
|
* @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
|
2011-03-18 00:43:13 +00:00
|
|
|
*
|
2019-09-11 09:25:39 +01:00
|
|
|
* @see DB_DataObject
|
2011-03-18 00:43:13 +00:00
|
|
|
*/
|
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
|
2015-02-12 17:18:55 +00:00
|
|
|
public $uri; // varchar(191) not 255 because utf8mb4 takes more space
|
2011-03-18 00:43:13 +00:00
|
|
|
public $profile_id; // int -> profile.id
|
|
|
|
public $title; // text
|
|
|
|
public $description; // text
|
2019-09-11 09:25:39 +01:00
|
|
|
public $closed; // bool -> whether a question is closed
|
2011-03-18 00:43:13 +00:00
|
|
|
public $created; // datetime
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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',
|
2015-02-12 17:18:55 +00:00
|
|
|
'length' => 191,
|
2011-03-21 02:24:35 +00:00
|
|
|
'not null' => true
|
|
|
|
),
|
|
|
|
'profile_id' => array('type' => 'int'),
|
|
|
|
'title' => array('type' => 'text'),
|
2019-09-11 09:25:39 +01:00
|
|
|
'closed' => array('type' => 'bool'),
|
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
|
|
|
|
*/
|
2019-09-11 09:25:39 +01:00
|
|
|
public static function getByNotice($notice)
|
2011-03-18 00:43:13 +00:00
|
|
|
{
|
2013-08-18 12:04:58 +01:00
|
|
|
return self::getKV('uri', $notice->uri);
|
2011-03-18 00:43:13 +00:00
|
|
|
}
|
|
|
|
|
2019-09-11 09:25:39 +01:00
|
|
|
public function getNotice()
|
2011-03-18 00:43:13 +00:00
|
|
|
{
|
2013-08-18 12:04:58 +01:00
|
|
|
return Notice::getKV('uri', $this->uri);
|
2011-03-18 00:43:13 +00:00
|
|
|
}
|
|
|
|
|
2019-09-11 09:25:39 +01:00
|
|
|
public function getUrl()
|
2011-03-18 00:43:13 +00:00
|
|
|
{
|
2014-04-29 18:46:58 +01:00
|
|
|
return $this->getNotice()->getUrl();
|
2011-03-18 00:43:13 +00:00
|
|
|
}
|
|
|
|
|
2019-09-11 09:25:39 +01:00
|
|
|
public function getProfile()
|
2011-03-30 18:33:15 +01:00
|
|
|
{
|
2013-08-18 12:04:58 +01:00
|
|
|
$profile = Profile::getKV('id', $this->profile_id);
|
2011-03-30 18:33:15 +01:00
|
|
|
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.
|
2019-09-11 09:25:39 +01:00
|
|
|
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
|
|
|
|
*/
|
2019-09-11 09:25:39 +01:00
|
|
|
public function getAnswer(Profile $profile)
|
2011-03-18 00:43:13 +00:00
|
|
|
{
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-11 09:25:39 +01:00
|
|
|
public function getAnswers()
|
2011-03-30 18:33:15 +01:00
|
|
|
{
|
|
|
|
$a = new QnA_Answer();
|
|
|
|
$a->question_id = $this->id;
|
|
|
|
$cnt = $a->find();
|
|
|
|
if (!empty($cnt)) {
|
|
|
|
return $a;
|
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-11 09:25:39 +01:00
|
|
|
public function countAnswers()
|
2011-03-18 00:43:13 +00:00
|
|
|
{
|
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
|
|
|
}
|
|
|
|
|
2019-09-11 09:25:39 +01:00
|
|
|
public static function fromNotice($notice)
|
2011-03-21 22:50:36 +00:00
|
|
|
{
|
2013-08-18 12:04:58 +01:00
|
|
|
return QnA_Question::getKV('uri', $notice->uri);
|
2011-03-21 22:50:36 +00:00
|
|
|
}
|
|
|
|
|
2019-09-11 09:25:39 +01:00
|
|
|
public function asHTML()
|
2011-03-30 18:33:15 +01:00
|
|
|
{
|
2011-04-04 22:33:20 +01:00
|
|
|
return self::toHTML($this->getProfile(), $this);
|
2011-03-30 18:33:15 +01:00
|
|
|
}
|
|
|
|
|
2019-09-11 09:25:39 +01:00
|
|
|
public function asString()
|
2011-03-30 18:33:15 +01:00
|
|
|
{
|
2011-04-04 22:33:20 +01:00
|
|
|
return self::toString($this->getProfile(), $this);
|
2011-03-30 18:33:15 +01:00
|
|
|
}
|
|
|
|
|
2019-09-11 09:25:39 +01:00
|
|
|
public 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.
|
2019-09-11 09:25:39 +01:00
|
|
|
$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
|
|
|
}
|
|
|
|
|
2019-09-11 09:25:39 +01:00
|
|
|
public static function toString($profile, $question, $answers)
|
2011-03-30 18:33:15 +01:00
|
|
|
{
|
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
|
|
|
|
*/
|
2019-09-11 09:25:39 +01:00
|
|
|
public static function saveNew($profile, $title, $description, $options = [])
|
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;
|
|
|
|
}
|
|
|
|
}
|