public function actionGenerate($id) { $station = $this->loadModel($id); $election = Elections::model()->findAll(); $oldtokens = Tokens::model()->findAll('station_id=:station_id', array(':station_id' => $id)); foreach ($oldtokens as $oldtoken) { Votes::model()->deleteAll('token_id=:token_id', array(':token_id' => $oldtoken->id)); } Tokens::model()->deleteAll('station_id=:station_id', array(':station_id' => $id)); $tokens = array(); for ($i = 0; $i < $station->voters_count; $i++) { $token = new Tokens(); $token->election_id = $election[0]->id; $token->station_id = $id; $token->token = $this->generatePassword(); $prevtoken = Tokens::model()->findAll('token=:token', array(':token' => $token->token)); while ($prevtoken) { $token->token = $this->generatePassword(); $prevtoken = Tokens::model()->findAll('token=:token', array(':token' => $token->token)); } $token->done_vote = 0; $token->save(); $tokens[] = $token; } $this->render('generate', array('election' => $election[0], 'tokens' => $tokens, 'station' => $station)); }
public function actionGenerate($id) { $election = $this->loadModel($id); $oldtokens = Tokens::model()->findAll('election_id=:election_id', array(':election_id' => $id)); foreach ($oldtokens as $oldtoken) { Votes::model()->deleteAll('token_id=:token_id', array(':token_id' => $oldtoken->id)); } foreach ($election->seats() as $seat) { foreach ($seat->candidates() as $candidate) { $candidate->votescount = 0; $candidate->save(); } } Tokens::model()->deleteAll('election_id=:election_id', array(':election_id' => $id)); $tokens = array(); for ($i = 0; $i < $election->voters_count; $i++) { $token = new Tokens(); $token->election_id = $id; $token->token = $this->generatePassword(); $prevtoken = Tokens::model()->findAll('token=:token', array(':token' => $token->token)); while ($prevtoken) { $token->token = $this->generatePassword(); $prevtoken = Tokens::model()->findAll('token=:token', array(':token' => $token->token)); } $token->done_vote = 0; $token->save(); $tokens[] = $token; } $this->redirect(array('Tokens/index', 'election_id' => $election->id)); }
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(); }
/** * Returns the data model based on the primary key given in the GET variable. * If the data model is not found, an HTTP exception will be raised. * @param integer the ID of the model to be loaded */ public function loadModel($id) { $model = Votes::model()->findByPk($id); if ($model === null) { throw new CHttpException(404, 'The requested page does not exist.'); } return $model; }
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 updatecount() { $votes = Votes::model()->findAll(array("condition" => "candidate_id=" . $this->id)); $this->votescount = count($votes); $duplicates = FixVotes::model()->findAll(array("condition" => "candidate_id=" . $this->id)); $fixduplicate = 0; foreach ($duplicates as $duplicate) { $fixduplicate += $duplicate->votecount - 1; } $this->fixduplicate = $fixduplicate; $this->save(false); }