]> git.mxchange.org Git - quix0rs-gnu-social.git/blobdiff - plugins/QnA/QnAPlugin.php
Moving some more repeat stuff into the Share plugin
[quix0rs-gnu-social.git] / plugins / QnA / QnAPlugin.php
index ba3b6e329a8bb9717c518d6238ce9b5821036391..a476c2af56540cf93405e68c165ac641566d7354 100644 (file)
@@ -46,6 +46,9 @@ if (!defined('STATUSNET')) {
  */
 class QnAPlugin extends MicroAppPlugin
 {
+
+    var $oldSaveNew = true;
+
     /**
      * Set up our tables (question and answer)
      *
@@ -65,52 +68,19 @@ class QnAPlugin extends MicroAppPlugin
         return true;
     }
 
-    /**
-     * Load related modules when needed
-     *
-     * @param string $cls Name of the class to be loaded
-     *
-     * @return boolean hook value; true means continue processing, false means stop.
-     */
-    function onAutoload($cls)
-    {
-        $dir = dirname(__FILE__);
-
-        switch ($cls)
-        {
-        case 'QnanewquestionAction':
-        case 'QnanewanswerAction':
-        case 'QnashowquestionAction':
-        case 'QnashowanswerAction':
-        case 'QnavoteAction':
-            include_once $dir . '/actions/'
-                . strtolower(mb_substr($cls, 0, -6)) . '.php';
-            return false;
-        case 'QnaquestionForm':
-        case 'QnaanswerForm':
-        case 'QnavoteForm';
-            include_once $dir . '/lib/' . strtolower($cls).'.php';
-            break;
-        case 'QnA_Question':
-        case 'QnA_Answer':
-        case 'QnA_Vote':
-            include_once $dir . '/classes/' . $cls.'.php';
-            return false;
-            break;
-        default:
-            return true;
-        }
+    public function newFormAction() {
+        return 'qnanewquestion';
     }
 
     /**
      * Map URLs to actions
      *
-     * @param Net_URL_Mapper $m path-to-action mapper
+     * @param URLMapper $m path-to-action mapper
      *
      * @return boolean hook value; true means continue processing, false means stop.
      */
 
-    function onRouterInitialized($m)
+    public function onRouterInitialized(URLMapper $m)
     {
         $UUIDregex = '[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}';
 
@@ -119,9 +89,16 @@ class QnAPlugin extends MicroAppPlugin
             array('action' => 'qnanewquestion')
         );
         $m->connect(
-            'main/qna/newanswer/:id',
-            array('action' => 'qnanewanswer'),
-            array('id' => $UUIDregex)
+            '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',
@@ -151,17 +128,19 @@ class QnAPlugin extends MicroAppPlugin
     {
         $versions[] = array(
             'name'        => 'QnA',
-            'version'     => STATUSNET_VERSION,
+            'version'     => GNUSOCIAL_VERSION,
             'author'      => 'Zach Copley',
             'homepage'    => 'http://status.net/wiki/Plugin:QnA',
             'description' =>
+             // TRANS: Plugin description.
              _m('Question and Answers micro-app.')
         );
         return true;
     }
 
     function appTitle() {
-        return _m('Question');
+        // TRANS: Application title.
+        return _m('TITLE','Question');
     }
 
     function tag() {
@@ -185,40 +164,43 @@ class QnAPlugin extends MicroAppPlugin
      *
      * @return Notice the resulting notice
      */
-    function saveNoticeFromActivity($activity, $actor, $options=array())
+    function saveNoticeFromActivity(Activity $activity, Profile $actor, array $options=array())
     {
         if (count($activity->objects) != 1) {
-            throw new Exception('Too many activity objects.');
+            // TRANS: Exception thrown when there are too many activity objects.
+            throw new Exception(_m('Too many activity objects.'));
         }
 
         $questionObj = $activity->objects[0];
 
-        if ($questinoObj->type != QnA_Question::OBJECT_TYPE) {
-            throw new Exception('Wrong type for object.');
+        if ($questionObj->type != QnA_Question::OBJECT_TYPE) {
+            // TRANS: Exception thrown when an incorrect object type is encountered.
+            throw new Exception(_m('Wrong type for object.'));
         }
 
         $notice = null;
 
         switch ($activity->verb) {
         case ActivityVerb::POST:
-            $notice = Question::saveNew(
+            $notice = QnA_Question::saveNew(
                 $actor,
-                $questionObj->title
-               // null,
-               // $questionObj->summary,
-               // $options
+                $questionObj->title,
+                $questionObj->summary,
+                $options
             );
             break;
-        case Answer::NORMAL:
-            $question = QnA_Question::staticGet('uri', $questionObj->id);
+        case Answer::ObjectType:
+            $question = QnA_Question::getKV('uri', $questionObj->id);
             if (empty($question)) {
                 // FIXME: save the question
-                throw new Exception("Answer to unknown question.");
+                // TRANS: Exception thrown when answering a non-existing question.
+                throw new Exception(_m('Answer to unknown question.'));
             }
-            $notice = QnA_Answer::saveNew($actor, $question, $activity->verb, $options);
+            $notice = QnA_Answer::saveNew($actor, $question, $options);
             break;
         default:
-            throw new Exception("Unknown verb for question");
+            // TRANS: Exception thrown when an object type is encountered that cannot be handled.
+            throw new Exception(_m('Unknown object type.'));
         }
 
         return $notice;
@@ -232,7 +214,7 @@ class QnAPlugin extends MicroAppPlugin
      * @return ActivityObject
      */
 
-    function activityObjectFromNotice($notice)
+    function activityObjectFromNotice(Notice $notice)
     {
         $question = null;
 
@@ -247,13 +229,15 @@ class QnAPlugin extends MicroAppPlugin
         }
 
         if (empty($question)) {
-            throw new Exception("Unknown object type.");
+            // TRANS: Exception thrown when an object type is encountered that cannot be handled.
+            throw new Exception(_m('Unknown object type.'));
         }
 
         $notice = $question->getNotice();
 
         if (empty($notice)) {
-            throw new Exception("Unknown question notice.");
+            // TRANS: Exception thrown when requesting a non-existing question notice.
+            throw new Exception(_m('Unknown question notice.'));
         }
 
         $obj = new ActivityObject();
@@ -261,7 +245,7 @@ class QnAPlugin extends MicroAppPlugin
         $obj->id      = $question->uri;
         $obj->type    = QnA_Question::OBJECT_TYPE;
         $obj->title   = $question->title;
-        $obj->link    = $notice->bestUrl();
+        $obj->link    = $notice->getUrl();
 
         // XXX: probably need other stuff here
 
@@ -269,150 +253,154 @@ 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
-     */
-
-    function onEndNoticeAsActivity($notice, &$act) {
-        switch ($notice->object_type) {
-        case Answer::NORMAL:
-        case Answer::ANONYMOUS:
-            $act->verb = $notice->object_type;
-            break;
-        }
-        return true;
-    }
-
-    /**
-     * Custom HTML output for our notices
-     *
-     * @param Notice $notice
-     * @param HTMLOutputter $out
+     * @return boolean hook value
      */
 
-    function showNotice($notice, $out)
+    function onStartOpenNoticeListItemElement($nli)
     {
-        switch ($notice->object_type) {
-        case QnA_Question::OBJECT_TYPE:
-            $this->showQuestionNotice($notice, $out);
-            break;
-        case QnA_Answer::OBJECT_TYPE:
-            $this->showAnswerNotice($notice, $out);
-            break;
-        }
-
-        // bad craziness
-        $out->elementStart('div', array('class' => 'question'));
-
-        $profile = $notice->getProfile();
-        $avatar = $profile->getAvatar(AVATAR_MINI_SIZE);
-
-        $out->element(
-            'img',
-            array(
-                'src'    => ($avatar)
-                    ? $avatar->displayUrl()
-                    : Avatar::defaultImage(AVATAR_MINI_SIZE),
-                'class'  => 'avatar photo question-avatar',
-                'width'  => AVATAR_MINI_SIZE,
-                'height' => AVATAR_MINI_SIZE,
-                'alt'    => $profile->getBestName()
-            )
-        );
-
-        $out->raw(' '); // avoid   for AJAX XML compatibility
-
-        // hack for belongsOnTimeline; JS needs to be able to find the author
-        $out->elementStart('span', 'vcard author');
-        $out->element(
-            'a',
-            array(
-                'class' => 'url',
-                'href'  => $profile->profileurl,
-                'title' => $profile->getBestName()
-            ),
-            $profile->nickname
-        );
-
-        $out->elementEnd('span');
-    }
+        $type = $nli->notice->object_type;
 
-    function showAnswerNotice($notice, $out)
-    {
-        $answer = QnA_Answer::fromNotice($notice);
+        switch($type)
+        {
+        case QnA_Question::OBJECT_TYPE:
+            $id = (empty($nli->repeat)) ? $nli->notice->id : $nli->repeat->id;
+            $class = 'h-entry notice question';
+            if ($nli->notice->scope != 0 && $nli->notice->scope != 1) {
+                $class .= ' limited-scope';
+            }
 
-        assert(!empty($answer));
+            $question = QnA_Question::getKV('uri', $nli->notice->uri);
 
-        $out->elementStart('div', 'answer');
-        $out->raw($answer->asHTML());
-        $out->elementEnd('div');
-    }
+            if (!empty($question->closed)) {
+                $class .= ' closed';
+            }
 
-    function showQuestionNotice($notice, $out)
-    {
-        $profile  = $notice->getProfile();
-        $question = QnA_Question::fromNotice($notice);
+            $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;
 
-        assert(!empty($question));
-        assert(!empty($profile));
+            $cls = array('h-entry', 'notice', 'answer');
 
-        $out->elementStart('div', 'question-notice');
+            $answer = QnA_Answer::getKV('uri', $nli->notice->uri);
 
-        $out->elementStart('h3');
+            if (!empty($answer) && !empty($answer->best)) {
+                $cls[] = 'best';
+            }
 
-        if (!empty($question->url)) {
-            $out->element(
-                'a',
+            $nli->out->elementStart(
+                'li',
                 array(
-                    'href'  => $question->url,
-                    'class' => 'question-title'
-                ),
-                $question->title
+                    'class' => implode(' ', $cls),
+                    'id'    => 'notice-' . $id
+                )
             );
-        } else {
-            $out->text($question->title);
+            Event::handle('EndOpenNoticeListItemElement', array($nli));
+            return false;
+            break;
+        default:
+            return true;
         }
 
-        if (!empty($question->location)) {
-            $out->elementStart('div', 'question-location');
-            $out->element('strong', null, _('Location: '));
-            $out->element('span', 'location', $question->location);
-            $out->elementEnd('div');
-        }
+        return true;
+    }
 
-        if (!empty($question->description)) {
-            $out->elementStart('div', 'question-description');
-            $out->element('strong', null, _('Description: '));
-            $out->element('span', 'description', $question->description);
-            $out->elementEnd('div');
+    /**
+     * Output the HTML for this kind of object in a list
+     *
+     * @param NoticeListItem $nli The list item being shown.
+     *
+     * @return boolean hook value
+     *
+     * @todo 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;
         }
 
-        //$answers = $question->getAnswers();
-
-        $out->elementStart('div', 'question-answers');
-        $out->element('strong', null, _('Answer: '));
-        $out->element('span', 'question-answer');
-
-        $out->elementEnd('div');
-
-        $user = common_current_user();
-
-        if (!empty($user)) {
-
-            $answer = $question->getAnswer($user->getProfile());
-
-            if (empty($answer)) {
-                $form = new QnaanswerForm($question, $out);
-                $form->show();
+        $out = $nli->out;
+        $notice = $nli->notice;
+
+        $nli->showNotice($notice, $out);
+
+        $nli->showNoticeLink();
+        $nli->showNoticeSource();
+        $nli->showNoticeLocation();
+        $nli->showPermalink();
+
+        $nli->showNoticeOptions();
+
+        if ($notice->object_type == QnA_Question::OBJECT_TYPE) {
+            $user = common_current_user();
+            $question = QnA_Question::getByNotice($notice);
+
+            if (!empty($user) and !empty($question)) {
+                $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',
+                            // TRANS: Placeholder value for a possible answer to a question
+                            // TRANS: by the logged in user.
+                            'value' => _m('Your answer...')
+                        )
+                    );
+                    $out->elementEnd('li');
+                    $out->elementEnd('ul');
+                }
             }
+        }
 
+        return false;
+    }
+
+    function adaptNoticeListItem($nli) {
+        return new QnAListItem($nli);
+    }
 
+    static function shorten($content, $notice)
+    {
+        $short = null;
+
+        if (Notice::contentTooLong($content)) {
+            common_debug("content too long");
+            $max = Notice::maxContent();
+            // TRANS: Link description for link to full notice text if it is longer than
+            // TRANS: what will be dispplayed.
+            $ellipsis = _m('…');
+            $short = mb_substr($content, 0, $max - 1);
+            $short .= sprintf('<a href="%1$s" rel="more" title="%2$s">%3$s</a>',
+                              $notice->getUrl(),
+                              // TRANS: Title for link that is an ellipsis in English.
+                              _m('more...'),
+                              $ellipsis);
+        } else {
+            $short = $content;
         }
 
-        $out->elementEnd('div');
+        return $short;
     }
 
     /**
@@ -421,10 +409,9 @@ class QnAPlugin extends MicroAppPlugin
      * @param HTMLOutputter $out
      * @return Widget
      */
-
     function entryForm($out)
     {
-        return new QnaquestionForm($out);
+        return new QnanewquestionForm($out);
     }
 
     /**
@@ -432,8 +419,7 @@ class QnAPlugin extends MicroAppPlugin
      *
      * @param Notice $notice
      */
-
-    function deleteRelated($notice)
+    function deleteRelated(Notice $notice)
     {
         switch ($notice->object_type) {
         case QnA_Question::OBJECT_TYPE:
@@ -454,7 +440,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)