ActionBoxGenerator.php
1 year ago
AnnouncementsGenerator.php
1 year ago
CourseMetaGenerator.php
1 year ago
CourseThumbnailGenerator.php
1 year ago
ElementGenerator.php
1 year ago
GradebookGenerator.php
1 year ago
InstructorGenerator.php
1 year ago
LessonGenerator.php
1 year ago
Preview.php
1 year ago
QnAGenerator.php
1 year ago
RatingGenerator.php
1 year ago
ResourcesGenerator.php
1 year ago
ShareGenerator.php
1 year ago
TopicsGenerator.php
1 year ago
WishListGenerator.php
1 year ago
LessonGenerator.php
292 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Lesson view generator |
| 4 | * |
| 5 | * @package tutor-droip-elements |
| 6 | */ |
| 7 | |
| 8 | namespace TutorLMSDroip\ElementGenerator; |
| 9 | |
| 10 | if ( ! defined( 'ABSPATH' ) ) { |
| 11 | exit; // Exit if accessed directly. |
| 12 | } |
| 13 | |
| 14 | /** |
| 15 | * Class LessonGenerator |
| 16 | * This class is used to define all lesson generator functions. |
| 17 | */ |
| 18 | trait LessonGenerator { |
| 19 | |
| 20 | /** |
| 21 | * Post type enum |
| 22 | * |
| 23 | * @var array |
| 24 | */ |
| 25 | private $post_type_enum = array( |
| 26 | 'QUIZ' => 'tutor_quiz', |
| 27 | 'ASSIGNMENT' => 'tutor_assignments', |
| 28 | 'LESSON' => 'lesson', |
| 29 | 'GOOGLE_MEET' => 'tutor-googl,-meet', |
| 30 | 'ZOOM' => 'tutor_zoom_meeting', |
| 31 | ); |
| 32 | |
| 33 | /** |
| 34 | * Generate lessons markup |
| 35 | */ |
| 36 | private function generate_lessions_all_markup() { |
| 37 | switch ( $this->element['name'] ) { |
| 38 | case TDE_APP_PREFIX . '-lessons': |
| 39 | return $this->generate_lessons_markup(); |
| 40 | case TDE_APP_PREFIX . '-assignment': |
| 41 | case TDE_APP_PREFIX . '-googlemeet': |
| 42 | case TDE_APP_PREFIX . '-lesson-video': |
| 43 | case TDE_APP_PREFIX . '-lesson-text': |
| 44 | case TDE_APP_PREFIX . '-quiz': |
| 45 | case TDE_APP_PREFIX . '-zoom': |
| 46 | return $this->generate_lesson_markup(); |
| 47 | case TDE_APP_PREFIX . '-lesson-duration': |
| 48 | return $this->generate_lesson_duration(); |
| 49 | case TDE_APP_PREFIX . '-lesson-access': |
| 50 | return $this->lesson_aceess(); |
| 51 | case TDE_APP_PREFIX . '-meeting-status': |
| 52 | return $this->lesson_status(); |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | /** |
| 57 | * Generate lessons markup |
| 58 | * |
| 59 | * @return string |
| 60 | */ |
| 61 | private function generate_lessons_markup() { |
| 62 | $element_block = $this->group_elements_by_element_name(); |
| 63 | |
| 64 | $lessons_query = tutor_utils()->get_course_contents_by_topic( $this->options['post']->ID, -1 ); |
| 65 | |
| 66 | $children = ''; |
| 67 | |
| 68 | while ( $lessons_query->have_posts() ) { |
| 69 | $lessons_query->the_post(); |
| 70 | global $post; |
| 71 | |
| 72 | $options = array_merge( |
| 73 | $this->options, |
| 74 | array( |
| 75 | 'post' => $post, |
| 76 | ) |
| 77 | ); |
| 78 | |
| 79 | switch ( get_post_type() ) { |
| 80 | case $this->post_type_enum['QUIZ']: |
| 81 | $quiz_block = $element_block[ TDE_APP_PREFIX . '-quiz' ]; |
| 82 | $children .= call_user_func( $this->generate_child_element, $quiz_block['id'], $options ); |
| 83 | break; |
| 84 | case $this->post_type_enum['ASSIGNMENT']: |
| 85 | $assignment_block = $element_block[ TDE_APP_PREFIX . '-assignment' ]; |
| 86 | |
| 87 | $children .= call_user_func( $this->generate_child_element, $assignment_block['id'], $options ); |
| 88 | break; |
| 89 | case $this->post_type_enum['LESSON']: |
| 90 | if ( $this->lesson_has_video() ) { |
| 91 | $lesson_block = $element_block[ TDE_APP_PREFIX . '-lesson-video' ]; |
| 92 | |
| 93 | $children .= call_user_func( $this->generate_child_element, $lesson_block['id'], $options ); |
| 94 | } else { |
| 95 | $lesson_block = $element_block[ TDE_APP_PREFIX . '-lesson-text' ]; |
| 96 | |
| 97 | $children .= call_user_func( $this->generate_child_element, $lesson_block['id'], $options ); |
| 98 | } |
| 99 | break; |
| 100 | |
| 101 | case $this->post_type_enum['ZOOM']: |
| 102 | $zoom_block = $element_block[ TDE_APP_PREFIX . '-zoom' ]; |
| 103 | |
| 104 | $children .= call_user_func( $this->generate_child_element, $zoom_block['id'], $options ); |
| 105 | break; |
| 106 | |
| 107 | case $this->post_type_enum['GOOGLE_MEET']: |
| 108 | $googlemeet_block = $element_block[ TDE_APP_PREFIX . '-googlemeet' ]; |
| 109 | |
| 110 | $children .= call_user_func( $this->generate_child_element, $googlemeet_block['id'], $options ); |
| 111 | break; |
| 112 | |
| 113 | default: |
| 114 | $children .= ( '<div>' . get_post_type() . ': ' . get_the_title() . '</div>' ); |
| 115 | break; |
| 116 | |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | wp_reset_postdata(); |
| 121 | |
| 122 | return "<div $this->attributes > $children </div>"; |
| 123 | } |
| 124 | |
| 125 | /** |
| 126 | * Generate lesson markup |
| 127 | * |
| 128 | * @return string |
| 129 | */ |
| 130 | private function generate_lesson_markup() { |
| 131 | global $post; |
| 132 | |
| 133 | $lesson_permalink = $this->lesson_has_access() ? get_the_permalink() : 'javascript:void()'; |
| 134 | |
| 135 | $children = ''; |
| 136 | |
| 137 | $options = array_merge( |
| 138 | $this->options, |
| 139 | array( |
| 140 | 'post' => $post, |
| 141 | ) |
| 142 | ); |
| 143 | |
| 144 | if ( isset( $this->element['children'] ) && $this->element['children'] && is_array( $this->element['children'] ) ) { |
| 145 | $children = array_reduce( |
| 146 | $this->element['children'], |
| 147 | fn ( $carry, $item ) => ( $carry . call_user_func( $this->generate_child_element, $item, $options ) ), |
| 148 | '' |
| 149 | ); |
| 150 | } |
| 151 | |
| 152 | return "<a $this->attributes href='$lesson_permalink' > $children </a>"; |
| 153 | } |
| 154 | |
| 155 | /** |
| 156 | * Generate lesson duration markup |
| 157 | * |
| 158 | * @return string |
| 159 | */ |
| 160 | private function generate_lesson_duration() { |
| 161 | $duration = tutor_utils()->get_optimized_duration( $this->lesson_play_time() ); |
| 162 | |
| 163 | return "<div $this->attributes> $duration </div>"; |
| 164 | } |
| 165 | |
| 166 | /** |
| 167 | * Generate lesson access markup |
| 168 | * |
| 169 | * @return string |
| 170 | */ |
| 171 | private function lesson_aceess() { |
| 172 | $is_locked = $this->lesson_is_locked(); |
| 173 | |
| 174 | $child = $is_locked ? |
| 175 | call_user_func( $this->generate_child_element, $this->element['children'][0], $this->options ) : |
| 176 | call_user_func( $this->generate_child_element, $this->element['children'][1], $this->options ); |
| 177 | |
| 178 | return "<span $this->attributes> $child </span>"; |
| 179 | } |
| 180 | |
| 181 | /** |
| 182 | * Generate lesson status markup |
| 183 | * |
| 184 | * @return string |
| 185 | */ |
| 186 | private function lesson_status() { |
| 187 | if ( ! tutor()->has_pro ) { |
| 188 | return ''; |
| 189 | } |
| 190 | |
| 191 | global $post; |
| 192 | |
| 193 | $element_blocks = $this->group_elements_by_element_name(); |
| 194 | |
| 195 | $live_block = $element_blocks[ TDE_APP_PREFIX . '-meeting-live' ]; |
| 196 | |
| 197 | $expired_block = $element_blocks[ TDE_APP_PREFIX . '-meeting-expired' ]; |
| 198 | |
| 199 | $zoom_meeting = tutor_zoom_meeting_data( $post->ID ); |
| 200 | |
| 201 | $block = null; |
| 202 | |
| 203 | if ( $zoom_meeting->is_expired ) { |
| 204 | $block = $expired_block; |
| 205 | } elseif ( $zoom_meeting->is_started ) { |
| 206 | $block = $live_block; |
| 207 | } |
| 208 | |
| 209 | $child = $block ? call_user_func( $this->generate_child_element, $block['id'], $this->options ) : ''; |
| 210 | |
| 211 | return "<div $this->attributes> $child </div>"; |
| 212 | } |
| 213 | |
| 214 | /** |
| 215 | * Check if lesson has video |
| 216 | * |
| 217 | * @return bool |
| 218 | */ |
| 219 | private function lesson_has_video() { |
| 220 | return (bool) tutor_utils()->get_video_info(); |
| 221 | } |
| 222 | |
| 223 | /** |
| 224 | * Check if lesson is locked |
| 225 | * |
| 226 | * @return bool |
| 227 | */ |
| 228 | private function lesson_play_time() { |
| 229 | $video = tutor_utils()->get_video_info(); |
| 230 | |
| 231 | $play_time = $video ? $video->playtime : 0; |
| 232 | |
| 233 | return $play_time; |
| 234 | } |
| 235 | |
| 236 | /** |
| 237 | * Check if lesson is enrolled |
| 238 | * |
| 239 | * @return bool |
| 240 | */ |
| 241 | private function is_enrolled() { |
| 242 | $course_id = $this->get_course_id(); |
| 243 | |
| 244 | return (bool) tutor_utils()->is_enrolled( $course_id ); |
| 245 | } |
| 246 | |
| 247 | /** |
| 248 | * Get course id |
| 249 | * |
| 250 | * @return int |
| 251 | */ |
| 252 | private function get_course_id() { |
| 253 | global $post; |
| 254 | |
| 255 | if ( ! $this->course_id ) { |
| 256 | $this->course_id = tutor_utils()->get_course_id_by( 'lesson', $post->ID ); |
| 257 | } |
| 258 | |
| 259 | return $this->course_id; |
| 260 | } |
| 261 | |
| 262 | /** |
| 263 | * Check if lesson has access |
| 264 | * |
| 265 | * @return bool |
| 266 | */ |
| 267 | private function lesson_has_access() { |
| 268 | $course_id = $this->get_course_id(); |
| 269 | |
| 270 | return $this->is_enrolled() || |
| 271 | ( |
| 272 | get_post_meta( $course_id, '_tutor_is_public_course', true ) == 'yes' && |
| 273 | ! tutor_utils()->is_course_purchasable( $course_id ) |
| 274 | ); |
| 275 | } |
| 276 | |
| 277 | /** |
| 278 | * Check if lesson is locked |
| 279 | * |
| 280 | * @return bool |
| 281 | */ |
| 282 | private function lesson_is_locked() { |
| 283 | global $post; |
| 284 | |
| 285 | $course_id = $this->get_course_id(); |
| 286 | |
| 287 | $is_preview = get_post_meta( $post->ID, '_is_preview', true ); |
| 288 | |
| 289 | return ! ( $this->is_enrolled() || $is_preview || \TUTOR\Course_List::is_public( $course_id ) ); |
| 290 | } |
| 291 | } |
| 292 |