Created
July 12, 2021 11:07
-
-
Save unaibamir/227c7041ce0b66af6d330f997850028c to your computer and use it in GitHub Desktop.
JS add negative points field on quiz builder question field and get value via AJAX.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function admin_footer_script_tes() { | |
?> | |
<script type="text/javascript"> | |
jQuery(function($){ | |
$(document).on( 'click', 'button.ld-node-header__points-form-toggle.ld-button-reset', function( e ){ | |
$(".ld-node-header__points-form .ld-question-points") | |
.before( '<input type="number" min="0" name="points_neg" class="ld-question-points points_neg" value="1"> Negative Points' ); | |
}); | |
$(document).on( 'click', 'form.ld-node-header__points-form .is-button.is-primary', function( e ){ | |
var neg_points = $(this).parent().find('.ld-question-points.points_neg').val(); | |
var data_node = $(this).parents().find('.ld__builder--content').find('div[data-rbd-draggable-id]'); | |
// this contains question object. below is an example | |
// {"points":1,"tipMsg":"","tipEnabled":false,"edit_link":"http://localhost/office/addons/learndash/wp-admin/post.php?post=10&action=edit&quiz_id=8","question_type":"laq_calculated_formula","answers":{"sort_answer":[{"points":0,"gradedType":null,"gradingProgression":"not-graded-none","correct":false,"html":false,"graded":false,"sortString":"","type":"answer","sortStringHtml":false,"answer":""}],"classic_answer":[{"points":0,"gradedType":null,"gradingProgression":"not-graded-none","correct":false,"html":false,"graded":false,"sortString":"","type":"answer","sortStringHtml":false,"answer":""}],"matrix_sort_answer":[{"points":0,"gradedType":null,"gradingProgression":"not-graded-none","correct":false,"html":false,"graded":false,"sortString":"","type":"answer","sortStringHtml":false,"answer":""}],"cloze_answer":[{"points":0,"gradedType":null,"gradingProgression":"not-graded-none","correct":false,"html":false,"graded":false,"sortString":"","type":"answer","sortStringHtml":false,"answer":""}],"free_answer":[{"points":0,"gradedType":null,"gradingProgression":"not-graded-none","correct":false,"html":false,"graded":false,"sortString":"","type":"answer","sortStringHtml":false,"answer":""}],"assessment_answer":[{"points":0,"gradedType":null,"gradingProgression":"not-graded-none","correct":false,"html":false,"graded":false,"sortString":"","type":"answer","sortStringHtml":false,"answer":""}],"essay":[{"points":0,"gradedType":null,"gradingProgression":"not-graded-none","correct":false,"html":false,"graded":false,"sortString":"","type":"answer","sortStringHtml":false,"answer":""}]},"correctMsg":"correct","expanded":true,"post_title":"Test Question","type":"sfwd-question","post_content":"asd asdasdasd","ID":10,"incorrectMsg":"incorrect","correctSameText":false} | |
var question_data = data_node.attr('data-rbd-draggable-id'); | |
var data ={ | |
'neg_points' : neg_points, | |
'question_data' : question_data, | |
'action' : "my_action" | |
}; | |
$.post(ajaxurl, data, function(response) { | |
console.log(response); | |
}); | |
}); | |
}); | |
</script> | |
<?php | |
} | |
add_action( 'admin_footer', 'admin_footer_script_tes' ); | |
add_action( 'wp_ajax_my_action', 'my_action' ); | |
function my_action() { | |
echo 'here: '. $_POST['neg_points']; | |
wp_die(); // this is required to terminate immediately and return a proper response | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment