/** * If there is a poll available within range, take the last one and return that poll. * @return mixed:boolean|String False if none found. String of HTML if found. */ public function PollForm() { $poll = Poll::get()->filter(array('Startdate:LessThan' => date('Y-m-d'), 'Enddate:GreaterThan' => date('Y-m-d'))); if ($poll->count()) { return PollForm::create(Controller::curr(), 'PollForm', $poll->last()); } return false; }
public static function action_vote($id = null) { if (!$id) { if ($_SERVER['REQUEST_METHOD'] !== 'POST') { return Response::error(404); } else { $id = (int) Param::post('id'); $answers = Param::post('answers'); $poll = Poll::get($id); $poll->various_answers = '0' != $poll->various_answers; $cookiename = 'p_' . $poll->id . '_v'; // Si no hay respuestas o hay más de una respuesta if (count($answers) === 0 || !$poll->various_answers && count($answers) > 1) { Redirect::to(Url::get('vote', $id, 'vote_error=true')); } if (Vote::where('voter_ip', '=', CURRENT_USER_IP)->and_where('poll_id', '=', $id)->first() || Cookie::get($cookiename)) { Cookie::set($cookiename, !$poll->various_answers ? (string) $answers[0] : 'true', 360); Redirect::to(Url::get('view', $poll->slug, 'poll_already_voted=true')); } Cookie::set($cookiename, !$poll->various_answers ? (string) $answers[0] : 'true', 360); Vote::create(array('voter_ip' => CURRENT_USER_IP, 'poll_id' => $id, 'answer_id' => !$poll->various_answers ? $answers[0] : 0)); foreach ($answers as $answer_id) { Answer::find($answer_id)->set(array('nofilter:votes' => '`votes` + 1')); } Poll::find($id)->set(array('nofilter:total_votes' => '`total_votes` + 1')); Redirect::to(Url::get('view', $poll->slug, 'voted=true')); } } elseif (!is_numeric($id)) { return Response::error(404); } $id = intval($id, 10); if (!($poll = Poll::get($id))) { return Response::error(404); } $answers = Answer::where('poll_id', '=', $poll->id)->get(); return View::make('vote')->add_var('poll', $poll)->add_var('answers', $answers); }
public static function action_edit($id = null) { if (!IS_ADMIN) { Redirect::to(Url::get('admin@login', null, 'redirect-to=' . urlencode(Url::current()))); } if ($_SERVER['REQUEST_METHOD'] === 'POST') { if ($id === 'delete-answer') { if (($answer_id = Param::post('answer_id')) && is_numeric($answer_id)) { $answer = Answer::get((int) $answer_id); Answer::find((int) $answer_id)->delete(); $votes = Vote::where('answer_id', '=', $answer_id)->count(); if ($votes) { Poll::find($answer->poll_id)->set(array('nofilter:total_votes' => "`total_votes` - {$votes}")); } return Response::json(array('status' => 200, 'deleted' => true)); } else { return Response::json(array('status' => 0, 'deleted' => false)); } } elseif ($id) { return Response::error(404); } else { $id = Param::post('id'); if ($answer_id = Param::post('remove_answer')) { Answer::find((int) $answer_id)->and_where('poll_id', '=', $id)->delete(); $votes = Vote::where('answer_id', '=', $answer_id)->count(); if ($votes) { Poll::find($id)->set(array('nofilter:total_votes' => "`total_votes` - {$votes}")); } Redirect::to(Url::get('admin@edit', $id, 'answer_deleted=true')); } if (Param::post('remove_poll')) { Poll::find($id)->delete(); Redirect::to(Url::get('admin', null, 'poll_deleted=true')); } if (is_numeric($id) && ($poll = Poll::get((int) $id))) { foreach ($_POST as $key => $value) { if (isset($poll->{$key}) && (!empty($_POST[$key]) || $_POST[$key] === "0")) { $poll->{$key} = is_numeric($_POST[$key]) ? intval($_POST[$key], 10) : $_POST[$key]; } elseif (false !== strpos($key, 'answer-')) { $answer_id = explode('-', $key); $answer_id = $answer_id[1]; if (is_numeric($answer_id)) { Answer::find((int) $answer_id)->set(array('text' => $value)); } } elseif ($key === 'new_answers') { foreach ($value as $new_answer) { if (!empty($new_answer)) { Answer::create(array('poll_id' => (int) $poll->id, 'text' => $new_answer)); } } } } Poll::save($poll); Redirect::to(Url::get('admin', null, 'success=' . $_POST['id'] . '&updated=true')); } else { return Response::error(500); } } } if (!$id || !is_numeric($id) || !($poll = Poll::get((int) $id))) { return Response::error(404); } else { $answers = Answer::where('poll_id', '=', $poll->id)->get(); return View::make('admin.edit')->add_var('answers', $answers)->add_var('poll', $poll); } }
public function __construct($pollId) { $this->poll = Poll::get($pollId); if (!$this->poll || !Team::isMember()) { HTMLResponse::exitWithRoute('/votaciones/'); } }