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}'"); }
$day_ordinal = rand(0, $time_range); $timeofday = rand(0, 60 * 60 * 24 - 1); $start_time = time() - $day_ordinal * 60 * 60 * 24 + $timeofday; $day = strftime('%Y-%m-%d', $start_time); $session_id = stat_test_started($test->id, $partner_id, $day, $test->all_questions[0]->id, $test->sms_enabled); $r = rand(0, 100); $count_questions = $r <= $percent_sessions[0] ? rand(0, count($test->all_questions) - 1) : count($test->all_questions) - 1; for ($q = 0; $q < $count_questions; $q++) { $question = $test->all_questions[$i]; $answer = $question->all_answers[rand(0, count($question->all_answers) - 1)]; stat_question_answered($session_id, $test->id, $partner_id, $day, $question->id, $answer->id, $test->all_questions[$i + 1]->id, $test->sms_enabled); } if ($r > $percent_sessions[0]) { $question = $test->all_questions[count($test->all_questions) - 1]; $answer = $question->all_answers[rand(0, count($question->all_answers) - 1)]; stat_test_finished($session_id, $test->id, $partner_id, $day, $question->id, $answer->id, $test->sms_enabled, random_string(REATESTER_SMS_CHAL_LENGTH), random_string(REATESTER_SMS_RESP_LENGTH)); if ($r > $percent_sessions[1]) { stat_sms_received($session_id, $test->id, $partner_id, $day, $test->sms_enabled, 300, 200); } } } $daily_statistics_by_partner = group_by(DailyStatistics::query("ORDER BY day"), "partner_id"); $payment_dates = array(); for ($i = 0; $i < $payment_count; $i++) { $payment_dates[] = time() - rand(0, 60 * 60 * 24 * $time_range); } sort($payment_dates); $partner_last_payment = array(); $partner_last_stat_index = array(); for ($i = 0; $i < count($all_partners); $i++) { $partner_last_payment[] = null;