From 283360f4b127cddbae3b0dc53a1a169b510fc048 Mon Sep 17 00:00:00 2001 From: etchirag Date: Mon, 7 Oct 2024 19:36:34 +0530 Subject: [PATCH 1/2] Fixed issue with importing quetoins --- js/qsm-admin.js | 23 +++++++++++++++++++++++ php/rest-api.php | 3 ++- 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/js/qsm-admin.js b/js/qsm-admin.js index 2cd08aab..4bf0de55 100644 --- a/js/qsm-admin.js +++ b/js/qsm-admin.js @@ -2029,6 +2029,26 @@ var qsm_link_button; } if ( 1 > questions.length ) { $('#question-bank').append('
' + qsm_admin_messages.questions_not_found + '
'); + } else { + $('.question-bank-question').each(function () { + let questionId = $(this).data('question-id'); + if (QSMQuestion.questions.some(q => q.get('id') == questionId)) { + let $linkButton = $(this).find('.link-question'); + if ($linkButton.length) { + $linkButton.prop('disabled', true).addClass('disabled'); + } + } + QSMQuestion.questions.each(function (question) { + let merged_questions = question.get('merged_question'); + let questionsArray = merged_questions ? merged_questions.split(',').map(q => q.trim()) : []; + questionsArray.forEach(function (id) { + let parentElement = $('.question-bank-question[data-question-id="' + id + '"]'); + if (parentElement.length) { + parentElement.remove(); // Remove the element if it exists + } + }); + }); + }); } }, addQuestionToQuestionBank: function (question) { @@ -3324,6 +3344,7 @@ var qsm_link_button; }); jQuery(document).on('click', '.qsm-linked-list-div-block .qsm-unlink-the-question', function () { + var to_be_unlink_question = jQuery(this).data('question-id'); $.ajax({ url: ajaxurl, method: 'POST', @@ -3334,6 +3355,8 @@ var qsm_link_button; }, success: function (response) { $(document).find('.qsm-linked-list-div-block').remove(); + let question = QSMQuestion.questions.get(to_be_unlink_question); + question.set('merged_question', ''); } }); }); diff --git a/php/rest-api.php b/php/rest-api.php index d59d4d80..4970dda8 100644 --- a/php/rest-api.php +++ b/php/rest-api.php @@ -500,7 +500,8 @@ function qsm_rest_get_question( WP_REST_Request $request ) { if ( isset( $question['linked_question'] ) && '' == $question['linked_question'] ) { $comma_seprated_ids = $is_linking; } else { - $expolded_question_array = explode(',', $question['linked_question']); + $linked_question = isset($question['linked_question']) ? $question['linked_question'] : ''; + $exploded_question_array = explode(',', $linked_question); $expolded_question_array[] = $is_linking; $comma_seprated_ids = implode( ',', array_unique($expolded_question_array) ); } From 6c727e7be0d6908c911ad2a16020efa1d86dbebd Mon Sep 17 00:00:00 2001 From: etchirag Date: Mon, 7 Oct 2024 20:55:53 +0530 Subject: [PATCH 2/2] Removed linked question functionality from dev branch --- css/qsm-admin.css | 85 --------- js/qsm-admin.js | 102 +---------- mlw_quizmaster2.php | 5 - php/admin/options-page-questions-tab.php | 216 +---------------------- php/classes/class-qsm-install.php | 9 - php/classes/class-qsm-questions.php | 107 ++++------- php/rest-api.php | 56 +----- 7 files changed, 46 insertions(+), 534 deletions(-) diff --git a/css/qsm-admin.css b/css/qsm-admin.css index dcc71bd4..f15f72b6 100644 --- a/css/qsm-admin.css +++ b/css/qsm-admin.css @@ -1459,91 +1459,6 @@ td.scheduled_time_start { box-sizing: border-box; } -#post-body-content .qsm-popup-upgrade-warning img { - width: auto; - height: 15px; - margin-right: 8px; -} - -.qsm-linked-list-div-block { - padding: 4px; - margin: 5px 0; - margin-top: -20px; - width: max-content; - background-color: #ffe684; - border-radius: 2px; -} - -span.qsm-linked-list-view-button { - cursor: pointer; - color: #135e96; - text-decoration: underline; - font-weight: 500; -} - -span.qsm-linked-list-item { - margin: 0 0 5px 0; - padding: 5px; - background: #f0f0f1; -} - -div.qsm-linked-list-inside span.qsm-unlink-the-question { - color: #DC3232; - font-weight: 500; - max-width: 60px; - cursor: pointer; -} - -.qsm-linked-list-div-block p { - margin: 0; - font-size: 13px; - color: #856404; -} - -.qsm-linked-list-container { - position: absolute; -} - -.qsm-linked-list-div-block .qsm-linked-list-inside { - position: relative; - right: -155px; - top: 20px; - z-index: 999; - width: 350px; - padding: 10px; - box-sizing: border-box; - border-radius: 4px; - background: #ffffff; - border: 1px solid #dfd4d4; - box-shadow: 0 0 6px 2px #ddd; - display: grid; - grid-template-columns: 1fr; -} - -.qsm-linked-list-div-block .qsm-linked-list-inside:before { - content: " "; - position: absolute; - top: -24px; - left: 28%; - margin-left: -12px; - border-width: 12px; - border-style: solid; - border-color: transparent transparent #ffffff transparent; - z-index: 1; -} - -.qsm-linked-list-div-block .qsm-linked-list-inside:after { - content: " "; - position: absolute; - top: -26px; - left: 28%; - margin-left: -12px; - border-width: 12px; - border-style: solid; - border-color: transparent transparent #dfd4d4 transparent; - z-index: 0; -} - .qsm-text-main-wrap #postbox-container-1 { position: relative; diff --git a/js/qsm-admin.js b/js/qsm-admin.js index 4bf0de55..277e3378 100644 --- a/js/qsm-admin.js +++ b/js/qsm-admin.js @@ -1899,7 +1899,6 @@ var QSMContact; */ var QSMQuestion; var import_button; -var qsm_link_button; (function ($) { if (jQuery('body').hasClass('admin_page_mlw_quiz_options')) { if (window.location.href.indexOf('&tab') == -1 || window.location.href.indexOf('tab=questions') > 0) { @@ -2029,26 +2028,6 @@ var qsm_link_button; } if ( 1 > questions.length ) { $('#question-bank').append('
' + qsm_admin_messages.questions_not_found + '
'); - } else { - $('.question-bank-question').each(function () { - let questionId = $(this).data('question-id'); - if (QSMQuestion.questions.some(q => q.get('id') == questionId)) { - let $linkButton = $(this).find('.link-question'); - if ($linkButton.length) { - $linkButton.prop('disabled', true).addClass('disabled'); - } - } - QSMQuestion.questions.each(function (question) { - let merged_questions = question.get('merged_question'); - let questionsArray = merged_questions ? merged_questions.split(',').map(q => q.trim()) : []; - questionsArray.forEach(function (id) { - let parentElement = $('.question-bank-question[data-question-id="' + id + '"]'); - if (parentElement.length) { - parentElement.remove(); // Remove the element if it exists - } - }); - }); - }); } }, addQuestionToQuestionBank: function (question) { @@ -2062,22 +2041,19 @@ var qsm_link_button; type: question.type, question: questionText, category: question.category, - quiz_name: question.quiz_name, - linked_question: question.linked_question.join(',') + quiz_name: question.quiz_name })); }, - addQuestionFromQuestionBank: function (questionID, is_linking = 0) { + addQuestionFromQuestionBank: function (questionID) { QSMAdmin.displayAlert(qsm_admin_messages.adding_question, 'info'); - let isLinkingData = is_linking == 1 ? questionID : 0; var model = new QSMQuestion.question({ - id: questionID, - is_linking: isLinkingData + id: questionID }); model.fetch({ headers: { 'X-WP-Nonce': qsmQuestionSettings.nonce }, - url: wpApiSettings.root + 'quiz-survey-master/v1/questions/' + questionID + '?is_linking=' + isLinkingData, + url: wpApiSettings.root + 'quiz-survey-master/v1/questions/' + questionID, success: QSMQuestion.questionBankSuccess, error: QSMAdmin.displayError }); @@ -2269,18 +2245,10 @@ var qsm_link_button; if(import_button){ import_button.html(qsm_admin_messages.add_question); } - if(qsm_link_button) { - qsm_link_button.html(qsm_admin_messages.link_question); - } if(import_button){ import_button.attr("onclick", "return confirm('" + qsm_admin_messages.confirm_message + " " + qsm_admin_messages.import_question_again + "');"); } QSMQuestion.openEditPopup(model.id, $('.question[data-question-id=' + model.id + ']').find('.edit-question-button')); - console.log(qsm_link_button); - if(qsm_link_button == ''){ - $(document).find('.qsm-linked-list-inside').hide().empty(); - $(document).find('.qsm-linked-list-div-block').hide(); - } // $('#save-popup-button').trigger('click'); }, addNewQuestion: function (model) { @@ -2816,31 +2784,6 @@ var qsm_link_button; $('#image_size_area').show(); } - let link_quizzes_array = question.get('link_quizzes'); - - $('.qsm-linked-list-inside').hide().empty(); - $('.qsm-linked-list-div-block').hide(); - if (typeof link_quizzes_array == 'object' && link_quizzes_array != null && Object.keys(link_quizzes_array).length > 0) { - Object.values(link_quizzes_array).forEach(function(quizName) { - // Ensure each quizName is a valid non-empty string - if (quizName && typeof quizName == 'string' && quizName.trim().length > 0) { - let link = $('') - .attr('class', 'qsm-linked-list-item') - .attr('title', quizName) - .text(quizName.length > 25 ? quizName.substring(0, 25) + '...' : quizName); - - $('.qsm-linked-list-div-block').show(); - $('.qsm-linked-list-inside').append(link); - } - }); - - // Add an "Unlink" link at the end - let unlink = $('') - .attr('class', 'qsm-unlink-the-question button button-danger') - .attr('data-question-id', questionID) - .text(qsm_admin_messages.unlink_question); - $('.qsm-linked-list-inside').append(unlink); - } jQuery(document).trigger('qsm_open_edit_popup', [questionID, CurrentElement]); }, openEditPagePopup: function (pageID) { @@ -3317,7 +3260,6 @@ var qsm_link_button; $(document).on('click', '.qsm-popup-bank .import-button', function (event) { event.preventDefault(); - qsm_link_button = ''; $(this).text(qsm_admin_messages.adding_question); import_button = $(this); $('.import-button').addClass('disable_import'); @@ -3325,45 +3267,9 @@ var qsm_link_button; MicroModal.close('modal-2'); }); - - $(document).on('click', '.qsm-popup-bank .link-question', function (event) { - event.preventDefault(); - $(this).text(qsm_admin_messages.linking_question); - qsm_link_button = $(this); - $('.link-question').addClass('disable_import'); - // 1 for the linking the questions default is 0 - QSMQuestion.addQuestionFromQuestionBank($(this).data('question-id'), 1); - MicroModal.close('modal-2'); - }); - - jQuery(document).on('click', '.qsm-linked-list-div-block .qsm-linked-list-view-button', function () { - let $this = jQuery(this); - let $inside = $this.parents('.qsm-linked-list-div-block').find('.qsm-linked-list-inside'); - $inside.toggle(); - $inside.is(':visible') ? $this.text(qsmQuestionSettings.linked_close) : $this.text(qsmQuestionSettings.linked_view); - }); - - jQuery(document).on('click', '.qsm-linked-list-div-block .qsm-unlink-the-question', function () { - var to_be_unlink_question = jQuery(this).data('question-id'); - $.ajax({ - url: ajaxurl, - method: 'POST', - data: { - action: 'qsm_unlink_question_from_list', - question_id: jQuery(this).data('question-id'), - nonce: qsmQuestionSettings.unlinkNonce - }, - success: function (response) { - $(document).find('.qsm-linked-list-div-block').remove(); - let question = QSMQuestion.questions.get(to_be_unlink_question); - question.set('merged_question', ''); - } - }); - }); //Click on selected question button. $('.qsm-popup-bank').on('click', '#qsm-import-selected-question', function (event) { var $total_selction = $('#question-bank').find('[name="qsm-question-checkbox[]"]:checked').length; - qsm_link_button = ''; if ($total_selction === 0) { alert(qsm_admin_messages.no_question_selected); } else { diff --git a/mlw_quizmaster2.php b/mlw_quizmaster2.php index 57d77da8..ce63fc21 100644 --- a/mlw_quizmaster2.php +++ b/mlw_quizmaster2.php @@ -525,13 +525,8 @@ public function qsm_admin_scripts_style( $hook ) { 'results_page_saved' => __('Results pages were saved!', 'quiz-master-next'), 'results_page_save_error' => __('There was an error when saving the results pages.', 'quiz-master-next'), 'all_categories' => __('All Categories', 'quiz-master-next'), - 'add_question' => __('Add', 'quiz-master-next'), 'question_created' => __('Question created!', 'quiz-master-next'), 'new_question' => __('Your new question!', 'quiz-master-next'), - 'unlink_question' => __('Unlink', 'quiz-master-next'), - 'adding_question' => __('Adding...', 'quiz-master-next'), - 'linking_question' => __('Linking...', 'quiz-master-next'), - 'link_question' => __('Link', 'quiz-master-next'), 'creating_question' => __('Creating question...', 'quiz-master-next'), 'duplicating_question' => __('Duplicating question...', 'quiz-master-next'), 'saving_question' => __('Saving question...', 'quiz-master-next'), diff --git a/php/admin/options-page-questions-tab.php b/php/admin/options-page-questions-tab.php index 5139748c..4ad17e24 100644 --- a/php/admin/options-page-questions-tab.php +++ b/php/admin/options-page-questions-tab.php @@ -88,14 +88,11 @@ function qsm_options_questions_tab_content() { $json_data = array( 'quizID' => $quiz_id, 'answerText' => __( 'Answer', 'quiz-master-next' ), - 'linked_view' => __( 'View', 'quiz-master-next' ), - 'linked_close' => __( 'Close', 'quiz-master-next' ), 'nonce' => wp_create_nonce( 'wp_rest' ), 'pages' => $pages, 'qpages' => $qpages, 'qsm_user_ve' => get_user_meta( $user_id, 'rich_editing', true ), 'saveNonce' => wp_create_nonce( 'ajax-nonce-sandy-page' ), - 'unlinkNonce' => wp_create_nonce( 'ajax-nonce-unlink-question' ), 'categories' => $question_categories, 'form_type' => $form_type, 'quiz_system' => $quiz_system, @@ -283,12 +280,6 @@ class="save-page-button button button-primary">
-
-

-
-
-
-
@@ -884,80 +875,6 @@ class="save-page-button button button-primary"> __( - 'Nonce verification failed.', - 'quiz-master-next' - ), - )); - } - $question_id = isset( $_POST['question_id'] ) ? intval( $_POST['question_id'] ) : 0; - if ( $question_id > 0 ) { - qsm_process_unlink_question_from_list_by_question_id($question_id); - wp_send_json_success( array( - 'message' => __( - 'Question is unlinked from all quizzes.', - 'quiz-master-next' - ), - )); - } else { - wp_send_json_error( array( - 'message' => __( - 'Invalid question ID.', - 'quiz-master-next' - ), - )); - } -} -add_action( 'wp_ajax_qsm_unlink_question_from_list', 'qsm_ajax_unlink_question_from_list' ); - -/** - * Unlinks a question from all quizzes it is associated with. - * @since 9.1.3 - * @param int $question_id The ID of the question to unlink. - * @return void - */ -function qsm_process_unlink_question_from_list_by_question_id( $question_id ) { - global $wpdb; - $current_linked_questions = $wpdb->get_var( $wpdb->prepare( - "SELECT linked_question FROM {$wpdb->prefix}mlw_questions WHERE question_id = %d", - $question_id - ) ); - - if ( $current_linked_questions ) { - $current_links = explode(',', $current_linked_questions); - $current_links = array_map('trim', $current_links); - $current_links = array_diff($current_links, [ $question_id ]); - $updated_linked_list = implode(',', array_filter($current_links)); - $linked_ids = explode(',', $updated_linked_list); - foreach ( $linked_ids as $linked_id ) { - $wpdb->update( - $wpdb->prefix . 'mlw_questions', - array( 'linked_question' => $updated_linked_list ), - array( 'question_id' => intval($linked_id) ), - array( '%s' ), - array( '%d' ) - ); - } - $wpdb->update( - $wpdb->prefix . 'mlw_questions', - array( 'linked_question' => '' ), - array( 'question_id' => intval($question_id) ), - array( '%s' ), - array( '%d' ) - ); - } -} - /** * Saves the pages and order from the Questions tab * @@ -1173,36 +1090,11 @@ function qsm_delete_question_from_database() { if ( ! isset( $_POST['nonce'] ) || ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['nonce'] ) ), 'delete_question_from_database' ) ) { wp_send_json_error( __( 'Nonce verification failed.', 'quiz-master-next' ) ); } - $base_question_id = $question_id = isset( $_POST['question_id'] ) ? intval( $_POST['question_id'] ) : 0; + $question_id = isset( $_POST['question_id'] ) ? intval( $_POST['question_id'] ) : 0; if ( $question_id ) { global $wpdb, $mlwQuizMasterNext; - $update_qpages_after_delete = array(); - $dependent_question_ids = qsm_get_unique_linked_question_ids_to_remove(array( $question_id ) ); - $question_id = array_merge($dependent_question_ids, array( $question_id ) ); - $question_id = array_unique($question_id); - $question_id = array_filter($question_id); - $placeholders = array_fill( 0, count( $question_id ), '%d' ); - if ( ! empty($dependent_question_ids) ) { - $dependent_question_ids = array_diff($dependent_question_ids, [ $base_question_id ] ); - $update_qpages_after_delete = qsm_process_to_update_qpages_after_unlink($dependent_question_ids); - } - // Construct the query with placeholders - $query = sprintf( - "DELETE FROM {$wpdb->prefix}mlw_questions WHERE question_id IN (%s)", - implode( ', ', $placeholders ) - ); - - // Prepare the query - $query = $wpdb->prepare( $query, $question_id ); - $results = $wpdb->query( $query ); + $results = $wpdb->delete( $wpdb->prefix . 'mlw_questions', array( 'question_id' => $question_id ) ); if ( $results ) { - if ( ! empty($update_qpages_after_delete) ) { - foreach ( $update_qpages_after_delete as $quiz_id => $aftervalue ) { - $mlwQuizMasterNext->pluginHelper->prepare_quiz( $quiz_id ); - $mlwQuizMasterNext->pluginHelper->update_quiz_setting( 'qpages', $aftervalue['qpages'] ); - $mlwQuizMasterNext->pluginHelper->update_quiz_setting( 'pages', $aftervalue['pages'] ); - } - } wp_send_json_success( __( 'Question removed Successfully.', 'quiz-master-next' ) ); }else { wp_send_json_error( __( 'Question delete failed!', 'quiz-master-next' ) ); @@ -1241,20 +1133,10 @@ function qsm_bulk_delete_question_from_database() { } ); // Sanitize and validate the IDs - $base_question_ids = $question_id = array_map( 'intval', $question_id ); + $question_id = array_map( 'intval', $question_id ); - if ( ! empty( $question_id ) ) { - global $wpdb; - $update_qpages_after_delete = array(); - $dependent_question_ids = qsm_get_unique_linked_question_ids_to_remove($question_id); - if ( ! empty($dependent_question_ids) ) { - $dependent_question_ids = array_diff($dependent_question_ids, [ $base_question_ids ] ); - $update_qpages_after_delete = qsm_process_to_update_qpages_after_unlink($dependent_question_ids); - } - $question_id = array_merge($dependent_question_ids, $question_id); - $question_id = array_unique($question_id); // Ensure all IDs are unique - $question_id = array_filter($question_id); // Remove empty values - + if ( ! empty( $question_id ) ) { + // Generate placeholders for each ID $placeholders = array_fill( 0, count( $question_id ), '%d' ); // Construct the query with placeholders @@ -1268,13 +1150,6 @@ function qsm_bulk_delete_question_from_database() { $results = $wpdb->query( $query ); if ( $results ) { - if ( ! empty($update_qpages_after_delete) ) { - foreach ( $update_qpages_after_delete as $quiz_id => $aftervalue ) { - $mlwQuizMasterNext->pluginHelper->prepare_quiz( $quiz_id ); - $mlwQuizMasterNext->pluginHelper->update_quiz_setting( 'qpages', $aftervalue['qpages'] ); - $mlwQuizMasterNext->pluginHelper->update_quiz_setting( 'pages', $aftervalue['pages'] ); - } - } wp_send_json_success( __( 'Questions removed Successfully.', 'quiz-master-next' ) ); }else { $mlwQuizMasterNext->log_manager->add( __('Error 0001 delete questions failed - question IDs:', 'quiz-master-next') . $question_id, '
Error:' . $wpdb->last_error . ' from ' . $wpdb->last_query, 0, 'error' ); @@ -1286,82 +1161,6 @@ function qsm_bulk_delete_question_from_database() { } add_action( 'wp_ajax_qsm_bulk_delete_question_from_database', 'qsm_bulk_delete_question_from_database' ); -/** - * returns pages and qpages for dependent question ids for update after deleting questions - * - * @param array $dependent_question_ids An array of question IDs. - * @since 9.1.3 - */ -function qsm_process_to_update_qpages_after_unlink( $dependent_question_ids ) { - $comma_seprated_ids = implode( ',', array_unique($dependent_question_ids) ); - $qpages_array = array(); - if ( ! empty($comma_seprated_ids) ) { - global $wpdb, $mlwQuizMasterNext; - $quiz_results = $wpdb->get_results( "SELECT `quiz_id`, `question_id` FROM `{$wpdb->prefix}mlw_questions` WHERE `question_id` IN (" .$comma_seprated_ids. ")" ); - if ( ! empty($quiz_results) ) { - foreach ( $quiz_results as $single_quiz ) { - $quiz_id = $single_quiz->quiz_id; - $mlwQuizMasterNext->pluginHelper->prepare_quiz( $quiz_id ); - $pages = $mlwQuizMasterNext->pluginHelper->get_quiz_setting( 'pages', array() ); - $clone_qpages = $qpages = $mlwQuizMasterNext->pluginHelper->get_quiz_setting( 'qpages', array() ); - if ( ! empty($clone_qpages) ) { - foreach ( $clone_qpages as $clonekey => $clonevalue ) { - if ( ! empty($clonevalue['questions']) && in_array($single_quiz->question_id, $clonevalue['questions']) ) { - $clone_qpages[ $clonekey ]['questions'] = array_diff($clonevalue['questions'], [ $single_quiz->question_id ]); - $pages[ $clonekey ] = array_diff($pages[ $clonekey ], [ $single_quiz->question_id ]); - } - } - $qpages = $clone_qpages; - } - //merge duplicate questions - $all_questions = array(); - foreach ( $pages as $page_key => $questions ) { - $page_questions = array(); - $questions = array_unique( $questions ); - foreach ( $questions as $id ) { - if ( ! in_array( $id, $all_questions, true ) ) { - $page_questions[] = $id; - } - } - $all_questions = array_merge( $all_questions, $questions ); - $pages[ $page_key ] = $page_questions; - if ( isset( $qpages[ $page_key ] ) ) { - $qpages[ $page_key ]['questions'] = $page_questions; - } - } - $qpages_array[ $quiz_id ] = array( - 'pages' => $pages, - 'qpages' => $qpages, - ); - } - } - } - return $qpages_array; -} - -/** - * Get Unique Linked Question IDs - * - * @param array $question_ids An array of question IDs to query for linked questions. - * @return array $unique_ids An array of unique question IDs extracted from the linked questions. - * @since 9.1.3 - */ -function qsm_get_unique_linked_question_ids_to_remove( $question_ids ) { - global $wpdb; - $all_ids = array(); - foreach ( $question_ids as $id ) { - $sql = $wpdb->prepare( - "SELECT linked_question FROM {$wpdb->prefix}mlw_questions WHERE question_id = %d", - $id - ); - $linked_question = $wpdb->get_var($sql); - if ( ! empty($linked_question) ) { - $all_ids = array_merge($all_ids, explode(',', $linked_question)); - } - } - return $all_ids; -} - add_action( 'wp_ajax_save_new_category', 'qsm_save_new_category' ); function qsm_save_new_category() { $category = isset( $_POST['name'] ) ? sanitize_text_field( wp_unslash( $_POST['name'] ) ) : ''; @@ -1441,10 +1240,7 @@ function qsm_options_questions_tab_template() {

{{{data.question}}}

Quiz Name: {{data.quiz_name}} <# if ( data.category != '' ) { #> Category: {{data.category}} <# } #>

-
- - -
+
diff --git a/php/classes/class-qsm-install.php b/php/classes/class-qsm-install.php index 7abd66c0..134d91bc 100644 --- a/php/classes/class-qsm-install.php +++ b/php/classes/class-qsm-install.php @@ -2038,15 +2038,6 @@ public function update() { } update_option( 'mlw_quiz_master_version', $data ); - - // Update 9.1.3 - $mlw_questions_table = $wpdb->prefix . 'mlw_questions'; - if ( 'linked_question' != $wpdb->get_var( "SHOW COLUMNS FROM $mlw_questions_table LIKE 'linked_question'" ) ) { - $sql = 'ALTER TABLE ' . $mlw_questions_table . ' ADD linked_question TEXT NULL DEFAULT \'\' AFTER category'; - $results = $mlwQuizMasterNext->wpdb_alter_table_query( $sql ); - $update_sql = 'UPDATE ' . $mlw_questions_table . ' SET linked_question = \'\' WHERE linked_question IS NULL'; - $results = $mlwQuizMasterNext->wpdb_alter_table_query( $update_sql ); - } } if ( ! get_option( 'mlw_advert_shows' ) ) { add_option( 'mlw_advert_shows', 'true' ); diff --git a/php/classes/class-qsm-questions.php b/php/classes/class-qsm-questions.php index a0cabf38..52f6441e 100644 --- a/php/classes/class-qsm-questions.php +++ b/php/classes/class-qsm-questions.php @@ -270,7 +270,6 @@ private static function create_save_question( $data, $answers, $settings, $is_cr 'order' => 1, 'category' => '', 'multicategories' => '', - 'linked_question' => '', ); $data = wp_parse_args( $data, $defaults ); @@ -298,14 +297,6 @@ private static function create_save_question( $data, $answers, $settings, $is_cr if ( $trim_question_description ) { $question_name = trim( preg_replace( '/\s+/', ' ', $question_name ) ); } - $linked_question = sanitize_text_field( $data['linked_question'] ); - if ( $is_creating && isset($data['is_linking']) ) { - if ( 1 <= $data['is_linking'] ) { - $linked_question = $data['is_linking']; - }elseif ( 0 == $data['is_linking'] ) { - $linked_question = ''; - } - } $values = array( 'quiz_id' => intval( $data['quiz_id'] ), @@ -318,11 +309,10 @@ private static function create_save_question( $data, $answers, $settings, $is_cr 'question_type_new' => sanitize_text_field( $data['type'] ), 'question_settings' => maybe_serialize( $settings ), 'category' => sanitize_text_field( $data['category'] ), - 'linked_question' => $linked_question, 'deleted' => 0, ); $values = apply_filters( 'qsm_save_question_data', $values ); - + $types = array( '%d', '%s', @@ -334,7 +324,6 @@ private static function create_save_question( $data, $answers, $settings, $is_cr '%s', '%s', '%s', - '%s', '%d', ); @@ -362,74 +351,44 @@ private static function create_save_question( $data, $answers, $settings, $is_cr throw new Exception( $msg ); } - $base_question_id = $question_id; - $quiz_questions_array = array(); - $quiz_questions_array[ intval( $data['quiz_id'] ) ] = $question_id; - $linked_questions_array[] = $question_id; - if ( isset($linked_question) && "" != $linked_question ) { - $expolded_question_array = explode(',', $linked_question); - $linked_questions_array = array_merge($expolded_question_array, $linked_questions_array); - // preparing array for quiz question id - $imploded_question_ids = implode( ',', array_unique($linked_questions_array) ); - if ( ! empty($linked_questions_array) ) { - $quiz_results = $wpdb->get_results( "SELECT `quiz_id`, `question_id` FROM `{$wpdb->prefix}mlw_questions` WHERE `question_id` IN (" . $imploded_question_ids . ")" ); - foreach ( $quiz_results as $key => $value ) { - $quiz_questions_array[ $value->quiz_id ] = $value->question_id; - } - } - $values['linked_question'] = $imploded_question_ids; - } + /** + * Process Question Categories + */ $question_terms_table = $wpdb->prefix . 'mlw_question_terms'; - foreach ( $quiz_questions_array as $quiz_id => $question_id_loop ) { - $values['quiz_id'] = intval( $quiz_id ); - $wpdb->update( - $wpdb->prefix . 'mlw_questions', - $values, - array( 'question_id' => intval($question_id_loop) ), - $types, - array( '%d' ) - ); - - /** - * Process Question Categories - */ - - $wpdb->delete( - $question_terms_table, - array( - 'question_id' => $question_id_loop, + $wpdb->delete( + $question_terms_table, + array( + 'question_id' => $question_id, + 'taxonomy' => 'qsm_category', + ) + ); + if ( ! empty( $data['multicategories'] ) ) { + foreach ( $data['multicategories'] as $term_id ) { + $term_rel_data = array( + 'question_id' => $question_id, + 'quiz_id' => intval( $data['quiz_id'] ), + 'term_id' => $term_id, 'taxonomy' => 'qsm_category', - ) - ); - if ( ! empty( $data['multicategories'] ) ) { - foreach ( $data['multicategories'] as $term_id ) { - $term_rel_data = array( - 'question_id' => $question_id_loop, - 'quiz_id' => intval( $quiz_id ), - 'term_id' => $term_id, - 'taxonomy' => 'qsm_category', - ); - // Check if the data already exists in the table - $data_exists = $wpdb->get_row($wpdb->prepare("SELECT * FROM $question_terms_table WHERE question_id = %s AND quiz_id = %s AND term_id = %s AND taxonomy = %s", $question_id_loop, intval( $quiz_id ), $term_id, 'qsm_category' )); - if ( ! $data_exists ) { - $wpdb->insert( $question_terms_table, $term_rel_data ); - } + ); + // Check if the data already exists in the table + $data_exists = $wpdb->get_row($wpdb->prepare("SELECT * FROM $question_terms_table WHERE question_id = %s AND quiz_id = %s AND term_id = %s AND taxonomy = %s", $question_id, intval( $data['quiz_id'] ), $term_id, 'qsm_category' )); + if ( ! $data_exists ) { + $wpdb->insert( $question_terms_table, $term_rel_data ); } } + } - /** - * Hook after saving question - */ - - if ( $is_creating && $base_question_id == $question_id_loop ) { - do_action( 'qsm_question_added', $question_id_loop, $values ); - } else { - do_action( 'qsm_question_updated', $question_id_loop, $values ); - } - do_action( 'qsm_saved_question', $question_id_loop, $values ); - + /** + * Hook after saving question + */ + if ( $is_creating ) { + do_action( 'qsm_question_added', $question_id, $values ); + } else { + do_action( 'qsm_question_updated', $question_id, $values ); } - return $base_question_id; + do_action( 'qsm_saved_question', $question_id, $values ); + + return $question_id; } /** diff --git a/php/rest-api.php b/php/rest-api.php index 4970dda8..53f1bd93 100644 --- a/php/rest-api.php +++ b/php/rest-api.php @@ -271,7 +271,6 @@ function qsm_rest_get_bank_questions( WP_REST_Request $request ) { 'file_upload_type' => isset( $question['settings']['file_upload_type'] ) ? $question['settings']['file_upload_type'] : '', 'quiz_name' => isset( $quiz_name['quiz_name'] ) ? $quiz_name['quiz_name'] : '', 'question_title' => isset( $question['settings']['question_title'] ) ? $question['settings']['question_title'] : '', - 'linked_question' => array_filter( isset( $question['linked_question'] ) ? explode(',', $question['linked_question']) : array() ), ); $question_data = apply_filters( 'qsm_rest_api_filter_question_data', $question_data, $question, $request ); $question_array['questions'][] = $question_data; @@ -488,34 +487,11 @@ function qsm_rest_save_results( WP_REST_Request $request ) { function qsm_rest_get_question( WP_REST_Request $request ) { // Makes sure user is logged in. if ( is_user_logged_in() ) { - global $wpdb; $current_user = wp_get_current_user(); if ( 0 !== $current_user ) { $question = QSM_Questions::load_question( $request['id'] ); $categorysArray = QSM_Questions::get_question_categories( $question['question_id'] ); if ( ! empty( $question ) ) { - $is_linking = $request['is_linking']; - $comma_seprated_ids = ''; - if ( 1 <= $is_linking ) { - if ( isset( $question['linked_question'] ) && '' == $question['linked_question'] ) { - $comma_seprated_ids = $is_linking; - } else { - $linked_question = isset($question['linked_question']) ? $question['linked_question'] : ''; - $exploded_question_array = explode(',', $linked_question); - $expolded_question_array[] = $is_linking; - $comma_seprated_ids = implode( ',', array_unique($expolded_question_array) ); - } - } - - $quiz_name_by_question = array(); - if ( ! empty($comma_seprated_ids) ) { - $quiz_results = $wpdb->get_results( "SELECT `quiz_id`, `question_id` FROM `{$wpdb->prefix}mlw_questions` WHERE `question_id` IN (" .$comma_seprated_ids. ")" ); - foreach ( $quiz_results as $value ) { - $quiz_name_in_loop = $wpdb->get_row( $wpdb->prepare( "SELECT quiz_name FROM {$wpdb->prefix}mlw_quizzes WHERE quiz_id = %d", $value->quiz_id ), ARRAY_A ); - $quiz_name_in_loop = isset( $quiz_name_in_loop['quiz_name'] ) ? $quiz_name_in_loop['quiz_name'] : ''; - $quiz_name_by_question[] = $quiz_name_in_loop; - } - } $question['page'] = isset( $question['page'] ) ? $question['page'] : 0; $question = array( 'id' => $question['question_id'], @@ -532,8 +508,6 @@ function qsm_rest_get_question( WP_REST_Request $request ) { 'answers' => $question['answers'], 'page' => $question['page'], 'question_title' => isset( $question['settings']['question_title'] ) ? $question['settings']['question_title'] : '', - 'link_quizzes' => $quiz_name_by_question, - 'merged_question' => $comma_seprated_ids, ); } return $question; @@ -564,31 +538,12 @@ function qsm_rest_get_questions( WP_REST_Request $request ) { $questions = QSM_Questions::load_questions( 0 ); } global $wpdb; - $stored_quiz_names = $procesed_question_ids = $question_array = array(); + $question_array = array(); foreach ( $questions as $question ) { $quiz_name = $wpdb->get_row( $wpdb->prepare( "SELECT quiz_name FROM {$wpdb->prefix}mlw_quizzes WHERE quiz_id = %d", $question['quiz_id'] ), ARRAY_A ); $question['page'] = isset( $question['page'] ) ? $question['page'] : 0; $categorysArray = QSM_Questions::get_question_categories( $question['question_id'] ); - $quiz_name = isset( $quiz_name['quiz_name'] ) ? $quiz_name['quiz_name'] : ''; - $quiz_name_by_question = array(); - $procesed_question_ids[] = $question['question_id']; - $stored_quiz_names[ $question['question_id'] ] = $quiz_name; - $linked_question_ids = array_filter( isset( $question['linked_question'] ) ? explode(',', $question['linked_question']) : array() ); - if ( ! empty($linked_question_ids) ) { - $quiz_results = $wpdb->get_results( "SELECT `quiz_id`, `question_id` FROM `{$wpdb->prefix}mlw_questions` WHERE `question_id` IN (" . implode( ',', $linked_question_ids ) . ")" ); - foreach ( $quiz_results as $value ) { - if ( ! in_array($value->question_id, $procesed_question_ids) ) { - $quiz_name_in_loop = $wpdb->get_row( $wpdb->prepare( "SELECT quiz_name FROM {$wpdb->prefix}mlw_quizzes WHERE quiz_id = %d", $value->quiz_id ), ARRAY_A ); - $quiz_name_in_loop = isset( $quiz_name_in_loop['quiz_name'] ) ? $quiz_name_in_loop['quiz_name'] : ''; - $quiz_name_by_question[] = $quiz_name_in_loop; - $procesed_question_ids[] = $value->question_id; - $stored_quiz_names[ $value->question_id ] = $quiz_name_in_loop; - } else { - $quiz_name_by_question[] = $stored_quiz_names[ $value->question_id ]; - } - } - } - $quiz_name_by_question = array_diff($quiz_name_by_question, [ $quiz_name ]); // remove current quiz id from the list + $question_data = array( 'id' => $question['question_id'], 'quizID' => $question['quiz_id'], @@ -611,13 +566,11 @@ function qsm_rest_get_questions( WP_REST_Request $request ) { 'limit_multiple_response' => isset( $question['settings']['limit_multiple_response'] ) ? $question['settings']['limit_multiple_response'] : 0, 'file_upload_limit' => isset( $question['settings']['file_upload_limit'] ) ? $question['settings']['file_upload_limit'] : 0, 'file_upload_type' => isset( $question['settings']['file_upload_type'] ) ? $question['settings']['file_upload_type'] : '', - 'quiz_name' => $quiz_name, + 'quiz_name' => isset( $quiz_name['quiz_name'] ) ? $quiz_name['quiz_name'] : '', 'question_title' => isset( $question['settings']['question_title'] ) ? $question['settings']['question_title'] : '', 'featureImageID' => isset( $question['settings']['featureImageID'] ) ? $question['settings']['featureImageID'] : '', 'featureImageSrc' => isset( $question['settings']['featureImageSrc'] ) ? $question['settings']['featureImageSrc'] : '', 'settings' => $question['settings'], - 'link_quizzes' => $quiz_name_by_question, - 'merged_question' => implode(",", $linked_question_ids), ); $question_data = apply_filters( 'qsm_rest_api_filter_question_data', $question_data, $question, $request ); $question_array[] = $question_data; @@ -655,8 +608,6 @@ function qsm_rest_create_question( WP_REST_Request $request ) { 'order' => 1, 'category' => $request['category'], 'multicategories' => $request['multicategories'], - 'linked_question' => $request['merged_question'], - 'is_linking' => $request['is_linking'], ); $settings = array( 'required' => $request['required'], @@ -720,7 +671,6 @@ function qsm_rest_save_question( WP_REST_Request $request ) { 'order' => 1, 'category' => $request['category'], 'multicategories' => $request['multicategories'], - 'linked_question' => $request['merged_question'], ); $settings = array(); $settings['answerEditor'] = $request['answerEditor'];