Skip to content

Commit

Permalink
Choice could be changed
Browse files Browse the repository at this point in the history
  • Loading branch information
chiliec committed Jan 19, 2015
1 parent 93658d6 commit 115da92
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 3 deletions.
11 changes: 9 additions & 2 deletions actions/VoteAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public function run()
return ['content' => Yii::t('vote', 'Wrong action'), 'successfully' => false];
}

$isVoted = Rating::findOne(["model_id"=>$model_id, "target_id"=>$target_id, "user_id"=>$user_id]);
$isVoted = Rating::findOne(['model_id'=>$model_id, 'target_id'=>$target_id, 'user_id'=>$user_id]);
if(is_null($isVoted)) {
$newVote = new Rating;
$newVote->model_id = $model_id;
Expand All @@ -68,7 +68,14 @@ public function run()
return ['content' => Yii::t('vote', 'Validation error'), 'successfully' => false];
}
} else {
return ['content' => Yii::t('vote', 'You have already voted!'), 'successfully' => false];
if($isVoted->value == $act) {
return ['content' => Yii::t('vote', 'You have already voted!'), 'successfully' => false];
} else {
$isVoted->value = $act;
if($isVoted->save()) {
return ['content' => Yii::t('vote', 'Your vote has been changed. Thanks!'), 'successfully' => true];
}
}
}
} else {
throw new MethodNotAllowedHttpException(Yii::t('vote', 'Forbidden method'), 405);
Expand Down
1 change: 1 addition & 0 deletions messages/ru/vote.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
'The purpose is not defined' => 'Цель не определена',
'Wrong action' => 'Неправильное действие!',
'Your vote is accepted. Thanks!' => 'Голос принят. Спасибо!',
'Your vote has been changed. Thanks!' => 'Голос изменен. Спасибо!',
'Thanks for your opinion' => 'Спасибо за ваше мнение',
'Validation error' => 'Ошибка валидации',
'You have already voted!' => 'Вы уже голосовали!',
Expand Down
3 changes: 2 additions & 1 deletion models/Rating.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ public function rules()
{
return [
[['model_id', 'target_id', 'user_id', 'value'], 'required'],
[['model_id', 'target_id', 'value', 'date'], 'integer'],
[['model_id', 'target_id', 'value'], 'integer'],
[['date'], 'safe'],
[['user_id'], 'string', 'max' => 16]
];
}
Expand Down

0 comments on commit 115da92

Please sign in to comment.