UQM 2.0 - Predictions Extensions

All web design discussion, including Ultimate Quiz MOD support.

Moderator: CricketMX Forum Moderators

subvertbeats
Know-It-All
Know-It-All
Posts: 113
Joined: Thu Jan 14, 2010 6:07 pm

Thanks moongirl !!
battye wrote: Cheating attempts happen because you refreshed the quiz submission page. It is to stop people from refreshing the page in order to reset the time limit. So don't ever refresh a quiz page :wink:
battye....where should I be looking to relax the cheating detection.
Specifically I only want to stop people SUBMITTING a competition more than once.
It should be fine for them to load a quiz to look at questions then revisit the page later to enter.

Thanks!
User avatar
moongirl
Moderator
Moderator
Posts: 19016
Joined: Mon Jan 12, 2004 8:07 am

subvertbeats wrote:Thanks moongirl !!
You're welcome :wink:
Image
That's not the man in the moon...that's me ;)
User avatar
battye
Site Admin
Site Admin
Posts: 14391
Joined: Sun Jan 11, 2004 8:26 am
Location: Australia
Contact:

subvertbeats wrote:One usage question on this battye....

If a match is cancelled, perhaps mid way through the week, when the comp is still open and lots of people have made their votes already, what is the correct procedure to remove that question from the comp (or mark it as invalid if removal is not possible), such that future entrants dont vote on that particular match, and most importantly, so the results for that particular match are not counted?

Thanks for your advice...
In this instance, set the quiz to inactive (using the script I wrote) and don't ever "enter the answers" for it. So results will never be counted.
subvertbeats wrote:Thanks moongirl !!
battye wrote: Cheating attempts happen because you refreshed the quiz submission page. It is to stop people from refreshing the page in order to reset the time limit. So don't ever refresh a quiz page :wink:
battye....where should I be looking to relax the cheating detection.
Specifically I only want to stop people SUBMITTING a competition more than once.
It should be fine for them to load a quiz to look at questions then revisit the page later to enter.

Thanks!
I don't recommend doing this, but if you must:

OPEN quiz.php

FIND

Code: Select all

trigger_error($user->lang['CHEATING_ATTEMPT']);
REPLACE WITH

Code: Select all

//trigger_error($user->lang['CHEATING_ATTEMPT']);
CricketMX.com in 2022: Still the home of bat's, rat's and other farmyard animals!

"OK, life [as you chose to define it] repeats until there are no more lessons to be learned." - nrnoble (June 12, 2005)
"the new forum looks awesome, it's getting bigger & better" - p2p-sharing-rules (11 Jan, 2008)
"Looks like CMX is not only getting bigger...but, also getting better!!" - moongirl (14 Dec, 2007)
subvertbeats
Know-It-All
Know-It-All
Posts: 113
Joined: Thu Jan 14, 2010 6:07 pm

Hi battye, I hope your weekend has been good.....thanks for the reply...
battye wrote:
subvertbeats wrote:One usage question on this battye....

If a match is cancelled, perhaps mid way through the week, when the comp is still open and lots of people have made their votes already, what is the correct procedure to remove that question from the comp (or mark it as invalid if removal is not possible), such that future entrants dont vote on that particular match, and most importantly, so the results for that particular match are not counted?

Thanks for your advice...
In this instance, set the quiz to inactive (using the script I wrote) and don't ever "enter the answers" for it. So results will never be counted.
Sorry, what I mean here is that one match from an event may have been cancelled, or changed at some point mid way through the competition being open. In that case, you want to ignore/disable/delete that question from the quiz, whilst leaving the other matches (questions) active in the quiz.
battye wrote:
subvertbeats wrote: battye....where should I be looking to relax the cheating detection.
Specifically I only want to stop people SUBMITTING a competition more than once.
It should be fine for them to load a quiz to look at questions then revisit the page later to enter.

Thanks!
I don't recommend doing this, but if you must:
Why is this not recommended (given the different usage context, and the fact we dont have any kind of time limit in action?)

Thanks!
User avatar
battye
Site Admin
Site Admin
Posts: 14391
Joined: Sun Jan 11, 2004 8:26 am
Location: Australia
Contact:

OPEN quiz.php

FIND around line 60

Code: Select all

					$result = $db->sql_query($sql);
					while( $row = $db->sql_fetchrow($result) )
REPLACE WITH

Code: Select all

					$result = $db->sql_query($sql);
					while( $row = $db->sql_fetchrow($result) && $_POST['question' . $question_id_set[$i]] != 'cancelled' )
Then if a match is cancelled, enter the word cancelled as the answer.
CricketMX.com in 2022: Still the home of bat's, rat's and other farmyard animals!

