private function placeVote($sessionId, $memberId, $voteValue) { // Fetch entities $session = $this->getSession($sessionId); $currentPoll = $session->getCurrentPoll(); $member = $this->getMember($memberId); // Reject votes if poll is completed if ($currentPoll != null && $currentPoll->getResult() > 0) { throw new Exception("Can not vote on completed polls!"); } // Find or create vote foreach ($currentPoll->getVotes() as $vote) { if ($vote->getMember() == $member) { $match = $vote; } } // Create vote if not found if (!isset($match)) { $match = new Vote(); $match->setPoll($currentPoll); $match->setMember($member); } // Set value $match->setValue($voteValue); // Evaluate the poll $this->evaluatePoll($session, $currentPoll); if ($currentPoll->getResult() > 0) { $this->highlightVotes($currentPoll); } // Save all to db $this->saveAll([$match, $currentPoll]); $this->saveAll($currentPoll->getVotes()->toArray()); }
private function placeVote($sessionId, $memberId, $voteValue) { // Fetch entities $session = $this->getSession($sessionId); $currentPoll = $session->getCurrentPoll(); $member = $this->getMember($memberId); // Find or create vote foreach ($currentPoll->getVotes() as $vote) { if ($vote->getMember() == $member) { $match = $vote; } } // Create vote if not found if (!isset($match)) { $match = new Vote(); $match->setPoll($currentPoll); $match->setMember($member); } // Set value $match->setValue($voteValue); // Evaluate current poll $this->evaluatePoll($session, $currentPoll); // Save all to db $this->saveAll([$match, $currentPoll]); }
public function executeVote(sfWebRequest $request) { $this->forward404Unless($request->isMethod('post')); $user = $this->getUser(); $blog_id = $user->getAttribute('blog_id'); $vote_form = new VoteForm(); $bind_value = array('blog_id' => $blog_id, 'value' => $request->getParameter('vote')); $vote_form->bind($bind_value); if ($vote_form->isValid()) { $vote = new Vote(); $vote->setBlog_id($vote_form->getValue('blog_id')); $vote->setValue($vote_form->getValue('value')); $vote->save(); // Update vote_SUM Doctrine_Query::create()->update('Blog b')->set('b.vote_sum', 'b.vote_sum + ' . $vote_form->getValue('value'))->where('b.id = ?', $vote_form->getValue('blog_id'))->execute(); // Update vote_COUNT Doctrine_Query::create()->update('Blog b')->set('b.vote_count', 'b.vote_count + 1')->where('b.id = ?', $vote_form->getValue('blog_id'))->execute(); if (sfConfig::get('sf_exclusion_list')) { // Exclusion list! $session_list = $user->getAttribute('already_voted_list'); if (is_array($session_list) && count($session_list) > 0) { } else { //No data in the array $session_list = array(); } $session_list[] = $vote_form->getValue('blog_id'); $user->setAttribute('already_voted_list', $session_list); } // Check if the vote history is empty it it is initiate an empty list $vote_history = $user->getAttribute('vote_history'); if (is_array($vote_history) && count($vote_history) > 0) { } else { $vote_history = array(); } // Add the just rated blog to the vote_history $voted_blog = Doctrine::getTable('Blog')->find($vote_form->getValue('blog_id')); $vote_avg = $voted_blog->getVote_sum() / $voted_blog->getVote_count(); $vote_info = array('my_vote' => $vote_form->getValue('value'), 'vote_avg' => $vote_avg, 'vote_count' => $voted_blog->getVote_count(), 'vote_sum' => $voted_blog->getVote_sum(), 'blog_url' => $voted_blog->getUrl(), 'blog_thmbnail' => $voted_blog->getThumbnail_url()); array_unshift($vote_history, $vote_info); $user->setAttribute('vote_history', $vote_history); $this->redirect('blog/index'); } }
function survey_submit() { // get global user object global $user; // protect from unauthorized access if (!isset($user) || !isset($_POST['formSurveySubmit'])) { logout(); die; } // create empty array for $_POST container $post = array(); // escape mysql injections array foreach ($_POST as $key => $value) { $post[$key] = stripslashes($value); } $post_keys = array_keys($_POST); $substring = 'Answer'; $pattern = '/' . $substring . '/'; $survey_keys = preg_grep($pattern, $post_keys); foreach ($survey_keys as $key) { // get question preg_match_all('!\\d+!', $key, $matches); $question_id = $matches[0][0]; $question = new Question(); $question->get_from_db($question_id); //get answer value $answer_value = $_POST[$key]; //get answer id $answer_id = $answer_value; if (isset($matches[0][1])) { $answer_id = $matches[0][1]; } // get current time $time_now = date("Y-m-d H:i:s"); // create vote object $vote = new Vote(); $vote->setIsActive(1); $vote->setCreatedOn($time_now); $vote->setLastEditedOn($time_now); $vote->setUser($user->getId()); $vote->setSurvey($question->getSurvey()); $vote->setQuestion($question_id); $vote->setAnswer($answer_id); $vote->setValue($answer_value); $vote->store_in_db(); } // set message cookie $cookie_key = 'msg'; $cookie_value = 'Благодарим Ви за отговорения въпрос!'; setcookie($cookie_key, $cookie_value, time() + 1); header('location:' . ROOT_DIR . '?page=survey'); }
/** * @expectedException InvalidArgumentException */ public function testSetZeroVoteValue() { $vote = new Vote(); $vote->setValue(0); }