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