]> git.mxchange.org Git - quix0rs-gnu-social.git/blobdiff - plugins/QnA/classes/QnA_Question.php
The overloaded DB_DataObject function staticGet is now called getKV
[quix0rs-gnu-social.git] / plugins / QnA / classes / QnA_Question.php
index 1a298ae4e9db08c2a8235d27c93781e43e04574b..58a9cd5e183fd24fc25523f6ba002c7dfd94d794 100644 (file)
@@ -42,12 +42,10 @@ if (!defined('STATUSNET')) {
  *
  * @see      DB_DataObject
  */
-
 class QnA_Question extends Managed_DataObject
 {
-    
-    const QUESTION = 'http://activityschema.org/object/question';
-    
+    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;
@@ -57,22 +55,6 @@ class QnA_Question extends Managed_DataObject
     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
      *
@@ -99,22 +81,22 @@ class QnA_Question extends Managed_DataObject
             'description' => 'Per-notice question data for QNA plugin',
             'fields' => array(
                 'id' => array(
-                    'type'        => 'char', 
-                    'length'      => 36, 
-                    'not null'    => true, 
+                    'type'        => 'char',
+                    'length'      => 36,
+                    'not null'    => true,
                     'description' => 'UUID'
                 ),
                 'uri' => array(
-                    'type'     => 'varchar', 
-                    'length'   => 255, 
+                    'type'     => 'varchar',
+                    'length'   => 255,
                     'not null' => true
                 ),
                 'profile_id'  => array('type' => 'int'),
                 'title'       => array('type' => 'text'),
-                'closed'      => array('type' => 'int', size => 'tiny'),
+                'closed'      => array('type' => 'int', 'size' => 'tiny'),
                 'description' => array('type' => 'text'),
                 'created'     => array(
-                    'type' => 'datetime', 
+                    'type'     => 'datetime',
                     'not null' => true
                 ),
             ),
@@ -132,14 +114,14 @@ 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()
@@ -147,6 +129,17 @@ class QnA_Question extends Managed_DataObject
         return $this->getNotice()->bestUrl();
     }
 
+    function getProfile()
+    {
+        $profile = Profile::getKV('id', $this->profile_id);
+        if (empty($profile)) {
+            // 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;
+    }
+
     /**
      * Get the answer from a particular user to this question, if any.
      *
@@ -167,11 +160,87 @@ class QnA_Question extends Managed_DataObject
         }
     }
 
+    function getAnswers()
+    {
+        $a = new QnA_Answer();
+        $a->question_id = $this->id;
+        $cnt = $a->find();
+        if (!empty($cnt)) {
+            return $a;
+        } else {
+            return null;
+        }
+    }
+
     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::getKV('uri', $notice->uri);
+    }
+
+    function asHTML()
+    {
+        return self::toHTML($this->getProfile(), $this);
+    }
+
+    function asString()
+    {
+        return self::toString($this->getProfile(), $this);
+    }
+
+    static function toHTML($profile, $question)
+    {
+        $notice = $question->getNotice();
+
+        $out = new XMLStringer();
+
+        $cls = array('qna_question');
+
+        if (!empty($question->closed)) {
+            $cls[] = 'closed';
+        }
+
+        $out->elementStart('p', array('class' => implode(' ', $cls)));
+
+        if (!empty($question->description)) {
+            $out->elementStart('span', 'question-description');
+            $out->raw(common_render_text($question->description));
+            $out->elementEnd('span');
+        }
+
+        $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');
+        }
+
+        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)
+    {
+        return sprintf(htmlspecialchars($question->description));
     }
 
     /**
@@ -185,7 +254,7 @@ class QnA_Question extends Managed_DataObject
      *
      * @return Notice saved notice
      */
-    static function saveNew($profile, $question, $title, $description, $options = array())
+    static function saveNew($profile, $title, $description, $options = array())
     {
         $q = new QnA_Question();
 
@@ -204,7 +273,7 @@ class QnA_Question extends Managed_DataObject
             $q->uri = $options['uri'];
         } else {
             $q->uri = common_local_url(
-                'showquestion',
+                'qnashowquestion',
                 array('id' => $q->id)
             );
         }
@@ -212,15 +281,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
-        );
-        
-        $link = '<a href="' . htmlspecialchars($q->uri) . '">' . htmlspecialchars($title) . '</a>';
+        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 = '<a href="' . htmlspecialchars($q->uri) . '">' . htmlspecialchars($q->title) . '</a>';
         // 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);
@@ -234,13 +304,13 @@ class QnA_Question extends Managed_DataObject
                 'rendered'    => $rendered,
                 'tags'        => $tags,
                 'replies'     => $replies,
-                'object_type' => QnAPlugin::QUESTION_OBJECT
+                'object_type' => self::OBJECT_TYPE
             ),
             $options
         );
 
         if (!array_key_exists('uri', $options)) {
-            $options['uri'] = $p->uri;
+            $options['uri'] = $q->uri;
         }
 
         $saved = Notice::saveNew(