Im about to patch this custom version upto beta 4, but before I do thought id try and resolve an issue.
Ive just run our first competition...60 or so entries, 11 questions (predictions).
I closed the competition successfully.
Code: Select all
<?php
/**
*
* @package phpBB3
* @version $Id: quiz.php
* @copyright (c) 2008, 2009 battye, CricketMX.com
* Ultimate Quiz MOD v2.0.0
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
*
*/
/**
* @ignore
*/
define('IN_PHPBB', true);
$phpbb_root_path = (defined('PHPBB_ROOT_PATH')) ? PHPBB_ROOT_PATH : './';
$phpEx = substr(strrchr(__FILE__, '.'), 1);
include($phpbb_root_path . 'common.' . $phpEx);
include($phpbb_root_path . 'includes/functions_quiz.' . $phpEx);
include($phpbb_root_path . 'includes/quiz_stats_class.' . $phpEx);
// Start session management
$user->session_begin();
$auth->acl($user->data);
$user->setup('mods/quiz');
error_reporting(E_ALL | E_NOTICE);
// Grab data
$mode = request_var('mode', '');
$action = request_var('action', '');
$category = request_var('category', 0);
if( !$user->data['is_registered'] )
{
trigger_error('QUIZ_MUST_BE_LOGGED_IN');
}
// Find which mode is set
if ($mode == 'submit')
{
// submit quiz
// First of all, check if the admin only setting is enabled.
// If so, only admins can access this page.
if( $config['quiz_admin_only_submit'] != 0 && !$auth->acl_get('a_') )
{
trigger_error($user->lang['QUIZ_ADMIN_SUBMIT_ONLY_ERROR']);
}
// Sort bbCode
$uid = $bitfield = $options = '';
$allow_bbcode = $allow_urls = $allow_smilies = true;
// uid - $uid, bitfield - $bitfield, options - $options
page_header($user->lang['SUBMIT_QUIZ']);
if( $action == 'submit' ) // The actual submission to the database stage
{
$data_array = array();
$full_correct_array = array();
$full_correct_array_count = 0;
$number_of_questions = request_var('number_of_questions', 0);
$this_quiz_id = quiz_latest_id() + 1;
if (!($number_of_questions > 0))
{
trigger_error($user->lang['NUMBER_QUESTIONS_UNDEFINED']);
}
for ($e = 1; $e <= $number_of_questions; $e++) // Just a trial run to make sure all questions are submitted
{
if (!request_var('question_name_' . $e, '', true))
{
trigger_error($user->lang['EMPTY_QUESTION']);
}
}
for ($i = 1; $i <= $number_of_questions; $i++)
{
$question_name = request_var('question_name_' . $i, '', true);
if (request_var('answers_' . $i, '', true)) // input answer or multiple choice
{
$correct_answer = request_var('mc_' . $i, '', true);
$multiple_answers = request_var('answers_' . $i, '', true);
$multiple_answers_array = explode("\n", $multiple_answers);
}
else if (request_var('tf_' . $i, 0) != 0) // true/false
{
$correct_answer = (request_var('tf_' . $i, 0) == 1) ? $user->lang['TRUE_FALSE_TRUE'] : $user->lang['TRUE_FALSE_FALSE'];
$multiple_answers_array = array($user->lang['TRUE_FALSE_TRUE'], $user->lang['TRUE_FALSE_FALSE']);
}
// Update questions table
generate_text_for_storage($question_name, $uid, $bitfield, $options, $allow_bbcode, $allow_urls, $allow_smilies);
$question_array = array(
'quiz_question' => utf8_normalize_nfc($question_name),
'quiz_related_id' => $this_quiz_id,
'quiz_uid' => $uid,
'quiz_bitfield' => $bitfield,
'quiz_options' => $options
);
$question_sql = 'INSERT INTO ' . QUIZ_QUESTIONS_TABLE . ' ' . $db->sql_build_array('INSERT', $question_array);
$db->sql_query($question_sql);
//$that_question_id = $db->sql_nextid() - 1; // This is the ID of the question submitted now.
$that_question_id = $db->sql_nextid();
// Now the actual data
$multiple_answers_array_number = sizeof($multiple_answers_array);
// For those people who submit too many choices
$key = 0;
if( $multiple_answers_array_number > $config['quiz_submit_max_multiple_choice'] )
{
$key = array_search($correct_answer, $multiple_answers_array);
$key++;
$how_many_further = $multiple_answers_array_number - $config['quiz_submit_max_multiple_choice'];
}
$stop_further_checks = 0;
for ($k = 0; $k < $multiple_answers_array_number; $k++)
{
/* Initialise any remaining variables, avoid offset */
if( !isset($remove_question) )
{
$remove_question = 0;
}
// If key is set, we need to remove one of the incorrect answers
if ($key > 0 && $stop_further_checks != 1)
{
if ( ($key-1) != $k ) // If it is not the correct answer then we remove this one
{
$remove_question = 1; // yes
$how_many_further = $how_many_further - 1;
if ($how_many_further == 0)
{
$stop_further_checks = 1; // don't check any more
}
else
{
$stop_further_checks = 0; // keep going
}
}
else
{
$remove_question = 0;
$stop_further_checks = 0;
}
}
if ( $remove_question != 1 )
{
$correct_true_input_question_answer = ($multiple_answers_array[$k] == $correct_answer) ? true : false;
generate_text_for_storage($multiple_answers_array[$k], $uid, $bitfield, $options, $allow_bbcode, $allow_urls, $allow_smilies);
$data_array[] = array(
'quiz_question_id' => $that_question_id,
'quiz_answer' => utf8_normalize_nfc($multiple_answers_array[$k]),
'quiz_correct' => $correct_true_input_question_answer,
'quiz_uid' => $uid,
'quiz_bitfield' => $bitfield,
'quiz_options' => $options,
);
}
$remove_question = 0;
}
// End the looping of the separate questions
}
$db->sql_multi_insert(QUIZ_DATA_TABLE, $data_array);
// Insert into main quiz table.
$quiz_category = request_var('select_category', 0);
$quiz_name = request_var('quiz_name', '', true);
$quiz_array = array(
'quiz_author' => $user->data['user_id'],
'quiz_category' => $quiz_category,
'quiz_name' => $quiz_name,
'quiz_date' => time(),
'quiz_number_of_questions' => $number_of_questions,
'quiz_views' => 0
);
$quiz_sql = 'INSERT INTO ' . QUIZ_TABLE . ' ' . $db->sql_build_array('INSERT', $quiz_array);
$db->sql_query($quiz_sql);
// update the latest quiz id
set_config('quiz_latest_id_submitted', ($config['quiz_latest_id_submitted'] + 1));
trigger_error(sprintf($user->lang['QUIZ_SUBMISSION_SUCCESSFUL'], '<a href="' . append_sid('quiz.'.$phpEx) . '">', '</a>')); // The quiz was successfully entered into the database
}
if( $action == 'verify' )
{
$number_of_questions = request_var('number_of_questions', 0);
$true_false_questions = '';
if (!($number_of_questions > 0))
{
trigger_error($user->lang['NUMBER_QUESTIONS_UNDEFINED']);
}
// ready to submit?
if( request_var('final_verification', 0) )
{
$readiness_fv = 1; // true by default
if( !request_var('quiz_name', '', true) || !request_var('select_category', '') || request_var('select_category', '') == 'undefined' )
{
// We are not ready
$readiness_fv = 0;
}
else // check the questions that answers have been selected
{
for ($r = 1; $r <= $number_of_questions; $r++)
{
if( !request_var('tf_' . $r, 0) && !request_var('mc_' . $r, '', true) )
{
// Not ready here either
$readiness_fv = 0;
}
}
}
if( $readiness_fv )
{
for ($r = 1; $r <= $number_of_questions; $r++)
{
// Define the quiz type
$reveal_multiples_row = '';
if( request_var('tf_' . $r, 0) )
{
$reveal_quiz_type = $user->lang['IS_TRUE_FALSE'];
}
else if ( strpos(request_var('answers_' . $r, '', true), "\n") )
{
$multi_list = explode("\n", request_var('answers_' . $r, '', true));
$alternates_list = '';
for ($b = 0; $b < sizeof($multi_list); $b++)
{
$multi_list[$b] = ($multi_list[$b] == request_var('mc_' . $r, '', true)) ? '' : $multi_list[$b];
$alternates_list .= ($multi_list[$b] != '') ? ', ' . $multi_list[$b] : '';
}
$alternates_list[0] = '';
$alternates_list[1] = '';
$uid = $bitfield = $options = '';
$allow_bbcode = $allow_urls = $allow_smilies = true;
generate_text_for_storage($alternates_list, $uid, $bitfield, $options, $allow_bbcode, $allow_urls, $allow_smilies);
$alternates_list = generate_text_for_display($alternates_list, $uid, $bitfield, $options, $allow_bbcode, $allow_urls, $allow_smilies);
$reveal_quiz_type = $user->lang['MULTIPLE_CHOICE_QUESTION'];
$reveal_multiples_row .= $alternates_list;
}
else
{
$reveal_quiz_type = $user->lang['INPUT_ANSWER_QUESTION'];
}
// Create bbCode for question
$reveal_name_of_quiz = request_var('question_name_' . $r, '', true);
$uid = $bitfield = $options = '';
$allow_bbcode = $allow_urls = $allow_smilies = true;
generate_text_for_storage($reveal_name_of_quiz, $uid, $bitfield, $options, $allow_bbcode, $allow_urls, $allow_smilies);
$reveal_name_of_quiz = generate_text_for_display($reveal_name_of_quiz, $uid, $bitfield, $options, $allow_bbcode, $allow_urls, $allow_smilies);
// Create bbCode for correct answer
$reveal_correct_answer_for_question = request_var('mc_' . $r, '', true);
$uid = $bitfield = $options = '';
$allow_bbcode = $allow_urls = $allow_smilies = true;
generate_text_for_storage($reveal_correct_answer_for_question, $uid, $bitfield, $options, $allow_bbcode, $allow_urls, $allow_smilies);
$reveal_correct_answer_for_question = generate_text_for_display($reveal_correct_answer_for_question, $uid, $bitfield, $options, $allow_bbcode, $allow_urls, $allow_smilies);
$template->assign_block_vars('question_row', array(
'QUESTION_ID' => $r,
'QUIZ_TYPE' => $reveal_quiz_type,
'QUESTION_NAME_REVEAL' => $reveal_name_of_quiz,
'QUESTION_NAME_HIDDEN' => '<input type="hidden" name="question_name_' . $r . '" value="' . request_var('question_name_' . $r, '', true) . '" />',
'QUESTION_ANSWER_REVEAL' => (request_var('tf_' . $r, 0) != 0) ? ((request_var('tf_' . $r, 0) == 1) ? $user->lang['TRUE_FALSE_TRUE'] : $user->lang['TRUE_FALSE_FALSE']) : $reveal_correct_answer_for_question,
'MULTIPLES_REVEAL' => $reveal_multiples_row,
'QUESTION_ANSWER_HIDDEN' => (request_var('tf_' . $r, 0) != 0) ? '<input type="hidden" name="tf_' . $r . '" value="' . request_var('tf_' . $r, '') . '" />' : '<input type="hidden" name="answers_' . $r . '" value="' . request_var('answers_' . $r, '', true) . '" /><input type="hidden" name="mc_' . $r . '" value="' . request_var('mc_' . $r, '', true) . '" />',
));
}
$s_hidden_fields = build_hidden_fields(array(
'number_of_questions' => $number_of_questions,
));
$template->assign_vars( array(
'S_SUBMIT_QUIZ_ACTION' => append_sid("{$phpbb_root_path}quiz.$phpEx", 'mode=submit&action=submit'),
'S_HIDDEN_FIELDS' => $s_hidden_fields,
'L_SUBMIT_INFORMATION' => $user->lang['FINAL_VERIFY_SUBMIT'],
'U_STEP_SUBMIT_QUIZ' => $user->lang['SUBMIT_STEP_3'],
'U_CHECK_INITIAL' => false,
'U_VERIFY_ANSWERS' => false,
'U_FINAL_VERIFY' => true,
'U_QUIZ_NAME' => request_var('quiz_name', '', true),
'U_FINAL_CATEGORY_NAME' => get_category_name(request_var('select_category', 0)),
'U_FINAL_CATEGORY_ID' => request_var('select_category', 0),
'U_SELECT_A_CATEGORY' => quiz_category_list(request_var('select_category', 0)),
));
$template->set_filenames(array(
'body' => 'quiz_submit_body.html')
);
page_footer();
}
}
/* Initialise any remaining variables */
if( !isset($readiness_fv) )
{
$readiness_fv = 0;
}
if( !$readiness_fv && !request_var('final_verification', 0) )
{
// let's check to make sure all of the questions have been filled out. Also that multiple choice is correct
for ($q = 1; $q <= $number_of_questions; $q++)
{
// is the question/answer actually filled out for all questions?
$verify_ok = ( request_var('question_name_' . $q, '', true) != '' && ( request_var('answers_' . $q, '', true) != '' || request_var('is_true_false_' . $q, 0) == 1 ) ) ? 1 : 0; // 0 represents something is empty
$multiple_choices = ( request_var('answers_' . $q, '', true) ) ? explode("\n", request_var('answers_' . $q, '', true)) : '';
$number_of_multiple_choices = sizeof($multiple_choices);
$mc_alert = ($number_of_multiple_choices > $config['quiz_submit_max_multiple_choice']) ? sprintf($user->lang['TOO_MANY_CHOICES'], $config['quiz_submit_max_multiple_choice']) : '';
if ($verify_ok == 0 || $number_of_multiple_choices > $config['quiz_submit_max_multiple_choice'])
{
break;
}
}
}
if ((!$readiness_fv && !request_var('final_verification', 0)) && (!$verify_ok || $number_of_multiple_choices > $config['quiz_submit_max_multiple_choice']))
{
$submit_quiz_action_figure = "verify";
$final_quiz_verification = false;
for ($i = 1; $i <= $number_of_questions; $i++)
{
$template->assign_block_vars('question_row', array(
'S_HIDDEN_ANSWERS' => @$hidden_field,
'QUESTION_ID' => $i,
'QUESTION_NAME' => request_var('question_name_' . $i, '', true),
'QUESTION_TF_CHECK' => (request_var('is_true_false_' . $i, 0) == 1) ? 'checked="checked"' : false, // Display true or false option
'QUESTION_ANSWER' => request_var('answers_' . $i, '', true),
));
}
// Deciding the error message
$ef_error_msg = ( !$verify_ok ) ? $user->lang['EMPTY_QUIZ_FIELDS'] : $mc_alert;
$verify_ok = false; // Just in case it was the multiple choice that caused this, to be used later on
$template->assign_vars( array(
'U_EMPTY_FIELDS' => true,
'U_EMPTY_FIELDS_MSG' => $ef_error_msg,
'L_SUBMIT_INFORMATION' => sprintf( $user->lang['SUBMIT_INFORMATION'], $number_of_questions, $config['quiz_submit_max_multiple_choice'] ),
));
}
else
{
$submit_quiz_action_figure = "verify";
$final_quiz_verification = true;
// If we are on Step 2 "retry", ie. referred back to this page, we need to check for the existance of true/false questions
$tf_questions_expl = explode(',', request_var('true_false_questions', ''));
for ($i = 1; $i <= $number_of_questions; $i++)
{
if ( request_var('question_name_' . $i, '', true) || request_var('answers_' . $i, '', true) && ( !request_var('final_verification', 0) && (request_var('is_true_false_' . $i, 0) == 1) ))
{
if (request_var('is_true_false_' . $i, 0) == 1)
{
$true_false_questions .= $i . ',';
}
else
{
$hidden_field = '<input type="hidden" name="answers_' . $i . '" value="' . request_var('answers_' . $i, '', true) . '" />';
$multiple_choices = ( request_var('answers_' . $i, '', true) ) ? explode("\n", request_var('answers_' . $i, '', true)) : '';
$number_of_multiple_choices = sizeof($multiple_choices);
//$mc_alert = ($number_of_multiple_choices > $config['quiz_submit_max_multiple_choice']) ? sprintf($user->lang['TOO_MANY_CHOICES'], $config['quiz_submit_max_multiple_choice']) : '';
if ($number_of_multiple_choices == 1) // If the quiz is input answer, this will apply
{
/* Initialise any remaining variables, avoid offset */
//$create_offsetted_mc = (isset($multiple_choices[0])) ? $multiple_choices[0] : '';
//$multiple_choice_lineup = $create_offsetted_mc . '<input type="hidden" name="mc_'.$i.'" value="' . $create_offsetted_mc . '" />';
@$multiple_choice_lineup = $multiple_choices[0] . '<input type="hidden" name="mc_'.$i.'" value="' . $multiple_choices[0] . '" />';
}
else // for deciding multiple choice options
{
$multiple_choice_lineup = '';
for ($x = 0; $x < $number_of_multiple_choices; $x++)
{
if( $multiple_choices[$x] == '' || $multiple_choices[$x] == ' ' )
{
trigger_error($user->lang['EMPTY_ANSWER']);
}
$multiple_choice_lineup .= '<input type="radio" name="mc_'.$i.'" value="' . $multiple_choices[$x] . '" /> ' . $multiple_choices[$x] . '<br />';
}
}
}
if ( !$true_false_questions && request_var('true_false_questions', '') )
{
$true_false_questions = request_var('true_false_questions', '');
}
$question_tf_vp = false;
// is_numeric must be used in case it is the 0th item in the array
if( request_var('is_true_false_' . $i, 0) == 1 || (!((!$readiness_fv && !request_var('final_verification', 0))) && is_numeric(array_search(($i), $tf_questions_expl))) )
{
$question_tf_vp = true;
}
$template->assign_block_vars('question_row', array(
'S_HIDDEN_ANSWERS' => @$hidden_field,
'QUESTION_ID' => $i,
'QUESTION_NAME' => request_var('question_name_' . $i, '', true),
'QUESTION_TF' => $question_tf_vp, // Display true or false option
'QUESTION_MC' => (isset($multiple_choice_lineup)) ? $multiple_choice_lineup : '', // Display a list of multiple choices or the input answer
// 'QUESTION_MC_ALERT' => $mc_alert,
));
}
}
}
$s_hidden_fields = build_hidden_fields(array(
'number_of_questions' => $number_of_questions,
'true_false_questions' => $true_false_questions,
'final_verification' => $final_quiz_verification,
));
$template->assign_vars( array(
'S_SUBMIT_QUIZ_ACTION' => append_sid("{$phpbb_root_path}quiz.$phpEx", 'mode=submit&action=' . $submit_quiz_action_figure),
'S_HIDDEN_FIELDS' => $s_hidden_fields,
'U_STEP_SUBMIT_QUIZ' => $user->lang['SUBMIT_STEP_2'],
'U_EMPTY_FIELDS' => ((!$readiness_fv && !request_var('final_verification', 0))) ? false : true,
'U_EMPTY_FIELDS_MSG' => $user->lang['EMPTY_RADIO_BOX'],
'U_CHECK_INITIAL' => false,
'U_VERIFY_ANSWERS' => ((!$readiness_fv && !request_var('final_verification', 0))) ? $verify_ok : true,
'U_QUIZ_NAME' => request_var('quiz_name', '', true),
'U_SELECT_A_CATEGORY' => quiz_category_list(request_var('select_category', 0)),
));
}
else
{
if (!request_var('number_of_questions', 0))
{
// initial "choose number of questions screen"
// quahappy's suggestion to use a drop down box
$num_drop = '<select name="number_of_questions">';
for ($d = $config['quiz_submit_min']; $d <= $config['quiz_submit_max']; $d++)
{
$num_drop .= ' <option value="' . $d . '">' . $d . '</option>';
}
$num_drop .= '</select>';
$template->assign_vars( array(
'L_SELECT_NUMBER_EXPLAIN' => sprintf($user->lang['SELECT_NUMBER_EXPLAIN'], $config['quiz_submit_min'], $config['quiz_submit_max']),
'S_SUBMIT_QUIZ_ACTION' => append_sid("{$phpbb_root_path}quiz.$phpEx", 'mode=submit'),
'U_STEP_SUBMIT_QUIZ' => $user->lang['SUBMIT_QUIZ'],
'U_CHECK_INITIAL' => true,
'U_NUM_DROP' => $num_drop,
));
}
else
{
// actual adding questions and answers screen
// check to make sure it is within required parameters
$number_of_questions = request_var('number_of_questions', 0);
if (!request_var('number_of_questions', 0) || $number_of_questions < $config['quiz_submit_min'] || $number_of_questions > $config['quiz_submit_max'])
{
trigger_error( sprintf($user->lang['OUTSIDE_QUESTION_PARAM'], $config['quiz_submit_min'], $config['quiz_submit_max']) );
}
for ($i = 0; $i < $number_of_questions; $i++)
{
$template->assign_block_vars('question_row', array(
'QUESTION_ID' => $i + 1,
));
}
$s_hidden_fields = build_hidden_fields(array(
'number_of_questions' => $number_of_questions,
));
$template->assign_vars( array(
'S_SUBMIT_QUIZ_ACTION' => append_sid("{$phpbb_root_path}quiz.$phpEx", 'mode=submit&action=verify'),
'S_HIDDEN_FIELDS' => $s_hidden_fields,
'L_SUBMIT_INFORMATION' => sprintf( $user->lang['SUBMIT_INFORMATION'], $number_of_questions, $config['quiz_submit_max_multiple_choice'] ),
'U_STEP_SUBMIT_QUIZ' => $user->lang['SUBMIT_STEP_1'],
'U_CHECK_INITIAL' => false,
'U_SELECT_A_CATEGORY' => quiz_category_list(''),
'U_MAX_MULTIPLE_CHOICE' => $config['quiz_submit_max_multiple_choice'],
));
}
}
$template->set_filenames(array(
'body' => 'quiz_submit_body.html')
);
}
if ($mode == 'play')
{
// Quiz play once
if( $config['quiz_play_once'] != 0 )
{
$check_once = 'SELECT quiz_stats_id
FROM ' . QUIZ_STATISTICS_TABLE . '
WHERE quiz_id = ' . request_var('q', 0) . '
AND quiz_player = ' . $user->data['user_id'] . '
ORDER BY quiz_stats_id DESC';
$check_once_result = $db->sql_query_limit($check_once, 1);
$check_once_id = $db->sql_fetchfield('quiz_stats_id');
if( (int) $check_once_id > 0 )
{
trigger_error(sprintf($user->lang['QUIZ_PLAY_ONCE_ONLY'], '<a href="' . append_sid('quiz.'.$phpEx) . '">', '</a>'));
}
}
if( $config['quiz_play_own_quiz'] == 0 )
{
if( get_author_of_quiz(request_var('q', 0)) == $user->data['user_id'] || get_author_of_quiz(request_var('quiz_id', 0)) == $user->data['user_id'] )
{
trigger_error($user->lang['QUIZ_USER_CANNOT_PLAY_OWN']);
}
}
// active check
$act_sql = 'SELECT quiz_deactivate FROM ' . QUIZ_TABLE . ' WHERE quiz_id = ' . request_var('q', 0);
$act_res = $db->sql_query($act_sql);
$act_check_look = $db->sql_fetchfield('quiz_deactivate');
if( $act_check_look > 0 )
{
trigger_error("This competition is closed");
}
if ($action == 'submit')
{
// The save type - "save" or "submit".
$save_type = request_var('submit', '');
$quiz_id = request_var('quiz_id', 0);
$current_quiz_in_progress_check = find_current_live_progress_id($quiz_id, 1);
if (!$current_quiz_in_progress_check)
{
trigger_error($user->lang['MULTIPLE_SUBMISSION']);
}
$find_number_of_questions_in_quiz = find_number_of_questions_in_quiz($quiz_id);
$quiz_start_time = find_current_live_progress_id($quiz_id, 2); // When "2", the unix timestamp of the start time is given
$time_taken_to_do_quiz = time() - $quiz_start_time + find_current_live_progress_id($quiz_id, 3);
if ($config['quiz_save_enabled'] == '1' && $save_type == $user->lang['QUIZ_SAVE'])
{
if ($config['quiz_time_limit_per_question'] != '0')
{
if ($time_taken_to_do_quiz > ($config['quiz_time_limit_per_question'] * $find_number_of_questions_in_quiz))
{
$db->sql_query("UPDATE " . QUIZ_PROGRESSIVE_TABLE . " SET quiz_in_progress = 0 WHERE quiz_progress_id = " . $current_quiz_in_progress_check);
trigger_error(sprintf($user->lang['QUIZ_TIME_EXCEEDED'], $time_taken_to_do_quiz));
}
}
$saved_input = '';
// Essentially a rehash of what is below, but saved.
$sql = "SELECT quiz_question_id
FROM " . QUIZ_QUESTIONS_TABLE . "
WHERE quiz_related_id = " . $quiz_id;
$result = $db->sql_query($sql);
while ($question_id_array = $db->sql_fetchrow($result))
{
$give_question_id = $question_id_array['quiz_question_id'];
$given_user_input = request_var('question' . $give_question_id, '');
$user_given_answer = (!is_numeric($given_user_input)) ? $given_user_input : correct_answer_for_id($given_user_input, 2);
$saved_input .= $give_question_id . ':/\:/\:/\:' . $user_given_answer . ';/\;/\;/\;';
}
$sql = "UPDATE " . QUIZ_PROGRESSIVE_TABLE . "
SET quiz_progress_saved = '" . $db->sql_escape($saved_input) . "',
quiz_progress_stop_time = " . time() . ",
quiz_time_used = " . $time_taken_to_do_quiz . "
WHERE quiz_progress_id = " . find_current_live_progress_id($quiz_id, 1);
$db->sql_query($sql);
trigger_error($user->lang['QUIZ_PROGRESS_SAVED']);
}
else if ($save_type == $user->lang['SUBMIT'])
{
page_header($user->lang['QUIZ_RESULTS']);
$quiz_start_time = find_current_live_progress_id($quiz_id, 2); // When "2", the unix timestamp of the start time is given
$time_taken_to_do_quiz = time() - $quiz_start_time + find_current_live_progress_id($quiz_id, 3);
$time_results_summary = sprintf($user->lang['TIME_SUMMARY'], $time_taken_to_do_quiz);
$submit_quiz_progressive_id = find_current_live_progress_id($quiz_id, 1);
// Before we do anything, let's see if the time limits stuff is alright
if ($config['quiz_time_limit_per_question'] != '0')
{
//if ($time_taken_to_do_quiz > (($config['quiz_time_limit_per_question'] * $find_number_of_questions_in_quiz) - find_current_live_progress_id($quiz_id, 3)))
if ($time_taken_to_do_quiz > ($config['quiz_time_limit_per_question'] * $find_number_of_questions_in_quiz))
{
$db->sql_query("UPDATE " . QUIZ_PROGRESSIVE_TABLE . " SET quiz_in_progress = 0 WHERE quiz_progress_id = " . $submit_quiz_progressive_id);
trigger_error(sprintf($user->lang['QUIZ_TIME_EXCEEDED'], $time_taken_to_do_quiz));
}
}
$number_of_correct_answers = 0;
$number_of_incorrect_answers = 0;
$show_answers = array();
$statistical_sql = array();
$sql = "SELECT quiz_question_id
FROM " . QUIZ_QUESTIONS_TABLE . "
WHERE quiz_related_id = " . $quiz_id;
$result = $db->sql_query($sql);
while ($question_id_array = $db->sql_fetchrow($result))
{
$give_question_id = $question_id_array['quiz_question_id'];
$given_user_input = request_var('question' . $give_question_id, ''); // raw input
// if it is input answer, we need to combat inputting "numbers"
$type_sql = "SELECT COUNT(quiz_data_id) AS quiz_type FROM " . QUIZ_DATA_TABLE . "
WHERE quiz_question_id = " . intval($give_question_id);
$type_result = $db->sql_query($type_sql);
$determine_quiz_type = $db->sql_fetchfield('quiz_type');
$db->sql_freeresult($type_result);
// true/false or multiple choice
if( $determine_quiz_type > 1 )
{
$user_given_answer = (!is_numeric($given_user_input)) ? $given_user_input : correct_answer_for_id($given_user_input, 2); // the answer the user chose or gave
}
// input answer
else
{
// $user_given_answer = (int) $given_user_input;
$user_given_answer = $given_user_input;
}
$actual_correct_answer = correct_answer_for_id($give_question_id, 1); // the correct (database) answer
if (utf8_case_fold_nfc($user_given_answer) == utf8_case_fold_nfc($actual_correct_answer))
{
if ($config['quiz_show_answers'] != '0') // Should we show the answers to the user? Err...
{
$show_answers[($number_of_correct_answers + $number_of_incorrect_answers)] = sprintf($user->lang['SHOW_RESULTS_CORRECT'], $user_given_answer);
}
$statistical_array = array(
'quiz_question_id' => (int) $give_question_id,
'quiz_id' => (int) $quiz_id,
'quiz_correct' => 1,
'quiz_answer' => $user_given_answer,
'quiz_player' => (int) $user->data['user_id'],
);
$number_of_correct_answers++;
}
else
{
if ($config['quiz_show_answers'] != '0') // Should we show the answers to the user? Err...
{
$show_answers[($number_of_correct_answers + $number_of_incorrect_answers)] = sprintf($user->lang['SHOW_RESULTS_INCORRECT'], $user_given_answer, $actual_correct_answer);
}
$statistical_array = array(
'quiz_question_id' => (int) $give_question_id,
'quiz_id' => (int) $quiz_id,
'quiz_correct' => 0,
'quiz_answer' => $user_given_answer,
'quiz_player' => (int) $user->data['user_id'],
);
$number_of_incorrect_answers++;
}
$statistical_sql[] = 'INSERT INTO ' . QUIZ_STATISTICS_TABLE . '
' . $db->sql_build_array('INSERT', $statistical_array);
}
$db->sql_freeresult($result);
// the basic stats (right/wrong in a quiz for quick reference)
$statistical_overview_setup = array(
'quiz_id' => (int) $quiz_id,
'quiz_correct' => (int) $number_of_correct_answers,
'quiz_incorrect' => (int) $number_of_incorrect_answers,
'user_id' => (int) $user->data['user_id'],
'quiz_time' => (int) $time_taken_to_do_quiz,
);
//$statistical_sql[] = 'INSERT INTO ' . QUIZ_STATISTICS_OVERVIEW_TABLE . '
// ' . $db->sql_build_array('INSERT', $statistical_overview_setup);
// For compatibility with cash, a lot of people want this feature
$cash_summary = '';
if( $config['quiz_cash_on'] != 0 )
{
$cash_gained_or_lost = ($number_of_correct_answers * (int) $config['quiz_cash_win']) - ($number_of_incorrect_answers * (int) $config['quiz_cash_lose']);
$cash_field = 'user_points';
$statistical_sql[] = "UPDATE " . USERS_TABLE . " SET $cash_field = $cash_field + $cash_gained_or_lost WHERE user_id = " . $user->data['user_id'];
if( $cash_gained_or_lost >= 0 )
{
$cash_summary = '<br />' . sprintf($user->lang['QUIZ_POINTS_CASH_SUMMARY_WON'], $cash_gained_or_lost);
}
else
{
$cash_summary = '<br />' . sprintf($user->lang['QUIZ_POINTS_CASH_SUMMARY_LOST'], (0-$cash_gained_or_lost));
}
}
$results_summary = sprintf($user->lang['RESULTS_SUMMARY'], $number_of_correct_answers, $number_of_incorrect_answers);
if ($config['quiz_show_answers'] != '0') // Ctrl+V. I mean, " // Should we show the answers to the user? Err..."
{
$sql = "SELECT * FROM " . QUIZ_QUESTIONS_TABLE . "
WHERE quiz_related_id = $quiz_id
ORDER BY quiz_question_id ASC";
$result = $db->sql_query($sql);
// Loops the questions, so we can display the answer
$n = 0;
while ($row = $db->sql_fetchrow($result))
{
$question_formatted = generate_text_for_display($row['quiz_question'], $row['quiz_uid'], $row['quiz_bitfield'], $row['quiz_options']); // does this look familiar? lets format the question
$user_result = $show_answers[$n];
$template->assign_block_vars('results_row', array(
'QUESTION' => $question_formatted,
'RESULT' => $user_result,
));
$n++;
}
}
$template->assign_vars( array(
'U_QUIZ_RESULTS_WITH_NAME' => sprintf($user->lang['QUIZ_RESULTS_WITH_NAME'], get_quiz_title($quiz_id)),
'SHOW_ANSWERS' => ($config['quiz_show_answers'] != '0') ? true : false,
'TIME_SUMMARY' => $time_results_summary,
'RESULTS_SUMMARY' => $results_summary,
'CASH_SUMMARY' => $cash_summary,
));
$template->set_filenames(array(
'body' => 'quiz_results_body.html')
);
// Delete all temporary/progressive data and update the view count of the quiz
// We'll use the stats array as it looks nicer, and loosely related anyway
$statistical_sql[] = "DELETE FROM " . QUIZ_PROGRESSIVE_TABLE . " WHERE quiz_progress_id = $submit_quiz_progressive_id";
$statistical_sql[] = "UPDATE " . QUIZ_TABLE . " SET quiz_views = quiz_views + 1 WHERE quiz_id = $quiz_id";
// Insert statistics
$size_statistical_sql = sizeof($statistical_sql);
for ($u = 0; $u < $size_statistical_sql; $u++)
{
$db->sql_query($statistical_sql[$u]);
}
page_footer();
}
else
{
trigger_error($user->lang['QUIZ_NO_TYPE_SUBMIT']);
}
}
$quiz_id = request_var('q', 0); // get the ID for this quiz being played
page_header(get_quiz_title($quiz_id));
$current_progressive = find_current_live_progress_id($quiz_id, 1);
if ($current_progressive > 0)
{
$sql = "SELECT quiz_progress_saved, quiz_progress_time, quiz_progress_stop_time, quiz_time_used
FROM " . QUIZ_PROGRESSIVE_TABLE . "
WHERE quiz_progress_id = $current_progressive";
$result = $db->sql_query($sql);
$row = $db->sql_fetchrow($result);
$db->sql_freeresult($result);
$progress_saved = $row['quiz_progress_saved'];
/*
// Stop people from refreshing the timelimits, etc.
if ($config['quiz_time_limit_per_question'] != 0 &&
(($row['quiz_time_used'] >= (find_number_of_questions_in_quiz($quiz_id) * $config['quiz_time_limit_per_question']))) ||
($row['quiz_time_used'] > 0 && $row['quiz_progress_stop_time'] == 0))
{
trigger_error($user->lang['CHEATING_ATTEMPT']);
}
*/
//if( $config['quiz_time_limit_per_question'] &&
// ($row['quiz_time_used'] > 0 && $row['quiz_progress_stop_time'] == 0) || () )
//$progress_time_used = $row['quiz_progress_stop_time'] - $row['quiz_progress_time'];
$progress_time_used = $row['quiz_time_used'];
if ($progress_time_used < 0)
{
$progress_time_used = 0;
}
$questions_saved = explode(';/\;/\;/\;', $progress_saved);
$questions_saved_size = sizeof($questions_saved);
$saved_information = array();
for ($w = 0; $w < $questions_saved_size; $w++)
{
$question_given_answer = explode(':/\:/\:/\:', $questions_saved[$w]);
$saved_information[$w]['question_number'] = intval($question_given_answer[0]);
$saved_information[$w]['given_answer'] = ( isset($question_given_answer[1]) ) ? $question_given_answer[1] : '';
}
}
else
{
$progress_array = array(
'quiz_player_id' => $user->data['user_id'],
'quiz_progress_time' => time(),
'quiz_id' => $quiz_id,
'quiz_progress_saved' => '',
'quiz_time_used' => 1,
'quiz_in_progress' => 1,
);
// Enter the data into the progressive data, important to know later on if a user has paused
// a quiz and wants to resume. Also helpful for admins with time limits enabled.
$progress_sql = 'INSERT INTO ' . QUIZ_PROGRESSIVE_TABLE . '
' . $db->sql_build_array('INSERT', $progress_array);
$db->sql_query($progress_sql);
}
// Get the questions and loop the loop
$sql = 'SELECT * FROM ' . QUIZ_QUESTIONS_TABLE . '
WHERE quiz_related_id = ' . $quiz_id . '
ORDER BY quiz_question_id ASC';
$result = $db->sql_query($sql);
$quiz_answers_list = array();
$quiz_answer_row_id = array();
$question_count = 0;
while ($row = $db->sql_fetchrow($result))
{
$question_formatted = ''; // reset values to avoid a continual build-up of options
$answer_select_output = '';
$quiz_question_id = $row['quiz_question_id']; // This is needed to match to results from the data table
$question_formatted = generate_text_for_display($row['quiz_question'], $row['quiz_uid'], $row['quiz_bitfield'], $row['quiz_options']);
$answer_sql = 'SELECT * FROM ' . QUIZ_DATA_TABLE . '
WHERE quiz_question_id = ' . $quiz_question_id;
$mc_answer_result = $db->sql_query($answer_sql);
$a = 0;
while ($answer_row = $db->sql_fetchrow($mc_answer_result))
{
$answer_formatted = generate_text_for_display($answer_row['quiz_answer'], $answer_row['quiz_uid'], $answer_row['quiz_bitfield'], $answer_row['quiz_options']);
$quiz_answers_list[$quiz_question_id][$a] = $answer_formatted;
$quiz_answer_row_id[$quiz_question_id][$a] = $answer_row['quiz_data_id'];
$a++;
}
$sizeof_mc_array = sizeof($quiz_answer_row_id[$quiz_question_id]);
if ($sizeof_mc_array > 1)
{
$answer_select_output = ''; // The input fields that are shown to the user
for ($i = 0; $i < $sizeof_mc_array; $i++)
{
$checked_input = '';
// If we are grabbing saved answers
if ($current_progressive > 0)
{
if (is_numeric($saved_information[$question_count]['given_answer']))
{
$checked_input = (intval($saved_information[$question_count]['given_answer']) == intval($quiz_answers_list[$quiz_question_id][$i])) ? 'checked="checked"' : '';
}
else
{
$checked_input = (utf8_case_fold_nfc($saved_information[$question_count]['given_answer']) == utf8_case_fold_nfc($quiz_answers_list[$quiz_question_id][$i])) ? 'checked="checked"' : '';
}
}
// $answer_select_output .= '<input type="radio" name="question' . $quiz_question_id . '" value="' . $quiz_answer_row_id[$quiz_question_id][$i] . '" ' . $checked_input . ' /> ' . $quiz_answers_list[$quiz_question_id][$i] . '<br />';
// thanks to mtrs for pointing out the label miss
$answer_select_output .= '<input type="radio" id="' . $quiz_question_id . $i . '" name="question' . $quiz_question_id . '" value="' . $quiz_answer_row_id[$quiz_question_id][$i] . '" ' . $checked_input . ' /> <label for="' . $quiz_question_id . $i . '">' . $quiz_answers_list[$quiz_question_id][$i] . '</label><br />';
}
}
else
{
$value_input = '';
// If we are grabbing saved answers
if ($current_progressive > 0)
{
$value_input = $saved_information[$question_count]['given_answer'];
}
@$use_offset_answer = ($quiz_answers_list[$quiz_question_id][$i] != '') ? $quiz_answers_list[$quiz_question_id][$i] : '';
$answer_select_output .= '<input type="text" name="question' . $quiz_question_id . '" value="' . $value_input . '" /> ' . $use_offset_answer . '<br />';
}
$template->assign_block_vars('play_row', array(
'QUESTION' => $question_formatted,
'ANSWER' => $answer_select_output,
));
$question_count++;
}
$db->sql_freeresult($result);
// Time limits stuff
// Javascript from http://www.java-scripts.net/javascripts/Countdown-Timer.phtml (thanks to author Thomas).
if ($config['quiz_time_limit_per_question'] > 0)
{
if ( isset($progress_time_used) && isset($progress_saved) )
{
// Reset the start time - we have already noted the time already used
$db->sql_query("UPDATE " . QUIZ_PROGRESSIVE_TABLE . "
SET quiz_time_used = " . $progress_time_used . ",
quiz_progress_time = " . time() . ",
quiz_progress_stop_time = 0
WHERE quiz_progress_id = " . $current_progressive);
}
$total_time_allowed = ( isset($progress_time_used) ) ? (($config['quiz_time_limit_per_question'] * $question_count) - $progress_time_used) : $config['quiz_time_limit_per_question'] * $question_count; // time (seconds) allowed to complete the entire quiz
}
$s_hidden_fields = build_hidden_fields(array(
'quiz_id' => $quiz_id,
));
$template->assign_vars( array(
'S_HIDDEN_FIELDS' => $s_hidden_fields,
'S_SUBMIT_QUIZ_ACTION' => append_sid("{$phpbb_root_path}quiz.$phpEx", 'mode=play&action=submit'),
'TIME_LIMITS_ENABLED' => (isset($total_time_allowed)) ? true : false,
'TIME_ALLOWED' => (isset($total_time_allowed)) ? $total_time_allowed : 0,
'SAVE_ENABLED' => ($config['quiz_save_enabled'] == '1') ? true : false,
));
$template->set_filenames(array(
'body' => 'quiz_play_body.html')
);
}
if ($mode == 'stats')
{
page_header($user->lang['QUIZ_STATISTICS']);
// view statistics
$stats = new quiz_stats;
$stats->best_results();
$stats->most_played();
$stats->most_correct_answers();
// $stats->high_percentage_correct_answers();
$template->set_filenames(array(
'body' => 'quiz_statistics_body.html')
);
page_footer();
}
else if (!$mode)
{
// index and categories
if( $category )
{
// list quizzes in a certain category
$category_title = get_category_name($category);
page_header($category_title);
$sql = "SELECT * FROM " . QUIZ_TABLE . "
WHERE quiz_category = $category
ORDER BY quiz_date DESC";
$result = $db->sql_query($sql);
while ($row = $db->sql_fetchrow($result))
{
$user_info = "SELECT username, user_colour
FROM " . USERS_TABLE . "
WHERE user_id = " . $row['quiz_author'];
$user_info_result = $db->sql_query($user_info);
$user_info_row = $db->sql_fetchrow($user_info_result);
$db->sql_freeresult($user_info_result);
// See if the quiz is saved
$find_if_saved_quiz = false;
if( $config['quiz_save_enabled'] == 1 )
{
$saved_sql = "SELECT quiz_progress_id FROM " . QUIZ_PROGRESSIVE_TABLE . "
WHERE quiz_player_id = " . $user->data['user_id'] . "
AND quiz_in_progress = 1
AND quiz_id = " . $row['quiz_id'];
$saved_result = $db->sql_query($saved_sql);
$find_if_saved_quiz = ($db->sql_fetchfield('quiz_progress_id') > 0) ? true : false;
}
// See if already played
$already_played = "SELECT COUNT(stats_id) AS countstats FROM " . QUIZ_STATISTICS_OVERVIEW_TABLE . "
WHERE quiz_id = " . $row['quiz_id'];
$already_played_sql = $db->sql_query($already_played);
$quiz_is_already_played = ($db->sql_fetchfield('countstats') > 0) ? true : false;
// is the quiz new? posted since last login
$is_quiz_new_login = ($user->data['user_lastvisit'] < $row['quiz_date']) ? $user->img('icon_topic_newest', 'VIEW_NEWEST_POST') . ' ' : '';
$template->assign_block_vars('quiz_row', array(
'QUIZ_LINK' => append_sid("{$phpbb_root_path}quiz.$phpEx", 'mode=play&q=' . $row['quiz_id']),
'QUIZ_NAME' => $row['quiz_name'],
'QUIZ_IN_PROGRESS' => $find_if_saved_quiz,
'QUIZ_NEW' => $is_quiz_new_login,
'QUIZ_ALREADY_PLAYED' => $quiz_is_already_played,
'QUIZ_AUTHOR' => get_username_string('full', $row['quiz_author'], $user_info_row['username'], $user_info_row['user_colour']),
'QUIZ_DATE' => $user->format_date($row['quiz_date']),
'QUIZ_VIEWS' => ($row['quiz_views'] == 1) ? sprintf($user->lang['NUMBER_OF_QUIZ_VIEWS_SINGLE'], $row['quiz_views']) : sprintf($user->lang['NUMBER_OF_QUIZ_VIEWS_PLURAL'], $row['quiz_views']),
));
}
$template->assign_vars( array(
'U_NEW_QUIZ' => append_sid("{$phpbb_root_path}quiz.$phpEx", 'mode=submit'),
'U_STATS' => append_sid("{$phpbb_root_path}quiz.$phpEx", 'mode=stats'),
'U_CP' => append_sid("{$phpbb_root_path}quiz_cp.$phpEx"),
'CATEGORY_VIEW' => true,
'CAN_ACCESS_CP' => ((intval($config['quiz_user_edit_own_quiz']) == 1) || $auth->acl_get('a_')) ? true : false,
'CATEGORY_VIEW_NAME' => $category_title,
));
$template->set_filenames(array(
'body' => 'quiz_body.html')
);
page_footer();
}
else
{
page_header($user->lang['QUIZ']);
$sql = "SELECT * FROM " . QUIZ_CATEGORY_TABLE . "
ORDER BY quiz_category_name ASC";
$result = $db->sql_query($sql);
while( $row = $db->sql_fetchrow($result) )
{
$quiz_category_id = $row['quiz_category_id'];
$quiz_category_name = $row['quiz_category_name'];
$quiz_category_desc = $row['quiz_category_description'];
$quiz_category_password = ($row['quiz_category_password']) ? $user->lang['QUIZ_PASSWORD_PROTECTED_CAT'] : false;
$quiz_category_link = append_sid("{$phpbb_root_path}quiz.$phpEx", 'category='.$quiz_category_id);
$quiz_count_quiz_num = find_number_of_quiz_in_category($quiz_category_id);
$template->assign_block_vars('category_row', array(
'CAT_LINK' => $quiz_category_link,
'CAT_NAME' => $quiz_category_name,
'CAT_DESC' => $quiz_category_desc,
'CAT_PSWD' => $quiz_category_password,
'CAT_NUMBER_QUIZZES' => ($quiz_count_quiz_num == 1) ? sprintf($user->lang['NUMBER_OF_QUIZ_INDEX_SINGLE'], $quiz_count_quiz_num) : sprintf($user->lang['NUMBER_OF_QUIZ_INDEX_PLURAL'], $quiz_count_quiz_num),
));
}
$template->assign_vars( array(
'U_NEW_QUIZ' => append_sid("{$phpbb_root_path}quiz.$phpEx", 'mode=submit'),
'U_STATS' => append_sid("{$phpbb_root_path}quiz.$phpEx", 'mode=stats'),
'U_CP' => append_sid("{$phpbb_root_path}quiz_cp.$phpEx"),
'U_QUIZ_INTRODUCTION_VALID' => (strlen($config['quiz_index_introduction']) > 0) ? true : false,
'U_QUIZ_INTRODUCTION_TEXT' => $config['quiz_index_introduction'],
'CAN_ACCESS_CP' => ((intval($config['quiz_user_edit_own_quiz']) == 1) || $auth->acl_get('a_')) ? true : false,
));
$template->set_filenames(array(
'body' => 'quiz_body.html')
);
page_footer();
}
}
page_footer();
?>