Skip to content

Commit 58ef464

Browse files
committed
Merge branch '4.0.0-dev' into v4-quiz-attempts
2 parents b3fc0f4 + dc1a37c commit 58ef464

File tree

4 files changed

+201
-35
lines changed

4 files changed

+201
-35
lines changed

classes/Quiz.php

Lines changed: 63 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
use Tutor\Models\CourseModel;
2020
use Tutor\Models\QuizModel;
2121
use Tutor\Traits\JsonResponse;
22+
use WP_Post;
2223

2324
/**
2425
* Manage quiz operations.
@@ -28,6 +29,15 @@
2829
class 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
}

classes/Template.php

Lines changed: 0 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@ public function __construct() {
4545
add_filter( 'template_include', array( $this, 'play_private_video' ), 99 );
4646
add_filter( 'tutor_single_content_template', array( $this, 'load_single_lesson_template' ), 99, 2 );
4747
add_filter( 'tutor_single_content_template', array( $this, 'load_quiz_template' ), 99, 2 );
48-
add_filter( 'tutor_single_content_template', array( $this, 'load_assignment_template' ), 99, 2 );
4948

5049
add_filter( 'template_include', array( $this, 'student_public_profile' ), 99 );
5150
add_filter( 'template_include', array( $this, 'tutor_dashboard' ), 99 );
@@ -457,37 +456,6 @@ public function load_quiz_template( $template, $post_type ) {
457456
return $template;
458457
}
459458

460-
/**
461-
* Load assignment template
462-
*
463-
* @since 1.0.0
464-
*
465-
* @since 4.0.0 Post type.
466-
*
467-
* @param string $template template file to load.
468-
* @param string $post_type Post type.
469-
*
470-
* @return string template path
471-
*/
472-
public function load_assignment_template( $template, $post_type ) {
473-
if ( tutor()->lesson_post_type !== $post_type ) {
474-
return $template;
475-
}
476-
477-
if ( is_user_logged_in() ) {
478-
$has_content_access = tutor_utils()->has_enrolled_content_access( 'assignment' );
479-
if ( $has_content_access ) {
480-
$template = tutor_get_template( 'single-assignment' );
481-
} else {
482-
$template = tutor_get_template( 'single.lesson.required-enroll' ); // You need to enroll first.
483-
}
484-
} else {
485-
$template = tutor_get_template( 'login' );
486-
}
487-
488-
return $template;
489-
}
490-
491459
/**
492460
* Student public profile
493461
*
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
<?php
2+
/**
3+
* Tutor learning area quiz.
4+
*
5+
* @package Tutor\Templates
6+
* @subpackage LearningArea
7+
* @author Themeum <[email protected]>
8+
* @link https://themeum.com
9+
* @since 4.0.0
10+
*/
11+
12+
defined( 'ABSPATH' ) || exit;
13+
14+
use TUTOR\Icon;
15+
16+
$quiz = $quiz ?? null;
17+
if ( ! $quiz || ! is_a( $quiz, 'WP_Post' ) ) {
18+
return;
19+
}
20+
21+
$tabs_data = array(
22+
array(
23+
'id' => 'overview',
24+
'label' => 'Overview',
25+
'icon' => Icon::COURSES,
26+
),
27+
array(
28+
'id' => 'notes',
29+
'label' => 'Notes',
30+
'icon' => Icon::NOTES,
31+
),
32+
array(
33+
'id' => 'comments',
34+
'label' => 'Comments',
35+
'icon' => Icon::COMMENTS,
36+
),
37+
);
38+
39+
?>
40+
<div class="tutor-pt-9">
41+
<div
42+
x-data='tutorTabs({
43+
tabs: <?php echo wp_json_encode( $tabs_data ); ?>,
44+
defaultTab: "overview",
45+
urlParams: {
46+
paramName: "tab",
47+
}
48+
})'
49+
class="tutor-surface-l1 tutor-border tutor-rounded-lg"
50+
>
51+
<div x-ref="tablist" class="tutor-tabs-nav tutor-p-6 tutor-border-b" role="tablist" aria-orientation="horizontal">
52+
<template x-for="tab in tabs" :key="tab.id">
53+
<button
54+
type="button"
55+
role="tab"
56+
:class='getTabClass(tab)'
57+
x-bind:aria-selected="isActive(tab.id)"
58+
:disabled="tab.disabled ? true : false"
59+
@click="selectTab(tab.id)"
60+
>
61+
<span x-data="TutorCore.icon({ name: tab.icon, width: 24, height: 24})"></span>
62+
<span x-text="tab.label"></span>
63+
</button>
64+
</template>
65+
</div>
66+
67+
<div class="tutor-tabs-content tutor-p-6">
68+
<div x-show="activeTab === 'overview'" x-cloak class="tutor-tab-panel" role="tabpanel">
69+
<h4 class="tutor-heading-4 tutor-mb-4">
70+
<?php echo esc_html( $quiz->post_title ); ?>
71+
</h4>
72+
<?php echo wp_kses_post( $quiz->post_content ); ?>
73+
</div>
74+
<div x-show="activeTab === 'notes'" x-cloak class="tutor-tab-panel" role="tabpanel">
75+
<?php esc_html_e( 'Notes', 'tutor' ); ?>
76+
</div>
77+
<div x-show="activeTab === 'comments'" x-cloak class="tutor-tab-panel" role="tabpanel">
78+
<?php esc_html_e( 'Comments', 'tutor' ); ?>
79+
</div>
80+
</div>
81+
</div>
82+
83+
<?php tutor_load_template( 'learning-area.components.footer' ); ?>
84+
</div>
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
/**
3+
* Show quiz nav item on the learning area
4+
*
5+
* @package Tutor\Templates
6+
* @subpackage LearningArea
7+
* @author Themeum <[email protected]>
8+
* @link https://themeum.com
9+
* @since 4.0.0
10+
*/
11+
12+
defined( 'ABSPATH' ) || exit;
13+
14+
use TUTOR\Icon;
15+
16+
global $tutor_current_content_id;
17+
18+
$quiz = $quiz ?? null;
19+
$can_access = $can_access ?? false;
20+
21+
if ( ! $quiz && ! is_a( $quiz, 'WP_Post' ) ) {
22+
return;
23+
}
24+
25+
$is_completed = tutor_utils()->is_completed_lesson( $quiz->ID );
26+
$lesson_title = $quiz->post_title;
27+
28+
$active_class = $tutor_current_content_id === $quiz->ID ? 'active' : '';
29+
?>
30+
31+
<div class="<?php echo esc_html( sprintf( 'tutor-learning-nav-item %s', $active_class ) ); ?>">
32+
<?php if ( $is_completed ) : ?>
33+
<a href="<?php echo esc_url( $can_access ? get_permalink( $quiz->ID ) : '#' ); ?>">
34+
<?php tutor_utils()->render_svg_icon( Icon::COMPLETED_COLORIZE, 20, 20 ); ?>
35+
<div><?php echo esc_html( $lesson_title ); ?></div>
36+
</a>
37+
<?php else : ?>
38+
<a href="<?php echo esc_url( $can_access ? get_permalink( $quiz->ID ) : '#' ); ?>">
39+
<div class="tutor-learning-nav-progress">
40+
<div x-data="tutorStatics({
41+
value: 0,
42+
size: 'tiny',
43+
type: 'progress',
44+
showLabel: false,
45+
background: 'var(--tutor-actions-gray-empty)',
46+
strokeColor: 'var(--tutor-border-hover)' })">
47+
<div x-html="render()"></div>
48+
</div>
49+
</div>
50+
<div><?php echo esc_html( $lesson_title ); ?></div>
51+
</a>
52+
<?php endif; ?>
53+
</div>
54+

0 commit comments

Comments
 (0)