X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=plugins%2FQnA%2Fclasses%2FQnA_Question.php;h=b5318386777b50dcd2569007bde40303565fa7ad;hb=afbb3ec37a32314d55e2163da4a2958bf3a6eba9;hp=2403857d283e90d1d21cb4591da0a758d4feee2a;hpb=57198a74647f8350db4de03b0b7ef157091a4359;p=quix0rs-gnu-social.git diff --git a/plugins/QnA/classes/QnA_Question.php b/plugins/QnA/classes/QnA_Question.php index 2403857d28..b531838677 100644 --- a/plugins/QnA/classes/QnA_Question.php +++ b/plugins/QnA/classes/QnA_Question.php @@ -42,53 +42,19 @@ if (!defined('STATUSNET')) { * * @see DB_DataObject */ - class QnA_Question extends Managed_DataObject { const OBJECT_TYPE = 'http://activityschema.org/object/question'; public $__table = 'qna_question'; // table name public $id; // char(36) primary key not null -> UUID - public $uri; + public $uri; // varchar(191) not 255 because utf8mb4 takes more space public $profile_id; // int -> profile.id public $title; // text public $description; // text public $closed; // int (boolean) whether a question is closed public $created; // datetime - /** - * Get an instance by key - * - * This is a utility method to get a single instance with a given key value. - * - * @param string $k Key to use to lookup - * @param mixed $v Value to lookup - * - * @return QnA_Question object found, or null for no hits - * - */ - function staticGet($k, $v=null) - { - return Memcached_DataObject::staticGet('QnA_Question', $k, $v); - } - - /** - * 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) - { - return Memcached_DataObject::pkeyGet('QnA_Question', $kv); - } - /** * The One True Thingy that must be defined and declared. */ @@ -105,7 +71,7 @@ class QnA_Question extends Managed_DataObject ), 'uri' => array( 'type' => 'varchar', - 'length' => 255, + 'length' => 191, 'not null' => true ), 'profile_id' => array('type' => 'int'), @@ -131,26 +97,28 @@ class QnA_Question extends Managed_DataObject * * @return Question found question or null */ - function getByNotice($notice) + static function getByNotice($notice) { - return self::staticGet('uri', $notice->uri); + return self::getKV('uri', $notice->uri); } function getNotice() { - return Notice::staticGet('uri', $this->uri); + return Notice::getKV('uri', $this->uri); } - function bestUrl() + function getUrl() { - return $this->getNotice()->bestUrl(); + return $this->getNotice()->getUrl(); } function getProfile() { - $profile = Profile::staticGet('id', $this->profile_id); + $profile = Profile::getKV('id', $this->profile_id); if (empty($profile)) { - throw new Exception("No profile with ID {$this->profile_id}"); + // 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)); } return $profile; } @@ -189,78 +157,73 @@ class QnA_Question extends Managed_DataObject function countAnswers() { - $a = new QnA_Answer(); + $a = new QnA_Answer(); + $a->question_id = $this->id; - return $a-count(); + + return $a->count(); } static function fromNotice($notice) { - return QnA_Question::staticGet('uri', $notice->uri); + return QnA_Question::getKV('uri', $notice->uri); } function asHTML() { - return self::toHTML( - $this->getProfile(), - $this, - $this->getAnswers() - ); + return self::toHTML($this->getProfile(), $this); } function asString() { - return self::toString( - $this->getProfile(), - $this, - $this->getAnswers() - ); + return self::toString($this->getProfile(), $this); } - static function toHTML($profile, $question, $answer) + static function toHTML($profile, $question) { $notice = $question->getNotice(); - $fmt = '
'; - $fmt .= '%2$s'; - $fmt .= '%3$s'; - $fmt .= 'asked by %5$s'; - $fmt .= '
'; - - $q = sprintf( - $fmt, - htmlspecialchars($notice->bestUrl()), - htmlspecialchars($question->title), - htmlspecialchars($question->description), - htmlspecialchars($profile->profileurl), - htmlspecialchars($profile->getBestName()) - ); + $out = new XMLStringer(); - $ans = array(); + $cls = array('qna_question'); - $ans[] = '
'; + if (!empty($question->closed)) { + $cls[] = 'closed'; + } + + $out->elementStart('p', array('class' => implode(' ', $cls))); - while($answer->fetch()) { - $ans[] = $answer->asHTML(); + if (!empty($question->description)) { + $out->elementStart('span', 'question-description'); + $out->raw(common_render_text($question->description)); + $out->elementEnd('span'); } - $ans[] .= '
'; + $cnt = $question->countAnswers(); + + if (!empty($cnt)) { + $out->elementStart('span', 'answer-count'); + // TRANS: Number of given answers to a question. + // TRANS: %s is the number of given answers. + $out->text(sprintf(_m('%s answer','%s answers',$cnt), $cnt)); + $out->elementEnd('span'); + } - return $q . implode($ans); + if (!empty($question->closed)) { + $out->elementStart('span', 'question-closed'); + // TRANS: Notification that a question cannot be answered anymore because it is closed. + $out->text(_m('This question is closed.')); + $out->elementEnd('span'); + } + + $out->elementEnd('p'); + + return $out->getString(); } static function toString($profile, $question, $answers) { - $fmt = _m( - '%1$s asked the question "%2$s": %3$s' - ); - - return sprintf( - $fmt, - htmlspecialchars($profile->getBestName()), - htmlspecialchars($question->title), - htmlspecialchars($question->description) - ); + return sprintf(htmlspecialchars($question->description)); } /** @@ -301,15 +264,16 @@ class QnA_Question extends Managed_DataObject common_log(LOG_DEBUG, "Saving question: $q->id $q->uri"); $q->insert(); - // TRANS: Notice content creating a question. - // TRANS: %1$s is the title of the question, %2$s is a link to the question. - $content = sprintf( - _m('question: %1$s %2$s'), - $title, - $q->uri - ); + 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; - $link = '' . htmlspecialchars($title) . ''; + $link = '' . htmlspecialchars($q->title) . ''; // 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);