"OK, life [as you chose to define it] repeats until there are no more lessons to be learned." - nrnoble (June 12, 2005)
"the new forum looks awesome, it's getting bigger & better" - p2p-sharing-rules (11 Jan, 2008)
"Looks like CMX is not only getting bigger...but, also getting better!!" - moongirl (14 Dec, 2007)
subvertbeats
Know-It-All
Know-It-All
Posts: 113
Joined: Thu Jan 14, 2010 6:07 pm

Thanks :)

So Id first edit the question so that 'Cancelled' is one of the multiple choice options for that question (maybe replaced 'Draw' with 'Cancelled')?

Then re-enter the answer using ?mode=enter_answers, and select the 'cancelled' option for that question?

Also, just to confirm that re-entering the answers should not cause any duplicate statistics to be recorded?
User avatar
battye
Site Admin
Site Admin
Posts: 14391
Joined: Sun Jan 11, 2004 8:26 am
Location: Australia
Contact:

Yes, use a lower case "c".
Also, just to confirm that re-entering the answers should not cause any duplicate statistics to be recorded?
I don't think it should.
CricketMX.com in 2022: Still the home of bat's, rat's and other farmyard animals!

"OK, life [as you chose to define it] repeats until there are no more lessons to be learned." - nrnoble (June 12, 2005)
"the new forum looks awesome, it's getting bigger & better" - p2p-sharing-rules (11 Jan, 2008)
"Looks like CMX is not only getting bigger...but, also getting better!!" - moongirl (14 Dec, 2007)
subvertbeats
Know-It-All
Know-It-All
Posts: 113
Joined: Thu Jan 14, 2010 6:07 pm

battye wrote:OPEN quiz.php

FIND around line 60

Code: Select all

					$result = $db->sql_query($sql);
					while( $row = $db->sql_fetchrow($result) )
REPLACE WITH

Code: Select all

					$result = $db->sql_query($sql);
					while( $row = $db->sql_fetchrow($result) && $_POST['question' . $question_id_set[$i]] != 'cancelled' )
Then if a match is cancelled, enter the word cancelled as the answer.
thanks (again!) battye....

One q on this....

You say quiz.php, around line 60

In my quiz.php, these lines occur around line 1140.
In control_quiz.php, they occur around line 60.

Im thinking this change needs to be in control_quiz.php, but it would be great if you could confirm that.
User avatar
battye
Site Admin
Site Admin
Posts: 14391
Joined: Sun Jan 11, 2004 8:26 am
Location: Australia
Contact:

Yes, I meant the one I wrote for you. Sorry for the confusion.
CricketMX.com in 2022: Still the home of bat's, rat's and other farmyard animals!

"OK, life [as you chose to define it] repeats until there are no more lessons to be learned." - nrnoble (June 12, 2005)
"the new forum looks awesome, it's getting bigger & better" - p2p-sharing-rules (11 Jan, 2008)
"Looks like CMX is not only getting bigger...but, also getting better!!" - moongirl (14 Dec, 2007)
subvertbeats
Know-It-All
Know-It-All
Posts: 113
Joined: Thu Jan 14, 2010 6:07 pm

Hi battye

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.

Today Ive come to enter the correct answers, and Im getting this SQL error:
SQL ERROR [ mysql4 ]

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 2 [1064]

SQL

SELECT username FROM phpbb_users WHERE user_id =

BACKTRACE

FILE: includes/db/mysql.php
LINE: 174
CALL: dbal->sql_error()

FILE: control_quiz.php
LINE: 66
CALL: dbal_mysql->sql_query()

quiz.php:

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();
?>

control_quiz.php:

Code: Select all

