/** * Creates a new model. * If creation is successful, the browser will be redirected to the 'view' page. */ public function actionCreate() { $model = new Votes(); // Uncomment the following line if AJAX validation is needed // $this->performAjaxValidation($model); if (isset($_POST['Votes'])) { $model->attributes = $_POST['Votes']; if ($model->save()) { $this->redirect(array('Votes/view', 'id' => $model->id)); } } $this->render('create', array('model' => $model)); }
public function actionRating() { $this->retVal = new stdClass(); $request = Yii::app()->request; if ($request->isPostRequest && isset($_POST)) { $ratingCriteria = new CDbCriteria(); $ratingCriteria->select = "*"; $ratingCriteria->condition = "teacher_id = " . StringHelper::filterString($_POST['teacher_id']); $rating = Votes::model()->findAll($ratingCriteria); $count = count($rating); $averageRatingScore = 0; $this->retVal->checkRatingStatus = 0; foreach ($rating as $rating) { $averageRatingScore += $rating["rating_score"]; if ($rating->user_id == Yii::app()->session['user_id']) { $this->retVal->checkRatingStatus = 1; } } if ($this->retVal->checkRatingStatus === 0) { $teacher = Teacher::model()->find(array('select' => '*', 'condition' => 'teacher_id = ' . StringHelper::filterString($_POST['teacher_id']))); $ratingScore = ($averageRatingScore + $_POST['rating_score']) / ($count + 1); $teacher->teacher_rate = round($ratingScore, 1); $teacher->save(FALSE); $vote = new Votes(); $vote->teacher_id = StringHelper::filterString($_POST['teacher_id']); $vote->user_id = Yii::app()->session['user_id']; $vote->rating_score = StringHelper::filterString($_POST['rating_score']); $vote->save(FALSE); $this->retVal->count = $count + 1; $this->retVal->aver = round($ratingScore, 1); $this->retVal->score = round($ratingScore); } else { $this->retVal->message = "Bạn đã đánh giá thầy/cô này."; } } echo CJSON::encode($this->retVal); Yii::app()->end(); }
public function actionVotes() { if ($_POST['voteType'] == 'like') { $model = new Votes(); $model->question_id = $_POST['qId']; $model->user_id = $_POST['id']; $model->voter_id = Yii::app()->session['userId']; if ($_POST['id'] == Yii::app()->session['userId']) { echo "You cannot vote Your own question"; die; } $rec = Votes::model()->findByAttributes(array('voter_id' => Yii::app()->session['userId'])); if ($rec) { echo "You already vote this question"; die; } if (Yii::app()->session['userId'] == "") { echo "You must have login to vote the question"; die; } $model->status = 1; if ($model->save(false)) { } } else { $rec = Votes::model()->findByAttributes(array('voter_id' => Yii::app()->session['userId'])); if ($rec) { echo "You already vote this question"; die; } if ($_POST['id'] == Yii::app()->session['userId']) { echo "You cannot vote Your own question"; die; } if (Yii::app()->session['userId'] == "") { echo "You must have login to vote the question"; die; } $model = new Votes(); $model->question_id = $_POST['qId']; $model->user_id = $_POST['id']; $model->status = 0; if ($model->save(false)) { // echo "dislike"; } } }
public function downAction() { $response = new ApiResponse(); $value = -1; if ($this->request->isGet()) { $questions_id = $this->request->get('questions_id'); $answers_id = $this->request->get('answers_id'); $users_id = $this->request->get('users_id'); if ($questions_id != null && $answers_id == null) { $question = Questions::findFirstById($questions_id); if ($question == false) { $response->setResponseError('Question not found!'); return $response; } else { if ($question->users_id != $users_id && !Votes::checkIfVotedQuestion($users_id, $questions_id)) { $question->downvotes += 1; if ($question->save() == false) { $response->setResponseError($question->getMessages()); return $response; } $vote = new Votes(); $vote->id = uniqid(); $vote->questions_id = $questions_id; $vote->users_id = $users_id; $vote->value = $value; if ($vote->save() == false) { $response->setResponseError($vote->getMessages()); return $response; } else { $response->setResponseMessage("Create vote down {$vote->id} for question {$questions_id} successfully!"); } } else { $response->setResponseError('Cant re-vote or vote for yourself!'); return $response; } } } else { if ($questions_id == null && $answers_id != null) { $answer = Answers::findFirstById($answers_id); if ($answer == false) { $response->setResponseError('Answer not found!'); return $response; } else { if ($answer->users_id != $users_id && !Votes::checkIfVotedAnswer($users_id, $answers_id)) { var_dump(Votes::checkIfVotedAnswer($users_id, $answers_id)); $answer->downvotes += 1; if ($answer->save() == false) { $response->setResponseError($answer->getMessages()); return $response; } $vote = new Votes(); $vote->id = uniqid(); $vote->answers_id = $answers_id; $vote->users_id = $users_id; $vote->value = $value; if ($vote->save() == false) { $response->setResponseError($vote->getMessages()); return $response; } else { $response->setResponseMessage("Create vote down {$vote->id} for answer {$answers_id} successfully!"); } } else { $response->setResponseError('Cant re-vote or vote for yourself!'); return $response; } } } } } else { $response->setResponseError('Wrong HTTP Method'); } return $response; }