Esempio n. 1
0
File: Polls.php Progetto: xJakub/LCE
    /**
     * @return void
     */
    public function show()
    {
        if (!TwitterAuth::isLogged()) {
            ?>
            Sólo los miembros pueden ver esta página.
            <a href="<?=HTMLResponse::getRoute()?>?authenticate=1">
                Inicia sesión.
            </a><br>
            <?
            return;
        }
        else if (!Team::isMember()) {
            ?>
            Sólo los miembros pueden ver esta página.<br>
            <?
            return;
        }
        else {
            ?><div style="text-align: left; margin: 0 auto" class="inblock"><?
            if (Team::isMember()) {
                ?><ul><?
                foreach(Poll::find('isvisible order by dateline desc') as $poll) {
                    ?>
                    <li>
                        <a href="/votaciones/<?=$poll->pollid?>/">
                            <?=htmlentities($poll->title)?>
                        </a>
                    </li>
                    <?
                }
                ?></ul><?

                ?>
                <a href="/votaciones/crear/">
                    Haz click aquí para añadir una nueva votación.
                </a>
                <?
            }
            ?></div><br><br><?

        }
    }
Esempio n. 2
0
 public function updatePoll()
 {
     $poll_title = Input::get('poll_title');
     $poll_id = Input::get('poll_id');
     $new_poll_questions = Input::get('new_poll_questions');
     $new_poll_questions_count = count($new_poll_questions);
     $poll = Poll::find($poll_id);
     if (isset($poll_title)) {
         $poll->title = $poll_title;
         $poll->save();
     }
     for ($i = 0; $i < $new_poll_questions_count; $i++) {
         if (isset($new_poll_questions[$i])) {
             $question = Question::create(array('created_by' => Auth::user()->id, 'content' => $new_poll_questions[$i], 'poll_id' => $poll_id));
             $question->save();
         }
     }
     foreach ($poll->questions as $question) {
         $save_question = Question::find($question->id);
         $save_question->content = Input::get('question-' . $question->id);
         $save_question->save();
         foreach ($question->answers as $answer) {
             $save_answer = Answer::find($answer->id);
             $save_answer->content = Input::get('answer-' . $answer->id);
             $save_answer->save();
         }
         $new_poll_answers = Input::get('new_poll_answers-' . $question->id);
         if (isset($new_poll_answers)) {
             $new_poll_answers_count = count($new_poll_answers);
             for ($i = 0; $i < $new_poll_answers_count; $i++) {
                 if (isset($new_poll_answers[$i])) {
                     $answer = Answer::create(array('created_by' => Auth::user()->id, 'content' => $new_poll_answers[$i], 'question_id' => $question->id));
                     $answer->save();
                 }
             }
         }
     }
     return Redirect::action('PollController@editPoll', array('title' => $poll_title));
     //	return View::make('edit-poll')
     //			->with('poll',$poll);
 }
Esempio n. 3
0
 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);
 }
Esempio n. 4
0
 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);
     }
 }
Esempio n. 5
0
 /**
  * Show the form for editing the specified resource.
  * GET /api/apipoll/{id}/edit
  *
  * @param  int  $id
  * @return Response
  */
 public function postCastVote()
 {
     $this->googleAnalytics('/polls/cast-vote/');
     $rules = array('poll_id' => 'Required', 'option_id' => 'Required');
     $v = Validator::make(Input::all(), $rules);
     if ($v->passes()) {
         $ip = Puskice::getIP();
         $vote = PollVote::where('poll_id', '=', Input::get("poll_id"))->where('ip_address', '=', $ip)->first();
         if ($vote != null) {
             return Response::json(array('status' => 'fail', 'text' => __("Већ сте гласали на овој анкети. Хвала :)")));
         }
         $vote = new PollVote();
         $vote->poll_id = strip_tags(Input::get("poll_id"));
         $vote->option_id = strip_tags(Input::get("option_id"));
         $vote->ip_address = $ip;
         $vote->save();
         $option = PollOption::find(Input::get("option_id"));
         $poll = Poll::find($option->poll_id);
         if ($poll->published == 1) {
             $option->vote_count = $option->vote_count + 1;
             $option->save();
             return Response::json(array('status' => 'success', 'text' => __("Хвала што сте гласали")));
         }
         return Response::json(array('status' => 'fail', 'text' => __("Хвала што покушавате да хакујете анкету :)")));
     } else {
         return Response::json(array('status' => 'fail', 'text' => __("Десила се грешка")));
     }
 }