<?php
// control_quiz.php

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);

   if( !$auth->acl_get('a_') )
   {
      die('No access');
   }

   else
   {
      if( $_GET['mode'] == 'deactivate' )
      {
         $deactivate_id = (int) $_GET['id'];

         $sql = "UPDATE " . QUIZ_TABLE . " SET quiz_deactivate = 1
               WHERE quiz_id = $deactivate_id";
         $db->sql_query($sql);
         die('Quiz id ' . $deactivate_id . ' has been deactivated');
      }

      if( $_GET['mode'] == 'activate' )
      {
         $activate_id = (int) $_GET['id'];

         $sql = "UPDATE " . QUIZ_TABLE . " SET quiz_deactivate = 0
               WHERE quiz_id = $activate_id";
         $db->sql_query($sql);
         die('Quiz id ' . $activate_id . ' has been activated');
      }

      if( $_GET['mode'] == 'enter_answers' )
      {
         if( $_GET['a'] == 'submit' )
         {
            $question_id_hidden = $_POST['questions'];
            $question_id_set = explode(',', $question_id_hidden);
            $question_id_set_count = sizeof($question_id_set) - 1;

            $users = array();
            $users_information = array();
            $users_names = array();

            for($i = 0; $i < $question_id_set_count; $i++ )
            {
               $sql = 'SELECT * FROM ' . QUIZ_STATISTICS_TABLE . '
                     WHERE quiz_question_id = ' . $question_id_set[$i] . '
                     ORDER BY quiz_question_id ASC';
               $result = $db->sql_query($sql);
               while( $row = $db->sql_fetchrow($result) && $_POST['question' . $question_id_set[$i]] != 'cancelled' )
               {
                  $users[$i] = $row['quiz_player'];

                  $user_sql = 'SELECT username FROM ' . USERS_TABLE . '
                            WHERE user_id = ' . $row['quiz_player'];
                  $user_result = $db->sql_query($user_sql);
                  $user_name_fetch = $db->sql_fetchfield('username');
                  $db->sql_freeresult($user_result);

                  $users_names[$row['quiz_player']] = $user_name_fetch;

                  $is_correct = 0;
                  $is_incorrect = 0;

                  if( $row['quiz_answer'] == $_POST['question' . $question_id_set[$i]] )
                  {
                     // echo 'User <strong>' . $user_name_fetch . '</strong> answered question <strong>' . $question_id_set[$i] . '</strong> CORRECTLY<br />';
         
                     $users_information[$row['quiz_player']]['quiz_correct']++;
                     $users_information[$row['quiz_player']]['quiz_id'] = $row['quiz_id'];
                     $users_information[$row['quiz_player']]['info'] = 'Question ' . $question_id_set[$i] . ' correct';
                  }

                  else
                  {
                     // echo 'User <strong>' . $user_name_fetch . '</strong> answered question <strong>' . $question_id_set[$i] . '</strong> INCORRECTLY. They entered the answer <strong>' . $row['quiz_answer'] . '</strong>.<br />';

                     $users_information[$row['quiz_player']]['quiz_incorrect']++;
                     $users_information[$row['quiz_player']]['quiz_id'] = $row['quiz_id'];
                     $users_information[$row['quiz_player']]['info'] = 'Question ' . $question_id_set[$i] . ' incorrect (' . $row['quiz_answer'] . ')';
                  }
               }
            }

            /* for( $j = 0, $j_count = sizeof($users); $j < $j_count; $j++ )
            {
               echo 'Username: ' . $users_names[$users[$j]] . '<br />';
               echo '-- Correct: ' .  $users_information[$users[$j]]['quiz_correct'] . '<br />';
               echo '-- Incorrect: ' .  $users_information[$users[$j]]['quiz_incorrect'] . '<br />';
               echo '-- Quiz ID: ' .  $users_information[$users[$j]]['quiz_id'] . '<br />';
            } */

            $counter = 0;
			$highest_correct = 0;
			$output_array = array();
            foreach($users_names as $key => $val)
            {
				$quiz_id_key = $users_information[$key]['quiz_id'];
               if( $counter == 0 )
               {
                  $counter++;
                  echo 'QUIZ ID: ' . $quiz_id_key . ' QUIZ NAME: ' . get_quiz_title($quiz_id_key) . '<br />';
               }
               // $key is the user_id, $val is the username
               $correct_answers = ($users_information[$key]['quiz_correct']) ? $users_information[$key]['quiz_correct'] : 0;
               $incorrect_answers = ($users_information[$key]['quiz_incorrect']) ? $users_information[$key]['quiz_incorrect'] : 0;

               $output_array[$correct_answers][] = $val . ' (correct: ' .  $correct_answers . ', incorrect: ' .  $incorrect_answers . ')<br />';
               $highest_correct = ($correct_answers > $highest_correct) ? $correct_answers : $highest_correct;

               $statistical_overview_setup[] = array(
                  'quiz_id'         => (int) $quiz_id_key,
                    'quiz_correct'      => (int) $correct_answers,
                    'quiz_incorrect'   => (int) $incorrect_answers,
                  'user_id'         => (int) $key,
                  'quiz_time'         => (int) 0,
               );
            }
			
			for( $m = $highest_correct; $m >= 0; $m-- )
            {
               $size_current_ca = sizeof($output_array[$m]);
               if( $size_current_ca > 0 )
               {
                  for( $n = 0; $n < $size_current_ca; $n++ )
                  {
                     echo $output_array[$m][$n];
                  }
               }
            }
			
            $db->sql_multi_insert(QUIZ_STATISTICS_OVERVIEW_TABLE, $statistical_overview_setup);

            echo 'SQL Query entered';

            die();
         }

         $quiz_id = (int) $_GET['id'];
         $sql = 'SELECT * FROM ' . QUIZ_QUESTIONS_TABLE . '
               WHERE quiz_related_id = ' . $quiz_id;
         $result = $db->sql_query($sql);
         echo '<form action="control_quiz.php?mode=enter_answers&id=' . $quiz_id . '&a=submit" method="post">';
         $question_id_hidden = '';
         while( $row = $db->sql_fetchrow($result) )
         {   
            $question_id_hidden .= $row['quiz_question_id'] . ',';
            echo '<strong>Question: ' . $row['quiz_question'] . '</strong><br />';
            // echo 'Correct answer: <input type="text" name="question' . $row['quiz_question_id'] . '" />';
            echo 'Correct answer:';

            $ans = 'SELECT quiz_answer FROM ' . QUIZ_DATA_TABLE . '
                  WHERE quiz_question_id = ' . $row['quiz_question_id'];
            $ans_res = $db->sql_query($ans);
            while( $ans_row = $db->sql_fetchrow($ans_res) )
            {
               echo '<input type="radio" value="' . $ans_row['quiz_answer'] . '" name="question' . $row['quiz_question_id'] . '" /> ' . $ans_row['quiz_answer'] . '&nbsp;&nbsp;&nbsp;';
            } 

            echo '<br /><br />';
         }
         echo '<input type="hidden" value="' . $question_id_hidden . '" name="questions" />';
         echo '<input type="submit" value="Submit" name="submit" />';
         echo '</form>';

         die(); // new
      }

      else
      {
         echo 'Mode types:<br />
            <strong>control_quiz.php?mode=deactivate&id=XXX</strong><br />
            <strong>control_quiz.php?mode=activate&id=XXX</strong><br />
            <strong>control_quiz.php?mode=enter_answers&id=XXX</strong>';
         die();
      }
   }
