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