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); } }
public function postProcessing(&$data) { include_once \Yii::getPathOfAlias('site.frontend.vendor.simplehtmldom_1_5') . DIRECTORY_SEPARATOR . 'simple_html_dom.php'; for ($i = 0; $i < count($data); $i++) { $data[$i]['purifiedHtml'] = Comment::model()->findByPk($data[$i]['id'])->purified->text; $data[$i]['text'] = HtmlParser::handleHtml($data[$i]['text'], $data[$i])->outertext; $data[$i]['purifiedHtml'] = HtmlParser::handleHtml($data[$i]['purifiedHtml'], $data[$i])->outertext; if (in_array('answers', $this->controller->getWithParameters(Comment::model(), true))) { $answers = Comment::model()->findAll(array('condition' => 'root_id = ' . $data[$i]['id'] . ' and id != ' . $data[$i]['id'])); if ($answers) { $data[$i]['answers'] = array(); foreach ($answers as $answer) { Formatter::format($answer); $temp = $answer->getAttributes(); $temp['text'] = HtmlParser::handleHtml($temp['text'], $temp)->outertext; Formatter::formatUser($answer->author); Formatter::formatCommon($answer->author); $temp['author'] = $answer->author->getAttributes(Filter::getFilter($answer->author->getAttributes(), get_class($answer->author))); //Formatter::format($temp); $data[$i]['answers'][] = $temp; } } } } }
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; } } }
public function postProcessing(&$data) { if ($this->controller->requestType != 'Param' || isset($data[0]['message'])) { return; } $params = $this->controller->getParams(array('read' => false, 'count' => false)); $fields = array('readEntities', 'unreadEntities'); if (isset($params['count'])) { $counter = 0; for ($i = 0; $i < count($data); $i++) { $counter += count($data[$i]['unreadEntities']); } $this->controller->setMessage($counter); return; } for ($i = 0; $i < count($data); $i++) { /** * @var \EMongoDocument $item */ foreach ($fields as $field) { if ($data[$i][$field] == null) { continue; } foreach ($data[$i][$field] as $key => $item) { $data[$i][$field][$key] = $item->getAttributes(); $user = \User::model()->findByPk($item->userId); Formatter::format($user); $data[$i][$field][$key]['user'] = $this->controller->getFilteredAttributes($user); } } } }
/** * Fill data field with array of user and token keys * In user key instance of user * In token key just created api token * * @param \User $user */ public function toAuthArray($user) { if ($user instanceof \User) { /** * @var UserApiToken $token */ $token = UserApiToken::model()->byAppId($this->getApplication()->_id->{'$id'})->findByPk($user->id . $this->getApplication()->_id); if (!$token) { //support old tokens where app_id is ObjectId $token = UserApiToken::model()->byAppId($this->getApplication()->_id)->findByPk($user->id . $this->getApplication()->_id); if (!$token) { $token = UserApiToken::model()->create($user, $this->getApplication()->_id->{'$id'}); } } Formatter::format($token); $user->refresh(); $this->data = array('user' => $user, 'token' => $token); } else { $this->setError('WrongAuthArrayParam', 500); } }