Ok.....
So, its all looking great.
One safety feature Id like to put in is for the stats for a given comp to be removed when you change the answers (i.e. to prevent stats being recorded twice).
In reality, it shouldnt happen - but human error could easily mean an incorrect result is submitted with the form, or a result may be overturned after the event, in which case we need to update the quiz with the new answer.
Currently, new statistics get added when you resubmit answers
if its too complex dont worry, I guess Im hoping its a simple thing to do.
One the enter answers page, the questions are still in an incorrect order.
Strange, as I cant see why that would be. Is it working of for you there?
Here is my completes 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) )
{
$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;
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;
echo $val . ' (correct: ' . $correct_answers . ', incorrect: ' . $incorrect_answers . ')<br />';
$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,
);
}
$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'] . ' ';
}
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();
}
}
?>