]> git.mxchange.org Git - quix0rs-gnu-social.git/blobdiff - plugins/QnA/QnAPlugin.php
Merge branch '1.0.x' of gitorious.org:statusnet/mainline into 1.0.x
[quix0rs-gnu-social.git] / plugins / QnA / QnAPlugin.php
index c62eefa6716362eb88fadd4a06d5a1e8518a56f8..ad0541a71cf1f08d34787261ba6e7e6e87ee9d8f 100644 (file)
@@ -81,14 +81,17 @@ class QnAPlugin extends MicroAppPlugin
         case 'QnanewquestionAction':
         case 'QnanewanswerAction':
         case 'QnashowquestionAction':
+        case 'QnaclosequestionAction':
         case 'QnashowanswerAction':
         case 'QnareviseanswerAction':
         case 'QnavoteAction':
             include_once $dir . '/actions/'
                 . strtolower(mb_substr($cls, 0, -6)) . '.php';
             return false;
-        case 'QnaquestionForm':
-        case 'QnaanswerForm':
+        case 'QnanewquestionForm':
+        case 'QnashowquestionForm':
+        case 'QnanewanswerForm':
+        case 'QnashowanswerForm':
         case 'QnareviseanswerForm':
         case 'QnavoteForm':
             include_once $dir . '/lib/' . strtolower($cls).'.php';
@@ -120,10 +123,18 @@ class QnAPlugin extends MicroAppPlugin
             'main/qna/newquestion',
             array('action' => 'qnanewquestion')
         );
+        $m->connect(
+            'answer/qna/closequestion',
+            array('action' => 'qnaclosequestion')
+        );
         $m->connect(
             'main/qna/newanswer',
             array('action' => 'qnanewanswer')
         );
