/**
  * Aggregate contributions by contributor.
  *
  * Returns results as ContributionByContributor array, wich contains contributor interface and contributions count.
  *
  * @return ContributionByContributor[]
  */
 public function aggregateByContributor()
 {
     $query = ContributionReview::find()->select(['repo_type', 'contributor_name', 'contributor_email', 'contributor_id', 'project_id', new Expression('COUNT(*) as cnt')])->andWhere(['or', ['reviewer_id' => $this->reviewerId], ['reviewer_id' => null]])->groupBy(['project_id', 'repo_type', 'contributor_id', 'contributor_name', 'contributor_email']);
     if ($this->projectId) {
         $query->andWhere(['project_id' => $this->projectId]);
     }
     if ($this->type == self::TYPE_FINISHED) {
         $query->andWhere(['not', 'reviewed' => null]);
     } elseif ($this->type == self::TYPE_NOT_FINISHED) {
         $query->andWhere(['reviewed' => null]);
     }
     $res = $query->createCommand()->query();
     /* @var $contributorsApi ContributorApi */
     $contributorsApi = Yii::$app->contributors;
     $result = [];
     foreach ($res as $item) {
         $contributor = null;
         if (!is_null($item['contributor_id'])) {
             $contributor = $contributorsApi->getContributorById($item['contributor_id']);
         } else {
             $contributor = $contributorsApi->getContributor($item['repo_type'], $item['contributor_name'], $item['contributor_email']);
         }
         $result[] = new ContributionByContributor(['contributor' => $contributor, 'cnt' => $item['cnt']]);
     }
     return $result;
 }
 /**
  * Render commit view.
  * If has CommonException - it's may be only not founded commit - generates 404.
  *
  * @return string
  * @throws NotFoundHttpException
  */
 public function run()
 {
     /* @var $commit BaseCommit */
     $commit = null;
     try {
         // get commit model by commit identifier
         $commit = $this->repository->getCommit($this->commitId);
     } catch (CommonException $ex) {
         throw new NotFoundHttpException(Yii::t('app', 'System error: {message}', ['message' => $ex->getMessage()]), $ex->getCode(), $ex);
     }
     // get contribution review model
     $reviewModel = ContributionReview::find()->andWhere(['project_id' => $this->project->id, 'commit_id' => $commit->getId()])->one();
     return $this->controller->render('commit/summary', ['project' => $this->project, 'repository' => $this->repository, 'commit' => $commit, 'reviewModel' => $reviewModel]);
 }
 /**
  * To be reviewer
  *
  * @param integer $projectId Project identifier
  * @param string $commitId Commit identifier
  *
  * @return array
  */
 public function actionCreateSelfReview($projectId, $commitId)
 {
     Yii::$app->response->format = Response::FORMAT_JSON;
     $project = $this->findProject($projectId);
     $commit = $this->findCommit($project, $commitId);
     $result = ['success' => false, 'html' => '', 'message' => ''];
     // find existent review model
     $model = ContributionReview::find()->andWhere(['project_id' => $project->id, 'commit_id' => $commit->getId()])->one();
     if ($model && $model->reviewer_id == Yii::$app->user->getId()) {
         // user try to create existent model
         // it's not an error
         $result['success'] = true;
     } elseif ($model && is_null($model->reviewer_id)) {
         // model exists, but reviewer is not set
         $model->reviewer_id = Yii::$app->user->getId();
         if ($model->save()) {
             $result['success'] = true;
         }
     } elseif ($model) {
         // other reviewer already installed
         $result['success'] = false;
         $result['message'] = Yii::t('project', 'Reviewer is already installed to this contribution');
     } else {
         // model is not exists
         // create it
         $contributor = $this->contributorApi->getContributor($project->repo_type, $commit->contributorName, $commit->contributorEmail);
         $reviewer = $this->contributorApi->getContributorById(Yii::$app->user->getId());
         $model = $this->projectApi->createContributionReview($project, $commit, $contributor, $reviewer);
         if (!$model instanceof ContributionReview) {
             $result['success'] = false;
             $result['message'] = Yii::t('project', 'An error occurred during the creation of the review');
         } else {
             $result['success'] = true;
         }
     }
     if ($result['success'] && $model) {
         $result['html'] = CommitPanel::widget(['reviewModel' => $model, 'authUser' => Yii::$app->user, 'contributor' => $model->contributor, 'project' => $project, 'commit' => $commit, 'reviewButtonClass' => 'js-review-button']);
     }
     return $result;
 }