PluginProbe
Tutor LMS – eLearning and online course solution / 1.3.5
Tutor LMS – eLearning and online course solution v1.3.5
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.3.5, at classes/Utils.php

4,636 lines 116.8 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 * @param null $key
97 * @param array $array
98 *
99 * @return array|bool|mixed
100 *
101 * alias of avalue_dot method of utils
102 *
103 * Get array value by key and recursive array value by dot notation key
104 *
105 * ex: tutor_utils()->array_get('key.child_key', $array);
106 *
107 * @since v.1.3.3
108 */
109 public function array_get($key = null, $array = array()){
110 return $this->avalue_dot($key, $array);
111 }
112
113 /**
114 * @return array
115 *
116 * Get all pages
117 *
118 * @since v.1.0.0
119 */
120 public function get_pages(){
121 $pages = array();
122 $wp_pages = get_pages();
123 if (is_array($wp_pages) && count($wp_pages)){
124 foreach ($wp_pages as $page){
125 $pages[$page->ID] = $page->post_title;
126 }
127 }
128 return $pages;
129 }
130
131 /**
132 * @return string
133 *
134 * Get course archive URL
135 *
136 * @since v.1.0.0
137 */
138 public function course_archive_page_url(){
139 $course_post_type = tutor()->course_post_type;
140 $course_page_url = trailingslashit(home_url()).$course_post_type;
141
142 $course_archive_page = $this->get_option('course_archive_page');
143 if ($course_archive_page && $course_archive_page !== '-1'){
144 $course_page_url = get_permalink($course_archive_page);
145 }
146 return trailingslashit($course_page_url);
147 }
148
149 /**
150 * @param int $student_id
151 *
152 * @return string
153 *
154 * Get student URL
155 *
156 * @since v.1.0.0
157 */
158
159 public function profile_url($student_id = 0){
160 $site_url = trailingslashit(home_url()).'profile/';
161 $user_name = '';
162
163 $student_id = $this->get_user_id($student_id);
164 if ($student_id){
165 global $wpdb;
166 $user = $wpdb->get_row("SELECT user_login from {$wpdb->users} WHERE ID = {$student_id} ");
167 if ($user){
168 $user_name = $user->user_login;
169 }
170 }else{
171 $user_name = 'user_name';
172 }
173
174 return $site_url.$user_name;
175 }
176
177 /**
178 * @param string $user_login
179 *
180 * @return array|null|object
181 *
182 * Get user by user login
183 *
184 * @since v.1.0.0
185 */
186 public function get_user_by_login($user_login = ''){
187 global $wpdb;
188 $user_login = sanitize_text_field($user_login);
189 $user = $wpdb->get_row("SELECT * from {$wpdb->users} WHERE user_login = '{$user_login}'");
190 return $user;
191 }
192
193 /**
194 * @return bool
195 *
196 * Check if WooCommerce Activated
197 *
198 * @since v.1.0.0
199 */
200
201 public function has_wc(){
202 $activated_plugins = apply_filters( 'active_plugins', get_option( 'active_plugins' ));
203 //$depends = array('woocommerce/woocommerce.php', 'tutor-woocommerce/tutor-woocommerce.php');
204 $depends = array('woocommerce/woocommerce.php');
205 $has = count(array_intersect($depends, $activated_plugins)) == count($depends);
206
207 return $has;
208 }
209
210 /**
211 * @return bool
212 *
213 * determine if EDD plugin activated
214 *
215 * @since v.1.0.0
216 */
217 public function has_edd(){
218 $activated_plugins = apply_filters( 'active_plugins', get_option( 'active_plugins' ));
219 //$depends = array('easy-digital-downloads/easy-digital-downloads.php', 'tutor-edd/tutor-edd.php');
220 $depends = array('easy-digital-downloads/easy-digital-downloads.php');
221 $has = count(array_intersect($depends, $activated_plugins)) == count($depends);
222
223 return $has;
224 }
225
226 /**
227 * @return mixed
228 *
229 * @since v.1.0.0
230 */
231 public function languages(){
232 $language_codes = array(
233 'en' => 'English' ,
234 'aa' => 'Afar' ,
235 'ab' => 'Abkhazian' ,
236 'af' => 'Afrikaans' ,
237 'am' => 'Amharic' ,
238 'ar' => 'Arabic' ,
239 'as' => 'Assamese' ,
240 'ay' => 'Aymara' ,
241 'az' => 'Azerbaijani' ,
242 'ba' => 'Bashkir' ,
243 'be' => 'Byelorussian' ,
244 'bg' => 'Bulgarian' ,
245 'bh' => 'Bihari' ,
246 'bi' => 'Bislama' ,
247 'bn' => 'Bengali/Bangla' ,
248 'bo' => 'Tibetan' ,
249 'br' => 'Breton' ,
250 'ca' => 'Catalan' ,
251 'co' => 'Corsican' ,
252 'cs' => 'Czech' ,
253 'cy' => 'Welsh' ,
254 'da' => 'Danish' ,
255 'de' => 'German' ,
256 'dz' => 'Bhutani' ,
257 'el' => 'Greek' ,
258 'eo' => 'Esperanto' ,
259 'es' => 'Spanish' ,
260 'et' => 'Estonian' ,
261 'eu' => 'Basque' ,
262 'fa' => 'Persian' ,
263 'fi' => 'Finnish' ,
264 'fj' => 'Fiji' ,
265 'fo' => 'Faeroese' ,
266 'fr' => 'French' ,
267 'fy' => 'Frisian' ,
268 'ga' => 'Irish' ,
269 'gd' => 'Scots/Gaelic' ,
270 'gl' => 'Galician' ,
271 'gn' => 'Guarani' ,
272 'gu' => 'Gujarati' ,
273 'ha' => 'Hausa' ,
274 'hi' => 'Hindi' ,
275 'hr' => 'Croatian' ,
276 'hu' => 'Hungarian' ,
277 'hy' => 'Armenian' ,
278 'ia' => 'Interlingua' ,
279 'ie' => 'Interlingue' ,
280 'ik' => 'Inupiak' ,
281 'in' => 'Indonesian' ,
282 'is' => 'Icelandic' ,
283 'it' => 'Italian' ,
284 'iw' => 'Hebrew' ,
285 'ja' => 'Japanese' ,
286 'ji' => 'Yiddish' ,
287 'jw' => 'Javanese' ,
288 'ka' => 'Georgian' ,
289 'kk' => 'Kazakh' ,
290 'kl' => 'Greenlandic' ,
291 'km' => 'Cambodian' ,
292 'kn' => 'Kannada' ,
293 'ko' => 'Korean' ,
294 'ks' => 'Kashmiri' ,
295 'ku' => 'Kurdish' ,
296 'ky' => 'Kirghiz' ,
297 'la' => 'Latin' ,
298 'ln' => 'Lingala' ,
299 'lo' => 'Laothian' ,
300 'lt' => 'Lithuanian' ,
301 'lv' => 'Latvian/Lettish' ,
302 'mg' => 'Malagasy' ,
303 'mi' => 'Maori' ,
304 'mk' => 'Macedonian' ,
305 'ml' => 'Malayalam' ,
306 'mn' => 'Mongolian' ,
307 'mo' => 'Moldavian' ,
308 'mr' => 'Marathi' ,
309 'ms' => 'Malay' ,
310 'mt' => 'Maltese' ,
311 'my' => 'Burmese' ,
312 'na' => 'Nauru' ,
313 'ne' => 'Nepali' ,
314 'nl' => 'Dutch' ,
315 'no' => 'Norwegian' ,
316 'oc' => 'Occitan' ,
317 'om' => '(Afan)/Oromoor/Oriya' ,
318 'pa' => 'Punjabi' ,
319 'pl' => 'Polish' ,
320 'ps' => 'Pashto/Pushto' ,
321 'pt' => 'Portuguese' ,
322 'qu' => 'Quechua' ,
323 'rm' => 'Rhaeto-Romance' ,
324 'rn' => 'Kirundi' ,
325 'ro' => 'Romanian' ,
326 'ru' => 'Russian' ,
327 'rw' => 'Kinyarwanda' ,
328 'sa' => 'Sanskrit' ,
329 'sd' => 'Sindhi' ,
330 'sg' => 'Sangro' ,
331 'sh' => 'Serbo-Croatian' ,
332 'si' => 'Singhalese' ,
333 'sk' => 'Slovak' ,
334 'sl' => 'Slovenian' ,
335 'sm' => 'Samoan' ,
336 'sn' => 'Shona' ,
337 'so' => 'Somali' ,
338 'sq' => 'Albanian' ,
339 'sr' => 'Serbian' ,
340 'ss' => 'Siswati' ,
341 'st' => 'Sesotho' ,
342 'su' => 'Sundanese' ,
343 'sv' => 'Swedish' ,
344 'sw' => 'Swahili' ,
345 'ta' => 'Tamil' ,
346 'te' => 'Tegulu' ,
347 'tg' => 'Tajik' ,
348 'th' => 'Thai' ,
349 'ti' => 'Tigrinya' ,
350 'tk' => 'Turkmen' ,
351 'tl' => 'Tagalog' ,
352 'tn' => 'Setswana' ,
353 'to' => 'Tonga' ,
354 'tr' => 'Turkish' ,
355 'ts' => 'Tsonga' ,
356 'tt' => 'Tatar' ,
357 'tw' => 'Twi' ,
358 'uk' => 'Ukrainian' ,
359 'ur' => 'Urdu' ,
360 'uz' => 'Uzbek' ,
361 'vi' => 'Vietnamese' ,
362 'vo' => 'Volapuk' ,
363 'wo' => 'Wolof' ,
364 'xh' => 'Xhosa' ,
365 'yo' => 'Yoruba' ,
366 'zh' => 'Chinese' ,
367 'zu' => 'Zulu' ,
368 );
369
370 return apply_filters('tutor/utils/languages', $language_codes);
371 }
372
373
374 /**
375 * @param string $value
376 *
377 * Check raw data
378 *
379 * @since v.1.0.0
380 */
381 public function print_view($value = ''){
382 echo '<pre>';
383 print_r($value);
384 echo '</pre>';
385 }
386
387 /**
388 * @param array $excludes
389 *
390 * @return array|null|object
391 *
392 * Get courses
393 *
394 * @since v.1.0.0
395 */
396
397 public function get_courses($excludes = array()){
398 global $wpdb;
399
400
401 $excludes = (array) $excludes;
402 $exclude_query = '';
403 if (count($excludes)){
404 $exclude_query = implode("','", $excludes);
405 }
406
407 $course_post_type = tutor()->course_post_type;
408 $query = $wpdb->get_results("SELECT ID, post_author, post_title, post_name,post_status, menu_order
409 from {$wpdb->posts} WHERE post_status = 'publish'
410 AND ID NOT IN('$exclude_query')
411 AND post_type = '{$course_post_type}' ");
412 return $query;
413 }
414
415 /**
416 * @param int $instructor_id
417 *
418 * @return array|null|object
419 *
420 * Get courses for instructors
421 *
422 * @since v.1.0.0
423 */
424 public function get_courses_for_instructors($instructor_id = 0){
425 global $wpdb;
426
427 $instructor_id = $this->get_user_id($instructor_id);
428
429 $course_post_type = tutor()->course_post_type;
430 $query = $wpdb->get_results("SELECT ID, post_author, post_title, post_name,post_status, menu_order
431 from {$wpdb->posts}
432 WHERE post_author = {$instructor_id}
433 AND post_status IN ('publish', 'pending')
434 AND post_type = '{$course_post_type}' ");
435 return $query;
436 }
437
438 /**
439 * @param $instructor_id
440 *
441 * @return null|string
442 *
443 * Get course count by instructor
444 *
445 * @since v.1.0.0
446 */
447
448 public function get_course_count_by_instructor($instructor_id){
449 global $wpdb;
450
451 $course_post_type = tutor()->course_post_type;
452 $count = $wpdb->get_var("SELECT COUNT(ID) from {$wpdb->posts}
453 INNER JOIN {$wpdb->usermeta} ON user_id = {$instructor_id} AND meta_key = '_tutor_instructor_course_id' AND meta_value = ID
454 WHERE post_status = 'publish'
455 AND post_type = '{$course_post_type}' ; ");
456
457 return $count;
458 }
459
460 /**
461 * @param $instructor_id
462 *
463 * @return array|null|object
464 *
465 * Get courses by a instructor
466 *
467 * @since v.1.0.0
468 */
469 public function get_courses_by_instructor($instructor_id = 0, $post_status = array('publish')){
470 global $wpdb;
471
472 $instructor_id = $this->get_user_id($instructor_id);
473 $course_post_type = tutor()->course_post_type;
474
475
476 if ($post_status === 'any'){
477 $where_post_status = "";
478 }else{
479 $post_status = (array) $post_status;
480 $statuses = "'".implode("','", $post_status)."'";
481 $where_post_status = "AND $wpdb->posts.post_status IN({$statuses}) ";
482 }
483
484 $querystr = "
485 SELECT $wpdb->posts.*
486 FROM $wpdb->posts
487 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
488
489 WHERE 1 = 1 {$where_post_status}
490 AND $wpdb->posts.post_type = '{$course_post_type}'
491 AND $wpdb->posts.post_date < NOW()
492 ORDER BY $wpdb->posts.post_date DESC";
493
494 $pageposts = $wpdb->get_results($querystr, OBJECT);
495 return $pageposts;
496 }
497
498 /**
499 * @return mixed
500 *
501 * Get archive page course count
502 *
503 * @since v.1.0.0
504 */
505 public function get_archive_page_course_count(){
506 global $wp_query;
507 return $wp_query->post_count;
508 }
509
510 /**
511 * @return null|string
512 *
513 * Get course count
514 *
515 * @since v.1.0.0
516 */
517 public function get_course_count(){
518 global $wpdb;
519
520 $course_post_type = tutor()->course_post_type;
521 $count = $wpdb->get_var("SELECT COUNT(ID) from {$wpdb->posts} WHERE post_status = 'publish' AND post_type = '{$course_post_type}'; ");
522 return $count;
523 }
524
525 /**
526 * @return null|string
527 *
528 * Get lesson count
529 *
530 * @since v.1.0.0
531 */
532 public function get_lesson_count(){
533 global $wpdb;
534
535 $lesson_post_type = tutor()->lesson_post_type;
536 $count = $wpdb->get_var("SELECT COUNT(ID) from {$wpdb->posts} WHERE post_status = 'publish' AND post_type = '{$lesson_post_type}'; ");
537 return $count;
538 }
539
540 /**
541 * @param int $course_id
542 * @param int $limit
543 *
544 * @return \WP_Query
545 *
546 * Get lesson
547 *
548 * @since v.1.0.0
549 */
550 public function get_lesson($course_id = 0, $limit = 10){
551 $course_id = $this->get_post_id($course_id);
552
553 $lesson_post_type = tutor()->lesson_post_type;
554 $args = array(
555 'post_status' => 'publish',
556 'post_type' => $lesson_post_type,
557 'posts_per_page' => $limit,
558 'meta_query' => array(
559 array(
560 'key' => '_tutor_course_id_for_lesson',
561 'value' => $course_id,
562 'compare' => '=',
563 ),
564 ),
565 );
566 $query = new \WP_Query($args);
567
568 return $query;
569 }
570
571 /**
572 * @param int $course_id
573 *
574 * @return int
575 *
576 * Get total lesson count by a course
577 *
578 * @since v.1.0.0
579 */
580 public function get_lesson_count_by_course($course_id = 0){
581 $course_id = $this->get_post_id($course_id);
582 global $wpdb;
583
584 $lesson_post_type = tutor()->lesson_post_type;
585
586 $course_id = $this->get_post_id($course_id);
587 $topicIDS = $wpdb->get_col("SELECT ID FROM {$wpdb->posts} WHERE post_type = 'topics' AND post_parent = {$course_id} ");
588
589 $lesson_count = 0;
590 if ($this->count($topicIDS)){
591 $inIDS = implode(",", $topicIDS);
592 $lesson_count = $wpdb->get_var("SELECT COUNT(ID) FROM {$wpdb->posts} WHERE post_parent IN({$inIDS}) AND post_type = '{$lesson_post_type}' ");
593 }
594
595 return (int) $lesson_count;
596 }
597
598 /**
599 * @param int $course_id
600 * @param int $user_id
601 *
602 * @return int
603 *
604 * Get completed lesson total number by a course
605 *
606 * @since v.1.0.0
607 */
608 public function get_completed_lesson_count_by_course($course_id = 0, $user_id = 0){
609 $course_id = $this->get_post_id($course_id);
610 $user_id = $this->get_user_id($user_id);
611 global $wpdb;
612
613 $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} ");
614
615 $count = 0;
616 if (is_array($completed_lesson_ids) && count($completed_lesson_ids)){
617 $completed_lesson_meta_ids = array();
618 foreach ($completed_lesson_ids as $lesson_id){
619 $completed_lesson_meta_ids[] = '_tutor_completed_lesson_id_'.$lesson_id;
620 }
621 $in_ids = implode("','", $completed_lesson_meta_ids);
622
623 $count = (int) $wpdb->get_var("select count(umeta_id) from {$wpdb->usermeta} WHERE user_id = '{$user_id}' AND meta_key in('{$in_ids}') ");
624 }
625
626 return $count;
627 }
628
629 /**
630 * @param int $course_id
631 * @param int $user_id
632 *
633 * @return float|int
634 *
635 * @since v.1.0.0
636 */
637 public function get_course_completed_percent($course_id = 0, $user_id = 0){
638 $course_id = $this->get_post_id($course_id);
639 $user_id = $this->get_user_id($user_id);
640
641 $total_lesson = $this->get_lesson_count_by_course($course_id);
642 $completed_lesson = $this->get_completed_lesson_count_by_course($course_id, $user_id);
643
644 if ($total_lesson > 0 && $completed_lesson > 0){
645 return number_format(($completed_lesson * 100) / $total_lesson);
646 }
647
648 return 0;
649 }
650
651 /**
652 * @param int $course_id
653 *
654 * @return \WP_Query
655 *
656 * Get all topics by given course ID
657 *
658 * @since v.1.0.0
659 */
660 public function get_topics($course_id = 0){
661 $course_id = $this->get_post_id($course_id);
662
663 $args = array(
664 'post_type' => 'topics',
665 'post_parent' => $course_id,
666 'orderby' => 'menu_order',
667 'order' => 'ASC',
668 'posts_per_page' => -1,
669 );
670
671 $query = new \WP_Query($args);
672 return $query;
673 }
674
675 /**
676 * @param $course_ID
677 *
678 * @return int
679 *
680 * Get next topic order id
681 *
682 * @since v.1.0.0
683 */
684 public function get_next_topic_order_id($course_ID){
685 global $wpdb;
686
687 $last_order = (int) $wpdb->get_var("SELECT MAX(menu_order) FROM {$wpdb->posts} WHERE post_parent = {$course_ID} AND post_type = 'topics';");
688 return $last_order + 1;
689 }
690
691 /**
692 * @param $topic_ID
693 *
694 * @return int
695 *
696 * Get next course content order id
697 *
698 * @since v.1.0.0
699 */
700 public function get_next_course_content_order_id($topic_ID){
701 global $wpdb;
702
703 $last_order = (int) $wpdb->get_var("SELECT MAX(menu_order) FROM {$wpdb->posts} WHERE post_parent = {$topic_ID};");
704 return $last_order + 1;
705 }
706
707 /**
708 * @param int $topics_id
709 * @param int $limit
710 *
711 * @return \WP_Query
712 *
713 * Get lesson by topic
714 *
715 * @since v.1.0.0
716 */
717 public function get_lessons_by_topic($topics_id = 0, $limit = 10){
718 $topics_id = $this->get_post_id($topics_id);
719
720 $lesson_post_type = tutor()->lesson_post_type;
721 $args = array(
722 'post_type' => $lesson_post_type,
723 'post_parent' => $topics_id,
724 'posts_per_page' => $limit,
725 'orderby' => 'menu_order',
726 'order' => 'ASC',
727 );
728
729 $query = new \WP_Query($args);
730
731 return $query;
732 }
733
734 /**
735 * @param int $topics_id
736 * @param int $limit
737 *
738 * @return \WP_Query
739 *
740 * Get course content by topic
741 *
742 * @since v.1.0.0
743 */
744 public function get_course_contents_by_topic($topics_id = 0, $limit = 10){
745 $topics_id = $this->get_post_id($topics_id);
746
747 $lesson_post_type = tutor()->lesson_post_type;
748 $args = array(
749 'post_type' => apply_filters('tutor_course_contents_post_types', array($lesson_post_type, 'tutor_quiz')),
750 'post_parent' => $topics_id,
751 'posts_per_page' => $limit,
752 'orderby' => 'menu_order',
753 'order' => 'ASC',
754 );
755
756 $query = new \WP_Query($args);
757
758 return $query;
759 }
760
761 /**
762 * @param string $request_method
763 *
764 * Check actions nonce
765 *
766 * @since v.1.0.0
767 */
768 public function checking_nonce($request_method = 'post'){
769 if ($request_method === 'post'){
770 if (!isset($_POST[tutor()->nonce]) || !wp_verify_nonce($_POST[tutor()->nonce], tutor()->nonce_action)) {
771 exit('Nonce does not matched');
772 }
773 }else{
774 if (!isset($_GET[tutor()->nonce]) || !wp_verify_nonce($_GET[tutor()->nonce], tutor()->nonce_action)) {
775 exit('Nonce does not matched');
776 }
777 }
778 }
779
780 /**
781 * @param int $course_id
782 *
783 * @return bool
784 *
785 * @since v.1.0.0
786 */
787 public function is_course_purchasable($course_id = 0){
788 $course_id = $this->get_post_id($course_id);
789
790 $price_type = $this->price_type($course_id);
791 if ($price_type === 'free'){
792 $is_paid = apply_filters('is_course_paid', false, $course_id);
793 if ( ! $is_paid){
794 return false;
795 }
796 }
797 return apply_filters('is_course_purchasable', false, $course_id);
798 }
799
800 /**
801 * @param int $course_id
802 *
803 * @return null|string
804 *
805 * get course price in digits format if any
806 *
807 * @since v.1.0.0
808 */
809
810 public function get_course_price($course_id = 0){
811 $course_id = $this->get_post_id($course_id);
812
813 $price = null;
814
815 if ($this->is_course_purchasable()) {
816 if ($this->has_wc()){
817 $product_id = tutor_utils()->get_course_product_id($course_id);
818 $product = wc_get_product( $product_id );
819
820 if ( $product ) {
821 $price = $product->get_price();
822 }
823 }else{
824 $price = apply_filters('get_tutor_course_price', null, $course_id);
825 }
826 }
827
828 return $price;
829 }
830
831 /**
832 * @param int $course_id
833 *
834 * @return object
835 *
836 * Get raw course price and sale price of a course
837 * It could help you to calculate something
838 * Such as Calculate discount by regular price and sale price
839 *
840 * @since v.1.3.1
841 */
842 public function get_raw_course_price($course_id = 0){
843 $course_id = $this->get_post_id($course_id);
844
845 $prices = array(
846 'regular_price' => 0,
847 'sale_price' => 0,
848 );
849
850 //if ($this->is_course_purchasable($course_id)){
851 $product_id = $this->get_course_product_id($course_id);
852 if ($product_id) {
853 if ( $this->get_option( 'enable_course_sell_by_woocommerce' ) && $this->has_wc() ) {
854 $prices['regular_price'] = get_post_meta( $product_id, '_regular_price', true );
855 $prices['sale_price'] = get_post_meta( $product_id, '_sale_price', true );
856 } elseif ( $this->get_option( 'enable_tutor_edd' ) && $this->has_edd() ) {
857 $prices['regular_price'] = get_post_meta( $product_id, 'edd_price', true );
858 $prices['sale_price'] = get_post_meta( $product_id, 'edd_price', true );
859 }
860 }
861 //}
862
863 return (object) $prices;
864 }
865
866 /**
867 * @param int $course_id
868 *
869 * @return mixed
870 *
871 * Get the course price type
872 *
873 * @since v.1.3.5
874 */
875
876 public function price_type($course_id = 0){
877 $course_id = $this->get_post_id($course_id);
878
879 $price_type = get_post_meta($course_id, '_tutor_course_price_type', true);
880 return $price_type;
881 }
882
883 /**
884 * @param int $course_id
885 *
886 * @return array|bool|null|object
887 *
888 * Check if current user has been enrolled or not
889 *
890 * @since v.1.0.0
891 */
892
893 public function is_enrolled($course_id = 0, $user_id = 0){
894 $course_id = $this->get_post_id($course_id);
895 $user_id = $this->get_user_id($user_id);
896
897 if (is_user_logged_in()) {
898 global $wpdb;
899
900 $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'; " );
901
902 if ( $getEnrolledInfo ) {
903 return $getEnrolledInfo;
904 }
905 }
906 return false;
907 }
908
909 /**
910 * @param int $course_id
911 * @param int $user_id
912 *
913 * @return array|bool|null|object|void
914 *
915 * Has any enrolled for a user in a course
916 *
917 * @since v.1.0.0
918 */
919 public function has_any_enrolled($course_id = 0, $user_id = 0){
920 $course_id = $this->get_post_id($course_id);
921 $user_id = $this->get_user_id($user_id);
922
923 if (is_user_logged_in()) {
924 global $wpdb;
925
926 $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}; " );
927
928 if ( $getEnrolledInfo ) {
929 return $getEnrolledInfo;
930 }
931 }
932 return false;
933 }
934
935 /**
936 * @param int $lesson_id
937 * @param int $user_id
938 *
939 * @return array|bool|null|object
940 *
941 * Get the course Enrolled confirmation by lesson ID
942 *
943 * @since v.1.0.0
944 */
945
946 public function is_course_enrolled_by_lesson($lesson_id = 0, $user_id = 0){
947 $lesson_id = $this->get_post_id($lesson_id);
948 $user_id = $this->get_user_id($user_id);
949
950 return $this->is_enrolled($this->get_course_id_by_lesson($lesson_id));
951 }
952
953 /**
954 * @param int $lesson_id
955 *
956 * @return bool|mixed
957 *
958 * Get the course ID by Lesson
959 *
960 * @since v.1.0.0
961 */
962 public function get_course_id_by_lesson($lesson_id = 0){
963 $lesson_id = $this->get_post_id($lesson_id);
964 return get_post_meta($lesson_id, '_tutor_course_id_for_lesson', true);
965 }
966
967 /**
968 * @param int $course_id
969 *
970 * @return bool|false|string
971 *
972 * Get first lesson of a course
973 *
974 * @since v.1.0.0
975 */
976 public function get_course_first_lesson($course_id = 0){
977 $course_id = $this->get_post_id($course_id);
978 global $wpdb;
979
980 $lesson_id = $wpdb->get_var("
981 SELECT post_id as lesson_id
982 FROM $wpdb->postmeta
983 INNER JOIN {$wpdb->posts} ON post_id = {$wpdb->posts}.ID
984 WHERE meta_key = '_tutor_course_id_for_lesson' AND meta_value = {$course_id}
985
986 ORDER BY menu_order ASC LIMIT 1
987 ");
988
989 /*
990 $lesson_id = $wpdb->get_var(" select main_posts.ID from {$wpdb->posts} main_posts
991 WHERE post_parent =
992 (SELECT sub_posts.ID FROM {$wpdb->posts} sub_posts
993 WHERE post_type = 'topics' AND
994 sub_posts.post_parent = {$course_id} ORDER BY sub_posts.menu_order ASC LIMIT 1 )
995 ORDER BY main_posts.menu_order ASC LIMIT 1 ;");
996 */
997
998 if ($lesson_id){
999 return get_permalink($lesson_id);
1000 }
1001 return false;
1002 }
1003
1004 /**
1005 *
1006 * Get course sub pages in course dashboard
1007 *
1008 * @since v.1.0.0
1009 */
1010 public function course_sub_pages(){
1011 $nav_items = array(
1012 'overview' => __('Overview', 'tutor'),
1013 );
1014
1015 $enable_q_and_a_on_course = tutor_utils()->get_option('enable_q_and_a_on_course');
1016 if ($enable_q_and_a_on_course){
1017 $nav_items['questions'] = __('Q&A', 'tutor');
1018 }
1019 $nav_items['announcements'] = __('Announcements', 'tutor');
1020
1021 return apply_filters('tutor_course/single/enrolled/nav_items', $nav_items);
1022 }
1023
1024 /**
1025 * @param int $post_id
1026 *
1027 * @return bool|array
1028 *
1029 * @since v.1.0.0
1030 */
1031 public function get_video($post_id = 0){
1032 $post_id = $this->get_post_id($post_id);
1033 $attachments = get_post_meta($post_id, '_video', true);
1034 if ($attachments) {
1035 $attachments = maybe_unserialize($attachments);
1036 }
1037 return $attachments;
1038 }
1039
1040 /**
1041 * @param int $post_id
1042 * @param array $video_data
1043 *
1044 * @return bool
1045 *
1046 * Update the video Info
1047 */
1048 public function update_video($post_id = 0, $video_data = array()){
1049 $post_id = $this->get_post_id($post_id);
1050
1051 if (is_array($video_data) && count($video_data)){
1052 update_post_meta($post_id, '_video', $video_data);
1053 }
1054 }
1055
1056 /**
1057 * @param int $post_id
1058 *
1059 * @return bool|mixed
1060 *
1061 * @since v.1.0.0
1062 */
1063 public function get_attachments($post_id = 0){
1064 $post_id = $this->get_post_id($post_id);
1065 $attachments_arr = array();
1066 $attachments = maybe_unserialize(get_post_meta($post_id, '_tutor_attachments', true));
1067
1068 $font_icons = apply_filters('tutor_file_types_icon', array(
1069 'archive',
1070 'audio',
1071 'code',
1072 'default',
1073 'document',
1074 'interactive',
1075 'spreadsheet',
1076 'text',
1077 'video',
1078 'image',
1079 ));
1080
1081 if ( is_array($attachments) && count($attachments)) {
1082 foreach ( $attachments as $attachment ) {
1083 $url = wp_get_attachment_url( $attachment );
1084 $file_type = wp_check_filetype( $url );
1085 $ext = $file_type['ext'];
1086 $title = get_the_title($attachment);
1087
1088 $file_path = get_attached_file( $attachment );
1089 $size_bytes = file_exists($file_path) ? filesize( $file_path ) : 0;
1090 $size = size_format( $size_bytes, 2 );
1091 $type = wp_ext2type( $ext );
1092
1093 $icon = 'default';
1094 if ( $type && in_array( $type, $font_icons ) ) {
1095 $icon = $type;
1096 }
1097
1098 $data = array(
1099 'post_id' => $post_id,
1100 'id' => $attachment,
1101 'url' => $url,
1102 'name' => $title . '.' . $ext,
1103 'title' => $title,
1104 'ext' => $ext,
1105 'size' => $size,
1106 'size_bytes' => $size_bytes,
1107 'icon' => $icon,
1108 );
1109
1110 $attachments_arr[] = (object) apply_filters( 'tutor/posts/attachments', $data );
1111 }
1112 }
1113
1114 return $attachments_arr;
1115 }
1116
1117
1118 /**
1119 * @param $seconds
1120 *
1121 * @return string
1122 *
1123 * return seconds to formatted playtime
1124 *
1125 * @since v.1.0.0
1126 */
1127 public function playtime_string($seconds) {
1128 $sign = (($seconds < 0) ? '-' : '');
1129 $seconds = round(abs($seconds));
1130 $H = (int) floor( $seconds / 3600);
1131 $M = (int) floor(($seconds - (3600 * $H) ) / 60);
1132 $S = (int) round( $seconds - (3600 * $H) - (60 * $M) );
1133 return $sign.($H ? $H.':' : '').($H ? str_pad($M, 2, '0', STR_PAD_LEFT) : intval($M)).':'.str_pad($S, 2, 0, STR_PAD_LEFT);
1134 }
1135
1136 /**
1137 * @param $seconds
1138 *
1139 * @return array
1140 *
1141 * Get the playtime in array
1142 *
1143 * @since v.1.0.0
1144 */
1145 public function playtime_array($seconds){
1146 $run_time_format = array(
1147 'hours' => '00',
1148 'minutes' => '00',
1149 'seconds' => '00',
1150 );
1151
1152 if ($seconds <= 0 ){
1153 return $run_time_format;
1154 }
1155
1156 $playTimeString = $this->playtime_string($seconds);
1157 $timeInArray = explode(':', $playTimeString);
1158
1159 $run_time_size = count($timeInArray);
1160 if ($run_time_size === 3){
1161 $run_time_format['hours'] = $timeInArray[0];
1162 $run_time_format['minutes'] = $timeInArray[1];
1163 $run_time_format['seconds'] = $timeInArray[2];
1164 }elseif($run_time_size === 2){
1165 $run_time_format['minutes'] = $timeInArray[0];
1166 $run_time_format['seconds'] = $timeInArray[1];
1167 }
1168
1169 return $run_time_format;
1170 }
1171
1172 /**
1173 * @param $seconds
1174 *
1175 * @return string
1176 *
1177 * Convert seconds to human readable time
1178 *
1179 * @since v.1.0.0
1180 */
1181 public function seconds_to_time_context($seconds) {
1182 $sign = (($seconds < 0) ? '-' : '');
1183 $seconds = round(abs($seconds));
1184 $H = (int) floor( $seconds / 3600);
1185 $M = (int) floor(($seconds - (3600 * $H) ) / 60);
1186 $S = (int) round( $seconds - (3600 * $H) - (60 * $M) );
1187
1188 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';
1189 }
1190
1191 /**
1192 * @param int $lesson_id
1193 *
1194 * @return bool|object
1195 *
1196 * @since v.1.0.0
1197 */
1198
1199 public function get_video_info($lesson_id = 0){
1200 $lesson_id = $this->get_post_id($lesson_id);
1201 $video = $this->get_video($lesson_id);
1202
1203 if ( ! $video){
1204 return false;
1205 }
1206
1207 $info = array(
1208 'playtime' => '00:00',
1209 );
1210
1211 $types = apply_filters('tutor_video_types', array("mp4"=>"video/mp4", "webm"=>"video/webm", "ogg"=>"video/ogg"));
1212
1213 $videoSource = $this->avalue_dot('source', $video);
1214 if ($videoSource === 'html5'){
1215 $sourceVideoID = $this->avalue_dot('source_video_id', $video);
1216 $video_info = get_post_meta($sourceVideoID, '_wp_attachment_metadata', true);
1217
1218 if ($video_info){
1219 $path = get_attached_file($sourceVideoID);
1220 $info['playtime'] = $video_info['length_formatted'];
1221 $info['path'] = $path;
1222 $info['url'] = wp_get_attachment_url($sourceVideoID);
1223 $info['ext'] = strtolower(pathinfo($path, PATHINFO_EXTENSION));
1224 $info['type'] = $types[$info['ext']];
1225 }
1226 }
1227
1228 if ($videoSource !== 'html5'){
1229 $video = maybe_unserialize(get_post_meta($lesson_id, '_video', true));
1230
1231 $runtimeHours = tutor_utils()->avalue_dot('runtime.hours', $video);
1232 $runtimeMinutes = tutor_utils()->avalue_dot('runtime.minutes', $video);
1233 $runtimeSeconds = tutor_utils()->avalue_dot('runtime.seconds', $video);
1234
1235 $runtimeHours = $runtimeHours ? $runtimeHours : '00';
1236 $runtimeMinutes = $runtimeMinutes ? $runtimeMinutes : '00';
1237 $runtimeSeconds = $runtimeSeconds ? $runtimeSeconds : '00';
1238
1239 $info['playtime'] = "$runtimeHours:$runtimeMinutes:$runtimeSeconds";
1240 }
1241
1242 $info = array_merge($info, $video);
1243
1244 return (object) $info;
1245 }
1246
1247 /**
1248 * @param int $post_id
1249 *
1250 * @return bool
1251 *
1252 * Ensure if attached video is self hosted or not
1253 *
1254 * @since v.1.0.0
1255 */
1256 public function is_html5_video($post_id = 0){
1257 $post_id = $this->get_post_id($post_id);
1258
1259 $video = $this->get_video($post_id);
1260 if ( ! $video){
1261 return false;
1262 }
1263 $videoSource = $this->avalue_dot('source', $video);
1264 return $videoSource === 'html5';
1265 }
1266
1267 /**
1268 *
1269 * return lesson type icon
1270 *
1271 * @param int $lesson_id
1272 * @param bool $html
1273 * @param bool $echo
1274 *
1275 * @return string
1276 *
1277 * @since v.1.0.0
1278 */
1279
1280 public function get_lesson_type_icon($lesson_id = 0, $html = false, $echo = false){
1281 $post_id = $this->get_post_id($lesson_id);
1282 $video = tutor_utils()->get_video_info($post_id);
1283
1284 $play_time = false;
1285 if ($video){
1286 $play_time = $video->playtime;
1287 }
1288
1289 $tutor_lesson_type_icon = $play_time ? 'youtube' : 'document';
1290
1291 if ($html){
1292 $tutor_lesson_type_icon = "<i class='tutor-icon-$tutor_lesson_type_icon'></i> ";
1293 }
1294
1295 if ($tutor_lesson_type_icon){
1296 echo $tutor_lesson_type_icon;
1297 }
1298
1299 return $tutor_lesson_type_icon;
1300 }
1301
1302 /**
1303 * @param int $lesson_id
1304 * @param int $user_id
1305 *
1306 * @return bool|mixed
1307 *
1308 * @since v.1.0.0
1309 */
1310
1311 public function is_completed_lesson($lesson_id = 0, $user_id = 0){
1312 $lesson_id = $this->get_post_id($lesson_id);
1313 $user_id = $this->get_user_id($user_id);
1314
1315 $is_completed = get_user_meta($user_id, '_tutor_completed_lesson_id_'.$lesson_id, true);
1316
1317 if ($is_completed){
1318 return $is_completed;
1319 }
1320
1321 return false;
1322 }
1323
1324 /**
1325 * @param int $course_id
1326 * @param int $user_id
1327 *
1328 * @return array|bool|null|object|void
1329 *
1330 * Determine if a course completed
1331 *
1332 * @since v.1.0.0
1333 */
1334
1335 public function is_completed_course($course_id = 0, $user_id = 0){
1336 if ( ! is_user_logged_in()){
1337 return false;
1338 }
1339
1340 global $wpdb;
1341 $course_id = $this->get_post_id($course_id);
1342 $user_id = $this->get_user_id($user_id);
1343
1344 $is_completed = $wpdb->get_row("SELECT comment_ID,
1345 comment_post_ID as course_id,
1346 comment_author as completed_user_id,
1347 comment_date as completion_date,
1348 comment_content as completed_hash
1349 from {$wpdb->comments}
1350 WHERE comment_agent = 'TutorLMSPlugin'
1351 AND comment_type = 'course_completed'
1352 AND comment_post_ID = {$course_id}
1353 AND user_id = {$user_id} ;");
1354
1355 if ($is_completed){
1356 return $is_completed;
1357 }
1358
1359 return false;
1360 }
1361
1362 /**
1363 * @param array $input
1364 *
1365 * @return array
1366 *
1367 * Sanitize input array
1368 *
1369 * @since v.1.0.0
1370 */
1371 public function sanitize_array($input = array()){
1372 $array = array();
1373
1374 if (is_array($input) && count($input)){
1375 foreach ($input as $key => $value){
1376 if (is_array($value)){
1377 $array[$key] = $this->sanitize_array($value);
1378 }else{
1379 $key = sanitize_text_field($key);
1380 $value = sanitize_text_field($value);
1381 $array[$key] = $value;
1382 }
1383 }
1384 }
1385
1386 return $array;
1387 }
1388
1389 /**
1390 * @param int $post_id
1391 *
1392 * @return array|bool
1393 *
1394 * Determine if has any video in single
1395 *
1396 * @since v.1.0.0
1397 */
1398
1399 public function has_video_in_single($post_id = 0){
1400 if (is_single()) {
1401 $post_id = $this->get_post_id($post_id);
1402
1403 $video = $this->get_video( $post_id );
1404 if ( $video ) {
1405 return $video;
1406 }
1407 }
1408 return false;
1409
1410 }
1411
1412 /**
1413 * @param int $start
1414 * @param int $limit
1415 * @param string $search_term
1416 * @param int $course_id
1417 *
1418 * @return array|null|object
1419 *
1420 *
1421 * Get the enrolled students for all courses.
1422 *
1423 * Pass course id in 4th parameter to get students course wise.
1424 *
1425 * @since v.1.0.0
1426 */
1427 public function get_students($start = 0, $limit = 10, $search_term = ''){
1428 $meta_key = '_is_tutor_student';
1429
1430 global $wpdb;
1431
1432 if ($search_term){
1433 $search_term = " AND ( {$wpdb->users}.display_name LIKE '%{$search_term}%' OR {$wpdb->users}.user_email LIKE '%{$search_term}%' ) ";
1434 }
1435
1436 $students = $wpdb->get_results("SELECT SQL_CALC_FOUND_ROWS {$wpdb->users}.* FROM {$wpdb->users}
1437 INNER JOIN {$wpdb->usermeta}
1438 ON ( {$wpdb->users}.ID = {$wpdb->usermeta}.user_id )
1439 WHERE 1=1 AND ( {$wpdb->usermeta}.meta_key = '{$meta_key}' ) {$search_term}
1440 ORDER BY {$wpdb->usermeta}.meta_value DESC
1441 LIMIT {$start}, {$limit} ");
1442
1443 return $students;
1444 }
1445
1446 /**
1447 * @return int
1448 *
1449 * @since v.1.0.0
1450 *
1451 * get the total students
1452 * pass course id to get course wise total students
1453 *
1454 * @since v.1.0.0
1455 */
1456 public function get_total_students($search_term = ''){
1457 $meta_key = '_is_tutor_student';
1458
1459 global $wpdb;
1460
1461 if ($search_term){
1462 $search_term = " AND ( {$wpdb->users}.display_name LIKE '%{$search_term}%' OR {$wpdb->users}.user_email LIKE '%{$search_term}%' ) ";
1463 }
1464
1465 $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 ");
1466
1467 return (int) $count;
1468 }
1469
1470 /**
1471 * @param int $user_id
1472 *
1473 * @return array
1474 *
1475 * Get complete courses ids by user
1476 *
1477 * @since v.1.0.0
1478 */
1479 public function get_completed_courses_ids_by_user($user_id = 0){
1480 global $wpdb;
1481
1482 $user_id = $this->get_user_id($user_id);
1483
1484 $course_ids = (array) $wpdb->get_col("SELECT comment_post_ID as course_id
1485 from {$wpdb->comments}
1486 WHERE comment_agent = 'TutorLMSPlugin'
1487 AND comment_type = 'course_completed'
1488 AND user_id = {$user_id} ;");
1489
1490 return $course_ids;
1491 }
1492
1493 /**
1494 * @param int $user_id
1495 *
1496 * @return bool|\WP_Query
1497 *
1498 * Return courses by user_id
1499 *
1500 * @since v.1.0.0
1501 */
1502 public function get_courses_by_user($user_id = 0){
1503 $user_id = $this->get_user_id($user_id);
1504 $course_ids = $this->get_completed_courses_ids_by_user($user_id);
1505
1506 if (count($course_ids)){
1507 $course_post_type = tutor()->course_post_type;
1508 $course_args = array(
1509 'post_type' => $course_post_type,
1510 'post_status' => 'publish',
1511 'post__in' => $course_ids,
1512 );
1513
1514 return new \WP_Query($course_args);
1515 }
1516
1517 return false;
1518 }
1519
1520 /**
1521 * @param int $user_id
1522 *
1523 * @return bool|\WP_Query
1524 *
1525 * Get the active course by user
1526 *
1527 * @since v.1.0.0
1528 */
1529
1530 public function get_active_courses_by_user($user_id = 0){
1531 $user_id = $this->get_user_id($user_id);
1532
1533 $course_ids = $this->get_completed_courses_ids_by_user($user_id);
1534 $enrolled_course_ids = $this->get_enrolled_courses_ids_by_user($user_id);
1535 $active_courses = array_diff($enrolled_course_ids, $course_ids);
1536
1537 if (count($active_courses)){
1538 $course_post_type = tutor()->course_post_type;
1539 $course_args = array(
1540 'post_type' => $course_post_type,
1541 'post_status' => 'publish',
1542 'post__in' => $active_courses,
1543 );
1544
1545 return new \WP_Query($course_args);
1546 }
1547
1548 return false;
1549 }
1550
1551 /**
1552 * @param int $user_id
1553 *
1554 * @return array
1555 *
1556 * Get enrolled course ids by a user
1557 *
1558 * @since v.1.0.0
1559 */
1560
1561 public function get_enrolled_courses_ids_by_user($user_id = 0){
1562 global $wpdb;
1563 $user_id = $this->get_user_id($user_id);
1564 $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'; ");
1565
1566 return $course_ids;
1567 }
1568
1569 /**
1570 * @param int $course_id
1571 *
1572 * @return int
1573 *
1574 * Get the total enrolled users at course
1575 */
1576 public function count_enrolled_users_by_course($course_id = 0){
1577 global $wpdb;
1578 $course_id = $this->get_post_id($course_id);
1579
1580 $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'; ");
1581
1582 return (int) $course_ids;
1583 }
1584
1585 /**
1586 * @param int $user_id
1587 *
1588 * @return bool|\WP_Query
1589 *
1590 * Get the enrolled courses by user
1591 */
1592 public function get_enrolled_courses_by_user($user_id = 0){
1593 global $wpdb;
1594
1595 $user_id = $this->get_user_id($user_id);
1596 $course_ids = $this->get_enrolled_courses_ids_by_user($user_id);
1597
1598 if (count($course_ids)){
1599 $course_post_type = tutor()->course_post_type;
1600 $course_args = array(
1601 'post_type' => $course_post_type,
1602 'post_status' => 'publish',
1603 'post__in' => $course_ids,
1604 );
1605 return new \WP_Query($course_args);
1606 }
1607 return false;
1608 }
1609
1610
1611 /**
1612 * @param int $post_id
1613 *
1614 * @return string
1615 *
1616 * Get the video streaming URL by post/lesson/course ID
1617 */
1618 public function get_video_stream_url($post_id = 0){
1619 $post_id = $this->get_post_id($post_id);
1620 $post = get_post($post_id);
1621
1622 if ($post->post_type === tutor()->lesson_post_type ){
1623 $video_url = trailingslashit(home_url()).'video-url/'.$post->post_name;
1624 }else{
1625 $video_info = tutor_utils()->get_video_info($post_id);
1626 $video_url = $video_info->url;
1627 }
1628
1629 return $video_url;
1630 }
1631
1632 /**
1633 * @param int $lesson_id
1634 * @param int $user_id
1635 *
1636 * @return array|bool|mixed
1637 *
1638 * Get student lesson reading current info
1639 *
1640 * @since v.1.0.0
1641 */
1642 public function get_lesson_reading_info_full($lesson_id = 0, $user_id = 0){
1643 $lesson_id = $this->get_post_id($lesson_id);
1644 $user_id = $this->get_user_id($user_id);
1645
1646 $lesson_info = (array) maybe_unserialize(get_user_meta($user_id, '_lesson_reading_info', true));
1647 return $this->avalue_dot($lesson_id, $lesson_info);
1648 }
1649
1650 /**
1651 * @param int $post_id
1652 *
1653 * @return bool|false|int
1654 *
1655 * Get current post id or given post id
1656 *
1657 * @since v.1.0.0
1658 */
1659 public function get_post_id($post_id = 0){
1660 if ( ! $post_id){
1661 $post_id = get_the_ID();
1662 if ( ! $post_id){
1663 return false;
1664 }
1665 }
1666
1667 return $post_id;
1668 }
1669
1670 /**
1671 * @param int $user_id
1672 *
1673 * @return bool|int
1674 *
1675 * Get current user or given user ID
1676 *
1677 * @since v.1.0.0
1678 */
1679 public function get_user_id($user_id = 0){
1680 if ( ! $user_id){
1681 $user_id = get_current_user_id();
1682 if ( ! $user_id){
1683 return false;
1684 }
1685 }
1686
1687 return $user_id;
1688 }
1689
1690 /**
1691 * @param int $lesson_id
1692 * @param int $user_id
1693 * @param string $key
1694 *
1695 * @return array|bool|mixed
1696 *
1697 * Get lesson reading info by key
1698 *
1699 * @since v.1.0.0
1700 */
1701
1702 public function get_lesson_reading_info($lesson_id = 0, $user_id = 0, $key = ''){
1703 $lesson_id = $this->get_post_id($lesson_id);
1704 $user_id = $this->get_user_id($user_id);
1705
1706 $lesson_info = $this->get_lesson_reading_info_full($lesson_id, $user_id);
1707
1708 return $this->avalue_dot($key, $lesson_info);
1709 }
1710
1711 /**
1712 * @param int $lesson_id
1713 * @param int $user_id
1714 * @param array $data
1715 *
1716 * @return bool
1717 *
1718 * Update student lesson reading info
1719 *
1720 * @since v.1.0.0
1721 */
1722 public function update_lesson_reading_info($lesson_id = 0, $user_id = 0, $key = '', $value = ''){
1723 $lesson_id = $this->get_post_id($lesson_id);
1724 $user_id = $this->get_user_id($user_id);
1725
1726 if ($key && $value){
1727 $lesson_info = (array) maybe_unserialize(get_user_meta($user_id, '_lesson_reading_info', true));
1728 $lesson_info[$lesson_id][$key] = $value;
1729 update_user_meta($user_id, '_lesson_reading_info', $lesson_info);
1730 }
1731 }
1732
1733 /**
1734 * @param string $url
1735 *
1736 * @return bool
1737 *
1738 * Get the Youtube Video ID from URL
1739 *
1740 * @since v.1.0.0
1741 */
1742 public function get_youtube_video_id($url = ''){
1743 if (!$url){
1744 return false;
1745 }
1746 preg_match('%(?:youtube(?:-nocookie)?\.com/(?:[^/]+/.+/|(?:v|e(?:mbed)?)/|.*[?&]v=)|youtu\.be/)([^"&?/ ]{11})%i', $url, $match);
1747
1748 if (isset($match[1])) {
1749 $youtube_id = $match[1];
1750 return $youtube_id;
1751 }
1752
1753 return false;
1754 }
1755
1756 /**
1757 * @param string $url
1758 *
1759 * @return bool
1760 *
1761 * Get the vimeo video id from URL
1762 *
1763 * @since v.1.0.0
1764 */
1765 public function get_vimeo_video_id($url = ''){
1766 if (preg_match('%^https?:\/\/(?:www\.|player\.)?vimeo.com\/(?:channels\/(?:\w+\/)?|groups\/([^\/]*)\/videos\/|album\/(\d+)\/video\/|video\/|)(\d+)(?:$|\/|\?)(?:[?]?.*)$%im', $url, $match)) {
1767 if (isset($match[3])){
1768 return $match[3];
1769 }
1770 }
1771 return false;
1772 }
1773
1774 /**
1775 * @param int $post_id
1776 *
1777 * Mark lesson complete
1778 *
1779 * @since v.1.0.0
1780 */
1781 public function mark_lesson_complete($post_id = 0, $user_id = 0){
1782 $post_id = $this->get_post_id($post_id);
1783 $user_id = $this->get_user_id($user_id);
1784
1785 do_action('tutor_mark_lesson_complete_before', $post_id, $user_id);
1786 update_user_meta($user_id, '_tutor_completed_lesson_id_'.$post_id, time());
1787 do_action('tutor_mark_lesson_complete_after', $post_id, $user_id);
1788
1789 }
1790
1791 /**
1792 * Saving enroll information to posts table
1793 * post_author = enrolled_student_id (wp_users id)
1794 * post_parent = enrolled course id
1795 *
1796 * @type: call when need
1797 * @return bool;
1798 *
1799 * @since v.1.0.0
1800 */
1801 public function do_enroll($course_id = 0, $order_id = 0){
1802 if ( ! $course_id){
1803 return false;
1804 }
1805
1806 do_action('tutor_before_enroll', $course_id);
1807 $user_id = get_current_user_id();
1808 $title = __('Course Enrolled', 'tutor')." &ndash; ".date_i18n(get_option('date_format')) .' @ '.date_i18n(get_option('time_format') ) ;
1809
1810 $enrolment_status = 'completed';
1811
1812 if ($this->is_course_purchasable($course_id)) {
1813 /**
1814 * We need to verify this enrollment, we will change the status later after payment confirmation
1815 */
1816 $enrolment_status = 'pending';
1817 }
1818
1819 $enroll_data = apply_filters('tutor_enroll_data',
1820 array(
1821 'post_type' => 'tutor_enrolled',
1822 'post_title' => $title,
1823 'post_status' => $enrolment_status,
1824 'post_author' => $user_id,
1825 'post_parent' => $course_id,
1826 )
1827 );
1828
1829 // Insert the post into the database
1830 $isEnrolled = wp_insert_post( $enroll_data );
1831 if ($isEnrolled) {
1832 do_action('tutor_after_enroll', $course_id, $isEnrolled);
1833
1834 //Mark Current User as Students with user meta data
1835 update_user_meta( $user_id, '_is_tutor_student', time() );
1836
1837 if ($order_id) {
1838 //Mark order for course and user
1839 $product_id = $this->get_course_product_id($course_id);
1840 update_post_meta( $isEnrolled, '_tutor_enrolled_by_order_id', $order_id );
1841 update_post_meta( $isEnrolled, '_tutor_enrolled_by_product_id', $product_id );
1842 update_post_meta( $order_id, '_is_tutor_order_for_course', time() );
1843 update_post_meta( $order_id, '_tutor_order_for_course_id_'.$course_id, $isEnrolled );
1844 }
1845 return true;
1846 }
1847
1848 return false;
1849 }
1850
1851 /**
1852 * @param $order_id
1853 *
1854 * Complete course enrollment and do some task
1855 *
1856 * @since v.1.0.0
1857 */
1858 public function complete_course_enroll($order_id){
1859 if ( ! tutor_utils()->is_tutor_order($order_id)){
1860 return;
1861 }
1862
1863 global $wpdb;
1864
1865 $enrolled_ids_with_course = $this->get_course_enrolled_ids_by_order_id($order_id);
1866 if ($enrolled_ids_with_course){
1867 $enrolled_ids = wp_list_pluck($enrolled_ids_with_course, 'enrolled_id');
1868
1869 if (is_array($enrolled_ids) && count($enrolled_ids)){
1870 foreach ($enrolled_ids as $enrolled_id){
1871 $wpdb->update( $wpdb->posts, array( 'post_status' => 'completed' ), array( 'ID' => $enrolled_id ) );
1872 }
1873 }
1874 }
1875 }
1876
1877 /**
1878 * @param $order_id
1879 *
1880 * @return array|bool
1881 *
1882 * @since v.1.0.0
1883 */
1884 public function get_course_enrolled_ids_by_order_id($order_id){
1885 global $wpdb;
1886 //Getting all of courses ids within this order
1887
1888 $courses_ids = $wpdb->get_results("SELECT * FROM {$wpdb->postmeta} WHERE post_id = {$order_id} AND meta_key LIKE '_tutor_order_for_course_id_%' ");
1889
1890 if (is_array($courses_ids) && count($courses_ids)){
1891 $course_enrolled_by_order = array();
1892 foreach ($courses_ids as $courses_id){
1893 $course_id = str_replace('_tutor_order_for_course_id_', '',$courses_id->meta_key);
1894 //array(order_id => array('course_id' => $course_id, 'enrolled_id' => enrolled_id))
1895 $course_enrolled_by_order[] = array('course_id' => $course_id, 'enrolled_id' => $courses_id->meta_value, 'order_id' => $courses_id->post_id );
1896 }
1897 return $course_enrolled_by_order;
1898 }
1899 return false;
1900 }
1901
1902 /**
1903 * Get wc product in efficient query
1904 *
1905 * @since v.1.0.0
1906 */
1907
1908 /**
1909 * @return array|null|object
1910 *
1911 * WooCommerce specific utils
1912 */
1913 public function get_wc_products_db(){
1914 global $wpdb;
1915 $query = $wpdb->get_results("SELECT ID, post_title from {$wpdb->posts} WHERE post_status = 'publish' AND post_type = 'product' ");
1916
1917 return $query;
1918 }
1919
1920 /**
1921 * @return array|null|object
1922 *
1923 * Get EDD Products
1924 */
1925 public function get_edd_products(){
1926 global $wpdb;
1927 $query = $wpdb->get_results("SELECT ID, post_title from {$wpdb->posts} WHERE post_status = 'publish' AND post_type = 'download' ");
1928
1929 return $query;
1930 }
1931
1932 /**
1933 * @param int $course_id
1934 *
1935 * @return int
1936 *
1937 * Get course productID
1938 *
1939 * @since v.1.0.0
1940 */
1941 public function get_course_product_id($course_id = 0){
1942 $course_id = $this->get_post_id($course_id);
1943 $product_id = (int) get_post_meta($course_id, '_tutor_course_product_id', true);
1944
1945 return $product_id;
1946 }
1947
1948 /**
1949 * @param int $product_id
1950 *
1951 * @return array|null|object|void
1952 *
1953 * Get Product belongs with course
1954 *
1955 * @since v.1.0.0
1956 */
1957
1958 public function product_belongs_with_course($product_id = 0){
1959 global $wpdb;
1960
1961 $query = $wpdb->get_row("select * from {$wpdb->postmeta} WHERE meta_key='_tutor_course_product_id' AND meta_value = {$product_id} limit 1 ");
1962 return $query;
1963 }
1964
1965 /**
1966 * #End WooCommerce specific utils
1967 *
1968 * @since v.1.0.0
1969 */
1970
1971 public function get_enrolled_statuses(){
1972 return apply_filters(
1973 'tutor_get_enrolled_statuses',
1974 array (
1975 'pending',
1976 'processing',
1977 'on-hold',
1978 'completed',
1979 'cancelled',
1980 'refunded',
1981 'failed',
1982 )
1983 );
1984 }
1985
1986 /**
1987 * @param $order_id
1988 *
1989 * @return mixed
1990 *
1991 * determine is this a tutor order
1992 *
1993 * @since v.1.0.0
1994 */
1995 public function is_tutor_order($order_id){
1996 return get_post_meta($order_id, '_is_tutor_order_for_course', true);
1997 }
1998
1999 /**
2000 * @return mixed
2001 *
2002 * @deprecated
2003 */
2004 public function tutor_student_dashboard_pages(){
2005 _deprecated_function(__METHOD__, '1.1.2', 'tutor_dashboard_pages');
2006 return $this->tutor_dashboard_pages();
2007 }
2008
2009 /**
2010 * @return mixed
2011 *
2012 * Tutor Dashboard Pages, supporting for the URL rewriting
2013 *
2014 * @since v.1.0.0
2015 */
2016
2017 public function tutor_dashboard_pages(){
2018 $nav_items = apply_filters('tutor_dashboard/nav_items', array(
2019 'index' => __('Dashboard', 'tutor'),
2020 'my-profile' => __('My Profile', 'tutor'),
2021 'create-course' => array('title' => __('Create Course', 'tutor'), 'show_ui' => false, 'auth_cap' => tutor()->instructor_role),
2022 'enrolled-courses' => __('Enrolled Courses', 'tutor'),
2023 'wishlist' => __('Wishlist', 'tutor'),
2024 'reviews' => __('Reviews', 'tutor'),
2025
2026 'my-courses' => array('title' => __('My Courses', 'tutor'), 'auth_cap' => tutor()->instructor_role),
2027 'quiz-attempts' => array('title' => __('Quiz Attempts', 'tutor'), 'auth_cap' => tutor()->instructor_role),
2028 'earning' => array('title' => __('Earning', 'tutor'), 'auth_cap' => tutor()->instructor_role),
2029 'withdraw' => array('title' => __('Withdraw', 'tutor'), 'auth_cap' => tutor()->instructor_role),
2030
2031 'purchase_history' => __('Purchase History', 'tutor'),
2032 ));
2033
2034 $new_navs = array(
2035 'settings' => __('Settings', 'tutor'),
2036 'logout' => __('Logout', 'tutor'),
2037 );
2038
2039 $all_nav_items = array_merge($nav_items, $new_navs);
2040
2041 return apply_filters('tutor_dashboard/nav_items_all', $all_nav_items);
2042 }
2043
2044 /**
2045 * @return mixed
2046 *
2047 * Tutor Dashboard UI nav, only for using in the nav, it's handling user permission based
2048 * Dashboard nav items
2049 *
2050 * @since v.1.3.4
2051 */
2052 public function tutor_dashboard_nav_ui_items(){
2053 $nav_items = $this->tutor_dashboard_pages();
2054
2055 foreach ($nav_items as $key => $nav_item){
2056 if (is_array($nav_item)){
2057
2058 if ( isset($nav_item['show_ui']) && ! tutor_utils()->array_get('show_ui', $nav_item)){
2059 unset($nav_items[$key]);
2060 }
2061 if ( isset($nav_item['auth_cap'] ) && ! current_user_can($nav_item['auth_cap']) ){
2062 unset($nav_items[$key]);
2063 }
2064 }
2065 }
2066
2067 return apply_filters('tutor_dashboard/nav_ui_items', $nav_items);
2068 }
2069
2070 /**
2071 * @param string $page_key
2072 * @param int $page_id
2073 *
2074 * @return string
2075 *
2076 * Get tutor dashboard page single URL
2077 *
2078 * @since v.1.0.0
2079 */
2080 public function get_tutor_dashboard_page_permalink($page_key = '', $page_id = 0){
2081 if ($page_key === 'index'){
2082 $page_key = '';
2083 }
2084 $page_id = $this->get_post_id($page_id);
2085 return trailingslashit(get_permalink($page_id)).$page_key;
2086 }
2087
2088 /**
2089 * @param string $input
2090 *
2091 * @return array|bool|mixed|string
2092 *
2093 * Get old input
2094 *
2095 * @since v.1.0.0
2096 */
2097 public function input_old($input = ''){
2098 $value = $this->avalue_dot($input, $_REQUEST);
2099 if ($value){
2100 return $value;
2101 }
2102 return '';
2103 }
2104
2105 /**
2106 * @param int $user_id
2107 *
2108 * @return mixed
2109 *
2110 * Determine if is instructor or not
2111 *
2112 * @since v.1.0.0
2113 */
2114 public function is_instructor($user_id = 0){
2115 $user_id = $this->get_user_id($user_id);
2116 return get_user_meta($user_id, '_is_tutor_instructor', true);
2117 }
2118
2119 /**
2120 * @param int $user_id
2121 * @param bool $status_name
2122 *
2123 * @return bool|mixed
2124 *
2125 * Instructor status
2126 *
2127 * @since v.1.0.0
2128 */
2129 public function instructor_status($user_id = 0, $status_name = true){
2130 $user_id = $this->get_user_id($user_id);
2131
2132 $instructor_status = apply_filters('tutor_instructor_statuses', array(
2133 'pending' => __('Pending', 'tutor'),
2134 'approved' => __('Approved', 'tutor'),
2135 'blocked' => __('Blocked', 'tutor'),
2136 ));
2137
2138 $status = get_user_meta($user_id, '_tutor_instructor_status', true);
2139
2140 if (isset($instructor_status[$status])){
2141 if ( ! $status_name){
2142 return $status;
2143 }
2144 return $instructor_status[$status];
2145 }
2146 return false;
2147 }
2148
2149 /**
2150 * @param string $search_term
2151 *
2152 * @return int
2153 *
2154 * Get total number of instructors
2155 *
2156 * @since v.1.0.0
2157 */
2158
2159 public function get_total_instructors($search_term = ''){
2160 $meta_key = '_is_tutor_instructor';
2161
2162 global $wpdb;
2163
2164 if ($search_term){
2165 $search_term = " AND ( {$wpdb->users}.display_name LIKE '%{$search_term}%' OR {$wpdb->users}.user_email LIKE '%{$search_term}%' ) ";
2166 }
2167
2168 $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 ");
2169
2170 return (int) $count;
2171 }
2172
2173 /**
2174 * @param int $start
2175 * @param int $limit
2176 * @param string $search_term
2177 *
2178 * @return array|null|object
2179 *
2180 * Get all instructors
2181 *
2182 * @since v.1.0.0
2183 */
2184 public function get_instructors($start = 0, $limit = 10, $search_term = ''){
2185 $meta_key = '_is_tutor_instructor';
2186 global $wpdb;
2187
2188 if ($search_term){
2189 $search_term = " AND ( {$wpdb->users}.display_name LIKE '%{$search_term}%' OR {$wpdb->users}.user_email LIKE '%{$search_term}%' ) ";
2190 }
2191
2192 $instructors = $wpdb->get_results("SELECT SQL_CALC_FOUND_ROWS {$wpdb->users}.* FROM {$wpdb->users}
2193 INNER JOIN {$wpdb->usermeta}
2194 ON ( {$wpdb->users}.ID = {$wpdb->usermeta}.user_id )
2195 WHERE 1=1 AND ( {$wpdb->usermeta}.meta_key = '{$meta_key}' ) {$search_term}
2196 ORDER BY {$wpdb->usermeta}.meta_value DESC
2197 LIMIT {$start}, {$limit} ");
2198
2199 return $instructors;
2200 }
2201
2202 /**
2203 * @param int $course_id
2204 *
2205 * @return array|bool|null|object
2206 *
2207 * Get all instructors by course
2208 *
2209 * @since v.1.0.0
2210 */
2211 public function get_instructors_by_course($course_id = 0){
2212 global $wpdb;
2213 $course_id = $this->get_post_id($course_id);
2214
2215 $instructors = $wpdb->get_results("select ID, display_name,
2216 get_course.meta_value as taught_course_id,
2217 tutor_job_title.meta_value as tutor_profile_job_title,
2218 tutor_bio.meta_value as tutor_profile_bio,
2219 tutor_photo.meta_value as tutor_profile_photo
2220 from {$wpdb->users}
2221 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}
2222 LEFT JOIN {$wpdb->usermeta} tutor_job_title ON ID = tutor_job_title.user_id AND tutor_job_title.meta_key = '_tutor_profile_job_title'
2223 LEFT JOIN {$wpdb->usermeta} tutor_bio ON ID = tutor_bio.user_id AND tutor_bio.meta_key = '_tutor_profile_bio'
2224 LEFT JOIN {$wpdb->usermeta} tutor_photo ON ID = tutor_photo.user_id AND tutor_photo.meta_key = '_tutor_profile_photo'
2225 ");
2226
2227 if (is_array($instructors) && count($instructors)){
2228 return $instructors;
2229 }
2230
2231 return false;
2232 }
2233
2234 /**
2235 * @param $instructor_id
2236 *
2237 * Get total Students by instructor
2238 * 1 enrollment = 1 student, so total enrolled for a equivalent total students (Tricks)
2239 *
2240 * @return int
2241 *
2242 * @since v.1.0.0
2243 */
2244
2245 public function get_total_students_by_instructor($instructor_id){
2246 global $wpdb;
2247
2248 $course_post_type = tutor()->course_post_type;
2249 $count = $wpdb->get_var("SELECT COUNT(courses.ID) from {$wpdb->posts} courses
2250
2251 INNER JOIN {$wpdb->posts} enrolled ON courses.ID = enrolled.post_parent AND enrolled.post_type = 'tutor_enrolled'
2252 WHERE courses.post_status = 'publish'
2253 AND courses.post_type = '{$course_post_type}'
2254 AND courses.post_author = {$instructor_id} ; ");
2255 return (int) $count;
2256 }
2257
2258 /**
2259 * @param float $input
2260 *
2261 * @return float|string
2262 *
2263 * Get rating format from value
2264 *
2265 * @since v.1.0.0
2266 */
2267 public function get_rating_value($input = 0.00){
2268
2269 if ( $input > 0){
2270 $input = number_format($input, 2);
2271 $int_value = (int) $input;
2272 $fraction = $input - $int_value;
2273
2274 if ($fraction == 0){
2275 $fraction = 0.00;
2276 }elseif($fraction > 0.5){
2277 $fraction = 1;
2278 }else{
2279 $fraction = 0.5;
2280 }
2281
2282 return number_format( ($int_value + $fraction), 2);
2283 }
2284 return 0.00;
2285 }
2286
2287 /**
2288 * @param float $current_rating
2289 * @param bool $echo
2290 *
2291 * @return string
2292 *
2293 * Generate star rating based in given rating value
2294 *
2295 * @since v.1.0.0
2296 */
2297 public function star_rating_generator($current_rating = 0.00, $echo = true){
2298 $output = '<div class="tutor-star-rating-group">';
2299
2300 for ($i = 1; $i <=5 ; $i++){
2301 $intRating = (int) $current_rating;
2302
2303 if ($intRating >= $i){
2304 $output.= '<i class="tutor-icon-star-full" data-rating-value="'.$i.'"></i>';
2305 } else{
2306 if ( ($current_rating - $i) == -0.5){
2307 $output.= '<i class="tutor-icon-star-half" data-rating-value="'.$i.'"></i>';
2308 }else{
2309 $output.= '<i class="tutor-icon-star-line" data-rating-value="'.$i.'"></i>';
2310 }
2311 }
2312 }
2313
2314 $output .= "<div class='tutor-rating-gen-input'><input type='hidden' name='tutor_rating_gen_input' value='{$current_rating}' /> </div>";
2315
2316 $output .= "</div>";
2317
2318 if ($echo){
2319 echo $output;
2320 }
2321 return $output;
2322 }
2323
2324 /**
2325 * @param null $name
2326 *
2327 * @return string
2328 *
2329 * Generate text to avatar
2330 *
2331 * @since v.1.0.0
2332 */
2333 public function get_tutor_avatar($user_id = null, $size = 'thumbnail'){
2334 global $wpdb;
2335
2336 if ( ! $user_id){
2337 return '';
2338 }
2339
2340 $user = $this->get_tutor_user($user_id);
2341 if ($user->tutor_profile_photo){
2342 return '<img src="'.wp_get_attachment_image_url($user->tutor_profile_photo, $size).'" class="tutor-image-avatar" alt="" /> ';
2343 }
2344
2345 $name = $user->display_name;
2346 $arr = explode(' ', trim($name));
2347
2348 if (count($arr) > 1){
2349 $first_char = substr($arr[0], 0, 1) ;
2350 $second_char = substr($arr[1], 0, 1) ;
2351 }else{
2352 $first_char = substr($arr[0], 0, 1) ;
2353 $second_char = substr($arr[0], 1, 1) ;
2354 }
2355
2356 $initial_avatar = strtoupper($first_char.$second_char);
2357
2358 $bg_color = '#'.substr(md5($initial_avatar), 0, 6);
2359 $initial_avatar = "<span class='tutor-text-avatar' style='background-color: {$bg_color}; color: #fff8e5'>{$initial_avatar}</span>";
2360
2361 return $initial_avatar;
2362 }
2363
2364 /**
2365 * @param $user_id
2366 *
2367 * @return array|null|object|void
2368 *
2369 * Get tutor user
2370 *
2371 * @since v.1.0.0
2372 */
2373
2374 public function get_tutor_user($user_id){
2375 global $wpdb;
2376
2377 $user = $wpdb->get_row("select ID, display_name,
2378 tutor_job_title.meta_value as tutor_profile_job_title,
2379 tutor_bio.meta_value as tutor_profile_bio,
2380 tutor_photo.meta_value as tutor_profile_photo
2381
2382 from {$wpdb->users}
2383 LEFT JOIN {$wpdb->usermeta} tutor_job_title ON ID = tutor_job_title.user_id AND tutor_job_title.meta_key = '_tutor_profile_job_title'
2384 LEFT JOIN {$wpdb->usermeta} tutor_bio ON ID = tutor_bio.user_id AND tutor_bio.meta_key = '_tutor_profile_bio'
2385 LEFT JOIN {$wpdb->usermeta} tutor_photo ON ID = tutor_photo.user_id AND tutor_photo.meta_key = '_tutor_profile_photo'
2386
2387 WHERE ID = {$user_id} ");
2388 return $user;
2389 }
2390
2391 /**
2392 * @param int $course_id
2393 * @param int $offset
2394 * @param int $limit
2395 *
2396 * @return array|null|object
2397 *
2398 * get course reviews
2399 *
2400 * @since v.1.0.0
2401 */
2402 public function get_course_reviews($course_id = 0, $offset = 0, $limit = 150){
2403 $course_id = $this->get_post_id($course_id);
2404 global $wpdb;
2405
2406 $reviews = $wpdb->get_results("select {$wpdb->comments}.comment_ID,
2407 {$wpdb->comments}.comment_post_ID,
2408 {$wpdb->comments}.comment_author,
2409 {$wpdb->comments}.comment_author_email,
2410 {$wpdb->comments}.comment_date,
2411 {$wpdb->comments}.comment_content,
2412 {$wpdb->comments}.user_id,
2413 {$wpdb->commentmeta}.meta_value as rating,
2414 {$wpdb->users}.display_name
2415
2416 from {$wpdb->comments}
2417 INNER JOIN {$wpdb->commentmeta}
2418 ON {$wpdb->comments}.comment_ID = {$wpdb->commentmeta}.comment_id
2419 INNER JOIN {$wpdb->users}
2420 ON {$wpdb->comments}.user_id = {$wpdb->users}.ID
2421 WHERE {$wpdb->comments}.comment_post_ID = {$course_id}
2422 AND comment_type = 'tutor_course_rating' AND meta_key = 'tutor_rating' ORDER BY comment_ID DESC LIMIT {$offset},{$limit} ;"
2423 );
2424
2425 return $reviews;
2426 }
2427
2428 /**
2429 * @param int $course_id
2430 *
2431 * @return object
2432 *
2433 * Get course rating
2434 *
2435 * @since v.1.0.0
2436 */
2437 public function get_course_rating($course_id = 0){
2438 $course_id = $this->get_post_id($course_id);
2439
2440 $ratings = array(
2441 'rating_count' => 0,
2442 'rating_sum' => 0,
2443 'rating_avg' => 0.00,
2444 'count_by_value' => array(5 => 0, 4 => 0, 3 => 0, 2 => 0, 1 => 0)
2445 );
2446
2447 global $wpdb;
2448
2449 $rating = $wpdb->get_row("select COUNT(meta_value) as rating_count, SUM(meta_value) as rating_sum
2450 from {$wpdb->comments}
2451 INNER JOIN {$wpdb->commentmeta}
2452 ON {$wpdb->comments}.comment_ID = {$wpdb->commentmeta}.comment_id
2453 WHERE {$wpdb->comments}.comment_post_ID = {$course_id}
2454 AND {$wpdb->comments}.comment_type = 'tutor_course_rating'
2455 AND meta_key = 'tutor_rating' ;"
2456 );
2457
2458 if ($rating->rating_count){
2459 $avg_rating = number_format(($rating->rating_sum / $rating->rating_count), 2);
2460
2461 /**
2462 * Get individual Rating by integer
2463 */
2464 $five_stars_count = $wpdb->get_var("select COUNT(meta_value) as rating_count
2465 from {$wpdb->comments}
2466 INNER JOIN {$wpdb->commentmeta} ON {$wpdb->comments}.comment_ID = {$wpdb->commentmeta}.comment_id
2467 WHERE {$wpdb->comments}.comment_post_ID = {$course_id}
2468 AND {$wpdb->comments}.comment_type = 'tutor_course_rating'
2469 AND meta_key = 'tutor_rating' AND meta_value = 5 ;"
2470 );
2471 $four_stars_count = $wpdb->get_var("select COUNT(meta_value) as rating_count
2472 from {$wpdb->comments}
2473 INNER JOIN {$wpdb->commentmeta} ON {$wpdb->comments}.comment_ID = {$wpdb->commentmeta}.comment_id
2474 WHERE {$wpdb->comments}.comment_post_ID = {$course_id}
2475 AND {$wpdb->comments}.comment_type = 'tutor_course_rating'
2476 AND meta_key = 'tutor_rating' AND meta_value = 4 ;"
2477 );
2478 $three_stars_count = $wpdb->get_var("select COUNT(meta_value) as rating_count
2479 from {$wpdb->comments}
2480 INNER JOIN {$wpdb->commentmeta} ON {$wpdb->comments}.comment_ID = {$wpdb->commentmeta}.comment_id
2481 WHERE {$wpdb->comments}.comment_post_ID = {$course_id}
2482 AND {$wpdb->comments}.comment_type = 'tutor_course_rating'
2483 AND meta_key = 'tutor_rating' AND meta_value = 3 ;"
2484 );
2485 $two_stars_count = $wpdb->get_var("select COUNT(meta_value) as rating_count
2486 from {$wpdb->comments}
2487 INNER JOIN {$wpdb->commentmeta} ON {$wpdb->comments}.comment_ID = {$wpdb->commentmeta}.comment_id
2488 WHERE {$wpdb->comments}.comment_post_ID = {$course_id}
2489 AND {$wpdb->comments}.comment_type = 'tutor_course_rating'
2490 AND meta_key = 'tutor_rating' AND meta_value = 2 ;"
2491 );
2492 $one_stars_count = $wpdb->get_var("select COUNT(meta_value) as rating_count
2493 from {$wpdb->comments}
2494 INNER JOIN {$wpdb->commentmeta} ON {$wpdb->comments}.comment_ID = {$wpdb->commentmeta}.comment_id
2495 WHERE {$wpdb->comments}.comment_post_ID = {$course_id}
2496 AND {$wpdb->comments}.comment_type = 'tutor_course_rating'
2497 AND meta_key = 'tutor_rating' AND meta_value = 1 ;"
2498 );
2499
2500 $ratings = array(
2501 'rating_count' => $rating->rating_count,
2502 'rating_sum' => $rating->rating_sum,
2503 'rating_avg' => $avg_rating,
2504 'count_by_value' => array(5 => $five_stars_count, 4 => $four_stars_count, 3 => $three_stars_count, 2 => $two_stars_count, 1 => $one_stars_count)
2505 );
2506
2507 }
2508
2509 return (object) $ratings;
2510 }
2511
2512 /**
2513 * @param int $user_id
2514 * @param int $offset
2515 * @param int $limit
2516 *
2517 * @return array|null|object
2518 *
2519 * Get reviews by a user
2520 *
2521 * @since v.1.0.0
2522 */
2523 public function get_reviews_by_user($user_id = 0, $offset = 0, $limit = 150){
2524 $user_id = $this->get_user_id($user_id);
2525 global $wpdb;
2526
2527 $reviews = $wpdb->get_results("select {$wpdb->comments}.comment_ID,
2528 {$wpdb->comments}.comment_post_ID,
2529 {$wpdb->comments}.comment_author,
2530 {$wpdb->comments}.comment_author_email,
2531 {$wpdb->comments}.comment_date,
2532 {$wpdb->comments}.comment_content,
2533 {$wpdb->comments}.user_id,
2534 {$wpdb->commentmeta}.meta_value as rating,
2535 {$wpdb->users}.display_name
2536
2537 from {$wpdb->comments}
2538 INNER JOIN {$wpdb->commentmeta}
2539 ON {$wpdb->comments}.comment_ID = {$wpdb->commentmeta}.comment_id
2540 INNER JOIN {$wpdb->users}
2541 ON {$wpdb->comments}.user_id = {$wpdb->users}.ID
2542 WHERE {$wpdb->comments}.user_id = {$user_id}
2543 AND comment_type = 'tutor_course_rating' AND meta_key = 'tutor_rating' ORDER BY comment_ID DESC LIMIT {$offset},{$limit} ;"
2544 );
2545
2546 return $reviews;
2547 }
2548
2549 /**
2550 * @param $instructor_id
2551 *
2552 * @return object
2553 *
2554 * Get instructors rating
2555 *
2556 * @since v.1.0.0
2557 */
2558 public function get_instructor_ratings($instructor_id){
2559 global $wpdb;
2560
2561 $ratings = array(
2562 'rating_count' => 0,
2563 'rating_sum' => 0,
2564 'rating_avg' => 0.00,
2565 );
2566
2567 $rating = $wpdb->get_row("SELECT COUNT(rating.meta_value) as rating_count, SUM(rating.meta_value) as rating_sum
2568 FROM {$wpdb->usermeta} courses
2569 INNER JOIN {$wpdb->comments} reviews ON courses.meta_value = reviews.comment_post_ID AND reviews.comment_type = 'tutor_course_rating'
2570 INNER JOIN {$wpdb->commentmeta} rating ON reviews.comment_ID = rating.comment_id AND rating.meta_key = 'tutor_rating'
2571 WHERE courses.user_id = {$instructor_id} AND courses.meta_key = '_tutor_instructor_course_id'");
2572
2573 if ($rating->rating_count){
2574 $avg_rating = number_format(($rating->rating_sum / $rating->rating_count), 2);
2575
2576 $ratings = array(
2577 'rating_count' => $rating->rating_count,
2578 'rating_sum' => $rating->rating_sum,
2579 'rating_avg' => $avg_rating,
2580 );
2581 }
2582
2583 return (object) $ratings;
2584 }
2585
2586 /**
2587 * @param int $course_id
2588 * @param int $user_id
2589 *
2590 * @return object
2591 *
2592 * Get course rating by user
2593 *
2594 * @since v.1.0.0
2595 */
2596 public function get_course_rating_by_user($course_id = 0, $user_id = 0){
2597 $course_id = $this->get_post_id($course_id);
2598 $user_id = $this->get_user_id($user_id);
2599
2600 $ratings = array(
2601 'rating' => 0,
2602 'review' => '',
2603 );
2604
2605 global $wpdb;
2606
2607 $rating = $wpdb->get_row("select meta_value as rating, comment_content as review from {$wpdb->comments}
2608 INNER JOIN {$wpdb->commentmeta}
2609 ON {$wpdb->comments}.comment_ID = {$wpdb->commentmeta}.comment_id
2610 WHERE {$wpdb->comments}.comment_post_ID = {$course_id} AND user_id = {$user_id}
2611 AND meta_key = 'tutor_rating' ;"
2612 );
2613
2614 if ($rating){
2615 $rating_format = number_format($rating->rating, 2);
2616
2617 $ratings = array(
2618 'rating' => $rating_format,
2619 'review' => $rating->review,
2620 );
2621 }
2622 return (object) $ratings;
2623 }
2624
2625 /**
2626 * @param int $user_id
2627 *
2628 * @return null|string
2629 *
2630 * @since v.1.0.0
2631 */
2632 public function count_reviews_wrote_by_user($user_id = 0){
2633 global $wpdb;
2634 $user_id = $this->get_user_id($user_id);
2635
2636 $count_reviews = $wpdb->get_var("SELECT COUNT(comment_ID) from {$wpdb->comments} WHERE user_id = {$user_id} AND comment_type = 'tutor_course_rating' ");
2637 return $count_reviews;
2638 }
2639
2640 /**
2641 * @param $size
2642 *
2643 * @return bool|int|string
2644 *
2645 * This function transforms the php.ini notation for numbers (like '2M') to an integer.
2646 *
2647 * @since v.1.0.0
2648 */
2649
2650 function let_to_num( $size ) {
2651 $l = substr( $size, -1 );
2652 $ret = substr( $size, 0, -1 );
2653 $byte = 1024;
2654
2655 switch ( strtoupper( $l ) ) {
2656 case 'P':
2657 $ret *= 1024;
2658 // No break.
2659 case 'T':
2660 $ret *= 1024;
2661 // No break.
2662 case 'G':
2663 $ret *= 1024;
2664 // No break.
2665 case 'M':
2666 $ret *= 1024;
2667 // No break.
2668 case 'K':
2669 $ret *= 1024;
2670 // No break.
2671 }
2672 return $ret;
2673 }
2674
2675 /**
2676 * @return array
2677 *
2678 * Get Database version
2679 *
2680 * @since v.1.0.0
2681 */
2682 function get_db_version() {
2683 global $wpdb;
2684
2685 if ( empty( $wpdb->is_mysql ) ) {
2686 return array(
2687 'string' => '',
2688 'number' => '',
2689 );
2690 }
2691
2692 if ( $wpdb->use_mysqli ) {
2693 $server_info = mysqli_get_server_info( $wpdb->dbh ); // @codingStandardsIgnoreLine.
2694 } else {
2695 $server_info = mysql_get_server_info( $wpdb->dbh ); // @codingStandardsIgnoreLine.
2696 }
2697
2698 return array(
2699 'string' => $server_info,
2700 'number' => preg_replace( '/([^\d.]+).*/', '', $server_info ),
2701 );
2702 }
2703
2704 public function help_tip($tip = ''){
2705 return '<span class="tutor-help-tip" data-tip="' . $tip . '"></span>';
2706 }
2707
2708 /**
2709 * @param int $course_id
2710 * @param int $user_id
2711 * @param int $offset
2712 * @param int $limit
2713 *
2714 * @return array|null|object
2715 *
2716 * Get top question
2717 *
2718 * @since v.1.0.0
2719 */
2720 public function get_top_question($course_id = 0, $user_id = 0, $offset = 0, $limit = 20){
2721 $course_id = $this->get_post_id($course_id);
2722 $user_id = $this->get_user_id($user_id);
2723
2724 global $wpdb;
2725
2726 $questions = $wpdb->get_results("select {$wpdb->comments}.comment_ID,
2727 {$wpdb->comments}.comment_post_ID,
2728 {$wpdb->comments}.comment_author,
2729 {$wpdb->comments}.comment_date,
2730 {$wpdb->comments}.comment_content,
2731 {$wpdb->comments}.user_id,
2732 {$wpdb->commentmeta}.meta_value as question_title,
2733 {$wpdb->users}.display_name
2734
2735 from {$wpdb->comments}
2736 INNER JOIN {$wpdb->commentmeta}
2737 ON {$wpdb->comments}.comment_ID = {$wpdb->commentmeta}.comment_id
2738 INNER JOIN {$wpdb->users}
2739 ON {$wpdb->comments}.user_id = {$wpdb->users}.ID
2740 WHERE {$wpdb->comments}.comment_post_ID = {$course_id}
2741 AND {$wpdb->comments}.user_id = {$user_id}
2742 AND {$wpdb->comments}.comment_type = 'tutor_q_and_a'
2743 AND meta_key = 'tutor_question_title' ORDER BY comment_ID DESC LIMIT {$offset},{$limit} ;"
2744 );
2745
2746 return $questions;
2747 }
2748
2749 /**
2750 * @param string $search_term
2751 *
2752 * @return int
2753 *
2754 * Get total number of Q&A questions
2755 *
2756 * @since v.1.0.0
2757 */
2758 public function get_total_qa_question($search_term = ''){
2759 global $wpdb;
2760
2761 if ($search_term){
2762 $search_term = " AND {$wpdb->commentmeta}.meta_value LIKE '%{$search_term}%' ";
2763 }
2764
2765 $user_id = get_current_user_id();
2766 $course_type = tutor()->course_post_type;
2767
2768 $in_question_id_query = '';
2769 /**
2770 * Get only assinged courses questions if current user is a
2771 */
2772 if ( ! current_user_can('administrator') && current_user_can(tutor()->instructor_role)) {
2773 $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' " );
2774 $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} " );
2775 $my_course_ids = array_unique( array_merge( $get_course_ids, $get_assigned_courses_ids ) );
2776
2777 if ( $this->count( $my_course_ids ) ) {
2778 $implode_ids = implode( ',', $my_course_ids );
2779 $in_question_id_query = " AND {$wpdb->comments}.comment_post_ID IN($implode_ids) ";
2780 }
2781 }
2782
2783 $count = $wpdb->get_var("SELECT COUNT({$wpdb->comments}.comment_ID) FROM {$wpdb->comments}
2784 INNER JOIN {$wpdb->commentmeta}
2785 ON {$wpdb->comments}.comment_ID = {$wpdb->commentmeta}.comment_id
2786 WHERE comment_type = 'tutor_q_and_a' AND comment_parent = 0 {$in_question_id_query} {$search_term} ");
2787
2788 return (int) $count;
2789 }
2790
2791 /**
2792 * @param int $start
2793 * @param int $limit
2794 * @param string $search_term
2795 *
2796 * @return array|null|object
2797 *
2798 *
2799 * Get question and answer query
2800 *
2801 * @since v.1.0.0
2802 */
2803 public function get_qa_questions($start = 0, $limit = 10, $search_term = '') {
2804 global $wpdb;
2805
2806 if ($search_term){
2807 $search_term = " AND {$wpdb->commentmeta}.meta_value LIKE '%{$search_term}%' ";
2808 }
2809
2810 $user_id = get_current_user_id();
2811 $course_type = tutor()->course_post_type;
2812
2813 $in_question_id_query = '';
2814 /**
2815 * Get only assinged courses questions if current user is a
2816 */
2817 if ( ! current_user_can('administrator') && current_user_can(tutor()->instructor_role)) {
2818 $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' " );
2819 $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} " );
2820 $my_course_ids = array_unique( array_merge( $get_course_ids, $get_assigned_courses_ids ) );
2821
2822 if ( $this->count( $my_course_ids ) ) {
2823 $implode_ids = implode( ',', $my_course_ids );
2824 $in_question_id_query = " AND {$wpdb->comments}.comment_post_ID IN($implode_ids) ";
2825 }
2826 }
2827
2828 $query = $wpdb->get_results("SELECT
2829 {$wpdb->comments}.comment_ID,
2830 {$wpdb->comments}.comment_post_ID,
2831 {$wpdb->comments}.comment_author,
2832 {$wpdb->comments}.comment_date,
2833 {$wpdb->comments}.comment_content,
2834 {$wpdb->comments}.user_id,
2835 {$wpdb->commentmeta}.meta_value as question_title,
2836 {$wpdb->users}.display_name,
2837 {$wpdb->posts}.post_title,
2838
2839 (SELECT COUNT(answers_t.comment_ID) FROM {$wpdb->comments} answers_t
2840 WHERE answers_t.comment_parent = {$wpdb->comments}.comment_ID ) as answer_count
2841
2842 FROM {$wpdb->comments}
2843
2844 INNER JOIN {$wpdb->commentmeta}
2845 ON {$wpdb->comments}.comment_ID = {$wpdb->commentmeta}.comment_id
2846
2847 INNER JOIN {$wpdb->posts}
2848 ON {$wpdb->comments}.comment_post_ID = {$wpdb->posts}.ID
2849
2850 INNER JOIN {$wpdb->users}
2851 ON {$wpdb->comments}.user_id = {$wpdb->users}.ID
2852
2853 WHERE {$wpdb->comments}.comment_type = 'tutor_q_and_a' AND {$wpdb->comments}.comment_parent = 0 {$search_term}
2854 {$in_question_id_query}
2855 ORDER BY {$wpdb->comments}.comment_ID DESC
2856 LIMIT {$start},{$limit}; ");
2857
2858 return $query;
2859 }
2860
2861 /**
2862 * @param $question_id
2863 *
2864 * @return array|null|object|void
2865 *
2866 * Get question for Q&A
2867 *
2868 * @since v.1.0.0
2869 */
2870 public function get_qa_question($question_id){
2871 global $wpdb;
2872 $query = $wpdb->get_row("SELECT
2873 {$wpdb->comments}.comment_ID,
2874 {$wpdb->comments}.comment_post_ID,
2875 {$wpdb->comments}.comment_author,
2876 {$wpdb->comments}.comment_date,
2877 {$wpdb->comments}.comment_content,
2878 {$wpdb->comments}.user_id,
2879 {$wpdb->commentmeta}.meta_value as question_title,
2880 {$wpdb->users}.display_name,
2881 {$wpdb->posts}.post_title
2882
2883 FROM {$wpdb->comments}
2884 INNER JOIN {$wpdb->commentmeta}
2885 ON {$wpdb->comments}.comment_ID = {$wpdb->commentmeta}.comment_id
2886
2887 INNER JOIN {$wpdb->posts}
2888 ON {$wpdb->comments}.comment_post_ID = {$wpdb->posts}.ID
2889
2890 INNER JOIN {$wpdb->users}
2891 ON {$wpdb->comments}.user_id = {$wpdb->users}.ID
2892 WHERE comment_type = 'tutor_q_and_a' AND {$wpdb->comments}.comment_ID = {$question_id}");
2893
2894 return $query;
2895 }
2896
2897 /**
2898 * @param $question_id
2899 *
2900 * @return array|null|object
2901 *
2902 * Get question and asnwer by question
2903 */
2904 public function get_qa_answer_by_question($question_id){
2905 global $wpdb;
2906 $query = $wpdb->get_results("SELECT
2907 {$wpdb->comments}.comment_ID,
2908 {$wpdb->comments}.comment_post_ID,
2909 {$wpdb->comments}.comment_author,
2910 {$wpdb->comments}.comment_date,
2911 {$wpdb->comments}.comment_content,
2912 {$wpdb->comments}.comment_parent,
2913 {$wpdb->comments}.user_id,
2914 {$wpdb->users}.display_name
2915
2916 FROM {$wpdb->comments}
2917
2918 INNER JOIN {$wpdb->users}
2919 ON {$wpdb->comments}.user_id = {$wpdb->users}.ID
2920 WHERE comment_type = 'tutor_q_and_a'
2921 AND {$wpdb->comments}.comment_parent = {$question_id} ORDER BY {$wpdb->comments}.comment_ID ASC ");
2922
2923 return $query;
2924 }
2925
2926 public function unanswered_question_count(){
2927 global $wpdb;
2928
2929 $count = $wpdb->get_var("select COUNT({$wpdb->comments}.comment_ID)
2930 from {$wpdb->comments}
2931 WHERE {$wpdb->comments}.comment_type = 'tutor_q_and_a'
2932 AND {$wpdb->comments}.comment_approved = 'waiting_for_answer'
2933 AND {$wpdb->comments}.comment_parent = 0;");
2934 return (int) $count;
2935 }
2936
2937 /**
2938 * @param int $course_id
2939 *
2940 * @return array|null|object
2941 *
2942 * Return all of announcements for a course
2943 *
2944 * @since v.1.0.0
2945 */
2946 public function get_announcements($course_id = 0){
2947 $course_id = $this->get_post_id($course_id);
2948 global $wpdb;
2949
2950 $query = $wpdb->get_results("select {$wpdb->posts}.ID, post_author, post_date, post_content, post_title, display_name
2951 from {$wpdb->posts}
2952 INNER JOIN {$wpdb->users} ON post_author = {$wpdb->users}.ID
2953 WHERE post_type = 'tutor_announcements'
2954 AND post_parent = {$course_id} ORDER BY {$wpdb->posts}.ID DESC;");
2955 return $query;
2956 }
2957
2958 /**
2959 * @param string $content
2960 *
2961 * @return mixed
2962 *
2963 * Announcement content
2964 *
2965 * @since v.1.0.0
2966 */
2967
2968 public function announcement_content($content = ''){
2969 $search = array('{user_display_name}');
2970
2971 $user_display_name = 'User';
2972 if (is_user_logged_in()){
2973 $user = wp_get_current_user();
2974 $user_display_name = $user->display_name;
2975 }
2976 $replace = array($user_display_name);
2977
2978 return str_replace($search, $replace, $content);
2979 }
2980
2981 /**
2982 * @param int $post_id
2983 * @param string $option_key
2984 * @param bool $default
2985 *
2986 * @return array|bool|mixed
2987 *
2988 * Get the quiz option from meta
2989 */
2990 public function get_quiz_option($post_id = 0, $option_key = '', $default = false){
2991 $post_id = $this->get_post_id($post_id);
2992 $get_option_meta = maybe_unserialize(get_post_meta($post_id, 'tutor_quiz_option', true));
2993
2994 if ( ! $option_key && ! empty($get_option_meta)) {
2995 return $get_option_meta;
2996 }
2997
2998 $value = $this->avalue_dot( $option_key, $get_option_meta );
2999 if ( $value ) {
3000 return $value;
3001 }
3002 return $default;
3003 }
3004
3005
3006 /**
3007 * @param int $quiz_id
3008 *
3009 * @return array|bool|null|object
3010 *
3011 * Get the questions by quiz ID
3012 */
3013 public function get_questions_by_quiz($quiz_id = 0){
3014 $quiz_id = $this->get_post_id($quiz_id);
3015 global $wpdb;
3016
3017 //$questions = $wpdb->get_results("SELECT ID, post_content, post_title, post_parent from {$wpdb->posts} WHERE post_type = 'tutor_question'
3018 // AND post_parent = {$quiz_id} ORDER BY menu_order ASC ");
3019
3020 $questions = $wpdb->get_results("SELECT * from {$wpdb->prefix}tutor_quiz_questions WHERE quiz_id = {$quiz_id} ORDER BY question_order ASC ");
3021
3022 if (is_array($questions) && count($questions)){
3023 return $questions;
3024 }
3025 return false;
3026 }
3027
3028 /**
3029 * @param int $question_id
3030 *
3031 * @return array|bool|object|void|null
3032 *
3033 * Get Quiz question by question id
3034 */
3035 public function get_quiz_question_by_id($question_id = 0){
3036 global $wpdb;
3037
3038 if ($question_id){
3039 $question = $wpdb->get_row("SELECT * from {$wpdb->prefix}tutor_quiz_questions WHERE question_id = {$question_id} LIMIT 0,1 ;");
3040 return $question;
3041 }
3042
3043 return false;
3044 }
3045
3046 /**
3047 * @param null $type
3048 *
3049 * @return array|mixed
3050 *
3051 * Get all question types
3052 *
3053 * @since v.1.0.0
3054 */
3055
3056 public function get_question_types($type = null){
3057 $types = array(
3058 'true_false' => array('name' => __('True/False', 'tutor'), 'icon' => '<i class="tutor-icon-block tutor-icon-yes-no"></i>', 'is_pro' => false),
3059 'single_choice' => array('name' => __('Single Choice', 'tutor'), 'icon' => '<i class="tutor-icon-block tutor-icon-mark"></i>', 'is_pro' => false),
3060 'multiple_choice' => array('name' => __('Multiple Choice', 'tutor'), 'icon' => '<i class="tutor-icon-block tutor-icon-multiple-choice"></i>', 'is_pro' => false),
3061 'open_ended' => array('name' => __('Open Ended/Essay', 'tutor'), 'icon' => '<i class="tutor-icon-block tutor-icon-open-ended"></i>', 'is_pro' => false),
3062 'fill_in_the_blank' => array('name' => __('Fill In The Blank', 'tutor'), 'icon' => '<i class="tutor-icon-block tutor-icon-fill-gaps"></i>', 'is_pro' => false),
3063 'short_answer' => array('name' => __('Short Answer', 'tutor'), 'icon' => '<i class="tutor-icon-block tutor-icon-short-ans"></i>', 'is_pro' => true),
3064 'matching' => array('name' => __('Matching', 'tutor'), 'icon' => '<i class="tutor-icon-block tutor-icon-matching"></i>', 'is_pro' => true),
3065 'image_matching' => array('name' => __('Image Matching', 'tutor'), 'icon' => '<i class="tutor-icon-block tutor-icon-image-matching"></i>', 'is_pro' => true),
3066 'image_answering' => array('name' => __('Image Answering', 'tutor'), 'icon' => '<i class="tutor-icon-block tutor-icon-image-ans"></i>', 'is_pro' => true),
3067 'ordering' => array('name' => __('Ordering', 'tutor'), 'icon' => '<i class="tutor-icon-block tutor-icon-ordering"></i>', 'is_pro' => true),
3068 );
3069
3070 if (isset($types[$type])){
3071 return $types[$type];
3072 }
3073 return $types;
3074 }
3075
3076 public function get_quiz_answer_options_by_question($question_id){
3077 global $wpdb;
3078
3079 $answer_options = $wpdb->get_results("select
3080 {$wpdb->comments}.comment_ID,
3081 {$wpdb->comments}.comment_post_ID,
3082 {$wpdb->comments}.comment_content
3083
3084 FROM {$wpdb->comments}
3085 WHERE {$wpdb->comments}.comment_post_ID = {$question_id}
3086 AND {$wpdb->comments}.comment_type = 'quiz_answer_option'
3087 ORDER BY {$wpdb->comments}.comment_karma ASC ;");
3088
3089 if (is_array($answer_options) && count($answer_options)){
3090 return $answer_options;
3091 }
3092 return false;
3093 }
3094
3095 /**
3096 * @param $quiz_id
3097 *
3098 * @return int
3099 *
3100 * Get the next question order ID
3101 *
3102 * @since v.1.0.0
3103 */
3104
3105 public function quiz_next_question_order_id($quiz_id){
3106 global $wpdb;
3107
3108 //$last_order = (int) $wpdb->get_var("SELECT MAX(menu_order) FROM {$wpdb->posts} WHERE post_parent = {$quiz_id} AND post_type =
3109 // 'tutor_question';");
3110
3111 $last_order = (int) $wpdb->get_var("SELECT MAX(question_order) FROM {$wpdb->prefix}tutor_quiz_questions WHERE quiz_id = {$quiz_id} ;");
3112 return $last_order + 1;
3113 }
3114
3115 /**
3116 * @param $quiz_id
3117 *
3118 * @return int
3119 *
3120 * new design quiz question
3121 * @since v.1.0.0
3122 */
3123 public function quiz_next_question_id(){
3124 global $wpdb;
3125
3126 $last_order = (int) $wpdb->get_var("SELECT MAX(question_id) FROM {$wpdb->prefix}tutor_quiz_questions;");
3127 return $last_order + 1;
3128 }
3129
3130 public function get_quiz_id_by_question($question_id){
3131 global $wpdb;
3132
3133 $quiz_id = $wpdb->get_var("SELECT post_parent FROM {$wpdb->posts} WHERE ID = {$question_id} AND post_type = 'tutor_question' ;");
3134 return $quiz_id;
3135 }
3136
3137 /**
3138 * @param array $config
3139 *
3140 * @return array|bool|null|object
3141 *
3142 * It was used in previous quiz algorithm
3143 *
3144 * @deprecated
3145 *
3146 * @since v.1.0.0
3147 */
3148 public function get_unattached_quiz($config = array()){
3149 global $wpdb;
3150
3151 $default_attr = array(
3152 'search_term' => '',
3153 'start' => '0',
3154 'limit' => '10',
3155 'order' => 'DESC',
3156 'order_by' => 'ID',
3157 );
3158 $attr = array_merge($default_attr, $config);
3159 extract($attr);
3160
3161 $search_query = '';
3162 if (! empty($search_term)){
3163 $search_query = "AND post_title LIKE '%{$search_term}%'";
3164 }
3165
3166 $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} ");
3167
3168 if (is_array($questions) && count($questions)){
3169 return $questions;
3170 }
3171 return false;
3172 }
3173
3174 /**
3175 * @param int $post_id
3176 *
3177 * @return array|bool|null|object
3178 *
3179 * @since v.1.0.0
3180 */
3181 public function get_attached_quiz($post_id = 0){
3182 global $wpdb;
3183
3184 $post_id = $this->get_post_id($post_id);
3185
3186 $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}");
3187
3188 if (is_array($questions) && count($questions)){
3189 return $questions;
3190 }
3191 return false;
3192 }
3193
3194 /**
3195 * @param $quiz_id
3196 *
3197 * @return array|bool|null|object|void
3198 *
3199 * Get course by quiz
3200 *
3201 * @since v.1.0.0
3202 */
3203
3204 public function get_course_by_quiz($quiz_id){
3205 global $wpdb;
3206
3207 $quiz_id = $this->get_post_id($quiz_id);
3208 $post = get_post($quiz_id);
3209
3210 if ($post) {
3211 $course_post_type = tutor()->course_post_type;
3212 $course = $wpdb->get_row( "select ID, post_name, post_type, post_parent from {$wpdb->posts} where ID = {$post->post_parent} " );
3213
3214 if ($course) {
3215 //Checking if this topic
3216 if ( $course->post_type !== $course_post_type ) {
3217 $course = $wpdb->get_row( "select ID, post_name, post_type, post_parent from {$wpdb->posts} where ID = {$course->post_parent} " );
3218 }
3219 //Checking if this lesson
3220 if ( $course->post_type !== $course_post_type ) {
3221 $course = $wpdb->get_row( "select ID, post_name, post_type, post_parent from {$wpdb->posts} where ID = {$course->post_parent} " );
3222 }
3223
3224 return $course;
3225 }
3226 }
3227
3228 return false;
3229 }
3230
3231 /**
3232 * @param $quiz_id
3233 *
3234 * @return int
3235 *
3236 * @since v.1.0.0
3237 */
3238 public function total_questions_for_student_by_quiz($quiz_id){
3239 $quiz_id = $this->get_post_id($quiz_id);
3240 global $wpdb;
3241
3242 $total_question = (int) $wpdb->get_var("select count(ID) from {$wpdb->posts} where post_parent = {$quiz_id} AND post_type = 'tutor_question' ");
3243
3244 return $total_question;
3245 }
3246
3247 /**
3248 * @param int $quiz_id
3249 *
3250 * @return array|null|object|void
3251 *
3252 * Determine if there is any started quiz exists
3253 *
3254 * @since v.1.0.0
3255 */
3256
3257 public function is_started_quiz($quiz_id = 0){
3258 global $wpdb;
3259
3260 $quiz_id = $this->get_post_id($quiz_id);
3261 $user_id = get_current_user_id();
3262
3263 $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' ");
3264
3265 return $is_started;
3266 }
3267
3268 /**
3269 * @param $quiz_id
3270 *
3271 * Method for get the total amount of question for a quiz
3272 * Student will answer this amount of question, one quiz have many question
3273 * but student will answer a specific amount of questions
3274 *
3275 * @return int
3276 *
3277 * @since v.1.0.0
3278 */
3279
3280 public function max_questions_for_take_quiz($quiz_id){
3281 $quiz_id = $this->get_post_id($quiz_id);
3282 global $wpdb;
3283
3284 $max_questions = (int) $wpdb->get_var("select count(question_id) from {$wpdb->prefix}tutor_quiz_questions where quiz_id = {$quiz_id} ");
3285 $max_mentioned = (int) $this->get_quiz_option($quiz_id, 'max_questions_for_answer', 10);
3286
3287 if ($max_mentioned < $max_questions ){
3288 return $max_mentioned;
3289 }
3290
3291 return $max_questions;
3292 }
3293
3294 /**
3295 * @param int $attempt_id
3296 *
3297 * @return array|bool|null|object|void
3298 *
3299 * Get single quiz attempt
3300 *
3301 * @since v.1.0.0
3302 */
3303 public function get_attempt($attempt_id = 0){
3304 global $wpdb;
3305 if ( ! $attempt_id){
3306 return false;
3307 }
3308 $attempt = $wpdb->get_row("SELECT * FROM {$wpdb->prefix}tutor_quiz_attempts WHERE attempt_id = {$attempt_id} ");
3309 return $attempt;
3310 }
3311
3312 /**
3313 * @param $attempt_info
3314 *
3315 * @return mixed
3316 *
3317 * Get unserialize attempt info
3318 *
3319 * @since v.1.0.0
3320 */
3321
3322 public function quiz_attempt_info($attempt_info){
3323 return maybe_unserialize($attempt_info);
3324 }
3325
3326 /**
3327 * @param $quiz_attempt_id
3328 * @param array $attempt_info
3329 *
3330 * @return bool|int
3331 *
3332 * Update attempt for various action
3333 *
3334 * @since v.1.0.0
3335 */
3336 public function quiz_update_attempt_info($quiz_attempt_id, $attempt_info = array()){
3337 $answers = tutor_utils()->avalue_dot('answers', $attempt_info);
3338 $total_marks = array_sum(wp_list_pluck($answers, 'question_mark'));
3339 $earned_marks = tutor_utils()->avalue_dot('marks_earned', $attempt_info);
3340 $earned_mark_percent = $earned_marks > 0 ? ( number_format(($earned_marks * 100) / $total_marks)) : 0;
3341 update_comment_meta($quiz_attempt_id, 'earned_mark_percent', $earned_mark_percent);
3342
3343 return update_comment_meta($quiz_attempt_id,'quiz_attempt_info', $attempt_info);
3344 }
3345
3346 /**
3347 * @param int $quiz_id
3348 *
3349 * @return array|null|object
3350 *
3351 * Get random question by quiz id
3352 *
3353 * @since v.1.0.0
3354 */
3355
3356 public function get_random_question_by_quiz($quiz_id = 0){
3357 global $wpdb;
3358
3359 $quiz_id = $this->get_post_id($quiz_id);
3360 $is_attempt = $this->is_started_quiz($quiz_id);
3361
3362 $tempSql = " AND question_type = 'matching' ";
3363 $questions = $wpdb->get_results("SELECT * FROM {$wpdb->prefix}tutor_quiz_questions WHERE quiz_id = {$quiz_id} {$tempSql} ORDER BY RAND() LIMIT 0,1 ");
3364
3365 return $questions;
3366 }
3367
3368 /**
3369 * @param int $quiz_id
3370 *
3371 * @return array|null|object
3372 *
3373 * Get random questions by quiz
3374 */
3375 public function get_random_questions_by_quiz($quiz_id = 0){
3376 global $wpdb;
3377
3378 $quiz_id = $this->get_post_id($quiz_id);
3379 $attempt = $this->is_started_quiz($quiz_id);
3380 if ( ! $attempt){
3381 return false;
3382 }
3383
3384 $questions = $wpdb->get_results("SELECT * FROM {$wpdb->prefix}tutor_quiz_questions WHERE quiz_id = {$quiz_id} ORDER BY RAND() LIMIT {$attempt->total_questions} ");
3385
3386 return $questions;
3387 }
3388
3389 /**
3390 * @param $question_id
3391 * @param bool $rand
3392 *
3393 * @return array|bool|null|object
3394 *
3395 * Get answers list by quiz question
3396 *
3397 * @since v.1.0.0
3398 */
3399 public function get_answers_by_quiz_question($question_id, $rand = false){
3400 global $wpdb;
3401
3402
3403 $question = $wpdb->get_row("SELECT * from {$wpdb->prefix}tutor_quiz_questions WHERE question_id = {$question_id} ;");
3404 if ( ! $question){
3405 return false;
3406 }
3407
3408 $order = " answer_order ASC ";
3409 if ($question->question_type === 'ordering'){
3410 $order = " RAND() ";
3411 }
3412
3413 if ($rand){
3414 $order = " RAND() ";
3415 }
3416
3417 $answers = $wpdb->get_results("SELECT * FROM {$wpdb->prefix}tutor_quiz_question_answers WHERE belongs_question_id = {$question_id} AND belongs_question_type =
3418 '{$question->question_type}' order by {$order} ");
3419 return $answers;
3420 }
3421
3422 /**
3423 * @param int $quiz_id
3424 * @param int $user_id
3425 *
3426 * @return array|bool|null|object
3427 *
3428 * Get all of the attempts by an user of a quiz
3429 *
3430 * @since v.1.0.0
3431 */
3432
3433 public function quiz_attempts($quiz_id = 0, $user_id = 0){
3434 global $wpdb;
3435
3436 $quiz_id = $this->get_post_id($quiz_id);
3437 $user_id = $this->get_user_id($user_id);
3438
3439 $attempts = $wpdb->get_results("SELECT * FROM {$wpdb->prefix}tutor_quiz_attempts WHERE quiz_id = {$quiz_id} AND user_id = {$user_id} ");
3440
3441 if (is_array($attempts) && count($attempts)){
3442 return $attempts;
3443 }
3444
3445 return false;
3446 }
3447
3448
3449 public function get_all_quiz_attempts_by_user($user_id = 0){
3450 global $wpdb;
3451
3452 $user_id = $this->get_user_id($user_id);
3453
3454 $attempts = $wpdb->get_results("SELECT * FROM {$wpdb->prefix}tutor_quiz_attempts WHERE user_id = {$user_id} ");
3455
3456 if (is_array($attempts) && count($attempts)){
3457 return $attempts;
3458 }
3459
3460 return false;
3461 }
3462
3463 /**
3464 * @param string $search_term
3465 *
3466 * @return int
3467 *
3468 * Total number of quiz attempts
3469 *
3470 * @since v.1.0.0
3471 */
3472
3473 public function get_total_quiz_attempts($search_term = ''){
3474 global $wpdb;
3475
3476 if ($search_term){
3477 $search_term = " AND ( user_email like '%{$search_term}%' OR display_name like '%{$search_term}%' OR post_title like '%{$search_term}%' ) ";
3478 }
3479
3480 $count = $wpdb->get_var("SELECT COUNT(attempt_id)
3481 FROM {$wpdb->prefix}tutor_quiz_attempts quiz_attempts
3482 INNER JOIN {$wpdb->posts} quiz
3483 ON quiz_attempts.quiz_id = quiz.ID
3484 INNER JOIN {$wpdb->users}
3485 ON quiz_attempts.user_id = {$wpdb->users}.ID
3486 WHERE 1=1 AND quiz_attempts.attempt_ended_at <= NOW() {$search_term} ");
3487 return (int) $count;
3488 }
3489
3490 /**
3491 * @param int $start
3492 * @param int $limit
3493 * @param string $search_term
3494 *
3495 * @return array|null|object
3496 *
3497 *
3498 * Get the all quiz attempts
3499 *
3500 * @since v.1.0.0
3501 */
3502 public function get_quiz_attempts($start = 0, $limit = 10, $search_term = '') {
3503 global $wpdb;
3504
3505 if ($search_term){
3506 $search_term = " AND ( user_email like '%{$search_term}%' OR display_name like '%{$search_term}%' OR post_title like '%{$search_term}%' ) ";
3507 }
3508
3509 $query = $wpdb->get_results("SELECT *
3510 FROM {$wpdb->prefix}tutor_quiz_attempts quiz_attempts
3511 INNER JOIN {$wpdb->posts} quiz
3512 ON quiz_attempts.quiz_id = quiz.ID
3513 INNER JOIN {$wpdb->users}
3514 ON quiz_attempts.user_id = {$wpdb->users}.ID
3515 WHERE 1=1 AND quiz_attempts.attempt_ended_at <= NOW() {$search_term}
3516 ORDER BY quiz_attempts.attempt_id DESC
3517 LIMIT {$start},{$limit}; ");
3518 return $query;
3519 }
3520
3521 public function get_quiz_attempts_by_course_ids($start = 0, $limit = 10, $course_ids = array(), $search_term = '') {
3522 global $wpdb;
3523
3524 if ($search_term){
3525 $search_term = " AND ( user_email like '%{$search_term}%' OR display_name like '%{$search_term}%' OR post_title like '%{$search_term}%' ) ";
3526 }
3527
3528 $course_ids_in = implode($course_ids, ',');
3529 $sql = " AND quiz_attempts.course_id IN({$course_ids_in}) ";
3530 $search_term = $sql.$search_term;
3531
3532 $query = $wpdb->get_results("SELECT *
3533 FROM {$wpdb->prefix}tutor_quiz_attempts quiz_attempts
3534 INNER JOIN {$wpdb->posts} quiz
3535 ON quiz_attempts.quiz_id = quiz.ID
3536 INNER JOIN {$wpdb->users}
3537 ON quiz_attempts.user_id = {$wpdb->users}.ID
3538 WHERE 1=1 AND quiz_attempts.attempt_ended_at <= NOW() {$search_term}
3539 ORDER BY quiz_attempts.attempt_id DESC
3540 LIMIT {$start},{$limit}; ");
3541 return $query;
3542 }
3543
3544 public function get_total_quiz_attempts_by_course_ids($course_ids = array(), $search_term = ''){
3545 global $wpdb;
3546
3547 if ($search_term){
3548 $search_term = " AND ( user_email like '%{$search_term}%' OR display_name like '%{$search_term}%' OR post_title like '%{$search_term}%' ) ";
3549 }
3550
3551 $course_ids_in = implode($course_ids, ',');
3552 $sql = " AND quiz_attempts.course_id IN({$course_ids_in}) ";
3553 $search_term = $sql.$search_term;
3554
3555 $count = $wpdb->get_var("SELECT COUNT(attempt_id)
3556 FROM {$wpdb->prefix}tutor_quiz_attempts quiz_attempts
3557 INNER JOIN {$wpdb->posts} quiz
3558 ON quiz_attempts.quiz_id = quiz.ID
3559 INNER JOIN {$wpdb->users}
3560 ON quiz_attempts.user_id = {$wpdb->users}.ID
3561 WHERE 1=1 AND quiz_attempts.attempt_ended_at <= NOW() {$search_term} ");
3562 return (int) $count;
3563 }
3564
3565 /**
3566 * @param $attempt_id
3567 *
3568 * @return array|null|object
3569 *
3570 * Get quiz answers by attempt id
3571 *
3572 * @since v.1.0.0
3573 */
3574 public function get_quiz_answers_by_attempt_id($attempt_id){
3575 global $wpdb;
3576
3577 $results = $wpdb->get_results("SELECT answers.*, question.question_title, question.question_type
3578 FROM {$wpdb->prefix}tutor_quiz_attempt_answers answers
3579 LEFT JOIN {$wpdb->prefix}tutor_quiz_questions question ON answers.question_id = question.question_id
3580 WHERE answers.quiz_attempt_id = {$attempt_id} ");
3581
3582 return $results;
3583 }
3584
3585 /**
3586 * @param $answer_id
3587 *
3588 * @return array|null|object
3589 *
3590 * Get single answer by answer_id
3591 *
3592 * @since v.1.0.0
3593 */
3594 public function get_answer_by_id($answer_id){
3595 global $wpdb;
3596
3597 if (is_array($answer_id)){
3598 $in_ids = implode(",", $answer_id);
3599 $sql = "answer.answer_id IN({$in_ids})";
3600 }else{
3601 $sql = "answer.answer_id = {$answer_id}";
3602 }
3603
3604 $answer = $wpdb->get_results("SELECT answer.*, question.question_title, question.question_type
3605 FROM {$wpdb->prefix}tutor_quiz_question_answers answer
3606 LEFT JOIN {$wpdb->prefix}tutor_quiz_questions question ON answer.belongs_question_id = question.question_id
3607 WHERE 1=1 AND {$sql} ");
3608
3609 return $answer;
3610 }
3611
3612 /**
3613 * @param $ids
3614 *
3615 * @return array|bool|null|object
3616 *
3617 * Get quiz answers by ids
3618 *
3619 * @since v.1.0.0
3620 */
3621
3622 public function get_quiz_answers_by_ids($ids){
3623 $ids = (array) $ids;
3624
3625 if (!count($ids)){
3626 return false;
3627 }
3628
3629 $in_ids = implode(",", $ids);
3630
3631 global $wpdb;
3632 $query = $wpdb->get_results("SELECT
3633 comment_ID,
3634 comment_content
3635 FROM {$wpdb->comments}
3636 WHERE comment_type = 'quiz_answer_option' AND comment_ID IN({$in_ids}) ");
3637
3638 if (is_array($query) && count($query)){
3639 return $query;
3640 }
3641
3642 return false;
3643 }
3644
3645 /**
3646 * @param null $level
3647 *
3648 * @return mixed
3649 *
3650 * Get the users / students / course levels
3651 *
3652 * @since v.1.0.0
3653 */
3654
3655 public function course_levels($level = null){
3656 $levels = apply_filters('tutor_course_level', array(
3657 'all_levels' => __('All Levels', 'tutor'),
3658 'beginner' => __('Beginner', 'tutor'),
3659 'intermediate' => __('Intermediate', 'tutor'),
3660 'expert' => __('Expert', 'tutor'),
3661 ));
3662
3663 if ($level){
3664 if (isset($levels[$level])){
3665 return $levels[$level];
3666 }else{
3667 return '';
3668 }
3669 }
3670
3671 return $levels;
3672 }
3673
3674 /**
3675 * @return mixed|void
3676 *
3677 * Get user permalink for dashboard
3678 *
3679 * @since v.1.0.0
3680 */
3681 public function user_profile_permalinks(){
3682 $permalinks = array(
3683 'courses_taken' => __('Courses Taken', 'tutor'),
3684 );
3685
3686 $show_enrolled_course = tutor_utils()->get_option('show_courses_completed_by_student');
3687 $enable_show_reviews_wrote = tutor_utils()->get_option('students_own_review_show_at_profile');
3688
3689 if ($show_enrolled_course){
3690 $permalinks['enrolled_course'] = __('Enrolled Course', 'tutor');
3691 }
3692 if ($enable_show_reviews_wrote){
3693 $permalinks['reviews_wrote'] = __('Reviews Written', 'tutor');
3694 }
3695
3696
3697 return apply_filters('tutor_public_profile/permalinks', $permalinks);
3698 }
3699
3700 /**
3701 * @return bool|false|string
3702 *
3703 * Student registration form
3704 *
3705 * @since v.1.0.0
3706 */
3707 public function student_register_url(){
3708 $student_register_page = (int) $this->get_option('student_register_page');
3709
3710 if ($student_register_page){
3711 return get_the_permalink($student_register_page);
3712 }
3713 return false;
3714 }
3715 /**
3716 * @return bool|false|string
3717 *
3718 * Instructor registration form
3719 *
3720 * @since v.1.2.13
3721 */
3722 public function instructor_register_url(){
3723 $instructor_register_page = (int) $this->get_option('instructor_register_page');
3724
3725 if ($instructor_register_page){
3726 return get_the_permalink($instructor_register_page);
3727 }
3728 return false;
3729 }
3730
3731 /**
3732 * @return false|string
3733 *
3734 * Get frontend dashboard URL
3735 */
3736 public function tutor_dashboard_url($sub_url = ''){
3737 $page_id = (int) tutor_utils()->get_option('tutor_dashboard_page_id');
3738 $page_id = apply_filters('tutor_dashboard_page_id', $page_id);
3739 return trailingslashit(get_the_permalink($page_id)).$sub_url;
3740 }
3741
3742 /**
3743 * Get the tutor dashboard page ID
3744 *
3745 * @return int
3746 *
3747 */
3748 public function dashboard_page_id(){
3749 $page_id = (int) tutor_utils()->get_option('tutor_dashboard_page_id');
3750 $page_id = apply_filters('tutor_dashboard_page_id', $page_id);
3751 return $page_id;
3752 }
3753
3754 /**
3755 * @param int $course_id
3756 * @param int $user_id
3757 *
3758 * @return bool
3759 *
3760 * is_wishlisted();
3761 *
3762 * @since v.1.0.0
3763 */
3764 public function is_wishlisted($course_id = 0, $user_id = 0){
3765 $course_id = $this->get_post_id($course_id);
3766 $user_id = $this->get_user_id($user_id);
3767 if ( ! $user_id){
3768 return false;
3769 }
3770
3771 global $wpdb;
3772 $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} ;");
3773
3774 return $if_added_to_list;
3775 }
3776
3777 /**
3778 * @param int $user_id
3779 *
3780 * @return array|null|object
3781 *
3782 * Get the wish lists by an user
3783 *
3784 * @since v.1.0.0
3785 */
3786 public function get_wishlist($user_id = 0){
3787 $user_id = $this->get_user_id($user_id);
3788 global $wpdb;
3789
3790 $query = "SELECT $wpdb->posts.*
3791 FROM $wpdb->posts
3792 LEFT JOIN $wpdb->usermeta ON ($wpdb->posts.ID = $wpdb->usermeta.meta_value)
3793 WHERE $wpdb->usermeta.meta_key = '_tutor_course_wishlist'
3794 AND $wpdb->usermeta.user_id = {$user_id}
3795 ORDER BY $wpdb->usermeta.umeta_id DESC ";
3796 $pageposts = $wpdb->get_results($query, OBJECT);
3797 return $pageposts;
3798 }
3799
3800 /**
3801 * @param int $limit
3802 *
3803 * @return array|null|object
3804 *
3805 * Getting popular courses
3806 *
3807 * @since v.1.0.0
3808 */
3809 public function most_popular_courses($limit = 10){
3810 global $wpdb;
3811
3812 $courses = $wpdb->get_results("
3813 SELECT COUNT(enrolled.ID) as total_enrolled,
3814 enrolled.post_parent as course_id,
3815 course.*
3816 from {$wpdb->posts} enrolled
3817 INNER JOIN {$wpdb->posts} course ON enrolled.post_parent = course.ID
3818 WHERE enrolled.post_type = 'tutor_enrolled' AND enrolled.post_status = 'completed'
3819
3820 GROUP BY course_id
3821 ORDER BY total_enrolled DESC LIMIT 0,{$limit} ;");
3822
3823 return $courses;
3824 }
3825
3826 /**
3827 * @param int $limit
3828 *
3829 * @return array|bool|null|object
3830 *
3831 * Get most rated courses lists
3832 *
3833 * @since v.1.0.0
3834 */
3835 public function most_rated_courses($limit = 10){
3836 global $wpdb;
3837
3838 $result = $wpdb->get_results("
3839 SELECT COUNT(comment_ID) AS total_rating,
3840 comment_ID,
3841 comment_post_ID,
3842 course.*
3843 FROM {$wpdb->comments}
3844 INNER JOIN {$wpdb->posts} course ON comment_post_ID = course.ID
3845 WHERE {$wpdb->comments}.comment_type = 'tutor_course_rating' AND {$wpdb->comments}.comment_approved = 'approved'
3846 GROUP BY comment_post_ID ORDER BY total_rating DESC LIMIT 0,{$limit}
3847 ;");
3848
3849 if (is_array($result) && count($result)){
3850 return $result;
3851 }
3852 return false;
3853 }
3854
3855 /**
3856 * @param null $addon_field
3857 *
3858 * @return bool
3859 *
3860 * Get Addon config
3861 *
3862 * @since v.1.0.0
3863 */
3864 public function get_addon_config($addon_field = null){
3865 if ( ! $addon_field){
3866 return false;
3867 }
3868
3869 $addonsConfig = maybe_unserialize(get_option('tutor_addons_config'));
3870
3871 if (isset($addonsConfig[$addon_field])){
3872 return $addonsConfig[$addon_field];
3873 }
3874
3875 return false;
3876 }
3877
3878 /**
3879 * @return array|false|string
3880 *
3881 * Get the IP from visitor
3882 *
3883 * @since v.1.0.0
3884 */
3885 function get_ip() {
3886 $ipaddress = '';
3887 if (getenv('HTTP_CLIENT_IP'))
3888 $ipaddress = getenv('HTTP_CLIENT_IP');
3889 else if(getenv('HTTP_X_FORWARDED_FOR'))
3890 $ipaddress = getenv('HTTP_X_FORWARDED_FOR');
3891 else if(getenv('HTTP_X_FORWARDED'))
3892 $ipaddress = getenv('HTTP_X_FORWARDED');
3893 else if(getenv('HTTP_FORWARDED_FOR'))
3894 $ipaddress = getenv('HTTP_FORWARDED_FOR');
3895 else if(getenv('HTTP_FORWARDED'))
3896 $ipaddress = getenv('HTTP_FORWARDED');
3897 else if(getenv('REMOTE_ADDR'))
3898 $ipaddress = getenv('REMOTE_ADDR');
3899 else
3900 $ipaddress = 'UNKNOWN';
3901 return $ipaddress;
3902 }
3903
3904 /**
3905 * @return mixed|void
3906 *
3907 * Get the social icons
3908 *
3909 * @since v.1.0.4
3910 */
3911
3912 public function tutor_social_share_icons(){
3913 $icons = array(
3914 'facebook' => array('share_class' => 's_facebook', 'icon_html' => '<i class="tutor-icon-facebook"></i>' ),
3915 'twitter' => array('share_class' => 's_twitter', 'icon_html' => '<i class="tutor-icon-twitter"></i>' ),
3916 'linkedin' => array('share_class' => 's_linkedin', 'icon_html' => '<i class="tutor-icon-linkdin"></i>' ),
3917 'tumblr' => array('share_class' => 's_tumblr', 'icon_html' => '<i class="tutor-icon-tumblr"></i>' ),
3918 );
3919
3920 return apply_filters('tutor_social_share_icons', $icons);
3921 }
3922
3923 /**
3924 * @param array $array
3925 *
3926 * @return bool
3927 *
3928 * count method with check is_array
3929 *
3930 * @since v.1.0.4
3931 */
3932 public function count($array = array()){
3933 if (is_array($array) && count($array)){
3934 return count($array);
3935 }
3936 return false;
3937 }
3938
3939 /**
3940 * @return array
3941 *
3942 * get all screen ids
3943 *
3944 * @since v.1.1.2
3945 */
3946 public function tutor_get_screen_ids(){
3947 $screen_ids = array(
3948 "edit-course",
3949 "course",
3950 "edit-course-category",
3951 "edit-course-tag",
3952 "tutor-lms_page_tutor-students",
3953 "tutor-lms_page_tutor-instructors",
3954 "tutor-lms_page_question_answer",
3955 "tutor-lms_page_tutor_quiz_attempts",
3956 "tutor-lms_page_tutor-addons",
3957 "tutor-lms_page_tutor-status",
3958 "tutor-lms_page_tutor_report",
3959 "tutor-lms_page_tutor_settings",
3960 "tutor-lms_page_tutor_emails",
3961 );
3962
3963 return apply_filters('tutor_get_screen_ids', $screen_ids);
3964 }
3965
3966
3967 /**
3968 * @return mixed
3969 *
3970 * get earning transaction completed status
3971 *
3972 * @since v.1.1.2
3973 */
3974 public function get_earnings_completed_statuses(){
3975 return apply_filters(
3976 'tutor_get_earnings_completed_statuses',
3977 array (
3978 'wc-completed',
3979 'completed',
3980 'complete',
3981 )
3982 );
3983 }
3984
3985 /**
3986 * @param int $user_id
3987 * @param array $date_filter
3988 *
3989 * @return array|null|object
3990 *
3991 * Get all time earning sum for an instructor with all commission
3992 *
3993 * @since v.1.1.2
3994 */
3995
3996 public function get_earning_sum($user_id = 0, $date_filter = array()){
3997 global $wpdb;
3998
3999 $user_id = $this->get_user_id($user_id);
4000 $date_query = '';
4001 if ($this->count($date_filter)){
4002 extract($date_filter);
4003
4004 if ( ! empty($dataFor)){
4005 if ($dataFor === 'yearly'){
4006 if (empty($year)){
4007 $year = date('Y');
4008 }
4009 $date_query = "AND YEAR(created_at) = {$year} ";
4010 }
4011 }else{
4012 $date_query = " AND (created_at BETWEEN '{$start_date}' AND '{$end_date}') ";
4013 }
4014 }
4015
4016 $complete_status = tutor_utils()->get_earnings_completed_statuses();
4017 $complete_status = "'".implode("','", $complete_status)."'";
4018
4019 $earning_sum = $wpdb->get_row("SELECT SUM(course_price_total) as course_price_total,
4020 SUM(course_price_grand_total) as course_price_grand_total,
4021 SUM(instructor_amount) as instructor_amount,
4022 (SELECT SUM(amount) FROM {$wpdb->prefix}tutor_withdraws WHERE user_id = {$user_id} AND status != 'rejected' ) as
4023 withdraws_amount,
4024 SUM(admin_amount) as admin_amount,
4025 SUM(deduct_fees_amount) as deduct_fees_amount
4026 FROM {$wpdb->prefix}tutor_earnings
4027 WHERE user_id = {$user_id} AND order_status IN({$complete_status}) {$date_query} ");
4028
4029 //TODO: need to check
4030 // (SUM(instructor_amount) - (SELECT withdraws_amount) ) as balance,
4031
4032
4033 if ( $earning_sum->course_price_total){
4034 $earning_sum->balance = $earning_sum->instructor_amount - $earning_sum->withdraws_amount;
4035 }else{
4036
4037 $earning_sum = (object) array(
4038 'course_price_total' => 0,
4039 'course_price_grand_total' => 0,
4040 'instructor_amount' => 0,
4041 'withdraws_amount' => 0,
4042 'balance' => 0,
4043 'admin_amount' => 0,
4044 'deduct_fees_amount' => 0,
4045 );
4046 }
4047
4048 return $earning_sum;
4049 }
4050
4051 /**
4052 * @param int $user_id
4053 * @param array $date_filter
4054 *
4055 * @return array|null|object
4056 *
4057 * Get earning statements
4058 *
4059 * @since v.1.1.2
4060 */
4061 public function get_earning_statements($user_id = 0, $filter_data = array()){
4062 global $wpdb;
4063
4064 $user_sql = "";
4065 if ($user_id){
4066 $user_sql = " AND user_id='{$user_id}' ";
4067 }
4068
4069 $date_query = '';
4070 $query_by_status = '';
4071 $pagination_query = '';
4072
4073 /**
4074 * Query by Date Filter
4075 */
4076 if ($this->count($filter_data)){
4077 extract($filter_data);
4078
4079 if ( ! empty($dataFor)){
4080 if ($dataFor === 'yearly'){
4081 if (empty($year)){
4082 $year = date('Y');
4083 }
4084 $date_query = "AND YEAR(created_at) = {$year} ";
4085 }
4086 }else{
4087 $date_query = " AND (created_at BETWEEN '{$start_date}' AND '{$end_date}') ";
4088 }
4089
4090 /**
4091 * Query by order status related to this earning transaction
4092 */
4093 if ( ! empty($statuses)) {
4094 if ( $this->count( $statuses ) ) {
4095 $status = "'" . implode( "','", $statuses ) . "'";
4096 $query_by_status = "AND order_status IN({$status})";
4097 } elseif ( $statuses === 'completed' ) {
4098
4099 $get_earnings_completed_statuses = $this->get_earnings_completed_statuses();
4100 if ( $this->count( $get_earnings_completed_statuses ) ) {
4101 $status = "'" . implode( "','", $get_earnings_completed_statuses ) . "'";
4102 $query_by_status = "AND order_status IN({$status})";
4103 }
4104 }
4105 }
4106
4107 if ( ! empty($per_page)){
4108 $offset = (int) ! empty($offset) ? $offset : 0;
4109
4110 $pagination_query = " LIMIT {$offset}, {$per_page} ";
4111
4112 }
4113
4114
4115 }
4116
4117 $query = $wpdb->get_results("SELECT earning_tbl.*, course.post_title as course_title
4118 FROM {$wpdb->prefix}tutor_earnings earning_tbl
4119 LEFT JOIN {$wpdb->posts} course ON earning_tbl.course_id = course.ID
4120 WHERE 1=1 {$user_sql} {$date_query} {$query_by_status} ORDER BY created_at DESC {$pagination_query} ");
4121
4122
4123 $query_count = (int) $wpdb->get_var("SELECT COUNT(earning_tbl.earning_id)
4124 FROM {$wpdb->prefix}tutor_earnings earning_tbl
4125 WHERE 1=1 {$user_sql} {$date_query} {$query_by_status} ORDER BY created_at DESC ");
4126
4127 return (object) array(
4128 'count' => $query_count,
4129 'results' => $query,
4130 );
4131 }
4132
4133 /**
4134 * @param int $price
4135 *
4136 * @return int|string
4137 *
4138 * Get the price format
4139 *
4140 * @since v.1.1.2
4141 */
4142
4143 public function tutor_price($price = 0){
4144 if (function_exists('wc_price')){
4145 return wc_price($price);
4146 }elseif (function_exists('edd_currency_filter')){
4147 return edd_currency_filter(edd_format_amount($price));
4148 }else{
4149 return number_format_i18n($price);
4150 }
4151 }
4152
4153 /**
4154 * @return mixed
4155 *
4156 * Get currency symbol from activated plugin, WC,EDD
4157 *
4158 * @since v.1.3.4
4159 */
4160
4161 public function currency_symbol(){
4162 $enable_tutor_edd = tutor_utils()->get_option('enable_tutor_edd');
4163 $enable_wc = tutor_utils()->get_option('enable_course_sell_by_woocommerce');
4164
4165 $symbol = '&#36;';
4166 if ($enable_tutor_edd && function_exists('edd_currency_symbol')){
4167 $symbol = edd_currency_symbol();
4168 }
4169
4170 if ($enable_wc && function_exists('get_woocommerce_currency_symbol') ){
4171 $symbol = get_woocommerce_currency_symbol();
4172 }
4173
4174 return apply_filters('get_tutor_currency_symbol', $symbol);
4175 }
4176
4177 /**
4178 * @param int $user_id
4179 *
4180 * @return bool|mixed
4181 *
4182 * Get withdraw method for a specific
4183 */
4184 public function get_user_withdraw_method($user_id = 0){
4185 $user_id = $this->get_user_id($user_id);
4186
4187 $account = get_user_meta($user_id, '_tutor_withdraw_method_data', true);
4188 if ($account){
4189 return maybe_unserialize($account);
4190 }
4191
4192 return false;
4193 }
4194
4195 /**
4196 * @param int $user_id
4197 * @param array $filter
4198 *
4199 * get withdrawal history
4200 *
4201 * @return object
4202 */
4203 public function get_withdrawals_history($user_id = 0, $filter = array()){
4204 global $wpdb;
4205
4206 $filter = (array) $filter;
4207 extract($filter);
4208
4209 $query_by_status_sql = "";
4210 $query_by_user_sql = "";
4211 $query_by_pagination = "";
4212
4213 if ( ! empty($status)){
4214 $status = (array) $status;
4215 $status = "'".implode("','", $status)."'";
4216
4217 $query_by_status_sql = " AND status IN({$status}) ";
4218 }
4219
4220 if ( ! empty($per_page)){
4221 if ( empty($start))
4222 $start = 0;
4223
4224 $query_by_pagination = " LIMIT {$start}, {$per_page} ";
4225 }
4226
4227 if ($user_id){
4228 $query_by_user_sql = " AND user_id = {$user_id} ";
4229 }
4230
4231
4232 $count = (int) $wpdb->get_var("SELECT COUNT(withdraw_id) FROM {$wpdb->prefix}tutor_withdraws WHERE 1=1 {$query_by_user_sql} {$query_by_status_sql} ");
4233
4234 $results = $wpdb->get_results("SELECT withdraw_tbl.*,
4235 user_tbl.display_name as user_name,
4236 user_tbl.user_email
4237 FROM {$wpdb->prefix}tutor_withdraws withdraw_tbl
4238 INNER JOIN {$wpdb->users} user_tbl ON withdraw_tbl.user_id = user_tbl.ID
4239 WHERE 1=1
4240 {$query_by_user_sql}
4241 {$query_by_status_sql} ORDER BY
4242 created_at DESC {$query_by_pagination} ");
4243
4244 $withdraw_history = array(
4245 'count' => 0,
4246 'results' => null,
4247 );
4248
4249 if ($count){
4250 $withdraw_history['count'] = $count;
4251 }
4252
4253 if (tutor_utils()->count($results)){
4254 $withdraw_history['results'] = $results;
4255 }
4256 return (object) $withdraw_history;
4257
4258 }
4259
4260 /**
4261 * @param int $instructor_id
4262 *
4263 * Add Instructor role to any user by user iD
4264 */
4265 public function add_instructor_role($instructor_id = 0){
4266 if ( ! $instructor_id){
4267 return;
4268 }
4269 do_action('tutor_before_approved_instructor', $instructor_id);
4270
4271 update_user_meta($instructor_id, '_tutor_instructor_status', 'approved');
4272 update_user_meta($instructor_id, '_tutor_instructor_approved', time());
4273
4274 $instructor = new \WP_User($instructor_id);
4275 $instructor->add_role(tutor()->instructor_role);
4276
4277 do_action('tutor_after_approved_instructor', $instructor_id);
4278 }
4279
4280 /**
4281 * @param int $instructor_id
4282 *
4283 * Remove instructor role by instructor id
4284 */
4285 public function remove_instructor_role($instructor_id = 0){
4286 if ( ! $instructor_id){
4287 return;
4288 }
4289
4290 do_action('tutor_before_blocked_instructor', $instructor_id);
4291 update_user_meta($instructor_id, '_tutor_instructor_status', 'blocked');
4292
4293 $instructor = new \WP_User($instructor_id);
4294 $instructor->remove_role(tutor()->instructor_role);
4295 do_action('tutor_after_blocked_instructor', $instructor_id);
4296 }
4297
4298 /**
4299 * @param string $msg
4300 * @param string $name
4301 *
4302 * Set Flash Message to view in next action / route
4303 */
4304 public function set_flash_msg($msg = '', $name = 'success'){
4305 global $wp_filesystem;
4306 if ( ! $wp_filesystem ) {
4307 require_once( ABSPATH . 'wp-admin/includes/file.php' );
4308 }
4309
4310 $filename = "tutor_flash_msg_{$name}.txt";
4311 $upload_dir = wp_upload_dir();
4312 $dir = trailingslashit($upload_dir['basedir']) . 'tutor/';
4313
4314 WP_Filesystem( false, $upload_dir['basedir'], true );
4315
4316 if( ! $wp_filesystem->is_dir( $dir ) ) {
4317 $wp_filesystem->mkdir( $dir );
4318 }
4319 $wp_filesystem->put_contents( $dir . $filename, $msg );
4320 }
4321
4322 /**
4323 * @param null $name
4324 *
4325 * @return mixed|string|void
4326 *
4327 * Get Flash Message
4328 */
4329 public function get_flash_msg($name = null){
4330 if ( ! $name){
4331 return '';
4332 }
4333
4334 $upload_dir = wp_get_upload_dir();
4335 $upload_dir = trailingslashit($upload_dir['basedir']);
4336 $msg_name = 'tutor_flash_msg_'.$name;
4337
4338 $msg = '';
4339 $flash_msg_file_name = $upload_dir."tutor/{$msg_name}.txt";
4340 if (file_exists($flash_msg_file_name)){
4341 $msg = file_get_contents($flash_msg_file_name);
4342 unlink($flash_msg_file_name);
4343 }
4344
4345 return apply_filters('tutor_get_flash_msg', $msg, $name);
4346 }
4347
4348 /**
4349 * @param int $user_id
4350 *
4351 * @return array|null|object
4352 *
4353 * Get purchase history by customer id
4354 */
4355
4356 public function get_orders_by_user_id($user_id = 0){
4357 global $wpdb;
4358
4359 $user_id = $this->get_user_id();
4360
4361 $query = $wpdb->get_results("SELECT {$wpdb->posts}.* FROM {$wpdb->posts}
4362 INNER JOIN {$wpdb->postmeta} customer ON ID = customer.post_id AND customer.meta_key = '_customer_user'
4363 INNER JOIN {$wpdb->postmeta} tutor_order ON ID = tutor_order.post_id AND tutor_order.meta_key = '_is_tutor_order_for_course'
4364 where post_type = 'shop_order' AND customer.meta_value = {$user_id} ");
4365 return $query;
4366 }
4367
4368 /**
4369 * @param null $status
4370 *
4371 * @return string
4372 *
4373 * Get status contact formatted for order
4374 *
4375 * @since v.1.3.1
4376 */
4377 public function order_status_context($status = null){
4378 $status = str_replace('wc-', '', $status);
4379 $status_name = ucwords(str_replace('-', ' ', $status));
4380
4381 return "<span class='label-order-status label-status-{$status}'>$status_name</span>";
4382 }
4383
4384 public function get_course_id_by_assignment($assignment_id = 0){
4385 $assignment_id = $this->get_post_id($assignment_id);
4386 return get_post_meta($assignment_id, '_tutor_course_id_for_assignments', true);
4387 }
4388
4389 /**
4390 * @param int $assignment_id
4391 * @param string $option_key
4392 * @param bool $default
4393 *
4394 * @return array|bool|mixed
4395 *
4396 * Get assignment options
4397 *
4398 * @since v.1.3.3
4399 */
4400 public function get_assignment_option($assignment_id = 0, $option_key = '', $default = false){
4401 $assignment_id = $this->get_post_id($assignment_id);
4402 $get_option_meta = maybe_unserialize(get_post_meta($assignment_id, 'assignment_option', true));
4403
4404 if ( ! $option_key && ! empty($get_option_meta)) {
4405 return $get_option_meta;
4406 }
4407
4408 $value = $this->avalue_dot( $option_key, $get_option_meta );
4409 if ( $value ) {
4410 return $value;
4411 }
4412 return $default;
4413 }
4414
4415 /**
4416 * @param int $assignment_id
4417 * @param int $user_id
4418 *
4419 * @return int
4420 *
4421 * Is running any assignment submitting
4422 *
4423 * @since v.1.3.3
4424 */
4425 public function is_assignment_submitting($assignment_id = 0, $user_id = 0){
4426 global $wpdb;
4427
4428 $assignment_id = $this->get_post_id($assignment_id);
4429 $user_id = $this->get_user_id($user_id);
4430
4431 $is_running_submit = (int) $wpdb->get_var("SELECT comment_ID FROM {$wpdb->comments} WHERE comment_type = 'tutor_assignment' AND comment_approved = 'submitting' AND user_id = {$user_id} AND comment_post_ID =
4432 {$assignment_id} ");
4433
4434 return $is_running_submit;
4435 }
4436
4437 /**
4438 * @param int $assignment_id
4439 * @param int $user_id
4440 *
4441 * @return array|null|object
4442 *
4443 * Determine if any assignment submitted by user to a assignment
4444 *
4445 * @since v.1.3.3
4446 */
4447
4448 public function is_assignment_submitted($assignment_id = 0, $user_id = 0){
4449 global $wpdb;
4450
4451 $assignment_id = $this->get_post_id($assignment_id);
4452 $user_id = $this->get_user_id($user_id);
4453
4454 $has_submitted = $wpdb->get_row("SELECT * FROM {$wpdb->comments} WHERE comment_type = 'tutor_assignment' AND comment_approved = 'submitted' AND user_id = {$user_id} AND comment_post_ID = {$assignment_id} ");
4455
4456 return $has_submitted;
4457 }
4458
4459 public function get_assignment_submit_info($assignment_submitted_id = 0){
4460 global $wpdb;
4461
4462 $assignment_submitted_id = $this->get_post_id($assignment_submitted_id);
4463 $submitted_info = $wpdb->get_row("SELECT * FROM {$wpdb->comments} WHERE comment_ID = {$assignment_submitted_id} AND comment_type = 'tutor_assignment' AND comment_approved = 'submitted' ");
4464
4465 return $submitted_info;
4466 }
4467
4468 public function get_total_assignments(){
4469 global $wpdb;
4470
4471 $count = $wpdb->get_var("SELECT COUNT(comment_ID) FROM {$wpdb->comments} WHERE comment_type = 'tutor_assignment' AND comment_approved = 'submitted' ");
4472
4473 return (int) $count;
4474 }
4475
4476 public function get_assignments(){
4477 global $wpdb;
4478
4479 $results = $wpdb->get_results("SELECT * FROM {$wpdb->comments} WHERE comment_type = 'tutor_assignment' AND comment_approved = 'submitted' ");
4480
4481 return $results;
4482 }
4483
4484 /**
4485 * @param int $user_id
4486 *
4487 * @return array
4488 *
4489 * Get all courses id assigned or owned by an instructors
4490 *
4491 * @since v.1.3.3
4492 */
4493 public function get_assigned_courses_ids_by_instructors($user_id = 0){
4494 global $wpdb;
4495 $user_id = $this->get_user_id($user_id);
4496
4497 $course_post_type = tutor()->course_post_type;
4498 $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} GROUP BY meta_value ; ");
4499
4500 /*
4501 $author_ids = $wpdb->get_col("SELECT ID FROM {$wpdb->posts} where post_type = '{$course_post_type}' AND post_author = {$user_id}");
4502 $final_course_ids = array_merge($get_assigned_courses_ids, $author_ids);
4503 $final_course_ids = array_unique($final_course_ids);
4504 */
4505
4506 return $get_assigned_courses_ids;
4507 }
4508
4509 /**
4510 * @param int $parent
4511 *
4512 * @return array
4513 *
4514 * Get course categories in array with child
4515 *
4516 * @since v.1.3.4
4517 */
4518
4519 public function get_course_categories($parent = 0 ){
4520 $args = apply_filters('tutor_get_course_categories_args', array(
4521 'taxonomy' => 'course-category',
4522 'hide_empty' => false,
4523 'parent' => $parent,
4524 ));
4525
4526 $terms = get_terms($args);
4527
4528 $children = array();
4529 foreach ( $terms as $term ){
4530 $term->children = $this->get_course_categories( $term->term_id );
4531 $children[ $term->term_id ] = $term;
4532 }
4533
4534 return $children;
4535 }
4536
4537 /**
4538 * @return mixed
4539 *
4540 * Get back url from the request
4541 * @since v.1.3.4
4542 */
4543 public function referer(){
4544 $url = $this->array_get('_wp_http_referer', $_REQUEST);
4545 return apply_filters('tutor_referer_url', $url);
4546 }
4547
4548 /**
4549 * @param int $course_id
4550 *
4551 * @return false|string
4552 *
4553 * Get the frontend dashboard course edit page
4554 *
4555 * @since v.1.3.4
4556 */
4557 public function course_edit_link($course_id = 0){
4558 $course_id = $this->get_post_id($course_id);
4559
4560 $url = admin_url("post.php?post={$course_id}&action=edit");
4561 if (tutor()->has_pro){
4562 $url = $this->tutor_dashboard_url("create-course/?course_ID=".$course_id);
4563 }
4564
4565 return $url;
4566 }
4567
4568 public function get_assignments_by_instructor($instructor_id = 0, $filter_data = array()){
4569 global $wpdb;
4570
4571 $instructor_id = $this->get_user_id($instructor_id);
4572 $course_ids = tutor_utils()->get_assigned_courses_ids_by_instructors($instructor_id);
4573
4574 //$new_course_ids = tutils()->get_courses_by_instructor();
4575
4576 //die($this->print_view($course_ids));
4577
4578 $in_course_ids = implode("','", $course_ids);
4579
4580 $count = (int) $wpdb->get_var("SELECT COUNT(ID) FROM {$wpdb->postmeta} post_meta
4581 INNER JOIN {$wpdb->posts} assignment ON post_meta.post_id = assignment.ID AND post_meta.meta_key = '_tutor_course_id_for_assignments'
4582 where post_type = 'tutor_assignments' AND post_meta.meta_value IN('$in_course_ids') ORDER BY ID DESC ");
4583
4584 $pagination_query = '';
4585 if ($this->count($filter_data)) {
4586 extract( $filter_data );
4587
4588 if ( ! empty( $per_page ) ) {
4589 $offset = (int) ! empty( $offset ) ? $offset : 0;
4590 $pagination_query = " LIMIT {$offset}, {$per_page} ";
4591 }
4592 }
4593
4594 $query = $wpdb->get_results("SELECT * FROM {$wpdb->postmeta} post_meta
4595 INNER JOIN {$wpdb->posts} assignment ON post_meta.post_id = assignment.ID AND post_meta.meta_key = '_tutor_course_id_for_assignments'
4596 where post_type = 'tutor_assignments' AND post_meta.meta_value IN('$in_course_ids') ORDER BY ID DESC {$pagination_query} ");
4597
4598 return (object) array('count' => $count, 'results' => $query);
4599 }
4600
4601 /**
4602 * @param int $course_id
4603 *
4604 * @return bool|object
4605 *
4606 * Get assignments by course id
4607 */
4608 public function get_assignments_by_course($course_id = 0){
4609 if ( ! $course_id){
4610 return false;
4611 }
4612 global $wpdb;
4613
4614 $count = (int) $wpdb->get_var("SELECT COUNT(ID) FROM {$wpdb->postmeta} post_meta
4615 INNER JOIN {$wpdb->posts} assignment ON post_meta.post_id = assignment.ID AND post_meta.meta_key = '_tutor_course_id_for_assignments'
4616 where post_type = 'tutor_assignments' AND post_meta.meta_value = {$course_id} ORDER BY ID DESC ");
4617
4618 $query = $wpdb->get_results("SELECT * FROM {$wpdb->postmeta} post_meta
4619 INNER JOIN {$wpdb->posts} assignment ON post_meta.post_id = assignment.ID AND post_meta.meta_key = '_tutor_course_id_for_assignments'
4620 where post_type = 'tutor_assignments' AND post_meta.meta_value = {$course_id} ORDER BY ID DESC");
4621
4622 return (object) array('count' => $count, 'results' => $query);
4623 }
4624
4625 /**
4626 * @return bool
4627 *
4628 * Determine if script debug
4629 *
4630 * @since v.1.3.4
4631 */
4632 public function is_script_debug(){
4633 return ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG );
4634 }
4635
4636 }