Addons.php
7 years ago
Admin.php
7 years ago
Ajax.php
7 years ago
Assets.php
7 years ago
Course.php
7 years ago
Gutenberg.php
7 years ago
Instructor.php
7 years ago
Instructors_List.php
7 years ago
Lesson.php
7 years ago
Options.php
7 years ago
Post_types.php
7 years ago
Q_and_A.php
7 years ago
Question.php
7 years ago
Question_Answers_List.php
7 years ago
Quiz.php
7 years ago
Quiz_Attempts_List.php
7 years ago
Rewrite_Rules.php
7 years ago
Shortcode.php
7 years ago
Student.php
7 years ago
Students_List.php
7 years ago
Template.php
7 years ago
Theme_Compatibility.php
7 years ago
Tools.php
7 years ago
Tutor_Base.php
7 years ago
Tutor_List_Table.php
7 years ago
User.php
7 years ago
Utils.php
7 years ago
Video_Stream.php
7 years ago
init.php
7 years ago
Utils.php
2838 lines
| 1 | <?php |
| 2 | namespace TUTOR; |
| 3 | |
| 4 | if ( ! defined( 'ABSPATH' ) ) |
| 5 | exit; |
| 6 | |
| 7 | |
| 8 | class Utils { |
| 9 | /** |
| 10 | * @param null $key |
| 11 | * @param bool $default |
| 12 | * |
| 13 | * @return array|bool|mixed |
| 14 | * |
| 15 | * Get option data |
| 16 | * |
| 17 | * @since v.1.0.0 |
| 18 | */ |
| 19 | public function get_option($key = null, $default = false){ |
| 20 | $option = (array) maybe_unserialize(get_option('tutor_option')); |
| 21 | |
| 22 | if (empty($option) || ! is_array($option)){ |
| 23 | return $default; |
| 24 | } |
| 25 | if ( ! $key){ |
| 26 | return $option; |
| 27 | } |
| 28 | if (array_key_exists($key, $option)){ |
| 29 | return apply_filters($key, $option[$key]); |
| 30 | } |
| 31 | //Access array value via dot notation, such as option->get('value.subvalue') |
| 32 | if (strpos($key, '.')){ |
| 33 | $option_key_array = explode('.', $key); |
| 34 | |
| 35 | $new_option = $option; |
| 36 | foreach ($option_key_array as $dotKey){ |
| 37 | if (isset($new_option[$dotKey])){ |
| 38 | $new_option = $new_option[$dotKey]; |
| 39 | }else{ |
| 40 | return $default; |
| 41 | } |
| 42 | } |
| 43 | return apply_filters($key, $new_option); |
| 44 | } |
| 45 | |
| 46 | return $default; |
| 47 | } |
| 48 | |
| 49 | /** |
| 50 | * @param null $key |
| 51 | * @param bool $value |
| 52 | * |
| 53 | * Update Option |
| 54 | */ |
| 55 | |
| 56 | public function update_option($key = null, $value = false){ |
| 57 | $option = (array) maybe_unserialize(get_option('tutor_option')); |
| 58 | $option[$key] = $value; |
| 59 | update_option('tutor_option', $option); |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * @param null $key |
| 64 | * @param array $array |
| 65 | * |
| 66 | * @return array|bool|mixed |
| 67 | * |
| 68 | * get array value by dot notation |
| 69 | * |
| 70 | * @since v.1.0.0 |
| 71 | * |
| 72 | */ |
| 73 | |
| 74 | public function avalue_dot($key = null, $array = array()){ |
| 75 | $array = (array) $array; |
| 76 | if ( ! $key || ! count($array) ){ |
| 77 | return false; |
| 78 | } |
| 79 | $option_key_array = explode('.', $key); |
| 80 | |
| 81 | $value = $array; |
| 82 | |
| 83 | foreach ($option_key_array as $dotKey){ |
| 84 | if (isset($value[$dotKey])){ |
| 85 | $value = $value[$dotKey]; |
| 86 | }else{ |
| 87 | return false; |
| 88 | } |
| 89 | } |
| 90 | return $value; |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * @return array |
| 95 | * |
| 96 | * Get all pages |
| 97 | */ |
| 98 | public function get_pages(){ |
| 99 | $pages = array(); |
| 100 | $wp_pages = get_pages(); |
| 101 | if (is_array($wp_pages) && count($wp_pages)){ |
| 102 | foreach ($wp_pages as $page){ |
| 103 | $pages[$page->ID] = $page->post_title; |
| 104 | } |
| 105 | } |
| 106 | return $pages; |
| 107 | } |
| 108 | |
| 109 | /** |
| 110 | * @return string |
| 111 | * |
| 112 | * Get course archive URL |
| 113 | */ |
| 114 | public function course_archive_page_url(){ |
| 115 | $course_post_type = tutor()->course_post_type; |
| 116 | $course_page_url = trailingslashit(home_url()).$course_post_type; |
| 117 | |
| 118 | $course_archive_page = $this->get_option('course_archive_page'); |
| 119 | if ($course_archive_page && $course_archive_page !== '-1'){ |
| 120 | $course_page_url = get_permalink($course_archive_page); |
| 121 | } |
| 122 | return trailingslashit($course_page_url); |
| 123 | } |
| 124 | |
| 125 | /** |
| 126 | * @param int $student_id |
| 127 | * |
| 128 | * @return string |
| 129 | * |
| 130 | * Get student URL |
| 131 | */ |
| 132 | |
| 133 | public function profile_url($student_id = 0){ |
| 134 | $site_url = trailingslashit(home_url()).'profile/'; |
| 135 | $user_name = ''; |
| 136 | |
| 137 | $student_id = $this->get_user_id($student_id); |
| 138 | if ($student_id){ |
| 139 | global $wpdb; |
| 140 | $user = $wpdb->get_row("SELECT user_login from {$wpdb->users} WHERE ID = {$student_id} "); |
| 141 | if ($user){ |
| 142 | $user_name = $user->user_login; |
| 143 | } |
| 144 | }else{ |
| 145 | $user_name = 'user_name'; |
| 146 | } |
| 147 | |
| 148 | return $site_url.$user_name; |
| 149 | } |
| 150 | |
| 151 | /** |
| 152 | * @param string $user_login |
| 153 | * |
| 154 | * @return array|null|object |
| 155 | * |
| 156 | * Get user by user login |
| 157 | */ |
| 158 | public function get_user_by_login($user_login = ''){ |
| 159 | global $wpdb; |
| 160 | $user_login = sanitize_text_field($user_login); |
| 161 | $user = $wpdb->get_row("SELECT * from {$wpdb->users} WHERE user_login = '{$user_login}'"); |
| 162 | return $user; |
| 163 | } |
| 164 | |
| 165 | /** |
| 166 | * @return bool |
| 167 | * |
| 168 | * Check if WooCommerce Activated |
| 169 | */ |
| 170 | |
| 171 | public function has_wc(){ |
| 172 | $activated_plugins = apply_filters( 'active_plugins', get_option( 'active_plugins' )); |
| 173 | //$depends = array('woocommerce/woocommerce.php', 'tutor-woocommerce/tutor-woocommerce.php'); |
| 174 | $depends = array('woocommerce/woocommerce.php'); |
| 175 | $has = count(array_intersect($depends, $activated_plugins)) == count($depends); |
| 176 | |
| 177 | return $has; |
| 178 | } |
| 179 | |
| 180 | public function has_edd(){ |
| 181 | $activated_plugins = apply_filters( 'active_plugins', get_option( 'active_plugins' )); |
| 182 | //$depends = array('easy-digital-downloads/easy-digital-downloads.php', 'tutor-edd/tutor-edd.php'); |
| 183 | $depends = array('easy-digital-downloads/easy-digital-downloads.php'); |
| 184 | $has = count(array_intersect($depends, $activated_plugins)) == count($depends); |
| 185 | |
| 186 | return $has; |
| 187 | } |
| 188 | |
| 189 | /** |
| 190 | * @return mixed |
| 191 | * |
| 192 | * |
| 193 | */ |
| 194 | public function languages(){ |
| 195 | $language_codes = array( |
| 196 | 'en' => 'English' , |
| 197 | 'aa' => 'Afar' , |
| 198 | 'ab' => 'Abkhazian' , |
| 199 | 'af' => 'Afrikaans' , |
| 200 | 'am' => 'Amharic' , |
| 201 | 'ar' => 'Arabic' , |
| 202 | 'as' => 'Assamese' , |
| 203 | 'ay' => 'Aymara' , |
| 204 | 'az' => 'Azerbaijani' , |
| 205 | 'ba' => 'Bashkir' , |
| 206 | 'be' => 'Byelorussian' , |
| 207 | 'bg' => 'Bulgarian' , |
| 208 | 'bh' => 'Bihari' , |
| 209 | 'bi' => 'Bislama' , |
| 210 | 'bn' => 'Bengali/Bangla' , |
| 211 | 'bo' => 'Tibetan' , |
| 212 | 'br' => 'Breton' , |
| 213 | 'ca' => 'Catalan' , |
| 214 | 'co' => 'Corsican' , |
| 215 | 'cs' => 'Czech' , |
| 216 | 'cy' => 'Welsh' , |
| 217 | 'da' => 'Danish' , |
| 218 | 'de' => 'German' , |
| 219 | 'dz' => 'Bhutani' , |
| 220 | 'el' => 'Greek' , |
| 221 | 'eo' => 'Esperanto' , |
| 222 | 'es' => 'Spanish' , |
| 223 | 'et' => 'Estonian' , |
| 224 | 'eu' => 'Basque' , |
| 225 | 'fa' => 'Persian' , |
| 226 | 'fi' => 'Finnish' , |
| 227 | 'fj' => 'Fiji' , |
| 228 | 'fo' => 'Faeroese' , |
| 229 | 'fr' => 'French' , |
| 230 | 'fy' => 'Frisian' , |
| 231 | 'ga' => 'Irish' , |
| 232 | 'gd' => 'Scots/Gaelic' , |
| 233 | 'gl' => 'Galician' , |
| 234 | 'gn' => 'Guarani' , |
| 235 | 'gu' => 'Gujarati' , |
| 236 | 'ha' => 'Hausa' , |
| 237 | 'hi' => 'Hindi' , |
| 238 | 'hr' => 'Croatian' , |
| 239 | 'hu' => 'Hungarian' , |
| 240 | 'hy' => 'Armenian' , |
| 241 | 'ia' => 'Interlingua' , |
| 242 | 'ie' => 'Interlingue' , |
| 243 | 'ik' => 'Inupiak' , |
| 244 | 'in' => 'Indonesian' , |
| 245 | 'is' => 'Icelandic' , |
| 246 | 'it' => 'Italian' , |
| 247 | 'iw' => 'Hebrew' , |
| 248 | 'ja' => 'Japanese' , |
| 249 | 'ji' => 'Yiddish' , |
| 250 | 'jw' => 'Javanese' , |
| 251 | 'ka' => 'Georgian' , |
| 252 | 'kk' => 'Kazakh' , |
| 253 | 'kl' => 'Greenlandic' , |
| 254 | 'km' => 'Cambodian' , |
| 255 | 'kn' => 'Kannada' , |
| 256 | 'ko' => 'Korean' , |
| 257 | 'ks' => 'Kashmiri' , |
| 258 | 'ku' => 'Kurdish' , |
| 259 | 'ky' => 'Kirghiz' , |
| 260 | 'la' => 'Latin' , |
| 261 | 'ln' => 'Lingala' , |
| 262 | 'lo' => 'Laothian' , |
| 263 | 'lt' => 'Lithuanian' , |
| 264 | 'lv' => 'Latvian/Lettish' , |
| 265 | 'mg' => 'Malagasy' , |
| 266 | 'mi' => 'Maori' , |
| 267 | 'mk' => 'Macedonian' , |
| 268 | 'ml' => 'Malayalam' , |
| 269 | 'mn' => 'Mongolian' , |
| 270 | 'mo' => 'Moldavian' , |
| 271 | 'mr' => 'Marathi' , |
| 272 | 'ms' => 'Malay' , |
| 273 | 'mt' => 'Maltese' , |
| 274 | 'my' => 'Burmese' , |
| 275 | 'na' => 'Nauru' , |
| 276 | 'ne' => 'Nepali' , |
| 277 | 'nl' => 'Dutch' , |
| 278 | 'no' => 'Norwegian' , |
| 279 | 'oc' => 'Occitan' , |
| 280 | 'om' => '(Afan)/Oromoor/Oriya' , |
| 281 | 'pa' => 'Punjabi' , |
| 282 | 'pl' => 'Polish' , |
| 283 | 'ps' => 'Pashto/Pushto' , |
| 284 | 'pt' => 'Portuguese' , |
| 285 | 'qu' => 'Quechua' , |
| 286 | 'rm' => 'Rhaeto-Romance' , |
| 287 | 'rn' => 'Kirundi' , |
| 288 | 'ro' => 'Romanian' , |
| 289 | 'ru' => 'Russian' , |
| 290 | 'rw' => 'Kinyarwanda' , |
| 291 | 'sa' => 'Sanskrit' , |
| 292 | 'sd' => 'Sindhi' , |
| 293 | 'sg' => 'Sangro' , |
| 294 | 'sh' => 'Serbo-Croatian' , |
| 295 | 'si' => 'Singhalese' , |
| 296 | 'sk' => 'Slovak' , |
| 297 | 'sl' => 'Slovenian' , |
| 298 | 'sm' => 'Samoan' , |
| 299 | 'sn' => 'Shona' , |
| 300 | 'so' => 'Somali' , |
| 301 | 'sq' => 'Albanian' , |
| 302 | 'sr' => 'Serbian' , |
| 303 | 'ss' => 'Siswati' , |
| 304 | 'st' => 'Sesotho' , |
| 305 | 'su' => 'Sundanese' , |
| 306 | 'sv' => 'Swedish' , |
| 307 | 'sw' => 'Swahili' , |
| 308 | 'ta' => 'Tamil' , |
| 309 | 'te' => 'Tegulu' , |
| 310 | 'tg' => 'Tajik' , |
| 311 | 'th' => 'Thai' , |
| 312 | 'ti' => 'Tigrinya' , |
| 313 | 'tk' => 'Turkmen' , |
| 314 | 'tl' => 'Tagalog' , |
| 315 | 'tn' => 'Setswana' , |
| 316 | 'to' => 'Tonga' , |
| 317 | 'tr' => 'Turkish' , |
| 318 | 'ts' => 'Tsonga' , |
| 319 | 'tt' => 'Tatar' , |
| 320 | 'tw' => 'Twi' , |
| 321 | 'uk' => 'Ukrainian' , |
| 322 | 'ur' => 'Urdu' , |
| 323 | 'uz' => 'Uzbek' , |
| 324 | 'vi' => 'Vietnamese' , |
| 325 | 'vo' => 'Volapuk' , |
| 326 | 'wo' => 'Wolof' , |
| 327 | 'xh' => 'Xhosa' , |
| 328 | 'yo' => 'Yoruba' , |
| 329 | 'zh' => 'Chinese' , |
| 330 | 'zu' => 'Zulu' , |
| 331 | ); |
| 332 | |
| 333 | return apply_filters('tutor/utils/languages', $language_codes); |
| 334 | } |
| 335 | |
| 336 | public function print_view($value = ''){ |
| 337 | echo '<pre>'; |
| 338 | print_r($value); |
| 339 | echo '</pre>'; |
| 340 | } |
| 341 | |
| 342 | public function get_courses($excludes = array()){ |
| 343 | global $wpdb; |
| 344 | |
| 345 | |
| 346 | $excludes = (array) $excludes; |
| 347 | $exclude_query = ''; |
| 348 | if (count($excludes)){ |
| 349 | $exclude_query = implode("','", $excludes); |
| 350 | } |
| 351 | |
| 352 | $course_post_type = tutor()->course_post_type; |
| 353 | $query = $wpdb->get_results("SELECT ID, post_author, post_title, post_name,post_status, menu_order |
| 354 | from {$wpdb->posts} WHERE post_status = 'publish' |
| 355 | AND ID NOT IN('$exclude_query') |
| 356 | AND post_type = '{$course_post_type}' "); |
| 357 | return $query; |
| 358 | } |
| 359 | |
| 360 | public function get_courses_for_instructors($instructor_id = 0){ |
| 361 | global $wpdb; |
| 362 | |
| 363 | $instructor_id = $this->get_user_id($instructor_id); |
| 364 | |
| 365 | $course_post_type = tutor()->course_post_type; |
| 366 | $query = $wpdb->get_results("SELECT ID, post_author, post_title, post_name,post_status, menu_order |
| 367 | from {$wpdb->posts} |
| 368 | WHERE post_author = {$instructor_id} |
| 369 | AND post_status IN ('publish', 'pending') |
| 370 | AND post_type = '{$course_post_type}' "); |
| 371 | return $query; |
| 372 | } |
| 373 | |
| 374 | public function get_course_count_by_instructor($instructor_id){ |
| 375 | global $wpdb; |
| 376 | |
| 377 | $course_post_type = tutor()->course_post_type; |
| 378 | $count = $wpdb->get_var("SELECT COUNT(ID) from {$wpdb->posts} |
| 379 | INNER JOIN {$wpdb->usermeta} ON user_id = {$instructor_id} AND meta_key = '_tutor_instructor_course_id' AND meta_value = ID |
| 380 | WHERE post_status = 'publish' |
| 381 | AND post_type = '{$course_post_type}' ; "); |
| 382 | |
| 383 | return $count; |
| 384 | } |
| 385 | |
| 386 | public function get_courses_by_instructor($instructor_id){ |
| 387 | global $wpdb; |
| 388 | |
| 389 | $course_post_type = tutor()->course_post_type; |
| 390 | |
| 391 | $querystr = " |
| 392 | SELECT $wpdb->posts.* |
| 393 | FROM $wpdb->posts |
| 394 | INNER JOIN {$wpdb->usermeta} ON $wpdb->usermeta.user_id = {$instructor_id} AND $wpdb->usermeta.meta_key = '_tutor_instructor_course_id' AND $wpdb->usermeta.meta_value = $wpdb->posts.ID |
| 395 | |
| 396 | |
| 397 | WHERE $wpdb->posts.post_status = 'publish' |
| 398 | AND $wpdb->posts.post_type = '{$course_post_type}' |
| 399 | AND $wpdb->posts.post_date < NOW() |
| 400 | ORDER BY $wpdb->posts.post_date DESC"; |
| 401 | |
| 402 | $pageposts = $wpdb->get_results($querystr, OBJECT); |
| 403 | return $pageposts; |
| 404 | } |
| 405 | |
| 406 | public function get_archive_page_course_count(){ |
| 407 | global $wp_query; |
| 408 | return $wp_query->post_count; |
| 409 | } |
| 410 | |
| 411 | public function get_course_count(){ |
| 412 | global $wpdb; |
| 413 | |
| 414 | $course_post_type = tutor()->course_post_type; |
| 415 | $count = $wpdb->get_var("SELECT COUNT(ID) from {$wpdb->posts} WHERE post_status = 'publish' AND post_type = '{$course_post_type}'; "); |
| 416 | return $count; |
| 417 | } |
| 418 | |
| 419 | public function get_lesson_count(){ |
| 420 | global $wpdb; |
| 421 | |
| 422 | $lesson_post_type = tutor()->lesson_post_type; |
| 423 | $count = $wpdb->get_var("SELECT COUNT(ID) from {$wpdb->posts} WHERE post_status = 'publish' AND post_type = '{$lesson_post_type}'; "); |
| 424 | return $count; |
| 425 | } |
| 426 | |
| 427 | public function get_lesson($course_id = 0, $limit = 10){ |
| 428 | $course_id = $this->get_post_id($course_id); |
| 429 | |
| 430 | $lesson_post_type = tutor()->lesson_post_type; |
| 431 | $args = array( |
| 432 | 'post_status' => 'publish', |
| 433 | 'post_type' => $lesson_post_type, |
| 434 | 'posts_per_page' => $limit, |
| 435 | 'meta_query' => array( |
| 436 | array( |
| 437 | 'key' => '_tutor_course_id_for_lesson', |
| 438 | 'value' => $course_id, |
| 439 | 'compare' => '=', |
| 440 | ), |
| 441 | ), |
| 442 | ); |
| 443 | $query = new \WP_Query($args); |
| 444 | |
| 445 | return $query; |
| 446 | } |
| 447 | |
| 448 | public function get_lesson_count_by_course($course_id = 0){ |
| 449 | $course_id = $this->get_post_id($course_id); |
| 450 | global $wpdb; |
| 451 | |
| 452 | $count_lesson = $wpdb->get_var("select count(meta_id) from {$wpdb->postmeta} where meta_key = '_tutor_course_id_for_lesson' AND meta_value = {$course_id} "); |
| 453 | |
| 454 | return (int) $count_lesson; |
| 455 | } |
| 456 | |
| 457 | public function get_completed_lesson_count_by_course($course_id = 0, $user_id = 0){ |
| 458 | $course_id = $this->get_post_id($course_id); |
| 459 | $user_id = $this->get_user_id($user_id); |
| 460 | global $wpdb; |
| 461 | |
| 462 | $completed_lesson_ids = $wpdb->get_col("select post_id from {$wpdb->postmeta} where meta_key = '_tutor_course_id_for_lesson' AND meta_value = {$course_id} "); |
| 463 | |
| 464 | $count = 0; |
| 465 | if (is_array($completed_lesson_ids) && count($completed_lesson_ids)){ |
| 466 | $completed_lesson_meta_ids = array(); |
| 467 | foreach ($completed_lesson_ids as $lesson_id){ |
| 468 | $completed_lesson_meta_ids[] = '_tutor_completed_lesson_id_'.$lesson_id; |
| 469 | } |
| 470 | $in_ids = implode("','", $completed_lesson_meta_ids); |
| 471 | |
| 472 | $count = (int) $wpdb->get_var("select count(umeta_id) from {$wpdb->usermeta} WHERE user_id = '{$user_id}' AND meta_key in('{$in_ids}') "); |
| 473 | } |
| 474 | |
| 475 | return $count; |
| 476 | } |
| 477 | |
| 478 | /** |
| 479 | * @param int $course_id |
| 480 | * @param int $user_id |
| 481 | * |
| 482 | * @return float|int |
| 483 | * |
| 484 | */ |
| 485 | public function get_course_completed_percent($course_id = 0, $user_id = 0){ |
| 486 | $course_id = $this->get_post_id($course_id); |
| 487 | $user_id = $this->get_user_id($user_id); |
| 488 | |
| 489 | $total_lesson = $this->get_lesson_count_by_course($course_id); |
| 490 | $completed_lesson = $this->get_completed_lesson_count_by_course($course_id, $user_id); |
| 491 | |
| 492 | if ($total_lesson > 0 && $completed_lesson > 0){ |
| 493 | return number_format(($completed_lesson * 100) / $total_lesson, 1); |
| 494 | } |
| 495 | |
| 496 | return 0; |
| 497 | } |
| 498 | |
| 499 | public function get_topics($course_id = 0){ |
| 500 | $course_id = $this->get_post_id($course_id); |
| 501 | |
| 502 | $args = array( |
| 503 | 'post_type' => 'topics', |
| 504 | 'post_parent' => $course_id, |
| 505 | 'orderby' => 'menu_order', |
| 506 | 'order' => 'ASC', |
| 507 | 'posts_per_page' => -1, |
| 508 | ); |
| 509 | |
| 510 | $query = new \WP_Query($args); |
| 511 | return $query; |
| 512 | } |
| 513 | |
| 514 | public function get_next_topic_order_id($course_ID){ |
| 515 | global $wpdb; |
| 516 | |
| 517 | $last_order = (int) $wpdb->get_var("SELECT MAX(menu_order) FROM {$wpdb->posts} WHERE post_parent = {$course_ID} AND post_type = 'topics';"); |
| 518 | return $last_order + 1; |
| 519 | } |
| 520 | |
| 521 | public function get_lessons_by_topic($topics_id = 0, $limit = 10){ |
| 522 | $topics_id = $this->get_post_id($topics_id); |
| 523 | |
| 524 | $lesson_post_type = tutor()->lesson_post_type; |
| 525 | $args = array( |
| 526 | 'post_type' => $lesson_post_type, |
| 527 | 'post_parent' => $topics_id, |
| 528 | 'posts_per_page' => $limit, |
| 529 | 'orderby' => 'menu_order', |
| 530 | 'order' => 'ASC', |
| 531 | ); |
| 532 | |
| 533 | $query = new \WP_Query($args); |
| 534 | |
| 535 | return $query; |
| 536 | } |
| 537 | |
| 538 | public function checking_nonce($request_method = 'post'){ |
| 539 | if ($request_method === 'post'){ |
| 540 | if (!isset($_POST[tutor()->nonce]) || !wp_verify_nonce($_POST[tutor()->nonce], tutor()->nonce_action)) { |
| 541 | exit(); |
| 542 | } |
| 543 | }else{ |
| 544 | if (!isset($_GET[tutor()->nonce]) || !wp_verify_nonce($_GET[tutor()->nonce], tutor()->nonce_action)) { |
| 545 | exit(); |
| 546 | } |
| 547 | } |
| 548 | } |
| 549 | |
| 550 | /** |
| 551 | * @param int $course_id |
| 552 | * |
| 553 | * @return bool |
| 554 | * |
| 555 | * @since v.1.0.0 |
| 556 | */ |
| 557 | public function is_course_purchasable($course_id = 0){ |
| 558 | return apply_filters('is_course_purchasable', false, $course_id); |
| 559 | } |
| 560 | |
| 561 | /** |
| 562 | * @param int $course_id |
| 563 | * |
| 564 | * @return null|string |
| 565 | * |
| 566 | * get course price in digits format if any |
| 567 | * |
| 568 | * @since v.1.0.0 |
| 569 | */ |
| 570 | |
| 571 | public function get_course_price($course_id = 0){ |
| 572 | $course_id = $this->get_post_id($course_id); |
| 573 | |
| 574 | $price = null; |
| 575 | |
| 576 | if ($this->is_course_purchasable()) { |
| 577 | if ($this->has_wc()){ |
| 578 | $product_id = tutor_utils()->get_course_product_id($course_id); |
| 579 | $product = wc_get_product( $product_id ); |
| 580 | |
| 581 | if ( $product ) { |
| 582 | $price = $product->get_price(); |
| 583 | } |
| 584 | }else{ |
| 585 | $price = apply_filters('get_tutor_course_price', null, $course_id); |
| 586 | } |
| 587 | |
| 588 | } |
| 589 | |
| 590 | return $price; |
| 591 | } |
| 592 | |
| 593 | /** |
| 594 | * @param int $course_id |
| 595 | * |
| 596 | * @return array|bool|null|object |
| 597 | * |
| 598 | * Check if current user has been enrolled or not |
| 599 | * |
| 600 | * @since v.1.0.0 |
| 601 | */ |
| 602 | |
| 603 | public function is_enrolled($course_id = 0, $user_id = 0){ |
| 604 | $course_id = $this->get_post_id($course_id); |
| 605 | $user_id = $this->get_user_id($user_id); |
| 606 | |
| 607 | if (is_user_logged_in()) { |
| 608 | global $wpdb; |
| 609 | |
| 610 | $getEnrolledInfo = $wpdb->get_row( "select ID, post_author, post_date,post_date_gmt,post_title from {$wpdb->posts} WHERE post_type = 'tutor_enrolled' AND post_parent = {$course_id} AND post_author = {$user_id} AND post_status = 'completed'; " ); |
| 611 | |
| 612 | if ( $getEnrolledInfo ) { |
| 613 | return $getEnrolledInfo; |
| 614 | } |
| 615 | } |
| 616 | return false; |
| 617 | } |
| 618 | |
| 619 | public function has_any_enrolled($course_id = 0, $user_id = 0){ |
| 620 | $course_id = $this->get_post_id($course_id); |
| 621 | $user_id = $this->get_user_id($user_id); |
| 622 | |
| 623 | if (is_user_logged_in()) { |
| 624 | global $wpdb; |
| 625 | |
| 626 | $getEnrolledInfo = $wpdb->get_row( "select ID, post_author, post_date,post_date_gmt,post_title from {$wpdb->posts} WHERE post_type = 'tutor_enrolled' AND post_parent = {$course_id} AND post_author = {$user_id}; " ); |
| 627 | |
| 628 | if ( $getEnrolledInfo ) { |
| 629 | return $getEnrolledInfo; |
| 630 | } |
| 631 | } |
| 632 | return false; |
| 633 | } |
| 634 | |
| 635 | /** |
| 636 | * @param int $lesson_id |
| 637 | * @param int $user_id |
| 638 | * |
| 639 | * @return array|bool|null|object |
| 640 | * |
| 641 | * Get the course Enrolled confirmation by lesson ID |
| 642 | * |
| 643 | * @since v.1.0.0 |
| 644 | */ |
| 645 | |
| 646 | public function is_course_enrolled_by_lesson($lesson_id = 0, $user_id = 0){ |
| 647 | $lesson_id = $this->get_post_id($lesson_id); |
| 648 | $user_id = $this->get_user_id($user_id); |
| 649 | |
| 650 | return $this->is_enrolled($this->get_course_id_by_lesson($lesson_id)); |
| 651 | } |
| 652 | |
| 653 | /** |
| 654 | * @param int $lesson_id |
| 655 | * |
| 656 | * @return bool|mixed |
| 657 | * |
| 658 | * Get the course ID by Lesson |
| 659 | * |
| 660 | * @since v.1.0.0 |
| 661 | */ |
| 662 | public function get_course_id_by_lesson($lesson_id = 0){ |
| 663 | $lesson_id = $this->get_post_id($lesson_id); |
| 664 | return get_post_meta($lesson_id, '_tutor_course_id_for_lesson', true); |
| 665 | } |
| 666 | |
| 667 | /** |
| 668 | * @param int $course_id |
| 669 | * |
| 670 | * @return bool|false|string |
| 671 | * |
| 672 | * Get first lesson of a course |
| 673 | * |
| 674 | * @since v.1.0.0 |
| 675 | */ |
| 676 | public function get_course_first_lesson($course_id = 0){ |
| 677 | $course_id = $this->get_post_id($course_id); |
| 678 | global $wpdb; |
| 679 | |
| 680 | $lesson_id = $wpdb->get_var(" |
| 681 | SELECT post_id as lesson_id |
| 682 | FROM $wpdb->postmeta |
| 683 | INNER JOIN {$wpdb->posts} ON post_id = {$wpdb->posts}.ID |
| 684 | WHERE meta_key = '_tutor_course_id_for_lesson' AND meta_value = {$course_id} |
| 685 | |
| 686 | ORDER BY menu_order ASC LIMIT 1 |
| 687 | "); |
| 688 | |
| 689 | /* |
| 690 | $lesson_id = $wpdb->get_var(" select main_posts.ID from {$wpdb->posts} main_posts |
| 691 | WHERE post_parent = |
| 692 | (SELECT sub_posts.ID FROM {$wpdb->posts} sub_posts |
| 693 | WHERE post_type = 'topics' AND |
| 694 | sub_posts.post_parent = {$course_id} ORDER BY sub_posts.menu_order ASC LIMIT 1 ) |
| 695 | ORDER BY main_posts.menu_order ASC LIMIT 1 ;"); |
| 696 | */ |
| 697 | |
| 698 | if ($lesson_id){ |
| 699 | return get_permalink($lesson_id); |
| 700 | } |
| 701 | return false; |
| 702 | } |
| 703 | |
| 704 | /* |
| 705 | * |
| 706 | * Get course sub pages in course dashboard |
| 707 | * |
| 708 | * @since v.1.0.0 |
| 709 | */ |
| 710 | public function course_sub_pages(){ |
| 711 | $nav_items = array( |
| 712 | 'overview' => __('Overview', 'tutor'), |
| 713 | ); |
| 714 | |
| 715 | $enable_q_and_a_on_course = tutor_utils()->get_option('enable_q_and_a_on_course'); |
| 716 | if ($enable_q_and_a_on_course){ |
| 717 | $nav_items['questions'] = __('Q&A', 'tutor'); |
| 718 | } |
| 719 | $nav_items['announcements'] = __('Announcements', 'tutor'); |
| 720 | |
| 721 | return apply_filters('tutor_course/single/enrolled/nav_items', $nav_items); |
| 722 | } |
| 723 | |
| 724 | /** |
| 725 | * @param int $post_id |
| 726 | * |
| 727 | * @return bool|array |
| 728 | * |
| 729 | * @since v.1.0.0 |
| 730 | */ |
| 731 | public function get_video($post_id = 0){ |
| 732 | $post_id = $this->get_post_id($post_id); |
| 733 | $attachments = get_post_meta($post_id, '_video', true); |
| 734 | if ($attachments) { |
| 735 | $attachments = maybe_unserialize($attachments); |
| 736 | } |
| 737 | return $attachments; |
| 738 | } |
| 739 | |
| 740 | /** |
| 741 | * @param int $post_id |
| 742 | * @param array $video_data |
| 743 | * |
| 744 | * @return bool |
| 745 | * |
| 746 | * Update the video Info |
| 747 | */ |
| 748 | public function update_video($post_id = 0, $video_data = array()){ |
| 749 | $post_id = $this->get_post_id($post_id); |
| 750 | |
| 751 | if (is_array($video_data) && count($video_data)){ |
| 752 | update_post_meta($post_id, '_video', $video_data); |
| 753 | } |
| 754 | } |
| 755 | |
| 756 | /** |
| 757 | * @param int $post_id |
| 758 | * |
| 759 | * @return bool|mixed |
| 760 | * |
| 761 | * @since v.1.0.0 |
| 762 | */ |
| 763 | public function get_attachments($post_id = 0){ |
| 764 | $post_id = $this->get_post_id($post_id); |
| 765 | $attachments_arr = array(); |
| 766 | $attachments = maybe_unserialize(get_post_meta($post_id, '_tutor_attachments', true)); |
| 767 | |
| 768 | $font_icons = apply_filters('tutor_file_types_icon', array( |
| 769 | 'archive', |
| 770 | 'audio', |
| 771 | 'code', |
| 772 | 'default', |
| 773 | 'document', |
| 774 | 'interactive', |
| 775 | 'spreadsheet', |
| 776 | 'text', |
| 777 | 'video', |
| 778 | 'image', |
| 779 | )); |
| 780 | |
| 781 | if ( is_array($attachments) && count($attachments)) { |
| 782 | foreach ( $attachments as $attachment ) { |
| 783 | $url = wp_get_attachment_url( $attachment ); |
| 784 | $file_type = wp_check_filetype( $url ); |
| 785 | $ext = $file_type['ext']; |
| 786 | $title = get_the_title($attachment); |
| 787 | |
| 788 | $file_path = get_attached_file( $attachment ); |
| 789 | $size_bytes = file_exists($file_path) ? filesize( $file_path ) : 0; |
| 790 | $size = size_format( $size_bytes, 2 ); |
| 791 | $type = wp_ext2type( $ext ); |
| 792 | |
| 793 | $icon = 'default'; |
| 794 | if ( $type && in_array( $type, $font_icons ) ) { |
| 795 | $icon = $type; |
| 796 | } |
| 797 | |
| 798 | $data = array( |
| 799 | 'post_id' => $post_id, |
| 800 | 'id' => $attachment, |
| 801 | 'url' => $url, |
| 802 | 'name' => $title . '.' . $ext, |
| 803 | 'title' => $title, |
| 804 | 'ext' => $ext, |
| 805 | 'size' => $size, |
| 806 | 'size_bytes' => $size_bytes, |
| 807 | 'icon' => $icon, |
| 808 | ); |
| 809 | |
| 810 | $attachments_arr[] = (object) apply_filters( 'tutor/posts/attachments', $data ); |
| 811 | } |
| 812 | } |
| 813 | |
| 814 | return $attachments_arr; |
| 815 | } |
| 816 | |
| 817 | |
| 818 | /** |
| 819 | * @param $seconds |
| 820 | * |
| 821 | * @return string |
| 822 | * |
| 823 | * return seconds to formatted playtime |
| 824 | * |
| 825 | * @since v.1.0.0 |
| 826 | */ |
| 827 | public function playtime_string($seconds) { |
| 828 | $sign = (($seconds < 0) ? '-' : ''); |
| 829 | $seconds = round(abs($seconds)); |
| 830 | $H = (int) floor( $seconds / 3600); |
| 831 | $M = (int) floor(($seconds - (3600 * $H) ) / 60); |
| 832 | $S = (int) round( $seconds - (3600 * $H) - (60 * $M) ); |
| 833 | return $sign.($H ? $H.':' : '').($H ? str_pad($M, 2, '0', STR_PAD_LEFT) : intval($M)).':'.str_pad($S, 2, 0, STR_PAD_LEFT); |
| 834 | } |
| 835 | |
| 836 | /** |
| 837 | * @param $seconds |
| 838 | * |
| 839 | * @return array |
| 840 | * |
| 841 | * Get the playtime in array |
| 842 | */ |
| 843 | public function playtime_array($seconds){ |
| 844 | $run_time_format = array( |
| 845 | 'hours' => '00', |
| 846 | 'minutes' => '00', |
| 847 | 'seconds' => '00', |
| 848 | ); |
| 849 | |
| 850 | if ($seconds <= 0 ){ |
| 851 | return $run_time_format; |
| 852 | } |
| 853 | |
| 854 | $playTimeString = $this->playtime_string($seconds); |
| 855 | $timeInArray = explode(':', $playTimeString); |
| 856 | |
| 857 | $run_time_size = count($timeInArray); |
| 858 | if ($run_time_size === 3){ |
| 859 | $run_time_format['hours'] = $timeInArray[0]; |
| 860 | $run_time_format['minutes'] = $timeInArray[1]; |
| 861 | $run_time_format['seconds'] = $timeInArray[2]; |
| 862 | }elseif($run_time_size === 2){ |
| 863 | $run_time_format['minutes'] = $timeInArray[0]; |
| 864 | $run_time_format['seconds'] = $timeInArray[1]; |
| 865 | } |
| 866 | |
| 867 | return $run_time_format; |
| 868 | } |
| 869 | |
| 870 | public function seconds_to_time_context($seconds) { |
| 871 | $sign = (($seconds < 0) ? '-' : ''); |
| 872 | $seconds = round(abs($seconds)); |
| 873 | $H = (int) floor( $seconds / 3600); |
| 874 | $M = (int) floor(($seconds - (3600 * $H) ) / 60); |
| 875 | $S = (int) round( $seconds - (3600 * $H) - (60 * $M) ); |
| 876 | |
| 877 | return $sign.($H ? $H.'h ' : '').($H ? str_pad($M, 2, '0', STR_PAD_LEFT) : intval($M)).'m '.str_pad($S, 2, 0, STR_PAD_LEFT).'s'; |
| 878 | } |
| 879 | |
| 880 | /** |
| 881 | * @param int $lesson_id |
| 882 | * |
| 883 | * @return bool|object |
| 884 | * |
| 885 | * @since v.1.0.0 |
| 886 | */ |
| 887 | |
| 888 | public function get_video_info($lesson_id = 0){ |
| 889 | $lesson_id = $this->get_post_id($lesson_id); |
| 890 | $video = $this->get_video($lesson_id); |
| 891 | |
| 892 | if ( ! $video){ |
| 893 | return false; |
| 894 | } |
| 895 | |
| 896 | $info = array( |
| 897 | 'playtime' => '00:00', |
| 898 | ); |
| 899 | |
| 900 | $types = apply_filters('tutor_video_types', array("mp4"=>"video/mp4", "webm"=>"video/webm", "ogg"=>"video/ogg")); |
| 901 | |
| 902 | $videoSource = $this->avalue_dot('source', $video); |
| 903 | if ($videoSource === 'html5'){ |
| 904 | $sourceVideoID = $this->avalue_dot('source_video_id', $video); |
| 905 | $video_info = get_post_meta($sourceVideoID, '_wp_attachment_metadata', true); |
| 906 | |
| 907 | if ($video_info){ |
| 908 | $path = get_attached_file($sourceVideoID); |
| 909 | $info['playtime'] = $video_info['length_formatted']; |
| 910 | $info['path'] = $path; |
| 911 | $info['url'] = wp_get_attachment_url($sourceVideoID); |
| 912 | $info['ext'] = strtolower(pathinfo($path, PATHINFO_EXTENSION)); |
| 913 | $info['type'] = $types[$info['ext']]; |
| 914 | } |
| 915 | } |
| 916 | |
| 917 | if ($videoSource !== 'html5'){ |
| 918 | $video = maybe_unserialize(get_post_meta($lesson_id, '_video', true)); |
| 919 | |
| 920 | $runtimeHours = tutor_utils()->avalue_dot('runtime.hours', $video); |
| 921 | $runtimeMinutes = tutor_utils()->avalue_dot('runtime.minutes', $video); |
| 922 | $runtimeSeconds = tutor_utils()->avalue_dot('runtime.seconds', $video); |
| 923 | |
| 924 | $runtimeHours = $runtimeHours ? $runtimeHours : '00'; |
| 925 | $runtimeMinutes = $runtimeMinutes ? $runtimeMinutes : '00'; |
| 926 | $runtimeSeconds = $runtimeSeconds ? $runtimeSeconds : '00'; |
| 927 | |
| 928 | $info['playtime'] = "$runtimeHours:$runtimeMinutes:$runtimeSeconds"; |
| 929 | } |
| 930 | |
| 931 | $info = array_merge($info, $video); |
| 932 | |
| 933 | return (object) $info; |
| 934 | } |
| 935 | |
| 936 | /** |
| 937 | * @param int $post_id |
| 938 | * |
| 939 | * @return bool |
| 940 | * |
| 941 | * Ensure if attached video is self hosted or not |
| 942 | */ |
| 943 | public function is_html5_video($post_id = 0){ |
| 944 | $post_id = $this->get_post_id($post_id); |
| 945 | |
| 946 | $video = $this->get_video($post_id); |
| 947 | if ( ! $video){ |
| 948 | return false; |
| 949 | } |
| 950 | $videoSource = $this->avalue_dot('source', $video); |
| 951 | return $videoSource === 'html5'; |
| 952 | } |
| 953 | |
| 954 | /** |
| 955 | * |
| 956 | * return lesson type icon |
| 957 | * |
| 958 | * @param int $lesson_id |
| 959 | * @param bool $html |
| 960 | * @param bool $echo |
| 961 | * |
| 962 | * @return string |
| 963 | * |
| 964 | * @since v.1.0.0 |
| 965 | */ |
| 966 | |
| 967 | public function get_lesson_type_icon($lesson_id = 0, $html = false, $echo = false){ |
| 968 | $post_id = $this->get_post_id($lesson_id); |
| 969 | $video = tutor_utils()->get_video_info($post_id); |
| 970 | |
| 971 | $play_time = false; |
| 972 | if ($video){ |
| 973 | $play_time = $video->playtime; |
| 974 | } |
| 975 | |
| 976 | $tutor_lesson_type_icon = $play_time ? 'youtube' : 'document'; |
| 977 | |
| 978 | if ($html){ |
| 979 | $tutor_lesson_type_icon = "<i class='tutor-icon-$tutor_lesson_type_icon'></i> "; |
| 980 | } |
| 981 | |
| 982 | if ($tutor_lesson_type_icon){ |
| 983 | echo $tutor_lesson_type_icon; |
| 984 | } |
| 985 | |
| 986 | return $tutor_lesson_type_icon; |
| 987 | } |
| 988 | |
| 989 | /** |
| 990 | * @param int $lesson_id |
| 991 | * @param int $user_id |
| 992 | * |
| 993 | * @return bool|mixed |
| 994 | * |
| 995 | * @since v.1.0.0 |
| 996 | */ |
| 997 | |
| 998 | public function is_completed_lesson($lesson_id = 0, $user_id = 0){ |
| 999 | $lesson_id = $this->get_post_id($lesson_id); |
| 1000 | $user_id = $this->get_user_id($user_id); |
| 1001 | |
| 1002 | $is_completed = get_user_meta($user_id, '_tutor_completed_lesson_id_'.$lesson_id, true); |
| 1003 | |
| 1004 | if ($is_completed){ |
| 1005 | return $is_completed; |
| 1006 | } |
| 1007 | |
| 1008 | return false; |
| 1009 | } |
| 1010 | |
| 1011 | /** |
| 1012 | * @param int $course_id |
| 1013 | * @param int $user_id |
| 1014 | * |
| 1015 | * @return array|bool|null|object|void |
| 1016 | * |
| 1017 | * Determine if a course completed |
| 1018 | */ |
| 1019 | |
| 1020 | public function is_completed_course($course_id = 0, $user_id = 0){ |
| 1021 | global $wpdb; |
| 1022 | $course_id = $this->get_post_id($course_id); |
| 1023 | $user_id = $this->get_user_id($user_id); |
| 1024 | |
| 1025 | $is_completed = $wpdb->get_row("SELECT comment_ID, |
| 1026 | comment_post_ID as course_id, |
| 1027 | comment_author as completed_user_id, |
| 1028 | comment_date as completion_date, |
| 1029 | comment_content as completed_hash |
| 1030 | from {$wpdb->comments} |
| 1031 | WHERE comment_agent = 'TutorLMSPlugin' |
| 1032 | AND comment_type = 'course_completed' |
| 1033 | AND comment_post_ID = {$course_id} |
| 1034 | AND user_id = {$user_id} ;"); |
| 1035 | |
| 1036 | if ($is_completed){ |
| 1037 | return $is_completed; |
| 1038 | } |
| 1039 | |
| 1040 | return false; |
| 1041 | } |
| 1042 | |
| 1043 | /** |
| 1044 | * @param array $input |
| 1045 | * |
| 1046 | * @return array |
| 1047 | * |
| 1048 | * Sanitize input array |
| 1049 | */ |
| 1050 | public function sanitize_array($input = array()){ |
| 1051 | $array = array(); |
| 1052 | |
| 1053 | if (is_array($input) && count($input)){ |
| 1054 | foreach ($input as $key => $value){ |
| 1055 | if (is_array($value)){ |
| 1056 | $array[$key] = $this->sanitize_array($value); |
| 1057 | }else{ |
| 1058 | $key = sanitize_text_field($key); |
| 1059 | $value = sanitize_text_field($value); |
| 1060 | $array[$key] = $value; |
| 1061 | } |
| 1062 | } |
| 1063 | } |
| 1064 | |
| 1065 | return $array; |
| 1066 | } |
| 1067 | |
| 1068 | public function has_video_in_single($post_id = 0){ |
| 1069 | if (is_single()) { |
| 1070 | $post_id = $this->get_post_id($post_id); |
| 1071 | |
| 1072 | $video = $this->get_video( $post_id ); |
| 1073 | if ( $video ) { |
| 1074 | return $video; |
| 1075 | } |
| 1076 | } |
| 1077 | return false; |
| 1078 | |
| 1079 | } |
| 1080 | |
| 1081 | /** |
| 1082 | * @param int $start |
| 1083 | * @param int $limit |
| 1084 | * @param string $search_term |
| 1085 | * @param int $course_id |
| 1086 | * |
| 1087 | * @return array|null|object |
| 1088 | * |
| 1089 | * |
| 1090 | * Get the enrolled students for all courses. |
| 1091 | * |
| 1092 | * Pass course id in 4th parameter to get students course wise. |
| 1093 | * |
| 1094 | * @since v.1.0.0 |
| 1095 | */ |
| 1096 | public function get_students($start = 0, $limit = 10, $search_term = ''){ |
| 1097 | $meta_key = '_is_tutor_student'; |
| 1098 | |
| 1099 | global $wpdb; |
| 1100 | |
| 1101 | if ($search_term){ |
| 1102 | $search_term = " AND ( {$wpdb->users}.display_name LIKE '%{$search_term}%' OR {$wpdb->users}.user_email LIKE '%{$search_term}%' ) "; |
| 1103 | } |
| 1104 | |
| 1105 | $students = $wpdb->get_results("SELECT SQL_CALC_FOUND_ROWS {$wpdb->users}.* FROM {$wpdb->users} |
| 1106 | INNER JOIN {$wpdb->usermeta} |
| 1107 | ON ( {$wpdb->users}.ID = {$wpdb->usermeta}.user_id ) |
| 1108 | WHERE 1=1 AND ( {$wpdb->usermeta}.meta_key = '{$meta_key}' ) {$search_term} |
| 1109 | ORDER BY {$wpdb->usermeta}.meta_value DESC |
| 1110 | LIMIT {$start}, {$limit} "); |
| 1111 | |
| 1112 | return $students; |
| 1113 | } |
| 1114 | |
| 1115 | /** |
| 1116 | * @return int |
| 1117 | * |
| 1118 | * @since v.1.0.0 |
| 1119 | * |
| 1120 | * get the total students |
| 1121 | * pass course id to get course wise total students |
| 1122 | */ |
| 1123 | public function get_total_students($search_term = ''){ |
| 1124 | $meta_key = '_is_tutor_student'; |
| 1125 | |
| 1126 | global $wpdb; |
| 1127 | |
| 1128 | if ($search_term){ |
| 1129 | $search_term = " AND ( {$wpdb->users}.display_name LIKE '%{$search_term}%' OR {$wpdb->users}.user_email LIKE '%{$search_term}%' ) "; |
| 1130 | } |
| 1131 | |
| 1132 | $count = $wpdb->get_var("SELECT COUNT({$wpdb->users}.ID) FROM {$wpdb->users} INNER JOIN {$wpdb->usermeta} ON ( {$wpdb->users}.ID = {$wpdb->usermeta}.user_id ) WHERE 1=1 AND ( {$wpdb->usermeta}.meta_key = '{$meta_key}' ) $search_term "); |
| 1133 | |
| 1134 | return (int) $count; |
| 1135 | } |
| 1136 | |
| 1137 | public function get_completed_courses_ids_by_user($user_id = 0){ |
| 1138 | global $wpdb; |
| 1139 | |
| 1140 | $user_id = $this->get_user_id($user_id); |
| 1141 | |
| 1142 | $course_ids = (array) $wpdb->get_col("SELECT comment_post_ID as course_id |
| 1143 | from {$wpdb->comments} |
| 1144 | WHERE comment_agent = 'TutorLMSPlugin' |
| 1145 | AND comment_type = 'course_completed' |
| 1146 | AND user_id = {$user_id} ;"); |
| 1147 | |
| 1148 | return $course_ids; |
| 1149 | } |
| 1150 | |
| 1151 | /** |
| 1152 | * @param int $user_id |
| 1153 | * |
| 1154 | * @return bool|\WP_Query |
| 1155 | * |
| 1156 | * Return courses by user_id |
| 1157 | */ |
| 1158 | public function get_courses_by_user($user_id = 0){ |
| 1159 | $user_id = $this->get_user_id($user_id); |
| 1160 | $course_ids = $this->get_completed_courses_ids_by_user($user_id); |
| 1161 | |
| 1162 | if (count($course_ids)){ |
| 1163 | $course_post_type = tutor()->course_post_type; |
| 1164 | $course_args = array( |
| 1165 | 'post_type' => $course_post_type, |
| 1166 | 'post_status' => 'publish', |
| 1167 | 'post__in' => $course_ids, |
| 1168 | ); |
| 1169 | |
| 1170 | return new \WP_Query($course_args); |
| 1171 | } |
| 1172 | |
| 1173 | return false; |
| 1174 | } |
| 1175 | |
| 1176 | /** |
| 1177 | * @param int $user_id |
| 1178 | * |
| 1179 | * @return bool|\WP_Query |
| 1180 | * |
| 1181 | * Get the active course by user |
| 1182 | */ |
| 1183 | |
| 1184 | public function get_active_courses_by_user($user_id = 0){ |
| 1185 | $user_id = $this->get_user_id($user_id); |
| 1186 | |
| 1187 | $course_ids = $this->get_completed_courses_ids_by_user($user_id); |
| 1188 | $enrolled_course_ids = $this->get_enrolled_courses_ids_by_user($user_id); |
| 1189 | $active_courses = array_diff($enrolled_course_ids, $course_ids); |
| 1190 | |
| 1191 | if (count($active_courses)){ |
| 1192 | $course_post_type = tutor()->course_post_type; |
| 1193 | $course_args = array( |
| 1194 | 'post_type' => $course_post_type, |
| 1195 | 'post_status' => 'publish', |
| 1196 | 'post__in' => $active_courses, |
| 1197 | ); |
| 1198 | |
| 1199 | return new \WP_Query($course_args); |
| 1200 | } |
| 1201 | |
| 1202 | return false; |
| 1203 | } |
| 1204 | |
| 1205 | public function get_enrolled_courses_ids_by_user($user_id = 0){ |
| 1206 | global $wpdb; |
| 1207 | $user_id = $this->get_user_id($user_id); |
| 1208 | $course_ids = $wpdb->get_col("select post_parent from {$wpdb->posts} WHERE post_type = 'tutor_enrolled' AND post_author = {$user_id} AND post_status = 'completed'; "); |
| 1209 | |
| 1210 | return $course_ids; |
| 1211 | } |
| 1212 | |
| 1213 | /** |
| 1214 | * @param int $course_id |
| 1215 | * |
| 1216 | * @return int |
| 1217 | * |
| 1218 | * Get the total enrolled users at course |
| 1219 | */ |
| 1220 | public function count_enrolled_users_by_course($course_id = 0){ |
| 1221 | global $wpdb; |
| 1222 | $course_id = $this->get_post_id($course_id); |
| 1223 | |
| 1224 | $course_ids = $wpdb->get_var("select COUNT(ID) from {$wpdb->posts} WHERE post_type = 'tutor_enrolled' AND post_parent = {$course_id} AND post_status = 'completed'; "); |
| 1225 | |
| 1226 | return (int) $course_ids; |
| 1227 | } |
| 1228 | |
| 1229 | /** |
| 1230 | * @param int $user_id |
| 1231 | * |
| 1232 | * @return bool|\WP_Query |
| 1233 | * |
| 1234 | * Get the enrolled courses by user |
| 1235 | */ |
| 1236 | public function get_enrolled_courses_by_user($user_id = 0){ |
| 1237 | global $wpdb; |
| 1238 | |
| 1239 | $user_id = $this->get_user_id($user_id); |
| 1240 | $course_ids = $this->get_enrolled_courses_ids_by_user($user_id); |
| 1241 | |
| 1242 | if (count($course_ids)){ |
| 1243 | $course_post_type = tutor()->course_post_type; |
| 1244 | $course_args = array( |
| 1245 | 'post_type' => $course_post_type, |
| 1246 | 'post_status' => 'publish', |
| 1247 | 'post__in' => $course_ids, |
| 1248 | ); |
| 1249 | return new \WP_Query($course_args); |
| 1250 | } |
| 1251 | return false; |
| 1252 | } |
| 1253 | |
| 1254 | |
| 1255 | /** |
| 1256 | * @param int $post_id |
| 1257 | * |
| 1258 | * @return string |
| 1259 | * |
| 1260 | * Get the video streaming URL by post/lesson/course ID |
| 1261 | */ |
| 1262 | public function get_video_stream_url($post_id = 0){ |
| 1263 | $post_id = $this->get_post_id($post_id); |
| 1264 | $post = get_post($post_id); |
| 1265 | |
| 1266 | if ($post->post_type === tutor()->lesson_post_type ){ |
| 1267 | $video_url = trailingslashit(home_url()).'video-url/'.$post->post_name; |
| 1268 | }else{ |
| 1269 | $video_info = tutor_utils()->get_video_info($post_id); |
| 1270 | $video_url = $video_info->url; |
| 1271 | } |
| 1272 | |
| 1273 | return $video_url; |
| 1274 | } |
| 1275 | |
| 1276 | /** |
| 1277 | * @param int $lesson_id |
| 1278 | * @param int $user_id |
| 1279 | * |
| 1280 | * @return array|bool|mixed |
| 1281 | * |
| 1282 | * Get student lesson reading current info |
| 1283 | * |
| 1284 | * @since v.1.0.0 |
| 1285 | */ |
| 1286 | public function get_lesson_reading_info_full($lesson_id = 0, $user_id = 0){ |
| 1287 | $lesson_id = $this->get_post_id($lesson_id); |
| 1288 | $user_id = $this->get_user_id($user_id); |
| 1289 | |
| 1290 | $lesson_info = (array) maybe_unserialize(get_user_meta($user_id, '_lesson_reading_info', true)); |
| 1291 | return $this->avalue_dot($lesson_id, $lesson_info); |
| 1292 | } |
| 1293 | |
| 1294 | public function get_post_id($post_id = 0){ |
| 1295 | if ( ! $post_id){ |
| 1296 | $post_id = get_the_ID(); |
| 1297 | if ( ! $post_id){ |
| 1298 | return false; |
| 1299 | } |
| 1300 | } |
| 1301 | |
| 1302 | return $post_id; |
| 1303 | } |
| 1304 | |
| 1305 | public function get_user_id($user_id = 0){ |
| 1306 | if ( ! $user_id){ |
| 1307 | $user_id = get_current_user_id(); |
| 1308 | if ( ! $user_id){ |
| 1309 | return false; |
| 1310 | } |
| 1311 | } |
| 1312 | |
| 1313 | return $user_id; |
| 1314 | } |
| 1315 | |
| 1316 | /** |
| 1317 | * @param int $lesson_id |
| 1318 | * @param int $user_id |
| 1319 | * @param string $key |
| 1320 | * |
| 1321 | * @return array|bool|mixed |
| 1322 | * |
| 1323 | * Get lesson reading info by key |
| 1324 | * |
| 1325 | * @since v.1.0.0 |
| 1326 | */ |
| 1327 | |
| 1328 | public function get_lesson_reading_info($lesson_id = 0, $user_id = 0, $key = ''){ |
| 1329 | $lesson_id = $this->get_post_id($lesson_id); |
| 1330 | $user_id = $this->get_user_id($user_id); |
| 1331 | |
| 1332 | $lesson_info = $this->get_lesson_reading_info_full($lesson_id, $user_id); |
| 1333 | |
| 1334 | return $this->avalue_dot($key, $lesson_info); |
| 1335 | } |
| 1336 | |
| 1337 | /** |
| 1338 | * @param int $lesson_id |
| 1339 | * @param int $user_id |
| 1340 | * @param array $data |
| 1341 | * |
| 1342 | * @return bool |
| 1343 | * |
| 1344 | * Update student lesson reading info |
| 1345 | * |
| 1346 | * @since v.1.0.0 |
| 1347 | */ |
| 1348 | public function update_lesson_reading_info($lesson_id = 0, $user_id = 0, $key = '', $value = ''){ |
| 1349 | $lesson_id = $this->get_post_id($lesson_id); |
| 1350 | $user_id = $this->get_user_id($user_id); |
| 1351 | |
| 1352 | if ($key && $value){ |
| 1353 | $lesson_info = (array) maybe_unserialize(get_user_meta($user_id, '_lesson_reading_info', true)); |
| 1354 | $lesson_info[$lesson_id][$key] = $value; |
| 1355 | update_user_meta($user_id, '_lesson_reading_info', $lesson_info); |
| 1356 | } |
| 1357 | } |
| 1358 | |
| 1359 | /** |
| 1360 | * @param string $url |
| 1361 | * |
| 1362 | * @return bool |
| 1363 | * |
| 1364 | * Get the Youtube Video ID from URL |
| 1365 | * |
| 1366 | * @since v.1.0.0 |
| 1367 | */ |
| 1368 | public function get_youtube_video_id($url = ''){ |
| 1369 | if (!$url){ |
| 1370 | return false; |
| 1371 | } |
| 1372 | preg_match('%(?:youtube(?:-nocookie)?\.com/(?:[^/]+/.+/|(?:v|e(?:mbed)?)/|.*[?&]v=)|youtu\.be/)([^"&?/ ]{11})%i', $url, $match); |
| 1373 | |
| 1374 | if (isset($match[1])) { |
| 1375 | $youtube_id = $match[1]; |
| 1376 | return $youtube_id; |
| 1377 | } |
| 1378 | |
| 1379 | return false; |
| 1380 | } |
| 1381 | |
| 1382 | /** |
| 1383 | * @param string $url |
| 1384 | * |
| 1385 | * @return bool |
| 1386 | * |
| 1387 | * Get the vimeo video id from URL |
| 1388 | * |
| 1389 | * @since v.1.0.0 |
| 1390 | */ |
| 1391 | public function get_vimeo_video_id($url = ''){ |
| 1392 | if (preg_match('%^https?:\/\/(?:www\.|player\.)?vimeo.com\/(?:channels\/(?:\w+\/)?|groups\/([^\/]*)\/videos\/|album\/(\d+)\/video\/|video\/|)(\d+)(?:$|\/|\?)(?:[?]?.*)$%im', $url, $match)) { |
| 1393 | if (isset($match[3])){ |
| 1394 | return $match[3]; |
| 1395 | } |
| 1396 | } |
| 1397 | return false; |
| 1398 | } |
| 1399 | |
| 1400 | /** |
| 1401 | * @param int $post_id |
| 1402 | * |
| 1403 | * Mark lesson complete |
| 1404 | */ |
| 1405 | public function mark_lesson_complete($post_id = 0, $user_id = 0){ |
| 1406 | $post_id = $this->get_post_id($post_id); |
| 1407 | $user_id = $this->get_user_id($user_id); |
| 1408 | update_user_meta($user_id, '_tutor_completed_lesson_id_'.$post_id, time()); |
| 1409 | } |
| 1410 | |
| 1411 | /** |
| 1412 | * Saving enroll information to posts table |
| 1413 | * post_author = enrolled_student_id (wp_users id) |
| 1414 | * post_parent = enrolled course id |
| 1415 | * |
| 1416 | * @type: call when need |
| 1417 | * @return bool; |
| 1418 | */ |
| 1419 | public function do_enroll($course_id = 0, $order_id = 0){ |
| 1420 | if ( ! $course_id){ |
| 1421 | return false; |
| 1422 | } |
| 1423 | |
| 1424 | do_action('tutor_before_enroll', $course_id); |
| 1425 | $user_id = get_current_user_id(); |
| 1426 | $title = __('Course Enrolled', 'tutor')." – ".date_i18n(get_option('date_format')) .' @ '.date_i18n(get_option('time_format') ) ; |
| 1427 | |
| 1428 | $enrolment_status = 'completed'; |
| 1429 | |
| 1430 | if ($this->is_course_purchasable($course_id)) { |
| 1431 | /** |
| 1432 | * We need to verify this enrollment, we will change the status later after payment confirmation |
| 1433 | */ |
| 1434 | $enrolment_status = 'pending'; |
| 1435 | } |
| 1436 | |
| 1437 | $enroll_data = apply_filters('tutor_enroll_data', |
| 1438 | array( |
| 1439 | 'post_type' => 'tutor_enrolled', |
| 1440 | 'post_title' => $title, |
| 1441 | 'post_status' => $enrolment_status, |
| 1442 | 'post_author' => $user_id, |
| 1443 | 'post_parent' => $course_id, |
| 1444 | ) |
| 1445 | ); |
| 1446 | |
| 1447 | // Insert the post into the database |
| 1448 | $isEnrolled = wp_insert_post( $enroll_data ); |
| 1449 | if ($isEnrolled) { |
| 1450 | do_action('tutor_after_enroll', $course_id, $isEnrolled); |
| 1451 | |
| 1452 | //Mark Current User as Students with user meta data |
| 1453 | update_user_meta( $user_id, '_is_tutor_student', time() ); |
| 1454 | |
| 1455 | if ($order_id) { |
| 1456 | //Mark order for course and user |
| 1457 | $product_id = $this->get_course_product_id($course_id); |
| 1458 | update_post_meta( $isEnrolled, '_tutor_enrolled_by_order_id', $order_id ); |
| 1459 | update_post_meta( $isEnrolled, '_tutor_enrolled_by_product_id', $product_id ); |
| 1460 | update_post_meta( $order_id, '_is_tutor_order_for_course', time() ); |
| 1461 | update_post_meta( $order_id, '_tutor_order_for_course_id_'.$course_id, $isEnrolled ); |
| 1462 | } |
| 1463 | return true; |
| 1464 | } |
| 1465 | |
| 1466 | return false; |
| 1467 | } |
| 1468 | |
| 1469 | public function complete_course_enroll($order_id){ |
| 1470 | if ( ! tutor_utils()->is_tutor_order($order_id)){ |
| 1471 | return; |
| 1472 | } |
| 1473 | |
| 1474 | global $wpdb; |
| 1475 | |
| 1476 | $enrolled_ids_with_course = $this->get_course_enrolled_ids_by_order_id($order_id); |
| 1477 | if ($enrolled_ids_with_course){ |
| 1478 | $enrolled_ids = wp_list_pluck($enrolled_ids_with_course, 'enrolled_id'); |
| 1479 | |
| 1480 | if (is_array($enrolled_ids) && count($enrolled_ids)){ |
| 1481 | foreach ($enrolled_ids as $enrolled_id){ |
| 1482 | $wpdb->update( $wpdb->posts, array( 'post_status' => 'completed' ), array( 'ID' => $enrolled_id ) ); |
| 1483 | } |
| 1484 | } |
| 1485 | } |
| 1486 | } |
| 1487 | |
| 1488 | /** |
| 1489 | * @param $order_id |
| 1490 | * |
| 1491 | * @return array|bool |
| 1492 | */ |
| 1493 | public function get_course_enrolled_ids_by_order_id($order_id){ |
| 1494 | global $wpdb; |
| 1495 | //Getting all of courses ids within this order |
| 1496 | |
| 1497 | $courses_ids = $wpdb->get_results("SELECT * FROM {$wpdb->postmeta} WHERE post_id = {$order_id} AND meta_key LIKE '_tutor_order_for_course_id_%' "); |
| 1498 | |
| 1499 | if (is_array($courses_ids) && count($courses_ids)){ |
| 1500 | $course_enrolled_by_order = array(); |
| 1501 | foreach ($courses_ids as $courses_id){ |
| 1502 | $course_id = str_replace('_tutor_order_for_course_id_', '',$courses_id->meta_key); |
| 1503 | //array(order_id => array('course_id' => $course_id, 'enrolled_id' => enrolled_id)) |
| 1504 | $course_enrolled_by_order[$courses_id->post_id] = array('course_id' => $course_id, 'enrolled_id' => $courses_id->meta_value); |
| 1505 | } |
| 1506 | return $course_enrolled_by_order; |
| 1507 | } |
| 1508 | return false; |
| 1509 | } |
| 1510 | |
| 1511 | /** |
| 1512 | * Get wc product in efficient query |
| 1513 | * |
| 1514 | * @since v.1.0.0 |
| 1515 | */ |
| 1516 | |
| 1517 | /** |
| 1518 | * @return array|null|object |
| 1519 | * |
| 1520 | * WooCommerce specific utils |
| 1521 | */ |
| 1522 | public function get_wc_products_db(){ |
| 1523 | global $wpdb; |
| 1524 | $query = $wpdb->get_results("SELECT ID, post_title from {$wpdb->posts} WHERE post_status = 'publish' AND post_type = 'product' "); |
| 1525 | |
| 1526 | return $query; |
| 1527 | } |
| 1528 | |
| 1529 | public function get_course_product_id($course_id = 0){ |
| 1530 | $course_id = $this->get_post_id($course_id); |
| 1531 | return (int) get_post_meta($course_id, '_tutor_course_product_id', true); |
| 1532 | } |
| 1533 | |
| 1534 | public function product_belongs_with_course($product_id = 0){ |
| 1535 | global $wpdb; |
| 1536 | |
| 1537 | $query = $wpdb->get_row("select * from {$wpdb->postmeta} WHERE meta_key='_tutor_course_product_id' AND meta_value = {$product_id} limit 1 "); |
| 1538 | return $query; |
| 1539 | } |
| 1540 | |
| 1541 | /** |
| 1542 | * #End WooCommerce specific utils |
| 1543 | */ |
| 1544 | |
| 1545 | public function get_enrolled_statuses(){ |
| 1546 | return array ( |
| 1547 | 'pending', |
| 1548 | 'processing', |
| 1549 | 'on-hold', |
| 1550 | 'completed', |
| 1551 | 'cancelled', |
| 1552 | 'refunded', |
| 1553 | 'failed', |
| 1554 | ); |
| 1555 | } |
| 1556 | |
| 1557 | public function is_tutor_order($order_id){ |
| 1558 | return get_post_meta($order_id, '_is_tutor_order_for_course', true); |
| 1559 | } |
| 1560 | |
| 1561 | /** |
| 1562 | * @return mixed |
| 1563 | * |
| 1564 | * Tutor Dashboard Pages |
| 1565 | */ |
| 1566 | |
| 1567 | public function tutor_student_dashboard_pages(){ |
| 1568 | $nav_items = array( |
| 1569 | 'index' => __('Home', 'tutor'), |
| 1570 | 'my-courses' => __('My Courses', 'tutor'), |
| 1571 | 'active-courses' => __('Active Courses', 'tutor'), |
| 1572 | 'completed-courses' => __('Completed Courses', 'tutor'), |
| 1573 | 'wishlist' => __('WishList', 'tutor'), |
| 1574 | ); |
| 1575 | |
| 1576 | return apply_filters('tutor_dashboard/student/pages', $nav_items); |
| 1577 | } |
| 1578 | |
| 1579 | |
| 1580 | public function get_tutor_dashboard_page_permalink($page_key = '', $page_id = 0){ |
| 1581 | if ($page_key === 'index'){ |
| 1582 | $page_key = ''; |
| 1583 | } |
| 1584 | $page_id = $this->get_post_id($page_id); |
| 1585 | return trailingslashit(get_permalink($page_id)).$page_key; |
| 1586 | } |
| 1587 | |
| 1588 | public function input_old($input = ''){ |
| 1589 | $value = $this->avalue_dot($input, $_REQUEST); |
| 1590 | if ($value){ |
| 1591 | return $value; |
| 1592 | } |
| 1593 | return ''; |
| 1594 | } |
| 1595 | |
| 1596 | /** |
| 1597 | * @param int $user_id |
| 1598 | * |
| 1599 | * @return mixed |
| 1600 | * |
| 1601 | * Determine if is instructor or not |
| 1602 | * |
| 1603 | * @since v.1.0.0 |
| 1604 | */ |
| 1605 | public function is_instructor($user_id = 0){ |
| 1606 | $user_id = $this->get_user_id($user_id); |
| 1607 | return get_user_meta($user_id, '_is_tutor_instructor', true); |
| 1608 | } |
| 1609 | |
| 1610 | public function instructor_status($user_id = 0, $status_name = true){ |
| 1611 | $user_id = $this->get_user_id($user_id); |
| 1612 | |
| 1613 | $instructor_status = apply_filters('tutor_instructor_statuses', array( |
| 1614 | 'pending' => __('Pending', 'tutor'), |
| 1615 | 'approved' => __('Approved', 'tutor'), |
| 1616 | 'blocked' => __('Blocked', 'tutor'), |
| 1617 | )); |
| 1618 | |
| 1619 | $status = get_user_meta($user_id, '_tutor_instructor_status', true); |
| 1620 | |
| 1621 | if (isset($instructor_status[$status])){ |
| 1622 | if ( ! $status_name){ |
| 1623 | return $status; |
| 1624 | } |
| 1625 | return $instructor_status[$status]; |
| 1626 | } |
| 1627 | return false; |
| 1628 | } |
| 1629 | |
| 1630 | |
| 1631 | public function get_total_instructors($search_term = ''){ |
| 1632 | $meta_key = '_is_tutor_instructor'; |
| 1633 | |
| 1634 | global $wpdb; |
| 1635 | |
| 1636 | if ($search_term){ |
| 1637 | $search_term = " AND ( {$wpdb->users}.display_name LIKE '%{$search_term}%' OR {$wpdb->users}.user_email LIKE '%{$search_term}%' ) "; |
| 1638 | } |
| 1639 | |
| 1640 | $count = $wpdb->get_var("SELECT COUNT({$wpdb->users}.ID) FROM {$wpdb->users} INNER JOIN {$wpdb->usermeta} ON ( {$wpdb->users}.ID = {$wpdb->usermeta}.user_id ) WHERE 1=1 AND ( {$wpdb->usermeta}.meta_key = '{$meta_key}' ) $search_term "); |
| 1641 | |
| 1642 | return (int) $count; |
| 1643 | } |
| 1644 | |
| 1645 | public function get_instructors($start = 0, $limit = 10, $search_term = ''){ |
| 1646 | $meta_key = '_is_tutor_instructor'; |
| 1647 | global $wpdb; |
| 1648 | |
| 1649 | if ($search_term){ |
| 1650 | $search_term = " AND ( {$wpdb->users}.display_name LIKE '%{$search_term}%' OR {$wpdb->users}.user_email LIKE '%{$search_term}%' ) "; |
| 1651 | } |
| 1652 | |
| 1653 | $instructors = $wpdb->get_results("SELECT SQL_CALC_FOUND_ROWS {$wpdb->users}.* FROM {$wpdb->users} |
| 1654 | INNER JOIN {$wpdb->usermeta} |
| 1655 | ON ( {$wpdb->users}.ID = {$wpdb->usermeta}.user_id ) |
| 1656 | WHERE 1=1 AND ( {$wpdb->usermeta}.meta_key = '{$meta_key}' ) {$search_term} |
| 1657 | ORDER BY {$wpdb->usermeta}.meta_value DESC |
| 1658 | LIMIT {$start}, {$limit} "); |
| 1659 | |
| 1660 | return $instructors; |
| 1661 | } |
| 1662 | |
| 1663 | public function get_instructors_by_course($course_id = 0){ |
| 1664 | global $wpdb; |
| 1665 | $course_id = $this->get_post_id($course_id); |
| 1666 | |
| 1667 | $instructors = $wpdb->get_results("select ID, display_name, |
| 1668 | get_course.meta_value as taught_course_id, |
| 1669 | tutor_job_title.meta_value as tutor_profile_job_title, |
| 1670 | tutor_bio.meta_value as tutor_profile_bio, |
| 1671 | tutor_photo.meta_value as tutor_profile_photo |
| 1672 | from {$wpdb->users} |
| 1673 | INNER JOIN {$wpdb->usermeta} get_course ON ID = get_course.user_id AND get_course.meta_value = {$course_id} |
| 1674 | LEFT JOIN {$wpdb->usermeta} tutor_job_title ON ID = tutor_job_title.user_id AND tutor_job_title.meta_key = '_tutor_profile_job_title' |
| 1675 | LEFT JOIN {$wpdb->usermeta} tutor_bio ON ID = tutor_bio.user_id AND tutor_bio.meta_key = '_tutor_profile_bio' |
| 1676 | LEFT JOIN {$wpdb->usermeta} tutor_photo ON ID = tutor_photo.user_id AND tutor_photo.meta_key = '_tutor_profile_photo' |
| 1677 | "); |
| 1678 | |
| 1679 | if (is_array($instructors) && count($instructors)){ |
| 1680 | return $instructors; |
| 1681 | } |
| 1682 | |
| 1683 | return false; |
| 1684 | } |
| 1685 | |
| 1686 | /** |
| 1687 | * @param $instructor_id |
| 1688 | * |
| 1689 | * Get total Students by instructor |
| 1690 | * 1 enrollment = 1 student, so total enrolled for a equivalent total students (Tricks) |
| 1691 | * |
| 1692 | * @since v.1.0.0 |
| 1693 | */ |
| 1694 | public function get_total_students_by_instructor($instructor_id){ |
| 1695 | global $wpdb; |
| 1696 | |
| 1697 | $course_post_type = tutor()->course_post_type; |
| 1698 | $count = $wpdb->get_var("SELECT COUNT(courses.ID) from {$wpdb->posts} courses |
| 1699 | |
| 1700 | INNER JOIN {$wpdb->posts} enrolled ON courses.ID = enrolled.post_parent AND enrolled.post_type = 'tutor_enrolled' |
| 1701 | WHERE courses.post_status = 'publish' |
| 1702 | AND courses.post_type = '{$course_post_type}' |
| 1703 | AND courses.post_author = {$instructor_id} ; "); |
| 1704 | return (int) $count; |
| 1705 | } |
| 1706 | |
| 1707 | /** |
| 1708 | * @param float $input |
| 1709 | * |
| 1710 | * @return float|string |
| 1711 | * |
| 1712 | * Get rating format from value |
| 1713 | */ |
| 1714 | public function get_rating_value($input = 0.00){ |
| 1715 | |
| 1716 | if ( $input > 0){ |
| 1717 | $input = number_format($input, 2); |
| 1718 | $int_value = (int) $input; |
| 1719 | $fraction = $input - $int_value; |
| 1720 | |
| 1721 | if ($fraction == 0){ |
| 1722 | $fraction = 0.00; |
| 1723 | }elseif($fraction > 0.5){ |
| 1724 | $fraction = 1; |
| 1725 | }else{ |
| 1726 | $fraction = 0.5; |
| 1727 | } |
| 1728 | |
| 1729 | return number_format( ($int_value + $fraction), 2); |
| 1730 | } |
| 1731 | return 0.00; |
| 1732 | } |
| 1733 | |
| 1734 | /** |
| 1735 | * @param float $current_rating |
| 1736 | * @param bool $echo |
| 1737 | * |
| 1738 | * @return string |
| 1739 | * |
| 1740 | * Generate star rating based in given rating value |
| 1741 | */ |
| 1742 | public function star_rating_generator($current_rating = 0.00, $echo = true){ |
| 1743 | $output = ''; |
| 1744 | |
| 1745 | for ($i = 1; $i <=5 ; $i++){ |
| 1746 | $intRating = (int) $current_rating; |
| 1747 | |
| 1748 | if ($intRating >= $i){ |
| 1749 | $output.= '<i class="tutor-icon-star-full" data-rating-value="'.$i.'"></i>'; |
| 1750 | } else{ |
| 1751 | if ( ($current_rating - $i) == -0.5){ |
| 1752 | $output.= '<i class="tutor-icon-star-half" data-rating-value="'.$i.'"></i>'; |
| 1753 | }else{ |
| 1754 | $output.= '<i class="tutor-icon-star-line" data-rating-value="'.$i.'"></i>'; |
| 1755 | } |
| 1756 | } |
| 1757 | } |
| 1758 | |
| 1759 | if ($echo){ |
| 1760 | echo $output; |
| 1761 | } |
| 1762 | return $output; |
| 1763 | } |
| 1764 | |
| 1765 | /** |
| 1766 | * @param null $name |
| 1767 | * |
| 1768 | * @return string |
| 1769 | * |
| 1770 | * Generate text to avatar |
| 1771 | */ |
| 1772 | public function get_tutor_avatar($user_id = null, $size = 'thumbnail'){ |
| 1773 | global $wpdb; |
| 1774 | |
| 1775 | if ( ! $user_id){ |
| 1776 | return ''; |
| 1777 | } |
| 1778 | |
| 1779 | $user = $this->get_tutor_user($user_id); |
| 1780 | if ($user->tutor_profile_photo){ |
| 1781 | return '<img src="'.wp_get_attachment_image_url($user->tutor_profile_photo, $size).'" class="tutor-image-avatar" alt="" /> '; |
| 1782 | } |
| 1783 | |
| 1784 | $name = $user->display_name; |
| 1785 | $arr = explode(' ', trim($name)); |
| 1786 | |
| 1787 | if (count($arr) > 1){ |
| 1788 | $first_char = substr($arr[0], 0, 1) ; |
| 1789 | $second_char = substr($arr[1], 0, 1) ; |
| 1790 | }else{ |
| 1791 | $first_char = substr($arr[0], 0, 1) ; |
| 1792 | $second_char = substr($arr[0], 1, 1) ; |
| 1793 | } |
| 1794 | |
| 1795 | $initial_avatar = strtoupper($first_char.$second_char); |
| 1796 | |
| 1797 | $bg_color = '#'.substr(md5($initial_avatar), 0, 6); |
| 1798 | $initial_avatar = "<span class='tutor-text-avatar' style='background-color: {$bg_color}; color: #fff8e5'>{$initial_avatar}</span>"; |
| 1799 | |
| 1800 | return $initial_avatar; |
| 1801 | } |
| 1802 | |
| 1803 | public function get_tutor_user($user_id){ |
| 1804 | global $wpdb; |
| 1805 | |
| 1806 | $user = $wpdb->get_row("select ID, display_name, |
| 1807 | tutor_job_title.meta_value as tutor_profile_job_title, |
| 1808 | tutor_bio.meta_value as tutor_profile_bio, |
| 1809 | tutor_photo.meta_value as tutor_profile_photo |
| 1810 | |
| 1811 | from {$wpdb->users} |
| 1812 | LEFT JOIN {$wpdb->usermeta} tutor_job_title ON ID = tutor_job_title.user_id AND tutor_job_title.meta_key = '_tutor_profile_job_title' |
| 1813 | LEFT JOIN {$wpdb->usermeta} tutor_bio ON ID = tutor_bio.user_id AND tutor_bio.meta_key = '_tutor_profile_bio' |
| 1814 | LEFT JOIN {$wpdb->usermeta} tutor_photo ON ID = tutor_photo.user_id AND tutor_photo.meta_key = '_tutor_profile_photo' |
| 1815 | |
| 1816 | WHERE ID = {$user_id} "); |
| 1817 | return $user; |
| 1818 | } |
| 1819 | |
| 1820 | /** |
| 1821 | * @param int $course_id |
| 1822 | * @param int $offset |
| 1823 | * @param int $limit |
| 1824 | * |
| 1825 | * @return array|null|object |
| 1826 | * |
| 1827 | * get course reviews |
| 1828 | * |
| 1829 | * @since v.1.0.0 |
| 1830 | */ |
| 1831 | public function get_course_reviews($course_id = 0, $offset = 0, $limit = 150){ |
| 1832 | $course_id = $this->get_post_id($course_id); |
| 1833 | global $wpdb; |
| 1834 | |
| 1835 | $reviews = $wpdb->get_results("select {$wpdb->comments}.comment_ID, |
| 1836 | {$wpdb->comments}.comment_post_ID, |
| 1837 | {$wpdb->comments}.comment_author, |
| 1838 | {$wpdb->comments}.comment_author_email, |
| 1839 | {$wpdb->comments}.comment_date, |
| 1840 | {$wpdb->comments}.comment_content, |
| 1841 | {$wpdb->comments}.user_id, |
| 1842 | {$wpdb->commentmeta}.meta_value as rating, |
| 1843 | {$wpdb->users}.display_name |
| 1844 | |
| 1845 | from {$wpdb->comments} |
| 1846 | INNER JOIN {$wpdb->commentmeta} |
| 1847 | ON {$wpdb->comments}.comment_ID = {$wpdb->commentmeta}.comment_id |
| 1848 | INNER JOIN {$wpdb->users} |
| 1849 | ON {$wpdb->comments}.user_id = {$wpdb->users}.ID |
| 1850 | WHERE {$wpdb->comments}.comment_post_ID = {$course_id} |
| 1851 | AND meta_key = 'tutor_rating' ORDER BY comment_ID DESC LIMIT {$offset},{$limit} ;" |
| 1852 | ); |
| 1853 | |
| 1854 | return $reviews; |
| 1855 | } |
| 1856 | |
| 1857 | /** |
| 1858 | * @param int $course_id |
| 1859 | * |
| 1860 | * @return object |
| 1861 | * |
| 1862 | * Get course rating |
| 1863 | */ |
| 1864 | public function get_course_rating($course_id = 0){ |
| 1865 | $course_id = $this->get_post_id($course_id); |
| 1866 | |
| 1867 | $ratings = array( |
| 1868 | 'rating_count' => 0, |
| 1869 | 'rating_sum' => 0, |
| 1870 | 'rating_avg' => 0.00, |
| 1871 | ); |
| 1872 | |
| 1873 | global $wpdb; |
| 1874 | |
| 1875 | $rating = $wpdb->get_row("select COUNT(meta_value) as rating_count, SUM(meta_value) as rating_sum from {$wpdb->comments} |
| 1876 | INNER JOIN {$wpdb->commentmeta} |
| 1877 | ON {$wpdb->comments}.comment_ID = {$wpdb->commentmeta}.comment_id |
| 1878 | WHERE {$wpdb->comments}.comment_post_ID = {$course_id} |
| 1879 | AND meta_key = 'tutor_rating' ;" |
| 1880 | ); |
| 1881 | |
| 1882 | if ($rating->rating_count){ |
| 1883 | $avg_rating = number_format(($rating->rating_sum / $rating->rating_count), 2); |
| 1884 | |
| 1885 | $ratings = array( |
| 1886 | 'rating_count' => $rating->rating_count, |
| 1887 | 'rating_sum' => $rating->rating_sum, |
| 1888 | 'rating_avg' => $avg_rating, |
| 1889 | ); |
| 1890 | } |
| 1891 | |
| 1892 | return (object) $ratings; |
| 1893 | } |
| 1894 | |
| 1895 | |
| 1896 | public function get_reviews_by_user($user_id = 0, $offset = 0, $limit = 150){ |
| 1897 | $user_id = $this->get_user_id($user_id); |
| 1898 | global $wpdb; |
| 1899 | |
| 1900 | $reviews = $wpdb->get_results("select {$wpdb->comments}.comment_ID, |
| 1901 | {$wpdb->comments}.comment_post_ID, |
| 1902 | {$wpdb->comments}.comment_author, |
| 1903 | {$wpdb->comments}.comment_author_email, |
| 1904 | {$wpdb->comments}.comment_date, |
| 1905 | {$wpdb->comments}.comment_content, |
| 1906 | {$wpdb->comments}.user_id, |
| 1907 | {$wpdb->commentmeta}.meta_value as rating, |
| 1908 | {$wpdb->users}.display_name |
| 1909 | |
| 1910 | from {$wpdb->comments} |
| 1911 | INNER JOIN {$wpdb->commentmeta} |
| 1912 | ON {$wpdb->comments}.comment_ID = {$wpdb->commentmeta}.comment_id |
| 1913 | INNER JOIN {$wpdb->users} |
| 1914 | ON {$wpdb->comments}.user_id = {$wpdb->users}.ID |
| 1915 | WHERE {$wpdb->comments}.user_id = {$user_id} |
| 1916 | AND meta_key = 'tutor_rating' ORDER BY comment_ID DESC LIMIT {$offset},{$limit} ;" |
| 1917 | ); |
| 1918 | |
| 1919 | return $reviews; |
| 1920 | } |
| 1921 | |
| 1922 | /** |
| 1923 | * @param $instructor_id |
| 1924 | * |
| 1925 | * @return object |
| 1926 | * |
| 1927 | * Get instructors rating |
| 1928 | */ |
| 1929 | public function get_instructor_ratings($instructor_id){ |
| 1930 | global $wpdb; |
| 1931 | |
| 1932 | $ratings = array( |
| 1933 | 'rating_count' => 0, |
| 1934 | 'rating_sum' => 0, |
| 1935 | 'rating_avg' => 0.00, |
| 1936 | ); |
| 1937 | |
| 1938 | $rating = $wpdb->get_row("SELECT COUNT(rating.meta_value) as rating_count, SUM(rating.meta_value) as rating_sum |
| 1939 | FROM {$wpdb->usermeta} courses |
| 1940 | INNER JOIN {$wpdb->comments} reviews ON courses.meta_value = reviews.comment_post_ID AND reviews.comment_type = 'tutor_course_rating' |
| 1941 | INNER JOIN {$wpdb->commentmeta} rating ON reviews.comment_ID = rating.comment_id AND rating.meta_key = 'tutor_rating' |
| 1942 | WHERE courses.user_id = {$instructor_id} AND courses.meta_key = '_tutor_instructor_course_id'"); |
| 1943 | |
| 1944 | if ($rating->rating_count){ |
| 1945 | $avg_rating = number_format(($rating->rating_sum / $rating->rating_count), 2); |
| 1946 | |
| 1947 | $ratings = array( |
| 1948 | 'rating_count' => $rating->rating_count, |
| 1949 | 'rating_sum' => $rating->rating_sum, |
| 1950 | 'rating_avg' => $avg_rating, |
| 1951 | ); |
| 1952 | } |
| 1953 | |
| 1954 | return (object) $ratings; |
| 1955 | } |
| 1956 | |
| 1957 | /** |
| 1958 | * @param int $course_id |
| 1959 | * @param int $user_id |
| 1960 | * |
| 1961 | * @return object |
| 1962 | * |
| 1963 | * Get course rating by user |
| 1964 | */ |
| 1965 | public function get_course_rating_by_user($course_id = 0, $user_id = 0){ |
| 1966 | $course_id = $this->get_post_id($course_id); |
| 1967 | $user_id = $this->get_user_id($user_id); |
| 1968 | |
| 1969 | $ratings = array( |
| 1970 | 'rating' => 0, |
| 1971 | 'review' => '', |
| 1972 | ); |
| 1973 | |
| 1974 | global $wpdb; |
| 1975 | |
| 1976 | $rating = $wpdb->get_row("select meta_value as rating, comment_content as review from {$wpdb->comments} |
| 1977 | INNER JOIN {$wpdb->commentmeta} |
| 1978 | ON {$wpdb->comments}.comment_ID = {$wpdb->commentmeta}.comment_id |
| 1979 | WHERE {$wpdb->comments}.comment_post_ID = {$course_id} AND user_id = {$user_id} |
| 1980 | AND meta_key = 'tutor_rating' ;" |
| 1981 | ); |
| 1982 | |
| 1983 | if ($rating){ |
| 1984 | $rating_format = number_format($rating->rating, 2); |
| 1985 | |
| 1986 | $ratings = array( |
| 1987 | 'rating' => $rating_format, |
| 1988 | 'review' => $rating->review, |
| 1989 | ); |
| 1990 | } |
| 1991 | return (object) $ratings; |
| 1992 | } |
| 1993 | |
| 1994 | /** |
| 1995 | * @param int $user_id |
| 1996 | * |
| 1997 | * @return null|string |
| 1998 | */ |
| 1999 | public function count_reviews_wrote_by_user($user_id = 0){ |
| 2000 | global $wpdb; |
| 2001 | $user_id = $this->get_user_id($user_id); |
| 2002 | |
| 2003 | $count_reviews = $wpdb->get_var("SELECT COUNT(comment_ID) from {$wpdb->comments} WHERE user_id = {$user_id} AND comment_type = 'tutor_course_rating' "); |
| 2004 | return $count_reviews; |
| 2005 | } |
| 2006 | |
| 2007 | /** |
| 2008 | * @param $size |
| 2009 | * |
| 2010 | * @return bool|int|string |
| 2011 | * |
| 2012 | * This function transforms the php.ini notation for numbers (like '2M') to an integer. |
| 2013 | */ |
| 2014 | |
| 2015 | function let_to_num( $size ) { |
| 2016 | $l = substr( $size, -1 ); |
| 2017 | $ret = substr( $size, 0, -1 ); |
| 2018 | $byte = 1024; |
| 2019 | |
| 2020 | switch ( strtoupper( $l ) ) { |
| 2021 | case 'P': |
| 2022 | $ret *= 1024; |
| 2023 | // No break. |
| 2024 | case 'T': |
| 2025 | $ret *= 1024; |
| 2026 | // No break. |
| 2027 | case 'G': |
| 2028 | $ret *= 1024; |
| 2029 | // No break. |
| 2030 | case 'M': |
| 2031 | $ret *= 1024; |
| 2032 | // No break. |
| 2033 | case 'K': |
| 2034 | $ret *= 1024; |
| 2035 | // No break. |
| 2036 | } |
| 2037 | return $ret; |
| 2038 | } |
| 2039 | |
| 2040 | |
| 2041 | |
| 2042 | function get_db_version() { |
| 2043 | global $wpdb; |
| 2044 | |
| 2045 | if ( empty( $wpdb->is_mysql ) ) { |
| 2046 | return array( |
| 2047 | 'string' => '', |
| 2048 | 'number' => '', |
| 2049 | ); |
| 2050 | } |
| 2051 | |
| 2052 | if ( $wpdb->use_mysqli ) { |
| 2053 | $server_info = mysqli_get_server_info( $wpdb->dbh ); // @codingStandardsIgnoreLine. |
| 2054 | } else { |
| 2055 | $server_info = mysql_get_server_info( $wpdb->dbh ); // @codingStandardsIgnoreLine. |
| 2056 | } |
| 2057 | |
| 2058 | return array( |
| 2059 | 'string' => $server_info, |
| 2060 | 'number' => preg_replace( '/([^\d.]+).*/', '', $server_info ), |
| 2061 | ); |
| 2062 | } |
| 2063 | |
| 2064 | public function help_tip($tip = ''){ |
| 2065 | return '<span class="tutor-help-tip" data-tip="' . $tip . '"></span>'; |
| 2066 | } |
| 2067 | |
| 2068 | |
| 2069 | public function get_top_question($course_id = 0, $user_id = 0, $offset = 0, $limit = 20){ |
| 2070 | $course_id = $this->get_post_id($course_id); |
| 2071 | $user_id = $this->get_user_id($user_id); |
| 2072 | |
| 2073 | global $wpdb; |
| 2074 | |
| 2075 | $questions = $wpdb->get_results("select {$wpdb->comments}.comment_ID, |
| 2076 | {$wpdb->comments}.comment_post_ID, |
| 2077 | {$wpdb->comments}.comment_author, |
| 2078 | {$wpdb->comments}.comment_date, |
| 2079 | {$wpdb->comments}.comment_content, |
| 2080 | {$wpdb->comments}.user_id, |
| 2081 | {$wpdb->commentmeta}.meta_value as question_title, |
| 2082 | {$wpdb->users}.display_name |
| 2083 | |
| 2084 | from {$wpdb->comments} |
| 2085 | INNER JOIN {$wpdb->commentmeta} |
| 2086 | ON {$wpdb->comments}.comment_ID = {$wpdb->commentmeta}.comment_id |
| 2087 | INNER JOIN {$wpdb->users} |
| 2088 | ON {$wpdb->comments}.user_id = {$wpdb->users}.ID |
| 2089 | WHERE {$wpdb->comments}.comment_post_ID = {$course_id} |
| 2090 | AND {$wpdb->comments}.user_id = {$user_id} |
| 2091 | AND {$wpdb->comments}.comment_type = 'tutor_q_and_a' |
| 2092 | AND meta_key = 'tutor_question_title' ORDER BY comment_ID DESC LIMIT {$offset},{$limit} ;" |
| 2093 | ); |
| 2094 | |
| 2095 | return $questions; |
| 2096 | } |
| 2097 | |
| 2098 | public function get_total_qa_question($search_term = ''){ |
| 2099 | global $wpdb; |
| 2100 | |
| 2101 | if ($search_term){ |
| 2102 | $search_term = " AND {$wpdb->commentmeta}.meta_value LIKE '%{$search_term}%' "; |
| 2103 | } |
| 2104 | |
| 2105 | $count = $wpdb->get_var("SELECT COUNT({$wpdb->comments}.comment_ID) FROM {$wpdb->comments} |
| 2106 | INNER JOIN {$wpdb->commentmeta} |
| 2107 | ON {$wpdb->comments}.comment_ID = {$wpdb->commentmeta}.comment_id |
| 2108 | WHERE comment_type = 'tutor_q_and_a' AND comment_parent = 0 {$search_term} "); |
| 2109 | |
| 2110 | return (int) $count; |
| 2111 | } |
| 2112 | |
| 2113 | /** |
| 2114 | * @param int $start |
| 2115 | * @param int $limit |
| 2116 | * @param string $search_term |
| 2117 | * |
| 2118 | * @return array|null|object |
| 2119 | * |
| 2120 | * |
| 2121 | * Get question and answer query |
| 2122 | * |
| 2123 | * @since v.1.0.0 |
| 2124 | */ |
| 2125 | public function get_qa_questions($start = 0, $limit = 10, $search_term = '') { |
| 2126 | global $wpdb; |
| 2127 | |
| 2128 | if ($search_term){ |
| 2129 | $search_term = " AND {$wpdb->commentmeta}.meta_value LIKE '%{$search_term}%' "; |
| 2130 | } |
| 2131 | |
| 2132 | $query = $wpdb->get_results("SELECT |
| 2133 | {$wpdb->comments}.comment_ID, |
| 2134 | {$wpdb->comments}.comment_post_ID, |
| 2135 | {$wpdb->comments}.comment_author, |
| 2136 | {$wpdb->comments}.comment_date, |
| 2137 | {$wpdb->comments}.comment_content, |
| 2138 | {$wpdb->comments}.user_id, |
| 2139 | {$wpdb->commentmeta}.meta_value as question_title, |
| 2140 | {$wpdb->users}.display_name, |
| 2141 | {$wpdb->posts}.post_title, |
| 2142 | |
| 2143 | (SELECT COUNT(answers_t.comment_ID) FROM {$wpdb->comments} answers_t |
| 2144 | WHERE answers_t.comment_parent = {$wpdb->comments}.comment_ID ) as answer_count |
| 2145 | |
| 2146 | FROM {$wpdb->comments} |
| 2147 | |
| 2148 | INNER JOIN {$wpdb->commentmeta} |
| 2149 | ON {$wpdb->comments}.comment_ID = {$wpdb->commentmeta}.comment_id |
| 2150 | |
| 2151 | INNER JOIN {$wpdb->posts} |
| 2152 | ON {$wpdb->comments}.comment_post_ID = {$wpdb->posts}.ID |
| 2153 | |
| 2154 | INNER JOIN {$wpdb->users} |
| 2155 | ON {$wpdb->comments}.user_id = {$wpdb->users}.ID |
| 2156 | |
| 2157 | WHERE {$wpdb->comments}.comment_type = 'tutor_q_and_a' AND {$wpdb->comments}.comment_parent = 0 {$search_term} |
| 2158 | ORDER BY {$wpdb->comments}.comment_ID DESC |
| 2159 | LIMIT {$start},{$limit}; "); |
| 2160 | |
| 2161 | return $query; |
| 2162 | } |
| 2163 | |
| 2164 | public function get_qa_question($question_id){ |
| 2165 | global $wpdb; |
| 2166 | $query = $wpdb->get_row("SELECT |
| 2167 | {$wpdb->comments}.comment_ID, |
| 2168 | {$wpdb->comments}.comment_post_ID, |
| 2169 | {$wpdb->comments}.comment_author, |
| 2170 | {$wpdb->comments}.comment_date, |
| 2171 | {$wpdb->comments}.comment_content, |
| 2172 | {$wpdb->comments}.user_id, |
| 2173 | {$wpdb->commentmeta}.meta_value as question_title, |
| 2174 | {$wpdb->users}.display_name, |
| 2175 | {$wpdb->posts}.post_title |
| 2176 | |
| 2177 | FROM {$wpdb->comments} |
| 2178 | INNER JOIN {$wpdb->commentmeta} |
| 2179 | ON {$wpdb->comments}.comment_ID = {$wpdb->commentmeta}.comment_id |
| 2180 | |
| 2181 | INNER JOIN {$wpdb->posts} |
| 2182 | ON {$wpdb->comments}.comment_post_ID = {$wpdb->posts}.ID |
| 2183 | |
| 2184 | INNER JOIN {$wpdb->users} |
| 2185 | ON {$wpdb->comments}.user_id = {$wpdb->users}.ID |
| 2186 | WHERE comment_type = 'tutor_q_and_a' AND {$wpdb->comments}.comment_ID = {$question_id}"); |
| 2187 | |
| 2188 | return $query; |
| 2189 | } |
| 2190 | |
| 2191 | public function get_qa_answer_by_question($question_id){ |
| 2192 | global $wpdb; |
| 2193 | $query = $wpdb->get_results("SELECT |
| 2194 | {$wpdb->comments}.comment_ID, |
| 2195 | {$wpdb->comments}.comment_post_ID, |
| 2196 | {$wpdb->comments}.comment_author, |
| 2197 | {$wpdb->comments}.comment_date, |
| 2198 | {$wpdb->comments}.comment_content, |
| 2199 | {$wpdb->comments}.comment_parent, |
| 2200 | {$wpdb->comments}.user_id, |
| 2201 | {$wpdb->users}.display_name |
| 2202 | |
| 2203 | FROM {$wpdb->comments} |
| 2204 | |
| 2205 | INNER JOIN {$wpdb->users} |
| 2206 | ON {$wpdb->comments}.user_id = {$wpdb->users}.ID |
| 2207 | WHERE comment_type = 'tutor_q_and_a' |
| 2208 | AND {$wpdb->comments}.comment_parent = {$question_id} ORDER BY {$wpdb->comments}.comment_ID ASC "); |
| 2209 | |
| 2210 | return $query; |
| 2211 | } |
| 2212 | |
| 2213 | public function unanswered_question_count(){ |
| 2214 | global $wpdb; |
| 2215 | |
| 2216 | $count = $wpdb->get_var("select COUNT({$wpdb->comments}.comment_ID) |
| 2217 | from {$wpdb->comments} |
| 2218 | WHERE {$wpdb->comments}.comment_type = 'tutor_q_and_a' |
| 2219 | AND {$wpdb->comments}.comment_approved = 'waiting_for_answer' |
| 2220 | AND {$wpdb->comments}.comment_parent = 0;"); |
| 2221 | return (int) $count; |
| 2222 | } |
| 2223 | |
| 2224 | /** |
| 2225 | * @param int $course_id |
| 2226 | * |
| 2227 | * @return array|null|object |
| 2228 | * |
| 2229 | * Return all of announcements for a course |
| 2230 | * |
| 2231 | * @since v.1.0.0 |
| 2232 | */ |
| 2233 | public function get_announcements($course_id = 0){ |
| 2234 | $course_id = $this->get_post_id($course_id); |
| 2235 | global $wpdb; |
| 2236 | |
| 2237 | $query = $wpdb->get_results("select {$wpdb->posts}.ID, post_author, post_date, post_content, post_title, display_name |
| 2238 | from {$wpdb->posts} |
| 2239 | INNER JOIN {$wpdb->users} ON post_author = {$wpdb->users}.ID |
| 2240 | WHERE post_type = 'tutor_announcements' |
| 2241 | AND post_parent = {$course_id} ORDER BY {$wpdb->posts}.ID DESC;"); |
| 2242 | return $query; |
| 2243 | } |
| 2244 | |
| 2245 | public function announcement_content($content = ''){ |
| 2246 | $search = array('{user_display_name}'); |
| 2247 | |
| 2248 | $user_display_name = 'User'; |
| 2249 | if (is_user_logged_in()){ |
| 2250 | $user = wp_get_current_user(); |
| 2251 | $user_display_name = $user->display_name; |
| 2252 | } |
| 2253 | $replace = array($user_display_name); |
| 2254 | |
| 2255 | return str_replace($search, $replace, $content); |
| 2256 | } |
| 2257 | |
| 2258 | /** |
| 2259 | * @param int $post_id |
| 2260 | * @param string $option_key |
| 2261 | * @param bool $default |
| 2262 | * |
| 2263 | * @return array|bool|mixed |
| 2264 | * |
| 2265 | * Get the quiz option from meta |
| 2266 | */ |
| 2267 | public function get_quiz_option($post_id = 0, $option_key = '', $default = false){ |
| 2268 | $post_id = $this->get_post_id($post_id); |
| 2269 | $get_option_meta = maybe_unserialize(get_post_meta($post_id, 'tutor_quiz_option', true)); |
| 2270 | |
| 2271 | $value = $this->avalue_dot($option_key, $get_option_meta); |
| 2272 | if ($value){ |
| 2273 | return $value; |
| 2274 | } |
| 2275 | return $default; |
| 2276 | } |
| 2277 | |
| 2278 | |
| 2279 | /** |
| 2280 | * @param int $quiz_id |
| 2281 | * |
| 2282 | * @return array|bool|null|object |
| 2283 | * |
| 2284 | * Get the questions by quiz ID |
| 2285 | */ |
| 2286 | public function get_questions_by_quiz($quiz_id = 0){ |
| 2287 | $quiz_id = $this->get_post_id($quiz_id); |
| 2288 | global $wpdb; |
| 2289 | |
| 2290 | $questions = $wpdb->get_results("SELECT ID, post_content, post_title, post_parent from {$wpdb->posts} WHERE post_type = 'tutor_question' AND post_parent = {$quiz_id} ORDER BY menu_order ASC "); |
| 2291 | |
| 2292 | if (is_array($questions) && count($questions)){ |
| 2293 | return $questions; |
| 2294 | } |
| 2295 | return false; |
| 2296 | } |
| 2297 | |
| 2298 | public function get_question_types($type = null){ |
| 2299 | $types = array( |
| 2300 | 'true_false' => __('True/False', 'tutor'), |
| 2301 | 'multiple_choice' => __('Multiple Choice', 'tutor'), |
| 2302 | 'single_choice' => __('Single Choice', 'tutor'), |
| 2303 | ); |
| 2304 | |
| 2305 | if (isset($types[$type])){ |
| 2306 | return $types[$type]; |
| 2307 | } |
| 2308 | return $types; |
| 2309 | } |
| 2310 | |
| 2311 | public function get_quiz_answer_options_by_question($question_id){ |
| 2312 | global $wpdb; |
| 2313 | |
| 2314 | $answer_options = $wpdb->get_results("select |
| 2315 | {$wpdb->comments}.comment_ID, |
| 2316 | {$wpdb->comments}.comment_post_ID, |
| 2317 | {$wpdb->comments}.comment_content |
| 2318 | |
| 2319 | FROM {$wpdb->comments} |
| 2320 | WHERE {$wpdb->comments}.comment_post_ID = {$question_id} |
| 2321 | AND {$wpdb->comments}.comment_type = 'quiz_answer_option' |
| 2322 | ORDER BY {$wpdb->comments}.comment_karma ASC ;"); |
| 2323 | |
| 2324 | if (is_array($answer_options) && count($answer_options)){ |
| 2325 | return $answer_options; |
| 2326 | } |
| 2327 | return false; |
| 2328 | } |
| 2329 | |
| 2330 | /** |
| 2331 | * @param $quiz_id |
| 2332 | * |
| 2333 | * @return int |
| 2334 | * |
| 2335 | * Get the next question order ID |
| 2336 | */ |
| 2337 | |
| 2338 | public function quiz_next_question_order_id($quiz_id){ |
| 2339 | global $wpdb; |
| 2340 | |
| 2341 | $last_order = (int) $wpdb->get_var("SELECT MAX(menu_order) FROM {$wpdb->posts} WHERE post_parent = {$quiz_id} AND post_type = 'tutor_question';"); |
| 2342 | return $last_order + 1; |
| 2343 | } |
| 2344 | |
| 2345 | public function get_quiz_id_by_question($question_id){ |
| 2346 | global $wpdb; |
| 2347 | |
| 2348 | $quiz_id = $wpdb->get_var("SELECT post_parent FROM {$wpdb->posts} WHERE ID = {$question_id} AND post_type = 'tutor_question' ;"); |
| 2349 | return $quiz_id; |
| 2350 | } |
| 2351 | |
| 2352 | public function get_unattached_quiz($config = array()){ |
| 2353 | global $wpdb; |
| 2354 | |
| 2355 | $default_attr = array( |
| 2356 | 'search_term' => '', |
| 2357 | 'start' => '0', |
| 2358 | 'limit' => '10', |
| 2359 | 'order' => 'DESC', |
| 2360 | 'order_by' => 'ID', |
| 2361 | ); |
| 2362 | $attr = array_merge($default_attr, $config); |
| 2363 | extract($attr); |
| 2364 | |
| 2365 | $search_query = ''; |
| 2366 | if (! empty($search_term)){ |
| 2367 | $search_query = "AND post_title LIKE '%{$search_term}%'"; |
| 2368 | } |
| 2369 | |
| 2370 | $questions = $wpdb->get_results("SELECT ID, post_content, post_title, post_parent from {$wpdb->posts} WHERE post_type = 'tutor_quiz' AND post_status = 'publish' AND post_parent = 0 {$search_query} ORDER BY {$order_by} {$order} LIMIT {$start},{$limit} "); |
| 2371 | |
| 2372 | if (is_array($questions) && count($questions)){ |
| 2373 | return $questions; |
| 2374 | } |
| 2375 | return false; |
| 2376 | } |
| 2377 | |
| 2378 | /** |
| 2379 | * @param int $post_id |
| 2380 | * |
| 2381 | * @return array|bool|null|object |
| 2382 | */ |
| 2383 | public function get_attached_quiz($post_id = 0){ |
| 2384 | global $wpdb; |
| 2385 | |
| 2386 | $post_id = $this->get_post_id($post_id); |
| 2387 | |
| 2388 | $questions = $wpdb->get_results("SELECT ID, post_content, post_title, post_parent from {$wpdb->posts} WHERE post_type = 'tutor_quiz' AND post_status = 'publish' AND post_parent = {$post_id}"); |
| 2389 | |
| 2390 | if (is_array($questions) && count($questions)){ |
| 2391 | return $questions; |
| 2392 | } |
| 2393 | return false; |
| 2394 | } |
| 2395 | |
| 2396 | |
| 2397 | public function get_course_by_quiz($quiz_id){ |
| 2398 | global $wpdb; |
| 2399 | |
| 2400 | $quiz_id = $this->get_post_id($quiz_id); |
| 2401 | $post = get_post($quiz_id); |
| 2402 | |
| 2403 | if ($post) { |
| 2404 | $course_post_type = tutor()->course_post_type; |
| 2405 | $course = $wpdb->get_row( "select ID, post_name, post_type, post_parent from {$wpdb->posts} where ID = {$post->post_parent} " ); |
| 2406 | |
| 2407 | if ($course) { |
| 2408 | //Checking if this topic |
| 2409 | if ( $course->post_type !== $course_post_type ) { |
| 2410 | $course = $wpdb->get_row( "select ID, post_name, post_type, post_parent from {$wpdb->posts} where ID = {$course->post_parent} " ); |
| 2411 | } |
| 2412 | //Checking if this lesson |
| 2413 | if ( $course->post_type !== $course_post_type ) { |
| 2414 | $course = $wpdb->get_row( "select ID, post_name, post_type, post_parent from {$wpdb->posts} where ID = {$course->post_parent} " ); |
| 2415 | } |
| 2416 | |
| 2417 | return $course; |
| 2418 | } |
| 2419 | } |
| 2420 | |
| 2421 | return false; |
| 2422 | } |
| 2423 | |
| 2424 | /** |
| 2425 | * @param $quiz_id |
| 2426 | * |
| 2427 | * @return int |
| 2428 | */ |
| 2429 | public function total_questions_for_student_by_quiz($quiz_id){ |
| 2430 | $quiz_id = $this->get_post_id($quiz_id); |
| 2431 | global $wpdb; |
| 2432 | |
| 2433 | $total_question = (int) $wpdb->get_var("select count(ID) from {$wpdb->posts} where post_parent = {$quiz_id} AND post_type = 'tutor_question' "); |
| 2434 | |
| 2435 | return $total_question; |
| 2436 | } |
| 2437 | |
| 2438 | public function is_started_quiz($quiz_id = 0){ |
| 2439 | global $wpdb; |
| 2440 | |
| 2441 | $quiz_id = $this->get_post_id($quiz_id); |
| 2442 | $user_id = get_current_user_id(); |
| 2443 | |
| 2444 | $is_started = $wpdb->get_row("SELECT |
| 2445 | comment_ID, |
| 2446 | comment_post_ID, |
| 2447 | comment_author, |
| 2448 | comment_date as quiz_started_at, |
| 2449 | comment_date_gmt, |
| 2450 | comment_approved as quiz_attempt_status, |
| 2451 | comment_parent, |
| 2452 | user_id |
| 2453 | |
| 2454 | FROM {$wpdb->comments} |
| 2455 | WHERE user_id = {$user_id} |
| 2456 | AND comment_type = 'tutor_quiz_attempt' |
| 2457 | AND comment_approved = 'quiz_started' |
| 2458 | AND comment_post_ID = {$quiz_id} ; "); |
| 2459 | |
| 2460 | return $is_started; |
| 2461 | } |
| 2462 | |
| 2463 | /** |
| 2464 | * @param $quiz_id |
| 2465 | * |
| 2466 | * Method for get the total amount of question for a quiz |
| 2467 | * Student will answer this amount of question, one quiz have many question |
| 2468 | * but student will answer a specific amount of questions |
| 2469 | * |
| 2470 | * @return int |
| 2471 | */ |
| 2472 | |
| 2473 | public function max_questions_for_take_quiz($quiz_id){ |
| 2474 | $quiz_id = $this->get_post_id($quiz_id); |
| 2475 | global $wpdb; |
| 2476 | |
| 2477 | $max_questions = (int) $wpdb->get_var("select count(ID) from {$wpdb->posts} where post_parent = {$quiz_id} AND post_type = 'tutor_question' "); |
| 2478 | $max_mentioned = (int) $this->get_quiz_option($quiz_id, 'max_questions_for_answer', 10); |
| 2479 | |
| 2480 | if ($max_mentioned < $max_questions ){ |
| 2481 | return $max_mentioned; |
| 2482 | } |
| 2483 | |
| 2484 | return $max_questions; |
| 2485 | } |
| 2486 | |
| 2487 | public function get_attempt($attempt_id = 0){ |
| 2488 | global $wpdb; |
| 2489 | |
| 2490 | $attempt = $wpdb->get_row("SELECT |
| 2491 | comment_ID, |
| 2492 | comment_post_ID, |
| 2493 | comment_author, |
| 2494 | comment_date as quiz_started_at, |
| 2495 | comment_date_gmt, |
| 2496 | comment_approved as quiz_attempt_status, |
| 2497 | comment_parent, |
| 2498 | user_id |
| 2499 | |
| 2500 | FROM {$wpdb->comments} |
| 2501 | WHERE comment_type = 'tutor_quiz_attempt' |
| 2502 | AND comment_ID = {$attempt_id} ;"); |
| 2503 | |
| 2504 | return $attempt; |
| 2505 | } |
| 2506 | |
| 2507 | public function quiz_attempt_info($quiz_attempt_id){ |
| 2508 | $attempt_info = get_comment_meta($quiz_attempt_id, 'quiz_attempt_info', true); |
| 2509 | return $attempt_info; |
| 2510 | } |
| 2511 | |
| 2512 | public function quiz_update_attempt_info($quiz_attempt_id, $attempt_info = array()){ |
| 2513 | $answers = tutor_utils()->avalue_dot('answers', $attempt_info); |
| 2514 | $total_marks = array_sum(wp_list_pluck($answers, 'question_mark')); |
| 2515 | $earned_marks = tutor_utils()->avalue_dot('marks_earned', $attempt_info); |
| 2516 | $earned_mark_percent = $earned_marks > 0 ? ( number_format(($earned_marks * 100) / $total_marks)) : 0; |
| 2517 | update_comment_meta($quiz_attempt_id, 'earned_mark_percent', $earned_mark_percent); |
| 2518 | |
| 2519 | return update_comment_meta($quiz_attempt_id,'quiz_attempt_info', $attempt_info); |
| 2520 | } |
| 2521 | |
| 2522 | public function get_rand_single_question_by_quiz_for_student($quiz_id = 0){ |
| 2523 | global $wpdb; |
| 2524 | |
| 2525 | $quiz_id = $this->get_post_id($quiz_id); |
| 2526 | |
| 2527 | $is_attempt = $this->is_started_quiz($quiz_id); |
| 2528 | $attempted_question_ids = array(); |
| 2529 | if ($is_attempt){ |
| 2530 | $attempt_info = $this->quiz_attempt_info($is_attempt->comment_ID); |
| 2531 | $attempted_question_ids = wp_list_pluck($this->avalue_dot('answers', $attempt_info),'questionID'); |
| 2532 | } |
| 2533 | $attempted_question_ids_string = implode(",", $attempted_question_ids); |
| 2534 | |
| 2535 | $not_in_sql = ""; |
| 2536 | if (is_array($attempted_question_ids) && count($attempted_question_ids)){ |
| 2537 | $not_in_sql = " AND ID NOT IN({$attempted_question_ids_string}) "; |
| 2538 | } |
| 2539 | |
| 2540 | $question = $wpdb->get_row("SELECT ID, post_content, post_title, post_parent |
| 2541 | from {$wpdb->posts} WHERE post_type = 'tutor_question' AND post_parent = {$quiz_id} {$not_in_sql} ORDER BY RAND() ;"); |
| 2542 | |
| 2543 | return $question; |
| 2544 | } |
| 2545 | |
| 2546 | /** |
| 2547 | * @param int $quiz_id |
| 2548 | * @param int $user_id |
| 2549 | * |
| 2550 | * @return array|bool|null|object |
| 2551 | * |
| 2552 | * Get all of the attempts by an user of a quiz |
| 2553 | */ |
| 2554 | |
| 2555 | public function quiz_attempts($quiz_id = 0, $user_id = 0){ |
| 2556 | global $wpdb; |
| 2557 | |
| 2558 | $quiz_id = $this->get_post_id($quiz_id); |
| 2559 | $user_id = $this->get_user_id($user_id); |
| 2560 | |
| 2561 | $attempts = $wpdb->get_results("SELECT |
| 2562 | {$wpdb->comments}.comment_ID, |
| 2563 | comment_post_ID, |
| 2564 | comment_author, |
| 2565 | comment_date as quiz_started_at, |
| 2566 | comment_date_gmt, |
| 2567 | comment_approved as quiz_attempt_status, |
| 2568 | comment_parent, |
| 2569 | user_id, |
| 2570 | |
| 2571 | attempt_info.meta_value as quiz_attempt_info, |
| 2572 | pass_mark.meta_value as pass_mark_percent |
| 2573 | |
| 2574 | FROM {$wpdb->comments} |
| 2575 | |
| 2576 | LEFT JOIN {$wpdb->commentmeta} attempt_info ON {$wpdb->comments}.comment_ID = attempt_info.comment_id AND attempt_info.meta_key = 'quiz_attempt_info' |
| 2577 | LEFT JOIN {$wpdb->commentmeta} pass_mark ON {$wpdb->comments}.comment_ID = pass_mark.comment_id AND pass_mark.meta_key = 'pass_mark_percent' |
| 2578 | |
| 2579 | WHERE user_id = {$user_id} |
| 2580 | AND comment_type = 'tutor_quiz_attempt' |
| 2581 | AND comment_approved != 'quiz_started' |
| 2582 | AND comment_post_ID = {$quiz_id} ; "); |
| 2583 | |
| 2584 | if (is_array($attempts) && count($attempts)){ |
| 2585 | return $attempts; |
| 2586 | } |
| 2587 | |
| 2588 | return false; |
| 2589 | } |
| 2590 | |
| 2591 | public function get_total_quiz_attempts($search_term = ''){ |
| 2592 | global $wpdb; |
| 2593 | |
| 2594 | if ($search_term){ |
| 2595 | $search_term = " AND ( user_email like '%{$search_term}%' OR display_name like '%{$search_term}%' OR post_title like '%{$search_term}%' ) "; |
| 2596 | } |
| 2597 | |
| 2598 | $count = $wpdb->get_var("SELECT COUNT({$wpdb->comments}.comment_ID) FROM {$wpdb->comments} |
| 2599 | INNER JOIN {$wpdb->posts} |
| 2600 | ON {$wpdb->comments}.comment_post_ID = {$wpdb->posts}.ID |
| 2601 | |
| 2602 | INNER JOIN {$wpdb->users} |
| 2603 | ON {$wpdb->comments}.user_id = {$wpdb->users}.ID |
| 2604 | |
| 2605 | WHERE comment_type = 'tutor_quiz_attempt' {$search_term} "); |
| 2606 | |
| 2607 | return (int) $count; |
| 2608 | } |
| 2609 | |
| 2610 | public function get_quiz_attempts($start = 0, $limit = 10, $search_term = '') { |
| 2611 | global $wpdb; |
| 2612 | |
| 2613 | if ($search_term){ |
| 2614 | $search_term = " AND ( user_email like '%{$search_term}%' OR display_name like '%{$search_term}%' OR post_title like '%{$search_term}%' ) "; |
| 2615 | } |
| 2616 | |
| 2617 | $query = $wpdb->get_results("SELECT |
| 2618 | {$wpdb->comments}.comment_ID, |
| 2619 | {$wpdb->comments}.comment_post_ID, |
| 2620 | {$wpdb->comments}.comment_author, |
| 2621 | {$wpdb->comments}.comment_date, |
| 2622 | {$wpdb->comments}.comment_content, |
| 2623 | {$wpdb->comments}.comment_approved as attempt_status, |
| 2624 | {$wpdb->comments}.user_id, |
| 2625 | {$wpdb->users}.display_name, |
| 2626 | {$wpdb->users}.user_email, |
| 2627 | {$wpdb->posts}.post_title, |
| 2628 | |
| 2629 | attempt_info.meta_value as quiz_attempt_info, |
| 2630 | pass_mark.meta_value as pass_mark_percent, |
| 2631 | |
| 2632 | (SELECT COUNT(answers_t.comment_ID) FROM {$wpdb->comments} answers_t |
| 2633 | WHERE answers_t.comment_parent = {$wpdb->comments}.comment_ID ) as answer_count |
| 2634 | |
| 2635 | FROM {$wpdb->comments} |
| 2636 | |
| 2637 | INNER JOIN {$wpdb->posts} |
| 2638 | ON {$wpdb->comments}.comment_post_ID = {$wpdb->posts}.ID |
| 2639 | |
| 2640 | INNER JOIN {$wpdb->users} |
| 2641 | ON {$wpdb->comments}.user_id = {$wpdb->users}.ID |
| 2642 | |
| 2643 | LEFT JOIN {$wpdb->commentmeta} attempt_info ON {$wpdb->comments}.comment_ID = attempt_info.comment_id AND attempt_info.meta_key = 'quiz_attempt_info' |
| 2644 | LEFT JOIN {$wpdb->commentmeta} pass_mark ON {$wpdb->comments}.comment_ID = pass_mark.comment_id AND pass_mark.meta_key = 'pass_mark_percent' |
| 2645 | |
| 2646 | WHERE {$wpdb->comments}.comment_type = 'tutor_quiz_attempt' {$search_term} |
| 2647 | ORDER BY {$wpdb->comments}.comment_ID DESC |
| 2648 | LIMIT {$start},{$limit}; "); |
| 2649 | |
| 2650 | return $query; |
| 2651 | } |
| 2652 | |
| 2653 | public function get_quiz_answers_by_ids($ids){ |
| 2654 | $ids = (array) $ids; |
| 2655 | |
| 2656 | if (!count($ids)){ |
| 2657 | return false; |
| 2658 | } |
| 2659 | |
| 2660 | $in_ids = implode(",", $ids); |
| 2661 | |
| 2662 | global $wpdb; |
| 2663 | $query = $wpdb->get_results("SELECT |
| 2664 | comment_ID, |
| 2665 | comment_content |
| 2666 | FROM {$wpdb->comments} |
| 2667 | WHERE comment_type = 'quiz_answer_option' AND comment_ID IN({$in_ids}) "); |
| 2668 | |
| 2669 | if (is_array($query) && count($query)){ |
| 2670 | return $query; |
| 2671 | } |
| 2672 | |
| 2673 | return false; |
| 2674 | } |
| 2675 | |
| 2676 | /** |
| 2677 | * @param null $level |
| 2678 | * |
| 2679 | * @return mixed |
| 2680 | * |
| 2681 | * Get the users / students / course levels |
| 2682 | */ |
| 2683 | |
| 2684 | public function course_levels($level = null){ |
| 2685 | $levels = apply_filters('tutor_course_level', array( |
| 2686 | 'all_levels' => __('All Levels', 'tutor'), |
| 2687 | 'beginner' => __('Beginner', 'tutor'), |
| 2688 | 'intermediate' => __('Intermediate', 'tutor'), |
| 2689 | 'expert' => __('Expert', 'tutor'), |
| 2690 | )); |
| 2691 | |
| 2692 | if ($level){ |
| 2693 | if (isset($levels[$level])){ |
| 2694 | return $levels[$level]; |
| 2695 | }else{ |
| 2696 | return ''; |
| 2697 | } |
| 2698 | } |
| 2699 | |
| 2700 | return $levels; |
| 2701 | } |
| 2702 | |
| 2703 | public function user_profile_permalinks(){ |
| 2704 | $permalinks = array( |
| 2705 | 'enrolled_course' => __('Enrolled Course', 'tutor'), |
| 2706 | 'courses_taken' => __('Courses Taken', 'tutor'), |
| 2707 | 'reviews_wrote' => __('Reviews Written', 'tutor'), |
| 2708 | ); |
| 2709 | |
| 2710 | return apply_filters('tutor_public_profile/permalinks', $permalinks); |
| 2711 | } |
| 2712 | |
| 2713 | public function student_register_url(){ |
| 2714 | $student_register_page = (int) $this->get_option('student_register_page'); |
| 2715 | |
| 2716 | if ($student_register_page){ |
| 2717 | return get_the_permalink($student_register_page); |
| 2718 | } |
| 2719 | return false; |
| 2720 | } |
| 2721 | |
| 2722 | public function tutor_dashboard_url(){ |
| 2723 | $page_id = (int) tutor_utils()->get_option('student_dashboard'); |
| 2724 | $page_id = apply_filters('tutor_dashboard_url', $page_id); |
| 2725 | return get_the_permalink($page_id); |
| 2726 | } |
| 2727 | |
| 2728 | /** |
| 2729 | * @param int $course_id |
| 2730 | * @param int $user_id |
| 2731 | * |
| 2732 | * @return bool |
| 2733 | * |
| 2734 | * is_wishlisted(); |
| 2735 | */ |
| 2736 | public function is_wishlisted($course_id = 0, $user_id = 0){ |
| 2737 | $course_id = $this->get_post_id($course_id); |
| 2738 | $user_id = $this->get_user_id($user_id); |
| 2739 | if ( ! $user_id){ |
| 2740 | return false; |
| 2741 | } |
| 2742 | |
| 2743 | global $wpdb; |
| 2744 | $if_added_to_list = (bool) $wpdb->get_row("select * from {$wpdb->usermeta} WHERE user_id = {$user_id} AND meta_key = '_tutor_course_wishlist' AND meta_value = {$course_id} ;"); |
| 2745 | |
| 2746 | return $if_added_to_list; |
| 2747 | } |
| 2748 | |
| 2749 | /** |
| 2750 | * @param int $user_id |
| 2751 | * |
| 2752 | * @return array|null|object |
| 2753 | * |
| 2754 | * Get the wish lists by an user |
| 2755 | */ |
| 2756 | public function get_wishlist($user_id = 0){ |
| 2757 | $user_id = $this->get_user_id($user_id); |
| 2758 | global $wpdb; |
| 2759 | |
| 2760 | $query = "SELECT $wpdb->posts.* |
| 2761 | FROM $wpdb->posts |
| 2762 | LEFT JOIN $wpdb->usermeta ON ($wpdb->posts.ID = $wpdb->usermeta.meta_value) |
| 2763 | WHERE $wpdb->usermeta.meta_key = '_tutor_course_wishlist' |
| 2764 | AND $wpdb->usermeta.user_id = {$user_id} |
| 2765 | ORDER BY $wpdb->usermeta.umeta_id DESC "; |
| 2766 | $pageposts = $wpdb->get_results($query, OBJECT); |
| 2767 | return $pageposts; |
| 2768 | } |
| 2769 | |
| 2770 | /** |
| 2771 | * @param int $limit |
| 2772 | * |
| 2773 | * @return array|null|object |
| 2774 | * |
| 2775 | * Getting popular courses |
| 2776 | */ |
| 2777 | public function most_popular_courses($limit = 10){ |
| 2778 | global $wpdb; |
| 2779 | |
| 2780 | $courses = $wpdb->get_results(" |
| 2781 | SELECT COUNT(enrolled.ID) as total_enrolled, |
| 2782 | enrolled.post_parent as course_id, |
| 2783 | course.* |
| 2784 | from {$wpdb->posts} enrolled |
| 2785 | INNER JOIN {$wpdb->posts} course ON enrolled.post_parent = course.ID |
| 2786 | WHERE enrolled.post_type = 'tutor_enrolled' AND enrolled.post_status = 'completed' |
| 2787 | |
| 2788 | GROUP BY course_id |
| 2789 | ORDER BY total_enrolled DESC LIMIT 0,{$limit} ;"); |
| 2790 | |
| 2791 | return $courses; |
| 2792 | } |
| 2793 | |
| 2794 | /** |
| 2795 | * @param int $limit |
| 2796 | * |
| 2797 | * @return array|bool|null|object |
| 2798 | * |
| 2799 | * Get most rated courses lists |
| 2800 | */ |
| 2801 | public function most_rated_courses($limit = 10){ |
| 2802 | global $wpdb; |
| 2803 | |
| 2804 | $result = $wpdb->get_results(" |
| 2805 | SELECT COUNT(comment_ID) AS total_rating, |
| 2806 | comment_ID, |
| 2807 | comment_post_ID, |
| 2808 | course.* |
| 2809 | FROM {$wpdb->comments} |
| 2810 | INNER JOIN {$wpdb->posts} course ON comment_post_ID = course.ID |
| 2811 | WHERE {$wpdb->comments}.comment_type = 'tutor_course_rating' AND {$wpdb->comments}.comment_approved = 'approved' |
| 2812 | GROUP BY comment_post_ID ORDER BY total_rating DESC LIMIT 0,{$limit} |
| 2813 | ;"); |
| 2814 | |
| 2815 | if (is_array($result) && count($result)){ |
| 2816 | return $result; |
| 2817 | } |
| 2818 | return false; |
| 2819 | } |
| 2820 | |
| 2821 | |
| 2822 | public function get_addon_config($addon_field = null){ |
| 2823 | if ( ! $addon_field){ |
| 2824 | return false; |
| 2825 | } |
| 2826 | |
| 2827 | $addonsConfig = maybe_unserialize(get_option('tutor_addons_config')); |
| 2828 | |
| 2829 | |
| 2830 | if (isset($addonsConfig[$addon_field])){ |
| 2831 | return $addonsConfig[$addon_field]; |
| 2832 | } |
| 2833 | |
| 2834 | return false; |
| 2835 | } |
| 2836 | |
| 2837 | |
| 2838 | } |