?>
Any ideas?
User avatar
battye
Site Admin
Site Admin
Posts: 14391
Joined: Sun Jan 11, 2004 8:26 am
Location: Australia
Contact:

Have a look in the phpbb_quiz_statistics table and make sure there is:

a) a quiz_player column
b) all rows have a quiz_player entry
CricketMX.com in 2022: Still the home of bat's, rat's and other farmyard animals!

"OK, life [as you chose to define it] repeats until there are no more lessons to be learned." - nrnoble (June 12, 2005)
"the new forum looks awesome, it's getting bigger & better" - p2p-sharing-rules (11 Jan, 2008)
"Looks like CMX is not only getting bigger...but, also getting better!!" - moongirl (14 Dec, 2007)
subvertbeats
Know-It-All
Know-It-All
Posts: 113
Joined: Thu Jan 14, 2010 6:07 pm

Hi battye

Yep, column exists and is populated throughout
User avatar
battye
Site Admin
Site Admin
Posts: 14391
Joined: Sun Jan 11, 2004 8:26 am
Location: Australia
Contact:

FIND in control_quiz.php

Code: Select all

                 $users[$i] = $row['quiz_player'];
AFTER ADD

Code: Select all

                 echo '>>'. $row['quiz_player'] . '<br />';
And post the output in your next reply :)
CricketMX.com in 2022: Still the home of bat's, rat's and other farmyard animals!

"OK, life [as you chose to define it] repeats until there are no more lessons to be learned." - nrnoble (June 12, 2005)
"the new forum looks awesome, it's getting bigger & better" - p2p-sharing-rules (11 Jan, 2008)
"Looks like CMX is not only getting bigger...but, also getting better!!" - moongirl (14 Dec, 2007)
subvertbeats
Know-It-All
Know-It-All
Posts: 113
Joined: Thu Jan 14, 2010 6:07 pm

thanks battye... I get no output there:
>>

Warning: Cannot modify header information - headers already sent by (output started at /home/mmadent/public_html/foru/control_quiz.php:64) in /home/mmadent/public_html/foru/includes/functions.php on line 3543
User avatar
battye
Site Admin
Site Admin
Posts: 14391
Joined: Sun Jan 11, 2004 8:26 am
Location: Australia
Contact:

I suspect you might be trying to enter answers for a quiz that doesn't exist. Double check the id you used is correct.
CricketMX.com in 2022: Still the home of bat's, rat's and other farmyard animals!

"OK, life [as you chose to define it] repeats until there are no more lessons to be learned." - nrnoble (June 12, 2005)
"the new forum looks awesome, it's getting bigger & better" - p2p-sharing-rules (11 Jan, 2008)
"Looks like CMX is not only getting bigger...but, also getting better!!" - moongirl (14 Dec, 2007)
Post Reply