PluginProbe ʕ •ᴥ•ʔ
Tutor LMS – eLearning and online course solution / 1.0.1
Tutor LMS – eLearning and online course solution v1.0.1
4.0.1 4.0.0 3.9.15 3.9.14 3.9.13 3.9.12 3.9.11 trunk 1.0.0 1.0.0-alpha 1.0.1 1.0.2 1.0.3 1.0.4 1.0.5 1.0.6 1.0.7 1.0.8 1.0.9 1.1.0 1.1.1 1.2.0 1.2.1 1.2.11 1.2.12 1.2.13 1.2.20 1.3.0 1.3.1 1.3.2 1.3.3 1.3.4 1.3.5 1.3.6 1.3.7 1.3.8 1.3.9 1.4.0 1.4.1 1.4.2 1.4.3 1.4.4 1.4.5 1.4.6 1.4.7 1.4.8 1.4.9 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.5.5 1.5.6 1.5.7 1.5.8 1.5.9 1.6.0 1.6.1 1.6.2 1.6.3 1.6.4 1.6.5 1.6.6 1.6.7 1.6.8 1.6.9 1.7.0 1.7.1 1.7.2 1.7.3 1.7.4 1.7.5 1.7.6 1.7.7 1.7.8 1.7.9 1.8.0 1.8.1 1.8.10 1.8.2 1.8.3 1.8.4 1.8.5 1.8.6 1.8.7 1.8.8 1.8.9 1.9.0 1.9.1 1.9.10 1.9.11 1.9.12 1.9.13 1.9.14 1.9.15 1.9.16 1.9.2 1.9.3 1.9.4 1.9.5 1.9.6 1.9.7 1.9.8 1.9.9 2.0.0 2.0.1 2.0.10 2.0.2 2.0.3 2.0.4 2.0.5 2.0.6 2.0.7 2.0.8 2.0.9 2.1.0 2.1.1 2.1.10 2.1.2 2.1.3 2.1.4 2.1.5 2.1.6 2.1.7 2.1.8 2.1.9 2.2.0 2.2.1 2.2.2 2.2.3 2.2.4 2.3.0 2.4.0 2.5.0 2.6.0 2.6.1 2.6.2 2.7.0 2.7.1 2.7.2 2.7.3 2.7.4 2.7.5 2.7.6 2.7.7 3.0.0 3.0.1 3.0.2 3.1.0 3.2.0 3.2.1 3.2.2 3.2.3 3.3.0 3.3.1 3.4.0 3.4.1 3.4.2 3.5.0 3.6.0 3.6.1 3.6.2 3.6.3 3.6.4 3.7.0 3.7.1 3.7.2 3.7.3 3.7.4 3.8.0 3.8.1 3.8.2 3.8.3 3.9.0 3.9.1 3.9.10 3.9.2 3.9.3 3.9.4 3.9.5 3.9.6 3.9.7 3.9.8 3.9.9
tutor / assets / js / tutor-admin.js
tutor / assets / js Last commit date
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
1647 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 console.log(topic_id);
192
193 if ( ! confirm('Are you sure to delete?')){
194 e.preventDefault();
195 }
196 });
197
198 /**
199 * Create Lesson Under Topic
200 */
201 /*
202 $(document).on('click', '.create-lesson-in-topic-btn', function(e){
203 e.preventDefault();
204 $(this).closest('.tutor-lessons').find('.tutor-create-new-lesson-form').toggle();
205 });
206 $(document).on('click', '.tutor-create-lesson-btn', function(e){
207 e.preventDefault();
208 var $that = $(this);
209
210 var course_id = $('#post_ID').val();
211 var topic_id = $that.closest('.tutor-create-new-lesson-form').attr('data-topic-id');
212
213 var form_data = $that.closest('.tutor-create-new-lesson-form').find('input, textarea').serialize()+'&course_id='+course_id+'&topic_id='+topic_id+'&action=tutor_create_lesson';
214
215 $.ajax({
216 url : ajaxurl,
217 type : 'POST',
218 data : form_data,
219 beforeSend: function () {
220 $that.addClass('tutor-updating-message');
221 },
222 success: function (data) {
223 if (data.success){
224 $('#tutor-course-content-wrap').html(data.data.course_contents);
225 $that.closest('.tutor-create-new-lesson-form').find('input, textarea').each(function () {
226 $(this).val('');
227 });
228 enable_sorting_topic_lesson();
229 }
230 },
231 complete: function () {
232 $that.removeClass('tutor-updating-message');
233 }
234 });
235 });
236
237 $(document).on('click', '.open-inline-lesson-edit-btn', function(e){
238 e.preventDefault();
239 $(this).closest('.tutor-lesson').find('.tutor-edit-inline-lesson-form').toggle();
240 });
241 $(document).on('change keyup', '.inline-lesson-title-input', function (e) {
242 e.preventDefault();
243 $(this).closest('.tutor-lesson').find('.open-inline-lesson-edit-btn').html($(this).val());
244 });
245
246 $(document).on('click', '.edit-inline-lesson-btn', function(e){
247 e.preventDefault();
248 var $that = $(this);
249
250 var course_id = $('#post_ID').val();
251 var lesson_id = $that.closest('.tutor-edit-inline-lesson-form').attr('data-lesson-id');
252 var topic_id = $that.closest('.tutor-edit-inline-lesson-form').attr('data-topic-id');
253
254 var form_data = $that.closest('.tutor-edit-inline-lesson-form').find('input, textarea').serialize()+'&course_id='+course_id+'&lesson_id='+lesson_id+'&topic_id='+topic_id+'&action=tutor_update_inline_lesson';
255
256 $.ajax({
257 url : ajaxurl,
258 type : 'POST',
259 data : form_data,
260 beforeSend: function () {
261 $that.addClass('tutor-updating-message');
262 },
263 success: function (data) {
264 if (data.success){
265 $that.closest('.tutor-edit-inline-lesson-form').hide();
266 }
267 },
268 complete: function () {
269 $that.removeClass('tutor-updating-message');
270 }
271 });
272 });
273 */
274 $(document).on('click', '.tutor-expand-all-topic', function (e) {
275 e.preventDefault();
276 $('.tutor-topics-body').slideDown();
277 });
278 $(document).on('click', '.tutor-collapse-all-topic', function (e) {
279 e.preventDefault();
280 $('.tutor-topics-body').slideUp();
281 });
282 $(document).on('click', '.expand-collapse-wrap', function (e) {
283 e.preventDefault();
284 var $that = $(this);
285 $that.closest('.tutor-topics-wrap').find('.tutor-topics-body').slideToggle();
286 $that.closest('.tutor-topics-wrap').find('.expand-collapse-wrap .dashicons').toggleClass('dashicons-arrow-down-alt2 dashicons-arrow-up-alt2 ');
287 });
288
289 /**
290 * Update Lesson Modal
291 */
292 $(document).on('click', '.open-tutor-lesson-modal', function(e){
293 e.preventDefault();
294
295 var $that = $(this);
296 var lesson_id = $that.attr('data-lesson-id');
297 var topic_id = $that.attr('data-topic-id');
298 var course_id = $('#post_ID').val();
299
300 $.ajax({
301 url : ajaxurl,
302 type : 'POST',
303 data : {lesson_id : lesson_id, topic_id : topic_id, course_id : course_id, action: 'tutor_load_edit_lesson_modal'},
304 beforeSend: function () {
305 $that.addClass('tutor-updating-message');
306 },
307 success: function (data) {
308 $('.tutor-lesson-modal-wrap .modal-container').html(data.data.output);
309 $('.tutor-lesson-modal-wrap').attr({'data-lesson-id' : lesson_id, 'data-topic-id':topic_id}).addClass('show');
310
311 tinymce.init(tinyMCEPreInit.mceInit.content);
312 tinymce.execCommand( 'mceRemoveEditor', false, 'tutor_lesson_modal_editor' );
313 tinyMCE.execCommand('mceAddEditor', false, "tutor_lesson_modal_editor");
314 },
315 complete: function () {
316 quicktags({id : "tutor_lesson_modal_editor"});
317 $that.removeClass('tutor-updating-message');
318 }
319 });
320 });
321
322 $(document).on( 'click', '.lesson_thumbnail_upload_btn', function( event ){
323 event.preventDefault();
324 var $that = $(this);
325 var frame;
326 if ( frame ) {
327 frame.open();
328 return;
329 }
330 frame = wp.media({
331 title: 'Select or Upload Media Of Your Chosen Persuasion',
332 button: {
333 text: 'Use this media'
334 },
335 multiple: false
336 });
337 frame.on( 'select', function() {
338 var attachment = frame.state().get('selection').first().toJSON();
339 $that.closest('.tutor-thumbnail-wrap').find('.thumbnail-img').html('<img src="'+attachment.url+'" alt="" />');
340 $that.closest('.tutor-thumbnail-wrap').find('input').val(attachment.id);
341 });
342 frame.open();
343 });
344
345 /**
346 * Delete Lesson from course builder
347 */
348 $(document).on('click', '.tutor-delete-lesson-btn', function(e){
349 e.preventDefault();
350
351 if( ! confirm('Are you sure?')){
352 return;
353 }
354
355 var $that = $(this);
356 var lesson_id = $that.attr('data-lesson-id');
357
358 $.ajax({
359 url : ajaxurl,
360 type : 'POST',
361 data : {lesson_id : lesson_id, action: 'tutor_delete_lesson_by_id'},
362 beforeSend: function () {
363 $that.addClass('tutor-updating-message');
364 },
365 success: function (data) {
366 if (data.success){
367 $that.closest('.tutor-lesson').remove();
368 }
369 },
370 complete: function () {
371 $that.removeClass('tutor-updating-message');
372 }
373 });
374 });
375
376 /**
377 * Delete quiz
378 */
379 $(document).on('click', '.tutor-delete-quiz-btn', function(e){
380 e.preventDefault();
381
382 if( ! confirm('Are you sure?')){
383 return;
384 }
385
386 var $that = $(this);
387 var quiz_id = $that.attr('data-quiz-id');
388
389 $.ajax({
390 url : ajaxurl,
391 type : 'POST',
392 data : {quiz_id : quiz_id, action: 'tutor_delete_quiz_by_id'},
393 beforeSend: function () {
394 $that.closest('.course-content-item').remove();
395 }
396 });
397 });
398
399 /**
400 * Lesson Update or Create Modal
401 */
402 $(document).on( 'click', '.update_lesson_modal_btn', function( event ){
403 event.preventDefault();
404
405 var $that = $(this);
406 var content;
407 var editor = tinyMCE.get('tutor_lesson_modal_editor');
408 if (editor) {
409 content = editor.getContent();
410 } else {
411 content = $('#'+inputid).val();
412 }
413
414 var form_data = $(this).closest('form').serialize();
415 form_data += '&lesson_content='+content;
416
417 $.ajax({
418 url : ajaxurl,
419 type : 'POST',
420 data : form_data,
421 beforeSend: function () {
422 $that.addClass('tutor-updating-message');
423 },
424 success: function (data) {
425 if (data.success){
426 $('#tutor-course-content-wrap').html(data.data.course_contents);
427 enable_sorting_topic_lesson();
428
429 //Close the modal
430 $('.tutor-lesson-modal-wrap').removeClass('show');
431 }
432 },
433 complete: function () {
434 $that.removeClass('tutor-updating-message');
435 }
436 });
437 });
438
439 /**
440 * Lesson Video
441 */
442 $(document).on('change', '.tutor_lesson_video_source', function(e){
443 var selector = $(this).val();
444 $('[class^="video_source_wrap"]').hide();
445 $('.video_source_wrap_'+selector).show();
446 });
447
448 $(document).on( 'click', '.video_source_wrap_html5 .video_upload_btn', function( event ){
449 event.preventDefault();
450
451 var $that = $(this);
452 var frame;
453 // If the media frame already exists, reopen it.
454 if ( frame ) {
455 frame.open();
456 return;
457 }
458
459 // Create a new media frame
460 frame = wp.media({
461 title: 'Select or Upload Media Of Your Chosen Persuasion',
462 button: {
463 text: 'Use this media'
464 },
465 multiple: false // Set to true to allow multiple files to be selected
466 });
467
468 // When an image is selected in the media frame...
469 frame.on( 'select', function() {
470 // Get media attachment details from the frame state
471 var attachment = frame.state().get('selection').first().toJSON();
472 $that.closest('.video_source_wrap_html5').find('span.video_media_id').text(attachment.id).closest('p').show();
473 $that.closest('.video_source_wrap_html5').find('input').val(attachment.id);
474 });
475 // Finally, open the modal on click
476 frame.open();
477 });
478
479 //tutor_video_poster_upload_btn
480 $(document).on( 'click', '.tutor_video_poster_upload_btn', function( event ){
481 event.preventDefault();
482
483 var $that = $(this);
484 var frame;
485 // If the media frame already exists, reopen it.
486 if ( frame ) {
487 frame.open();
488 return;
489 }
490
491 // Create a new media frame
492 frame = wp.media({
493 title: 'Select or Upload Media Of Your Chosen Persuasion',
494 button: {
495 text: 'Use this media'
496 },
497 multiple: false // Set to true to allow multiple files to be selected
498 });
499
500 // When an image is selected in the media frame...
501 frame.on( 'select', function() {
502 // Get media attachment details from the frame state
503 var attachment = frame.state().get('selection').first().toJSON();
504 $that.closest('.tutor-video-poster-wrap').find('.video-poster-img').html('<img src="'+attachment.url+'" alt="" />');
505 $that.closest('.tutor-video-poster-wrap').find('input').val(attachment.id);
506 });
507 // Finally, open the modal on click
508 frame.open();
509 });
510
511 $(document).on('click', 'a.tutor-delete-attachment', function(e){
512 e.preventDefault();
513 $(this).closest('.tutor-added-attachment').remove();
514 });
515
516 $(document).on('click', '.tutorUploadAttachmentBtn', function(e){
517 e.preventDefault();
518
519 var $that = $(this);
520 var frame;
521 // If the media frame already exists, reopen it.
522 if ( frame ) {
523 frame.open();
524 return;
525 }
526 // Create a new media frame
527 frame = wp.media({
528 title: 'Select or Upload Media Of Your Chosen Persuasion',
529 button: {
530 text: 'Use this media'
531 },
532 multiple: true // Set to true to allow multiple files to be selected
533 });
534 // When an image is selected in the media frame...
535 frame.on( 'select', function() {
536 // Get media attachment details from the frame state
537 var attachments = frame.state().get('selection').toJSON();
538 if (attachments.length){
539 for (var i=0; i < attachments.length; i++){
540 var attachment = attachments[i];
541
542 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>';
543 $that.closest('.tutor-lesson-attachments-metabox').find('.tutor-added-attachments-wrap').append(inputHtml);
544 }
545 }
546 });
547 // Finally, open the modal on click
548 frame.open();
549 });
550
551 /**
552 * Open Sidebar Menu
553 */
554 if (tutor_data.open_tutor_admin_menu){
555 var $adminMenu = $('#adminmenu');
556 $adminMenu.find('[href="admin.php?page=tutor"]').closest('li.wp-has-submenu').addClass('wp-has-current-submenu');
557 $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');
558 }
559
560 /**
561 * Add question answer for quiz
562 */
563
564 $(document).on('change keyup paste', '.question_field_title', function(){
565 var $that = $(this);
566 $that.closest('.single-question-item').find('.tutor-question-item-head').find('.question-title').text($that.val());
567 });
568
569 $(document).on('change', '.question_type_field', function(){
570 var $that = $(this);
571 var question_type = $that.val();
572
573 var option_text = $that.find('option[value="'+question_type+'"]').text();
574 $that.closest('.single-question-item').find('.tutor-question-item-head').find('.question-type').text(option_text);
575
576 var question_id = $that.closest('.single-question-item').attr('data-question-id');
577 var data = {question_id: question_id, question_type : question_type, action: 'quiz_question_type_changed'};
578
579 $.ajax({
580 url : ajaxurl,
581 type : 'POST',
582 data : data,
583 beforeSend: function () {
584 $that.closest('.single-question-item').find('.tutor-loading-icon-wrap').addClass('tutor-updating-message');
585 },
586 success: function (data) {
587 if (data.success){
588 $that.closest('.quiz-question-form-wrap').find('.answer-entry-wrap').html(data.data.multi_answer_options);
589
590 if (question_type === 'true_false' && $('.answer-option-row').length >= 2){
591 $('.add_answer_option_wrap').hide();
592 }else{
593 $('.add_answer_option_wrap').show();
594 }
595 }
596 },
597 complete: function () {
598 $that.closest('.single-question-item').find('.tutor-loading-icon-wrap').removeClass('tutor-updating-message');
599 }
600 });
601 });
602
603 $(document).on('click', '.add_answer_option_btn', function(e){
604 e.preventDefault();
605
606 var $that = $(this);
607 var question_id = $that.closest('.single-question-item').attr('data-question-id');
608 var question_type = $that.closest('.quiz-question-form-wrap').find('select.question_type_field').val();
609 var data = {question_id: question_id, action: 'quiz_add_answer_to_question'};
610
611 $.ajax({
612 url : ajaxurl,
613 type : 'POST',
614 data : data,
615 beforeSend: function () {
616 $that.removeClass('updated-message').addClass('tutor-updating-message');
617 },
618 success: function (data) {
619 if (data.success){
620 $that.closest('.answer-entry-wrap').find('table.multi-answers-options').append(data.data.data_tr);
621
622 //Hide add answer button if true false and 2 option exists
623 if (question_type === 'true_false' && $that.closest('.answer-entry-wrap').find('tr.answer-option-row').length >= 2){
624 $that.closest('.add_answer_option_wrap').hide();
625 }else{
626 $that.closest('.add_answer_option_wrap').show();
627 }
628 }
629 },
630 complete: function () {
631 $that.removeClass('tutor-updating-message').addClass('updated-message');
632 }
633 });
634 });
635
636 $(document).on('click', '.add_question_btn', function(e){
637 e.preventDefault();
638
639 var $that = $(this);
640 var $title = $('[name="new_question_title"]');
641 var question_title = $title.val();
642 var question_type = $('[name="new_question_type"]').val();
643 var quiz_id = $('#post_ID').val();
644
645 //If no question title, stop here
646 if ( ! question_title.length){
647 $title.addClass('tutor-input-text-error');
648 return;
649 }else{
650 $title.removeClass('tutor-input-text-error');
651 }
652
653 var data = {question_title : question_title, question_type:question_type, quiz_id : quiz_id, action: 'quiz_page_add_new_question' };
654 $.ajax({
655 url : ajaxurl,
656 type : 'POST',
657 data : data,
658 beforeSend: function () {
659 $that.removeClass('updated-message').addClass('tutor-updating-message');
660 },
661 success: function (data) {
662 if (data.success){
663 $('.single-question-item .quiz-question-form-wrap').hide();
664 $('.tutor-quiz-questions-wrap').append(data.data.question_html);
665 $('.single-question-item:last-child .quiz-question-form-wrap').show();
666 $title.val('');
667 }
668 },
669 complete: function () {
670 $that.removeClass('tutor-updating-message').addClass('updated-message');
671 }
672 });
673 });
674
675 //Show hide question settings
676 $(document).on('click', '.question-action-btn.down', function(e){
677 e.preventDefault();
678 $(this).closest('.single-question-item').find('.quiz-question-form-wrap').toggle();
679 $(this).find('i.dashicons').toggleClass('dashicons-arrow-up-alt2 dashicons-arrow-down-alt2');
680 });
681
682 $(document).on('change', '.single-question-item', function(e){
683 e.preventDefault();
684
685 var $that = $(this);
686 var data = $(this).find("select, textarea, input").serialize()+'&action=update_tutor_question';
687 $.ajax({
688 url : ajaxurl,
689 type : 'POST',
690 data : data,
691 beforeSend: function () {
692 $that.find('.tutor-loading-icon-wrap').addClass('tutor-updating-message');
693 },
694 success: function (data) {
695 if (data.success){
696
697 }
698 },
699 complete: function () {
700 $that.find('.tutor-loading-icon-wrap').removeClass('tutor-updating-message');
701 }
702 });
703 });
704
705 $(document).on('click', '.quiz-answer-option-delete-btn', function(e){
706 e.preventDefault();
707 var $that = $(this);
708 var $closestTable = $that.closest('table');
709 var $loadingIcon = $that.closest('.single-question-item').find('.tutor-loading-icon-wrap');
710
711 var question_type = $that.closest('.quiz-question-form-wrap').find('select.question_type_field').val();
712 var answer_option_id = $that.closest('tr').attr('data-answer-option-id');
713
714 $.ajax({
715 url : ajaxurl,
716 type : 'POST',
717 data : {answer_option_id:answer_option_id, action: 'quiz_delete_answer_option'},
718 beforeSend: function () {
719 $loadingIcon.addClass('tutor-updating-message');
720 },
721 success: function (data) {
722 if (data.success){
723 $that.closest('tr').remove();
724 //Hide add answer button if true false and 2 option exists
725 if (question_type === 'true_false' && $closestTable.find('tr.answer-option-row').length >= 2){
726 $closestTable.closest('.answer-entry-wrap').find('.add_answer_option_wrap').hide();
727 }else{
728 $closestTable.closest('.answer-entry-wrap').find('.add_answer_option_wrap').show();
729 }
730 }
731 },
732 complete: function () {
733 $loadingIcon.removeClass('tutor-updating-message');
734 }
735 });
736 });
737
738 $(document).on('click', '.question-action-btn.trash', function(e){
739 e.preventDefault();
740
741 var $that = $(this);
742 var question_id = $that.closest('.single-question-item').attr('data-question-id');
743 var $loadingIcon = $that.closest('.single-question-item').find('.tutor-loading-icon-wrap');
744
745 $.ajax({
746 url : ajaxurl,
747 type : 'POST',
748 data : {question_id:question_id, action: 'quiz_question_delete'},
749 beforeSend: function () {
750 $loadingIcon.addClass('tutor-updating-message');
751 },
752 success: function (data) {
753 if (data.success){
754 $that.closest('.single-question-item').remove();
755 }
756 },
757 complete: function () {
758 $loadingIcon.removeClass('tutor-updating-message');
759 }
760 });
761 });
762
763 /**
764 * Sort quiz questions
765 */
766
767 if (jQuery().sortable) {
768 $(".tutor-quiz-questions-wrap").sortable({
769 handle: ".question-short",
770 start: function (e, ui) {
771 ui.placeholder.css('visibility', 'visible');
772 },
773 stop: function (e, ui) {
774 var questions = {};
775 $('.single-question-item').each(function(index, item){
776 var $question = $(this);
777 var question_id = parseInt($question.attr('data-question-id').match(/\d+/)[0], 10);
778 questions[index] = { 'question_id' : question_id };
779 });
780
781 $.post(ajaxurl, {questions : questions, action: 'sorting_quiz_questions'});
782 },
783 });
784 }
785
786 /**
787 * Quiz Modal
788 */
789
790 $(document).on('click', '.modal-close-btn', function(e){
791 e.preventDefault();
792 $('.tutor-modal-wrap').removeClass('show');
793 });
794 $(document).on('keyup', function(e){
795 if (e.keyCode === 27){
796 $('.tutor-modal-wrap').removeClass('show');
797 }
798 });
799
800 /*
801 $(document).on('click', '.tutor-add-quiz-btn', function(e){
802 e.preventDefault();
803
804 var $that = $(this);
805 var quiz_for_post_id = $(this).closest('.tutor_add_quiz_wrap').attr('data-add-quiz-under');
806 $.ajax({
807 url : ajaxurl,
808 type : 'POST',
809 data : {quiz_for_post_id : quiz_for_post_id, action: 'tutor_load_quiz_modal'},
810 beforeSend: function () {
811 $that.addClass('tutor-updating-message');
812 },
813 success: function (data) {
814 $('.tutor-quiz-modal-wrap .modal-container').html(data.data.output);
815 $('.tutor-quiz-modal-wrap').attr('quiz-for-post-id', quiz_for_post_id).addClass('show');
816 },
817 complete: function () {
818 $that.removeClass('tutor-updating-message');
819 }
820 });
821 });
822 */
823
824
825
826 $(document).on('click', '.tutor-add-quiz-btn', function(e){
827 e.preventDefault();
828
829 var $that = $(this);
830 var quiz_for_post_id = $(this).closest('.tutor_add_quiz_wrap').attr('data-add-quiz-under');
831 $.ajax({
832 url : ajaxurl,
833 type : 'POST',
834 data : {quiz_for_post_id : quiz_for_post_id, action: 'tutor_load_quiz_builder_modal'},
835 beforeSend: function () {
836 $that.addClass('tutor-updating-message');
837 },
838 success: function (data) {
839 $('.tutor-quiz-builder-modal-wrap .modal-container').html(data.data.output);
840 $('.tutor-quiz-builder-modal-wrap').attr('quiz-for-post-id', quiz_for_post_id).addClass('show');
841 },
842 complete: function () {
843 $that.removeClass('tutor-updating-message');
844 }
845 });
846 });
847
848 /**
849 * Quiz Builder Modal Tabs
850 */
851 $(document).on('click', '.tutor-quiz-modal-tab-item', function(e){
852 e.preventDefault();
853
854 var $that = $(this);
855
856 var $quizTitle = $('[name="quiz_title"]');
857 var quiz_title = $quizTitle.val();
858 if ( ! quiz_title){
859 $quizTitle.closest('.tutor-quiz-builder-form-row').find('.quiz_form_msg').html('<p class="quiz-form-warning">Please save the quiz' +
860 ' first</p>');
861 return;
862 }else{
863 $quizTitle.closest('.tutor-quiz-builder-form-row').find('.quiz_form_msg').html('');
864 }
865
866 var tabSelector = $that.attr('href');
867 $('.quiz-builder-tab-container').hide();
868 $(tabSelector).show();
869
870 $('a.tutor-quiz-modal-tab-item').removeClass('active');
871 $that.addClass('active');
872 });
873
874 //Next Prev Tab
875 $(document).on('click', '.quiz-modal-btn-next, .quiz-modal-btn-back', function(e){
876 e.preventDefault();
877
878 var tabSelector = $(this).attr('href');
879 $('#tutor-quiz-modal-tab-items-wrap a[href="'+tabSelector+'"]').trigger('click');
880 });
881
882 $(document).on('click', '.quiz-modal-tab-navigation-btn.quiz-modal-btn-cancel', function(e){
883 e.preventDefault();
884 $('.tutor-modal-wrap').removeClass('show');
885 });
886
887 $(document).on('click', '.quiz-modal-btn-first-step', function(e){
888 e.preventDefault();
889
890 var $that = $(this);
891 var $quizTitle = $('[name="quiz_title"]');
892 var quiz_title = $quizTitle.val();
893 var quiz_description = $('[name="quiz_description"]').val();
894
895 if ( ! quiz_title){
896 $quizTitle.closest('.tutor-quiz-builder-form-row').find('.quiz_form_msg').html('<p class="quiz-form-warning">Please enter quiz title</p>');
897 return;
898 }else{
899 $quizTitle.closest('.tutor-quiz-builder-form-row').find('.quiz_form_msg').html('');
900 }
901
902
903
904 var course_id = $('#post_ID').val();
905 var topic_id = $that.closest('.tutor-modal-wrap').attr('quiz-for-post-id');
906
907
908 if ($('#tutor_quiz_builder_quiz_id').length) {
909 /**
910 *
911 * @type {jQuery}
912 *
913 * if quiz id exists, we are sending it to update quiz
914 */
915
916 var quiz_id = $('#tutor_quiz_builder_quiz_id').val();
917 $.ajax({
918 url : ajaxurl,
919 type : 'POST',
920 data : {quiz_title:quiz_title, quiz_description: quiz_description, quiz_id : quiz_id, topic_id : topic_id, action: 'tutor_quiz_builder_quiz_update'},
921 beforeSend: function () {
922 $that.addClass('tutor-updating-message');
923 },
924 success: function (data) {
925 $('#tutor-quiz-'+quiz_id).html(data.data.output_quiz_row);
926 $('#tutor-quiz-modal-tab-items-wrap a[href="#quiz-builder-tab-questions"]').trigger('click');
927 },
928 complete: function () {
929 $that.removeClass('tutor-updating-message');
930 }
931 });
932
933 return;
934 }
935
936 $.ajax({
937 url : ajaxurl,
938 type : 'POST',
939 data : {quiz_title:quiz_title, quiz_description: quiz_description, course_id : course_id, topic_id : topic_id, action: 'tutor_create_quiz_and_load_modal'},
940 beforeSend: function () {
941 $that.addClass('tutor-updating-message');
942 },
943 success: function (data) {
944 $('.tutor-quiz-builder-modal-wrap .modal-container').html(data.data.output);
945 $('#tutor-topics-'+topic_id+' .tutor-lessons').append(data.data.output_quiz_row);
946 $('#tutor-quiz-modal-tab-items-wrap a[href="#quiz-builder-tab-questions"]').trigger('click');
947 },
948 complete: function () {
949 $that.removeClass('tutor-updating-message');
950 }
951 });
952
953 });
954
955 /**
956 * Ope modal for edit quiz
957 */
958 $(document).on('click', '.open-tutor-quiz-modal', function(e){
959 e.preventDefault();
960
961 var $that = $(this);
962 var quiz_id = $that.attr('data-quiz-id');
963 var topic_id = $that.attr('data-topic-id');
964 var course_id = $('#post_ID').val();
965
966 $.ajax({
967 url : ajaxurl,
968 type : 'POST',
969 data : {quiz_id : quiz_id, topic_id : topic_id, course_id : course_id, action: 'tutor_load_edit_quiz_modal'},
970 beforeSend: function () {
971 $that.addClass('tutor-updating-message');
972 },
973 success: function (data) {
974 $('.tutor-quiz-builder-modal-wrap .modal-container').html(data.data.output);
975 $('.tutor-quiz-builder-modal-wrap').attr('data-quiz-id', quiz_id).addClass('show');
976
977 //Back to question Tab if exists
978 if ($that.attr('data-back-to-tab')){
979 var tabSelector = $that.attr('data-back-to-tab');
980 $('#tutor-quiz-modal-tab-items-wrap a[href="'+tabSelector+'"]').trigger('click');
981 }
982 tutor_slider_init();
983 },
984 complete: function () {
985 $that.removeClass('tutor-updating-message');
986 }
987 });
988 });
989
990
991 $(document).on('click', '.quiz-modal-settings-save-btn', function(e){
992 e.preventDefault();
993
994 var $that = $(this);
995 var quiz_id = $('.tutor-quiz-builder-modal-wrap').attr('data-quiz-id');
996
997 var $formInput = $('#quiz-builder-tab-settings :input, #quiz-builder-tab-advanced-options :input').serialize()+'&quiz_id='+quiz_id+'&action=tutor_quiz_modal_update_settings';
998
999 $.ajax({
1000 url : ajaxurl,
1001 type : 'POST',
1002 data : $formInput,
1003 beforeSend: function () {
1004 $that.addClass('tutor-updating-message');
1005 },
1006 success: function (data) {
1007 //
1008 },
1009 complete: function () {
1010 $that.removeClass('tutor-updating-message');
1011 }
1012 });
1013 });
1014
1015
1016 /**
1017 * Add Question to quiz modal
1018 */
1019 $(document).on('click', '.tutor-quiz-open-question-form', function(e){
1020 e.preventDefault();
1021
1022 var $that = $(this);
1023
1024 var quiz_id = $('#tutor_quiz_builder_quiz_id').val();
1025 var course_id = $('#post_ID').val();
1026 var question_id = $that.attr('data-question-id');
1027
1028
1029 var params = {quiz_id : quiz_id, course_id : course_id, action: 'tutor_quiz_builder_get_question_form'};
1030
1031 if (question_id) {
1032 params.question_id = question_id;
1033 }
1034
1035 $.ajax({
1036 url : ajaxurl,
1037 type : 'POST',
1038 data : params,
1039 beforeSend: function () {
1040 $that.addClass('tutor-updating-message');
1041 },
1042 success: function (data) {
1043 $('.tutor-quiz-builder-modal-contents').html(data.data.output);
1044
1045 //Initializing Tutor Select
1046 tutor_select().reInit();
1047 enable_quiz_answer_sorting();
1048 },
1049 complete: function () {
1050 $that.removeClass('tutor-updating-message');
1051 }
1052 });
1053
1054 });
1055
1056 $(document).on('click', '.quiz-modal-question-save-btn', function(e){
1057 e.preventDefault();
1058
1059 var $that = $(this);
1060 var $formInput = $('.quiz_question_form :input').serialize()+'&action=tutor_quiz_modal_update_question';
1061 $.ajax({
1062 url : ajaxurl,
1063 type : 'POST',
1064 data : $formInput,
1065 beforeSend: function () {
1066 $that.addClass('tutor-updating-message');
1067 },
1068 success: function (data) {
1069 //ReOpen questions
1070 $that.closest('.quiz-questions-form').find('.open-tutor-quiz-modal').trigger('click');
1071 },
1072 complete: function () {
1073 $that.removeClass('tutor-updating-message');
1074 }
1075 });
1076 });
1077
1078 $(document).on('click', '.tutor-quiz-question-trash', function(e){
1079 e.preventDefault();
1080
1081 var $that = $(this);
1082 var question_id = $that.attr('data-question-id');
1083
1084 $.ajax({
1085 url : ajaxurl,
1086 type : 'POST',
1087 data : {question_id : question_id, action: 'tutor_quiz_builder_question_delete'},
1088 beforeSend: function () {
1089 $that.closest('.quiz-builder-question-wrap').remove();
1090 },
1091 });
1092 });
1093
1094 /**
1095 * Get question answers option form to save multiple/single/true-false options
1096 *
1097 * @since v.1.0.0
1098 */
1099
1100 $(document).on('click', '.add_question_answers_option', function(e){
1101 e.preventDefault();
1102
1103 var $that = $(this);
1104 var question_id = $that.attr('data-question-id');
1105 var $formInput = $('.quiz_question_form :input').serialize()+'&question_id='+question_id+'&action=tutor_quiz_add_question_answers';
1106
1107 $.ajax({
1108 url : ajaxurl,
1109 type : 'POST',
1110 data : $formInput,
1111 beforeSend: function () {
1112 $that.addClass('tutor-updating-message');
1113 },
1114 success: function (data) {
1115 $('#tutor_quiz_question_answer_form').html(data.data.output);
1116 },
1117 complete: function () {
1118 $that.removeClass('tutor-updating-message');
1119 }
1120 });
1121 });
1122
1123 /**
1124 * Get question answers option edit form
1125 *
1126 * @since v.1.0.0
1127 */
1128 $(document).on('click', '.tutor-quiz-answer-edit a', function(e){
1129 e.preventDefault();
1130
1131 var $that = $(this);
1132 var answer_id = $that.closest('.tutor-quiz-answer-wrap').attr('data-answer-id');
1133
1134 $.ajax({
1135 url : ajaxurl,
1136 type : 'POST',
1137 data : {answer_id : answer_id, action : 'tutor_quiz_edit_question_answer'},
1138 beforeSend: function () {
1139 $that.addClass('tutor-updating-message');
1140 },
1141 success: function (data) {
1142 $('#tutor_quiz_question_answer_form').html(data.data.output);
1143 },
1144 complete: function () {
1145 $that.removeClass('tutor-updating-message');
1146 }
1147 });
1148 });
1149
1150
1151 /**
1152 * Saving question answers options
1153 * Student should select the right answer at quiz attempts
1154 *
1155 * @since v.1.0.0
1156 */
1157
1158 $(document).on('click', '#quiz-answer-save-btn', function(e){
1159 e.preventDefault();
1160
1161 var $that = $(this);
1162 var $formInput = $('.quiz_question_form :input').serialize()+'&action=tutor_save_quiz_answer_options';
1163
1164 $.ajax({
1165 url : ajaxurl,
1166 type : 'POST',
1167 data : $formInput,
1168 beforeSend: function () {
1169 $that.addClass('tutor-updating-message');
1170 },
1171 success: function (data) {
1172 $('#tutor_quiz_question_answers').trigger('refresh');
1173 },
1174 complete: function () {
1175 $that.removeClass('tutor-updating-message');
1176 }
1177 });
1178 });
1179
1180 /**
1181 * Updating Answer
1182 *
1183 * @since v.1.0.0
1184 */
1185 $(document).on('click', '#quiz-answer-edit-btn', function(e){
1186 e.preventDefault();
1187
1188 var $that = $(this);
1189 var $formInput = $('.quiz_question_form :input').serialize()+'&action=tutor_update_quiz_answer_options';
1190
1191 $.ajax({
1192 url : ajaxurl,
1193 type : 'POST',
1194 data : $formInput,
1195 beforeSend: function () {
1196 $that.addClass('tutor-updating-message');
1197 },
1198 success: function (data) {
1199 $('#tutor_quiz_question_answers').trigger('refresh');
1200 },
1201 complete: function () {
1202 $that.removeClass('tutor-updating-message');
1203 }
1204 });
1205 });
1206
1207 $(document).on('change', '.tutor-quiz-answers-mark-correct-wrap input', function(e){
1208 e.preventDefault();
1209
1210 var $that = $(this);
1211
1212 var answer_id = $that.val();
1213 var inputValue = 1;
1214 if ( ! $that.prop('checked')) {
1215 inputValue = 0;
1216 }
1217
1218 $.ajax({
1219 url : ajaxurl,
1220 type : 'POST',
1221 data : {answer_id:answer_id, inputValue : inputValue, action : 'tutor_mark_answer_as_correct'},
1222 });
1223 });
1224
1225
1226 $(document).on('refresh', '#tutor_quiz_question_answers', function(e){
1227 e.preventDefault();
1228
1229 var $that = $(this);
1230 var question_id = $that.attr('data-question-id');
1231 var question_type = $('.tutor_select_value_holder').val();
1232
1233 $.ajax({
1234 url : ajaxurl,
1235 type : 'POST',
1236 data : {question_id : question_id, question_type : question_type, action: 'tutor_quiz_builder_get_answers_by_question'},
1237 beforeSend: function () {
1238 $that.addClass('tutor-updating-message');
1239 $('#tutor_quiz_question_answer_form').html('');
1240 },
1241 success: function (data) {
1242 if (data.success){
1243 $that.html(data.data.output);
1244 }
1245 },
1246 complete: function () {
1247 $that.removeClass('tutor-updating-message');
1248 }
1249 });
1250 });
1251
1252 /**
1253 * Delete answer for a question in quiz builder
1254 *
1255 * @since v.1.0.0
1256 */
1257
1258 $(document).on('click', '.tutor-quiz-answer-trash-wrap a.answer-trash-btn', function(e){
1259 e.preventDefault();
1260
1261 var $that = $(this);
1262 var answer_id = $that.attr('data-answer-id');
1263
1264 $.ajax({
1265 url : ajaxurl,
1266 type : 'POST',
1267 data : {answer_id : answer_id, action: 'tutor_quiz_builder_delete_answer'},
1268 beforeSend: function () {
1269 $that.closest('.tutor-quiz-answer-wrap').remove();
1270 },
1271 });
1272 });
1273
1274 /**
1275 * Save answer sorting placement
1276 *
1277 * @since v.1.0.0
1278 */
1279 function enable_quiz_answer_sorting(){
1280 if (jQuery().sortable) {
1281 $("#tutor_quiz_question_answers").sortable({
1282 handle: ".tutor-quiz-answer-sort-icon",
1283 start: function (e, ui) {
1284 ui.placeholder.css('visibility', 'visible');
1285 },
1286 stop: function (e, ui) {
1287 tutor_save_sorting_quiz_answer_order();
1288 },
1289 });
1290 }
1291 }
1292 function tutor_save_sorting_quiz_answer_order(){
1293 var answers = {};
1294 $('.tutor-quiz-answer-wrap').each(function(index, item){
1295 var $answer = $(this);
1296 var answer_id = parseInt($answer.attr('data-answer-id'), 10);
1297 answers[index] = answer_id;
1298 });
1299
1300 $.ajax({url : ajaxurl, type : 'POST',
1301 data : {sorted_answer_ids : answers, action: 'tutor_quiz_answer_sorting'},
1302 });
1303 }
1304
1305
1306
1307 /**
1308 * Deprecated, should remove
1309 * @todo: should remove this
1310 */
1311
1312 $(document).on('click', '.add_quiz_to_post_btn', function(e){
1313 e.preventDefault();
1314
1315 var $that = $(this);
1316 var $modal = $('.tutor-modal-wrap');
1317
1318 var quiz_for_post_id = $modal.attr('quiz-for-post-id');
1319 var data = $modal.find('input').serialize()+'&action=tutor_add_quiz_to_post&parent_post_id='+quiz_for_post_id;
1320
1321 $.ajax({
1322 url : ajaxurl,
1323 type : 'POST',
1324 data : data,
1325 beforeSend: function () {
1326 $that.addClass('tutor-updating-message');
1327 },
1328 success: function (data) {
1329 if (data.success){
1330 $('[data-add-quiz-under="'+quiz_for_post_id+'"] .tutor-available-quizzes').html(data.data.output);
1331 $('.tutor-modal-wrap').removeClass('show');
1332 }
1333 },
1334 complete: function () {
1335 $that.removeClass('tutor-updating-message');
1336 }
1337 });
1338 });
1339
1340 $(document).on('change keyup', '.tutor-quiz-modal-wrap .tutor-modal-search-input', function(e){
1341 e.preventDefault();
1342
1343 var $that = $(this);
1344 var $modal = $('.tutor-modal-wrap');
1345
1346 tutor_delay(function(){
1347 var search_terms = $that.val();
1348 var quiz_for_post_id = $modal.attr('quiz-for-post-id');
1349
1350 $.ajax({
1351 url : ajaxurl,
1352 type : 'POST',
1353 data : {quiz_for_post_id : quiz_for_post_id, search_terms : search_terms, action: 'tutor_load_quiz_modal'},
1354 beforeSend: function () {
1355 $modal.addClass('loading');
1356 },
1357 success: function (data) {
1358 if (data.success){
1359 $('.tutor-modal-wrap .modal-container').html(data.data.output);
1360 }
1361 },
1362 complete: function () {
1363 $modal.removeClass('loading');
1364 }
1365 });
1366
1367 }, 1000)
1368 });
1369
1370 var tutor_delay = (function(){
1371 var timer = 0;
1372 return function(callback, ms){
1373 clearTimeout (timer);
1374 timer = setTimeout(callback, ms);
1375 };
1376 })();
1377
1378 $(document).on('click', '.tutor-quiz-delete-btn', function(e){
1379 e.preventDefault();
1380
1381 var $that = $(this);
1382 var quiz_id = $that.closest('.added-quiz-item').attr('data-quiz-id');
1383
1384 $.ajax({
1385 url : ajaxurl,
1386 type : 'POST',
1387 data : {quiz_id:quiz_id, action: 'remove_quiz_from_post'},
1388 success: function (data) {
1389 if (data.success){
1390 $that.closest('.added-quiz-item').remove();
1391 }
1392 }
1393 });
1394 });
1395
1396 /**
1397 * Add instructor modal
1398 */
1399 $(document).on('click', '.tutor-add-instructor-btn', function(e){
1400 e.preventDefault();
1401
1402 var $that = $(this);
1403 var course_id = $('#post_ID').val();
1404
1405 $.ajax({
1406 url : ajaxurl,
1407 type : 'POST',
1408 data : {course_id : course_id, action: 'tutor_load_instructors_modal'},
1409 beforeSend: function () {
1410 $that.addClass('tutor-updating-message');
1411 },
1412 success: function (data) {
1413 if (data.success){
1414 $('.tutor-instructors-modal-wrap .modal-container').html(data.data.output);
1415 $('.tutor-instructors-modal-wrap').addClass('show');
1416 }
1417 },
1418 complete: function () {
1419 $that.removeClass('tutor-updating-message');
1420 }
1421 });
1422 });
1423
1424 $(document).on('change keyup', '.tutor-instructors-modal-wrap .tutor-modal-search-input', function(e){
1425 e.preventDefault();
1426
1427 var $that = $(this);
1428 var $modal = $('.tutor-modal-wrap');
1429
1430 tutor_delay(function(){
1431 var search_terms = $that.val();
1432 var course_id = $('#post_ID').val();
1433
1434 $.ajax({
1435 url : ajaxurl,
1436 type : 'POST',
1437 data : {course_id : course_id, search_terms : search_terms, action: 'tutor_load_instructors_modal'},
1438 beforeSend: function () {
1439 $modal.addClass('loading');
1440 },
1441 success: function (data) {
1442 if (data.success){
1443 $('.tutor-instructors-modal-wrap .modal-container').html(data.data.output);
1444 $('.tutor-instructors-modal-wrap').addClass('show');
1445 }
1446 },
1447 complete: function () {
1448 $modal.removeClass('loading');
1449 }
1450 });
1451
1452 }, 1000)
1453 });
1454 $(document).on('click', '.add_instructor_to_course_btn', function(e){
1455 e.preventDefault();
1456
1457 var $that = $(this);
1458 var $modal = $('.tutor-modal-wrap');
1459 var course_id = $('#post_ID').val();
1460 var data = $modal.find('input').serialize()+'&course_id='+course_id+'&action=tutor_add_instructors_to_course';
1461
1462 $.ajax({
1463 url : ajaxurl,
1464 type : 'POST',
1465 data : data,
1466 beforeSend: function () {
1467 $that.addClass('tutor-updating-message');
1468 },
1469 success: function (data) {
1470 if (data.success){
1471 $('.tutor-course-available-instructors').html(data.data.output);
1472 $('.tutor-modal-wrap').removeClass('show');
1473 }
1474 },
1475 complete: function () {
1476 $that.removeClass('tutor-updating-message');
1477 }
1478 });
1479 });
1480
1481 $(document).on('click', '.tutor-instructor-delete-btn', function(e){
1482 e.preventDefault();
1483
1484 var $that = $(this);
1485 var course_id = $('#post_ID').val();
1486 var instructor_id = $that.closest('.added-instructor-item').attr('data-instructor-id');
1487
1488 $.ajax({
1489 url : ajaxurl,
1490 type : 'POST',
1491 data : {course_id:course_id, instructor_id:instructor_id, action : 'detach_instructor_from_course'},
1492 success: function (data) {
1493 if (data.success){
1494 $that.closest('.added-instructor-item').remove();
1495 }
1496 }
1497 });
1498 });
1499
1500 $(document).on('click', '.tutor-option-media-upload-btn', function(e){
1501 e.preventDefault();
1502
1503 var $that = $(this);
1504 var frame;
1505 if ( frame ) {
1506 frame.open();
1507 return;
1508 }
1509 frame = wp.media({
1510 title: 'Select or Upload Media Of Your Chosen Persuasion',
1511 button: {
1512 text: 'Use this media'
1513 },
1514 multiple: false
1515 });
1516 frame.on( 'select', function() {
1517 var attachment = frame.state().get('selection').first().toJSON();
1518 $that.closest('.option-media-wrap').find('.option-media-preview').html('<img src="'+attachment.url+'" alt="" />');
1519 $that.closest('.option-media-wrap').find('input').val(attachment.id);
1520 });
1521 frame.open();
1522 });
1523
1524 $(document).on('change', '.tutor_addons_list_item', function(e) {
1525 var $that = $(this);
1526
1527 var isEnable = $that.prop('checked') ? 1 : 0;
1528 var addonFieldName = $that.attr('name');
1529
1530 $.ajax({
1531 url : ajaxurl,
1532 type : 'POST',
1533 data : {isEnable:isEnable, addonFieldName:addonFieldName, action : 'addon_enable_disable'},
1534 success: function (data) {
1535 if (data.success){
1536 //Success
1537 }
1538 }
1539 });
1540 });
1541
1542 /**
1543 * Tutor Custom Select
1544 */
1545
1546 function tutor_select(){
1547 var obj = {
1548 init : function(){
1549 $(document).on('click', '.tutor-select .tutor-select-option', function(e){
1550 e.preventDefault();
1551
1552 var $that = $(this);
1553 var $html = $that.html().trim();
1554 $that.closest('.tutor-select').find('.select-header .lead-option').html($html);
1555 $that.closest('.tutor-select').find('.select-header input.tutor_select_value_holder').val($that.attr('data-value')).trigger('change');
1556 $that.closest('.tutor-select-options').hide();
1557 });
1558 $(document).on('click', '.tutor-select .select-header', function(e){
1559 e.preventDefault();
1560
1561 var $that = $(this);
1562 $that.closest('.tutor-select').find('.tutor-select-options').slideToggle();
1563 });
1564
1565 this.setValue();
1566 this.hideOnOutSideClick();
1567 },
1568 setValue : function(){
1569 $('.tutor-select').each(function(){
1570 var $that = $(this);
1571 var $option = $that.find('.tutor-select-option');
1572
1573 if ($option.length){
1574 $option.each(function(){
1575 var $thisOption = $(this);
1576
1577 if ($thisOption.attr('data-selected') === 'selected'){
1578 var $html = $thisOption.html().trim();
1579 $thisOption.closest('.tutor-select').find('.select-header .lead-option').html($html);
1580 $thisOption.closest('.tutor-select').find('.select-header input.tutor_select_value_holder').val($thisOption.attr('data-value'));
1581 }
1582 });
1583 }
1584 });
1585 },
1586 hideOnOutSideClick : function(){
1587 $(document).mouseup(function(e) {
1588 var $option_wrap = $(".tutor-select-options");
1589 if ( ! $(e.target).closest('.select-header').length && !$option_wrap.is(e.target) && $option_wrap.has(e.target).length === 0) {
1590 $option_wrap.hide();
1591 }
1592 });
1593 },
1594 reInit : function(){
1595 this.setValue();
1596 }
1597 };
1598
1599 return obj;
1600 }
1601 tutor_select().init();
1602
1603 /**
1604 * If change question type from quiz builder question
1605 *
1606 * @since v.1.0.0
1607 */
1608 $(document).on('change', 'input.tutor_select_value_holder', function(e) {
1609 var $that = $(this);
1610 //$('#tutor_quiz_question_answer_form').html('');
1611 $('.add_question_answers_option').trigger('click');
1612 $('#tutor_quiz_question_answers').trigger('refresh');
1613 });
1614
1615 $(document).on('click', '.tutor-media-upload-btn', function(e){
1616 e.preventDefault();
1617
1618 var $that = $(this);
1619 var frame;
1620 if ( frame ) {
1621 frame.open();
1622 return;
1623 }
1624 frame = wp.media({
1625 title: 'Select or Upload Media Of Your Chosen Persuasion',
1626 button: {
1627 text: 'Use this media'
1628 },
1629 multiple: false
1630 });
1631 frame.on( 'select', function() {
1632 var attachment = frame.state().get('selection').first().toJSON();
1633 $that.html('<img src="'+attachment.url+'" alt="" />');
1634 $that.closest('.tutor-media-upload-wrap').find('input').val(attachment.id);
1635 });
1636 frame.open();
1637 });
1638 $(document).on('click', '.tutor-media-upload-trash', function(e){
1639 e.preventDefault();
1640
1641 var $that = $(this);
1642 $that.closest('.tutor-media-upload-wrap').find('.tutor-media-upload-btn').html('<i class="tutor-icon-image1"></i>');
1643 $that.closest('.tutor-media-upload-wrap').find('input').val('');
1644 });
1645
1646 });
1647