class-learndash-admin-question-edit.php
33.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
<?php
/**
* LearnDash Admin Question Edit.
*
* @since 2.6.0
* @package LearnDash\Question\Edit
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
if ( ( class_exists( 'Learndash_Admin_Post_Edit' ) ) && ( ! class_exists( 'Learndash_Admin_Question_Edit' ) ) ) {
/**
* Class LearnDash Admin Question Edit.
*
* @since 2.6.0
* @uses Learndash_Admin_Post_Edit
*/
class Learndash_Admin_Question_Edit extends Learndash_Admin_Post_Edit {
/**
* WPProQuiz Question instance.
* This is used to bridge the WPProQuiz to WP systems.
*
* @var object $pro_question_edit WPProQuiz instance.
*/
private $pro_question_edit = null;
/**
* WPProQuiz View QuestionEdit instance.
*
* @var WpProQuiz_View_QuestionEdit
*/
private $view = null;
/**
* Public constructor for class.
*
* @since 2.6.0
*/
public function __construct() {
$this->post_type = learndash_get_post_type_slug( 'question' );
parent::__construct();
}
/**
* On Load handler function for this post type edit.
* This function is called by a WP action when the admin
* page 'post.php' or 'post-new.php' are loaded.
*
* @since 2.6.0
*/
public function on_load() {
if ( $this->post_type_check() ) {
parent::on_load();
wp_enqueue_script( 'media-upload' );
wp_enqueue_script( 'thickbox' );
$wpproquiz_controller_admin = new WpProQuiz_Controller_Admin();
$wpproquiz_controller_admin->enqueueScript();
add_action( 'admin_footer', array( $this, 'admin_footer' ) );
}
}
/**
* Hook into the WP admin footer logic to add custom JavaScript to replace the default page title.
*
* @since 2.6.0
*/
public function admin_footer() {
global $post_type, $post_type_object;
if ( ( ! is_admin() ) || ( $post_type !== $this->post_type ) ) {
return;
}
if ( isset( $_GET['quiz_id'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
$quiz_id = absint( $_GET['quiz_id'] ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended
if ( ! empty( $quiz_id ) ) {
$quizzes_url = add_query_arg( 'post_type', learndash_get_post_type_slug( 'quiz' ), admin_url( 'edit.php' ) );
$new_title = '<a href="' . $quizzes_url . '">' . LearnDash_Custom_Label::get_label( 'quizzes' ) . '</a> > <a href="' . get_edit_post_link( $quiz_id ) . '">' . get_the_title( $quiz_id ) . '</a> ';
$post_new_file = add_query_arg(
array(
'post_type' => $post_type,
'quiz_id' => $quiz_id,
),
'post-new.php'
);
$add_new_url = admin_url( $post_new_file );
?>
<script>
jQuery( function() {
jQuery('h1.wp-heading-inline').html('<?php echo wp_kses_post( $new_title ); ?>');
jQuery('a.page-title-action').attr( 'href', '<?php echo esc_url( $add_new_url ); ?>' );
});
</script>
<?php
}
}
}
/**
* Filter the SFWD display options logic to set a default value for the Quiz.
*
* @since 2.6.0
*
* @param array $options Array of settings values for the current post_type.
*
* @return array of $options.
*/
public function display_options( $options = array() ) {
global $pagenow;
if ( 'post-new.php' === $pagenow ) {
if ( ( isset( $_GET['quiz_id'] ) ) && ( ! empty( $_GET['quiz_id'] ) ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
$quiz_id = absint( $_GET['quiz_id'] ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended
$options[ $this->post_type . '_quiz' ] = $quiz_id;
}
}
return $options;
}
/** This function is documented in includes/admin/class-learndash-admin-posts-edit.php */
// phpcs:ignore Squiz.Commenting.FunctionComment
public function edit_post_post_type( $post_id = 0, $post = null ) {
$post_id = absint( $post_id );
if ( ! $this->post_type_check( $post ) ) {
return false;
}
if ( ! empty( $post_id ) ) {
$question_pro_id = (int) get_Post_meta( $post_id, 'question_pro_id', true );
if ( ! empty( $question_pro_id ) ) {
$question_mapper = new WpProQuiz_Model_QuestionMapper();
$question_pro = $question_mapper->fetch( $question_pro_id );
if ( ( is_a( $question_pro, 'WpProQuiz_Model_Question' ) ) && ( $question_pro_id === $question_pro->getId() ) ) {
$question_pro->setTitle( $post->post_title );
$question_pro = $question_mapper->save( $question_pro );
}
}
}
}
/**
* Initialize the ProQuiz Question being edited.
*
* @since 2.6.0
*
* @param object $post WP_Post Question being edited.
*/
public function init_question_edit( $post ) {
global $pagenow;
if ( 'post-new.php' === $pagenow ) {
add_filter( $this->post_type . '_display_options', array( $this, 'display_options' ) );
}
if ( ! is_a( $this->pro_question_edit, 'WpProQuiz_Model_Question' ) ) {
$question_pro_id = (int) get_post_meta( $post->ID, 'question_pro_id', true );
$question_mapper = new WpProQuiz_Model_QuestionMapper();
if ( ! empty( $question_pro_id ) ) {
$this->pro_question_edit = $question_mapper->fetch( $question_pro_id );
if ( ( $this->pro_question_edit ) && is_a( $this->pro_question_edit, 'WpProQuiz_Model_Question' ) ) {
$post->post_title = $this->pro_question_edit->getTitle();
$post->post_content = $this->pro_question_edit->getQuestion();
}
} else {
$this->pro_question_edit = $question_mapper->fetch( null );
}
if ( ( isset( $_GET['templateLoadId'] ) ) && ( ! empty( $_GET['templateLoadId'] ) ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
$template_mapper = new WpProQuiz_Model_TemplateMapper();
$template = $template_mapper->fetchById( absint( $_GET['templateLoadId'] ) ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended
if ( ( $template ) && ( is_a( $template, 'WpProQuiz_Model_Template' ) ) ) {
$data = $template->getData();
if ( null !== $data ) {
$data['question']->setId( $this->pro_question_edit->getId() );
$data['question']->setQuizId( $this->pro_question_edit->getQuizId() );
$this->pro_question_edit = $data['question'];
$post->post_title = $this->pro_question_edit->getTitle();
$post->post_content = $this->pro_question_edit->getQuestion();
}
}
}
}
}
/**
* Save Question handler function.
*
* @since 2.6.0
*
* @param integer $post_id Post ID Question being edited.
* @param object $post WP_Post Question being edited.
* @param boolean $update If update true, else false.
*/
public function save_post( $post_id = 0, $post = null, $update = false ) {
if ( ! $this->post_type_check( $post ) ) {
return false;
}
if ( ! parent::save_post( $post_id, $post, $update ) ) {
return false;
}
$post_data = $this->clear_request_data( $_POST ); // phpcs:ignore WordPress.Security.NonceVerification.Missing
$question_pro_id = get_post_meta( $post_id, 'question_pro_id', true );
if ( ! empty( $question_pro_id ) ) {
$question_pro_id = absint( $question_pro_id );
} else {
$question_pro_id = 0;
}
$question_pro_id_new = learndash_update_pro_question( $question_pro_id, $post_data );
if ( ( ! empty( $question_pro_id_new ) ) && ( ( absint( $question_pro_id_new ) ) !== ( absint( $question_pro_id ) ) ) ) {
update_post_meta( $post_id, 'question_pro_id', absint( $question_pro_id_new ) );
learndash_set_question_quizzes_dirty( $post_id );
}
learndash_proquiz_sync_question_fields( $post_id, $question_pro_id_new );
learndash_proquiz_sync_question_category( $post_id, $question_pro_id_new );
}
/**
* Register metaboxes for Question editing
*
* @since 2.6.0
*
* @param string $post_type Post Type being edited.
* @param object $post WP_Post Post being edited.
*/
public function add_metaboxes( $post_type = '', $post = null ) {
if ( $this->post_type_check( $post_type ) ) {
parent::add_metaboxes( $post_type, $post );
$this->init_question_edit( $post );
add_meta_box(
'learndash_question_category_proquiz',
sprintf(
// translators: placeholders: Question.
esc_html_x( '%s Category', 'placeholders: Question', 'learndash' ),
LearnDash_Custom_Label::get_label( 'question' )
) . ' ' . esc_html__( '(optional)', 'learndash' ),
array( $this, 'question_category_proquiz_page_box' ),
$this->post_type,
'side',
'high'
);
add_meta_box(
'learndash_question_type',
esc_html__( 'Answer type', 'learndash' ),
array( $this, 'question_type_page_box' ),
$this->post_type,
'side',
'default'
);
add_meta_box(
'learndash_question_points',
esc_html__( 'Points', 'learndash' ) . ' ' . esc_html__( '(required)', 'learndash' ),
array( $this, 'question_points_page_box' ),
$this->post_type,
'side',
'default'
);
add_meta_box(
'learndash_question_answers',
esc_html__( 'Answers', 'learndash' ) . ' ' . esc_html__( '(required)', 'learndash' ),
array( $this, 'question_answers_page_box' ),
$this->post_type,
'normal',
'high'
);
add_meta_box(
'learndash_question_single_choice_options',
esc_html__( 'Single choice options', 'learndash' ) . ' ' . esc_html__( '(optional)', 'learndash' ),
array( $this, 'question_single_choice_options' ),
$this->post_type,
'normal',
'high'
);
add_meta_box(
'learndash_question_message_correct_answer',
esc_html__( 'Message with the correct answer', 'learndash' ) . ' ' . esc_html__( '(optional)', 'learndash' ),
array( $this, 'question_message_correct_answer_page_box' ),
$this->post_type,
'normal',
'high'
);
add_meta_box(
'learndash_question_message_incorrect_answer',
esc_html__( 'Message with the incorrect answer', 'learndash' ) . ' ' . esc_html__( '(optional)', 'learndash' ),
array( $this, 'question_message_incorrect_answer_page_box' ),
$this->post_type,
'normal',
'high'
);
add_meta_box(
'learndash_question_hint',
esc_html__( 'Hint', 'learndash' ) . ' ' . esc_html__( '(optional)', 'learndash' ),
array( $this, 'question_hint_page_box' ),
$this->post_type,
'normal',
'high'
);
add_meta_box(
'learndash_question_template',
esc_html__( 'Template', 'learndash' ),
array( $this, 'question_template_page_box' ),
$this->post_type,
'normal',
'high'
);
global $wp_meta_boxes;
if ( isset( $wp_meta_boxes[ $this->post_type ]['normal']['high']['learndash_question_answers'] ) ) {
$learndash_question_answers = $wp_meta_boxes[ $this->post_type ]['normal']['high']['learndash_question_answers'];
unset( $wp_meta_boxes[ $this->post_type ]['normal']['high']['learndash_question_answers'] );
} else {
$learndash_question_answers = null;
}
if ( isset( $wp_meta_boxes[ $this->post_type ]['normal']['high']['learndash_question_single_choice_options'] ) ) {
$learndash_question_single_choice_options = $wp_meta_boxes[ $this->post_type ]['normal']['high']['learndash_question_single_choice_options'];
unset( $wp_meta_boxes[ $this->post_type ]['normal']['high']['learndash_question_single_choice_options'] );
} else {
$learndash_question_single_choice_options = null;
}
$question_metaboxes_new = array();
if ( ! is_null( $learndash_question_answers ) ) {
$question_metaboxes_new = array_merge(
$question_metaboxes_new,
array( 'learndash_question_answers' => $learndash_question_answers )
);
}
if ( ! is_null( $learndash_question_single_choice_options ) ) {
$question_metaboxes_new = array_merge(
$question_metaboxes_new,
array( 'learndash_question_single_choice_options' => $learndash_question_single_choice_options )
);
}
if ( ! empty( $question_metaboxes_new ) ) {
// phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
$wp_meta_boxes[ $this->post_type ]['normal']['high'] = array_merge(
$question_metaboxes_new,
$wp_meta_boxes[ $this->post_type ]['normal']['high']
);
}
/**
* Check if the editor is classic or new Gutenberg Block editor and hide non-important metaboxes
*/
if ( ( $post ) && ( is_a( $post, 'WP_Post' ) ) ) {
$user_closed_postboxes = get_user_meta( get_current_user_id(), 'closedpostboxes_' . $this->post_type, true );
if ( ( is_string( $user_closed_postboxes ) ) && ( '' === $user_closed_postboxes ) ) {
if ( ( function_exists( 'use_block_editor_for_post' ) ) && ( use_block_editor_for_post( $post ) ) ) {
$all_postboxes = array(
'sfwd-question',
'learndash_question_single_choice_options',
'learndash_question_message_correct_answer',
'learndash_question_message_incorrect_answer',
'learndash_question_hint',
'learndash_question_template',
);
} else {
$all_postboxes = array(
'learndash_question_message_correct_answer',
'learndash_question_message_incorrect_answer',
'learndash_question_hint',
'learndash_question_template',
);
}
update_user_meta( get_current_user_id(), 'closedpostboxes_' . $this->post_type, $all_postboxes );
}
}
}
}
/**
* Shows the Question Types metabox.
*
* @since 2.6.0
*
* @param object $post WP_Post Question being edited.
*/
public function question_type_page_box( $post ) {
global $learndash_question_types;
if ( is_a( $this->pro_question_edit, 'WpProQuiz_Model_Question' ) ) {
$question_type = $this->pro_question_edit->getAnswerType();
} else {
$question_type = 'single';
}
?>
<fieldset>
<legend class="screen-reader-text"><?php esc_html__( 'Answer Type', 'learndash' ); ?></legend>
<ul>
<?php
foreach ( $learndash_question_types as $q_type => $q_label ) {
?>
<li><input id="learndash-question-type-<?php echo esc_attr( $q_type ); ?>" type="radio" name="answerType" value="<?php echo esc_attr( $q_type ); ?>" <?php checked( $q_type, $question_type ); ?> />
<label for="learndash-question-type-<?php echo esc_attr( $q_type ); ?>" ><?php echo esc_attr( $q_label ); ?></label></li>
<?php
}
?>
</ul>
</fieldset>
<?php
}
/**
* Shows the Question Category metabox.
*
* @since 2.6.0
*
* @param object $post WP_Post Question being edited.
*/
public function question_category_proquiz_page_box( $post ) {
if ( is_a( $this->pro_question_edit, 'WpProQuiz_Model_Question' ) ) {
$question_category_id = $this->pro_question_edit->getCategoryId();
} else {
$question_category_id = 0;
}
$category_mapper = new WpProQuiz_Model_CategoryMapper();
$all_categories = $category_mapper->fetchAll();
?>
<div id="wpProQuiz_nonce" data-nonce="<?php echo esc_attr( wp_create_nonce( 'wpProQuiz_nonce' ) ); ?>" style="display:none;"></div>
<p class="description">
<?php esc_html_e( 'You can assign classify category for a question. Categories are e.g. visible in statistics function.', 'learndash' ); ?>
</p>
<p class="description">
<?php esc_html_e( 'You can manage categories in global settings.', 'learndash' ); ?>
</p>
<div>
<select name="category">
<option value="-1">--- <?php esc_html_e( 'Create new category', 'learndash' ); ?> ----</option>
<option value="0" <?php echo $this->pro_question_edit->getCategoryId() == 0 ? 'selected="selected"' : ''; ?>>--- <?php esc_html_e( 'No category', 'learndash' ); ?> ---</option>
<?php
foreach ( $all_categories as $cat ) {
echo '<option ' . selected( $question_category_id, $cat->getCategoryId(), false ) . ' value="' . esc_attr( $cat->getCategoryId() ) . '">' . esc_html( stripslashes( $cat->getCategoryName() ) ) . '</option>';
}
?>
</select>
</div>
<div style="display: none;" id="categoryAddBox">
<h4><?php esc_html_e( 'Create new category', 'learndash' ); ?></h4>
<input type="text" name="categoryAdd" value="">
<input type="button" class="button-secondary" name="" id="categoryAddBtn" value="<?php esc_html_e( 'Create', 'learndash' ); ?>">
</div>
<div id="categoryMsgBox" style="display:none; padding: 5px; border: 1px solid rgb(160, 160, 160); background-color: rgb(255, 255, 168); font-weight: bold; margin: 5px; ">
Category
</div>
<?php
}
/**
* Shows the Question Points metabox.
*
* @since 2.6.0
*
* @param object $post WP_Post Question being edited.
*/
public function question_points_page_box( $post ) {
if ( is_a( $this->pro_question_edit, 'WpProQuiz_Model_Question' ) ) {
$question_points = $this->pro_question_edit->getPoints();
} else {
$question_points = 1;
}
?>
<p class="description">
<?php
printf(
// translators: placeholder: question.
esc_html_x( 'Points for this %s (Standard is 1 point)', 'placeholder: question', 'learndash' ),
esc_html( learndash_get_custom_label_lower( 'question' ) )
)
?>
</p>
<label>
<input name="points" class="small-text" value="<?php echo intval( $question_points ); ?>" type="number" min="1"> <?php esc_html_e( 'Points', 'learndash' ); ?>
</label>
<p class="description">
<?php
printf(
// translators: placeholder: question.
esc_html_x( 'These points will be rewarded, only if the user chooses the %s correctly', 'placeholder: question', 'learndash' ),
esc_html( learndash_get_custom_label( 'question' ) )
)
?>
</p>
<div style="margin-top: 10px;" id="wpProQuiz_answerPointsActivated">
<label>
<input name="answerPointsActivated" type="checkbox" value="1" <?php checked( '1', $this->pro_question_edit->isAnswerPointsActivated() ); ?>>
<?php esc_html_e( 'Different points for each answer', 'learndash' ); ?>
</label>
<p class="description">
<?php esc_html_e( 'If you enable this option, you can enter different points for every answer.', 'learndash' ); ?>
</p>
</div>
<div style="margin-top: 10px;" id="wpProQuiz_showPointsBox">
<label>
<input name="showPointsInBox" value="1" type="checkbox" <?php checked( '1', $this->pro_question_edit->isShowPointsInBox() ); ?>>
<?php esc_html_e( 'Show reached points in the correct- and incorrect message?', 'learndash' ); ?>
</label>
</div>
<?php
}
/**
* Shows the Question Correct Answer Message metabox.
*
* @since 2.6.0
*
* @param object $post WP_Post Question being edited.
*/
public function question_message_correct_answer_page_box( $post ) {
if ( is_a( $this->pro_question_edit, 'WpProQuiz_Model_Question' ) ) {
$question_correct_same_text = checked( '1', $this->pro_question_edit->isCorrectSameText(), false );
$question_correct_message = $this->pro_question_edit->getCorrectMsg();
} else {
$question_correct_same_text = '';
$question_correct_message = '';
}
?>
<p class="description">
<?php esc_html_e( 'This text will be visible if answered correctly. It can be used as explanation for complex questions. The message "Right" or "Wrong" is always displayed automatically.', 'learndash' ); ?>
</p>
<div class="learndash-same-answer-text" style="padding-top: 10px; padding-bottom: 10px;">
<label for="wpProQuiz_correctSameText">
<?php esc_html_e( 'Same text for correct- and incorrect-message?', 'learndash' ); ?>
<input type="checkbox" name="correctSameText" id="wpProQuiz_correctSameText" value="1" <?php echo esc_html( $question_correct_same_text ); ?>>
</label>
</div>
<?php
wp_editor( $question_correct_message, 'correctMsg', array( 'textarea_rows' => 3 ) );
}
/**
* Shows the Question Incorrect Answer Message metabox.
*
* @since 2.6.0
*
* @param object $post WP_Post Question being edited.
*/
public function question_message_incorrect_answer_page_box( $post ) {
if ( is_a( $this->pro_question_edit, 'WpProQuiz_Model_Question' ) ) {
$question_incorrect_message = $this->pro_question_edit->getIncorrectMsg();
} else {
$question_incorrect_message = '';
}
?>
<div style="padding-top: 10px; padding-bottom: 10px;"></div>
<p class="description">
<?php esc_html_e( 'This text will be visible if answered incorrectly. It can be used as explanation for complex questions. The message "Right" or "Wrong" is always displayed automatically.', 'learndash' ); ?>
</p>
<?php
wp_editor( $question_incorrect_message, 'incorrectMsg', array( 'textarea_rows' => 3 ) );
}
/**
* Shows the Question Hint metabox.
*
* @since 2.6.0
*
* @param object $post WP_Post Question being edited.
*/
public function question_hint_page_box( $post ) {
if ( is_a( $this->pro_question_edit, 'WpProQuiz_Model_Question' ) ) {
$question_hint_enabled = checked( '1', $this->pro_question_edit->isTipEnabled(), false );
$question_hint_message = $this->pro_question_edit->getTipMsg();
} else {
$question_hint_enabled = '';
$question_hint_message = '';
}
?>
<p class="description">
<?php esc_html_e( 'Here you can enter solution hint.', 'learndash' ); ?>
</p>
<div style="padding-top: 10px; padding-bottom: 10px;">
<label for="wpProQuiz_tip">
<?php esc_html_e( 'Activate hint for this question?', 'learndash' ); ?>
<input type="checkbox" name="tipEnabled" id="wpProQuiz_tip" value="1" <?php echo esc_html( $question_hint_enabled ); ?>>
</label>
</div>
<div id="wpProQuiz_tipBox">
<?php wp_editor( $question_hint_message, 'tipMsg', array( 'textarea_rows' => 3 ) ); ?>
</div>
<?php
}
/**
* Shows the Single Choice Question Options metabox.
*
* @since 2.6.0
*
* @param object $post WP_Post Question being edited.
*/
public function question_single_choice_options( $post ) {
?>
<p class="description">
<?php echo wp_kses_post( __( 'If "Different points for each answer" is activated, you can activate a special mode.<br> This changes the calculation of the points', 'learndash' ) ); ?>
</p>
<label>
<input type="checkbox" name="answerPointsDiffModusActivated" value="1" <?php checked( '1', $this->pro_question_edit->isAnswerPointsDiffModusActivated() ); ?>>
<?php esc_html_e( 'Different points - modus 2 activate', 'learndash' ); ?>
</label>
<br><br>
<p class="description">
<?php esc_html_e( 'Disables the distinction between correct and incorrect.', 'learndash' ); ?><br>
</p>
<label>
<input type="checkbox" name=disableCorrect value="1" <?php checked( '1', $this->pro_question_edit->isDisableCorrect() ); ?>>
<?php esc_html_e( 'Disable correct and incorrect', 'learndash' ); ?>
</label>
<div style="padding-top: 20px;">
<a href="#" id="clickPointDia"><?php esc_html_e( 'Explanation of points calculation', 'learndash' ); ?></a>
<?php $this->answerPointDia(); ?>
</div>
<?php
}
/**
* Shows the Question Answers metabox.
*
* @since 2.6.0
*
* @param object $post WP_Post Question being edited.
*/
public function question_answers_page_box( $post ) {
$proquiz_controller_question = new WpProQuiz_Controller_Question();
if ( is_a( $this->pro_question_edit, 'WpProQuiz_Model_Question' ) ) {
$pro_question_data = $proquiz_controller_question->setAnswerObject( $this->pro_question_edit );
} else {
$pro_question_data = $proquiz_controller_question->setAnswerObject();
}
$this->view = new WpProQuiz_View_QuestionEdit();
?>
<div class="inside answer_felder">
<div class="free_answer">
<?php $this->view->freeChoice( $pro_question_data['free_answer'] ); ?>
</div>
<div class="sort_answer">
<p class="description">
<?php esc_html_e( 'Please sort the answers in right order with the "Move" - Button. The answers will be displayed randomly.', 'learndash' ); ?>
</p>
<ul class="answerList">
<?php $this->view->sortingChoice( $pro_question_data['sort_answer'] ); ?>
</ul>
<input type="button" class="button-primary addAnswer" data-default-value="<?php echo esc_attr( (string) LEARNDASH_LMS_DEFAULT_ANSWER_POINTS ); ?>" value="<?php esc_html_e( 'Add new answer', 'learndash' ); ?>">
</div>
<div class="classic_answer">
<ul class="answerList">
<?php $this->view->singleMultiCoice( $pro_question_data['classic_answer'] ); // cspell:disable-line. ?>
</ul>
<input type="button" class="button-primary addAnswer" data-default-value="<?php echo esc_attr( (string) LEARNDASH_LMS_DEFAULT_ANSWER_POINTS ); ?>" value="<?php esc_html_e( 'Add new answer', 'learndash' ); ?>">
</div>
<div class="matrix_sort_answer">
<p class="description">
<?php esc_html_e( 'In this mode, Sort Elements must be assigned to their corresponding Criterion.', 'learndash' ); ?>
</p>
<p class="description">
<?php esc_html_e( 'Each Sort Element must be unique, and only one-to-one associations are supported.', 'learndash' ); ?>
</p>
<br>
<label>
<?php esc_html_e( 'Percentage width of criteria table column:', 'learndash' ); ?>
<?php $msacw_value = $this->pro_question_edit->getMatrixSortAnswerCriteriaWidth() > 0 ? $this->pro_question_edit->getMatrixSortAnswerCriteriaWidth() : 20; ?>
<input type="number" min="1" max="99" step="1" name="matrixSortAnswerCriteriaWidth" value="<?php echo esc_attr( $msacw_value ); ?>">%
</label>
<p class="description">
<?php esc_html_e( 'Allows adjustment of the left column\'s width, and the right column will auto-fill the rest of the available space. Increase this to allow accommodate longer criterion text. Defaults to 20%.', 'learndash' ); ?>
</p>
<br>
<ul class="answerList">
<?php $this->view->matrixSortingChoice( $pro_question_data['matrix_sort_answer'] ); ?>
</ul>
<input type="button" class="button-primary addAnswer" data-default-value="<?php echo esc_attr( (string) LEARNDASH_LMS_DEFAULT_ANSWER_POINTS ); ?>" value="<?php esc_html_e( 'Add new answer', 'learndash' ); ?>">
</div>
<div class="cloze_answer">
<?php $this->view->clozeChoice( $pro_question_data['cloze_answer'] ); ?>
</div>
<div class="assessment_answer">
<?php $this->view->assessmentChoice( $pro_question_data['assessment_answer'] ); ?>
</div>
<div class="essay">
<?php $this->view->essayChoice( $pro_question_data['essay'] ); ?>
</div>
</div>
<?php
}
/**
* Shows the Question Template metabox.
*
* @since 2.6.0
*
* @param object $post WP_Post Question being edited.
*/
public function question_template_page_box( $post ) {
$template_mapper = new WpProQuiz_Model_TemplateMapper();
$templates = $template_mapper->fetchAll( WpProQuiz_Model_Template::TEMPLATE_TYPE_QUESTION, false );
$template_loaded_id = '';
if ( ( isset( $_GET['templateLoadId'] ) ) && ( ! empty( $_GET['templateLoadId'] ) ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
$template_loaded_id = intval( $_GET['templateLoadId'] ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended
}
?>
<div class="wrap wpProQuiz_questionEdit">
<table class="form-table">
<tbody>
<tr>
<th scope="row">
<?php esc_html_e( 'Use Template', 'learndash' ); ?>
</th>
<td>
<select id="templateLoadId" name="templateLoadId">
<?php
if ( ( isset( $_GET['post'] ) ) && ( ! empty( $_GET['post'] ) ) && ( isset( $_GET['templateLoadId'] ) ) && ( ! empty( $_GET['templateLoadId'] ) ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
$template_url = remove_query_arg( 'templateLoadId' );
echo '<option value="' . esc_url( $template_url ) . '">' . sprintf(
// translators: Question Title.
esc_html_x( 'Revert: %s', 'placeholder: Question Title', 'learndash' ),
wp_kses_post( get_the_title( absint( $_GET['post'] ) ) ) // phpcs:ignore WordPress.Security.NonceVerification.Recommended
) . '</option>';
} else {
echo '<option value="">' . esc_html__( 'Select a Template to load', 'learndash' ) . '</option>';
}
foreach ( $templates as $template ) {
$template_url = add_query_arg( 'templateLoadId', absint( $template->getTemplateId() ) );
echo '<option ' . selected( $template_loaded_id, $template->getTemplateId() ) . ' value="' . esc_url( $template_url ) . '">' . esc_html( $template->getName() ) . '</option>';
}
?>
</select><br />
<input type="submit" name="templateLoad" value="<?php esc_html_e( 'load template', 'learndash' ); ?>" class="button-primary">
</td>
</tr>
<tr>
<th scope="row">
<?php esc_html_e( 'Save as Template', 'learndash' ); ?>
</th>
<td>
<select name="templateSaveList">
<option value=""><?php esc_html_e( 'Select a templates to save or new', 'learndash' ); ?></option>
<option value="0">=== <?php esc_html_e( 'Create new template', 'learndash' ); ?> === </option>
<?php
foreach ( $templates as $template ) {
echo '<option value="' . absint( $template->getTemplateId() ), '">' . esc_html( $template->getName() ) . '</option>';
}
?>
</select><br />
<input type="text" placeholder="<?php esc_html_e( 'new template name', 'learndash' ); ?>" class="regular-text" name="templateName">
</td>
</tr>
</tbody>
</table>
</div>
<?php
}
/**
* Display special answer points diagram.
*
* @since 2.6.0
*/
private function answerPointDia() {
?>
<style>
.pointDia td {
border: 1px solid #9E9E9E;
padding: 8px;
}
</style>
<table style="border-collapse: collapse; display: none; margin-top: 10px;" class="pointDia">
<tr>
<th>
<?php esc_html_e( '"Different points for each answer" enabled', 'learndash' ); ?><br>
<?php esc_html_e( '"Different points - mode 2" disable', 'learndash' ); ?>
</th>
<th>
<?php esc_html_e( '"Different points for each answer" enabled', 'learndash' ); ?><br>
<?php esc_html_e( '"Different points - mode 2" enabled', 'learndash' ); ?>
</th>
</tr>
<tr>
<td>
<?php
echo nl2br(
esc_html__(
'Question - Single Choice - 3 Answers - Diff points mode
A=3 Points [correct]
B=2 Points [incorrect]
C=1 Point [incorrect]
= 6 Points',
'learndash'
)
);
?>
</td>
<td>
<?php
echo nl2br(
esc_html__(
'Question - Single Choice - 3 Answers - Modus 2
A=3 Points [correct]
B=2 Points [incorrect]
C=1 Point [incorrect]
= 3 Points',
'learndash'
)
);
?>
</td>
</tr>
<tr>
<td>
<?php
echo nl2br(
esc_html__(
'~~~ User 1: ~~~
A=checked
B=unchecked
C=unchecked
Result:
A=correct and checked (correct) = 3 Points
B=incorrect and unchecked (correct) = 2 Points
C=incorrect and unchecked (correct) = 1 Points
= 6 / 6 Points 100%',
'learndash'
)
);
?>
</td>
<td>
<?php
echo nl2br(
esc_html__(
'~~~ User 1: ~~~
A=checked
B=unchecked
C=unchecked
Result:
A=checked = 3 Points
B=unchecked = 0 Points
C=unchecked = 0 Points
= 3 / 3 Points 100%',
'learndash'
)
);
?>
</td>
</tr>
<tr>
<td>
<?php
echo nl2br(
esc_html__(
'~~~ User 2: ~~~
A=unchecked
B=checked
C=unchecked
Result:
A=correct and unchecked (incorrect) = 0 Points
B=incorrect and checked (incorrect) = 0 Points
C=incorrect and unchecked (correct) = 1 Points
= 1 / 6 Points 16.67%',
'learndash'
)
);
?>
</td>
<td>
<?php
echo nl2br(
esc_html__(
'~~~ User 2: ~~~
A=unchecked
B=checked
C=unchecked
Result:
A=unchecked = 0 Points
B=checked = 2 Points
C=unchecked = 0 Points
= 2 / 3 Points 66,67%',
'learndash'
)
);
?>
</td>
</tr>
<tr>
<td>
<?php
echo nl2br(
esc_html__(
'~~~ User 3: ~~~
A=unchecked
B=unchecked
C=checked
Result:
A=correct and unchecked (incorrect) = 0 Points
B=incorrect and unchecked (correct) = 2 Points
C=incorrect and checked (incorrect) = 0 Points
= 2 / 6 Points 33.33%',
'learndash'
)
);
?>
</td>
<td>
<?php
echo nl2br(
esc_html__(
'~~~ User 3: ~~~
A=unchecked
B=unchecked
C=checked
Result:
A=unchecked = 0 Points
B=unchecked = 0 Points
C=checked = 1 Points
= 1 / 3 Points 33,33%',
'learndash'
)
);
?>
</td>
</tr>
<tr>
<td>
<?php
echo nl2br(
esc_html__(
'~~~ User 4: ~~~
A=unchecked
B=unchecked
C=unchecked
Result:
A=correct and unchecked (incorrect) = 0 Points
B=incorrect and unchecked (correct) = 2 Points
C=incorrect and unchecked (correct) = 1 Points
= 3 / 6 Points 50%',
'learndash'
)
);
?>
</td>
<td>
<?php
echo nl2br(
esc_html__(
'~~~ User 4: ~~~
A=unchecked
B=unchecked
C=unchecked
Result:
A=unchecked = 0 Points
B=unchecked = 0 Points
C=unchecked = 0 Points
= 0 / 3 Points 0%',
'learndash'
)
);
?>
</td>
</tr>
</table>
<?php
}
// End of functions.
}
}
new Learndash_Admin_Question_Edit();