1919use Tutor \Models \CourseModel ;
2020use Tutor \Models \QuizModel ;
2121use Tutor \Traits \JsonResponse ;
22+ use WP_Post ;
2223
2324/**
2425 * Manage quiz operations.
2829class Quiz {
2930 use JsonResponse;
3031
32+ /**
33+ * Quiz post type
34+ *
35+ * @since 4.0.0
36+ *
37+ * @var string
38+ */
39+ private $ post_type ;
40+
3141 const META_QUIZ_OPTION = 'tutor_quiz_option ' ;
3242
3343 /**
@@ -65,10 +75,20 @@ class Quiz {
6575 * Register hooks
6676 *
6777 * @since 1.0.0
78+ * @since 4.0.0 $register_hooks param added
79+ *
80+ * @param bool $register_hooks To register hooks.
6881 *
6982 * @return void
7083 */
71- public function __construct () {
84+ public function __construct ( $ register_hooks = true ) {
85+ $ this ->post_type = tutor ()->quiz_post_type ;
86+ $ this ->prepare_allowed_html ();
87+
88+ if ( ! $ register_hooks ) {
89+ return ;
90+ }
91+
7292 add_action ( 'wp_ajax_tutor_quiz_timeout ' , array ( $ this , 'tutor_quiz_timeout ' ) );
7393
7494 // User take the quiz.
@@ -100,8 +120,6 @@ public function __construct() {
100120 */
101121 add_action ( 'wp_ajax_tutor_quiz_abandon ' , array ( $ this , 'tutor_quiz_abandon ' ) );
102122
103- $ this ->prepare_allowed_html ();
104-
105123 /**
106124 * Delete quiz attempt
107125 *
@@ -110,6 +128,10 @@ public function __construct() {
110128 add_action ( 'wp_ajax_tutor_attempt_delete ' , array ( $ this , 'attempt_delete ' ) );
111129
112130 add_action ( 'tutor_quiz/answer/review/after ' , array ( $ this , 'do_auto_course_complete ' ), 10 , 3 );
131+
132+ // Add quiz title as nav item & render single content on the learning area.
133+ add_action ( "tutor_learning_area_nav_item_ {$ this ->post_type }" , array ( $ this , 'render_nav_item ' ), 10 , 2 );
134+ add_action ( "tutor_single_content_ {$ this ->post_type }" , array ( $ this , 'render_single_content ' ) );
113135 }
114136
115137 /**
@@ -1260,4 +1282,42 @@ private function get_quiz_attempt_answers_by_attempt_id( int $attempt_id ): arra
12601282
12611283 return $ results ;
12621284 }
1285+
1286+ /**
1287+ * Render quiz title as nav item to show on the learning area
1288+ *
1289+ * @since 4.0.0
1290+ *
1291+ * @param WP_Post $quiz Quiz post object.
1292+ * @param bool $can_access Can user access this content.
1293+ *
1294+ * @return void
1295+ */
1296+ public function render_nav_item ( WP_Post $ quiz , bool $ can_access ): void {
1297+ tutor_load_template (
1298+ 'learning-area.quiz.nav-item ' ,
1299+ array (
1300+ 'quiz ' => $ quiz ,
1301+ 'can_access ' => $ can_access ,
1302+ )
1303+ );
1304+ }
1305+
1306+ /**
1307+ * Render content for the a single quiz
1308+ *
1309+ * @since 4.0.0
1310+ *
1311+ * @param WP_Post $quiz Quiz post object.
1312+ *
1313+ * @return void
1314+ */
1315+ public function render_single_content ( WP_Post $ quiz ): void {
1316+ tutor_load_template (
1317+ 'learning-area.quiz.content ' ,
1318+ array (
1319+ 'quiz ' => $ quiz ,
1320+ )
1321+ );
1322+ }
12631323}
0 commit comments