Example #1
0
 public function next()
 {
     $q_id = Param::get('id');
     $selection_id = Param::get('selection');
     $question = Question::get();
     $answer = Answer::get($q_id);
     $this->set(['question' => $question, 'answer' => $answer, 'selection_id' => $selection_id]);
     $this->render('index');
 }
 public function submitPoll($data, $form)
 {
     $choices = Answer::get()->filter(array('ID' => $data['PollChoices']));
     if ($choices) {
         foreach ($choices as $choice) {
             $choice->Votecount = $choice->Votecount + 1;
             $choice->write();
             $choice->Poll()->markAsVoted();
         }
     }
     $this->controller->redirectBack();
 }
Example #3
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);
     }
 }
Example #4
0
 if (!isset($_SESSION['tests']) || !isset($_SESSION['tests'][$test_id])) {
     include 'templates/test_expired.inc.php';
     exit;
 }
 $RES =& $_SESSION['tests'][$test_id];
 if ($RES->finished) {
     redirect("/tests/{$test->id}/");
     die;
 }
 $answer_id = intval($_POST['answer']);
 if ($answer_id == 0) {
     die("Bad request: answer not specified");
 }
 $question = Question::get("WHERE `id` = %d AND `test_id` = %d", $RES->question_id, $test->id);
 if ($question) {
     $answer = Answer::get("WHERE `id`=%d AND `question_id`=%d", $answer_id, $question->id);
     if ($answer) {
         $a = new QuestionResult();
         $a->question_id = $question->id;
         $a->question_ord = $question->order;
         $a->ord = $answer->order;
         $a->points = $answer->points;
         $RES->answers[] = $a;
     }
 }
 include $test->handler_file();
 if (!function_exists('next_action')) {
     die("Invalid handler {$handler_file}: a handler must define function next_action(\$RES, \$question_count)");
 }
 $question = run_handler($RES, $test, $question ? $question->id : 0, $answer ? $answer->id : 0);
 redirect("/tests/{$test->id}/");