]> git.mxchange.org Git - quix0rs-gnu-social.git/blobdiff - plugins/QnA/classes/QnA_Question.php
QnA - save answer revisions and show # of revisions
[quix0rs-gnu-social.git] / plugins / QnA / classes / QnA_Question.php
index 1a298ae4e9db08c2a8235d27c93781e43e04574b..0446128ea0afa8ba12200ee288dc00691086c2a5 100644 (file)
@@ -45,9 +45,8 @@ if (!defined('STATUSNET')) {
 
 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;
@@ -99,22 +98,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
                 ),
             ),
@@ -147,6 +146,15 @@ class QnA_Question extends Managed_DataObject
         return $this->getNotice()->bestUrl();
     }
 
+    function getProfile()
+    {
+        $profile = Profile::staticGet('id', $this->profile_id);
+        if (empty($profile)) {
+            throw new Exception("No profile with ID {$this->profile_id}");
+        }
+        return $profile;
+    }
+
     /**
      * Get the answer from a particular user to this question, if any.
      *
@@ -167,6 +175,18 @@ 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();
@@ -174,6 +194,75 @@ class QnA_Question extends Managed_DataObject
         return $a-count();
     }
 
+    static function fromNotice($notice)
+    {
+        return QnA_Question::staticGet('uri', $notice->uri);
+    }
+
+    function asHTML()
+    {
+        return self::toHTML(
+            $this->getProfile(),
+            $this,
+            $this->getAnswers()
+        );
+    }
+
+    function asString()
+    {
+        return self::toString(
+            $this->getProfile(),
+            $this,
+            $this->getAnswers()
+        );
+    }
+
+    static function toHTML($profile, $question, $answer)
+    {
+        $notice = $question->getNotice();
+
+        $fmt =  '<p class="qna_question">';
+        $fmt .= '<span class="question_title"><a href="%1$s">%2$s</a></span>';
+        $fmt .= '<span class="question_description">%3$s</span>';
+        $fmt .= '<span class="question_author">asked by <a href="%4$s">%5$s</a></span>';
+        $fmt .= '</p>';
+
+        $q = sprintf(
+            $fmt,
+            htmlspecialchars($notice->bestUrl()),
+            htmlspecialchars($question->title),
+            htmlspecialchars($question->description),
+            htmlspecialchars($profile->profileurl),
+            htmlspecialchars($profile->getBestName())
+        );
+
+        $ans = array();
+
+        $ans[] = '<div class="qna_answers">';
+
+        while($answer->fetch()) {
+            $ans[] = $answer->asHTML();
+        }
+
+        $ans[] .= '</div>';
+
+        return $q . implode($ans);
+    }
+
+    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)
+        );
+    }
+
     /**
      * Save a new question notice
      *
@@ -185,7 +274,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 +293,7 @@ class QnA_Question extends Managed_DataObject
             $q->uri = $options['uri'];
         } else {
             $q->uri = common_local_url(
-                'showquestion',
+                'qnashowquestion',
                 array('id' => $q->id)
             );
         }
@@ -219,7 +308,7 @@ class QnA_Question extends Managed_DataObject
             $title,
             $q->uri
         );
-        
+
         $link = '<a href="' . htmlspecialchars($q->uri) . '">' . htmlspecialchars($title) . '</a>';
         // TRANS: Rendered version of the notice content creating a question.
         // TRANS: %s a link to the question as link description.
@@ -234,13 +323,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(