Ejemplo n.º 1
0
 /**
  * Get html div with answers for this one question,
  * sorted according to param passed in request
  *
  * Enclose result in <div> and add pagination to bottom
  * of div if there is any pagination necessary!
  *
  * @todo add skip() and limit() to cursor
  *       in order to use pagination.
  *
  * @param Question $Question
  *
  * @param string   $result desired format of result. Possible
  *                         options are: html, array or json object
  *
  *
  *
  * @throws Exception
  * @return string html block
  */
 public function getAnswers(Question $Question, $result = 'html')
 {
     $qid = $Question[Schema::PRIMARY];
     $url = $Question['url'];
     $pageID = $this->Registry->Router->getPageID();
     d('url: ' . $url . ' $pageID: ' . $pageID);
     $this->pagetPath = $Question->getUrl() . '/';
     $urlParts = $this->Registry->Ini->getSection('URI_PARTS');
     $cond = $this->Registry->Router->getSegment(3, 's', $urlParts['SORT_RECENT']);
     d('cond: ' . $cond);
     $noComments = false === (bool) $this->Registry->Ini->MAX_COMMENTS;
     d('no comments: ' . $noComments);
     $aFields = $noComments || false === (bool) $this->Registry->Ini->SHOW_COMMENTS ? array('comments' => 0) : array();
     /**
      * Extra security validation,
      * IMPORTANT because we should never use
      * anything in Mongo methods directly from
      * user input
      */
     if (!in_array($cond, array($urlParts['SORT_RECENT'], $urlParts['SORT_BEST'], $urlParts['SORT_OLDEST']))) {
         throw new Exception('Invalid value of param "cond" was: ' . $cond);
     }
     $where = array(Schema::QUESTION_ID => $qid);
     /**
      * Important as of version 0.2 no longer using i_del_ts
      * as indicator of deleted status - instead using i_status
      */
     if (!$this->Registry->Viewer->isModerator()) {
         d('not moderator. Get only questions with status 1');
         $where[Schema::RESOURCE_STATUS_ID] = Schema::POSTED;
     } else {
         $where[Schema::RESOURCE_STATUS_ID] < Schema::DELETED;
     }
     switch ($cond) {
         case $urlParts['SORT_RECENT']:
             /**
              * Accepted answer will always be the first one,
              * then most recently modified
              */
             $sort = array(Schema::IS_ACCEPTED => -1, Schema::LAST_MODIFIED_TIMESTAMP => -1);
             break;
         case $urlParts['SORT_OLDEST']:
             $sort = array(Schema::CREATED_TIMESTAMP => 1);
             break;
         case $urlParts['SORT_BEST']:
         default:
             /**
              * Accepted answer will be first
              * then most highly voted
              */
             $sort = array(Schema::IS_ACCEPTED => -1, Schema::VOTES_SCORE => -1);
     }
     $cursor = $this->Registry->Mongo->ANSWERS->find($where, $aFields);
     d('$cursor: ' . gettype($cursor));
     $cursor->sort($sort);
     $oPager = Paginator::factory($this->Registry);
     $oPager->paginate($cursor, $this->Registry->Ini->PER_PAGE_ANSWERS, array('path' => $this->pagetPath . $cond, 'append' => false, 'currentPage' => $pageID));
     $pagerLinks = $oPager->getLinks();
     $ownerId = $Question[Schema::POSTER_ID];
     $showLink = $ownerId > 0 && ($this->Registry->Viewer->isModerator() || $ownerId == $this->Registry->Viewer->getUid());
     $noComments = $noComments ? ' nocomments' : '';
     $func = function (&$a) use($showLink, $noComments) {
         /**
          * Don't show Accept link for
          * already accepted answer
          */
         if (!$a['accepted']) {
             if ($showLink) {
                 $a['accept_link'] = '<a class="accept ttt" title="@@Click to accept this as best answer@@" href="{_WEB_ROOT_}/{_accept_}/' . $a['_id'] . '">@@Accept@@</a>';
             }
         } else {
             $a['accepted'] = '<img src="{_IMAGE_SITE_}{_DIR_}/images/accepted.png" alt="@@Best answer@@" class="ttt" title="@@Owner of the question accepted this as best answer@@">';
         }
         $a['nocomments'] = $noComments;
         $a['edited'] = '@@Edited@@';
     };
     /**
      * Create div with answers, append pagination links
      * to bottom and return the whole div block
      */
     $answers = \tplAnswer::loop($cursor, true, $func) . $pagerLinks;
     return $answers;
 }