Exemplo n.º 1
0
 /**
  * Retrieves a user's questions
  *
  * @param   string  $kind       The kind of results to retrieve
  * @param   array   $interests  Array of tags
  * @return  array   Database results
  */
 private function _getQuestions($kind = 'open', $interests = array())
 {
     // Get some classes we need
     require_once Component::path('com_answers') . DS . 'models' . DS . 'question.php';
     require_once Component::path('com_answers') . DS . 'helpers' . DS . 'economy.php';
     $limit = intval($this->params->get('limit', 10));
     $tags = null;
     $records = \Components\Answers\Models\Question::all()->including(['responses', function ($response) {
         $response->select('id')->select('question_id');
     }])->whereEquals('state', 0);
     if ($kind == 'mine') {
         $records->whereEquals('created_by', User::get('id'));
     }
     if ($kind == 'interest') {
         $tags = count($interests) <= 0 ? $this->_getInterests() : $interests;
     }
     if ($kind == 'assigned') {
         require_once Component::path('com_tools') . DS . 'tables' . DS . 'author.php';
         $database = \App::get('db');
         $TA = new \Components\Tools\Tables\Author($database);
         $tools = $TA->getToolContributions(User::get('id'));
         if ($tools) {
             foreach ($tools as $tool) {
                 $tags .= 'tool' . $tool->toolname . ',';
             }
             $tags = rtrim($tags, ',');
         }
     }
     if ($tags) {
         $cloud = new \Components\Answers\Models\Tags();
         $tags = $cloud->parse($tags);
         $records->select('#__answers_questions.*')->join('#__tags_object', '#__tags_object.objectid', '#__answers_questions.id')->join('#__tags', '#__tags.id', '#__tags_object.tagid')->whereEquals('#__tags_object.tbl', 'answers')->whereIn('#__tags.tag', $tags);
     }
     $data = $records->limit($limit)->ordered()->rows();
     $results = array();
     foreach ($data as $datum) {
         $datum->set('rcount', $datum->responses->count());
         $results[] = $datum;
     }
     if ($this->banking && $results) {
         $database = \App::get('db');
         $AE = new \Components\Answers\Helpers\Economy($database);
         $awards = array();
         foreach ($results as $result) {
             // Calculate max award
             $result->set('marketvalue', round($AE->calculate_marketvalue($result->get('id'), 'maxaward')));
             $result->set('maxaward', round(2 * ($result->get('marketvalue', 0) / 3)));
             if ($kind != 'mine') {
                 $result->set('maxaward', $result->get('maxaward') + $result->get('reward'));
             }
             $awards[] = $result->get('maxaward', 0);
         }
         // re-sort by max reponses
         array_multisort($awards, SORT_DESC, $results);
     }
     return $results;
 }
Exemplo n.º 2
0
 /**
  * Parse a list of filters to return data for
  *
  * @return  object
  */
 private function _find()
 {
     $records = \Components\Answers\Models\Question::all()->including(['responses', function ($response) {
         $response->select('id')->select('question_id')->where('state', '!=', 2);
     }]);
     if ($this->filters['tag']) {
         $cloud = new \Components\Answers\Models\Tags();
         $tags = $cloud->parse($this->filters['tag']);
         $records->select('#__answers_questions.*')->join('#__tags_object', '#__tags_object.objectid', '#__answers_questions.id')->join('#__tags', '#__tags.id', '#__tags_object.tagid')->whereEquals('#__tags_object.tbl', 'answers')->whereIn('#__tags.tag', $tags);
     }
     if ($this->filters['search']) {
         $this->filters['search'] = strtolower((string) $this->filters['search']);
         $records->whereLike('subject', $this->filters['search'], 1)->orWhereLike('question', $this->filters['search'], 1)->resetDepth();
     }
     if ($this->filters['filterby'] == 'open') {
         $records->whereEquals('state', 0);
     }
     if ($this->filters['filterby'] == 'closed') {
         $records->whereEquals('state', 1);
     }
     if (!$this->filters['filterby'] || $this->filters['filterby'] == 'both') {
         $records->where('state', '<', 2);
     }
     return $records;
 }