gutenberg_blocks.js
7 years ago
mce-button.js
7 years ago
tutor-admin.js
7 years ago
tutor-front.js
7 years ago
tutor-admin.js
1651 lines
| 1 | jQuery(document).ready(function($){ |
| 2 | 'use strict'; |
| 3 | |
| 4 | if (jQuery().select2){ |
| 5 | $('.tutor_select2').select2(); |
| 6 | } |
| 7 | |
| 8 | /** |
| 9 | * Option Settings Nav Tab |
| 10 | */ |
| 11 | $('.tutor-option-nav-tabs li a').click(function(e){ |
| 12 | e.preventDefault(); |
| 13 | var tab_page_id = $(this).attr('href'); |
| 14 | $('.option-nav-item').removeClass('current'); |
| 15 | $(this).closest('li').addClass('current'); |
| 16 | $('.tutor-option-nav-page').hide(); |
| 17 | $(tab_page_id).addClass('current-page').show(); |
| 18 | }); |
| 19 | |
| 20 | $('#save_tutor_option').click(function (e) { |
| 21 | e.preventDefault(); |
| 22 | $(this).closest('form').submit(); |
| 23 | }); |
| 24 | $('#tutor-option-form').submit(function(e){ |
| 25 | e.preventDefault(); |
| 26 | |
| 27 | var $form = $(this); |
| 28 | var data = $form.serialize(); |
| 29 | |
| 30 | $.ajax({ |
| 31 | url : ajaxurl, |
| 32 | type : 'POST', |
| 33 | data : data, |
| 34 | beforeSend: function () { |
| 35 | $form.find('.button').addClass('tutor-updating-message'); |
| 36 | }, |
| 37 | success: function (data) { |
| 38 | // |
| 39 | }, |
| 40 | complete: function () { |
| 41 | $form.find('.button').removeClass('tutor-updating-message'); |
| 42 | window.location.reload(); |
| 43 | } |
| 44 | }); |
| 45 | }); |
| 46 | |
| 47 | function tutor_slider_init(){ |
| 48 | $('.tutor-field-slider').each(function(){ |
| 49 | var $slider = $(this); |
| 50 | var $input = $slider.closest('.tutor-field-type-slider').find('input[type="hidden"]'); |
| 51 | var $showVal = $slider.closest('.tutor-field-type-slider').find('.tutor-field-type-slider-value'); |
| 52 | var min = parseFloat($slider.closest('.tutor-field-type-slider').attr('data-min')); |
| 53 | var max = parseFloat($slider.closest('.tutor-field-type-slider').attr('data-max')); |
| 54 | |
| 55 | $slider.slider({ |
| 56 | range: "max", |
| 57 | min: min, |
| 58 | max: max, |
| 59 | value: $input.val(), |
| 60 | slide: function( event, ui ) { |
| 61 | $showVal.text(ui.value); |
| 62 | $input.val(ui.value); |
| 63 | } |
| 64 | }); |
| 65 | }); |
| 66 | } |
| 67 | |
| 68 | tutor_slider_init(); |
| 69 | |
| 70 | /** |
| 71 | * Course and lesson sorting |
| 72 | */ |
| 73 | function enable_sorting_topic_lesson(){ |
| 74 | if (jQuery().sortable) { |
| 75 | $(".course-contents").sortable({ |
| 76 | handle: ".course-move-handle", |
| 77 | start: function (e, ui) { |
| 78 | ui.placeholder.css('visibility', 'visible'); |
| 79 | }, |
| 80 | stop: function (e, ui) { |
| 81 | tutor_sorting_topics_and_lesson(); |
| 82 | }, |
| 83 | }); |
| 84 | $(".tutor-lessons:not(.drop-lessons)").sortable({ |
| 85 | connectWith: ".tutor-lessons", |
| 86 | items: "div.course-content-item", |
| 87 | start: function (e, ui) { |
| 88 | ui.placeholder.css('visibility', 'visible'); |
| 89 | }, |
| 90 | stop: function (e, ui) { |
| 91 | tutor_sorting_topics_and_lesson(); |
| 92 | }, |
| 93 | }); |
| 94 | } |
| 95 | } |
| 96 | enable_sorting_topic_lesson(); |
| 97 | function tutor_sorting_topics_and_lesson(){ |
| 98 | var topics = {}; |
| 99 | $('.tutor-topics-wrap').each(function(index, item){ |
| 100 | var $topic = $(this); |
| 101 | var topics_id = parseInt($topic.attr('id').match(/\d+/)[0], 10); |
| 102 | var lessons = {}; |
| 103 | |
| 104 | $topic.find('.course-content-item').each(function(lessonIndex, lessonItem){ |
| 105 | var $lesson = $(this); |
| 106 | var lesson_id = parseInt($lesson.attr('id').match(/\d+/)[0], 10); |
| 107 | |
| 108 | lessons[lessonIndex] = lesson_id; |
| 109 | }); |
| 110 | topics[index] = { 'topic_id' : topics_id, 'lesson_ids' : lessons }; |
| 111 | }); |
| 112 | $('#tutor_topics_lessons_sorting').val(JSON.stringify(topics)); |
| 113 | } |
| 114 | |
| 115 | $(document).on('click', '.create_new_topic_btn', function (e) { |
| 116 | e.preventDefault(); |
| 117 | $('.tutor-metabox-add-topics').slideToggle(); |
| 118 | }); |
| 119 | |
| 120 | $(document).on('click', '#tutor-add-topic-btn', function (e) { |
| 121 | e.preventDefault(); |
| 122 | var $that = $(this); |
| 123 | var form_data = $that.closest('.tutor-metabox-add-topics').find('input, textarea').serialize()+'&action=tutor_add_course_topic'; |
| 124 | |
| 125 | $.ajax({ |
| 126 | url : ajaxurl, |
| 127 | type : 'POST', |
| 128 | data : form_data, |
| 129 | beforeSend: function () { |
| 130 | $that.addClass('tutor-updating-message'); |
| 131 | }, |
| 132 | success: function (data) { |
| 133 | if (data.success){ |
| 134 | $('#tutor-course-content-wrap').html(data.data.course_contents); |
| 135 | $that.closest('.tutor-metabox-add-topics').find('input[type!="hidden"], textarea').each(function () { |
| 136 | $(this).val(''); |
| 137 | }); |
| 138 | $that.closest('.tutor-metabox-add-topics').slideUp(); |
| 139 | enable_sorting_topic_lesson(); |
| 140 | } |
| 141 | }, |
| 142 | complete: function () { |
| 143 | $that.removeClass('tutor-updating-message'); |
| 144 | } |
| 145 | }); |
| 146 | }); |
| 147 | |
| 148 | $(document).on('change keyup', '.course-edit-topic-title-input', function (e) { |
| 149 | e.preventDefault(); |
| 150 | $(this).closest('.tutor-topics-top').find('.topic-inner-title').html($(this).val()); |
| 151 | }); |
| 152 | |
| 153 | $(document).on('click', '.topic-edit-icon', function (e) { |
| 154 | e.preventDefault(); |
| 155 | $(this).closest('.tutor-topics-top').find('.tutor-topics-edit-form').slideToggle(); |
| 156 | }); |
| 157 | |
| 158 | $(document).on('click', '.tutor-topics-edit-button', function(e){ |
| 159 | e.preventDefault(); |
| 160 | var $button = $(this); |
| 161 | var $topic = $button.closest('.tutor-topics-wrap'); |
| 162 | var topics_id = parseInt($topic.attr('id').match(/\d+/)[0], 10); |
| 163 | var topic_title = $button.closest('.tutor-topics-wrap').find('[name="topic_title"]').val(); |
| 164 | var topic_summery = $button.closest('.tutor-topics-wrap').find('[name="topic_summery"]').val(); |
| 165 | |
| 166 | var data = {topic_title: topic_title, topic_summery : topic_summery, topic_id : topics_id, action: 'tutor_update_topic'}; |
| 167 | $.ajax({ |
| 168 | url : ajaxurl, |
| 169 | type : 'POST', |
| 170 | data : data, |
| 171 | beforeSend: function () { |
| 172 | $button.addClass('tutor-updating-message'); |
| 173 | }, |
| 174 | success: function (data) { |
| 175 | if (data.success){ |
| 176 | $button.closest('.tutor-topics-wrap').find('span.topic-inner-title').text(topic_title); |
| 177 | $button.closest('.tutor-topics-wrap').find('.tutor-topics-edit-form').slideUp(); |
| 178 | } |
| 179 | }, |
| 180 | complete: function () { |
| 181 | $button.removeClass('tutor-updating-message'); |
| 182 | } |
| 183 | }); |
| 184 | }); |
| 185 | |
| 186 | /** |
| 187 | * Confirmation for deleting Topic |
| 188 | */ |
| 189 | $(document).on('click', '.topic-delete-btn a', function(e){ |
| 190 | var topic_id = $(this).attr('data-topic-id'); |
| 191 | |
| 192 | if ( ! confirm('Are you sure to delete?')){ |
| 193 | e.preventDefault(); |
| 194 | } |
| 195 | }); |
| 196 | |
| 197 | $(document).on('click', '.tutor-expand-all-topic', function (e) { |
| 198 | e.preventDefault(); |
| 199 | $('.tutor-topics-body').slideDown(); |
| 200 | $('.expand-collapse-wrap i').removeClass('tutor-icon-light-down').addClass('tutor-icon-light-up'); |
| 201 | }); |
| 202 | $(document).on('click', '.tutor-collapse-all-topic', function (e) { |
| 203 | e.preventDefault(); |
| 204 | $('.tutor-topics-body').slideUp(); |
| 205 | $('.expand-collapse-wrap i').removeClass('tutor-icon-light-up').addClass('tutor-icon-light-down'); |
| 206 | }); |
| 207 | $(document).on('click', '.topic-inner-title, .expand-collapse-wrap', function (e) { |
| 208 | e.preventDefault(); |
| 209 | var $that = $(this); |
| 210 | $that.closest('.tutor-topics-wrap').find('.tutor-topics-body').slideToggle(); |
| 211 | $that.closest('.tutor-topics-wrap').find('.expand-collapse-wrap i').toggleClass('tutor-icon-light-down tutor-icon-light-up'); |
| 212 | }); |
| 213 | |
| 214 | /** |
| 215 | * Update Lesson Modal |
| 216 | */ |
| 217 | $(document).on('click', '.open-tutor-lesson-modal', function(e){ |
| 218 | e.preventDefault(); |
| 219 | |
| 220 | var $that = $(this); |
| 221 | var lesson_id = $that.attr('data-lesson-id'); |
| 222 | var topic_id = $that.attr('data-topic-id'); |
| 223 | var course_id = $('#post_ID').val(); |
| 224 | |
| 225 | $.ajax({ |
| 226 | url : ajaxurl, |
| 227 | type : 'POST', |
| 228 | data : {lesson_id : lesson_id, topic_id : topic_id, course_id : course_id, action: 'tutor_load_edit_lesson_modal'}, |
| 229 | beforeSend: function () { |
| 230 | $that.addClass('tutor-updating-message'); |
| 231 | }, |
| 232 | success: function (data) { |
| 233 | $('.tutor-lesson-modal-wrap .modal-container').html(data.data.output); |
| 234 | $('.tutor-lesson-modal-wrap').attr({'data-lesson-id' : lesson_id, 'data-topic-id':topic_id}).addClass('show'); |
| 235 | |
| 236 | tinymce.init(tinyMCEPreInit.mceInit.content); |
| 237 | tinymce.execCommand( 'mceRemoveEditor', false, 'tutor_lesson_modal_editor' ); |
| 238 | tinyMCE.execCommand('mceAddEditor', false, "tutor_lesson_modal_editor"); |
| 239 | }, |
| 240 | complete: function () { |
| 241 | quicktags({id : "tutor_lesson_modal_editor"}); |
| 242 | $that.removeClass('tutor-updating-message'); |
| 243 | } |
| 244 | }); |
| 245 | }); |
| 246 | |
| 247 | $(document).on( 'click', '.lesson_thumbnail_upload_btn', function( event ){ |
| 248 | event.preventDefault(); |
| 249 | var $that = $(this); |
| 250 | var frame; |
| 251 | if ( frame ) { |
| 252 | frame.open(); |
| 253 | return; |
| 254 | } |
| 255 | frame = wp.media({ |
| 256 | title: 'Select or Upload Media Of Your Chosen Persuasion', |
| 257 | button: { |
| 258 | text: 'Use this media' |
| 259 | }, |
| 260 | multiple: false |
| 261 | }); |
| 262 | frame.on( 'select', function() { |
| 263 | var attachment = frame.state().get('selection').first().toJSON(); |
| 264 | $that.closest('.tutor-thumbnail-wrap').find('.thumbnail-img').html('<img src="'+attachment.url+'" alt="" />'); |
| 265 | $that.closest('.tutor-thumbnail-wrap').find('input').val(attachment.id); |
| 266 | }); |
| 267 | frame.open(); |
| 268 | }); |
| 269 | |
| 270 | /** |
| 271 | * Delete Lesson from course builder |
| 272 | */ |
| 273 | $(document).on('click', '.tutor-delete-lesson-btn', function(e){ |
| 274 | e.preventDefault(); |
| 275 | |
| 276 | if( ! confirm('Are you sure?')){ |
| 277 | return; |
| 278 | } |
| 279 | |
| 280 | var $that = $(this); |
| 281 | var lesson_id = $that.attr('data-lesson-id'); |
| 282 | |
| 283 | $.ajax({ |
| 284 | url : ajaxurl, |
| 285 | type : 'POST', |
| 286 | data : {lesson_id : lesson_id, action: 'tutor_delete_lesson_by_id'}, |
| 287 | beforeSend: function () { |
| 288 | $that.addClass('tutor-updating-message'); |
| 289 | }, |
| 290 | success: function (data) { |
| 291 | if (data.success){ |
| 292 | $that.closest('.tutor-lesson').remove(); |
| 293 | } |
| 294 | }, |
| 295 | complete: function () { |
| 296 | $that.removeClass('tutor-updating-message'); |
| 297 | } |
| 298 | }); |
| 299 | }); |
| 300 | |
| 301 | /** |
| 302 | * Delete quiz |
| 303 | */ |
| 304 | $(document).on('click', '.tutor-delete-quiz-btn', function(e){ |
| 305 | e.preventDefault(); |
| 306 | |
| 307 | if( ! confirm('Are you sure?')){ |
| 308 | return; |
| 309 | } |
| 310 | |
| 311 | var $that = $(this); |
| 312 | var quiz_id = $that.attr('data-quiz-id'); |
| 313 | |
| 314 | $.ajax({ |
| 315 | url : ajaxurl, |
| 316 | type : 'POST', |
| 317 | data : {quiz_id : quiz_id, action: 'tutor_delete_quiz_by_id'}, |
| 318 | beforeSend: function () { |
| 319 | $that.closest('.course-content-item').remove(); |
| 320 | } |
| 321 | }); |
| 322 | }); |
| 323 | |
| 324 | /** |
| 325 | * Lesson Update or Create Modal |
| 326 | */ |
| 327 | $(document).on( 'click', '.update_lesson_modal_btn', function( event ){ |
| 328 | event.preventDefault(); |
| 329 | |
| 330 | var $that = $(this); |
| 331 | var content; |
| 332 | var editor = tinyMCE.get('tutor_lesson_modal_editor'); |
| 333 | if (editor) { |
| 334 | content = editor.getContent(); |
| 335 | } else { |
| 336 | content = $('#'+inputid).val(); |
| 337 | } |
| 338 | |
| 339 | var form_data = $(this).closest('form').serialize(); |
| 340 | form_data += '&lesson_content='+content; |
| 341 | |
| 342 | $.ajax({ |
| 343 | url : ajaxurl, |
| 344 | type : 'POST', |
| 345 | data : form_data, |
| 346 | beforeSend: function () { |
| 347 | $that.addClass('tutor-updating-message'); |
| 348 | }, |
| 349 | success: function (data) { |
| 350 | if (data.success){ |
| 351 | $('#tutor-course-content-wrap').html(data.data.course_contents); |
| 352 | enable_sorting_topic_lesson(); |
| 353 | |
| 354 | //Close the modal |
| 355 | $('.tutor-lesson-modal-wrap').removeClass('show'); |
| 356 | } |
| 357 | }, |
| 358 | complete: function () { |
| 359 | $that.removeClass('tutor-updating-message'); |
| 360 | } |
| 361 | }); |
| 362 | }); |
| 363 | |
| 364 | /** |
| 365 | * Lesson Video |
| 366 | */ |
| 367 | $(document).on('change', '.tutor_lesson_video_source', function(e){ |
| 368 | var selector = $(this).val(); |
| 369 | $('[class^="video_source_wrap"]').hide(); |
| 370 | $('.video_source_wrap_'+selector).show(); |
| 371 | }); |
| 372 | |
| 373 | $(document).on( 'click', '.video_source_wrap_html5 .video_upload_btn', function( event ){ |
| 374 | event.preventDefault(); |
| 375 | |
| 376 | var $that = $(this); |
| 377 | var frame; |
| 378 | // If the media frame already exists, reopen it. |
| 379 | if ( frame ) { |
| 380 | frame.open(); |
| 381 | return; |
| 382 | } |
| 383 | |
| 384 | // Create a new media frame |
| 385 | frame = wp.media({ |
| 386 | title: 'Select or Upload Media Of Your Chosen Persuasion', |
| 387 | button: { |
| 388 | text: 'Use this media' |
| 389 | }, |
| 390 | multiple: false // Set to true to allow multiple files to be selected |
| 391 | }); |
| 392 | |
| 393 | // When an image is selected in the media frame... |
| 394 | frame.on( 'select', function() { |
| 395 | // Get media attachment details from the frame state |
| 396 | var attachment = frame.state().get('selection').first().toJSON(); |
| 397 | $that.closest('.video_source_wrap_html5').find('span.video_media_id').text(attachment.id).closest('p').show(); |
| 398 | $that.closest('.video_source_wrap_html5').find('input').val(attachment.id); |
| 399 | }); |
| 400 | // Finally, open the modal on click |
| 401 | frame.open(); |
| 402 | }); |
| 403 | |
| 404 | //tutor_video_poster_upload_btn |
| 405 | $(document).on( 'click', '.tutor_video_poster_upload_btn', function( event ){ |
| 406 | event.preventDefault(); |
| 407 | |
| 408 | var $that = $(this); |
| 409 | var frame; |
| 410 | // If the media frame already exists, reopen it. |
| 411 | if ( frame ) { |
| 412 | frame.open(); |
| 413 | return; |
| 414 | } |
| 415 | |
| 416 | // Create a new media frame |
| 417 | frame = wp.media({ |
| 418 | title: 'Select or Upload Media Of Your Chosen Persuasion', |
| 419 | button: { |
| 420 | text: 'Use this media' |
| 421 | }, |
| 422 | multiple: false // Set to true to allow multiple files to be selected |
| 423 | }); |
| 424 | |
| 425 | // When an image is selected in the media frame... |
| 426 | frame.on( 'select', function() { |
| 427 | // Get media attachment details from the frame state |
| 428 | var attachment = frame.state().get('selection').first().toJSON(); |
| 429 | $that.closest('.tutor-video-poster-wrap').find('.video-poster-img').html('<img src="'+attachment.url+'" alt="" />'); |
| 430 | $that.closest('.tutor-video-poster-wrap').find('input').val(attachment.id); |
| 431 | }); |
| 432 | // Finally, open the modal on click |
| 433 | frame.open(); |
| 434 | }); |
| 435 | |
| 436 | $(document).on('click', 'a.tutor-delete-attachment', function(e){ |
| 437 | e.preventDefault(); |
| 438 | $(this).closest('.tutor-added-attachment').remove(); |
| 439 | }); |
| 440 | |
| 441 | $(document).on('click', '.tutorUploadAttachmentBtn', function(e){ |
| 442 | e.preventDefault(); |
| 443 | |
| 444 | var $that = $(this); |
| 445 | var frame; |
| 446 | // If the media frame already exists, reopen it. |
| 447 | if ( frame ) { |
| 448 | frame.open(); |
| 449 | return; |
| 450 | } |
| 451 | // Create a new media frame |
| 452 | frame = wp.media({ |
| 453 | title: 'Select or Upload Media Of Your Chosen Persuasion', |
| 454 | button: { |
| 455 | text: 'Use this media' |
| 456 | }, |
| 457 | multiple: true // Set to true to allow multiple files to be selected |
| 458 | }); |
| 459 | // When an image is selected in the media frame... |
| 460 | frame.on( 'select', function() { |
| 461 | // Get media attachment details from the frame state |
| 462 | var attachments = frame.state().get('selection').toJSON(); |
| 463 | if (attachments.length){ |
| 464 | for (var i=0; i < attachments.length; i++){ |
| 465 | var attachment = attachments[i]; |
| 466 | |
| 467 | var inputHtml = '<div class="tutor-added-attachment"><p> <a href="javascript:;" class="tutor-delete-attachment">×</a> <span> <a href="'+attachment.url+'">'+attachment.filename+'</a> </span> </p><input type="hidden" name="tutor_attachments[]" value="'+attachment.id+'"></div>'; |
| 468 | $that.closest('.tutor-lesson-attachments-metabox').find('.tutor-added-attachments-wrap').append(inputHtml); |
| 469 | } |
| 470 | } |
| 471 | }); |
| 472 | // Finally, open the modal on click |
| 473 | frame.open(); |
| 474 | }); |
| 475 | |
| 476 | /** |
| 477 | * Open Sidebar Menu |
| 478 | */ |
| 479 | if (tutor_data.open_tutor_admin_menu){ |
| 480 | var $adminMenu = $('#adminmenu'); |
| 481 | $adminMenu.find('[href="admin.php?page=tutor"]').closest('li.wp-has-submenu').addClass('wp-has-current-submenu'); |
| 482 | $adminMenu.find('[href="admin.php?page=tutor"]').closest('li.wp-has-submenu').find('a.wp-has-submenu').removeClass('wp-has-current-submenu').addClass('wp-has-current-submenu'); |
| 483 | } |
| 484 | |
| 485 | /** |
| 486 | * Add question answer for quiz |
| 487 | */ |
| 488 | |
| 489 | $(document).on('change keyup paste', '.question_field_title', function(){ |
| 490 | var $that = $(this); |
| 491 | $that.closest('.single-question-item').find('.tutor-question-item-head').find('.question-title').text($that.val()); |
| 492 | }); |
| 493 | |
| 494 | $(document).on('change', '.question_type_field', function(){ |
| 495 | var $that = $(this); |
| 496 | var question_type = $that.val(); |
| 497 | |
| 498 | var option_text = $that.find('option[value="'+question_type+'"]').text(); |
| 499 | $that.closest('.single-question-item').find('.tutor-question-item-head').find('.question-type').text(option_text); |
| 500 | |
| 501 | var question_id = $that.closest('.single-question-item').attr('data-question-id'); |
| 502 | var data = {question_id: question_id, question_type : question_type, action: 'quiz_question_type_changed'}; |
| 503 | |
| 504 | $.ajax({ |
| 505 | url : ajaxurl, |
| 506 | type : 'POST', |
| 507 | data : data, |
| 508 | beforeSend: function () { |
| 509 | $that.closest('.single-question-item').find('.tutor-loading-icon-wrap').addClass('tutor-updating-message'); |
| 510 | }, |
| 511 | success: function (data) { |
| 512 | if (data.success){ |
| 513 | $that.closest('.quiz-question-form-wrap').find('.answer-entry-wrap').html(data.data.multi_answer_options); |
| 514 | |
| 515 | if (question_type === 'true_false' && $('.answer-option-row').length >= 2){ |
| 516 | $('.add_answer_option_wrap').hide(); |
| 517 | }else{ |
| 518 | $('.add_answer_option_wrap').show(); |
| 519 | } |
| 520 | } |
| 521 | }, |
| 522 | complete: function () { |
| 523 | $that.closest('.single-question-item').find('.tutor-loading-icon-wrap').removeClass('tutor-updating-message'); |
| 524 | } |
| 525 | }); |
| 526 | }); |
| 527 | |
| 528 | $(document).on('click', '.add_answer_option_btn', function(e){ |
| 529 | e.preventDefault(); |
| 530 | |
| 531 | var $that = $(this); |
| 532 | var question_id = $that.closest('.single-question-item').attr('data-question-id'); |
| 533 | var question_type = $that.closest('.quiz-question-form-wrap').find('select.question_type_field').val(); |
| 534 | var data = {question_id: question_id, action: 'quiz_add_answer_to_question'}; |
| 535 | |
| 536 | $.ajax({ |
| 537 | url : ajaxurl, |
| 538 | type : 'POST', |
| 539 | data : data, |
| 540 | beforeSend: function () { |
| 541 | $that.removeClass('updated-message').addClass('tutor-updating-message'); |
| 542 | }, |
| 543 | success: function (data) { |
| 544 | if (data.success){ |
| 545 | $that.closest('.answer-entry-wrap').find('table.multi-answers-options').append(data.data.data_tr); |
| 546 | |
| 547 | //Hide add answer button if true false and 2 option exists |
| 548 | if (question_type === 'true_false' && $that.closest('.answer-entry-wrap').find('tr.answer-option-row').length >= 2){ |
| 549 | $that.closest('.add_answer_option_wrap').hide(); |
| 550 | }else{ |
| 551 | $that.closest('.add_answer_option_wrap').show(); |
| 552 | } |
| 553 | } |
| 554 | }, |
| 555 | complete: function () { |
| 556 | $that.removeClass('tutor-updating-message').addClass('updated-message'); |
| 557 | } |
| 558 | }); |
| 559 | }); |
| 560 | |
| 561 | $(document).on('click', '.add_question_btn', function(e){ |
| 562 | e.preventDefault(); |
| 563 | |
| 564 | var $that = $(this); |
| 565 | var $title = $('[name="new_question_title"]'); |
| 566 | var question_title = $title.val(); |
| 567 | var question_type = $('[name="new_question_type"]').val(); |
| 568 | var quiz_id = $('#post_ID').val(); |
| 569 | |
| 570 | //If no question title, stop here |
| 571 | if ( ! question_title.length){ |
| 572 | $title.addClass('tutor-input-text-error'); |
| 573 | return; |
| 574 | }else{ |
| 575 | $title.removeClass('tutor-input-text-error'); |
| 576 | } |
| 577 | |
| 578 | var data = {question_title : question_title, question_type:question_type, quiz_id : quiz_id, action: 'quiz_page_add_new_question' }; |
| 579 | $.ajax({ |
| 580 | url : ajaxurl, |
| 581 | type : 'POST', |
| 582 | data : data, |
| 583 | beforeSend: function () { |
| 584 | $that.removeClass('updated-message').addClass('tutor-updating-message'); |
| 585 | }, |
| 586 | success: function (data) { |
| 587 | if (data.success){ |
| 588 | $('.single-question-item .quiz-question-form-wrap').hide(); |
| 589 | $('.tutor-quiz-questions-wrap').append(data.data.question_html); |
| 590 | $('.single-question-item:last-child .quiz-question-form-wrap').show(); |
| 591 | $title.val(''); |
| 592 | } |
| 593 | }, |
| 594 | complete: function () { |
| 595 | $that.removeClass('tutor-updating-message').addClass('updated-message'); |
| 596 | } |
| 597 | }); |
| 598 | }); |
| 599 | |
| 600 | //Show hide question settings |
| 601 | $(document).on('click', '.question-action-btn.down', function(e){ |
| 602 | e.preventDefault(); |
| 603 | $(this).closest('.single-question-item').find('.quiz-question-form-wrap').toggle(); |
| 604 | $(this).find('i.dashicons').toggleClass('dashicons-arrow-up-alt2 dashicons-arrow-down-alt2'); |
| 605 | }); |
| 606 | |
| 607 | $(document).on('change', '.single-question-item', function(e){ |
| 608 | e.preventDefault(); |
| 609 | |
| 610 | var $that = $(this); |
| 611 | var data = $(this).find("select, textarea, input").serialize()+'&action=update_tutor_question'; |
| 612 | $.ajax({ |
| 613 | url : ajaxurl, |
| 614 | type : 'POST', |
| 615 | data : data, |
| 616 | beforeSend: function () { |
| 617 | $that.find('.tutor-loading-icon-wrap').addClass('tutor-updating-message'); |
| 618 | }, |
| 619 | success: function (data) { |
| 620 | if (data.success){ |
| 621 | |
| 622 | } |
| 623 | }, |
| 624 | complete: function () { |
| 625 | $that.find('.tutor-loading-icon-wrap').removeClass('tutor-updating-message'); |
| 626 | } |
| 627 | }); |
| 628 | }); |
| 629 | |
| 630 | $(document).on('click', '.quiz-answer-option-delete-btn', function(e){ |
| 631 | e.preventDefault(); |
| 632 | var $that = $(this); |
| 633 | var $closestTable = $that.closest('table'); |
| 634 | var $loadingIcon = $that.closest('.single-question-item').find('.tutor-loading-icon-wrap'); |
| 635 | |
| 636 | var question_type = $that.closest('.quiz-question-form-wrap').find('select.question_type_field').val(); |
| 637 | var answer_option_id = $that.closest('tr').attr('data-answer-option-id'); |
| 638 | |
| 639 | $.ajax({ |
| 640 | url : ajaxurl, |
| 641 | type : 'POST', |
| 642 | data : {answer_option_id:answer_option_id, action: 'quiz_delete_answer_option'}, |
| 643 | beforeSend: function () { |
| 644 | $loadingIcon.addClass('tutor-updating-message'); |
| 645 | }, |
| 646 | success: function (data) { |
| 647 | if (data.success){ |
| 648 | $that.closest('tr').remove(); |
| 649 | //Hide add answer button if true false and 2 option exists |
| 650 | if (question_type === 'true_false' && $closestTable.find('tr.answer-option-row').length >= 2){ |
| 651 | $closestTable.closest('.answer-entry-wrap').find('.add_answer_option_wrap').hide(); |
| 652 | }else{ |
| 653 | $closestTable.closest('.answer-entry-wrap').find('.add_answer_option_wrap').show(); |
| 654 | } |
| 655 | } |
| 656 | }, |
| 657 | complete: function () { |
| 658 | $loadingIcon.removeClass('tutor-updating-message'); |
| 659 | } |
| 660 | }); |
| 661 | }); |
| 662 | |
| 663 | $(document).on('click', '.question-action-btn.trash', function(e){ |
| 664 | e.preventDefault(); |
| 665 | |
| 666 | var $that = $(this); |
| 667 | var question_id = $that.closest('.single-question-item').attr('data-question-id'); |
| 668 | var $loadingIcon = $that.closest('.single-question-item').find('.tutor-loading-icon-wrap'); |
| 669 | |
| 670 | $.ajax({ |
| 671 | url : ajaxurl, |
| 672 | type : 'POST', |
| 673 | data : {question_id:question_id, action: 'quiz_question_delete'}, |
| 674 | beforeSend: function () { |
| 675 | $loadingIcon.addClass('tutor-updating-message'); |
| 676 | }, |
| 677 | success: function (data) { |
| 678 | if (data.success){ |
| 679 | $that.closest('.single-question-item').remove(); |
| 680 | } |
| 681 | }, |
| 682 | complete: function () { |
| 683 | $loadingIcon.removeClass('tutor-updating-message'); |
| 684 | } |
| 685 | }); |
| 686 | }); |
| 687 | |
| 688 | /** |
| 689 | * Sort quiz questions |
| 690 | */ |
| 691 | |
| 692 | if (jQuery().sortable) { |
| 693 | $(".tutor-quiz-questions-wrap").sortable({ |
| 694 | handle: ".question-short", |
| 695 | start: function (e, ui) { |
| 696 | ui.placeholder.css('visibility', 'visible'); |
| 697 | }, |
| 698 | stop: function (e, ui) { |
| 699 | var questions = {}; |
| 700 | $('.single-question-item').each(function(index, item){ |
| 701 | var $question = $(this); |
| 702 | var question_id = parseInt($question.attr('data-question-id').match(/\d+/)[0], 10); |
| 703 | questions[index] = { 'question_id' : question_id }; |
| 704 | }); |
| 705 | |
| 706 | $.post(ajaxurl, {questions : questions, action: 'sorting_quiz_questions'}); |
| 707 | }, |
| 708 | }); |
| 709 | } |
| 710 | |
| 711 | /** |
| 712 | * Quiz Modal |
| 713 | */ |
| 714 | |
| 715 | $(document).on('click', '.modal-close-btn', function(e){ |
| 716 | e.preventDefault(); |
| 717 | $('.tutor-modal-wrap').removeClass('show'); |
| 718 | }); |
| 719 | $(document).on('keyup', function(e){ |
| 720 | if (e.keyCode === 27){ |
| 721 | $('.tutor-modal-wrap').removeClass('show'); |
| 722 | } |
| 723 | }); |
| 724 | |
| 725 | /* |
| 726 | $(document).on('click', '.tutor-add-quiz-btn', function(e){ |
| 727 | e.preventDefault(); |
| 728 | |
| 729 | var $that = $(this); |
| 730 | var quiz_for_post_id = $(this).closest('.tutor_add_quiz_wrap').attr('data-add-quiz-under'); |
| 731 | $.ajax({ |
| 732 | url : ajaxurl, |
| 733 | type : 'POST', |
| 734 | data : {quiz_for_post_id : quiz_for_post_id, action: 'tutor_load_quiz_modal'}, |
| 735 | beforeSend: function () { |
| 736 | $that.addClass('tutor-updating-message'); |
| 737 | }, |
| 738 | success: function (data) { |
| 739 | $('.tutor-quiz-modal-wrap .modal-container').html(data.data.output); |
| 740 | $('.tutor-quiz-modal-wrap').attr('quiz-for-post-id', quiz_for_post_id).addClass('show'); |
| 741 | }, |
| 742 | complete: function () { |
| 743 | $that.removeClass('tutor-updating-message'); |
| 744 | } |
| 745 | }); |
| 746 | }); |
| 747 | */ |
| 748 | |
| 749 | |
| 750 | |
| 751 | $(document).on('click', '.tutor-add-quiz-btn', function(e){ |
| 752 | e.preventDefault(); |
| 753 | |
| 754 | var $that = $(this); |
| 755 | var quiz_for_post_id = $(this).closest('.tutor_add_quiz_wrap').attr('data-add-quiz-under'); |
| 756 | $.ajax({ |
| 757 | url : ajaxurl, |
| 758 | type : 'POST', |
| 759 | data : {quiz_for_post_id : quiz_for_post_id, action: 'tutor_load_quiz_builder_modal'}, |
| 760 | beforeSend: function () { |
| 761 | $that.addClass('tutor-updating-message'); |
| 762 | }, |
| 763 | success: function (data) { |
| 764 | $('.tutor-quiz-builder-modal-wrap .modal-container').html(data.data.output); |
| 765 | $('.tutor-quiz-builder-modal-wrap').attr('quiz-for-post-id', quiz_for_post_id).addClass('show'); |
| 766 | }, |
| 767 | complete: function () { |
| 768 | $that.removeClass('tutor-updating-message'); |
| 769 | } |
| 770 | }); |
| 771 | }); |
| 772 | |
| 773 | /** |
| 774 | * Quiz Builder Modal Tabs |
| 775 | */ |
| 776 | $(document).on('click', '.tutor-quiz-modal-tab-item', function(e){ |
| 777 | e.preventDefault(); |
| 778 | |
| 779 | var $that = $(this); |
| 780 | |
| 781 | var $quizTitle = $('[name="quiz_title"]'); |
| 782 | var quiz_title = $quizTitle.val(); |
| 783 | if ( ! quiz_title){ |
| 784 | $quizTitle.closest('.tutor-quiz-builder-form-row').find('.quiz_form_msg').html('<p class="quiz-form-warning">Please save the quiz' + |
| 785 | ' first</p>'); |
| 786 | return; |
| 787 | }else{ |
| 788 | $quizTitle.closest('.tutor-quiz-builder-form-row').find('.quiz_form_msg').html(''); |
| 789 | } |
| 790 | |
| 791 | var tabSelector = $that.attr('href'); |
| 792 | $('.quiz-builder-tab-container').hide(); |
| 793 | $(tabSelector).show(); |
| 794 | |
| 795 | $('a.tutor-quiz-modal-tab-item').removeClass('active'); |
| 796 | $that.addClass('active'); |
| 797 | }); |
| 798 | |
| 799 | //Next Prev Tab |
| 800 | $(document).on('click', '.quiz-modal-btn-next, .quiz-modal-btn-back', function(e){ |
| 801 | e.preventDefault(); |
| 802 | |
| 803 | var tabSelector = $(this).attr('href'); |
| 804 | $('#tutor-quiz-modal-tab-items-wrap a[href="'+tabSelector+'"]').trigger('click'); |
| 805 | }); |
| 806 | |
| 807 | $(document).on('click', '.quiz-modal-tab-navigation-btn.quiz-modal-btn-cancel', function(e){ |
| 808 | e.preventDefault(); |
| 809 | $('.tutor-modal-wrap').removeClass('show'); |
| 810 | }); |
| 811 | |
| 812 | $(document).on('click', '.quiz-modal-btn-first-step', function(e){ |
| 813 | e.preventDefault(); |
| 814 | |
| 815 | var $that = $(this); |
| 816 | var $quizTitle = $('[name="quiz_title"]'); |
| 817 | var quiz_title = $quizTitle.val(); |
| 818 | var quiz_description = $('[name="quiz_description"]').val(); |
| 819 | |
| 820 | if ( ! quiz_title){ |
| 821 | $quizTitle.closest('.tutor-quiz-builder-form-row').find('.quiz_form_msg').html('<p class="quiz-form-warning">Please enter quiz title</p>'); |
| 822 | return; |
| 823 | }else{ |
| 824 | $quizTitle.closest('.tutor-quiz-builder-form-row').find('.quiz_form_msg').html(''); |
| 825 | } |
| 826 | |
| 827 | |
| 828 | |
| 829 | var course_id = $('#post_ID').val(); |
| 830 | var topic_id = $that.closest('.tutor-modal-wrap').attr('quiz-for-post-id'); |
| 831 | |
| 832 | |
| 833 | if ($('#tutor_quiz_builder_quiz_id').length) { |
| 834 | /** |
| 835 | * |
| 836 | * @type {jQuery} |
| 837 | * |
| 838 | * if quiz id exists, we are sending it to update quiz |
| 839 | */ |
| 840 | |
| 841 | var quiz_id = $('#tutor_quiz_builder_quiz_id').val(); |
| 842 | $.ajax({ |
| 843 | url : ajaxurl, |
| 844 | type : 'POST', |
| 845 | data : {quiz_title:quiz_title, quiz_description: quiz_description, quiz_id : quiz_id, topic_id : topic_id, action: 'tutor_quiz_builder_quiz_update'}, |
| 846 | beforeSend: function () { |
| 847 | $that.addClass('tutor-updating-message'); |
| 848 | }, |
| 849 | success: function (data) { |
| 850 | $('#tutor-quiz-'+quiz_id).html(data.data.output_quiz_row); |
| 851 | $('#tutor-quiz-modal-tab-items-wrap a[href="#quiz-builder-tab-questions"]').trigger('click'); |
| 852 | }, |
| 853 | complete: function () { |
| 854 | $that.removeClass('tutor-updating-message'); |
| 855 | } |
| 856 | }); |
| 857 | |
| 858 | return; |
| 859 | } |
| 860 | |
| 861 | $.ajax({ |
| 862 | url : ajaxurl, |
| 863 | type : 'POST', |
| 864 | data : {quiz_title:quiz_title, quiz_description: quiz_description, course_id : course_id, topic_id : topic_id, action: 'tutor_create_quiz_and_load_modal'}, |
| 865 | beforeSend: function () { |
| 866 | $that.addClass('tutor-updating-message'); |
| 867 | }, |
| 868 | success: function (data) { |
| 869 | $('.tutor-quiz-builder-modal-wrap .modal-container').html(data.data.output); |
| 870 | $('#tutor-topics-'+topic_id+' .tutor-lessons').append(data.data.output_quiz_row); |
| 871 | $('#tutor-quiz-modal-tab-items-wrap a[href="#quiz-builder-tab-questions"]').trigger('click'); |
| 872 | }, |
| 873 | complete: function () { |
| 874 | $that.removeClass('tutor-updating-message'); |
| 875 | } |
| 876 | }); |
| 877 | |
| 878 | }); |
| 879 | |
| 880 | /** |
| 881 | * Ope modal for edit quiz |
| 882 | */ |
| 883 | $(document).on('click', '.open-tutor-quiz-modal', function(e){ |
| 884 | e.preventDefault(); |
| 885 | |
| 886 | var $that = $(this); |
| 887 | var quiz_id = $that.attr('data-quiz-id'); |
| 888 | var topic_id = $that.attr('data-topic-id'); |
| 889 | var course_id = $('#post_ID').val(); |
| 890 | |
| 891 | $.ajax({ |
| 892 | url : ajaxurl, |
| 893 | type : 'POST', |
| 894 | data : {quiz_id : quiz_id, topic_id : topic_id, course_id : course_id, action: 'tutor_load_edit_quiz_modal'}, |
| 895 | beforeSend: function () { |
| 896 | $that.addClass('tutor-updating-message'); |
| 897 | }, |
| 898 | success: function (data) { |
| 899 | $('.tutor-quiz-builder-modal-wrap .modal-container').html(data.data.output); |
| 900 | $('.tutor-quiz-builder-modal-wrap').attr('data-quiz-id', quiz_id).addClass('show'); |
| 901 | |
| 902 | //Back to question Tab if exists |
| 903 | if ($that.attr('data-back-to-tab')){ |
| 904 | var tabSelector = $that.attr('data-back-to-tab'); |
| 905 | $('#tutor-quiz-modal-tab-items-wrap a[href="'+tabSelector+'"]').trigger('click'); |
| 906 | } |
| 907 | tutor_slider_init(); |
| 908 | enable_quiz_questions_sorting(); |
| 909 | }, |
| 910 | complete: function () { |
| 911 | $that.removeClass('tutor-updating-message'); |
| 912 | } |
| 913 | }); |
| 914 | }); |
| 915 | |
| 916 | |
| 917 | $(document).on('click', '.quiz-modal-settings-save-btn', function(e){ |
| 918 | e.preventDefault(); |
| 919 | |
| 920 | var $that = $(this); |
| 921 | var quiz_id = $('.tutor-quiz-builder-modal-wrap').attr('data-quiz-id'); |
| 922 | |
| 923 | var $formInput = $('#quiz-builder-tab-settings :input, #quiz-builder-tab-advanced-options :input').serialize()+'&quiz_id='+quiz_id+'&action=tutor_quiz_modal_update_settings'; |
| 924 | |
| 925 | $.ajax({ |
| 926 | url : ajaxurl, |
| 927 | type : 'POST', |
| 928 | data : $formInput, |
| 929 | beforeSend: function () { |
| 930 | $that.addClass('tutor-updating-message'); |
| 931 | }, |
| 932 | success: function (data) { |
| 933 | // |
| 934 | }, |
| 935 | complete: function () { |
| 936 | $that.removeClass('tutor-updating-message'); |
| 937 | if ($that.attr('data-action') === 'modal_close'){ |
| 938 | $('.tutor-modal-wrap').removeClass('show'); |
| 939 | } |
| 940 | } |
| 941 | }); |
| 942 | }); |
| 943 | |
| 944 | |
| 945 | /** |
| 946 | * Add Question to quiz modal |
| 947 | */ |
| 948 | $(document).on('click', '.tutor-quiz-open-question-form', function(e){ |
| 949 | e.preventDefault(); |
| 950 | |
| 951 | var $that = $(this); |
| 952 | |
| 953 | var quiz_id = $('#tutor_quiz_builder_quiz_id').val(); |
| 954 | var course_id = $('#post_ID').val(); |
| 955 | var question_id = $that.attr('data-question-id'); |
| 956 | |
| 957 | |
| 958 | var params = {quiz_id : quiz_id, course_id : course_id, action: 'tutor_quiz_builder_get_question_form'}; |
| 959 | |
| 960 | if (question_id) { |
| 961 | params.question_id = question_id; |
| 962 | } |
| 963 | |
| 964 | $.ajax({ |
| 965 | url : ajaxurl, |
| 966 | type : 'POST', |
| 967 | data : params, |
| 968 | beforeSend: function () { |
| 969 | $that.addClass('tutor-updating-message'); |
| 970 | }, |
| 971 | success: function (data) { |
| 972 | $('.tutor-quiz-builder-modal-contents').html(data.data.output); |
| 973 | |
| 974 | //Initializing Tutor Select |
| 975 | tutor_select().reInit(); |
| 976 | enable_quiz_answer_sorting(); |
| 977 | }, |
| 978 | complete: function () { |
| 979 | $that.removeClass('tutor-updating-message'); |
| 980 | } |
| 981 | }); |
| 982 | |
| 983 | }); |
| 984 | |
| 985 | $(document).on('click', '.quiz-modal-question-save-btn', function(e){ |
| 986 | e.preventDefault(); |
| 987 | |
| 988 | var $that = $(this); |
| 989 | var $formInput = $('.quiz_question_form :input').serialize()+'&action=tutor_quiz_modal_update_question'; |
| 990 | $.ajax({ |
| 991 | url : ajaxurl, |
| 992 | type : 'POST', |
| 993 | data : $formInput, |
| 994 | beforeSend: function () { |
| 995 | $that.addClass('tutor-updating-message'); |
| 996 | }, |
| 997 | success: function (data) { |
| 998 | //ReOpen questions |
| 999 | $that.closest('.quiz-questions-form').find('.open-tutor-quiz-modal').trigger('click'); |
| 1000 | }, |
| 1001 | complete: function () { |
| 1002 | $that.removeClass('tutor-updating-message'); |
| 1003 | } |
| 1004 | }); |
| 1005 | }); |
| 1006 | |
| 1007 | $(document).on('click', '.tutor-quiz-question-trash', function(e){ |
| 1008 | e.preventDefault(); |
| 1009 | |
| 1010 | var $that = $(this); |
| 1011 | var question_id = $that.attr('data-question-id'); |
| 1012 | |
| 1013 | $.ajax({ |
| 1014 | url : ajaxurl, |
| 1015 | type : 'POST', |
| 1016 | data : {question_id : question_id, action: 'tutor_quiz_builder_question_delete'}, |
| 1017 | beforeSend: function () { |
| 1018 | $that.closest('.quiz-builder-question-wrap').remove(); |
| 1019 | }, |
| 1020 | }); |
| 1021 | }); |
| 1022 | |
| 1023 | /** |
| 1024 | * Get question answers option form to save multiple/single/true-false options |
| 1025 | * |
| 1026 | * @since v.1.0.0 |
| 1027 | */ |
| 1028 | |
| 1029 | $(document).on('click', '.add_question_answers_option', function(e){ |
| 1030 | e.preventDefault(); |
| 1031 | |
| 1032 | var $that = $(this); |
| 1033 | var question_id = $that.attr('data-question-id'); |
| 1034 | var $formInput = $('.quiz_question_form :input').serialize()+'&question_id='+question_id+'&action=tutor_quiz_add_question_answers'; |
| 1035 | |
| 1036 | $.ajax({ |
| 1037 | url : ajaxurl, |
| 1038 | type : 'POST', |
| 1039 | data : $formInput, |
| 1040 | beforeSend: function () { |
| 1041 | $that.addClass('tutor-updating-message'); |
| 1042 | }, |
| 1043 | success: function (data) { |
| 1044 | $('#tutor_quiz_question_answer_form').html(data.data.output); |
| 1045 | }, |
| 1046 | complete: function () { |
| 1047 | $that.removeClass('tutor-updating-message'); |
| 1048 | } |
| 1049 | }); |
| 1050 | }); |
| 1051 | |
| 1052 | /** |
| 1053 | * Get question answers option edit form |
| 1054 | * |
| 1055 | * @since v.1.0.0 |
| 1056 | */ |
| 1057 | $(document).on('click', '.tutor-quiz-answer-edit a', function(e){ |
| 1058 | e.preventDefault(); |
| 1059 | |
| 1060 | var $that = $(this); |
| 1061 | var answer_id = $that.closest('.tutor-quiz-answer-wrap').attr('data-answer-id'); |
| 1062 | |
| 1063 | $.ajax({ |
| 1064 | url : ajaxurl, |
| 1065 | type : 'POST', |
| 1066 | data : {answer_id : answer_id, action : 'tutor_quiz_edit_question_answer'}, |
| 1067 | beforeSend: function () { |
| 1068 | $that.addClass('tutor-updating-message'); |
| 1069 | }, |
| 1070 | success: function (data) { |
| 1071 | $('#tutor_quiz_question_answer_form').html(data.data.output); |
| 1072 | }, |
| 1073 | complete: function () { |
| 1074 | $that.removeClass('tutor-updating-message'); |
| 1075 | } |
| 1076 | }); |
| 1077 | }); |
| 1078 | |
| 1079 | |
| 1080 | /** |
| 1081 | * Saving question answers options |
| 1082 | * Student should select the right answer at quiz attempts |
| 1083 | * |
| 1084 | * @since v.1.0.0 |
| 1085 | */ |
| 1086 | |
| 1087 | $(document).on('click', '#quiz-answer-save-btn', function(e){ |
| 1088 | e.preventDefault(); |
| 1089 | |
| 1090 | var $that = $(this); |
| 1091 | var $formInput = $('.quiz_question_form :input').serialize()+'&action=tutor_save_quiz_answer_options'; |
| 1092 | |
| 1093 | $.ajax({ |
| 1094 | url : ajaxurl, |
| 1095 | type : 'POST', |
| 1096 | data : $formInput, |
| 1097 | beforeSend: function () { |
| 1098 | $that.addClass('tutor-updating-message'); |
| 1099 | }, |
| 1100 | success: function (data) { |
| 1101 | $('#tutor_quiz_question_answers').trigger('refresh'); |
| 1102 | }, |
| 1103 | complete: function () { |
| 1104 | $that.removeClass('tutor-updating-message'); |
| 1105 | } |
| 1106 | }); |
| 1107 | }); |
| 1108 | |
| 1109 | /** |
| 1110 | * Updating Answer |
| 1111 | * |
| 1112 | * @since v.1.0.0 |
| 1113 | */ |
| 1114 | $(document).on('click', '#quiz-answer-edit-btn', function(e){ |
| 1115 | e.preventDefault(); |
| 1116 | |
| 1117 | var $that = $(this); |
| 1118 | var $formInput = $('.quiz_question_form :input').serialize()+'&action=tutor_update_quiz_answer_options'; |
| 1119 | |
| 1120 | $.ajax({ |
| 1121 | url : ajaxurl, |
| 1122 | type : 'POST', |
| 1123 | data : $formInput, |
| 1124 | beforeSend: function () { |
| 1125 | $that.addClass('tutor-updating-message'); |
| 1126 | }, |
| 1127 | success: function (data) { |
| 1128 | $('#tutor_quiz_question_answers').trigger('refresh'); |
| 1129 | }, |
| 1130 | complete: function () { |
| 1131 | $that.removeClass('tutor-updating-message'); |
| 1132 | } |
| 1133 | }); |
| 1134 | }); |
| 1135 | |
| 1136 | $(document).on('change', '.tutor-quiz-answers-mark-correct-wrap input', function(e){ |
| 1137 | e.preventDefault(); |
| 1138 | |
| 1139 | var $that = $(this); |
| 1140 | |
| 1141 | var answer_id = $that.val(); |
| 1142 | var inputValue = 1; |
| 1143 | if ( ! $that.prop('checked')) { |
| 1144 | inputValue = 0; |
| 1145 | } |
| 1146 | |
| 1147 | $.ajax({ |
| 1148 | url : ajaxurl, |
| 1149 | type : 'POST', |
| 1150 | data : {answer_id:answer_id, inputValue : inputValue, action : 'tutor_mark_answer_as_correct'}, |
| 1151 | }); |
| 1152 | }); |
| 1153 | |
| 1154 | |
| 1155 | $(document).on('refresh', '#tutor_quiz_question_answers', function(e){ |
| 1156 | e.preventDefault(); |
| 1157 | |
| 1158 | var $that = $(this); |
| 1159 | var question_id = $that.attr('data-question-id'); |
| 1160 | var question_type = $('.tutor_select_value_holder').val(); |
| 1161 | |
| 1162 | $.ajax({ |
| 1163 | url : ajaxurl, |
| 1164 | type : 'POST', |
| 1165 | data : {question_id : question_id, question_type : question_type, action: 'tutor_quiz_builder_get_answers_by_question'}, |
| 1166 | beforeSend: function () { |
| 1167 | $that.addClass('tutor-updating-message'); |
| 1168 | $('#tutor_quiz_question_answer_form').html(''); |
| 1169 | }, |
| 1170 | success: function (data) { |
| 1171 | if (data.success){ |
| 1172 | $that.html(data.data.output); |
| 1173 | } |
| 1174 | }, |
| 1175 | complete: function () { |
| 1176 | $that.removeClass('tutor-updating-message'); |
| 1177 | } |
| 1178 | }); |
| 1179 | }); |
| 1180 | |
| 1181 | /** |
| 1182 | * Delete answer for a question in quiz builder |
| 1183 | * |
| 1184 | * @since v.1.0.0 |
| 1185 | */ |
| 1186 | |
| 1187 | $(document).on('click', '.tutor-quiz-answer-trash-wrap a.answer-trash-btn', function(e){ |
| 1188 | e.preventDefault(); |
| 1189 | |
| 1190 | var $that = $(this); |
| 1191 | var answer_id = $that.attr('data-answer-id'); |
| 1192 | |
| 1193 | $.ajax({ |
| 1194 | url : ajaxurl, |
| 1195 | type : 'POST', |
| 1196 | data : {answer_id : answer_id, action: 'tutor_quiz_builder_delete_answer'}, |
| 1197 | beforeSend: function () { |
| 1198 | $that.closest('.tutor-quiz-answer-wrap').remove(); |
| 1199 | }, |
| 1200 | }); |
| 1201 | }); |
| 1202 | |
| 1203 | /** |
| 1204 | * Sort quiz questions |
| 1205 | */ |
| 1206 | function enable_quiz_questions_sorting(){ |
| 1207 | if (jQuery().sortable) { |
| 1208 | $(".quiz-builder-questions-wrap").sortable({ |
| 1209 | handle: ".question-sorting", |
| 1210 | start: function (e, ui) { |
| 1211 | ui.placeholder.css('visibility', 'visible'); |
| 1212 | }, |
| 1213 | stop: function (e, ui) { |
| 1214 | tutor_save_sorting_quiz_questions_order(); |
| 1215 | }, |
| 1216 | }); |
| 1217 | } |
| 1218 | } |
| 1219 | |
| 1220 | function tutor_save_sorting_quiz_questions_order(){ |
| 1221 | var questions = {}; |
| 1222 | $('.quiz-builder-question-wrap').each(function(index, item){ |
| 1223 | var $question = $(this); |
| 1224 | var question_id = parseInt($question.attr('data-question-id'), 10); |
| 1225 | questions[index] = question_id; |
| 1226 | }); |
| 1227 | |
| 1228 | $.ajax({url : ajaxurl, type : 'POST', |
| 1229 | data : {sorted_question_ids : questions, action: 'tutor_quiz_question_sorting'}, |
| 1230 | }); |
| 1231 | } |
| 1232 | |
| 1233 | /** |
| 1234 | * Save answer sorting placement |
| 1235 | * |
| 1236 | * @since v.1.0.0 |
| 1237 | */ |
| 1238 | function enable_quiz_answer_sorting(){ |
| 1239 | if (jQuery().sortable) { |
| 1240 | $("#tutor_quiz_question_answers").sortable({ |
| 1241 | handle: ".tutor-quiz-answer-sort-icon", |
| 1242 | start: function (e, ui) { |
| 1243 | ui.placeholder.css('visibility', 'visible'); |
| 1244 | }, |
| 1245 | stop: function (e, ui) { |
| 1246 | tutor_save_sorting_quiz_answer_order(); |
| 1247 | }, |
| 1248 | }); |
| 1249 | } |
| 1250 | } |
| 1251 | function tutor_save_sorting_quiz_answer_order(){ |
| 1252 | var answers = {}; |
| 1253 | $('.tutor-quiz-answer-wrap').each(function(index, item){ |
| 1254 | var $answer = $(this); |
| 1255 | var answer_id = parseInt($answer.attr('data-answer-id'), 10); |
| 1256 | answers[index] = answer_id; |
| 1257 | }); |
| 1258 | |
| 1259 | $.ajax({url : ajaxurl, type : 'POST', |
| 1260 | data : {sorted_answer_ids : answers, action: 'tutor_quiz_answer_sorting'}, |
| 1261 | }); |
| 1262 | } |
| 1263 | |
| 1264 | |
| 1265 | |
| 1266 | /** |
| 1267 | * Deprecated, should remove |
| 1268 | * @todo: should remove this |
| 1269 | */ |
| 1270 | |
| 1271 | $(document).on('click', '.add_quiz_to_post_btn', function(e){ |
| 1272 | e.preventDefault(); |
| 1273 | |
| 1274 | var $that = $(this); |
| 1275 | var $modal = $('.tutor-modal-wrap'); |
| 1276 | |
| 1277 | var quiz_for_post_id = $modal.attr('quiz-for-post-id'); |
| 1278 | var data = $modal.find('input').serialize()+'&action=tutor_add_quiz_to_post&parent_post_id='+quiz_for_post_id; |
| 1279 | |
| 1280 | $.ajax({ |
| 1281 | url : ajaxurl, |
| 1282 | type : 'POST', |
| 1283 | data : data, |
| 1284 | beforeSend: function () { |
| 1285 | $that.addClass('tutor-updating-message'); |
| 1286 | }, |
| 1287 | success: function (data) { |
| 1288 | if (data.success){ |
| 1289 | $('[data-add-quiz-under="'+quiz_for_post_id+'"] .tutor-available-quizzes').html(data.data.output); |
| 1290 | $('.tutor-modal-wrap').removeClass('show'); |
| 1291 | } |
| 1292 | }, |
| 1293 | complete: function () { |
| 1294 | $that.removeClass('tutor-updating-message'); |
| 1295 | } |
| 1296 | }); |
| 1297 | }); |
| 1298 | |
| 1299 | $(document).on('change keyup', '.tutor-quiz-modal-wrap .tutor-modal-search-input', function(e){ |
| 1300 | e.preventDefault(); |
| 1301 | |
| 1302 | var $that = $(this); |
| 1303 | var $modal = $('.tutor-modal-wrap'); |
| 1304 | |
| 1305 | tutor_delay(function(){ |
| 1306 | var search_terms = $that.val(); |
| 1307 | var quiz_for_post_id = $modal.attr('quiz-for-post-id'); |
| 1308 | |
| 1309 | $.ajax({ |
| 1310 | url : ajaxurl, |
| 1311 | type : 'POST', |
| 1312 | data : {quiz_for_post_id : quiz_for_post_id, search_terms : search_terms, action: 'tutor_load_quiz_modal'}, |
| 1313 | beforeSend: function () { |
| 1314 | $modal.addClass('loading'); |
| 1315 | }, |
| 1316 | success: function (data) { |
| 1317 | if (data.success){ |
| 1318 | $('.tutor-modal-wrap .modal-container').html(data.data.output); |
| 1319 | } |
| 1320 | }, |
| 1321 | complete: function () { |
| 1322 | $modal.removeClass('loading'); |
| 1323 | } |
| 1324 | }); |
| 1325 | |
| 1326 | }, 1000) |
| 1327 | }); |
| 1328 | |
| 1329 | var tutor_delay = (function(){ |
| 1330 | var timer = 0; |
| 1331 | return function(callback, ms){ |
| 1332 | clearTimeout (timer); |
| 1333 | timer = setTimeout(callback, ms); |
| 1334 | }; |
| 1335 | })(); |
| 1336 | |
| 1337 | $(document).on('click', '.tutor-quiz-delete-btn', function(e){ |
| 1338 | e.preventDefault(); |
| 1339 | |
| 1340 | var $that = $(this); |
| 1341 | var quiz_id = $that.closest('.added-quiz-item').attr('data-quiz-id'); |
| 1342 | |
| 1343 | $.ajax({ |
| 1344 | url : ajaxurl, |
| 1345 | type : 'POST', |
| 1346 | data : {quiz_id:quiz_id, action: 'remove_quiz_from_post'}, |
| 1347 | success: function (data) { |
| 1348 | if (data.success){ |
| 1349 | $that.closest('.added-quiz-item').remove(); |
| 1350 | } |
| 1351 | } |
| 1352 | }); |
| 1353 | }); |
| 1354 | |
| 1355 | /** |
| 1356 | * Add instructor modal |
| 1357 | */ |
| 1358 | $(document).on('click', '.tutor-add-instructor-btn', function(e){ |
| 1359 | e.preventDefault(); |
| 1360 | |
| 1361 | var $that = $(this); |
| 1362 | var course_id = $('#post_ID').val(); |
| 1363 | |
| 1364 | $.ajax({ |
| 1365 | url : ajaxurl, |
| 1366 | type : 'POST', |
| 1367 | data : {course_id : course_id, action: 'tutor_load_instructors_modal'}, |
| 1368 | beforeSend: function () { |
| 1369 | $that.addClass('tutor-updating-message'); |
| 1370 | }, |
| 1371 | success: function (data) { |
| 1372 | if (data.success){ |
| 1373 | $('.tutor-instructors-modal-wrap .modal-container').html(data.data.output); |
| 1374 | $('.tutor-instructors-modal-wrap').addClass('show'); |
| 1375 | } |
| 1376 | }, |
| 1377 | complete: function () { |
| 1378 | $that.removeClass('tutor-updating-message'); |
| 1379 | } |
| 1380 | }); |
| 1381 | }); |
| 1382 | |
| 1383 | $(document).on('change keyup', '.tutor-instructors-modal-wrap .tutor-modal-search-input', function(e){ |
| 1384 | e.preventDefault(); |
| 1385 | |
| 1386 | var $that = $(this); |
| 1387 | var $modal = $('.tutor-modal-wrap'); |
| 1388 | |
| 1389 | tutor_delay(function(){ |
| 1390 | var search_terms = $that.val(); |
| 1391 | var course_id = $('#post_ID').val(); |
| 1392 | |
| 1393 | $.ajax({ |
| 1394 | url : ajaxurl, |
| 1395 | type : 'POST', |
| 1396 | data : {course_id : course_id, search_terms : search_terms, action: 'tutor_load_instructors_modal'}, |
| 1397 | beforeSend: function () { |
| 1398 | $modal.addClass('loading'); |
| 1399 | }, |
| 1400 | success: function (data) { |
| 1401 | if (data.success){ |
| 1402 | $('.tutor-instructors-modal-wrap .modal-container').html(data.data.output); |
| 1403 | $('.tutor-instructors-modal-wrap').addClass('show'); |
| 1404 | } |
| 1405 | }, |
| 1406 | complete: function () { |
| 1407 | $modal.removeClass('loading'); |
| 1408 | } |
| 1409 | }); |
| 1410 | |
| 1411 | }, 1000) |
| 1412 | }); |
| 1413 | $(document).on('click', '.add_instructor_to_course_btn', function(e){ |
| 1414 | e.preventDefault(); |
| 1415 | |
| 1416 | var $that = $(this); |
| 1417 | var $modal = $('.tutor-modal-wrap'); |
| 1418 | var course_id = $('#post_ID').val(); |
| 1419 | var data = $modal.find('input').serialize()+'&course_id='+course_id+'&action=tutor_add_instructors_to_course'; |
| 1420 | |
| 1421 | $.ajax({ |
| 1422 | url : ajaxurl, |
| 1423 | type : 'POST', |
| 1424 | data : data, |
| 1425 | beforeSend: function () { |
| 1426 | $that.addClass('tutor-updating-message'); |
| 1427 | }, |
| 1428 | success: function (data) { |
| 1429 | if (data.success){ |
| 1430 | $('.tutor-course-available-instructors').html(data.data.output); |
| 1431 | $('.tutor-modal-wrap').removeClass('show'); |
| 1432 | } |
| 1433 | }, |
| 1434 | complete: function () { |
| 1435 | $that.removeClass('tutor-updating-message'); |
| 1436 | } |
| 1437 | }); |
| 1438 | }); |
| 1439 | |
| 1440 | $(document).on('click', '.tutor-instructor-delete-btn', function(e){ |
| 1441 | e.preventDefault(); |
| 1442 | |
| 1443 | var $that = $(this); |
| 1444 | var course_id = $('#post_ID').val(); |
| 1445 | var instructor_id = $that.closest('.added-instructor-item').attr('data-instructor-id'); |
| 1446 | |
| 1447 | $.ajax({ |
| 1448 | url : ajaxurl, |
| 1449 | type : 'POST', |
| 1450 | data : {course_id:course_id, instructor_id:instructor_id, action : 'detach_instructor_from_course'}, |
| 1451 | success: function (data) { |
| 1452 | if (data.success){ |
| 1453 | $that.closest('.added-instructor-item').remove(); |
| 1454 | } |
| 1455 | } |
| 1456 | }); |
| 1457 | }); |
| 1458 | |
| 1459 | $(document).on('click', '.tutor-option-media-upload-btn', function(e){ |
| 1460 | e.preventDefault(); |
| 1461 | |
| 1462 | var $that = $(this); |
| 1463 | var frame; |
| 1464 | if ( frame ) { |
| 1465 | frame.open(); |
| 1466 | return; |
| 1467 | } |
| 1468 | frame = wp.media({ |
| 1469 | title: 'Select or Upload Media Of Your Chosen Persuasion', |
| 1470 | button: { |
| 1471 | text: 'Use this media' |
| 1472 | }, |
| 1473 | multiple: false |
| 1474 | }); |
| 1475 | frame.on( 'select', function() { |
| 1476 | var attachment = frame.state().get('selection').first().toJSON(); |
| 1477 | $that.closest('.option-media-wrap').find('.option-media-preview').html('<img src="'+attachment.url+'" alt="" />'); |
| 1478 | $that.closest('.option-media-wrap').find('input').val(attachment.id); |
| 1479 | }); |
| 1480 | frame.open(); |
| 1481 | }); |
| 1482 | |
| 1483 | $(document).on('change', '.tutor_addons_list_item', function(e) { |
| 1484 | var $that = $(this); |
| 1485 | |
| 1486 | var isEnable = $that.prop('checked') ? 1 : 0; |
| 1487 | var addonFieldName = $that.attr('name'); |
| 1488 | |
| 1489 | $.ajax({ |
| 1490 | url : ajaxurl, |
| 1491 | type : 'POST', |
| 1492 | data : {isEnable:isEnable, addonFieldName:addonFieldName, action : 'addon_enable_disable'}, |
| 1493 | success: function (data) { |
| 1494 | if (data.success){ |
| 1495 | //Success |
| 1496 | } |
| 1497 | } |
| 1498 | }); |
| 1499 | }); |
| 1500 | |
| 1501 | /** |
| 1502 | * Tutor Custom Select |
| 1503 | */ |
| 1504 | |
| 1505 | function tutor_select(){ |
| 1506 | var obj = { |
| 1507 | init : function(){ |
| 1508 | $(document).on('click', '.tutor-select .tutor-select-option', function(e){ |
| 1509 | e.preventDefault(); |
| 1510 | |
| 1511 | var $that = $(this); |
| 1512 | var $html = $that.html().trim(); |
| 1513 | $that.closest('.tutor-select').find('.select-header .lead-option').html($html); |
| 1514 | $that.closest('.tutor-select').find('.select-header input.tutor_select_value_holder').val($that.attr('data-value')).trigger('change'); |
| 1515 | $that.closest('.tutor-select-options').hide(); |
| 1516 | }); |
| 1517 | $(document).on('click', '.tutor-select .select-header', function(e){ |
| 1518 | e.preventDefault(); |
| 1519 | |
| 1520 | var $that = $(this); |
| 1521 | $that.closest('.tutor-select').find('.tutor-select-options').slideToggle(); |
| 1522 | }); |
| 1523 | |
| 1524 | this.setValue(); |
| 1525 | this.hideOnOutSideClick(); |
| 1526 | }, |
| 1527 | setValue : function(){ |
| 1528 | $('.tutor-select').each(function(){ |
| 1529 | var $that = $(this); |
| 1530 | var $option = $that.find('.tutor-select-option'); |
| 1531 | |
| 1532 | if ($option.length){ |
| 1533 | $option.each(function(){ |
| 1534 | var $thisOption = $(this); |
| 1535 | |
| 1536 | if ($thisOption.attr('data-selected') === 'selected'){ |
| 1537 | var $html = $thisOption.html().trim(); |
| 1538 | $thisOption.closest('.tutor-select').find('.select-header .lead-option').html($html); |
| 1539 | $thisOption.closest('.tutor-select').find('.select-header input.tutor_select_value_holder').val($thisOption.attr('data-value')); |
| 1540 | } |
| 1541 | }); |
| 1542 | } |
| 1543 | }); |
| 1544 | }, |
| 1545 | hideOnOutSideClick : function(){ |
| 1546 | $(document).mouseup(function(e) { |
| 1547 | var $option_wrap = $(".tutor-select-options"); |
| 1548 | if ( ! $(e.target).closest('.select-header').length && !$option_wrap.is(e.target) && $option_wrap.has(e.target).length === 0) { |
| 1549 | $option_wrap.hide(); |
| 1550 | } |
| 1551 | }); |
| 1552 | }, |
| 1553 | reInit : function(){ |
| 1554 | this.setValue(); |
| 1555 | } |
| 1556 | }; |
| 1557 | |
| 1558 | return obj; |
| 1559 | } |
| 1560 | tutor_select().init(); |
| 1561 | |
| 1562 | /** |
| 1563 | * If change question type from quiz builder question |
| 1564 | * |
| 1565 | * @since v.1.0.0 |
| 1566 | */ |
| 1567 | $(document).on('change', 'input.tutor_select_value_holder', function(e) { |
| 1568 | var $that = $(this); |
| 1569 | //$('#tutor_quiz_question_answer_form').html(''); |
| 1570 | $('.add_question_answers_option').trigger('click'); |
| 1571 | $('#tutor_quiz_question_answers').trigger('refresh'); |
| 1572 | }); |
| 1573 | |
| 1574 | $(document).on('click', '.tutor-media-upload-btn', function(e){ |
| 1575 | e.preventDefault(); |
| 1576 | |
| 1577 | var $that = $(this); |
| 1578 | var frame; |
| 1579 | if ( frame ) { |
| 1580 | frame.open(); |
| 1581 | return; |
| 1582 | } |
| 1583 | frame = wp.media({ |
| 1584 | title: 'Select or Upload Media Of Your Chosen Persuasion', |
| 1585 | button: { |
| 1586 | text: 'Use this media' |
| 1587 | }, |
| 1588 | multiple: false |
| 1589 | }); |
| 1590 | frame.on( 'select', function() { |
| 1591 | var attachment = frame.state().get('selection').first().toJSON(); |
| 1592 | $that.html('<img src="'+attachment.url+'" alt="" />'); |
| 1593 | $that.closest('.tutor-media-upload-wrap').find('input').val(attachment.id); |
| 1594 | }); |
| 1595 | frame.open(); |
| 1596 | }); |
| 1597 | $(document).on('click', '.tutor-media-upload-trash', function(e){ |
| 1598 | e.preventDefault(); |
| 1599 | |
| 1600 | var $that = $(this); |
| 1601 | $that.closest('.tutor-media-upload-wrap').find('.tutor-media-upload-btn').html('<i class="tutor-icon-image1"></i>'); |
| 1602 | $that.closest('.tutor-media-upload-wrap').find('input').val(''); |
| 1603 | }); |
| 1604 | |
| 1605 | /** |
| 1606 | * Add instructor |
| 1607 | * @since v.1.0.3 |
| 1608 | */ |
| 1609 | $(document).on('submit', '#new-instructor-form', function(e){ |
| 1610 | e.preventDefault(); |
| 1611 | |
| 1612 | var $that = $(this); |
| 1613 | var formData = $that.serialize()+'&action=tutor_add_instructor'; |
| 1614 | |
| 1615 | $.ajax({ |
| 1616 | url : ajaxurl, |
| 1617 | type : 'POST', |
| 1618 | data : formData, |
| 1619 | success: function (data) { |
| 1620 | if (data.success){ |
| 1621 | $that.trigger("reset"); |
| 1622 | $('#form-response').html('<p class="tutor-status-approved-context">'+data.data.msg+'</p>'); |
| 1623 | }else{ |
| 1624 | var errorMsg = ''; |
| 1625 | |
| 1626 | var errors = data.data.errors; |
| 1627 | if (errors && Object.keys(errors).length){ |
| 1628 | $.each(data.data.errors, function( index, value ) { |
| 1629 | if (isObject(value)){ |
| 1630 | |
| 1631 | $.each(value, function( key, value1 ) { |
| 1632 | errorMsg += '<p class="tutor-required-fields">'+value1[0]+'</p>'; |
| 1633 | }); |
| 1634 | } else{ |
| 1635 | errorMsg += '<p class="tutor-required-fields">'+value+'</p>'; |
| 1636 | } |
| 1637 | }); |
| 1638 | $('#form-response').html(errorMsg); |
| 1639 | } |
| 1640 | |
| 1641 | } |
| 1642 | } |
| 1643 | }); |
| 1644 | }); |
| 1645 | |
| 1646 | function isObject (value) { |
| 1647 | return value && typeof value === 'object' && value.constructor === Object; |
| 1648 | } |
| 1649 | |
| 1650 | }); |
| 1651 |