+        $m->connect(
+            'main/qna/reviseanswer',
+            array('action' => 'qnareviseanswer')
+        );
         $m->connect(
             'question/vote/:id',
             array('action' => 'qnavote', 'type' => 'question'),
@@ -194,7 +205,7 @@ class QnAPlugin extends MicroAppPlugin
 
         $questionObj = $activity->objects[0];
 
-        if ($questinoObj->type != QnA_Question::OBJECT_TYPE) {
+        if ($questionObj->type != QnA_Question::OBJECT_TYPE) {
             throw new Exception('Wrong type for object.');
         }
 
@@ -269,20 +280,66 @@ class QnAPlugin extends MicroAppPlugin
     }
 
     /**
-     * Change the verb on Answer notices
+     * Output our CSS class for QnA notice list elements
      *
-     * @param Notice $notice
+     * @param NoticeListItem $nli The item being shown
      *
-     * @return ActivityObject
+     * @return boolean hook value
      */
 
-    function onEndNoticeAsActivity($notice, &$act) {
-        switch ($notice->object_type) {
-        case Answer::NORMAL:
-        case Answer::ANONYMOUS:
-            $act->verb = $notice->object_type;
+    function onStartOpenNoticeListItemElement($nli)
+    {
+        $type = $nli->notice->object_type;
+
+        switch($type)
+        {
+        case QnA_Question::OBJECT_TYPE:
+            $id = (empty($nli->repeat)) ? $nli->notice->id : $nli->repeat->id;
+            $class = 'hentry notice question';
+            if ($nli->notice->scope != 0 && $nli->notice->scope != 1) {
+                $class .= ' limited-scope';
+            }
+
+            $question = QnA_Question::staticGet('uri', $nli->notice->uri);
+
+            if (!empty($question->closed)) {
+                $class .= ' closed';
+            }
+
+            $nli->out->elementStart(
+                'li', array(
+                    'class' => $class,
+                    'id'    => 'notice-' . $id
+                )
+            );
+            Event::handle('EndOpenNoticeListItemElement', array($nli));
+            return false;
             break;
+        case QnA_Answer::OBJECT_TYPE:
+            $id = (empty($nli->repeat)) ? $nli->notice->id : $nli->repeat->id;
+
+            $cls = array('hentry', 'notice', 'answer');
+
+            $answer = QnA_Answer::staticGet('uri', $nli->notice->uri);
+
+            if (!empty($answer) && !empty($answer->best)) {
+                $cls[] = 'best';
+            }
+
+            $nli->out->elementStart(
+                'li',
+                array(
+                    'class' => implode(' ', $cls),
+                    'id'    => 'notice-' . $id
+                )
+            );
+            Event::handle('EndOpenNoticeListItemElement', array($nli));
+            return false;
+            break;
+        default:
+            return true;
         }
+
         return true;
     }
 
@@ -292,6 +349,7 @@ class QnAPlugin extends MicroAppPlugin
      * @param Notice $notice
      * @param HTMLOutputter $out
      */
+
     function showNotice($notice, $out)
     {
         switch ($notice->object_type) {
@@ -319,21 +377,15 @@ class QnAPlugin extends MicroAppPlugin
         $nli = new NoticeListItem($notice, $out);
         $nli->showNotice();
 
-        $out->elementStart('div', array('class' => 'entry-content question-content'));
+        $out->elementStart('div', array('class' => 'entry-content question-description'));
+
         $question = QnA_Question::getByNotice($notice);
 
-        if ($question) {
-            if ($user) {
-                $profile = $user->getProfile();
-                $answer = $question->getAnswer($profile);
-                if ($answer) {
-                    // User has already answer; show the results.
-                    $form = new QnareviseanswerForm($answer, $out);
-                } else {
-                    $form = new QnaanswerForm($question, $out);
-                }
-                $form->show();
-            }
+        if (!empty($question)) {
+
+            $form = new QnashowquestionForm($out, $question);
+            $form->show();
+
         } else {
             $out->text(_m('Question data is missing.'));
         }
@@ -343,18 +395,116 @@ class QnAPlugin extends MicroAppPlugin
         $out->elementStart('div', array('class' => 'entry-content'));
     }
 
+
+    /**
+     * Output the HTML for this kind of object in a list
+     *
+     * @param NoticeListItem $nli The list item being shown.
+     *
+     * @return boolean hook value
+     *
+     * @fixme WARNING WARNING WARNING this closes a 'div' that is implicitly opened in BookmarkPlugin's showNotice implementation
+     */
+    function onStartShowNoticeItem($nli)
+    {
+        if (!$this->isMyNotice($nli->notice)) {
+            return true;
+        }
+
+        $out = $nli->out;
+        $notice = $nli->notice;
+
+        $this->showNotice($notice, $out);
+
+        $nli->showNoticeLink();
+        $nli->showNoticeSource();
+        $nli->showNoticeLocation();
+        $nli->showContext();
+        $nli->showRepeat();
+
+        $out->elementEnd('div');
+
+        $nli->showNoticeOptions();
+
+        if ($notice->object_type == QnA_Question::OBJECT_TYPE) {
+
+            $user = common_current_user();
+            $question = QnA_Question::getByNotice($notice);
+
+            if (!empty($user)) {
+
+                $profile = $user->getProfile();
+                $answer = $question->getAnswer($profile);
+
+                // Output a placeholder input -- clicking on it will
+                // bring up a real answer form
+
+                // NOTE: this whole ul is just a placeholder
+                if (empty($question->closed) && empty($answer)) {
+                    $out->elementStart('ul', 'notices qna-dummy');
+                    $out->elementStart('li', 'qna-dummy-placeholder');
+                    $out->element(
+                        'input',
+                        array(
+                            'class' => 'placeholder',
+                            'value' => _m('Your answer...')
+                        )
+                    );
+                    $out->elementEnd('li');
+                    $out->elementEnd('ul');
+                }
+            }
+        }
+
+        return false;
+    }
+
+
     function showNoticeAnswer($notice, $out)
     {
         $user = common_current_user();
 
-        // @hack we want regular rendering, then just add stuff after that
+        $answer   = QnA_Answer::getByNotice($notice);
+        $question = $answer->getQuestion();
+
         $nli = new NoticeListItem($notice, $out);
         $nli->showNotice();
 
+        $out->elementStart('div', array('class' => 'entry-content answer-content'));
+
+        if (!empty($answer)) {
+            $form = new QnashowanswerForm($out, $answer);
+            $form->show();
+        } else {
+            $out->text(_m('Answer data is missing.'));
+        }
+
+        $out->elementEnd('div');
+
         // @fixme
         $out->elementStart('div', array('class' => 'entry-content'));
     }
 
+    static function shorten($content, $notice)
+    {
+        $short = null;
+
+        if (Notice::contentTooLong($content)) {
+            common_debug("content too long");
+            $max = Notice::maxContent();
+            $short = mb_substr($content, 0, $max - 1);
+            $short .= sprintf(
+                '<a href="%s" rel="more" title="%s">…</a>',
+                $notice->uri,
+                _m('more')
+            );
+        } else {
+            $short = $content;
+        }
+
+        return $short;
+    }
+
     /**
      * Form for our app
      *
@@ -364,7 +514,7 @@ class QnAPlugin extends MicroAppPlugin
 
     function entryForm($out)
     {
-        return new QnaquestionForm($out);
+        return new QnanewquestionForm($out);
     }
 
     /**
@@ -394,7 +544,8 @@ class QnAPlugin extends MicroAppPlugin
 
     function onEndShowScripts($action)
     {
-        // XXX maybe some cool shiz here
+        $action->script($this->path('js/qna.js'));
+        return true;
     }
 
     function onEndShowStyles($action)