Skip to content

Commit

Permalink
added function to check table structure
Browse files Browse the repository at this point in the history
  • Loading branch information
zubairraeen committed Jun 27, 2024
1 parent af6429b commit 54e06fd
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 2 deletions.
103 changes: 103 additions & 0 deletions mlw_quizmaster2.php
Original file line number Diff line number Diff line change
Expand Up @@ -784,6 +784,109 @@ public function admin_failed_submission_page() {
$QmnFailedSubmissions->render_list_table();
}
}
/**
* Check database structure
*
* @since 9.0.6
* @return void
*/
public function qsm_check_database_structure() {
global $wpdb;

// Define the table names
$quiz_table_name = $wpdb->prefix . 'mlw_quizzes';
$question_table_name = $wpdb->prefix . 'mlw_questions';
$results_table_name = $wpdb->prefix . 'mlw_results';
$audit_table_name = $wpdb->prefix . 'mlw_qm_audit_trail';
$themes_table_name = $wpdb->prefix . 'mlw_themes';
$quiz_themes_settings_table_name = $wpdb->prefix . 'mlw_quiz_theme_settings';
$question_terms_table_name = $wpdb->prefix . 'mlw_question_terms';

// List of tables and their columns
$tables = [
$quiz_table_name => [
'quiz_id', 'quiz_name', 'message_before', 'message_after', 'message_comment', 'message_end_template',
'user_email_template', 'admin_email_template', 'submit_button_text', 'name_field_text', 'business_field_text',
'email_field_text', 'phone_field_text', 'comment_field_text', 'email_from_text', 'question_answer_template',
'leaderboard_template', 'quiz_system', 'randomness_order', 'loggedin_user_contact', 'show_score', 'send_user_email',
'send_admin_email', 'contact_info_location', 'user_name', 'user_comp', 'user_email', 'user_phone', 'admin_email',
'comment_section', 'question_from_total', 'total_user_tries', 'total_user_tries_text', 'certificate_template',
'social_media', 'social_media_text', 'pagination', 'pagination_text', 'timer_limit', 'quiz_stye', 'question_numbering',
'quiz_settings', 'theme_selected', 'last_activity', 'require_log_in', 'require_log_in_text', 'limit_total_entries',
'limit_total_entries_text', 'scheduled_timeframe', 'scheduled_timeframe_text', 'disable_answer_onselect', 'ajax_show_correct',
'quiz_views', 'quiz_taken', 'deleted', 'quiz_author_id'
],
$question_table_name => [
'question_id', 'quiz_id', 'question_name', 'answer_array', 'answer_one', 'answer_one_points', 'answer_two',
'answer_two_points', 'answer_three', 'answer_three_points', 'answer_four', 'answer_four_points', 'answer_five',
'answer_five_points', 'answer_six', 'answer_six_points', 'correct_answer', 'question_answer_info', 'comments',
'hints', 'question_order', 'question_type', 'question_type_new', 'question_settings', 'category', 'deleted',
'deleted_question_bank'
],
$results_table_name => [
'result_id', 'quiz_id', 'quiz_name', 'quiz_system', 'point_score', 'correct_score', 'correct', 'total', 'name',
'business', 'email', 'phone', 'user', 'user_ip', 'time_taken', 'time_taken_real', 'quiz_results', 'deleted',
'unique_id', 'form_type', 'page_name', 'page_url'
],
$audit_table_name => [
'trail_id', 'action_user', 'action', 'quiz_id', 'quiz_name', 'form_data', 'time'
],
$themes_table_name => [
'id', 'theme', 'theme_name', 'default_settings', 'theme_active'
],
$quiz_themes_settings_table_name => [
'id', 'theme_id', 'quiz_id', 'quiz_theme_settings', 'active_theme'
],
$question_terms_table_name => [
'id', 'question_id', 'quiz_id', 'term_id', 'taxonomy'
]
];

// Check all tables
$errors = [];
foreach ($tables as $table_name => $columns) {
$error = $this->qsm_check_table_structure($table_name, $columns);
if ($error) {
$errors[] = $error;
}
}

if (!empty($errors)) {
foreach ($errors as $error) {
echo esc_html($error) . "<br>";
}
} else {
esc_html_e("All tables have the correct structure.", "quiz-master-next");
update_option('qsm_check_database_structure', 1);
}
}

/**
* Check if table and columns exist
*
* @since 9.0.6
* @param string $table_name
* @param array $expected_columns
* @return string|null
*/
public function qsm_check_table_structure($table_name, $expected_columns) {
global $wpdb;
$columns = $wpdb->get_results("SHOW COLUMNS FROM $table_name");
if (!$columns) {
return esc_html__("Table ", "quiz-master-next") . $table_name . esc_html__(" does not exist.", "quiz-master-next");
}
$existing_columns = array_column($columns, 'Field');
$missing_columns = [];
foreach ($expected_columns as $column) {
if (!in_array($column, $existing_columns)) {
$missing_columns[] = $column;
}
}
if (!empty($missing_columns)) {
return esc_html__("Table ", "quiz-master-next") . $table_name . esc_html__(" is missing columns: ", "quiz-master-next") . implode(', ', $missing_columns);
}
return null;
}

/**
* Failed Database queries
Expand Down
4 changes: 2 additions & 2 deletions php/template-variables.php
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ function qsm_variable_single_answer( $content, $mlw_quiz_array ) {
$ser_answer = $wpdb->get_row( $wpdb->prepare( "SELECT question_settings FROM {$wpdb->prefix}mlw_questions WHERE question_id = %d", $question_id ), ARRAY_A );
$question_settings = qmn_sanitize_input_data( $ser_answer['question_settings'] );
$answerstr = "";
if ( isset($answers['user_answer']) ) {
if ( isset($answers['user_answer']) && is_array($answers['user_answer']) ) {
if ( 13 === intval( $answers['question_type'] ) ) {
$answerstr .= $answers['points'];
}elseif ( 12 === intval( $answers['question_type'] ) ) {
Expand Down Expand Up @@ -510,7 +510,7 @@ function qsm_contact_field_variable( $content, $results_array ) {
function qsm_all_contact_fields_variable( $content, $results ) {
global $mlwQuizMasterNext;
$contact_form = $mlwQuizMasterNext->pluginHelper->get_quiz_setting( 'contact_form' );

$return = '';
if ( isset( $results['contact'] ) && ( is_array( $results['contact'] ) || is_object( $results['contact'] ) ) ) {
foreach ( $results['contact'] as $results_contact ) {
Expand Down

0 comments on commit 54e06fd

Please sign in to comment.