Example #1
0
function run_handler(&$RES, $test, $answered_question_id, $answer_id)
{
    $t = DBkitModel::get("SELECT MAX(`order`) AS question_count FROM questions WHERE `test_id` = %d", $test->id);
    $question_count = $t->question_count;
    $action = next_action($RES, $question_count);
    if ($action == 'random') {
        $action = array(1, $question_count);
    }
    if ($action == 'next') {
        $action = $RES->question_ord + 1;
    }
    if (is_integer($action) && $action > $question_count) {
        $action = 'finish';
    }
    $answered_question_ids = collect_attrs($RES->answers, 'question_id');
    $id_cond = empty($answered_question_ids) ? "TRUE" : "`id` NOT IN ?";
    if (is_integer($action)) {
        $t = DBkitModel::get("SELECT MIN(`order`) AS `order` FROM `questions` WHERE `test_id`=%d AND `order`>=%d AND {$id_cond}", $test->id, $action, $answered_question_ids);
        if (!$t) {
            // TODO: what to do when the question_ord returned by the handler does not exist?
        }
        $question = Question::get("WHERE `order` = %d AND `test_id` = %d LIMIT 1", $t->order, $test->id);
    } else {
        if (is_array($action)) {
            $t = DBkitModel::query("SELECT `id` FROM `questions` WHERE `test_id`=%d AND `order` BETWEEN %d AND %d AND {$id_cond}", $test->id, $action[0], $action[1], $answered_question_ids);
            if (empty($t)) {
                $action = 'finish';
            } else {
                $t = $t[mt_rand(0, count($t) - 1)];
                $question = Question::get("WHERE `id` = %d AND `test_id` = %d LIMIT 1", $t->id, $test->id);
            }
        }
    }
    if ($question) {
        $RES->question_id = $question->id;
        $RES->question_ord = $question->order;
        $RES->question_no++;
        if (!is_null($answered_question_id)) {
            stat_question_answered($RES->session_id, $test->id, $RES->partner_id, $RES->day, $answered_question_id, $answer_id, $RES->question_id, $RES->paid);
        }
        return $question;
    }
    if ($action == 'finish') {
        $RES->finished = true;
        if ($RES->paid) {
            $RES->sms_chal = random_string(REATESTER_SMS_CHAL_LENGTH);
            $RES->sms_resp = random_string(REATESTER_SMS_RESP_LENGTH);
        } else {
            $RES->sms_chal = $RES->sms_resp = null;
        }
        stat_test_finished($RES->session_id, $test->id, $RES->partner_id, $RES->day, $answered_question_id, $answer_id, $RES->paid, $RES->sms_chal, $RES->sms_resp);
        redirect("/tests/{$test->id}/");
        die;
    }
    die("Internal error: invalid handler action '{$action}'");
}
Example #2
0
 function do_insert($fields)
 {
     $names = array();
     $values = array();
     $args = array();
     foreach ($fields as $field) {
         $names[] = "`{$field}`";
         $values[] = "?";
         $value = $this->{$field};
         if (DBkitModel::is_date_field($field) && is_numeric($value)) {
             $value = strftime('%Y%m%d%H%M%S', $value);
         }
         $args[] = $value;
     }
     $names = implode(", ", $names);
     $values = implode(", ", $values);
     dbkit_execute_with_array("INSERT INTO `{$this->table_name}`({$names}) VALUES ({$values})", $args);
     $this->id = mysql_insert_id();
 }
Example #3
0
<?php

include '../lib/common.inc.php';
$id = $_REQUEST['question_id'];
$is_new = $id == 'new';
if ($_POST) {
    if ($is_new) {
        $question = new Question();
        $question->test_id = $_REQUEST['test_id'];
        $v = DBkitModel::get("SELECT MAX(`order`) AS max_order FROM `questions` WHERE `test_id`=%s", $question->test_id);
        $max_order = $v ? $v->max_order : 0;
        $question->order = $max_order + 1;
        $answers = array();
    } else {
        if (!($question = Question::get("WHERE id = %s", $id))) {
            jsdie('questionNotFound', $id);
        }
    }
    $question->assign('question_', array('text', 'image_code'));
    $answers_data = array();
    foreach ($_POST as $k => $v) {
        if (0 === strpos($k, "ans_")) {
            $arr = explode('_', $k, 3);
            $aid = $arr[1];
            if (!isset($answers_data[$aid])) {
                $answers_data[$aid] = array();
            }
            $answers_data[$aid][$arr[2]] = trim($v);
        }
    }
    $answers_by_id = Answer::query_indexed('id', "WHERE question_id=%d", $question->id);
Example #4
0
function loginkit_process_login($model_name, $default_logged_in_url = '/', $no_such_user_error = "Sorry, no such user exists.", $invalid_password_error = "Sorry, the password is incorrect.")
{
    $name = '';
    $flash = '';
    if ($_POST) {
        if (!isset($_REQUEST['email'])) {
            die("invalid request: missing email");
        }
        if (!isset($_REQUEST['password'])) {
            die("invalid request: missing password");
        }
        $name = $_REQUEST['email'];
        $password = $_REQUEST['password'];
        setcookie(LOGINKIT_LAST_USERNAME_COOKIE, $name, time() + 60 * 60 * 24 * 366);
        $user = DBkitModel::get_with_klass($model_name, "WHERE `email` = ?", $name);
        if (!$user) {
            $flash = $no_such_user_error;
        } else {
            if (empty($user->password_salt)) {
                die("{$model_name->password_salt} must be defined");
            }
            $password_hash = sha1($user->password_salt . $password);
            if ($password_hash != $user->password_hash) {
                $flash = $invalid_password_error;
            } else {
                $url = $default_logged_in_url;
                if (!empty($_REQUEST['url'])) {
                    $url = $_REQUEST['url'];
                } else {
                    if (method_exists($user, 'url_to_redirect_to_after_login')) {
                        $url = $user->url_to_redirect_to_after_login();
                    } else {
                        if (!empty($user->url_to_redirect_to_after_login)) {
                            $url = $user->url_to_redirect_to_after_login;
                        }
                    }
                }
                loginkit_logged_in($user);
                redirect($url);
                die;
            }
        }
    } else {
        if (isset($_COOKIE[LOGINKIT_LAST_USERNAME_COOKIE])) {
            $name = $_COOKIE[LOGINKIT_LAST_USERNAME_COOKIE];
        }
    }
    return array($name, $flash);
}