ElementGenerator
10 months ago
Ajax.php
10 months ago
Backend.php
1 year ago
Editor.php
1 year ago
Frontend.php
1 year ago
Helper.php
1 year ago
Hooks.php
11 months ago
Iframe.php
1 year ago
Pages.php
11 months ago
VisibilityCondition.php
1 year ago
VisibilityCondition.php
427 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * Preview script for html markup generator |
| 5 | * |
| 6 | * @package tutor-droip-elements |
| 7 | */ |
| 8 | |
| 9 | namespace TutorLMSDroip; |
| 10 | |
| 11 | use Tutor\Models\CartModel; |
| 12 | use TutorLMSDroip\ElementGenerator\CourseMetaGenerator; |
| 13 | |
| 14 | if (! defined('ABSPATH')) { |
| 15 | exit; // Exit if accessed directly. |
| 16 | } |
| 17 | |
| 18 | /** |
| 19 | * Class Helper |
| 20 | * This class is used to define all helper functions. |
| 21 | */ |
| 22 | class VisibilityCondition |
| 23 | { |
| 24 | |
| 25 | use CourseMetaGenerator; |
| 26 | |
| 27 | public static function visibility_condition_fields($conditions, $collection_data) |
| 28 | { |
| 29 | $type = $collection_data['type']; |
| 30 | $collectionType = $collection_data['collectionType']; |
| 31 | if ($collectionType === 'posts') { |
| 32 | switch ($type) { |
| 33 | case 'courses': { |
| 34 | $conditions = self::get_course_type_conditions($conditions); |
| 35 | } |
| 36 | } |
| 37 | } elseif ($collectionType === 'user') { |
| 38 | switch ($type) { |
| 39 | case 'courses': { |
| 40 | $conditions['user'] = array( |
| 41 | 'title' => 'Instructor', |
| 42 | 'fields' => array_merge( |
| 43 | $conditions['user']['fields'], |
| 44 | array( |
| 45 | array( |
| 46 | 'source' => TDE_APP_PREFIX, |
| 47 | 'value' => 'instructor_course_count', |
| 48 | 'title' => 'Course Count', |
| 49 | 'operator_type' => 'numeric_operators', |
| 50 | 'operand_type' => array_merge( |
| 51 | DROIP_PLUGIN_SETTINGS['INPUT_NUMBER'], |
| 52 | array( |
| 53 | 'placeholder' => 'Course Count', |
| 54 | ) |
| 55 | ), |
| 56 | ), |
| 57 | array( |
| 58 | 'source' => TDE_APP_PREFIX, |
| 59 | 'value' => 'instructor_student_count', |
| 60 | 'title' => 'Student Count', |
| 61 | 'operator_type' => 'numeric_operators', |
| 62 | 'operand_type' => array_merge( |
| 63 | DROIP_PLUGIN_SETTINGS['INPUT_NUMBER'], |
| 64 | array( |
| 65 | 'placeholder' => 'Student Count', |
| 66 | ) |
| 67 | ), |
| 68 | ), |
| 69 | array( |
| 70 | 'source' => TDE_APP_PREFIX, |
| 71 | 'value' => 'instructor_rating', |
| 72 | 'title' => 'Rating', |
| 73 | 'operator_type' => 'numeric_operators', |
| 74 | 'operand_type' => array_merge( |
| 75 | DROIP_PLUGIN_SETTINGS['INPUT_NUMBER'], |
| 76 | array( |
| 77 | 'placeholder' => 'Rating', |
| 78 | ) |
| 79 | ), |
| 80 | ), |
| 81 | array( |
| 82 | 'source' => TDE_APP_PREFIX, |
| 83 | 'value' => 'instructor_rating_count', |
| 84 | 'title' => 'Rating Count', |
| 85 | 'operator_type' => 'numeric_operators', |
| 86 | 'operand_type' => array_merge( |
| 87 | DROIP_PLUGIN_SETTINGS['INPUT_NUMBER'], |
| 88 | array( |
| 89 | 'placeholder' => 'Rating Count', |
| 90 | ) |
| 91 | ), |
| 92 | ), |
| 93 | ) |
| 94 | ), |
| 95 | ); |
| 96 | } |
| 97 | } |
| 98 | } else { |
| 99 | switch ($type) { |
| 100 | case 'TUTOR_LMS-tutor_course_rating': { |
| 101 | $conditions = self::get_course_type_conditions($conditions); |
| 102 | } |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | return $conditions; |
| 107 | } |
| 108 | |
| 109 | private static function get_course_type_conditions($conditions) |
| 110 | { |
| 111 | if (!isset($conditions['post']['fields'])) { |
| 112 | $conditions['post']['fields'] = array(); |
| 113 | } |
| 114 | $conditions['post'] = array( |
| 115 | 'title' => 'Course', |
| 116 | 'fields' => array_merge( |
| 117 | $conditions['post']['fields'], |
| 118 | array( |
| 119 | array( |
| 120 | 'source' => TDE_APP_PREFIX, |
| 121 | 'value' => 'is_paid', |
| 122 | 'title' => 'Paid', |
| 123 | 'operator_type' => 'boolean_operators', |
| 124 | ), |
| 125 | array( |
| 126 | 'source' => TDE_APP_PREFIX, |
| 127 | 'value' => 'on_sale', |
| 128 | 'title' => 'On Sale', |
| 129 | 'operator_type' => 'boolean_operators', |
| 130 | ), |
| 131 | array( |
| 132 | 'source' => TDE_APP_PREFIX, |
| 133 | 'value' => 'average_rating', |
| 134 | 'title' => 'Rating (Avg.)', |
| 135 | 'operator_type' => 'numeric_operators', |
| 136 | 'operand_type' => array_merge( |
| 137 | DROIP_PLUGIN_SETTINGS['INPUT_NUMBER'], |
| 138 | array( |
| 139 | 'placeholder' => 'Rating', |
| 140 | ) |
| 141 | ), |
| 142 | ), |
| 143 | array( |
| 144 | 'source' => TDE_APP_PREFIX, |
| 145 | 'value' => 'total_ratings', |
| 146 | 'title' => 'Total Ratings', |
| 147 | 'operator_type' => 'numeric_operators', |
| 148 | 'operand_type' => array_merge( |
| 149 | DROIP_PLUGIN_SETTINGS['INPUT_NUMBER'], |
| 150 | array( |
| 151 | 'placeholder' => 'Rating count', |
| 152 | ) |
| 153 | ), |
| 154 | ), |
| 155 | array( |
| 156 | 'source' => TDE_APP_PREFIX, |
| 157 | 'value' => 'enroll_count', |
| 158 | 'title' => 'Enrollment Count', |
| 159 | 'operator_type' => 'numeric_operators', |
| 160 | 'operand_type' => array_merge( |
| 161 | DROIP_PLUGIN_SETTINGS['INPUT_NUMBER'], |
| 162 | array( |
| 163 | 'placeholder' => 'Enroll Count', |
| 164 | ) |
| 165 | ), |
| 166 | ), |
| 167 | array( |
| 168 | 'source' => TDE_APP_PREFIX, |
| 169 | 'value' => 'student_state', |
| 170 | 'title' => 'Student status', |
| 171 | 'operator_type' => array( |
| 172 | array( |
| 173 | 'title' => '= Equals', |
| 174 | 'value' => 'user_state-is_equal', |
| 175 | ), |
| 176 | array( |
| 177 | 'title' => '≠ Does not equal', |
| 178 | 'value' => 'user_state-not_equal', |
| 179 | ), |
| 180 | ), |
| 181 | 'operand_type' => array_merge( |
| 182 | DROIP_PLUGIN_SETTINGS['SELECT'], |
| 183 | array( |
| 184 | 'options' => array( |
| 185 | array( |
| 186 | 'value' => 'logged_in', |
| 187 | 'title' => 'Logged in', |
| 188 | ), |
| 189 | array( |
| 190 | 'value' => 'wishlisted', |
| 191 | 'title' => 'Wishlisted', |
| 192 | ), |
| 193 | array( |
| 194 | 'value' => 'added_in_cart', |
| 195 | 'title' => 'Added in Cart', |
| 196 | ), |
| 197 | array( |
| 198 | 'value' => 'enrolled', |
| 199 | 'title' => 'Enrolled', |
| 200 | ), |
| 201 | array( |
| 202 | 'value' => 'learner', |
| 203 | 'title' => 'Learner', |
| 204 | ), |
| 205 | array( |
| 206 | 'value' => 'can_retake', |
| 207 | 'title' => 'Can Retake', |
| 208 | ), |
| 209 | array( |
| 210 | 'value' => 'completed', |
| 211 | 'title' => 'Completed', |
| 212 | ), |
| 213 | ), |
| 214 | ) |
| 215 | ), |
| 216 | ), |
| 217 | // [ |
| 218 | // 'source' => TDE_APP_PREFIX, |
| 219 | // 'value' => 'user_authored', |
| 220 | // 'title' => 'User authored', |
| 221 | // 'operator_type' => 'dropdown_operators', |
| 222 | // 'operand_type' => array_merge( |
| 223 | // DROIP_PLUGIN_SETTINGS['SELECT'], |
| 224 | // [ |
| 225 | // 'options' => [ |
| 226 | // ['value' => 'no_review', 'title' => 'No Review'], |
| 227 | // ['value' => 'current_review', 'title' => 'Current Review'], |
| 228 | // ['value' => 'no_question', 'title' => 'No Question'], |
| 229 | // ['value' => 'current_question', 'title' => 'Current Question'], |
| 230 | // ], |
| 231 | // ] |
| 232 | // ), |
| 233 | // ], |
| 234 | array( |
| 235 | 'source' => TDE_APP_PREFIX, |
| 236 | 'value' => 'resource_count', |
| 237 | 'title' => 'Resources', |
| 238 | 'operator_type' => 'numeric_operators', |
| 239 | 'operand_type' => array_merge( |
| 240 | DROIP_PLUGIN_SETTINGS['INPUT_NUMBER'], |
| 241 | array( |
| 242 | 'placeholder' => 'Resource Count', |
| 243 | ) |
| 244 | ), |
| 245 | ), |
| 246 | array( |
| 247 | 'source' => TDE_APP_PREFIX, |
| 248 | 'value' => 'lesson_count', |
| 249 | 'title' => 'Lessons', |
| 250 | 'operator_type' => 'numeric_operators', |
| 251 | 'operand_type' => array_merge( |
| 252 | DROIP_PLUGIN_SETTINGS['INPUT_NUMBER'], |
| 253 | array( |
| 254 | 'placeholder' => 'Lesson Count', |
| 255 | ) |
| 256 | ), |
| 257 | ), |
| 258 | array( |
| 259 | 'source' => TDE_APP_PREFIX, |
| 260 | 'value' => 'quiz_count', |
| 261 | 'title' => 'Quizzes', |
| 262 | 'operator_type' => 'numeric_operators', |
| 263 | 'operand_type' => array_merge( |
| 264 | DROIP_PLUGIN_SETTINGS['INPUT_NUMBER'], |
| 265 | array( |
| 266 | 'placeholder' => 'Quiz Count', |
| 267 | ) |
| 268 | ), |
| 269 | ), |
| 270 | array( |
| 271 | 'source' => TDE_APP_PREFIX, |
| 272 | 'value' => 'assignments_count', |
| 273 | 'title' => 'Assignments', |
| 274 | 'operator_type' => 'numeric_operators', |
| 275 | 'operand_type' => array_merge( |
| 276 | DROIP_PLUGIN_SETTINGS['INPUT_NUMBER'], |
| 277 | array( |
| 278 | 'placeholder' => 'Assignment Count', |
| 279 | ) |
| 280 | ), |
| 281 | ), |
| 282 | array( |
| 283 | 'source' => TDE_APP_PREFIX, |
| 284 | 'value' => 'is_review_enabled', |
| 285 | 'title' => 'Review Enabled', |
| 286 | 'operator_type' => 'boolean_operators', |
| 287 | ), |
| 288 | array( |
| 289 | 'source' => TDE_APP_PREFIX, |
| 290 | 'value' => 'is_qna_enabled', |
| 291 | 'title' => 'QnA Enabled', |
| 292 | 'operator_type' => 'boolean_operators', |
| 293 | ), |
| 294 | array( |
| 295 | 'source' => TDE_APP_PREFIX, |
| 296 | 'value' => 'is_certificate_enabled', |
| 297 | 'title' => 'Certificate Enabled', |
| 298 | 'operator_type' => 'boolean_operators', |
| 299 | ), |
| 300 | ) |
| 301 | ), |
| 302 | ); |
| 303 | return $conditions; |
| 304 | } |
| 305 | |
| 306 | |
| 307 | |
| 308 | public static function element_visibility_condition_check($default_value, $condition, $options) |
| 309 | { |
| 310 | $source = $condition['source']; |
| 311 | $field = $condition['field']['value']; |
| 312 | $operator = $condition['operator']['value']; |
| 313 | $operand = $condition['operand']['value']; |
| 314 | |
| 315 | $fieldValue = self::get_course_field_value($field, $options); |
| 316 | |
| 317 | if ($source === TDE_APP_PREFIX) { |
| 318 | switch ($operator) { |
| 319 | case 'true': { |
| 320 | return (bool) $fieldValue; |
| 321 | } |
| 322 | case 'false': { |
| 323 | return ! $fieldValue; |
| 324 | } |
| 325 | case 'is_equal': { |
| 326 | return $fieldValue == $operand; |
| 327 | } |
| 328 | case 'not_equal': { |
| 329 | return $fieldValue != $operand; |
| 330 | } |
| 331 | case 'less_than': { |
| 332 | return $fieldValue < $operand; |
| 333 | } |
| 334 | case 'greater_than': { |
| 335 | return $fieldValue > $operand; |
| 336 | } |
| 337 | case 'less_than_or_equal': { |
| 338 | return $fieldValue <= $operand; |
| 339 | } |
| 340 | case 'greater_than_or_equal': { |
| 341 | return $fieldValue >= $operand; |
| 342 | } |
| 343 | case 'user_state-is_equal': { |
| 344 | return in_array($operand, $fieldValue); |
| 345 | } |
| 346 | case 'user_state-not_equal': { |
| 347 | return in_array($operand, $fieldValue); |
| 348 | } |
| 349 | } |
| 350 | } |
| 351 | |
| 352 | return $default_value; |
| 353 | } |
| 354 | |
| 355 | |
| 356 | |
| 357 | private static function get_course_field_value($field, $options) |
| 358 | { |
| 359 | $course_id = isset($options['post']) ? $options['post']->ID : get_the_ID(); |
| 360 | switch ($field) { |
| 361 | case 'is_paid': { |
| 362 | $is_paid_course = tutor_utils()->is_course_purchasable($course_id); |
| 363 | return $is_paid_course; |
| 364 | } |
| 365 | case 'on_sale': { |
| 366 | $course_price = tutor_utils()->get_raw_course_price($course_id); |
| 367 | return $course_price->sale_price != 0; |
| 368 | } |
| 369 | case 'average_rating': { |
| 370 | $average_rating = self::get_course_meta($field, $course_id, $options); |
| 371 | $average_rating = floatval(str_replace(',', '', $average_rating)); |
| 372 | return $average_rating; |
| 373 | } |
| 374 | case 'total_ratings': |
| 375 | case 'enroll_count': |
| 376 | case 'resource_count': |
| 377 | case 'lesson_count': |
| 378 | case 'quiz_count': |
| 379 | case 'assignments_count': { |
| 380 | $value = self::get_course_meta($field, $course_id, $options); |
| 381 | return $value; |
| 382 | } |
| 383 | case 'student_state': { |
| 384 | $state = array(); |
| 385 | $entry_box_logic = tutor_entry_box_buttons($course_id); |
| 386 | if (is_user_logged_in()) { |
| 387 | $state[] = 'logged_in'; |
| 388 | if (tutor_utils()->is_wishlisted($course_id)) { |
| 389 | $state[] = 'wishlisted'; |
| 390 | } |
| 391 | |
| 392 | if ($entry_box_logic->show_view_cart_btn) { |
| 393 | $state[] = 'added_in_cart'; |
| 394 | } elseif (tutor_utils()->is_enrolled($course_id)) { |
| 395 | $state[] = 'enrolled'; |
| 396 | } |
| 397 | |
| 398 | if ($entry_box_logic->show_continue_learning_btn) { |
| 399 | $state[] = 'learner'; |
| 400 | } elseif (tutor_utils()->is_completed_course($course_id)) { |
| 401 | $state[] = 'completed'; |
| 402 | } |
| 403 | |
| 404 | if ($entry_box_logic->show_retake_course_btn) { |
| 405 | $state[] = 'can_retake'; |
| 406 | } |
| 407 | } |
| 408 | return $state; |
| 409 | } |
| 410 | case 'is_review_enabled': { |
| 411 | $review_enabled = (bool) get_tutor_option('enable_course_review'); |
| 412 | return $review_enabled; |
| 413 | } |
| 414 | case 'is_qna_enabled': { |
| 415 | $enable_q_and_a_on_course = (bool) get_tutor_option('enable_q_and_a_on_course'); |
| 416 | $disable_qa_for_this_course = get_post_meta($course_id, '_tutor_enable_qa', true) != 'yes' ?? false; |
| 417 | return ! (! $enable_q_and_a_on_course || $disable_qa_for_this_course); |
| 418 | } |
| 419 | case 'is_certificate_enabled': { |
| 420 | $addon_config = tutor_utils()->get_addon_config('tutor-pro/addons/tutor-certificate/tutor-certificate.php'); |
| 421 | $is_enabled = (bool) tutor_utils()->avalue_dot('is_enable', $addon_config); |
| 422 | return $is_enabled; |
| 423 | } |
| 424 | } |
| 425 | } |
| 426 | } |
| 427 |