PluginProbe ʕ •ᴥ•ʔ
Tutor LMS – eLearning and online course solution / 1.0.2
Tutor LMS – eLearning and online course solution v1.0.2
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
1681 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 enable_quiz_questions_sorting();
984 },
985 complete: function () {
986 $that.removeClass('tutor-updating-message');
987 }
988 });
989 });
990
991
992 $(document).on('click', '.quiz-modal-settings-save-btn', function(e){
993 e.preventDefault();
994
995 var $that = $(this);
996 var quiz_id = $('.tutor-quiz-builder-modal-wrap').attr('data-quiz-id');
997
998 var $formInput = $('#quiz-builder-tab-settings :input, #quiz-builder-tab-advanced-options :input').serialize()+'&quiz_id='+quiz_id+'&action=tutor_quiz_modal_update_settings';
999
1000 $.ajax({
1001 url : ajaxurl,
1002 type : 'POST',
1003 data : $formInput,
1004 beforeSend: function () {
1005 $that.addClass('tutor-updating-message');
1006 },
1007 success: function (data) {
1008 //
1009 },
1010 complete: function () {
1011 $that.removeClass('tutor-updating-message');
1012 if ($that.attr('data-action') === 'modal_close'){
1013 $('.tutor-modal-wrap').removeClass('show');
1014 }
1015 }
1016 });
1017 });
1018
1019
1020 /**
1021 * Add Question to quiz modal
1022 */
1023 $(document).on('click', '.tutor-quiz-open-question-form', function(e){
1024 e.preventDefault();
1025
1026 var $that = $(this);
1027
1028 var quiz_id = $('#tutor_quiz_builder_quiz_id').val();
1029 var course_id = $('#post_ID').val();
1030 var question_id = $that.attr('data-question-id');
1031
1032
1033 var params = {quiz_id : quiz_id, course_id : course_id, action: 'tutor_quiz_builder_get_question_form'};
1034
1035 if (question_id) {
1036 params.question_id = question_id;
1037 }
1038
1039 $.ajax({
1040 url : ajaxurl,
1041 type : 'POST',
1042 data : params,
1043 beforeSend: function () {
1044 $that.addClass('tutor-updating-message');
1045 },
1046 success: function (data) {
1047 $('.tutor-quiz-builder-modal-contents').html(data.data.output);
1048
1049 //Initializing Tutor Select
1050 tutor_select().reInit();
1051 enable_quiz_answer_sorting();
1052 },
1053 complete: function () {
1054 $that.removeClass('tutor-updating-message');
1055 }
1056 });
1057
1058 });
1059
1060 $(document).on('click', '.quiz-modal-question-save-btn', function(e){
1061 e.preventDefault();
1062
1063 var $that = $(this);
1064 var $formInput = $('.quiz_question_form :input').serialize()+'&action=tutor_quiz_modal_update_question';
1065 $.ajax({
1066 url : ajaxurl,
1067 type : 'POST',
1068 data : $formInput,
1069 beforeSend: function () {
1070 $that.addClass('tutor-updating-message');
1071 },
1072 success: function (data) {
1073 //ReOpen questions
1074 $that.closest('.quiz-questions-form').find('.open-tutor-quiz-modal').trigger('click');
1075 },
1076 complete: function () {
1077 $that.removeClass('tutor-updating-message');
1078 }
1079 });
1080 });
1081
1082 $(document).on('click', '.tutor-quiz-question-trash', function(e){
1083 e.preventDefault();
1084
1085 var $that = $(this);
1086 var question_id = $that.attr('data-question-id');
1087
1088 $.ajax({
1089 url : ajaxurl,
1090 type : 'POST',
1091 data : {question_id : question_id, action: 'tutor_quiz_builder_question_delete'},
1092 beforeSend: function () {
1093 $that.closest('.quiz-builder-question-wrap').remove();
1094 },
1095 });
1096 });
1097
1098 /**
1099 * Get question answers option form to save multiple/single/true-false options
1100 *
1101 * @since v.1.0.0
1102 */
1103
1104 $(document).on('click', '.add_question_answers_option', function(e){
1105 e.preventDefault();
1106
1107 var $that = $(this);
1108 var question_id = $that.attr('data-question-id');
1109 var $formInput = $('.quiz_question_form :input').serialize()+'&question_id='+question_id+'&action=tutor_quiz_add_question_answers';
1110
1111 $.ajax({
1112 url : ajaxurl,
1113 type : 'POST',
1114 data : $formInput,
1115 beforeSend: function () {
1116 $that.addClass('tutor-updating-message');
1117 },
1118 success: function (data) {
1119 $('#tutor_quiz_question_answer_form').html(data.data.output);
1120 },
1121 complete: function () {
1122 $that.removeClass('tutor-updating-message');
1123 }
1124 });
1125 });
1126
1127 /**
1128 * Get question answers option edit form
1129 *
1130 * @since v.1.0.0
1131 */
1132 $(document).on('click', '.tutor-quiz-answer-edit a', function(e){
1133 e.preventDefault();
1134
1135 var $that = $(this);
1136 var answer_id = $that.closest('.tutor-quiz-answer-wrap').attr('data-answer-id');
1137
1138 $.ajax({
1139 url : ajaxurl,
1140 type : 'POST',
1141 data : {answer_id : answer_id, action : 'tutor_quiz_edit_question_answer'},
1142 beforeSend: function () {
1143 $that.addClass('tutor-updating-message');
1144 },
1145 success: function (data) {
1146 $('#tutor_quiz_question_answer_form').html(data.data.output);
1147 },
1148 complete: function () {
1149 $that.removeClass('tutor-updating-message');
1150 }
1151 });
1152 });
1153
1154
1155 /**
1156 * Saving question answers options
1157 * Student should select the right answer at quiz attempts
1158 *
1159 * @since v.1.0.0
1160 */
1161
1162 $(document).on('click', '#quiz-answer-save-btn', function(e){
1163 e.preventDefault();
1164
1165 var $that = $(this);
1166 var $formInput = $('.quiz_question_form :input').serialize()+'&action=tutor_save_quiz_answer_options';
1167
1168 $.ajax({
1169 url : ajaxurl,
1170 type : 'POST',
1171 data : $formInput,
1172 beforeSend: function () {
1173 $that.addClass('tutor-updating-message');
1174 },
1175 success: function (data) {
1176 $('#tutor_quiz_question_answers').trigger('refresh');
1177 },
1178 complete: function () {
1179 $that.removeClass('tutor-updating-message');
1180 }
1181 });
1182 });
1183
1184 /**
1185 * Updating Answer
1186 *
1187 * @since v.1.0.0
1188 */
1189 $(document).on('click', '#quiz-answer-edit-btn', function(e){
1190 e.preventDefault();
1191
1192 var $that = $(this);
1193 var $formInput = $('.quiz_question_form :input').serialize()+'&action=tutor_update_quiz_answer_options';
1194
1195 $.ajax({
1196 url : ajaxurl,
1197 type : 'POST',
1198 data : $formInput,
1199 beforeSend: function () {
1200 $that.addClass('tutor-updating-message');
1201 },
1202 success: function (data) {
1203 $('#tutor_quiz_question_answers').trigger('refresh');
1204 },
1205 complete: function () {
1206 $that.removeClass('tutor-updating-message');
1207 }
1208 });
1209 });
1210
1211 $(document).on('change', '.tutor-quiz-answers-mark-correct-wrap input', function(e){
1212 e.preventDefault();
1213
1214 var $that = $(this);
1215
1216 var answer_id = $that.val();
1217 var inputValue = 1;
1218 if ( ! $that.prop('checked')) {
1219 inputValue = 0;
1220 }
1221
1222 $.ajax({
1223 url : ajaxurl,
1224 type : 'POST',
1225 data : {answer_id:answer_id, inputValue : inputValue, action : 'tutor_mark_answer_as_correct'},
1226 });
1227 });
1228
1229
1230 $(document).on('refresh', '#tutor_quiz_question_answers', function(e){
1231 e.preventDefault();
1232
1233 var $that = $(this);
1234 var question_id = $that.attr('data-question-id');
1235 var question_type = $('.tutor_select_value_holder').val();
1236
1237 $.ajax({
1238 url : ajaxurl,
1239 type : 'POST',
1240 data : {question_id : question_id, question_type : question_type, action: 'tutor_quiz_builder_get_answers_by_question'},
1241 beforeSend: function () {
1242 $that.addClass('tutor-updating-message');
1243 $('#tutor_quiz_question_answer_form').html('');
1244 },
1245 success: function (data) {
1246 if (data.success){
1247 $that.html(data.data.output);
1248 }
1249 },
1250 complete: function () {
1251 $that.removeClass('tutor-updating-message');
1252 }
1253 });
1254 });
1255
1256 /**
1257 * Delete answer for a question in quiz builder
1258 *
1259 * @since v.1.0.0
1260 */
1261
1262 $(document).on('click', '.tutor-quiz-answer-trash-wrap a.answer-trash-btn', function(e){
1263 e.preventDefault();
1264
1265 var $that = $(this);
1266 var answer_id = $that.attr('data-answer-id');
1267
1268 $.ajax({
1269 url : ajaxurl,
1270 type : 'POST',
1271 data : {answer_id : answer_id, action: 'tutor_quiz_builder_delete_answer'},
1272 beforeSend: function () {
1273 $that.closest('.tutor-quiz-answer-wrap').remove();
1274 },
1275 });
1276 });
1277
1278 /**
1279 * Sort quiz questions
1280 */
1281 function enable_quiz_questions_sorting(){
1282 if (jQuery().sortable) {
1283 $(".quiz-builder-questions-wrap").sortable({
1284 handle: ".question-sorting",
1285 start: function (e, ui) {
1286 ui.placeholder.css('visibility', 'visible');
1287 },
1288 stop: function (e, ui) {
1289 tutor_save_sorting_quiz_questions_order();
1290 },
1291 });
1292 }
1293 }
1294
1295 function tutor_save_sorting_quiz_questions_order(){
1296 var questions = {};
1297 $('.quiz-builder-question-wrap').each(function(index, item){
1298 var $question = $(this);
1299 var question_id = parseInt($question.attr('data-question-id'), 10);
1300 questions[index] = question_id;
1301 });
1302
1303 $.ajax({url : ajaxurl, type : 'POST',
1304 data : {sorted_question_ids : questions, action: 'tutor_quiz_question_sorting'},
1305 });
1306 }
1307
1308 /**
1309 * Save answer sorting placement
1310 *
1311 * @since v.1.0.0
1312 */
1313 function enable_quiz_answer_sorting(){
1314 if (jQuery().sortable) {
1315 $("#tutor_quiz_question_answers").sortable({
1316 handle: ".tutor-quiz-answer-sort-icon",
1317 start: function (e, ui) {
1318 ui.placeholder.css('visibility', 'visible');
1319 },
1320 stop: function (e, ui) {
1321 tutor_save_sorting_quiz_answer_order();
1322 },
1323 });
1324 }
1325 }
1326 function tutor_save_sorting_quiz_answer_order(){
1327 var answers = {};
1328 $('.tutor-quiz-answer-wrap').each(function(index, item){
1329 var $answer = $(this);
1330 var answer_id = parseInt($answer.attr('data-answer-id'), 10);
1331 answers[index] = answer_id;
1332 });
1333
1334 $.ajax({url : ajaxurl, type : 'POST',
1335 data : {sorted_answer_ids : answers, action: 'tutor_quiz_answer_sorting'},
1336 });
1337 }
1338
1339
1340
1341 /**
1342 * Deprecated, should remove
1343 * @todo: should remove this
1344 */
1345
1346 $(document).on('click', '.add_quiz_to_post_btn', function(e){
1347 e.preventDefault();
1348
1349 var $that = $(this);
1350 var $modal = $('.tutor-modal-wrap');
1351
1352 var quiz_for_post_id = $modal.attr('quiz-for-post-id');
1353 var data = $modal.find('input').serialize()+'&action=tutor_add_quiz_to_post&parent_post_id='+quiz_for_post_id;
1354
1355 $.ajax({
1356 url : ajaxurl,
1357 type : 'POST',
1358 data : data,
1359 beforeSend: function () {
1360 $that.addClass('tutor-updating-message');
1361 },
1362 success: function (data) {
1363 if (data.success){
1364 $('[data-add-quiz-under="'+quiz_for_post_id+'"] .tutor-available-quizzes').html(data.data.output);
1365 $('.tutor-modal-wrap').removeClass('show');
1366 }
1367 },
1368 complete: function () {
1369 $that.removeClass('tutor-updating-message');
1370 }
1371 });
1372 });
1373
1374 $(document).on('change keyup', '.tutor-quiz-modal-wrap .tutor-modal-search-input', function(e){
1375 e.preventDefault();
1376
1377 var $that = $(this);
1378 var $modal = $('.tutor-modal-wrap');
1379
1380 tutor_delay(function(){
1381 var search_terms = $that.val();
1382 var quiz_for_post_id = $modal.attr('quiz-for-post-id');
1383
1384 $.ajax({
1385 url : ajaxurl,
1386 type : 'POST',
1387 data : {quiz_for_post_id : quiz_for_post_id, search_terms : search_terms, action: 'tutor_load_quiz_modal'},
1388 beforeSend: function () {
1389 $modal.addClass('loading');
1390 },
1391 success: function (data) {
1392 if (data.success){
1393 $('.tutor-modal-wrap .modal-container').html(data.data.output);
1394 }
1395 },
1396 complete: function () {
1397 $modal.removeClass('loading');
1398 }
1399 });
1400
1401 }, 1000)
1402 });
1403
1404 var tutor_delay = (function(){
1405 var timer = 0;
1406 return function(callback, ms){
1407 clearTimeout (timer);
1408 timer = setTimeout(callback, ms);
1409 };
1410 })();
1411
1412 $(document).on('click', '.tutor-quiz-delete-btn', function(e){
1413 e.preventDefault();
1414
1415 var $that = $(this);
1416 var quiz_id = $that.closest('.added-quiz-item').attr('data-quiz-id');
1417
1418 $.ajax({
1419 url : ajaxurl,
1420 type : 'POST',
1421 data : {quiz_id:quiz_id, action: 'remove_quiz_from_post'},
1422 success: function (data) {
1423 if (data.success){
1424 $that.closest('.added-quiz-item').remove();
1425 }
1426 }
1427 });
1428 });
1429
1430 /**
1431 * Add instructor modal
1432 */
1433 $(document).on('click', '.tutor-add-instructor-btn', function(e){
1434 e.preventDefault();
1435
1436 var $that = $(this);
1437 var course_id = $('#post_ID').val();
1438
1439 $.ajax({
1440 url : ajaxurl,
1441 type : 'POST',
1442 data : {course_id : course_id, action: 'tutor_load_instructors_modal'},
1443 beforeSend: function () {
1444 $that.addClass('tutor-updating-message');
1445 },
1446 success: function (data) {
1447 if (data.success){
1448 $('.tutor-instructors-modal-wrap .modal-container').html(data.data.output);
1449 $('.tutor-instructors-modal-wrap').addClass('show');
1450 }
1451 },
1452 complete: function () {
1453 $that.removeClass('tutor-updating-message');
1454 }
1455 });
1456 });
1457
1458 $(document).on('change keyup', '.tutor-instructors-modal-wrap .tutor-modal-search-input', function(e){
1459 e.preventDefault();
1460
1461 var $that = $(this);
1462 var $modal = $('.tutor-modal-wrap');
1463
1464 tutor_delay(function(){
1465 var search_terms = $that.val();
1466 var course_id = $('#post_ID').val();
1467
1468 $.ajax({
1469 url : ajaxurl,
1470 type : 'POST',
1471 data : {course_id : course_id, search_terms : search_terms, action: 'tutor_load_instructors_modal'},
1472 beforeSend: function () {
1473 $modal.addClass('loading');
1474 },
1475 success: function (data) {
1476 if (data.success){
1477 $('.tutor-instructors-modal-wrap .modal-container').html(data.data.output);
1478 $('.tutor-instructors-modal-wrap').addClass('show');
1479 }
1480 },
1481 complete: function () {
1482 $modal.removeClass('loading');
1483 }
1484 });
1485
1486 }, 1000)
1487 });
1488 $(document).on('click', '.add_instructor_to_course_btn', function(e){
1489 e.preventDefault();
1490
1491 var $that = $(this);
1492 var $modal = $('.tutor-modal-wrap');
1493 var course_id = $('#post_ID').val();
1494 var data = $modal.find('input').serialize()+'&course_id='+course_id+'&action=tutor_add_instructors_to_course';
1495
1496 $.ajax({
1497 url : ajaxurl,
1498 type : 'POST',
1499 data : data,
1500 beforeSend: function () {
1501 $that.addClass('tutor-updating-message');
1502 },
1503 success: function (data) {
1504 if (data.success){
1505 $('.tutor-course-available-instructors').html(data.data.output);
1506 $('.tutor-modal-wrap').removeClass('show');
1507 }
1508 },
1509 complete: function () {
1510 $that.removeClass('tutor-updating-message');
1511 }
1512 });
1513 });
1514
1515 $(document).on('click', '.tutor-instructor-delete-btn', function(e){
1516 e.preventDefault();
1517
1518 var $that = $(this);
1519 var course_id = $('#post_ID').val();
1520 var instructor_id = $that.closest('.added-instructor-item').attr('data-instructor-id');
1521
1522 $.ajax({
1523 url : ajaxurl,
1524 type : 'POST',
1525 data : {course_id:course_id, instructor_id:instructor_id, action : 'detach_instructor_from_course'},
1526 success: function (data) {
1527 if (data.success){
1528 $that.closest('.added-instructor-item').remove();
1529 }
1530 }
1531 });
1532 });
1533
1534 $(document).on('click', '.tutor-option-media-upload-btn', function(e){
1535 e.preventDefault();
1536
1537 var $that = $(this);
1538 var frame;
1539 if ( frame ) {
1540 frame.open();
1541 return;
1542 }
1543 frame = wp.media({
1544 title: 'Select or Upload Media Of Your Chosen Persuasion',
1545 button: {
1546 text: 'Use this media'
1547 },
1548 multiple: false
1549 });
1550 frame.on( 'select', function() {
1551 var attachment = frame.state().get('selection').first().toJSON();
1552 $that.closest('.option-media-wrap').find('.option-media-preview').html('<img src="'+attachment.url+'" alt="" />');
1553 $that.closest('.option-media-wrap').find('input').val(attachment.id);
1554 });
1555 frame.open();
1556 });
1557
1558 $(document).on('change', '.tutor_addons_list_item', function(e) {
1559 var $that = $(this);
1560
1561 var isEnable = $that.prop('checked') ? 1 : 0;
1562 var addonFieldName = $that.attr('name');
1563
1564 $.ajax({
1565 url : ajaxurl,
1566 type : 'POST',
1567 data : {isEnable:isEnable, addonFieldName:addonFieldName, action : 'addon_enable_disable'},
1568 success: function (data) {
1569 if (data.success){
1570 //Success
1571 }
1572 }
1573 });
1574 });
1575
1576 /**
1577 * Tutor Custom Select
1578 */
1579
1580 function tutor_select(){
1581 var obj = {
1582 init : function(){
1583 $(document).on('click', '.tutor-select .tutor-select-option', function(e){
1584 e.preventDefault();
1585
1586 var $that = $(this);
1587 var $html = $that.html().trim();
1588 $that.closest('.tutor-select').find('.select-header .lead-option').html($html);
1589 $that.closest('.tutor-select').find('.select-header input.tutor_select_value_holder').val($that.attr('data-value')).trigger('change');
1590 $that.closest('.tutor-select-options').hide();
1591 });
1592 $(document).on('click', '.tutor-select .select-header', function(e){
1593 e.preventDefault();
1594
1595 var $that = $(this);
1596 $that.closest('.tutor-select').find('.tutor-select-options').slideToggle();
1597 });
1598
1599 this.setValue();
1600 this.hideOnOutSideClick();
1601 },
1602 setValue : function(){
1603 $('.tutor-select').each(function(){
1604 var $that = $(this);
1605 var $option = $that.find('.tutor-select-option');
1606
1607 if ($option.length){
1608 $option.each(function(){
1609 var $thisOption = $(this);
1610
1611 if ($thisOption.attr('data-selected') === 'selected'){
1612 var $html = $thisOption.html().trim();
1613 $thisOption.closest('.tutor-select').find('.select-header .lead-option').html($html);
1614 $thisOption.closest('.tutor-select').find('.select-header input.tutor_select_value_holder').val($thisOption.attr('data-value'));
1615 }
1616 });
1617 }
1618 });
1619 },
1620 hideOnOutSideClick : function(){
1621 $(document).mouseup(function(e) {
1622 var $option_wrap = $(".tutor-select-options");
1623 if ( ! $(e.target).closest('.select-header').length && !$option_wrap.is(e.target) && $option_wrap.has(e.target).length === 0) {
1624 $option_wrap.hide();
1625 }
1626 });
1627 },
1628 reInit : function(){
1629 this.setValue();
1630 }
1631 };
1632
1633 return obj;
1634 }
1635 tutor_select().init();
1636
1637 /**
1638 * If change question type from quiz builder question
1639 *
1640 * @since v.1.0.0
1641 */
1642 $(document).on('change', 'input.tutor_select_value_holder', function(e) {
1643 var $that = $(this);
1644 //$('#tutor_quiz_question_answer_form').html('');
1645 $('.add_question_answers_option').trigger('click');
1646 $('#tutor_quiz_question_answers').trigger('refresh');
1647 });
1648
1649 $(document).on('click', '.tutor-media-upload-btn', function(e){
1650 e.preventDefault();
1651
1652 var $that = $(this);
1653 var frame;
1654 if ( frame ) {
1655 frame.open();
1656 return;
1657 }
1658 frame = wp.media({
1659 title: 'Select or Upload Media Of Your Chosen Persuasion',
1660 button: {
1661 text: 'Use this media'
1662 },
1663 multiple: false
1664 });
1665 frame.on( 'select', function() {
1666 var attachment = frame.state().get('selection').first().toJSON();
1667 $that.html('<img src="'+attachment.url+'" alt="" />');
1668 $that.closest('.tutor-media-upload-wrap').find('input').val(attachment.id);
1669 });
1670 frame.open();
1671 });
1672 $(document).on('click', '.tutor-media-upload-trash', function(e){
1673 e.preventDefault();
1674
1675 var $that = $(this);
1676 $that.closest('.tutor-media-upload-wrap').find('.tutor-media-upload-btn').html('<i class="tutor-icon-image1"></i>');
1677 $that.closest('.tutor-media-upload-wrap').find('input').val('');
1678 });
1679
1680 });
1681