Пример #1
0
 public function getRating()
 {
     $required = array('user_id' => false, 'start_date' => false, 'end_date' => false, 'category_id' => true);
     $this->params = $this->controller->getParams($required);
     if (isset($this->params['start_date']) && isset($this->params['end_date'])) {
         $model = QaRatingHistory::model()->between($this->params['start_date'], $this->params['end_date']);
         $this->history = true;
     } else {
         $model = QaRating::model();
     }
     $model->byCategory($this->params['category_id']);
     if (isset($this->params['user_id'])) {
         $model->byUser($this->params['user_id']);
     }
     //temp
     if ($this->history) {
         $model->getDbCriteria()->select = $model->getTableAlias() . '.user_id, count(*) as points';
         $model->getDbCriteria()->group = $model->getTableAlias() . '.user_id';
         $model->getDbCriteria()->order = 'points DESC';
         $result = array();
         $records = $model->findAll($this->controller->getPaginationParams());
         if (count($records) == 0) {
             throw new NotFoundApiException();
         }
         foreach ($records as $record) {
             $user = \User::model()->findByPk($record->user_id);
             Formatter::format($user);
             $result[] = array('user_id' => (int) $record->user_id, 'total_count' => (int) $record->points, 'answers_count' => (int) QaRatingHistory::model()->byCategory($this->params['category_id'])->byUser($record->user_id)->between($this->params['start_date'], $this->params['end_date'])->byModel((new \ReflectionClass(QaAnswer::model()))->getShortName())->count(), 'votes_count' => (int) QaRatingHistory::model()->byCategory($this->params['category_id'])->byUser($record->user_id)->between($this->params['start_date'], $this->params['end_date'])->byModel((new \ReflectionClass(QaAnswerVote::model()))->getShortName())->count(), 'user' => $this->getController()->getFilteredAttributes($user));
         }
         echo \CJSON::encode($result);
         die;
     } else {
         $this->controller->get($model, $this);
     }
 }
Пример #2
0
 function postProcessing(&$data)
 {
     if ($data[0]['votesCount'] > 0) {
         $vote = QaAnswerVote::model()->byAnswer($data[0]['id'])->user($this->controller->identity->getId())->find();
         $data[0]['has_my_vote'] = $vote != null;
     } else {
         $data[0]['has_my_vote'] = false;
     }
 }
Пример #3
0
 public function postProcessing(&$data)
 {
     $getQuestionAuthor = in_array('question', $this->controller->getWithParameters(QaAnswer::model(), true));
     for ($i = 0; $i < count($data); $i++) {
         /**
          * @var QaAnswer $temp
          */
         $temp = QaAnswer::model()->findByPk($data[$i]['id']);
         if (!$temp->question) {
             $data[$i] = null;
             continue;
         }
         if ($getQuestionAuthor) {
             $questionAuthor = \User::model()->findByPk($data[$i]['question']['authorId']);
             Formatter::format($questionAuthor);
             $data[$i]['question']['author'] = $this->getController()->getFilteredAttributes($questionAuthor);
         }
         if ($temp->votesCount > 0) {
             $vote = QaAnswerVote::model()->byAnswer($temp->id)->user($this->controller->identity->getId())->find();
             $data[$i]['has_my_vote'] = $vote != null;
         } else {
             $data[$i]['has_my_vote'] = false;
         }
         $favourite = Favourite::model()->byUser($this->controller->identity->getId())->byModel(get_class(QaAnswer::model()), $data[$i]['id'])->find();
         $data[$i]['is_my_favourite'] = $favourite == null ? false : true;
     }
     $data = array_map(function ($item) {
         if ($item != null) {
             return $item;
         }
     }, $data);
     if ($question_id = \Yii::app()->request->getParam('question_id', null)) {
         $best = QaAnswer::model()->findByAttributes(array('questionId' => $question_id, 'isBest' => 1));
         if ($best) {
             $newData = array();
             $newData['answers'] = $data;
             Formatter::format($best);
             $newData['best'] = $this->getController()->getFilteredAttributes($best);
             if (in_array('author', $this->controller->getWithParameters(QaAnswer::model(), true))) {
                 $author = \User::model()->findByPk($best->authorId);
                 Formatter::format($author);
                 $newData['best']['author'] = $this->getController()->getFilteredAttributes($author);
             }
             $data = $newData;
         }
     }
 }