Example #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);
     }
 }
Example #2
0
 public function getQuestions()
 {
     $required = array('category_id' => false, 'user_id' => false, 'empty' => false, 'answer_id' => false);
     $params = $this->controller->getParams($required);
     if (isset($params['answer_id'])) {
         $answer = QaAnswer::model()->findByPk($params['answer_id']);
         if (!$answer) {
             throw new NotFoundApiException();
         }
         $question = $answer->question;
         $this->controller->data = $question;
         return;
     }
     $model = QaQuestion::model();
     if (isset($params['empty'])) {
         $model->unanswered();
     }
     if (isset($params['category_id'])) {
         $model->category($params['category_id']);
     }
     if (isset($params['user_id'])) {
         $model->user($params['user_id']);
     }
     $this->controller->get($model, $this);
 }
Example #3
0
 public function vote()
 {
     $required = array('answer_id' => true);
     $params = $this->controller->getParams($required);
     /**
      * @var QaAnswer $answer
      */
     $answer = QaAnswer::model()->findByPk($params['answer_id']);
     if ($this->controller->identity->getId() == $answer->authorId) {
         throw new AccessDeniedApiException();
     }
     $this->controller->data = VotesManager::changeVote($this->controller->identity->getId(), $params['answer_id']);
 }
Example #4
0
 public function deleteAnswer()
 {
     $this->controller->getActionProperties()->setModel(QaAnswer::model())->setRequire(array('id' => true, 'action' => true))->setAccess(array('removeQaAnswer', array('entity' => 'Record')));
     $this->controller->delete();
 }