PluginProbe
Tutor LMS – eLearning and online course solution / 1.0.7
Tutor LMS – eLearning and online course solution v1.0.7
4.0.7 4.0.6 4.0.5 4.0.4 4.0.3 4.0.2 4.0.1 4.0.0 3.9.15 3.9.14 3.9.13 3.9.12 3.9.11 trunk 1.0.0 1.0.0-alpha 1.0.1 1.0.2 1.0.3 1.0.4 1.0.5 1.0.6 1.0.7 1.0.8 1.0.9 All 191 releases
tutor / classes / Utils.php

Utils.php in Tutor LMS – eLearning and online course solution 1.0.7, at classes/Utils.php

3,715 lines 92.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 * @since v.1.0.0
56 */
57
58 public function update_option($key = null, $value = false){
59 $option = (array) maybe_unserialize(get_option('tutor_option'));
60 $option[$key] = $value;
61 update_option('tutor_option', $option);
62 }
63
64 /**
65 * @param null $key
66 * @param array $array
67 *
68 * @return array|bool|mixed
69 *
70 * get array value by dot notation
71 *
72 * @since v.1.0.0
73 *
74 */
75
76 public function avalue_dot($key = null, $array = array()){
77 $array = (array) $array;
78 if ( ! $key || ! count($array) ){
79 return false;
80 }
81 $option_key_array = explode('.', $key);
82
83 $value = $array;
84
85 foreach ($option_key_array as $dotKey){
86 if (isset($value[$dotKey])){
87 $value = $value[$dotKey];
88 }else{
89 return false;
90 }
91 }
92 return $value;
93 }
94
95 /**
96 * @return array
97 *
98 * Get all pages
99 *
100 * @since v.1.0.0
101 */
102 public function get_pages(){
103 $pages = array();
104 $wp_pages = get_pages();
105 if (is_array($wp_pages) && count($wp_pages)){
106 foreach ($wp_pages as $page){
107 $pages[$page->ID] = $page->post_title;
108 }
109 }
110 return $pages;
111 }
112
113 /**
114 * @return string
115 *
116 * Get course archive URL
117 *
118 * @since v.1.0.0
119 */
120 public function course_archive_page_url(){
121 $course_post_type = tutor()->course_post_type;
122 $course_page_url = trailingslashit(home_url()).$course_post_type;
123
124 $course_archive_page = $this->get_option('course_archive_page');
125 if ($course_archive_page && $course_archive_page !== '-1'){
126 $course_page_url = get_permalink($course_archive_page);
127 }
128 return trailingslashit($course_page_url);
129 }
130
131 /**
132 * @param int $student_id
133 *
134 * @return string
135 *
136 * Get student URL
137 *
138 * @since v.1.0.0
139 */
140
141 public function profile_url($student_id = 0){
142 $site_url = trailingslashit(home_url()).'profile/';
143 $user_name = '';
144
145 $student_id = $this->get_user_id($student_id);
146 if ($student_id){
147 global $wpdb;
148 $user = $wpdb->get_row("SELECT user_login from {$wpdb->users} WHERE ID = {$student_id} ");
149 if ($user){
150 $user_name = $user->user_login;
151 }
152 }else{
153 $user_name = 'user_name';
154 }
155
156 return $site_url.$user_name;
157 }
158
159 /**
160 * @param string $user_login
161 *
162 * @return array|null|object
163 *
164 * Get user by user login
165 *
166 * @since v.1.0.0
167 */
168 public function get_user_by_login($user_login = ''){
169 global $wpdb;
170 $user_login = sanitize_text_field($user_login);
171 $user = $wpdb->get_row("SELECT * from {$wpdb->users} WHERE user_login = '{$user_login}'");
172 return $user;
173 }
174
175 /**
176 * @return bool
177 *
178 * Check if WooCommerce Activated
179 *
180 * @since v.1.0.0
181 */
182
183 public function has_wc(){
184 $activated_plugins = apply_filters( 'active_plugins', get_option( 'active_plugins' ));
185 //$depends = array('woocommerce/woocommerce.php', 'tutor-woocommerce/tutor-woocommerce.php');
186 $depends = array('woocommerce/woocommerce.php');
187 $has = count(array_intersect($depends, $activated_plugins)) == count($depends);
188
189 return $has;
190 }
191
192 /**
193 * @return bool
194 *
195 * determine if EDD plugin activated
196 *
197 * @since v.1.0.0
198 */
199 public function has_edd(){
200 $activated_plugins = apply_filters( 'active_plugins', get_option( 'active_plugins' ));
201 //$depends = array('easy-digital-downloads/easy-digital-downloads.php', 'tutor-edd/tutor-edd.php');
202 $depends = array('easy-digital-downloads/easy-digital-downloads.php');
203 $has = count(array_intersect($depends, $activated_plugins)) == count($depends);
204
205 return $has;
206 }
207
208 /**
209 * @return mixed
210 *
211 * @since v.1.0.0
212 */
213 public function languages(){
214 $language_codes = array(
215 'en' => 'English' ,
216 'aa' => 'Afar' ,
217 'ab' => 'Abkhazian' ,
218 'af' => 'Afrikaans' ,
219 'am' => 'Amharic' ,
220 'ar' => 'Arabic' ,
221 'as' => 'Assamese' ,
222 'ay' => 'Aymara' ,
223 'az' => 'Azerbaijani' ,
224 'ba' => 'Bashkir' ,
225 'be' => 'Byelorussian' ,
226 'bg' => 'Bulgarian' ,
227 'bh' => 'Bihari' ,
228 'bi' => 'Bislama' ,
229 'bn' => 'Bengali/Bangla' ,
230 'bo' => 'Tibetan' ,
231 'br' => 'Breton' ,
232 'ca' => 'Catalan' ,
233 'co' => 'Corsican' ,
234 'cs' => 'Czech' ,
235 'cy' => 'Welsh' ,
236 'da' => 'Danish' ,
237 'de' => 'German' ,
238 'dz' => 'Bhutani' ,
239 'el' => 'Greek' ,
240 'eo' => 'Esperanto' ,
241 'es' => 'Spanish' ,
242 'et' => 'Estonian' ,
243 'eu' => 'Basque' ,
244 'fa' => 'Persian' ,
245 'fi' => 'Finnish' ,
246 'fj' => 'Fiji' ,
247 'fo' => 'Faeroese' ,
248 'fr' => 'French' ,
249 'fy' => 'Frisian' ,
250 'ga' => 'Irish' ,
251 'gd' => 'Scots/Gaelic' ,
252 'gl' => 'Galician' ,
253 'gn' => 'Guarani' ,
254 'gu' => 'Gujarati' ,
255 'ha' => 'Hausa' ,
256 'hi' => 'Hindi' ,
257 'hr' => 'Croatian' ,
258 'hu' => 'Hungarian' ,
259 'hy' => 'Armenian' ,
260 'ia' => 'Interlingua' ,
261 'ie' => 'Interlingue' ,
262 'ik' => 'Inupiak' ,
263 'in' => 'Indonesian' ,
264 'is' => 'Icelandic' ,
265 'it' => 'Italian' ,
266 'iw' => 'Hebrew' ,
267 'ja' => 'Japanese' ,
268 'ji' => 'Yiddish' ,
269 'jw' => 'Javanese' ,
270 'ka' => 'Georgian' ,
271 'kk' => 'Kazakh' ,
272 'kl' => 'Greenlandic' ,
273 'km' => 'Cambodian' ,
274 'kn' => 'Kannada' ,
275 'ko' => 'Korean' ,
276 'ks' => 'Kashmiri' ,
277 'ku' => 'Kurdish' ,
278 'ky' => 'Kirghiz' ,
279 'la' => 'Latin' ,
280 'ln' => 'Lingala' ,
281 'lo' => 'Laothian' ,
282 'lt' => 'Lithuanian' ,
283 'lv' => 'Latvian/Lettish' ,
284 'mg' => 'Malagasy' ,
285 'mi' => 'Maori' ,
286 'mk' => 'Macedonian' ,
287 'ml' => 'Malayalam' ,
288 'mn' => 'Mongolian' ,
289 'mo' => 'Moldavian' ,
290 'mr' => 'Marathi' ,
291 'ms' => 'Malay' ,
292 'mt' => 'Maltese' ,
293 'my' => 'Burmese' ,
294 'na' => 'Nauru' ,
295 'ne' => 'Nepali' ,
296 'nl' => 'Dutch' ,
297 'no' => 'Norwegian' ,
298 'oc' => 'Occitan' ,
299 'om' => '(Afan)/Oromoor/Oriya' ,
300 'pa' => 'Punjabi' ,
301 'pl' => 'Polish' ,
302 'ps' => 'Pashto/Pushto' ,
303 'pt' => 'Portuguese' ,
304 'qu' => 'Quechua' ,
305 'rm' => 'Rhaeto-Romance' ,
306 'rn' => 'Kirundi' ,
307 'ro' => 'Romanian' ,
308 'ru' => 'Russian' ,
309 'rw' => 'Kinyarwanda' ,
310 'sa' => 'Sanskrit' ,
311 'sd' => 'Sindhi' ,
312 'sg' => 'Sangro' ,
313 'sh' => 'Serbo-Croatian' ,
314 'si' => 'Singhalese' ,
315 'sk' => 'Slovak' ,
316 'sl' => 'Slovenian' ,
317 'sm' => 'Samoan' ,
318 'sn' => 'Shona' ,
319 'so' => 'Somali' ,
320 'sq' => 'Albanian' ,
321 'sr' => 'Serbian' ,
322 'ss' => 'Siswati' ,
323 'st' => 'Sesotho' ,
324 'su' => 'Sundanese' ,
325 'sv' => 'Swedish' ,
326 'sw' => 'Swahili' ,
327 'ta' => 'Tamil' ,
328 'te' => 'Tegulu' ,
329 'tg' => 'Tajik' ,
330 'th' => 'Thai' ,
331 'ti' => 'Tigrinya' ,
332 'tk' => 'Turkmen' ,
333 'tl' => 'Tagalog' ,
334 'tn' => 'Setswana' ,
335 'to' => 'Tonga' ,
336 'tr' => 'Turkish' ,
337 'ts' => 'Tsonga' ,
338 'tt' => 'Tatar' ,
339 'tw' => 'Twi' ,
340 'uk' => 'Ukrainian' ,
341 'ur' => 'Urdu' ,
342 'uz' => 'Uzbek' ,
343 'vi' => 'Vietnamese' ,
344 'vo' => 'Volapuk' ,
345 'wo' => 'Wolof' ,
346 'xh' => 'Xhosa' ,
347 'yo' => 'Yoruba' ,
348 'zh' => 'Chinese' ,
349 'zu' => 'Zulu' ,
350 );
351
352 return apply_filters('tutor/utils/languages', $language_codes);
353 }
354
355
356 /**
357 * @param string $value
358 *
359 * Check raw data
360 *
361 * @since v.1.0.0
362 */
363 public function print_view($value = ''){
364 echo '<pre>';
365 print_r($value);
366 echo '</pre>';
367 }
368
369 /**
370 * @param array $excludes
371 *
372 * @return array|null|object
373 *
374 * Get courses
375 *
376 * @since v.1.0.0
377 */
378
379 public function get_courses($excludes = array()){
380 global $wpdb;
381
382
383 $excludes = (array) $excludes;
384 $exclude_query = '';
385 if (count($excludes)){
386 $exclude_query = implode("','", $excludes);
387 }
388
389 $course_post_type = tutor()->course_post_type;
390 $query = $wpdb->get_results("SELECT ID, post_author, post_title, post_name,post_status, menu_order
391 from {$wpdb->posts} WHERE post_status = 'publish'
392 AND ID NOT IN('$exclude_query')
393 AND post_type = '{$course_post_type}' ");
394 return $query;
395 }
396
397 /**
398 * @param int $instructor_id
399 *
400 * @return array|null|object
401 *
402 * Get courses for instructors
403 *
404 * @since v.1.0.0
405 */
406 public function get_courses_for_instructors($instructor_id = 0){
407 global $wpdb;
408
409 $instructor_id = $this->get_user_id($instructor_id);
410
411 $course_post_type = tutor()->course_post_type;
412 $query = $wpdb->get_results("SELECT ID, post_author, post_title, post_name,post_status, menu_order
413 from {$wpdb->posts}
414 WHERE post_author = {$instructor_id}
415 AND post_status IN ('publish', 'pending')
416 AND post_type = '{$course_post_type}' ");
417 return $query;
418 }
419
420 /**
421 * @param $instructor_id
422 *
423 * @return null|string
424 *
425 * Get course count by instructor
426 *
427 * @since v.1.0.0
428 */
429
430 public function get_course_count_by_instructor($instructor_id){
431 global $wpdb;
432
433 $course_post_type = tutor()->course_post_type;
434 $count = $wpdb->get_var("SELECT COUNT(ID) from {$wpdb->posts}
435 INNER JOIN {$wpdb->usermeta} ON user_id = {$instructor_id} AND meta_key = '_tutor_instructor_course_id' AND meta_value = ID
436 WHERE post_status = 'publish'
437 AND post_type = '{$course_post_type}' ; ");
438
439 return $count;
440 }
441
442 /**
443 * @param $instructor_id
444 *
445 * @return array|null|object
446 *
447 * Get courses by a instructor
448 *
449 * @since v.1.0.0
450 */
451 public function get_courses_by_instructor($instructor_id){
452 global $wpdb;
453
454 $course_post_type = tutor()->course_post_type;
455
456 $querystr = "
457 SELECT $wpdb->posts.*
458 FROM $wpdb->posts
459 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
460
461
462 WHERE $wpdb->posts.post_status = 'publish'
463 AND $wpdb->posts.post_type = '{$course_post_type}'
464 AND $wpdb->posts.post_date < NOW()
465 ORDER BY $wpdb->posts.post_date DESC";
466
467 $pageposts = $wpdb->get_results($querystr, OBJECT);
468 return $pageposts;
469 }
470
471 /**
472 * @return mixed
473 *
474 * Get archive page course count
475 *
476 * @since v.1.0.0
477 */
478 public function get_archive_page_course_count(){
479 global $wp_query;
480 return $wp_query->post_count;
481 }
482
483 /**
484 * @return null|string
485 *
486 * Get course count
487 *
488 * @since v.1.0.0
489 */
490 public function get_course_count(){
491 global $wpdb;
492
493 $course_post_type = tutor()->course_post_type;
494 $count = $wpdb->get_var("SELECT COUNT(ID) from {$wpdb->posts} WHERE post_status = 'publish' AND post_type = '{$course_post_type}'; ");
495 return $count;
496 }
497
498 /**
499 * @return null|string
500 *
501 * Get lesson count
502 *
503 * @since v.1.0.0
504 */
505 public function get_lesson_count(){
506 global $wpdb;
507
508 $lesson_post_type = tutor()->lesson_post_type;
509 $count = $wpdb->get_var("SELECT COUNT(ID) from {$wpdb->posts} WHERE post_status = 'publish' AND post_type = '{$lesson_post_type}'; ");
510 return $count;
511 }
512
513 /**
514 * @param int $course_id
515 * @param int $limit
516 *
517 * @return \WP_Query
518 *
519 * Get lesson
520 *
521 * @since v.1.0.0
522 */
523 public function get_lesson($course_id = 0, $limit = 10){
524 $course_id = $this->get_post_id($course_id);
525
526 $lesson_post_type = tutor()->lesson_post_type;
527 $args = array(
528 'post_status' => 'publish',
529 'post_type' => $lesson_post_type,
530 'posts_per_page' => $limit,
531 'meta_query' => array(
532 array(
533 'key' => '_tutor_course_id_for_lesson',
534 'value' => $course_id,
535 'compare' => '=',
536 ),
537 ),
538 );
539 $query = new \WP_Query($args);
540
541 return $query;
542 }
543
544 /**
545 * @param int $course_id
546 *
547 * @return int
548 *
549 * Get total lesson count by a course
550 *
551 * @since v.1.0.0
552 */
553 public function get_lesson_count_by_course($course_id = 0){
554 $course_id = $this->get_post_id($course_id);
555 global $wpdb;
556
557 $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} ");
558
559 return (int) $count_lesson;
560 }
561
562 /**
563 * @param int $course_id
564 * @param int $user_id
565 *
566 * @return int
567 *
568 * Get completed lesson total number by a course
569 *
570 * @since v.1.0.0
571 */
572 public function get_completed_lesson_count_by_course($course_id = 0, $user_id = 0){
573 $course_id = $this->get_post_id($course_id);
574 $user_id = $this->get_user_id($user_id);
575 global $wpdb;
576
577 $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} ");
578
579 $count = 0;
580 if (is_array($completed_lesson_ids) && count($completed_lesson_ids)){
581 $completed_lesson_meta_ids = array();
582 foreach ($completed_lesson_ids as $lesson_id){
583 $completed_lesson_meta_ids[] = '_tutor_completed_lesson_id_'.$lesson_id;
584 }
585 $in_ids = implode("','", $completed_lesson_meta_ids);
586
587 $count = (int) $wpdb->get_var("select count(umeta_id) from {$wpdb->usermeta} WHERE user_id = '{$user_id}' AND meta_key in('{$in_ids}') ");
588 }
589
590 return $count;
591 }
592
593 /**
594 * @param int $course_id
595 * @param int $user_id
596 *
597 * @return float|int
598 *
599 * @since v.1.0.0
600 */
601 public function get_course_completed_percent($course_id = 0, $user_id = 0){
602 $course_id = $this->get_post_id($course_id);
603 $user_id = $this->get_user_id($user_id);
604
605 $total_lesson = $this->get_lesson_count_by_course($course_id);
606 $completed_lesson = $this->get_completed_lesson_count_by_course($course_id, $user_id);
607
608 if ($total_lesson > 0 && $completed_lesson > 0){
609 return number_format(($completed_lesson * 100) / $total_lesson);
610 }
611
612 return 0;
613 }
614
615 /**
616 * @param int $course_id
617 *
618 * @return \WP_Query
619 *
620 * Get all topics by given course ID
621 *
622 * @since v.1.0.0
623 */
624 public function get_topics($course_id = 0){
625 $course_id = $this->get_post_id($course_id);
626
627 $args = array(
628 'post_type' => 'topics',
629 'post_parent' => $course_id,
630 'orderby' => 'menu_order',
631 'order' => 'ASC',
632 'posts_per_page' => -1,
633 );
634
635 $query = new \WP_Query($args);
636 return $query;
637 }
638
639 /**
640 * @param $course_ID
641 *
642 * @return int
643 *
644 * Get next topic order id
645 *
646 * @since v.1.0.0
647 */
648 public function get_next_topic_order_id($course_ID){
649 global $wpdb;
650
651 $last_order = (int) $wpdb->get_var("SELECT MAX(menu_order) FROM {$wpdb->posts} WHERE post_parent = {$course_ID} AND post_type = 'topics';");
652 return $last_order + 1;
653 }
654
655 /**
656 * @param $topic_ID
657 *
658 * @return int
659 *
660 * Get next course content order id
661 *
662 * @since v.1.0.0
663 */
664 public function get_next_course_content_order_id($topic_ID){
665 global $wpdb;
666
667 $last_order = (int) $wpdb->get_var("SELECT MAX(menu_order) FROM {$wpdb->posts} WHERE post_parent = {$topic_ID};");
668 return $last_order + 1;
669 }
670
671 /**
672 * @param int $topics_id
673 * @param int $limit
674 *
675 * @return \WP_Query
676 *
677 * Get lesson by topic
678 *
679 * @since v.1.0.0
680 */
681 public function get_lessons_by_topic($topics_id = 0, $limit = 10){
682 $topics_id = $this->get_post_id($topics_id);
683
684 $lesson_post_type = tutor()->lesson_post_type;
685 $args = array(
686 'post_type' => $lesson_post_type,
687 'post_parent' => $topics_id,
688 'posts_per_page' => $limit,
689 'orderby' => 'menu_order',
690 'order' => 'ASC',
691 );
692
693 $query = new \WP_Query($args);
694
695 return $query;
696 }
697
698 /**
699 * @param int $topics_id
700 * @param int $limit
701 *
702 * @return \WP_Query
703 *
704 * Get course content by topic
705 *
706 * @since v.1.0.0
707 */
708 public function get_course_contents_by_topic($topics_id = 0, $limit = 10){
709 $topics_id = $this->get_post_id($topics_id);
710
711 $lesson_post_type = tutor()->lesson_post_type;
712 $args = array(
713 'post_type' => array($lesson_post_type, 'tutor_quiz'),
714 'post_parent' => $topics_id,
715 'posts_per_page' => $limit,
716 'orderby' => 'menu_order',
717 'order' => 'ASC',
718 );
719
720 $query = new \WP_Query($args);
721
722 return $query;
723 }
724
725 /**
726 * @param string $request_method
727 *
728 * Check actions nonce
729 *
730 * @since v.1.0.0
731 */
732 public function checking_nonce($request_method = 'post'){
733 if ($request_method === 'post'){
734 if (!isset($_POST[tutor()->nonce]) || !wp_verify_nonce($_POST[tutor()->nonce], tutor()->nonce_action)) {
735 exit();
736 }
737 }else{
738 if (!isset($_GET[tutor()->nonce]) || !wp_verify_nonce($_GET[tutor()->nonce], tutor()->nonce_action)) {
739 exit();
740 }
741 }
742 }
743
744 /**
745 * @param int $course_id
746 *
747 * @return bool
748 *
749 * @since v.1.0.0
750 */
751 public function is_course_purchasable($course_id = 0){
752 return apply_filters('is_course_purchasable', false, $course_id);
753 }
754
755 /**
756 * @param int $course_id
757 *
758 * @return null|string
759 *
760 * get course price in digits format if any
761 *
762 * @since v.1.0.0
763 */
764
765 public function get_course_price($course_id = 0){
766 $course_id = $this->get_post_id($course_id);
767
768 $price = null;
769
770 if ($this->is_course_purchasable()) {
771 if ($this->has_wc()){
772 $product_id = tutor_utils()->get_course_product_id($course_id);
773 $product = wc_get_product( $product_id );
774
775 if ( $product ) {
776 $price = $product->get_price();
777 }
778 }else{
779 $price = apply_filters('get_tutor_course_price', null, $course_id);
780 }
781
782 }
783
784 return $price;
785 }
786
787 /**
788 * @param int $course_id
789 *
790 * @return array|bool|null|object
791 *
792 * Check if current user has been enrolled or not
793 *
794 * @since v.1.0.0
795 */
796
797 public function is_enrolled($course_id = 0, $user_id = 0){
798 $course_id = $this->get_post_id($course_id);
799 $user_id = $this->get_user_id($user_id);
800
801 if (is_user_logged_in()) {
802 global $wpdb;
803
804 $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'; " );
805
806 if ( $getEnrolledInfo ) {
807 return $getEnrolledInfo;
808 }
809 }
810 return false;
811 }
812
813 /**
814 * @param int $course_id
815 * @param int $user_id
816 *
817 * @return array|bool|null|object|void
818 *
819 * Has any enrolled for a user in a course
820 *
821 * @since v.1.0.0
822 */
823 public function has_any_enrolled($course_id = 0, $user_id = 0){
824 $course_id = $this->get_post_id($course_id);
825 $user_id = $this->get_user_id($user_id);
826
827 if (is_user_logged_in()) {
828 global $wpdb;
829
830 $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}; " );
831
832 if ( $getEnrolledInfo ) {
833 return $getEnrolledInfo;
834 }
835 }
836 return false;
837 }
838
839 /**
840 * @param int $lesson_id
841 * @param int $user_id
842 *
843 * @return array|bool|null|object
844 *
845 * Get the course Enrolled confirmation by lesson ID
846 *
847 * @since v.1.0.0
848 */
849
850 public function is_course_enrolled_by_lesson($lesson_id = 0, $user_id = 0){
851 $lesson_id = $this->get_post_id($lesson_id);
852 $user_id = $this->get_user_id($user_id);
853
854 return $this->is_enrolled($this->get_course_id_by_lesson($lesson_id));
855 }
856
857 /**
858 * @param int $lesson_id
859 *
860 * @return bool|mixed
861 *
862 * Get the course ID by Lesson
863 *
864 * @since v.1.0.0
865 */
866 public function get_course_id_by_lesson($lesson_id = 0){
867 $lesson_id = $this->get_post_id($lesson_id);
868 return get_post_meta($lesson_id, '_tutor_course_id_for_lesson', true);
869 }
870
871 /**
872 * @param int $course_id
873 *
874 * @return bool|false|string
875 *
876 * Get first lesson of a course
877 *
878 * @since v.1.0.0
879 */
880 public function get_course_first_lesson($course_id = 0){
881 $course_id = $this->get_post_id($course_id);
882 global $wpdb;
883
884 $lesson_id = $wpdb->get_var("
885 SELECT post_id as lesson_id
886 FROM $wpdb->postmeta
887 INNER JOIN {$wpdb->posts} ON post_id = {$wpdb->posts}.ID
888 WHERE meta_key = '_tutor_course_id_for_lesson' AND meta_value = {$course_id}
889
890 ORDER BY menu_order ASC LIMIT 1
891 ");
892
893 /*
894 $lesson_id = $wpdb->get_var(" select main_posts.ID from {$wpdb->posts} main_posts
895 WHERE post_parent =
896 (SELECT sub_posts.ID FROM {$wpdb->posts} sub_posts
897 WHERE post_type = 'topics' AND
898 sub_posts.post_parent = {$course_id} ORDER BY sub_posts.menu_order ASC LIMIT 1 )
899 ORDER BY main_posts.menu_order ASC LIMIT 1 ;");
900 */
901
902 if ($lesson_id){
903 return get_permalink($lesson_id);
904 }
905 return false;
906 }
907
908 /**
909 *
910 * Get course sub pages in course dashboard
911 *
912 * @since v.1.0.0
913 */
914 public function course_sub_pages(){
915 $nav_items = array(
916 'overview' => __('Overview', 'tutor'),
917 );
918
919 $enable_q_and_a_on_course = tutor_utils()->get_option('enable_q_and_a_on_course');
920 if ($enable_q_and_a_on_course){
921 $nav_items['questions'] = __('Q&A', 'tutor');
922 }
923 $nav_items['announcements'] = __('Announcements', 'tutor');
924
925 return apply_filters('tutor_course/single/enrolled/nav_items', $nav_items);
926 }
927
928 /**
929 * @param int $post_id
930 *
931 * @return bool|array
932 *
933 * @since v.1.0.0
934 */
935 public function get_video($post_id = 0){
936 $post_id = $this->get_post_id($post_id);
937 $attachments = get_post_meta($post_id, '_video', true);
938 if ($attachments) {
939 $attachments = maybe_unserialize($attachments);
940 }
941 return $attachments;
942 }
943
944 /**
945 * @param int $post_id
946 * @param array $video_data
947 *
948 * @return bool
949 *
950 * Update the video Info
951 */
952 public function update_video($post_id = 0, $video_data = array()){
953 $post_id = $this->get_post_id($post_id);
954
955 if (is_array($video_data) && count($video_data)){
956 update_post_meta($post_id, '_video', $video_data);
957 }
958 }
959
960 /**
961 * @param int $post_id
962 *
963 * @return bool|mixed
964 *
965 * @since v.1.0.0
966 */
967 public function get_attachments($post_id = 0){
968 $post_id = $this->get_post_id($post_id);
969 $attachments_arr = array();
970 $attachments = maybe_unserialize(get_post_meta($post_id, '_tutor_attachments', true));
971
972 $font_icons = apply_filters('tutor_file_types_icon', array(
973 'archive',
974 'audio',
975 'code',
976 'default',
977 'document',
978 'interactive',
979 'spreadsheet',
980 'text',
981 'video',
982 'image',
983 ));
984
985 if ( is_array($attachments) && count($attachments)) {
986 foreach ( $attachments as $attachment ) {
987 $url = wp_get_attachment_url( $attachment );
988 $file_type = wp_check_filetype( $url );
989 $ext = $file_type['ext'];
990 $title = get_the_title($attachment);
991
992 $file_path = get_attached_file( $attachment );
993 $size_bytes = file_exists($file_path) ? filesize( $file_path ) : 0;
994 $size = size_format( $size_bytes, 2 );
995 $type = wp_ext2type( $ext );
996
997 $icon = 'default';
998 if ( $type && in_array( $type, $font_icons ) ) {
999 $icon = $type;
1000 }
1001
1002 $data = array(
1003 'post_id' => $post_id,
1004 'id' => $attachment,
1005 'url' => $url,
1006 'name' => $title . '.' . $ext,
1007 'title' => $title,
1008 'ext' => $ext,
1009 'size' => $size,
1010 'size_bytes' => $size_bytes,
1011 'icon' => $icon,
1012 );
1013
1014 $attachments_arr[] = (object) apply_filters( 'tutor/posts/attachments', $data );
1015 }
1016 }
1017
1018 return $attachments_arr;
1019 }
1020
1021
1022 /**
1023 * @param $seconds
1024 *
1025 * @return string
1026 *
1027 * return seconds to formatted playtime
1028 *
1029 * @since v.1.0.0
1030 */
1031 public function playtime_string($seconds) {
1032 $sign = (($seconds < 0) ? '-' : '');
1033 $seconds = round(abs($seconds));
1034 $H = (int) floor( $seconds / 3600);
1035 $M = (int) floor(($seconds - (3600 * $H) ) / 60);
1036 $S = (int) round( $seconds - (3600 * $H) - (60 * $M) );
1037 return $sign.($H ? $H.':' : '').($H ? str_pad($M, 2, '0', STR_PAD_LEFT) : intval($M)).':'.str_pad($S, 2, 0, STR_PAD_LEFT);
1038 }
1039
1040 /**
1041 * @param $seconds
1042 *
1043 * @return array
1044 *
1045 * Get the playtime in array
1046 *
1047 * @since v.1.0.0
1048 */
1049 public function playtime_array($seconds){
1050 $run_time_format = array(
1051 'hours' => '00',
1052 'minutes' => '00',
1053 'seconds' => '00',
1054 );
1055
1056 if ($seconds <= 0 ){
1057 return $run_time_format;
1058 }
1059
1060 $playTimeString = $this->playtime_string($seconds);
1061 $timeInArray = explode(':', $playTimeString);
1062
1063 $run_time_size = count($timeInArray);
1064 if ($run_time_size === 3){
1065 $run_time_format['hours'] = $timeInArray[0];
1066 $run_time_format['minutes'] = $timeInArray[1];
1067 $run_time_format['seconds'] = $timeInArray[2];
1068 }elseif($run_time_size === 2){
1069 $run_time_format['minutes'] = $timeInArray[0];
1070 $run_time_format['seconds'] = $timeInArray[1];
1071 }
1072
1073 return $run_time_format;
1074 }
1075
1076 /**
1077 * @param $seconds
1078 *
1079 * @return string
1080 *
1081 * Convert seconds to human readable time
1082 *
1083 * @since v.1.0.0
1084 */
1085 public function seconds_to_time_context($seconds) {
1086 $sign = (($seconds < 0) ? '-' : '');
1087 $seconds = round(abs($seconds));
1088 $H = (int) floor( $seconds / 3600);
1089 $M = (int) floor(($seconds - (3600 * $H) ) / 60);
1090 $S = (int) round( $seconds - (3600 * $H) - (60 * $M) );
1091
1092 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';
1093 }
1094
1095 /**
1096 * @param int $lesson_id
1097 *
1098 * @return bool|object
1099 *
1100 * @since v.1.0.0
1101 */
1102
1103 public function get_video_info($lesson_id = 0){
1104 $lesson_id = $this->get_post_id($lesson_id);
1105 $video = $this->get_video($lesson_id);
1106
1107 if ( ! $video){
1108 return false;
1109 }
1110
1111 $info = array(
1112 'playtime' => '00:00',
1113 );
1114
1115 $types = apply_filters('tutor_video_types', array("mp4"=>"video/mp4", "webm"=>"video/webm", "ogg"=>"video/ogg"));
1116
1117 $videoSource = $this->avalue_dot('source', $video);
1118 if ($videoSource === 'html5'){
1119 $sourceVideoID = $this->avalue_dot('source_video_id', $video);
1120 $video_info = get_post_meta($sourceVideoID, '_wp_attachment_metadata', true);
1121
1122 if ($video_info){
1123 $path = get_attached_file($sourceVideoID);
1124 $info['playtime'] = $video_info['length_formatted'];
1125 $info['path'] = $path;
1126 $info['url'] = wp_get_attachment_url($sourceVideoID);
1127 $info['ext'] = strtolower(pathinfo($path, PATHINFO_EXTENSION));
1128 $info['type'] = $types[$info['ext']];
1129 }
1130 }
1131
1132 if ($videoSource !== 'html5'){
1133 $video = maybe_unserialize(get_post_meta($lesson_id, '_video', true));
1134
1135 $runtimeHours = tutor_utils()->avalue_dot('runtime.hours', $video);
1136 $runtimeMinutes = tutor_utils()->avalue_dot('runtime.minutes', $video);
1137 $runtimeSeconds = tutor_utils()->avalue_dot('runtime.seconds', $video);
1138
1139 $runtimeHours = $runtimeHours ? $runtimeHours : '00';
1140 $runtimeMinutes = $runtimeMinutes ? $runtimeMinutes : '00';
1141 $runtimeSeconds = $runtimeSeconds ? $runtimeSeconds : '00';
1142
1143 $info['playtime'] = "$runtimeHours:$runtimeMinutes:$runtimeSeconds";
1144 }
1145
1146 $info = array_merge($info, $video);
1147
1148 return (object) $info;
1149 }
1150
1151 /**
1152 * @param int $post_id
1153 *
1154 * @return bool
1155 *
1156 * Ensure if attached video is self hosted or not
1157 *
1158 * @since v.1.0.0
1159 */
1160 public function is_html5_video($post_id = 0){
1161 $post_id = $this->get_post_id($post_id);
1162
1163 $video = $this->get_video($post_id);
1164 if ( ! $video){
1165 return false;
1166 }
1167 $videoSource = $this->avalue_dot('source', $video);
1168 return $videoSource === 'html5';
1169 }
1170
1171 /**
1172 *
1173 * return lesson type icon
1174 *
1175 * @param int $lesson_id
1176 * @param bool $html
1177 * @param bool $echo
1178 *
1179 * @return string
1180 *
1181 * @since v.1.0.0
1182 */
1183
1184 public function get_lesson_type_icon($lesson_id = 0, $html = false, $echo = false){
1185 $post_id = $this->get_post_id($lesson_id);
1186 $video = tutor_utils()->get_video_info($post_id);
1187
1188 $play_time = false;
1189 if ($video){
1190 $play_time = $video->playtime;
1191 }
1192
1193 $tutor_lesson_type_icon = $play_time ? 'youtube' : 'document';
1194
1195 if ($html){
1196 $tutor_lesson_type_icon = "<i class='tutor-icon-$tutor_lesson_type_icon'></i> ";
1197 }
1198
1199 if ($tutor_lesson_type_icon){
1200 echo $tutor_lesson_type_icon;
1201 }
1202
1203 return $tutor_lesson_type_icon;
1204 }
1205
1206 /**
1207 * @param int $lesson_id
1208 * @param int $user_id
1209 *
1210 * @return bool|mixed
1211 *
1212 * @since v.1.0.0
1213 */
1214
1215 public function is_completed_lesson($lesson_id = 0, $user_id = 0){
1216 $lesson_id = $this->get_post_id($lesson_id);
1217 $user_id = $this->get_user_id($user_id);
1218
1219 $is_completed = get_user_meta($user_id, '_tutor_completed_lesson_id_'.$lesson_id, true);
1220
1221 if ($is_completed){
1222 return $is_completed;
1223 }
1224
1225 return false;
1226 }
1227
1228 /**
1229 * @param int $course_id
1230 * @param int $user_id
1231 *
1232 * @return array|bool|null|object|void
1233 *
1234 * Determine if a course completed
1235 *
1236 * @since v.1.0.0
1237 */
1238
1239 public function is_completed_course($course_id = 0, $user_id = 0){
1240 global $wpdb;
1241 $course_id = $this->get_post_id($course_id);
1242 $user_id = $this->get_user_id($user_id);
1243
1244 $is_completed = $wpdb->get_row("SELECT comment_ID,
1245 comment_post_ID as course_id,
1246 comment_author as completed_user_id,
1247 comment_date as completion_date,
1248 comment_content as completed_hash
1249 from {$wpdb->comments}
1250 WHERE comment_agent = 'TutorLMSPlugin'
1251 AND comment_type = 'course_completed'
1252 AND comment_post_ID = {$course_id}
1253 AND user_id = {$user_id} ;");
1254
1255 if ($is_completed){
1256 return $is_completed;
1257 }
1258
1259 return false;
1260 }
1261
1262 /**
1263 * @param array $input
1264 *
1265 * @return array
1266 *
1267 * Sanitize input array
1268 *
1269 * @since v.1.0.0
1270 */
1271 public function sanitize_array($input = array()){
1272 $array = array();
1273
1274 if (is_array($input) && count($input)){
1275 foreach ($input as $key => $value){
1276 if (is_array($value)){
1277 $array[$key] = $this->sanitize_array($value);
1278 }else{
1279 $key = sanitize_text_field($key);
1280 $value = sanitize_text_field($value);
1281 $array[$key] = $value;
1282 }
1283 }
1284 }
1285
1286 return $array;
1287 }
1288
1289 /**
1290 * @param int $post_id
1291 *
1292 * @return array|bool
1293 *
1294 * Determine if has any video in single
1295 *
1296 * @since v.1.0.0
1297 */
1298
1299 public function has_video_in_single($post_id = 0){
1300 if (is_single()) {
1301 $post_id = $this->get_post_id($post_id);
1302
1303 $video = $this->get_video( $post_id );
1304 if ( $video ) {
1305 return $video;
1306 }
1307 }
1308 return false;
1309
1310 }
1311
1312 /**
1313 * @param int $start
1314 * @param int $limit
1315 * @param string $search_term
1316 * @param int $course_id
1317 *
1318 * @return array|null|object
1319 *
1320 *
1321 * Get the enrolled students for all courses.
1322 *
1323 * Pass course id in 4th parameter to get students course wise.
1324 *
1325 * @since v.1.0.0
1326 */
1327 public function get_students($start = 0, $limit = 10, $search_term = ''){
1328 $meta_key = '_is_tutor_student';
1329
1330 global $wpdb;
1331
1332 if ($search_term){
1333 $search_term = " AND ( {$wpdb->users}.display_name LIKE '%{$search_term}%' OR {$wpdb->users}.user_email LIKE '%{$search_term}%' ) ";
1334 }
1335
1336 $students = $wpdb->get_results("SELECT SQL_CALC_FOUND_ROWS {$wpdb->users}.* FROM {$wpdb->users}
1337 INNER JOIN {$wpdb->usermeta}
1338 ON ( {$wpdb->users}.ID = {$wpdb->usermeta}.user_id )
1339 WHERE 1=1 AND ( {$wpdb->usermeta}.meta_key = '{$meta_key}' ) {$search_term}
1340 ORDER BY {$wpdb->usermeta}.meta_value DESC
1341 LIMIT {$start}, {$limit} ");
1342
1343 return $students;
1344 }
1345
1346 /**
1347 * @return int
1348 *
1349 * @since v.1.0.0
1350 *
1351 * get the total students
1352 * pass course id to get course wise total students
1353 *
1354 * @since v.1.0.0
1355 */
1356 public function get_total_students($search_term = ''){
1357 $meta_key = '_is_tutor_student';
1358
1359 global $wpdb;
1360
1361 if ($search_term){
1362 $search_term = " AND ( {$wpdb->users}.display_name LIKE '%{$search_term}%' OR {$wpdb->users}.user_email LIKE '%{$search_term}%' ) ";
1363 }
1364
1365 $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 ");
1366
1367 return (int) $count;
1368 }
1369
1370 /**
1371 * @param int $user_id
1372 *
1373 * @return array
1374 *
1375 * Get complete courses ids by user
1376 *
1377 * @since v.1.0.0
1378 */
1379 public function get_completed_courses_ids_by_user($user_id = 0){
1380 global $wpdb;
1381
1382 $user_id = $this->get_user_id($user_id);
1383
1384 $course_ids = (array) $wpdb->get_col("SELECT comment_post_ID as course_id
1385 from {$wpdb->comments}
1386 WHERE comment_agent = 'TutorLMSPlugin'
1387 AND comment_type = 'course_completed'
1388 AND user_id = {$user_id} ;");
1389
1390 return $course_ids;
1391 }
1392
1393 /**
1394 * @param int $user_id
1395 *
1396 * @return bool|\WP_Query
1397 *
1398 * Return courses by user_id
1399 *
1400 * @since v.1.0.0
1401 */
1402 public function get_courses_by_user($user_id = 0){
1403 $user_id = $this->get_user_id($user_id);
1404 $course_ids = $this->get_completed_courses_ids_by_user($user_id);
1405
1406 if (count($course_ids)){
1407 $course_post_type = tutor()->course_post_type;
1408 $course_args = array(
1409 'post_type' => $course_post_type,
1410 'post_status' => 'publish',
1411 'post__in' => $course_ids,
1412 );
1413
1414 return new \WP_Query($course_args);
1415 }
1416
1417 return false;
1418 }
1419
1420 /**
1421 * @param int $user_id
1422 *
1423 * @return bool|\WP_Query
1424 *
1425 * Get the active course by user
1426 *
1427 * @since v.1.0.0
1428 */
1429
1430 public function get_active_courses_by_user($user_id = 0){
1431 $user_id = $this->get_user_id($user_id);
1432
1433 $course_ids = $this->get_completed_courses_ids_by_user($user_id);
1434 $enrolled_course_ids = $this->get_enrolled_courses_ids_by_user($user_id);
1435 $active_courses = array_diff($enrolled_course_ids, $course_ids);
1436
1437 if (count($active_courses)){
1438 $course_post_type = tutor()->course_post_type;
1439 $course_args = array(
1440 'post_type' => $course_post_type,
1441 'post_status' => 'publish',
1442 'post__in' => $active_courses,
1443 );
1444
1445 return new \WP_Query($course_args);
1446 }
1447
1448 return false;
1449 }
1450
1451 /**
1452 * @param int $user_id
1453 *
1454 * @return array
1455 *
1456 * Get enrolled course ids by a user
1457 *
1458 * @since v.1.0.0
1459 */
1460
1461 public function get_enrolled_courses_ids_by_user($user_id = 0){
1462 global $wpdb;
1463 $user_id = $this->get_user_id($user_id);
1464 $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'; ");
1465
1466 return $course_ids;
1467 }
1468
1469 /**
1470 * @param int $course_id
1471 *
1472 * @return int
1473 *
1474 * Get the total enrolled users at course
1475 */
1476 public function count_enrolled_users_by_course($course_id = 0){
1477 global $wpdb;
1478 $course_id = $this->get_post_id($course_id);
1479
1480 $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'; ");
1481
1482 return (int) $course_ids;
1483 }
1484
1485 /**
1486 * @param int $user_id
1487 *
1488 * @return bool|\WP_Query
1489 *
1490 * Get the enrolled courses by user
1491 */
1492 public function get_enrolled_courses_by_user($user_id = 0){
1493 global $wpdb;
1494
1495 $user_id = $this->get_user_id($user_id);
1496 $course_ids = $this->get_enrolled_courses_ids_by_user($user_id);
1497
1498 if (count($course_ids)){
1499 $course_post_type = tutor()->course_post_type;
1500 $course_args = array(
1501 'post_type' => $course_post_type,
1502 'post_status' => 'publish',
1503 'post__in' => $course_ids,
1504 );
1505 return new \WP_Query($course_args);
1506 }
1507 return false;
1508 }
1509
1510
1511 /**
1512 * @param int $post_id
1513 *
1514 * @return string
1515 *
1516 * Get the video streaming URL by post/lesson/course ID
1517 */
1518 public function get_video_stream_url($post_id = 0){
1519 $post_id = $this->get_post_id($post_id);
1520 $post = get_post($post_id);
1521
1522 if ($post->post_type === tutor()->lesson_post_type ){
1523 $video_url = trailingslashit(home_url()).'video-url/'.$post->post_name;
1524 }else{
1525 $video_info = tutor_utils()->get_video_info($post_id);
1526 $video_url = $video_info->url;
1527 }
1528
1529 return $video_url;
1530 }
1531
1532 /**
1533 * @param int $lesson_id
1534 * @param int $user_id
1535 *
1536 * @return array|bool|mixed
1537 *
1538 * Get student lesson reading current info
1539 *
1540 * @since v.1.0.0
1541 */
1542 public function get_lesson_reading_info_full($lesson_id = 0, $user_id = 0){
1543 $lesson_id = $this->get_post_id($lesson_id);
1544 $user_id = $this->get_user_id($user_id);
1545
1546 $lesson_info = (array) maybe_unserialize(get_user_meta($user_id, '_lesson_reading_info', true));
1547 return $this->avalue_dot($lesson_id, $lesson_info);
1548 }
1549
1550 /**
1551 * @param int $post_id
1552 *
1553 * @return bool|false|int
1554 *
1555 * Get current post id or given post id
1556 *
1557 * @since v.1.0.0
1558 */
1559 public function get_post_id($post_id = 0){
1560 if ( ! $post_id){
1561 $post_id = get_the_ID();
1562 if ( ! $post_id){
1563 return false;
1564 }
1565 }
1566
1567 return $post_id;
1568 }
1569
1570 /**
1571 * @param int $user_id
1572 *
1573 * @return bool|int
1574 *
1575 * Get current user or given user ID
1576 *
1577 * @since v.1.0.0
1578 */
1579 public function get_user_id($user_id = 0){
1580 if ( ! $user_id){
1581 $user_id = get_current_user_id();
1582 if ( ! $user_id){
1583 return false;
1584 }
1585 }
1586
1587 return $user_id;
1588 }
1589
1590 /**
1591 * @param int $lesson_id
1592 * @param int $user_id
1593 * @param string $key
1594 *
1595 * @return array|bool|mixed
1596 *
1597 * Get lesson reading info by key
1598 *
1599 * @since v.1.0.0
1600 */
1601
1602 public function get_lesson_reading_info($lesson_id = 0, $user_id = 0, $key = ''){
1603 $lesson_id = $this->get_post_id($lesson_id);
1604 $user_id = $this->get_user_id($user_id);
1605
1606 $lesson_info = $this->get_lesson_reading_info_full($lesson_id, $user_id);
1607
1608 return $this->avalue_dot($key, $lesson_info);
1609 }
1610
1611 /**
1612 * @param int $lesson_id
1613 * @param int $user_id
1614 * @param array $data
1615 *
1616 * @return bool
1617 *
1618 * Update student lesson reading info
1619 *
1620 * @since v.1.0.0
1621 */
1622 public function update_lesson_reading_info($lesson_id = 0, $user_id = 0, $key = '', $value = ''){
1623 $lesson_id = $this->get_post_id($lesson_id);
1624 $user_id = $this->get_user_id($user_id);
1625
1626 if ($key && $value){
1627 $lesson_info = (array) maybe_unserialize(get_user_meta($user_id, '_lesson_reading_info', true));
1628 $lesson_info[$lesson_id][$key] = $value;
1629 update_user_meta($user_id, '_lesson_reading_info', $lesson_info);
1630 }
1631 }
1632
1633 /**
1634 * @param string $url
1635 *
1636 * @return bool
1637 *
1638 * Get the Youtube Video ID from URL
1639 *
1640 * @since v.1.0.0
1641 */
1642 public function get_youtube_video_id($url = ''){
1643 if (!$url){
1644 return false;
1645 }
1646 preg_match('%(?:youtube(?:-nocookie)?\.com/(?:[^/]+/.+/|(?:v|e(?:mbed)?)/|.*[?&]v=)|youtu\.be/)([^"&?/ ]{11})%i', $url, $match);
1647
1648 if (isset($match[1])) {
1649 $youtube_id = $match[1];
1650 return $youtube_id;
1651 }
1652
1653 return false;
1654 }
1655
1656 /**
1657 * @param string $url
1658 *
1659 * @return bool
1660 *
1661 * Get the vimeo video id from URL
1662 *
1663 * @since v.1.0.0
1664 */
1665 public function get_vimeo_video_id($url = ''){
1666 if (preg_match('%^https?:\/\/(?:www\.|player\.)?vimeo.com\/(?:channels\/(?:\w+\/)?|groups\/([^\/]*)\/videos\/|album\/(\d+)\/video\/|video\/|)(\d+)(?:$|\/|\?)(?:[?]?.*)$%im', $url, $match)) {
1667 if (isset($match[3])){
1668 return $match[3];
1669 }
1670 }
1671 return false;
1672 }
1673
1674 /**
1675 * @param int $post_id
1676 *
1677 * Mark lesson complete
1678 *
1679 * @since v.1.0.0
1680 */
1681 public function mark_lesson_complete($post_id = 0, $user_id = 0){
1682 $post_id = $this->get_post_id($post_id);
1683 $user_id = $this->get_user_id($user_id);
1684 update_user_meta($user_id, '_tutor_completed_lesson_id_'.$post_id, time());
1685 }
1686
1687 /**
1688 * Saving enroll information to posts table
1689 * post_author = enrolled_student_id (wp_users id)
1690 * post_parent = enrolled course id
1691 *
1692 * @type: call when need
1693 * @return bool;
1694 *
1695 * @since v.1.0.0
1696 */
1697 public function do_enroll($course_id = 0, $order_id = 0){
1698 if ( ! $course_id){
1699 return false;
1700 }
1701
1702 do_action('tutor_before_enroll', $course_id);
1703 $user_id = get_current_user_id();
1704 $title = __('Course Enrolled', 'tutor')." &ndash; ".date_i18n(get_option('date_format')) .' @ '.date_i18n(get_option('time_format') ) ;
1705
1706 $enrolment_status = 'completed';
1707
1708 if ($this->is_course_purchasable($course_id)) {
1709 /**
1710 * We need to verify this enrollment, we will change the status later after payment confirmation
1711 */
1712 $enrolment_status = 'pending';
1713 }
1714
1715 $enroll_data = apply_filters('tutor_enroll_data',
1716 array(
1717 'post_type' => 'tutor_enrolled',
1718 'post_title' => $title,
1719 'post_status' => $enrolment_status,
1720 'post_author' => $user_id,
1721 'post_parent' => $course_id,
1722 )
1723 );
1724
1725 // Insert the post into the database
1726 $isEnrolled = wp_insert_post( $enroll_data );
1727 if ($isEnrolled) {
1728 do_action('tutor_after_enroll', $course_id, $isEnrolled);
1729
1730 //Mark Current User as Students with user meta data
1731 update_user_meta( $user_id, '_is_tutor_student', time() );
1732
1733 if ($order_id) {
1734 //Mark order for course and user
1735 $product_id = $this->get_course_product_id($course_id);
1736 update_post_meta( $isEnrolled, '_tutor_enrolled_by_order_id', $order_id );
1737 update_post_meta( $isEnrolled, '_tutor_enrolled_by_product_id', $product_id );
1738 update_post_meta( $order_id, '_is_tutor_order_for_course', time() );
1739 update_post_meta( $order_id, '_tutor_order_for_course_id_'.$course_id, $isEnrolled );
1740 }
1741 return true;
1742 }
1743
1744 return false;
1745 }
1746
1747 /**
1748 * @param $order_id
1749 *
1750 * Complete course enrollment and do some task
1751 *
1752 * @since v.1.0.0
1753 */
1754 public function complete_course_enroll($order_id){
1755 if ( ! tutor_utils()->is_tutor_order($order_id)){
1756 return;
1757 }
1758
1759 global $wpdb;
1760
1761 $enrolled_ids_with_course = $this->get_course_enrolled_ids_by_order_id($order_id);
1762 if ($enrolled_ids_with_course){
1763 $enrolled_ids = wp_list_pluck($enrolled_ids_with_course, 'enrolled_id');
1764
1765 if (is_array($enrolled_ids) && count($enrolled_ids)){
1766 foreach ($enrolled_ids as $enrolled_id){
1767 $wpdb->update( $wpdb->posts, array( 'post_status' => 'completed' ), array( 'ID' => $enrolled_id ) );
1768 }
1769 }
1770 }
1771 }
1772
1773 /**
1774 * @param $order_id
1775 *
1776 * @return array|bool
1777 *
1778 * @since v.1.0.0
1779 */
1780 public function get_course_enrolled_ids_by_order_id($order_id){
1781 global $wpdb;
1782 //Getting all of courses ids within this order
1783
1784 $courses_ids = $wpdb->get_results("SELECT * FROM {$wpdb->postmeta} WHERE post_id = {$order_id} AND meta_key LIKE '_tutor_order_for_course_id_%' ");
1785
1786 if (is_array($courses_ids) && count($courses_ids)){
1787 $course_enrolled_by_order = array();
1788 foreach ($courses_ids as $courses_id){
1789 $course_id = str_replace('_tutor_order_for_course_id_', '',$courses_id->meta_key);
1790 //array(order_id => array('course_id' => $course_id, 'enrolled_id' => enrolled_id))
1791 $course_enrolled_by_order[$courses_id->post_id] = array('course_id' => $course_id, 'enrolled_id' => $courses_id->meta_value);
1792 }
1793 return $course_enrolled_by_order;
1794 }
1795 return false;
1796 }
1797
1798 /**
1799 * Get wc product in efficient query
1800 *
1801 * @since v.1.0.0
1802 */
1803
1804 /**
1805 * @return array|null|object
1806 *
1807 * WooCommerce specific utils
1808 */
1809 public function get_wc_products_db(){
1810 global $wpdb;
1811 $query = $wpdb->get_results("SELECT ID, post_title from {$wpdb->posts} WHERE post_status = 'publish' AND post_type = 'product' ");
1812
1813 return $query;
1814 }
1815
1816 /**
1817 * @param int $course_id
1818 *
1819 * @return int
1820 *
1821 * Get course productID
1822 *
1823 * @since v.1.0.0
1824 */
1825 public function get_course_product_id($course_id = 0){
1826 $course_id = $this->get_post_id($course_id);
1827 return (int) get_post_meta($course_id, '_tutor_course_product_id', true);
1828 }
1829
1830 /**
1831 * @param int $product_id
1832 *
1833 * @return array|null|object|void
1834 *
1835 * Get Product belongs with course
1836 *
1837 * @since v.1.0.0
1838 */
1839
1840 public function product_belongs_with_course($product_id = 0){
1841 global $wpdb;
1842
1843 $query = $wpdb->get_row("select * from {$wpdb->postmeta} WHERE meta_key='_tutor_course_product_id' AND meta_value = {$product_id} limit 1 ");
1844 return $query;
1845 }
1846
1847 /**
1848 * #End WooCommerce specific utils
1849 *
1850 * @since v.1.0.0
1851 */
1852
1853 public function get_enrolled_statuses(){
1854 return array (
1855 'pending',
1856 'processing',
1857 'on-hold',
1858 'completed',
1859 'cancelled',
1860 'refunded',
1861 'failed',
1862 );
1863 }
1864
1865 /**
1866 * @param $order_id
1867 *
1868 * @return mixed
1869 *
1870 * determine is this a tutor order
1871 *
1872 * @since v.1.0.0
1873 */
1874 public function is_tutor_order($order_id){
1875 return get_post_meta($order_id, '_is_tutor_order_for_course', true);
1876 }
1877
1878 /**
1879 * @return mixed
1880 *
1881 * Tutor Dashboard Pages
1882 *
1883 * @since v.1.0.0
1884 */
1885
1886 public function tutor_student_dashboard_pages(){
1887 $nav_items = array(
1888 'index' => __('Home', 'tutor'),
1889 'my-courses' => __('My Courses', 'tutor'),
1890 'active-courses' => __('Active Courses', 'tutor'),
1891 'completed-courses' => __('Completed Courses', 'tutor'),
1892 'wishlist' => __('WishList', 'tutor'),
1893 );
1894
1895 return apply_filters('tutor_dashboard/student/pages', $nav_items);
1896 }
1897
1898 /**
1899 * @param string $page_key
1900 * @param int $page_id
1901 *
1902 * @return string
1903 *
1904 * Get tutor dashboard page single URL
1905 *
1906 * @since v.1.0.0
1907 */
1908 public function get_tutor_dashboard_page_permalink($page_key = '', $page_id = 0){
1909 if ($page_key === 'index'){
1910 $page_key = '';
1911 }
1912 $page_id = $this->get_post_id($page_id);
1913 return trailingslashit(get_permalink($page_id)).$page_key;
1914 }
1915
1916 /**
1917 * @param string $input
1918 *
1919 * @return array|bool|mixed|string
1920 *
1921 * Get old input
1922 *
1923 * @since v.1.0.0
1924 */
1925 public function input_old($input = ''){
1926 $value = $this->avalue_dot($input, $_REQUEST);
1927 if ($value){
1928 return $value;
1929 }
1930 return '';
1931 }
1932
1933 /**
1934 * @param int $user_id
1935 *
1936 * @return mixed
1937 *
1938 * Determine if is instructor or not
1939 *
1940 * @since v.1.0.0
1941 */
1942 public function is_instructor($user_id = 0){
1943 $user_id = $this->get_user_id($user_id);
1944 return get_user_meta($user_id, '_is_tutor_instructor', true);
1945 }
1946
1947 /**
1948 * @param int $user_id
1949 * @param bool $status_name
1950 *
1951 * @return bool|mixed
1952 *
1953 * Instructor status
1954 *
1955 * @since v.1.0.0
1956 */
1957 public function instructor_status($user_id = 0, $status_name = true){
1958 $user_id = $this->get_user_id($user_id);
1959
1960 $instructor_status = apply_filters('tutor_instructor_statuses', array(
1961 'pending' => __('Pending', 'tutor'),
1962 'approved' => __('Approved', 'tutor'),
1963 'blocked' => __('Blocked', 'tutor'),
1964 ));
1965
1966 $status = get_user_meta($user_id, '_tutor_instructor_status', true);
1967
1968 if (isset($instructor_status[$status])){
1969 if ( ! $status_name){
1970 return $status;
1971 }
1972 return $instructor_status[$status];
1973 }
1974 return false;
1975 }
1976
1977 /**
1978 * @param string $search_term
1979 *
1980 * @return int
1981 *
1982 * Get total number of instructors
1983 *
1984 * @since v.1.0.0
1985 */
1986
1987 public function get_total_instructors($search_term = ''){
1988 $meta_key = '_is_tutor_instructor';
1989
1990 global $wpdb;
1991
1992 if ($search_term){
1993 $search_term = " AND ( {$wpdb->users}.display_name LIKE '%{$search_term}%' OR {$wpdb->users}.user_email LIKE '%{$search_term}%' ) ";
1994 }
1995
1996 $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 ");
1997
1998 return (int) $count;
1999 }
2000
2001 /**
2002 * @param int $start
2003 * @param int $limit
2004 * @param string $search_term
2005 *
2006 * @return array|null|object
2007 *
2008 * Get all instructors
2009 *
2010 * @since v.1.0.0
2011 */
2012 public function get_instructors($start = 0, $limit = 10, $search_term = ''){
2013 $meta_key = '_is_tutor_instructor';
2014 global $wpdb;
2015
2016 if ($search_term){
2017 $search_term = " AND ( {$wpdb->users}.display_name LIKE '%{$search_term}%' OR {$wpdb->users}.user_email LIKE '%{$search_term}%' ) ";
2018 }
2019
2020 $instructors = $wpdb->get_results("SELECT SQL_CALC_FOUND_ROWS {$wpdb->users}.* FROM {$wpdb->users}
2021 INNER JOIN {$wpdb->usermeta}
2022 ON ( {$wpdb->users}.ID = {$wpdb->usermeta}.user_id )
2023 WHERE 1=1 AND ( {$wpdb->usermeta}.meta_key = '{$meta_key}' ) {$search_term}
2024 ORDER BY {$wpdb->usermeta}.meta_value DESC
2025 LIMIT {$start}, {$limit} ");
2026
2027 return $instructors;
2028 }
2029
2030 /**
2031 * @param int $course_id
2032 *
2033 * @return array|bool|null|object
2034 *
2035 * Get all instructors by course
2036 *
2037 * @since v.1.0.0
2038 */
2039 public function get_instructors_by_course($course_id = 0){
2040 global $wpdb;
2041 $course_id = $this->get_post_id($course_id);
2042
2043 $instructors = $wpdb->get_results("select ID, display_name,
2044 get_course.meta_value as taught_course_id,
2045 tutor_job_title.meta_value as tutor_profile_job_title,
2046 tutor_bio.meta_value as tutor_profile_bio,
2047 tutor_photo.meta_value as tutor_profile_photo
2048 from {$wpdb->users}
2049 INNER JOIN {$wpdb->usermeta} get_course ON ID = get_course.user_id AND get_course.meta_key = '_tutor_instructor_course_id' AND get_course.meta_value = {$course_id}
2050 LEFT JOIN {$wpdb->usermeta} tutor_job_title ON ID = tutor_job_title.user_id AND tutor_job_title.meta_key = '_tutor_profile_job_title'
2051 LEFT JOIN {$wpdb->usermeta} tutor_bio ON ID = tutor_bio.user_id AND tutor_bio.meta_key = '_tutor_profile_bio'
2052 LEFT JOIN {$wpdb->usermeta} tutor_photo ON ID = tutor_photo.user_id AND tutor_photo.meta_key = '_tutor_profile_photo'
2053 ");
2054
2055 if (is_array($instructors) && count($instructors)){
2056 return $instructors;
2057 }
2058
2059 return false;
2060 }
2061
2062 /**
2063 * @param $instructor_id
2064 *
2065 * Get total Students by instructor
2066 * 1 enrollment = 1 student, so total enrolled for a equivalent total students (Tricks)
2067 *
2068 * @return int
2069 *
2070 * @since v.1.0.0
2071 */
2072
2073 public function get_total_students_by_instructor($instructor_id){
2074 global $wpdb;
2075
2076 $course_post_type = tutor()->course_post_type;
2077 $count = $wpdb->get_var("SELECT COUNT(courses.ID) from {$wpdb->posts} courses
2078
2079 INNER JOIN {$wpdb->posts} enrolled ON courses.ID = enrolled.post_parent AND enrolled.post_type = 'tutor_enrolled'
2080 WHERE courses.post_status = 'publish'
2081 AND courses.post_type = '{$course_post_type}'
2082 AND courses.post_author = {$instructor_id} ; ");
2083 return (int) $count;
2084 }
2085
2086 /**
2087 * @param float $input
2088 *
2089 * @return float|string
2090 *
2091 * Get rating format from value
2092 *
2093 * @since v.1.0.0
2094 */
2095 public function get_rating_value($input = 0.00){
2096
2097 if ( $input > 0){
2098 $input = number_format($input, 2);
2099 $int_value = (int) $input;
2100 $fraction = $input - $int_value;
2101
2102 if ($fraction == 0){
2103 $fraction = 0.00;
2104 }elseif($fraction > 0.5){
2105 $fraction = 1;
2106 }else{
2107 $fraction = 0.5;
2108 }
2109
2110 return number_format( ($int_value + $fraction), 2);
2111 }
2112 return 0.00;
2113 }
2114
2115 /**
2116 * @param float $current_rating
2117 * @param bool $echo
2118 *
2119 * @return string
2120 *
2121 * Generate star rating based in given rating value
2122 *
2123 * @since v.1.0.0
2124 */
2125 public function star_rating_generator($current_rating = 0.00, $echo = true){
2126 $output = '<div class="tutor-star-rating-group">';
2127
2128 for ($i = 1; $i <=5 ; $i++){
2129 $intRating = (int) $current_rating;
2130
2131 if ($intRating >= $i){
2132 $output.= '<i class="tutor-icon-star-full" data-rating-value="'.$i.'"></i>';
2133 } else{
2134 if ( ($current_rating - $i) == -0.5){
2135 $output.= '<i class="tutor-icon-star-half" data-rating-value="'.$i.'"></i>';
2136 }else{
2137 $output.= '<i class="tutor-icon-star-line" data-rating-value="'.$i.'"></i>';
2138 }
2139 }
2140 }
2141
2142 $output .= "<div class='tutor-rating-gen-input'><input type='hidden' name='tutor_rating_gen_input' value='{$current_rating}' /> </div>";
2143
2144 $output .= "</div>";
2145
2146 if ($echo){
2147 echo $output;
2148 }
2149 return $output;
2150 }
2151
2152 /**
2153 * @param null $name
2154 *
2155 * @return string
2156 *
2157 * Generate text to avatar
2158 *
2159 * @since v.1.0.0
2160 */
2161 public function get_tutor_avatar($user_id = null, $size = 'thumbnail'){
2162 global $wpdb;
2163
2164 if ( ! $user_id){
2165 return '';
2166 }
2167
2168 $user = $this->get_tutor_user($user_id);
2169 if ($user->tutor_profile_photo){
2170 return '<img src="'.wp_get_attachment_image_url($user->tutor_profile_photo, $size).'" class="tutor-image-avatar" alt="" /> ';
2171 }
2172
2173 $name = $user->display_name;
2174 $arr = explode(' ', trim($name));
2175
2176 if (count($arr) > 1){
2177 $first_char = substr($arr[0], 0, 1) ;
2178 $second_char = substr($arr[1], 0, 1) ;
2179 }else{
2180 $first_char = substr($arr[0], 0, 1) ;
2181 $second_char = substr($arr[0], 1, 1) ;
2182 }
2183
2184 $initial_avatar = strtoupper($first_char.$second_char);
2185
2186 $bg_color = '#'.substr(md5($initial_avatar), 0, 6);
2187 $initial_avatar = "<span class='tutor-text-avatar' style='background-color: {$bg_color}; color: #fff8e5'>{$initial_avatar}</span>";
2188
2189 return $initial_avatar;
2190 }
2191
2192 /**
2193 * @param $user_id
2194 *
2195 * @return array|null|object|void
2196 *
2197 * Get tutor user
2198 *
2199 * @since v.1.0.0
2200 */
2201
2202 public function get_tutor_user($user_id){
2203 global $wpdb;
2204
2205 $user = $wpdb->get_row("select ID, display_name,
2206 tutor_job_title.meta_value as tutor_profile_job_title,
2207 tutor_bio.meta_value as tutor_profile_bio,
2208 tutor_photo.meta_value as tutor_profile_photo
2209
2210 from {$wpdb->users}
2211 LEFT JOIN {$wpdb->usermeta} tutor_job_title ON ID = tutor_job_title.user_id AND tutor_job_title.meta_key = '_tutor_profile_job_title'
2212 LEFT JOIN {$wpdb->usermeta} tutor_bio ON ID = tutor_bio.user_id AND tutor_bio.meta_key = '_tutor_profile_bio'
2213 LEFT JOIN {$wpdb->usermeta} tutor_photo ON ID = tutor_photo.user_id AND tutor_photo.meta_key = '_tutor_profile_photo'
2214
2215 WHERE ID = {$user_id} ");
2216 return $user;
2217 }
2218
2219 /**
2220 * @param int $course_id
2221 * @param int $offset
2222 * @param int $limit
2223 *
2224 * @return array|null|object
2225 *
2226 * get course reviews
2227 *
2228 * @since v.1.0.0
2229 */
2230 public function get_course_reviews($course_id = 0, $offset = 0, $limit = 150){
2231 $course_id = $this->get_post_id($course_id);
2232 global $wpdb;
2233
2234 $reviews = $wpdb->get_results("select {$wpdb->comments}.comment_ID,
2235 {$wpdb->comments}.comment_post_ID,
2236 {$wpdb->comments}.comment_author,
2237 {$wpdb->comments}.comment_author_email,
2238 {$wpdb->comments}.comment_date,
2239 {$wpdb->comments}.comment_content,
2240 {$wpdb->comments}.user_id,
2241 {$wpdb->commentmeta}.meta_value as rating,
2242 {$wpdb->users}.display_name
2243
2244 from {$wpdb->comments}
2245 INNER JOIN {$wpdb->commentmeta}
2246 ON {$wpdb->comments}.comment_ID = {$wpdb->commentmeta}.comment_id
2247 INNER JOIN {$wpdb->users}
2248 ON {$wpdb->comments}.user_id = {$wpdb->users}.ID
2249 WHERE {$wpdb->comments}.comment_post_ID = {$course_id}
2250 AND comment_type = 'tutor_course_rating' AND meta_key = 'tutor_rating' ORDER BY comment_ID DESC LIMIT {$offset},{$limit} ;"
2251 );
2252
2253 return $reviews;
2254 }
2255
2256 /**
2257 * @param int $course_id
2258 *
2259 * @return object
2260 *
2261 * Get course rating
2262 *
2263 * @since v.1.0.0
2264 */
2265 public function get_course_rating($course_id = 0){
2266 $course_id = $this->get_post_id($course_id);
2267
2268 $ratings = array(
2269 'rating_count' => 0,
2270 'rating_sum' => 0,
2271 'rating_avg' => 0.00,
2272 'count_by_value' => array(5 => 0, 4 => 0, 3 => 0, 2 => 0, 1 => 0)
2273 );
2274
2275 global $wpdb;
2276
2277 $rating = $wpdb->get_row("select COUNT(meta_value) as rating_count, SUM(meta_value) as rating_sum
2278 from {$wpdb->comments}
2279 INNER JOIN {$wpdb->commentmeta}
2280 ON {$wpdb->comments}.comment_ID = {$wpdb->commentmeta}.comment_id
2281 WHERE {$wpdb->comments}.comment_post_ID = {$course_id}
2282 AND {$wpdb->comments}.comment_type = 'tutor_course_rating'
2283 AND meta_key = 'tutor_rating' ;"
2284 );
2285
2286 if ($rating->rating_count){
2287 $avg_rating = number_format(($rating->rating_sum / $rating->rating_count), 2);
2288
2289 /**
2290 * Get individual Rating by integer
2291 */
2292 $five_stars_count = $wpdb->get_var("select COUNT(meta_value) as rating_count
2293 from {$wpdb->comments}
2294 INNER JOIN {$wpdb->commentmeta} ON {$wpdb->comments}.comment_ID = {$wpdb->commentmeta}.comment_id
2295 WHERE {$wpdb->comments}.comment_post_ID = {$course_id}
2296 AND {$wpdb->comments}.comment_type = 'tutor_course_rating'
2297 AND meta_key = 'tutor_rating' AND meta_value = 5 ;"
2298 );
2299 $four_stars_count = $wpdb->get_var("select COUNT(meta_value) as rating_count
2300 from {$wpdb->comments}
2301 INNER JOIN {$wpdb->commentmeta} ON {$wpdb->comments}.comment_ID = {$wpdb->commentmeta}.comment_id
2302 WHERE {$wpdb->comments}.comment_post_ID = {$course_id}
2303 AND {$wpdb->comments}.comment_type = 'tutor_course_rating'
2304 AND meta_key = 'tutor_rating' AND meta_value = 4 ;"
2305 );
2306 $three_stars_count = $wpdb->get_var("select COUNT(meta_value) as rating_count
2307 from {$wpdb->comments}
2308 INNER JOIN {$wpdb->commentmeta} ON {$wpdb->comments}.comment_ID = {$wpdb->commentmeta}.comment_id
2309 WHERE {$wpdb->comments}.comment_post_ID = {$course_id}
2310 AND {$wpdb->comments}.comment_type = 'tutor_course_rating'
2311 AND meta_key = 'tutor_rating' AND meta_value = 3 ;"
2312 );
2313 $two_stars_count = $wpdb->get_var("select COUNT(meta_value) as rating_count
2314 from {$wpdb->comments}
2315 INNER JOIN {$wpdb->commentmeta} ON {$wpdb->comments}.comment_ID = {$wpdb->commentmeta}.comment_id
2316 WHERE {$wpdb->comments}.comment_post_ID = {$course_id}
2317 AND {$wpdb->comments}.comment_type = 'tutor_course_rating'
2318 AND meta_key = 'tutor_rating' AND meta_value = 2 ;"
2319 );
2320 $one_stars_count = $wpdb->get_var("select COUNT(meta_value) as rating_count
2321 from {$wpdb->comments}
2322 INNER JOIN {$wpdb->commentmeta} ON {$wpdb->comments}.comment_ID = {$wpdb->commentmeta}.comment_id
2323 WHERE {$wpdb->comments}.comment_post_ID = {$course_id}
2324 AND {$wpdb->comments}.comment_type = 'tutor_course_rating'
2325 AND meta_key = 'tutor_rating' AND meta_value = 1 ;"
2326 );
2327
2328 $ratings = array(
2329 'rating_count' => $rating->rating_count,
2330 'rating_sum' => $rating->rating_sum,
2331 'rating_avg' => $avg_rating,
2332 'count_by_value' => array(5 => $five_stars_count, 4 => $four_stars_count, 3 => $three_stars_count, 2 => $two_stars_count, 1 => $one_stars_count)
2333 );
2334
2335 }
2336
2337 return (object) $ratings;
2338 }
2339
2340 /**
2341 * @param int $user_id
2342 * @param int $offset
2343 * @param int $limit
2344 *
2345 * @return array|null|object
2346 *
2347 * Get reviews by a user
2348 *
2349 * @since v.1.0.0
2350 */
2351 public function get_reviews_by_user($user_id = 0, $offset = 0, $limit = 150){
2352 $user_id = $this->get_user_id($user_id);
2353 global $wpdb;
2354
2355 $reviews = $wpdb->get_results("select {$wpdb->comments}.comment_ID,
2356 {$wpdb->comments}.comment_post_ID,
2357 {$wpdb->comments}.comment_author,
2358 {$wpdb->comments}.comment_author_email,
2359 {$wpdb->comments}.comment_date,
2360 {$wpdb->comments}.comment_content,
2361 {$wpdb->comments}.user_id,
2362 {$wpdb->commentmeta}.meta_value as rating,
2363 {$wpdb->users}.display_name
2364
2365 from {$wpdb->comments}
2366 INNER JOIN {$wpdb->commentmeta}
2367 ON {$wpdb->comments}.comment_ID = {$wpdb->commentmeta}.comment_id
2368 INNER JOIN {$wpdb->users}
2369 ON {$wpdb->comments}.user_id = {$wpdb->users}.ID
2370 WHERE {$wpdb->comments}.user_id = {$user_id}
2371 AND comment_type = 'tutor_course_rating' AND meta_key = 'tutor_rating' ORDER BY comment_ID DESC LIMIT {$offset},{$limit} ;"
2372 );
2373
2374 return $reviews;
2375 }
2376
2377 /**
2378 * @param $instructor_id
2379 *
2380 * @return object
2381 *
2382 * Get instructors rating
2383 *
2384 * @since v.1.0.0
2385 */
2386 public function get_instructor_ratings($instructor_id){
2387 global $wpdb;
2388
2389 $ratings = array(
2390 'rating_count' => 0,
2391 'rating_sum' => 0,
2392 'rating_avg' => 0.00,
2393 );
2394
2395 $rating = $wpdb->get_row("SELECT COUNT(rating.meta_value) as rating_count, SUM(rating.meta_value) as rating_sum
2396 FROM {$wpdb->usermeta} courses
2397 INNER JOIN {$wpdb->comments} reviews ON courses.meta_value = reviews.comment_post_ID AND reviews.comment_type = 'tutor_course_rating'
2398 INNER JOIN {$wpdb->commentmeta} rating ON reviews.comment_ID = rating.comment_id AND rating.meta_key = 'tutor_rating'
2399 WHERE courses.user_id = {$instructor_id} AND courses.meta_key = '_tutor_instructor_course_id'");
2400
2401 if ($rating->rating_count){
2402 $avg_rating = number_format(($rating->rating_sum / $rating->rating_count), 2);
2403
2404 $ratings = array(
2405 'rating_count' => $rating->rating_count,
2406 'rating_sum' => $rating->rating_sum,
2407 'rating_avg' => $avg_rating,
2408 );
2409 }
2410
2411 return (object) $ratings;
2412 }
2413
2414 /**
2415 * @param int $course_id
2416 * @param int $user_id
2417 *
2418 * @return object
2419 *
2420 * Get course rating by user
2421 *
2422 * @since v.1.0.0
2423 */
2424 public function get_course_rating_by_user($course_id = 0, $user_id = 0){
2425 $course_id = $this->get_post_id($course_id);
2426 $user_id = $this->get_user_id($user_id);
2427
2428 $ratings = array(
2429 'rating' => 0,
2430 'review' => '',
2431 );
2432
2433 global $wpdb;
2434
2435 $rating = $wpdb->get_row("select meta_value as rating, comment_content as review from {$wpdb->comments}
2436 INNER JOIN {$wpdb->commentmeta}
2437 ON {$wpdb->comments}.comment_ID = {$wpdb->commentmeta}.comment_id
2438 WHERE {$wpdb->comments}.comment_post_ID = {$course_id} AND user_id = {$user_id}
2439 AND meta_key = 'tutor_rating' ;"
2440 );
2441
2442 if ($rating){
2443 $rating_format = number_format($rating->rating, 2);
2444
2445 $ratings = array(
2446 'rating' => $rating_format,
2447 'review' => $rating->review,
2448 );
2449 }
2450 return (object) $ratings;
2451 }
2452
2453 /**
2454 * @param int $user_id
2455 *
2456 * @return null|string
2457 *
2458 * @since v.1.0.0
2459 */
2460 public function count_reviews_wrote_by_user($user_id = 0){
2461 global $wpdb;
2462 $user_id = $this->get_user_id($user_id);
2463
2464 $count_reviews = $wpdb->get_var("SELECT COUNT(comment_ID) from {$wpdb->comments} WHERE user_id = {$user_id} AND comment_type = 'tutor_course_rating' ");
2465 return $count_reviews;
2466 }
2467
2468 /**
2469 * @param $size
2470 *
2471 * @return bool|int|string
2472 *
2473 * This function transforms the php.ini notation for numbers (like '2M') to an integer.
2474 *
2475 * @since v.1.0.0
2476 */
2477
2478 function let_to_num( $size ) {
2479 $l = substr( $size, -1 );
2480 $ret = substr( $size, 0, -1 );
2481 $byte = 1024;
2482
2483 switch ( strtoupper( $l ) ) {
2484 case 'P':
2485 $ret *= 1024;
2486 // No break.
2487 case 'T':
2488 $ret *= 1024;
2489 // No break.
2490 case 'G':
2491 $ret *= 1024;
2492 // No break.
2493 case 'M':
2494 $ret *= 1024;
2495 // No break.
2496 case 'K':
2497 $ret *= 1024;
2498 // No break.
2499 }
2500 return $ret;
2501 }
2502
2503 /**
2504 * @return array
2505 *
2506 * Get Database version
2507 *
2508 * @since v.1.0.0
2509 */
2510 function get_db_version() {
2511 global $wpdb;
2512
2513 if ( empty( $wpdb->is_mysql ) ) {
2514 return array(
2515 'string' => '',
2516 'number' => '',
2517 );
2518 }
2519
2520 if ( $wpdb->use_mysqli ) {
2521 $server_info = mysqli_get_server_info( $wpdb->dbh ); // @codingStandardsIgnoreLine.
2522 } else {
2523 $server_info = mysql_get_server_info( $wpdb->dbh ); // @codingStandardsIgnoreLine.
2524 }
2525
2526 return array(
2527 'string' => $server_info,
2528 'number' => preg_replace( '/([^\d.]+).*/', '', $server_info ),
2529 );
2530 }
2531
2532 public function help_tip($tip = ''){
2533 return '<span class="tutor-help-tip" data-tip="' . $tip . '"></span>';
2534 }
2535
2536 /**
2537 * @param int $course_id
2538 * @param int $user_id
2539 * @param int $offset
2540 * @param int $limit
2541 *
2542 * @return array|null|object
2543 *
2544 * Get top question
2545 *
2546 * @since v.1.0.0
2547 */
2548 public function get_top_question($course_id = 0, $user_id = 0, $offset = 0, $limit = 20){
2549 $course_id = $this->get_post_id($course_id);
2550 $user_id = $this->get_user_id($user_id);
2551
2552 global $wpdb;
2553
2554 $questions = $wpdb->get_results("select {$wpdb->comments}.comment_ID,
2555 {$wpdb->comments}.comment_post_ID,
2556 {$wpdb->comments}.comment_author,
2557 {$wpdb->comments}.comment_date,
2558 {$wpdb->comments}.comment_content,
2559 {$wpdb->comments}.user_id,
2560 {$wpdb->commentmeta}.meta_value as question_title,
2561 {$wpdb->users}.display_name
2562
2563 from {$wpdb->comments}
2564 INNER JOIN {$wpdb->commentmeta}
2565 ON {$wpdb->comments}.comment_ID = {$wpdb->commentmeta}.comment_id
2566 INNER JOIN {$wpdb->users}
2567 ON {$wpdb->comments}.user_id = {$wpdb->users}.ID
2568 WHERE {$wpdb->comments}.comment_post_ID = {$course_id}
2569 AND {$wpdb->comments}.user_id = {$user_id}
2570 AND {$wpdb->comments}.comment_type = 'tutor_q_and_a'
2571 AND meta_key = 'tutor_question_title' ORDER BY comment_ID DESC LIMIT {$offset},{$limit} ;"
2572 );
2573
2574 return $questions;
2575 }
2576
2577 /**
2578 * @param string $search_term
2579 *
2580 * @return int
2581 *
2582 * Get total number of Q&A questions
2583 *
2584 * @since v.1.0.0
2585 */
2586 public function get_total_qa_question($search_term = ''){
2587 global $wpdb;
2588
2589 if ($search_term){
2590 $search_term = " AND {$wpdb->commentmeta}.meta_value LIKE '%{$search_term}%' ";
2591 }
2592
2593 $user_id = get_current_user_id();
2594 $course_type = tutor()->course_post_type;
2595
2596 $in_question_id_query = '';
2597 /**
2598 * Get only assinged courses questions if current user is a
2599 */
2600 if ( ! current_user_can('administrator') && current_user_can(tutor()->instructor_role)) {
2601 $get_course_ids = $wpdb->get_col( "SELECT ID FROM {$wpdb->posts} WHERE post_author = {$user_id} AND post_type = '{$course_type}' AND post_status = 'publish' " );
2602 $get_assigned_courses_ids = $wpdb->get_col( "SELECT meta_value from {$wpdb->usermeta} WHERE meta_key = '_tutor_instructor_course_id' AND user_id = {$user_id} " );
2603 $my_course_ids = array_unique( array_merge( $get_course_ids, $get_assigned_courses_ids ) );
2604
2605 if ( $this->count( $my_course_ids ) ) {
2606 $implode_ids = implode( ',', $my_course_ids );
2607 $in_question_id_query = " AND {$wpdb->comments}.comment_post_ID IN($implode_ids) ";
2608 }
2609 }
2610
2611 $count = $wpdb->get_var("SELECT COUNT({$wpdb->comments}.comment_ID) FROM {$wpdb->comments}
2612 INNER JOIN {$wpdb->commentmeta}
2613 ON {$wpdb->comments}.comment_ID = {$wpdb->commentmeta}.comment_id
2614 WHERE comment_type = 'tutor_q_and_a' AND comment_parent = 0 {$in_question_id_query} {$search_term} ");
2615
2616 return (int) $count;
2617 }
2618
2619 /**
2620 * @param int $start
2621 * @param int $limit
2622 * @param string $search_term
2623 *
2624 * @return array|null|object
2625 *
2626 *
2627 * Get question and answer query
2628 *
2629 * @since v.1.0.0
2630 */
2631 public function get_qa_questions($start = 0, $limit = 10, $search_term = '') {
2632 global $wpdb;
2633
2634 if ($search_term){
2635 $search_term = " AND {$wpdb->commentmeta}.meta_value LIKE '%{$search_term}%' ";
2636 }
2637
2638 $user_id = get_current_user_id();
2639 $course_type = tutor()->course_post_type;
2640
2641 $in_question_id_query = '';
2642 /**
2643 * Get only assinged courses questions if current user is a
2644 */
2645 if ( ! current_user_can('administrator') && current_user_can(tutor()->instructor_role)) {
2646 $get_course_ids = $wpdb->get_col( "SELECT ID FROM {$wpdb->posts} WHERE post_author = {$user_id} AND post_type = '{$course_type}' AND post_status = 'publish' " );
2647 $get_assigned_courses_ids = $wpdb->get_col( "SELECT meta_value from {$wpdb->usermeta} WHERE meta_key = '_tutor_instructor_course_id' AND user_id = {$user_id} " );
2648 $my_course_ids = array_unique( array_merge( $get_course_ids, $get_assigned_courses_ids ) );
2649
2650 if ( $this->count( $my_course_ids ) ) {
2651 $implode_ids = implode( ',', $my_course_ids );
2652 $in_question_id_query = " AND {$wpdb->comments}.comment_post_ID IN($implode_ids) ";
2653 }
2654 }
2655
2656 $query = $wpdb->get_results("SELECT
2657 {$wpdb->comments}.comment_ID,
2658 {$wpdb->comments}.comment_post_ID,
2659 {$wpdb->comments}.comment_author,
2660 {$wpdb->comments}.comment_date,
2661 {$wpdb->comments}.comment_content,
2662 {$wpdb->comments}.user_id,
2663 {$wpdb->commentmeta}.meta_value as question_title,
2664 {$wpdb->users}.display_name,
2665 {$wpdb->posts}.post_title,
2666
2667 (SELECT COUNT(answers_t.comment_ID) FROM {$wpdb->comments} answers_t
2668 WHERE answers_t.comment_parent = {$wpdb->comments}.comment_ID ) as answer_count
2669
2670 FROM {$wpdb->comments}
2671
2672 INNER JOIN {$wpdb->commentmeta}
2673 ON {$wpdb->comments}.comment_ID = {$wpdb->commentmeta}.comment_id
2674
2675 INNER JOIN {$wpdb->posts}
2676 ON {$wpdb->comments}.comment_post_ID = {$wpdb->posts}.ID
2677
2678 INNER JOIN {$wpdb->users}
2679 ON {$wpdb->comments}.user_id = {$wpdb->users}.ID
2680
2681 WHERE {$wpdb->comments}.comment_type = 'tutor_q_and_a' AND {$wpdb->comments}.comment_parent = 0 {$search_term}
2682 {$in_question_id_query}
2683 ORDER BY {$wpdb->comments}.comment_ID DESC
2684 LIMIT {$start},{$limit}; ");
2685
2686 return $query;
2687 }
2688
2689 /**
2690 * @param $question_id
2691 *
2692 * @return array|null|object|void
2693 *
2694 * Get question for Q&A
2695 *
2696 * @since v.1.0.0
2697 */
2698 public function get_qa_question($question_id){
2699 global $wpdb;
2700 $query = $wpdb->get_row("SELECT
2701 {$wpdb->comments}.comment_ID,
2702 {$wpdb->comments}.comment_post_ID,
2703 {$wpdb->comments}.comment_author,
2704 {$wpdb->comments}.comment_date,
2705 {$wpdb->comments}.comment_content,
2706 {$wpdb->comments}.user_id,
2707 {$wpdb->commentmeta}.meta_value as question_title,
2708 {$wpdb->users}.display_name,
2709 {$wpdb->posts}.post_title
2710
2711 FROM {$wpdb->comments}
2712 INNER JOIN {$wpdb->commentmeta}
2713 ON {$wpdb->comments}.comment_ID = {$wpdb->commentmeta}.comment_id
2714
2715 INNER JOIN {$wpdb->posts}
2716 ON {$wpdb->comments}.comment_post_ID = {$wpdb->posts}.ID
2717
2718 INNER JOIN {$wpdb->users}
2719 ON {$wpdb->comments}.user_id = {$wpdb->users}.ID
2720 WHERE comment_type = 'tutor_q_and_a' AND {$wpdb->comments}.comment_ID = {$question_id}");
2721
2722 return $query;
2723 }
2724
2725 /**
2726 * @param $question_id
2727 *
2728 * @return array|null|object
2729 *
2730 * Get question and asnwer by question
2731 */
2732 public function get_qa_answer_by_question($question_id){
2733 global $wpdb;
2734 $query = $wpdb->get_results("SELECT
2735 {$wpdb->comments}.comment_ID,
2736 {$wpdb->comments}.comment_post_ID,
2737 {$wpdb->comments}.comment_author,
2738 {$wpdb->comments}.comment_date,
2739 {$wpdb->comments}.comment_content,
2740 {$wpdb->comments}.comment_parent,
2741 {$wpdb->comments}.user_id,
2742 {$wpdb->users}.display_name
2743
2744 FROM {$wpdb->comments}
2745
2746 INNER JOIN {$wpdb->users}
2747 ON {$wpdb->comments}.user_id = {$wpdb->users}.ID
2748 WHERE comment_type = 'tutor_q_and_a'
2749 AND {$wpdb->comments}.comment_parent = {$question_id} ORDER BY {$wpdb->comments}.comment_ID ASC ");
2750
2751 return $query;
2752 }
2753
2754 public function unanswered_question_count(){
2755 global $wpdb;
2756
2757 $count = $wpdb->get_var("select COUNT({$wpdb->comments}.comment_ID)
2758 from {$wpdb->comments}
2759 WHERE {$wpdb->comments}.comment_type = 'tutor_q_and_a'
2760 AND {$wpdb->comments}.comment_approved = 'waiting_for_answer'
2761 AND {$wpdb->comments}.comment_parent = 0;");
2762 return (int) $count;
2763 }
2764
2765 /**
2766 * @param int $course_id
2767 *
2768 * @return array|null|object
2769 *
2770 * Return all of announcements for a course
2771 *
2772 * @since v.1.0.0
2773 */
2774 public function get_announcements($course_id = 0){
2775 $course_id = $this->get_post_id($course_id);
2776 global $wpdb;
2777
2778 $query = $wpdb->get_results("select {$wpdb->posts}.ID, post_author, post_date, post_content, post_title, display_name
2779 from {$wpdb->posts}
2780 INNER JOIN {$wpdb->users} ON post_author = {$wpdb->users}.ID
2781 WHERE post_type = 'tutor_announcements'
2782 AND post_parent = {$course_id} ORDER BY {$wpdb->posts}.ID DESC;");
2783 return $query;
2784 }
2785
2786 /**
2787 * @param string $content
2788 *
2789 * @return mixed
2790 *
2791 * Announcement content
2792 *
2793 * @since v.1.0.0
2794 */
2795
2796 public function announcement_content($content = ''){
2797 $search = array('{user_display_name}');
2798
2799 $user_display_name = 'User';
2800 if (is_user_logged_in()){
2801 $user = wp_get_current_user();
2802 $user_display_name = $user->display_name;
2803 }
2804 $replace = array($user_display_name);
2805
2806 return str_replace($search, $replace, $content);
2807 }
2808
2809 /**
2810 * @param int $post_id
2811 * @param string $option_key
2812 * @param bool $default
2813 *
2814 * @return array|bool|mixed
2815 *
2816 * Get the quiz option from meta
2817 */
2818 public function get_quiz_option($post_id = 0, $option_key = '', $default = false){
2819 $post_id = $this->get_post_id($post_id);
2820 $get_option_meta = maybe_unserialize(get_post_meta($post_id, 'tutor_quiz_option', true));
2821
2822 if ( ! $option_key && ! empty($get_option_meta)) {
2823 return $get_option_meta;
2824 }
2825
2826 $value = $this->avalue_dot( $option_key, $get_option_meta );
2827 if ( $value ) {
2828 return $value;
2829 }
2830 return $default;
2831 }
2832
2833
2834 /**
2835 * @param int $quiz_id
2836 *
2837 * @return array|bool|null|object
2838 *
2839 * Get the questions by quiz ID
2840 */
2841 public function get_questions_by_quiz($quiz_id = 0){
2842 $quiz_id = $this->get_post_id($quiz_id);
2843 global $wpdb;
2844
2845 //$questions = $wpdb->get_results("SELECT ID, post_content, post_title, post_parent from {$wpdb->posts} WHERE post_type = 'tutor_question'
2846 // AND post_parent = {$quiz_id} ORDER BY menu_order ASC ");
2847
2848 $questions = $wpdb->get_results("SELECT * from {$wpdb->prefix}tutor_quiz_questions WHERE quiz_id = {$quiz_id} ORDER BY question_order ASC ");
2849
2850 if (is_array($questions) && count($questions)){
2851 return $questions;
2852 }
2853 return false;
2854 }
2855
2856 /**
2857 * @param int $question_id
2858 *
2859 * @return array|bool|object|void|null
2860 *
2861 * Get Quiz question by question id
2862 */
2863 public function get_quiz_question_by_id($question_id = 0){
2864 global $wpdb;
2865
2866 if ($question_id){
2867 $question = $wpdb->get_row("SELECT * from {$wpdb->prefix}tutor_quiz_questions WHERE question_id = {$question_id} LIMIT 0,1 ;");
2868 return $question;
2869 }
2870
2871 return false;
2872 }
2873
2874 /**
2875 * @param null $type
2876 *
2877 * @return array|mixed
2878 *
2879 * Get all question types
2880 *
2881 * @since v.1.0.0
2882 */
2883
2884 public function get_question_types($type = null){
2885 $types = array(
2886 'true_false' => array('name' => __('True/False', 'tutor'), 'icon' => '<i class="tutor-icon-block tutor-icon-yes-no"></i>'),
2887 'single_choice' => array('name' => __('Single Choice', 'tutor'), 'icon' => '<i class="tutor-icon-block tutor-icon-mark"></i>'),
2888 'multiple_choice' => array('name' => __('Multiple Choice', 'tutor'), 'icon' => '<i class="tutor-icon-block tutor-icon-multiple-choice"></i>'),
2889 'open_ended' => array('name' => __('Open Ended/Essay', 'tutor'), 'icon' => '<i class="tutor-icon-block tutor-icon-open-ended"></i>'),
2890 'short_answer' => array('name' => __('Short Answer', 'tutor'), 'icon' => '<i class="tutor-icon-block tutor-icon-short-ans"></i>'),
2891 'fill_in_the_blank' => array('name' => __('Fill In The Blank', 'tutor'), 'icon' => '<i class="tutor-icon-block tutor-icon-fill-gaps"></i>'),
2892 'answer_sorting' => array('name' => __('Answer Sorting', 'tutor'), 'icon' => '<i class="tutor-icon-block tutor-icon-answer-shorting"></i>'),
2893 'assessment' => array('name' => __('Assessment', 'tutor'), 'icon' => '<i class="tutor-icon-block tutor-icon-assesment"></i>'),
2894 'matching' => array('name' => __('Matching', 'tutor'), 'icon' => '<i class="tutor-icon-block tutor-icon-matching"></i>'),
2895 'image_matching' => array('name' => __('Image Matching', 'tutor'), 'icon' => '<i class="tutor-icon-block tutor-icon-image-matching"></i>'),
2896 'image_answering' => array('name' => __('Image Answering', 'tutor'), 'icon' => '<i class="tutor-icon-block tutor-icon-image-ans"></i>'),
2897 'ordering' => array('name' => __('Ordering', 'tutor'), 'icon' => '<i class="tutor-icon-block tutor-icon-ordering"></i>'),
2898 );
2899
2900 if (isset($types[$type])){
2901 return $types[$type];
2902 }
2903 return $types;
2904 }
2905
2906 public function get_quiz_answer_options_by_question($question_id){
2907 global $wpdb;
2908
2909 $answer_options = $wpdb->get_results("select
2910 {$wpdb->comments}.comment_ID,
2911 {$wpdb->comments}.comment_post_ID,
2912 {$wpdb->comments}.comment_content
2913
2914 FROM {$wpdb->comments}
2915 WHERE {$wpdb->comments}.comment_post_ID = {$question_id}
2916 AND {$wpdb->comments}.comment_type = 'quiz_answer_option'
2917 ORDER BY {$wpdb->comments}.comment_karma ASC ;");
2918
2919 if (is_array($answer_options) && count($answer_options)){
2920 return $answer_options;
2921 }
2922 return false;
2923 }
2924
2925 /**
2926 * @param $quiz_id
2927 *
2928 * @return int
2929 *
2930 * Get the next question order ID
2931 *
2932 * @since v.1.0.0
2933 */
2934
2935 public function quiz_next_question_order_id($quiz_id){
2936 global $wpdb;
2937
2938 //$last_order = (int) $wpdb->get_var("SELECT MAX(menu_order) FROM {$wpdb->posts} WHERE post_parent = {$quiz_id} AND post_type =
2939 // 'tutor_question';");
2940
2941 $last_order = (int) $wpdb->get_var("SELECT MAX(question_order) FROM {$wpdb->prefix}tutor_quiz_questions WHERE quiz_id = {$quiz_id} ;");
2942 return $last_order + 1;
2943 }
2944
2945 /**
2946 * @param $quiz_id
2947 *
2948 * @return int
2949 *
2950 * new design quiz question
2951 * @since v.1.0.0
2952 */
2953 public function quiz_next_question_id(){
2954 global $wpdb;
2955
2956 $last_order = (int) $wpdb->get_var("SELECT MAX(question_id) FROM {$wpdb->prefix}tutor_quiz_questions;");
2957 return $last_order + 1;
2958 }
2959
2960 public function get_quiz_id_by_question($question_id){
2961 global $wpdb;
2962
2963 $quiz_id = $wpdb->get_var("SELECT post_parent FROM {$wpdb->posts} WHERE ID = {$question_id} AND post_type = 'tutor_question' ;");
2964 return $quiz_id;
2965 }
2966
2967 /**
2968 * @param array $config
2969 *
2970 * @return array|bool|null|object
2971 *
2972 * It was used in previous quiz algorithm
2973 *
2974 * @deprecated
2975 *
2976 * @since v.1.0.0
2977 */
2978 public function get_unattached_quiz($config = array()){
2979 global $wpdb;
2980
2981 $default_attr = array(
2982 'search_term' => '',
2983 'start' => '0',
2984 'limit' => '10',
2985 'order' => 'DESC',
2986 'order_by' => 'ID',
2987 );
2988 $attr = array_merge($default_attr, $config);
2989 extract($attr);
2990
2991 $search_query = '';
2992 if (! empty($search_term)){
2993 $search_query = "AND post_title LIKE '%{$search_term}%'";
2994 }
2995
2996 $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} ");
2997
2998 if (is_array($questions) && count($questions)){
2999 return $questions;
3000 }
3001 return false;
3002 }
3003
3004 /**
3005 * @param int $post_id
3006 *
3007 * @return array|bool|null|object
3008 *
3009 * @since v.1.0.0
3010 */
3011 public function get_attached_quiz($post_id = 0){
3012 global $wpdb;
3013
3014 $post_id = $this->get_post_id($post_id);
3015
3016 $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}");
3017
3018 if (is_array($questions) && count($questions)){
3019 return $questions;
3020 }
3021 return false;
3022 }
3023
3024 /**
3025 * @param $quiz_id
3026 *
3027 * @return array|bool|null|object|void
3028 *
3029 * Get course by quiz
3030 *
3031 * @since v.1.0.0
3032 */
3033
3034 public function get_course_by_quiz($quiz_id){
3035 global $wpdb;
3036
3037 $quiz_id = $this->get_post_id($quiz_id);
3038 $post = get_post($quiz_id);
3039
3040 if ($post) {
3041 $course_post_type = tutor()->course_post_type;
3042 $course = $wpdb->get_row( "select ID, post_name, post_type, post_parent from {$wpdb->posts} where ID = {$post->post_parent} " );
3043
3044 if ($course) {
3045 //Checking if this topic
3046 if ( $course->post_type !== $course_post_type ) {
3047 $course = $wpdb->get_row( "select ID, post_name, post_type, post_parent from {$wpdb->posts} where ID = {$course->post_parent} " );
3048 }
3049 //Checking if this lesson
3050 if ( $course->post_type !== $course_post_type ) {
3051 $course = $wpdb->get_row( "select ID, post_name, post_type, post_parent from {$wpdb->posts} where ID = {$course->post_parent} " );
3052 }
3053
3054 return $course;
3055 }
3056 }
3057
3058 return false;
3059 }
3060
3061 /**
3062 * @param $quiz_id
3063 *
3064 * @return int
3065 *
3066 * @since v.1.0.0
3067 */
3068 public function total_questions_for_student_by_quiz($quiz_id){
3069 $quiz_id = $this->get_post_id($quiz_id);
3070 global $wpdb;
3071
3072 $total_question = (int) $wpdb->get_var("select count(ID) from {$wpdb->posts} where post_parent = {$quiz_id} AND post_type = 'tutor_question' ");
3073
3074 return $total_question;
3075 }
3076
3077 /**
3078 * @param int $quiz_id
3079 *
3080 * @return array|null|object|void
3081 *
3082 * Determine if there is any started quiz exists
3083 *
3084 * @since v.1.0.0
3085 */
3086
3087 public function is_started_quiz($quiz_id = 0){
3088 global $wpdb;
3089
3090 $quiz_id = $this->get_post_id($quiz_id);
3091 $user_id = get_current_user_id();
3092
3093 $is_started = $wpdb->get_row("SELECT * FROM {$wpdb->prefix}tutor_quiz_attempts WHERE user_id = {$user_id} AND quiz_id = {$quiz_id} AND attempt_status = 'attempt_started' ");
3094
3095 return $is_started;
3096 }
3097
3098 /**
3099 * @param $quiz_id
3100 *
3101 * Method for get the total amount of question for a quiz
3102 * Student will answer this amount of question, one quiz have many question
3103 * but student will answer a specific amount of questions
3104 *
3105 * @return int
3106 *
3107 * @since v.1.0.0
3108 */
3109
3110 public function max_questions_for_take_quiz($quiz_id){
3111 $quiz_id = $this->get_post_id($quiz_id);
3112 global $wpdb;
3113
3114 $max_questions = (int) $wpdb->get_var("select count(question_id) from {$wpdb->prefix}tutor_quiz_questions where quiz_id = {$quiz_id} ");
3115 $max_mentioned = (int) $this->get_quiz_option($quiz_id, 'max_questions_for_answer', 10);
3116
3117 if ($max_mentioned < $max_questions ){
3118 return $max_mentioned;
3119 }
3120
3121 return $max_questions;
3122 }
3123
3124 /**
3125 * @param int $attempt_id
3126 *
3127 * @return array|bool|null|object|void
3128 *
3129 * Get single quiz attempt
3130 *
3131 * @since v.1.0.0
3132 */
3133 public function get_attempt($attempt_id = 0){
3134 global $wpdb;
3135 if ( ! $attempt_id){
3136 return false;
3137 }
3138 $attempt = $wpdb->get_row("SELECT * FROM {$wpdb->prefix}tutor_quiz_attempts WHERE attempt_id = {$attempt_id} ");
3139 return $attempt;
3140 }
3141
3142 /**
3143 * @param $attempt_info
3144 *
3145 * @return mixed
3146 *
3147 * Get unserialize attempt info
3148 *
3149 * @since v.1.0.0
3150 */
3151
3152 public function quiz_attempt_info($attempt_info){
3153 return maybe_unserialize($attempt_info);
3154 }
3155
3156 /**
3157 * @param $quiz_attempt_id
3158 * @param array $attempt_info
3159 *
3160 * @return bool|int
3161 *
3162 * Update attempt for various action
3163 *
3164 * @since v.1.0.0
3165 */
3166 public function quiz_update_attempt_info($quiz_attempt_id, $attempt_info = array()){
3167 $answers = tutor_utils()->avalue_dot('answers', $attempt_info);
3168 $total_marks = array_sum(wp_list_pluck($answers, 'question_mark'));
3169 $earned_marks = tutor_utils()->avalue_dot('marks_earned', $attempt_info);
3170 $earned_mark_percent = $earned_marks > 0 ? ( number_format(($earned_marks * 100) / $total_marks)) : 0;
3171 update_comment_meta($quiz_attempt_id, 'earned_mark_percent', $earned_mark_percent);
3172
3173 return update_comment_meta($quiz_attempt_id,'quiz_attempt_info', $attempt_info);
3174 }
3175
3176 /**
3177 * @param int $quiz_id
3178 *
3179 * @return array|null|object
3180 *
3181 * Get random question by quiz id
3182 *
3183 * @since v.1.0.0
3184 */
3185
3186 public function get_random_question_by_quiz($quiz_id = 0){
3187 global $wpdb;
3188
3189 $quiz_id = $this->get_post_id($quiz_id);
3190 $is_attempt = $this->is_started_quiz($quiz_id);
3191
3192 $tempSql = " AND question_type = 'matching' ";
3193 $questions = $wpdb->get_results("SELECT * FROM {$wpdb->prefix}tutor_quiz_questions WHERE quiz_id = {$quiz_id} {$tempSql} ORDER BY RAND() LIMIT 0,1 ");
3194
3195 return $questions;
3196 }
3197
3198 /**
3199 * @param int $quiz_id
3200 *
3201 * @return array|null|object
3202 *
3203 * Get random questions by quiz
3204 */
3205 public function get_random_questions_by_quiz($quiz_id = 0){
3206 global $wpdb;
3207
3208 $quiz_id = $this->get_post_id($quiz_id);
3209 $attempt = $this->is_started_quiz($quiz_id);
3210 if ( ! $attempt){
3211 return false;
3212 }
3213
3214 $questions = $wpdb->get_results("SELECT * FROM {$wpdb->prefix}tutor_quiz_questions WHERE quiz_id = {$quiz_id} ORDER BY RAND() LIMIT {$attempt->total_questions} ");
3215
3216 return $questions;
3217 }
3218
3219 /**
3220 * @param $question_id
3221 * @param bool $rand
3222 *
3223 * @return array|bool|null|object
3224 *
3225 * Get answers list by quiz question
3226 *
3227 * @since v.1.0.0
3228 */
3229 public function get_answers_by_quiz_question($question_id, $rand = false){
3230 global $wpdb;
3231
3232
3233 $question = $wpdb->get_row("SELECT * from {$wpdb->prefix}tutor_quiz_questions WHERE question_id = {$question_id} ;");
3234 if ( ! $question){
3235 return false;
3236 }
3237
3238 $order = " answer_order ASC ";
3239 if ($question->question_type === 'ordering'){
3240 $order = " RAND() ";
3241 }
3242
3243 if ($rand){
3244 $order = " RAND() ";
3245 }
3246
3247 $answers = $wpdb->get_results("SELECT * FROM {$wpdb->prefix}tutor_quiz_question_answers WHERE belongs_question_id = {$question_id} AND belongs_question_type =
3248 '{$question->question_type}' order by {$order} ");
3249 return $answers;
3250 }
3251
3252 /**
3253 * @param int $quiz_id
3254 * @param int $user_id
3255 *
3256 * @return array|bool|null|object
3257 *
3258 * Get all of the attempts by an user of a quiz
3259 *
3260 * @since v.1.0.0
3261 */
3262
3263 public function quiz_attempts($quiz_id = 0, $user_id = 0){
3264 global $wpdb;
3265
3266 $quiz_id = $this->get_post_id($quiz_id);
3267 $user_id = $this->get_user_id($user_id);
3268
3269 $attempts = $wpdb->get_results("SELECT * FROM {$wpdb->prefix}tutor_quiz_attempts WHERE quiz_id = {$quiz_id} AND user_id = {$user_id} ");
3270
3271 if (is_array($attempts) && count($attempts)){
3272 return $attempts;
3273 }
3274
3275 return false;
3276 }
3277
3278 /**
3279 * @param string $search_term
3280 *
3281 * @return int
3282 *
3283 * Total number of quiz attempts
3284 *
3285 * @since v.1.0.0
3286 */
3287
3288 public function get_total_quiz_attempts($search_term = ''){
3289 global $wpdb;
3290
3291 if ($search_term){
3292 $search_term = " AND ( user_email like '%{$search_term}%' OR display_name like '%{$search_term}%' OR post_title like '%{$search_term}%' ) ";
3293 }
3294
3295 $count = $wpdb->get_var("SELECT COUNT(attempt_id)
3296 FROM {$wpdb->prefix}tutor_quiz_attempts quiz_attempts
3297 INNER JOIN {$wpdb->posts} quiz
3298 ON quiz_attempts.quiz_id = quiz.ID
3299 INNER JOIN {$wpdb->users}
3300 ON quiz_attempts.user_id = {$wpdb->users}.ID
3301 WHERE 1=1 AND quiz_attempts.attempt_ended_at <= NOW() {$search_term} ");
3302 return (int) $count;
3303 }
3304
3305 /**
3306 * @param int $start
3307 * @param int $limit
3308 * @param string $search_term
3309 *
3310 * @return array|null|object
3311 *
3312 *
3313 * Get the all quiz attempts
3314 *
3315 * @since v.1.0.0
3316 */
3317 public function get_quiz_attempts($start = 0, $limit = 10, $search_term = '') {
3318 global $wpdb;
3319
3320 if ($search_term){
3321 $search_term = " AND ( user_email like '%{$search_term}%' OR display_name like '%{$search_term}%' OR post_title like '%{$search_term}%' ) ";
3322 }
3323
3324 $query = $wpdb->get_results("SELECT *
3325 FROM {$wpdb->prefix}tutor_quiz_attempts quiz_attempts
3326 INNER JOIN {$wpdb->posts} quiz
3327 ON quiz_attempts.quiz_id = quiz.ID
3328 INNER JOIN {$wpdb->users}
3329 ON quiz_attempts.user_id = {$wpdb->users}.ID
3330 WHERE 1=1 AND quiz_attempts.attempt_ended_at <= NOW() {$search_term}
3331 ORDER BY quiz_attempts.attempt_id DESC
3332 LIMIT {$start},{$limit}; ");
3333 return $query;
3334 }
3335
3336 public function get_quiz_attempts_by_course_ids($start = 0, $limit = 10, $course_ids = array(), $search_term = '') {
3337 global $wpdb;
3338
3339 if ($search_term){
3340 $search_term = " AND ( user_email like '%{$search_term}%' OR display_name like '%{$search_term}%' OR post_title like '%{$search_term}%' ) ";
3341 }
3342
3343 $course_ids_in = implode($course_ids, ',');
3344 $sql = " AND quiz_attempts.course_id IN({$course_ids_in}) ";
3345 $search_term = $sql.$search_term;
3346
3347 $query = $wpdb->get_results("SELECT *
3348 FROM {$wpdb->prefix}tutor_quiz_attempts quiz_attempts
3349 INNER JOIN {$wpdb->posts} quiz
3350 ON quiz_attempts.quiz_id = quiz.ID
3351 INNER JOIN {$wpdb->users}
3352 ON quiz_attempts.user_id = {$wpdb->users}.ID
3353 WHERE 1=1 AND quiz_attempts.attempt_ended_at <= NOW() {$search_term}
3354 ORDER BY quiz_attempts.attempt_id DESC
3355 LIMIT {$start},{$limit}; ");
3356 return $query;
3357 }
3358
3359 public function get_total_quiz_attempts_by_course_ids($course_ids = array(), $search_term = ''){
3360 global $wpdb;
3361
3362 if ($search_term){
3363 $search_term = " AND ( user_email like '%{$search_term}%' OR display_name like '%{$search_term}%' OR post_title like '%{$search_term}%' ) ";
3364 }
3365
3366 $course_ids_in = implode($course_ids, ',');
3367 $sql = " AND quiz_attempts.course_id IN({$course_ids_in}) ";
3368 $search_term = $sql.$search_term;
3369
3370 $count = $wpdb->get_var("SELECT COUNT(attempt_id)
3371 FROM {$wpdb->prefix}tutor_quiz_attempts quiz_attempts
3372 INNER JOIN {$wpdb->posts} quiz
3373 ON quiz_attempts.quiz_id = quiz.ID
3374 INNER JOIN {$wpdb->users}
3375 ON quiz_attempts.user_id = {$wpdb->users}.ID
3376 WHERE 1=1 AND quiz_attempts.attempt_ended_at <= NOW() {$search_term} ");
3377 return (int) $count;
3378 }
3379
3380 /**
3381 * @param $attempt_id
3382 *
3383 * @return array|null|object
3384 *
3385 * Get quiz answers by attempt id
3386 *
3387 * @since v.1.0.0
3388 */
3389 public function get_quiz_answers_by_attempt_id($attempt_id){
3390 global $wpdb;
3391
3392 $results = $wpdb->get_results("SELECT answers.*, question.question_title, question.question_type
3393 FROM {$wpdb->prefix}tutor_quiz_attempt_answers answers
3394 LEFT JOIN {$wpdb->prefix}tutor_quiz_questions question ON answers.question_id = question.question_id
3395 WHERE answers.quiz_attempt_id = {$attempt_id} ");
3396
3397 return $results;
3398 }
3399
3400 /**
3401 * @param $answer_id
3402 *
3403 * @return array|null|object
3404 *
3405 * Get single answer by answer_id
3406 *
3407 * @since v.1.0.0
3408 */
3409 public function get_answer_by_id($answer_id){
3410 global $wpdb;
3411
3412 if (is_array($answer_id)){
3413 $in_ids = implode(",", $answer_id);
3414 $sql = "answer.answer_id IN({$in_ids})";
3415 }else{
3416 $sql = "answer.answer_id = {$answer_id}";
3417 }
3418
3419 $answer = $wpdb->get_results("SELECT answer.*, question.question_title, question.question_type
3420 FROM {$wpdb->prefix}tutor_quiz_question_answers answer
3421 LEFT JOIN {$wpdb->prefix}tutor_quiz_questions question ON answer.belongs_question_id = question.question_id
3422 WHERE 1=1 AND {$sql} ");
3423
3424 return $answer;
3425 }
3426
3427 /**
3428 * @param $ids
3429 *
3430 * @return array|bool|null|object
3431 *
3432 * Get quiz answers by ids
3433 *
3434 * @since v.1.0.0
3435 */
3436
3437 public function get_quiz_answers_by_ids($ids){
3438 $ids = (array) $ids;
3439
3440 if (!count($ids)){
3441 return false;
3442 }
3443
3444 $in_ids = implode(",", $ids);
3445
3446 global $wpdb;
3447 $query = $wpdb->get_results("SELECT
3448 comment_ID,
3449 comment_content
3450 FROM {$wpdb->comments}
3451 WHERE comment_type = 'quiz_answer_option' AND comment_ID IN({$in_ids}) ");
3452
3453 if (is_array($query) && count($query)){
3454 return $query;
3455 }
3456
3457 return false;
3458 }
3459
3460 /**
3461 * @param null $level
3462 *
3463 * @return mixed
3464 *
3465 * Get the users / students / course levels
3466 *
3467 * @since v.1.0.0
3468 */
3469
3470 public function course_levels($level = null){
3471 $levels = apply_filters('tutor_course_level', array(
3472 'all_levels' => __('All Levels', 'tutor'),
3473 'beginner' => __('Beginner', 'tutor'),
3474 'intermediate' => __('Intermediate', 'tutor'),
3475 'expert' => __('Expert', 'tutor'),
3476 ));
3477
3478 if ($level){
3479 if (isset($levels[$level])){
3480 return $levels[$level];
3481 }else{
3482 return '';
3483 }
3484 }
3485
3486 return $levels;
3487 }
3488
3489 /**
3490 * @return mixed|void
3491 *
3492 * Get user permalink for dashboard
3493 *
3494 * @since v.1.0.0
3495 */
3496 public function user_profile_permalinks(){
3497 $permalinks = array(
3498 'enrolled_course' => __('Enrolled Course', 'tutor'),
3499 'courses_taken' => __('Courses Taken', 'tutor'),
3500 'reviews_wrote' => __('Reviews Written', 'tutor'),
3501 );
3502
3503 return apply_filters('tutor_public_profile/permalinks', $permalinks);
3504 }
3505
3506 /**
3507 * @return bool|false|string
3508 *
3509 * Student registration form
3510 *
3511 * @since v.1.0.0
3512 */
3513 public function student_register_url(){
3514 $student_register_page = (int) $this->get_option('student_register_page');
3515
3516 if ($student_register_page){
3517 return get_the_permalink($student_register_page);
3518 }
3519 return false;
3520 }
3521
3522 /**
3523 * @return false|string
3524 *
3525 * Get frontend dashboard URL
3526 */
3527 public function tutor_dashboard_url(){
3528 $page_id = (int) tutor_utils()->get_option('student_dashboard');
3529 $page_id = apply_filters('tutor_dashboard_url', $page_id);
3530 return get_the_permalink($page_id);
3531 }
3532
3533 /**
3534 * @param int $course_id
3535 * @param int $user_id
3536 *
3537 * @return bool
3538 *
3539 * is_wishlisted();
3540 *
3541 * @since v.1.0.0
3542 */
3543 public function is_wishlisted($course_id = 0, $user_id = 0){
3544 $course_id = $this->get_post_id($course_id);
3545 $user_id = $this->get_user_id($user_id);
3546 if ( ! $user_id){
3547 return false;
3548 }
3549
3550 global $wpdb;
3551 $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} ;");
3552
3553 return $if_added_to_list;
3554 }
3555
3556 /**
3557 * @param int $user_id
3558 *
3559 * @return array|null|object
3560 *
3561 * Get the wish lists by an user
3562 *
3563 * @since v.1.0.0
3564 */
3565 public function get_wishlist($user_id = 0){
3566 $user_id = $this->get_user_id($user_id);
3567 global $wpdb;
3568
3569 $query = "SELECT $wpdb->posts.*
3570 FROM $wpdb->posts
3571 LEFT JOIN $wpdb->usermeta ON ($wpdb->posts.ID = $wpdb->usermeta.meta_value)
3572 WHERE $wpdb->usermeta.meta_key = '_tutor_course_wishlist'
3573 AND $wpdb->usermeta.user_id = {$user_id}
3574 ORDER BY $wpdb->usermeta.umeta_id DESC ";
3575 $pageposts = $wpdb->get_results($query, OBJECT);
3576 return $pageposts;
3577 }
3578
3579 /**
3580 * @param int $limit
3581 *
3582 * @return array|null|object
3583 *
3584 * Getting popular courses
3585 *
3586 * @since v.1.0.0
3587 */
3588 public function most_popular_courses($limit = 10){
3589 global $wpdb;
3590
3591 $courses = $wpdb->get_results("
3592 SELECT COUNT(enrolled.ID) as total_enrolled,
3593 enrolled.post_parent as course_id,
3594 course.*
3595 from {$wpdb->posts} enrolled
3596 INNER JOIN {$wpdb->posts} course ON enrolled.post_parent = course.ID
3597 WHERE enrolled.post_type = 'tutor_enrolled' AND enrolled.post_status = 'completed'
3598
3599 GROUP BY course_id
3600 ORDER BY total_enrolled DESC LIMIT 0,{$limit} ;");
3601
3602 return $courses;
3603 }
3604
3605 /**
3606 * @param int $limit
3607 *
3608 * @return array|bool|null|object
3609 *
3610 * Get most rated courses lists
3611 *
3612 * @since v.1.0.0
3613 */
3614 public function most_rated_courses($limit = 10){
3615 global $wpdb;
3616
3617 $result = $wpdb->get_results("
3618 SELECT COUNT(comment_ID) AS total_rating,
3619 comment_ID,
3620 comment_post_ID,
3621 course.*
3622 FROM {$wpdb->comments}
3623 INNER JOIN {$wpdb->posts} course ON comment_post_ID = course.ID
3624 WHERE {$wpdb->comments}.comment_type = 'tutor_course_rating' AND {$wpdb->comments}.comment_approved = 'approved'
3625 GROUP BY comment_post_ID ORDER BY total_rating DESC LIMIT 0,{$limit}
3626 ;");
3627
3628 if (is_array($result) && count($result)){
3629 return $result;
3630 }
3631 return false;
3632 }
3633
3634 /**
3635 * @param null $addon_field
3636 *
3637 * @return bool
3638 *
3639 * Get Addon config
3640 *
3641 * @since v.1.0.0
3642 */
3643 public function get_addon_config($addon_field = null){
3644 if ( ! $addon_field){
3645 return false;
3646 }
3647
3648 $addonsConfig = maybe_unserialize(get_option('tutor_addons_config'));
3649
3650 if (isset($addonsConfig[$addon_field])){
3651 return $addonsConfig[$addon_field];
3652 }
3653
3654 return false;
3655 }
3656
3657 /**
3658 * @return array|false|string
3659 *
3660 * Get the IP from visitor
3661 *
3662 * @since v.1.0.0
3663 */
3664 function get_ip() {
3665 $ipaddress = '';
3666 if (getenv('HTTP_CLIENT_IP'))
3667 $ipaddress = getenv('HTTP_CLIENT_IP');
3668 else if(getenv('HTTP_X_FORWARDED_FOR'))
3669 $ipaddress = getenv('HTTP_X_FORWARDED_FOR');
3670 else if(getenv('HTTP_X_FORWARDED'))
3671 $ipaddress = getenv('HTTP_X_FORWARDED');
3672 else if(getenv('HTTP_FORWARDED_FOR'))
3673 $ipaddress = getenv('HTTP_FORWARDED_FOR');
3674 else if(getenv('HTTP_FORWARDED'))
3675 $ipaddress = getenv('HTTP_FORWARDED');
3676 else if(getenv('REMOTE_ADDR'))
3677 $ipaddress = getenv('REMOTE_ADDR');
3678 else
3679 $ipaddress = 'UNKNOWN';
3680 return $ipaddress;
3681 }
3682
3683 /**
3684 * @return mixed|void
3685 *
3686 * Get the social icons
3687 *
3688 * @since v.1.0.4
3689 */
3690
3691 public function tutor_social_share_icons(){
3692 $icons = array(
3693 'facebook' => array('share_class' => 's_facebook', 'icon_html' => '<i class="tutor-icon-facebook"></i>' ),
3694 'twitter' => array('share_class' => 's_twitter', 'icon_html' => '<i class="tutor-icon-twitter"></i>' ),
3695 'linkedin' => array('share_class' => 's_linkedin', 'icon_html' => '<i class="tutor-icon-linkdin"></i>' ),
3696 'tumblr' => array('share_class' => 's_tumblr', 'icon_html' => '<i class="tutor-icon-tumblr"></i>' ),
3697 );
3698
3699 return apply_filters('tutor_social_share_icons', $icons);
3700 }
3701
3702 /**
3703 * @param array $array
3704 *
3705 * @return bool
3706 *
3707 * count method with check is_array
3708 *
3709 * @since v.1.0.4
3710 */
3711 public function count($array = array()){
3712 return is_array($array) && count($array);
3713 }
3714
3715 }