PluginProbe ʕ •ᴥ•ʔ
Tutor LMS – eLearning and online course solution / 1.0.8
Tutor LMS – eLearning and online course solution v1.0.8
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
3727 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 global $wpdb;
1241 $course_id = $this->get_post_id($course_id);
1242 $user_id = $this->get_user_id($user_id);
1243
1244 $is_completed = $wpdb->get_row("SELECT comment_ID,
1245 comment_post_ID as course_id,
1246 comment_author as completed_user_id,
1247 comment_date as completion_date,
1248 comment_content as completed_hash
1249 from {$wpdb->comments}
1250 WHERE comment_agent = 'TutorLMSPlugin'
1251 AND comment_type = 'course_completed'
1252 AND comment_post_ID = {$course_id}
1253 AND user_id = {$user_id} ;");
1254
1255 if ($is_completed){
1256 return $is_completed;
1257 }
1258
1259 return false;
1260 }
1261
1262 /**
1263 * @param array $input
1264 *
1265 * @return array
1266 *
1267 * Sanitize input array
1268 *
1269 * @since v.1.0.0
1270 */
1271 public function sanitize_array($input = array()){
1272 $array = array();
1273
1274 if (is_array($input) && count($input)){
1275 foreach ($input as $key => $value){
1276 if (is_array($value)){
1277 $array[$key] = $this->sanitize_array($value);
1278 }else{
1279 $key = sanitize_text_field($key);
1280 $value = sanitize_text_field($value);
1281 $array[$key] = $value;
1282 }
1283 }
1284 }
1285
1286 return $array;
1287 }
1288
1289 /**
1290 * @param int $post_id
1291 *
1292 * @return array|bool
1293 *
1294 * Determine if has any video in single
1295 *
1296 * @since v.1.0.0
1297 */
1298
1299 public function has_video_in_single($post_id = 0){
1300 if (is_single()) {
1301 $post_id = $this->get_post_id($post_id);
1302
1303 $video = $this->get_video( $post_id );
1304 if ( $video ) {
1305 return $video;
1306 }
1307 }
1308 return false;
1309
1310 }
1311
1312 /**
1313 * @param int $start
1314 * @param int $limit
1315 * @param string $search_term
1316 * @param int $course_id
1317 *
1318 * @return array|null|object
1319 *
1320 *
1321 * Get the enrolled students for all courses.
1322 *
1323 * Pass course id in 4th parameter to get students course wise.
1324 *
1325 * @since v.1.0.0
1326 */
1327 public function get_students($start = 0, $limit = 10, $search_term = ''){
1328 $meta_key = '_is_tutor_student';
1329
1330 global $wpdb;
1331
1332 if ($search_term){
1333 $search_term = " AND ( {$wpdb->users}.display_name LIKE '%{$search_term}%' OR {$wpdb->users}.user_email LIKE '%{$search_term}%' ) ";
1334 }
1335
1336 $students = $wpdb->get_results("SELECT SQL_CALC_FOUND_ROWS {$wpdb->users}.* FROM {$wpdb->users}
1337 INNER JOIN {$wpdb->usermeta}
1338 ON ( {$wpdb->users}.ID = {$wpdb->usermeta}.user_id )
1339 WHERE 1=1 AND ( {$wpdb->usermeta}.meta_key = '{$meta_key}' ) {$search_term}
1340 ORDER BY {$wpdb->usermeta}.meta_value DESC
1341 LIMIT {$start}, {$limit} ");
1342
1343 return $students;
1344 }
1345
1346 /**
1347 * @return int
1348 *
1349 * @since v.1.0.0
1350 *
1351 * get the total students
1352 * pass course id to get course wise total students
1353 *
1354 * @since v.1.0.0
1355 */
1356 public function get_total_students($search_term = ''){
1357 $meta_key = '_is_tutor_student';
1358
1359 global $wpdb;
1360
1361 if ($search_term){
1362 $search_term = " AND ( {$wpdb->users}.display_name LIKE '%{$search_term}%' OR {$wpdb->users}.user_email LIKE '%{$search_term}%' ) ";
1363 }
1364
1365 $count = $wpdb->get_var("SELECT COUNT({$wpdb->users}.ID) FROM {$wpdb->users} INNER JOIN {$wpdb->usermeta} ON ( {$wpdb->users}.ID = {$wpdb->usermeta}.user_id ) WHERE 1=1 AND ( {$wpdb->usermeta}.meta_key = '{$meta_key}' ) $search_term ");
1366
1367 return (int) $count;
1368 }
1369
1370 /**
1371 * @param int $user_id
1372 *
1373 * @return array
1374 *
1375 * Get complete courses ids by user
1376 *
1377 * @since v.1.0.0
1378 */
1379 public function get_completed_courses_ids_by_user($user_id = 0){
1380 global $wpdb;
1381
1382 $user_id = $this->get_user_id($user_id);
1383
1384 $course_ids = (array) $wpdb->get_col("SELECT comment_post_ID as course_id
1385 from {$wpdb->comments}
1386 WHERE comment_agent = 'TutorLMSPlugin'
1387 AND comment_type = 'course_completed'
1388 AND user_id = {$user_id} ;");
1389
1390 return $course_ids;
1391 }
1392
1393 /**
1394 * @param int $user_id
1395 *
1396 * @return bool|\WP_Query
1397 *
1398 * Return courses by user_id
1399 *
1400 * @since v.1.0.0
1401 */
1402 public function get_courses_by_user($user_id = 0){
1403 $user_id = $this->get_user_id($user_id);
1404 $course_ids = $this->get_completed_courses_ids_by_user($user_id);
1405
1406 if (count($course_ids)){
1407 $course_post_type = tutor()->course_post_type;
1408 $course_args = array(
1409 'post_type' => $course_post_type,
1410 'post_status' => 'publish',
1411 'post__in' => $course_ids,
1412 );
1413
1414 return new \WP_Query($course_args);
1415 }
1416
1417 return false;
1418 }
1419
1420 /**
1421 * @param int $user_id
1422 *
1423 * @return bool|\WP_Query
1424 *
1425 * Get the active course by user
1426 *
1427 * @since v.1.0.0
1428 */
1429
1430 public function get_active_courses_by_user($user_id = 0){
1431 $user_id = $this->get_user_id($user_id);
1432
1433 $course_ids = $this->get_completed_courses_ids_by_user($user_id);
1434 $enrolled_course_ids = $this->get_enrolled_courses_ids_by_user($user_id);
1435 $active_courses = array_diff($enrolled_course_ids, $course_ids);
1436
1437 if (count($active_courses)){
1438 $course_post_type = tutor()->course_post_type;
1439 $course_args = array(
1440 'post_type' => $course_post_type,
1441 'post_status' => 'publish',
1442 'post__in' => $active_courses,
1443 );
1444
1445 return new \WP_Query($course_args);
1446 }
1447
1448 return false;
1449 }
1450
1451 /**
1452 * @param int $user_id
1453 *
1454 * @return array
1455 *
1456 * Get enrolled course ids by a user
1457 *
1458 * @since v.1.0.0
1459 */
1460
1461 public function get_enrolled_courses_ids_by_user($user_id = 0){
1462 global $wpdb;
1463 $user_id = $this->get_user_id($user_id);
1464 $course_ids = $wpdb->get_col("select post_parent from {$wpdb->posts} WHERE post_type = 'tutor_enrolled' AND post_author = {$user_id} AND post_status = 'completed'; ");
1465
1466 return $course_ids;
1467 }
1468
1469 /**
1470 * @param int $course_id
1471 *
1472 * @return int
1473 *
1474 * Get the total enrolled users at course
1475 */
1476 public function count_enrolled_users_by_course($course_id = 0){
1477 global $wpdb;
1478 $course_id = $this->get_post_id($course_id);
1479
1480 $course_ids = $wpdb->get_var("select COUNT(ID) from {$wpdb->posts} WHERE post_type = 'tutor_enrolled' AND post_parent = {$course_id} AND post_status = 'completed'; ");
1481
1482 return (int) $course_ids;
1483 }
1484
1485 /**
1486 * @param int $user_id
1487 *
1488 * @return bool|\WP_Query
1489 *
1490 * Get the enrolled courses by user
1491 */
1492 public function get_enrolled_courses_by_user($user_id = 0){
1493 global $wpdb;
1494
1495 $user_id = $this->get_user_id($user_id);
1496 $course_ids = $this->get_enrolled_courses_ids_by_user($user_id);
1497
1498 if (count($course_ids)){
1499 $course_post_type = tutor()->course_post_type;
1500 $course_args = array(
1501 'post_type' => $course_post_type,
1502 'post_status' => 'publish',
1503 'post__in' => $course_ids,
1504 );
1505 return new \WP_Query($course_args);
1506 }
1507 return false;
1508 }
1509
1510
1511 /**
1512 * @param int $post_id
1513 *
1514 * @return string
1515 *
1516 * Get the video streaming URL by post/lesson/course ID
1517 */
1518 public function get_video_stream_url($post_id = 0){
1519 $post_id = $this->get_post_id($post_id);
1520 $post = get_post($post_id);
1521
1522 if ($post->post_type === tutor()->lesson_post_type ){
1523 $video_url = trailingslashit(home_url()).'video-url/'.$post->post_name;
1524 }else{
1525 $video_info = tutor_utils()->get_video_info($post_id);
1526 $video_url = $video_info->url;
1527 }
1528
1529 return $video_url;
1530 }
1531
1532 /**
1533 * @param int $lesson_id
1534 * @param int $user_id
1535 *
1536 * @return array|bool|mixed
1537 *
1538 * Get student lesson reading current info
1539 *
1540 * @since v.1.0.0
1541 */
1542 public function get_lesson_reading_info_full($lesson_id = 0, $user_id = 0){
1543 $lesson_id = $this->get_post_id($lesson_id);
1544 $user_id = $this->get_user_id($user_id);
1545
1546 $lesson_info = (array) maybe_unserialize(get_user_meta($user_id, '_lesson_reading_info', true));
1547 return $this->avalue_dot($lesson_id, $lesson_info);
1548 }
1549
1550 /**
1551 * @param int $post_id
1552 *
1553 * @return bool|false|int
1554 *
1555 * Get current post id or given post id
1556 *
1557 * @since v.1.0.0
1558 */
1559 public function get_post_id($post_id = 0){
1560 if ( ! $post_id){
1561 $post_id = get_the_ID();
1562 if ( ! $post_id){
1563 return false;
1564 }
1565 }
1566
1567 return $post_id;
1568 }
1569
1570 /**
1571 * @param int $user_id
1572 *
1573 * @return bool|int
1574 *
1575 * Get current user or given user ID
1576 *
1577 * @since v.1.0.0
1578 */
1579 public function get_user_id($user_id = 0){
1580 if ( ! $user_id){
1581 $user_id = get_current_user_id();
1582 if ( ! $user_id){
1583 return false;
1584 }
1585 }
1586
1587 return $user_id;
1588 }
1589
1590 /**
1591 * @param int $lesson_id
1592 * @param int $user_id
1593 * @param string $key
1594 *
1595 * @return array|bool|mixed
1596 *
1597 * Get lesson reading info by key
1598 *
1599 * @since v.1.0.0
1600 */
1601
1602 public function get_lesson_reading_info($lesson_id = 0, $user_id = 0, $key = ''){
1603 $lesson_id = $this->get_post_id($lesson_id);
1604 $user_id = $this->get_user_id($user_id);
1605
1606 $lesson_info = $this->get_lesson_reading_info_full($lesson_id, $user_id);
1607
1608 return $this->avalue_dot($key, $lesson_info);
1609 }
1610
1611 /**
1612 * @param int $lesson_id
1613 * @param int $user_id
1614 * @param array $data
1615 *
1616 * @return bool
1617 *
1618 * Update student lesson reading info
1619 *
1620 * @since v.1.0.0
1621 */
1622 public function update_lesson_reading_info($lesson_id = 0, $user_id = 0, $key = '', $value = ''){
1623 $lesson_id = $this->get_post_id($lesson_id);
1624 $user_id = $this->get_user_id($user_id);
1625
1626 if ($key && $value){
1627 $lesson_info = (array) maybe_unserialize(get_user_meta($user_id, '_lesson_reading_info', true));
1628 $lesson_info[$lesson_id][$key] = $value;
1629 update_user_meta($user_id, '_lesson_reading_info', $lesson_info);
1630 }
1631 }
1632
1633 /**
1634 * @param string $url
1635 *
1636 * @return bool
1637 *
1638 * Get the Youtube Video ID from URL
1639 *
1640 * @since v.1.0.0
1641 */
1642 public function get_youtube_video_id($url = ''){
1643 if (!$url){
1644 return false;
1645 }
1646 preg_match('%(?:youtube(?:-nocookie)?\.com/(?:[^/]+/.+/|(?:v|e(?:mbed)?)/|.*[?&]v=)|youtu\.be/)([^"&?/ ]{11})%i', $url, $match);
1647
1648 if (isset($match[1])) {
1649 $youtube_id = $match[1];
1650 return $youtube_id;
1651 }
1652
1653 return false;
1654 }
1655
1656 /**
1657 * @param string $url
1658 *
1659 * @return bool
1660 *
1661 * Get the vimeo video id from URL
1662 *
1663 * @since v.1.0.0
1664 */
1665 public function get_vimeo_video_id($url = ''){
1666 if (preg_match('%^https?:\/\/(?:www\.|player\.)?vimeo.com\/(?:channels\/(?:\w+\/)?|groups\/([^\/]*)\/videos\/|album\/(\d+)\/video\/|video\/|)(\d+)(?:$|\/|\?)(?:[?]?.*)$%im', $url, $match)) {
1667 if (isset($match[3])){
1668 return $match[3];
1669 }
1670 }
1671 return false;
1672 }
1673
1674 /**
1675 * @param int $post_id
1676 *
1677 * Mark lesson complete
1678 *
1679 * @since v.1.0.0
1680 */
1681 public function mark_lesson_complete($post_id = 0, $user_id = 0){
1682 $post_id = $this->get_post_id($post_id);
1683 $user_id = $this->get_user_id($user_id);
1684 update_user_meta($user_id, '_tutor_completed_lesson_id_'.$post_id, time());
1685 }
1686
1687 /**
1688 * Saving enroll information to posts table
1689 * post_author = enrolled_student_id (wp_users id)
1690 * post_parent = enrolled course id
1691 *
1692 * @type: call when need
1693 * @return bool;
1694 *
1695 * @since v.1.0.0
1696 */
1697 public function do_enroll($course_id = 0, $order_id = 0){
1698 if ( ! $course_id){
1699 return false;
1700 }
1701
1702 do_action('tutor_before_enroll', $course_id);
1703 $user_id = get_current_user_id();
1704 $title = __('Course Enrolled', 'tutor')." &ndash; ".date_i18n(get_option('date_format')) .' @ '.date_i18n(get_option('time_format') ) ;
1705
1706 $enrolment_status = 'completed';
1707
1708 if ($this->is_course_purchasable($course_id)) {
1709 /**
1710 * We need to verify this enrollment, we will change the status later after payment confirmation
1711 */
1712 $enrolment_status = 'pending';
1713 }
1714
1715 $enroll_data = apply_filters('tutor_enroll_data',
1716 array(
1717 'post_type' => 'tutor_enrolled',
1718 'post_title' => $title,
1719 'post_status' => $enrolment_status,
1720 'post_author' => $user_id,
1721 'post_parent' => $course_id,
1722 )
1723 );
1724
1725 // Insert the post into the database
1726 $isEnrolled = wp_insert_post( $enroll_data );
1727 if ($isEnrolled) {
1728 do_action('tutor_after_enroll', $course_id, $isEnrolled);
1729
1730 //Mark Current User as Students with user meta data
1731 update_user_meta( $user_id, '_is_tutor_student', time() );
1732
1733 if ($order_id) {
1734 //Mark order for course and user
1735 $product_id = $this->get_course_product_id($course_id);
1736 update_post_meta( $isEnrolled, '_tutor_enrolled_by_order_id', $order_id );
1737 update_post_meta( $isEnrolled, '_tutor_enrolled_by_product_id', $product_id );
1738 update_post_meta( $order_id, '_is_tutor_order_for_course', time() );
1739 update_post_meta( $order_id, '_tutor_order_for_course_id_'.$course_id, $isEnrolled );
1740 }
1741 return true;
1742 }
1743
1744 return false;
1745 }
1746
1747 /**
1748 * @param $order_id
1749 *
1750 * Complete course enrollment and do some task
1751 *
1752 * @since v.1.0.0
1753 */
1754 public function complete_course_enroll($order_id){
1755 if ( ! tutor_utils()->is_tutor_order($order_id)){
1756 return;
1757 }
1758
1759 global $wpdb;
1760
1761 $enrolled_ids_with_course = $this->get_course_enrolled_ids_by_order_id($order_id);
1762 if ($enrolled_ids_with_course){
1763 $enrolled_ids = wp_list_pluck($enrolled_ids_with_course, 'enrolled_id');
1764
1765 if (is_array($enrolled_ids) && count($enrolled_ids)){
1766 foreach ($enrolled_ids as $enrolled_id){
1767 $wpdb->update( $wpdb->posts, array( 'post_status' => 'completed' ), array( 'ID' => $enrolled_id ) );
1768 }
1769 }
1770 }
1771 }
1772
1773 /**
1774 * @param $order_id
1775 *
1776 * @return array|bool
1777 *
1778 * @since v.1.0.0
1779 */
1780 public function get_course_enrolled_ids_by_order_id($order_id){
1781 global $wpdb;
1782 //Getting all of courses ids within this order
1783
1784 $courses_ids = $wpdb->get_results("SELECT * FROM {$wpdb->postmeta} WHERE post_id = {$order_id} AND meta_key LIKE '_tutor_order_for_course_id_%' ");
1785
1786 if (is_array($courses_ids) && count($courses_ids)){
1787 $course_enrolled_by_order = array();
1788 foreach ($courses_ids as $courses_id){
1789 $course_id = str_replace('_tutor_order_for_course_id_', '',$courses_id->meta_key);
1790 //array(order_id => array('course_id' => $course_id, 'enrolled_id' => enrolled_id))
1791 $course_enrolled_by_order[$courses_id->post_id] = array('course_id' => $course_id, 'enrolled_id' => $courses_id->meta_value);
1792 }
1793 return $course_enrolled_by_order;
1794 }
1795 return false;
1796 }
1797
1798 /**
1799 * Get wc product in efficient query
1800 *
1801 * @since v.1.0.0
1802 */
1803
1804 /**
1805 * @return array|null|object
1806 *
1807 * WooCommerce specific utils
1808 */
1809 public function get_wc_products_db(){
1810 global $wpdb;
1811 $query = $wpdb->get_results("SELECT ID, post_title from {$wpdb->posts} WHERE post_status = 'publish' AND post_type = 'product' ");
1812
1813 return $query;
1814 }
1815
1816 /**
1817 * @return array|null|object
1818 *
1819 * Get EDD Products
1820 */
1821 public function get_edd_products(){
1822 global $wpdb;
1823 $query = $wpdb->get_results("SELECT ID, post_title from {$wpdb->posts} WHERE post_status = 'publish' AND post_type = 'download' ");
1824
1825 return $query;
1826 }
1827
1828 /**
1829 * @param int $course_id
1830 *
1831 * @return int
1832 *
1833 * Get course productID
1834 *
1835 * @since v.1.0.0
1836 */
1837 public function get_course_product_id($course_id = 0){
1838 $course_id = $this->get_post_id($course_id);
1839 return (int) get_post_meta($course_id, '_tutor_course_product_id', true);
1840 }
1841
1842 /**
1843 * @param int $product_id
1844 *
1845 * @return array|null|object|void
1846 *
1847 * Get Product belongs with course
1848 *
1849 * @since v.1.0.0
1850 */
1851
1852 public function product_belongs_with_course($product_id = 0){
1853 global $wpdb;
1854
1855 $query = $wpdb->get_row("select * from {$wpdb->postmeta} WHERE meta_key='_tutor_course_product_id' AND meta_value = {$product_id} limit 1 ");
1856 return $query;
1857 }
1858
1859 /**
1860 * #End WooCommerce specific utils
1861 *
1862 * @since v.1.0.0
1863 */
1864
1865 public function get_enrolled_statuses(){
1866 return array (
1867 'pending',
1868 'processing',
1869 'on-hold',
1870 'completed',
1871 'cancelled',
1872 'refunded',
1873 'failed',
1874 );
1875 }
1876
1877 /**
1878 * @param $order_id
1879 *
1880 * @return mixed
1881 *
1882 * determine is this a tutor order
1883 *
1884 * @since v.1.0.0
1885 */
1886 public function is_tutor_order($order_id){
1887 return get_post_meta($order_id, '_is_tutor_order_for_course', true);
1888 }
1889
1890 /**
1891 * @return mixed
1892 *
1893 * Tutor Dashboard Pages
1894 *
1895 * @since v.1.0.0
1896 */
1897
1898 public function tutor_student_dashboard_pages(){
1899 $nav_items = array(
1900 'index' => __('Home', 'tutor'),
1901 'my-courses' => __('My Courses', 'tutor'),
1902 'active-courses' => __('Active Courses', 'tutor'),
1903 'completed-courses' => __('Completed Courses', 'tutor'),
1904 'wishlist' => __('WishList', 'tutor'),
1905 );
1906
1907 return apply_filters('tutor_dashboard/student/pages', $nav_items);
1908 }
1909
1910 /**
1911 * @param string $page_key
1912 * @param int $page_id
1913 *
1914 * @return string
1915 *
1916 * Get tutor dashboard page single URL
1917 *
1918 * @since v.1.0.0
1919 */
1920 public function get_tutor_dashboard_page_permalink($page_key = '', $page_id = 0){
1921 if ($page_key === 'index'){
1922 $page_key = '';
1923 }
1924 $page_id = $this->get_post_id($page_id);
1925 return trailingslashit(get_permalink($page_id)).$page_key;
1926 }
1927
1928 /**
1929 * @param string $input
1930 *
1931 * @return array|bool|mixed|string
1932 *
1933 * Get old input
1934 *
1935 * @since v.1.0.0
1936 */
1937 public function input_old($input = ''){
1938 $value = $this->avalue_dot($input, $_REQUEST);
1939 if ($value){
1940 return $value;
1941 }
1942 return '';
1943 }
1944
1945 /**
1946 * @param int $user_id
1947 *
1948 * @return mixed
1949 *
1950 * Determine if is instructor or not
1951 *
1952 * @since v.1.0.0
1953 */
1954 public function is_instructor($user_id = 0){
1955 $user_id = $this->get_user_id($user_id);
1956 return get_user_meta($user_id, '_is_tutor_instructor', true);
1957 }
1958
1959 /**
1960 * @param int $user_id
1961 * @param bool $status_name
1962 *
1963 * @return bool|mixed
1964 *
1965 * Instructor status
1966 *
1967 * @since v.1.0.0
1968 */
1969 public function instructor_status($user_id = 0, $status_name = true){
1970 $user_id = $this->get_user_id($user_id);
1971
1972 $instructor_status = apply_filters('tutor_instructor_statuses', array(
1973 'pending' => __('Pending', 'tutor'),
1974 'approved' => __('Approved', 'tutor'),
1975 'blocked' => __('Blocked', 'tutor'),
1976 ));
1977
1978 $status = get_user_meta($user_id, '_tutor_instructor_status', true);
1979
1980 if (isset($instructor_status[$status])){
1981 if ( ! $status_name){
1982 return $status;
1983 }
1984 return $instructor_status[$status];
1985 }
1986 return false;
1987 }
1988
1989 /**
1990 * @param string $search_term
1991 *
1992 * @return int
1993 *
1994 * Get total number of instructors
1995 *
1996 * @since v.1.0.0
1997 */
1998
1999 public function get_total_instructors($search_term = ''){
2000 $meta_key = '_is_tutor_instructor';
2001
2002 global $wpdb;
2003
2004 if ($search_term){
2005 $search_term = " AND ( {$wpdb->users}.display_name LIKE '%{$search_term}%' OR {$wpdb->users}.user_email LIKE '%{$search_term}%' ) ";
2006 }
2007
2008 $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 ");
2009
2010 return (int) $count;
2011 }
2012
2013 /**
2014 * @param int $start
2015 * @param int $limit
2016 * @param string $search_term
2017 *
2018 * @return array|null|object
2019 *
2020 * Get all instructors
2021 *
2022 * @since v.1.0.0
2023 */
2024 public function get_instructors($start = 0, $limit = 10, $search_term = ''){
2025 $meta_key = '_is_tutor_instructor';
2026 global $wpdb;
2027
2028 if ($search_term){
2029 $search_term = " AND ( {$wpdb->users}.display_name LIKE '%{$search_term}%' OR {$wpdb->users}.user_email LIKE '%{$search_term}%' ) ";
2030 }
2031
2032 $instructors = $wpdb->get_results("SELECT SQL_CALC_FOUND_ROWS {$wpdb->users}.* FROM {$wpdb->users}
2033 INNER JOIN {$wpdb->usermeta}
2034 ON ( {$wpdb->users}.ID = {$wpdb->usermeta}.user_id )
2035 WHERE 1=1 AND ( {$wpdb->usermeta}.meta_key = '{$meta_key}' ) {$search_term}
2036 ORDER BY {$wpdb->usermeta}.meta_value DESC
2037 LIMIT {$start}, {$limit} ");
2038
2039 return $instructors;
2040 }
2041
2042 /**
2043 * @param int $course_id
2044 *
2045 * @return array|bool|null|object
2046 *
2047 * Get all instructors by course
2048 *
2049 * @since v.1.0.0
2050 */
2051 public function get_instructors_by_course($course_id = 0){
2052 global $wpdb;
2053 $course_id = $this->get_post_id($course_id);
2054
2055 $instructors = $wpdb->get_results("select ID, display_name,
2056 get_course.meta_value as taught_course_id,
2057 tutor_job_title.meta_value as tutor_profile_job_title,
2058 tutor_bio.meta_value as tutor_profile_bio,
2059 tutor_photo.meta_value as tutor_profile_photo
2060 from {$wpdb->users}
2061 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}
2062 LEFT JOIN {$wpdb->usermeta} tutor_job_title ON ID = tutor_job_title.user_id AND tutor_job_title.meta_key = '_tutor_profile_job_title'
2063 LEFT JOIN {$wpdb->usermeta} tutor_bio ON ID = tutor_bio.user_id AND tutor_bio.meta_key = '_tutor_profile_bio'
2064 LEFT JOIN {$wpdb->usermeta} tutor_photo ON ID = tutor_photo.user_id AND tutor_photo.meta_key = '_tutor_profile_photo'
2065 ");
2066
2067 if (is_array($instructors) && count($instructors)){
2068 return $instructors;
2069 }
2070
2071 return false;
2072 }
2073
2074 /**
2075 * @param $instructor_id
2076 *
2077 * Get total Students by instructor
2078 * 1 enrollment = 1 student, so total enrolled for a equivalent total students (Tricks)
2079 *
2080 * @return int
2081 *
2082 * @since v.1.0.0
2083 */
2084
2085 public function get_total_students_by_instructor($instructor_id){
2086 global $wpdb;
2087
2088 $course_post_type = tutor()->course_post_type;
2089 $count = $wpdb->get_var("SELECT COUNT(courses.ID) from {$wpdb->posts} courses
2090
2091 INNER JOIN {$wpdb->posts} enrolled ON courses.ID = enrolled.post_parent AND enrolled.post_type = 'tutor_enrolled'
2092 WHERE courses.post_status = 'publish'
2093 AND courses.post_type = '{$course_post_type}'
2094 AND courses.post_author = {$instructor_id} ; ");
2095 return (int) $count;
2096 }
2097
2098 /**
2099 * @param float $input
2100 *
2101 * @return float|string
2102 *
2103 * Get rating format from value
2104 *
2105 * @since v.1.0.0
2106 */
2107 public function get_rating_value($input = 0.00){
2108
2109 if ( $input > 0){
2110 $input = number_format($input, 2);
2111 $int_value = (int) $input;
2112 $fraction = $input - $int_value;
2113
2114 if ($fraction == 0){
2115 $fraction = 0.00;
2116 }elseif($fraction > 0.5){
2117 $fraction = 1;
2118 }else{
2119 $fraction = 0.5;
2120 }
2121
2122 return number_format( ($int_value + $fraction), 2);
2123 }
2124 return 0.00;
2125 }
2126
2127 /**
2128 * @param float $current_rating
2129 * @param bool $echo
2130 *
2131 * @return string
2132 *
2133 * Generate star rating based in given rating value
2134 *
2135 * @since v.1.0.0
2136 */
2137 public function star_rating_generator($current_rating = 0.00, $echo = true){
2138 $output = '<div class="tutor-star-rating-group">';
2139
2140 for ($i = 1; $i <=5 ; $i++){
2141 $intRating = (int) $current_rating;
2142
2143 if ($intRating >= $i){
2144 $output.= '<i class="tutor-icon-star-full" data-rating-value="'.$i.'"></i>';
2145 } else{
2146 if ( ($current_rating - $i) == -0.5){
2147 $output.= '<i class="tutor-icon-star-half" data-rating-value="'.$i.'"></i>';
2148 }else{
2149 $output.= '<i class="tutor-icon-star-line" data-rating-value="'.$i.'"></i>';
2150 }
2151 }
2152 }
2153
2154 $output .= "<div class='tutor-rating-gen-input'><input type='hidden' name='tutor_rating_gen_input' value='{$current_rating}' /> </div>";
2155
2156 $output .= "</div>";
2157
2158 if ($echo){
2159 echo $output;
2160 }
2161 return $output;
2162 }
2163
2164 /**
2165 * @param null $name
2166 *
2167 * @return string
2168 *
2169 * Generate text to avatar
2170 *
2171 * @since v.1.0.0
2172 */
2173 public function get_tutor_avatar($user_id = null, $size = 'thumbnail'){
2174 global $wpdb;
2175
2176 if ( ! $user_id){
2177 return '';
2178 }
2179
2180 $user = $this->get_tutor_user($user_id);
2181 if ($user->tutor_profile_photo){
2182 return '<img src="'.wp_get_attachment_image_url($user->tutor_profile_photo, $size).'" class="tutor-image-avatar" alt="" /> ';
2183 }
2184
2185 $name = $user->display_name;
2186 $arr = explode(' ', trim($name));
2187
2188 if (count($arr) > 1){
2189 $first_char = substr($arr[0], 0, 1) ;
2190 $second_char = substr($arr[1], 0, 1) ;
2191 }else{
2192 $first_char = substr($arr[0], 0, 1) ;
2193 $second_char = substr($arr[0], 1, 1) ;
2194 }
2195
2196 $initial_avatar = strtoupper($first_char.$second_char);
2197
2198 $bg_color = '#'.substr(md5($initial_avatar), 0, 6);
2199 $initial_avatar = "<span class='tutor-text-avatar' style='background-color: {$bg_color}; color: #fff8e5'>{$initial_avatar}</span>";
2200
2201 return $initial_avatar;
2202 }
2203
2204 /**
2205 * @param $user_id
2206 *
2207 * @return array|null|object|void
2208 *
2209 * Get tutor user
2210 *
2211 * @since v.1.0.0
2212 */
2213
2214 public function get_tutor_user($user_id){
2215 global $wpdb;
2216
2217 $user = $wpdb->get_row("select ID, display_name,
2218 tutor_job_title.meta_value as tutor_profile_job_title,
2219 tutor_bio.meta_value as tutor_profile_bio,
2220 tutor_photo.meta_value as tutor_profile_photo
2221
2222 from {$wpdb->users}
2223 LEFT JOIN {$wpdb->usermeta} tutor_job_title ON ID = tutor_job_title.user_id AND tutor_job_title.meta_key = '_tutor_profile_job_title'
2224 LEFT JOIN {$wpdb->usermeta} tutor_bio ON ID = tutor_bio.user_id AND tutor_bio.meta_key = '_tutor_profile_bio'
2225 LEFT JOIN {$wpdb->usermeta} tutor_photo ON ID = tutor_photo.user_id AND tutor_photo.meta_key = '_tutor_profile_photo'
2226
2227 WHERE ID = {$user_id} ");
2228 return $user;
2229 }
2230
2231 /**
2232 * @param int $course_id
2233 * @param int $offset
2234 * @param int $limit
2235 *
2236 * @return array|null|object
2237 *
2238 * get course reviews
2239 *
2240 * @since v.1.0.0
2241 */
2242 public function get_course_reviews($course_id = 0, $offset = 0, $limit = 150){
2243 $course_id = $this->get_post_id($course_id);
2244 global $wpdb;
2245
2246 $reviews = $wpdb->get_results("select {$wpdb->comments}.comment_ID,
2247 {$wpdb->comments}.comment_post_ID,
2248 {$wpdb->comments}.comment_author,
2249 {$wpdb->comments}.comment_author_email,
2250 {$wpdb->comments}.comment_date,
2251 {$wpdb->comments}.comment_content,
2252 {$wpdb->comments}.user_id,
2253 {$wpdb->commentmeta}.meta_value as rating,
2254 {$wpdb->users}.display_name
2255
2256 from {$wpdb->comments}
2257 INNER JOIN {$wpdb->commentmeta}
2258 ON {$wpdb->comments}.comment_ID = {$wpdb->commentmeta}.comment_id
2259 INNER JOIN {$wpdb->users}
2260 ON {$wpdb->comments}.user_id = {$wpdb->users}.ID
2261 WHERE {$wpdb->comments}.comment_post_ID = {$course_id}
2262 AND comment_type = 'tutor_course_rating' AND meta_key = 'tutor_rating' ORDER BY comment_ID DESC LIMIT {$offset},{$limit} ;"
2263 );
2264
2265 return $reviews;
2266 }
2267
2268 /**
2269 * @param int $course_id
2270 *
2271 * @return object
2272 *
2273 * Get course rating
2274 *
2275 * @since v.1.0.0
2276 */
2277 public function get_course_rating($course_id = 0){
2278 $course_id = $this->get_post_id($course_id);
2279
2280 $ratings = array(
2281 'rating_count' => 0,
2282 'rating_sum' => 0,
2283 'rating_avg' => 0.00,
2284 'count_by_value' => array(5 => 0, 4 => 0, 3 => 0, 2 => 0, 1 => 0)
2285 );
2286
2287 global $wpdb;
2288
2289 $rating = $wpdb->get_row("select COUNT(meta_value) as rating_count, SUM(meta_value) as rating_sum
2290 from {$wpdb->comments}
2291 INNER JOIN {$wpdb->commentmeta}
2292 ON {$wpdb->comments}.comment_ID = {$wpdb->commentmeta}.comment_id
2293 WHERE {$wpdb->comments}.comment_post_ID = {$course_id}
2294 AND {$wpdb->comments}.comment_type = 'tutor_course_rating'
2295 AND meta_key = 'tutor_rating' ;"
2296 );
2297
2298 if ($rating->rating_count){
2299 $avg_rating = number_format(($rating->rating_sum / $rating->rating_count), 2);
2300
2301 /**
2302 * Get individual Rating by integer
2303 */
2304 $five_stars_count = $wpdb->get_var("select COUNT(meta_value) as rating_count
2305 from {$wpdb->comments}
2306 INNER JOIN {$wpdb->commentmeta} ON {$wpdb->comments}.comment_ID = {$wpdb->commentmeta}.comment_id
2307 WHERE {$wpdb->comments}.comment_post_ID = {$course_id}
2308 AND {$wpdb->comments}.comment_type = 'tutor_course_rating'
2309 AND meta_key = 'tutor_rating' AND meta_value = 5 ;"
2310 );
2311 $four_stars_count = $wpdb->get_var("select COUNT(meta_value) as rating_count
2312 from {$wpdb->comments}
2313 INNER JOIN {$wpdb->commentmeta} ON {$wpdb->comments}.comment_ID = {$wpdb->commentmeta}.comment_id
2314 WHERE {$wpdb->comments}.comment_post_ID = {$course_id}
2315 AND {$wpdb->comments}.comment_type = 'tutor_course_rating'
2316 AND meta_key = 'tutor_rating' AND meta_value = 4 ;"
2317 );
2318 $three_stars_count = $wpdb->get_var("select COUNT(meta_value) as rating_count
2319 from {$wpdb->comments}
2320 INNER JOIN {$wpdb->commentmeta} ON {$wpdb->comments}.comment_ID = {$wpdb->commentmeta}.comment_id
2321 WHERE {$wpdb->comments}.comment_post_ID = {$course_id}
2322 AND {$wpdb->comments}.comment_type = 'tutor_course_rating'
2323 AND meta_key = 'tutor_rating' AND meta_value = 3 ;"
2324 );
2325 $two_stars_count = $wpdb->get_var("select COUNT(meta_value) as rating_count
2326 from {$wpdb->comments}
2327 INNER JOIN {$wpdb->commentmeta} ON {$wpdb->comments}.comment_ID = {$wpdb->commentmeta}.comment_id
2328 WHERE {$wpdb->comments}.comment_post_ID = {$course_id}
2329 AND {$wpdb->comments}.comment_type = 'tutor_course_rating'
2330 AND meta_key = 'tutor_rating' AND meta_value = 2 ;"
2331 );
2332 $one_stars_count = $wpdb->get_var("select COUNT(meta_value) as rating_count
2333 from {$wpdb->comments}
2334 INNER JOIN {$wpdb->commentmeta} ON {$wpdb->comments}.comment_ID = {$wpdb->commentmeta}.comment_id
2335 WHERE {$wpdb->comments}.comment_post_ID = {$course_id}
2336 AND {$wpdb->comments}.comment_type = 'tutor_course_rating'
2337 AND meta_key = 'tutor_rating' AND meta_value = 1 ;"
2338 );
2339
2340 $ratings = array(
2341 'rating_count' => $rating->rating_count,
2342 'rating_sum' => $rating->rating_sum,
2343 'rating_avg' => $avg_rating,
2344 'count_by_value' => array(5 => $five_stars_count, 4 => $four_stars_count, 3 => $three_stars_count, 2 => $two_stars_count, 1 => $one_stars_count)
2345 );
2346
2347 }
2348
2349 return (object) $ratings;
2350 }
2351
2352 /**
2353 * @param int $user_id
2354 * @param int $offset
2355 * @param int $limit
2356 *
2357 * @return array|null|object
2358 *
2359 * Get reviews by a user
2360 *
2361 * @since v.1.0.0
2362 */
2363 public function get_reviews_by_user($user_id = 0, $offset = 0, $limit = 150){
2364 $user_id = $this->get_user_id($user_id);
2365 global $wpdb;
2366
2367 $reviews = $wpdb->get_results("select {$wpdb->comments}.comment_ID,
2368 {$wpdb->comments}.comment_post_ID,
2369 {$wpdb->comments}.comment_author,
2370 {$wpdb->comments}.comment_author_email,
2371 {$wpdb->comments}.comment_date,
2372 {$wpdb->comments}.comment_content,
2373 {$wpdb->comments}.user_id,
2374 {$wpdb->commentmeta}.meta_value as rating,
2375 {$wpdb->users}.display_name
2376
2377 from {$wpdb->comments}
2378 INNER JOIN {$wpdb->commentmeta}
2379 ON {$wpdb->comments}.comment_ID = {$wpdb->commentmeta}.comment_id
2380 INNER JOIN {$wpdb->users}
2381 ON {$wpdb->comments}.user_id = {$wpdb->users}.ID
2382 WHERE {$wpdb->comments}.user_id = {$user_id}
2383 AND comment_type = 'tutor_course_rating' AND meta_key = 'tutor_rating' ORDER BY comment_ID DESC LIMIT {$offset},{$limit} ;"
2384 );
2385
2386 return $reviews;
2387 }
2388
2389 /**
2390 * @param $instructor_id
2391 *
2392 * @return object
2393 *
2394 * Get instructors rating
2395 *
2396 * @since v.1.0.0
2397 */
2398 public function get_instructor_ratings($instructor_id){
2399 global $wpdb;
2400
2401 $ratings = array(
2402 'rating_count' => 0,
2403 'rating_sum' => 0,
2404 'rating_avg' => 0.00,
2405 );
2406
2407 $rating = $wpdb->get_row("SELECT COUNT(rating.meta_value) as rating_count, SUM(rating.meta_value) as rating_sum
2408 FROM {$wpdb->usermeta} courses
2409 INNER JOIN {$wpdb->comments} reviews ON courses.meta_value = reviews.comment_post_ID AND reviews.comment_type = 'tutor_course_rating'
2410 INNER JOIN {$wpdb->commentmeta} rating ON reviews.comment_ID = rating.comment_id AND rating.meta_key = 'tutor_rating'
2411 WHERE courses.user_id = {$instructor_id} AND courses.meta_key = '_tutor_instructor_course_id'");
2412
2413 if ($rating->rating_count){
2414 $avg_rating = number_format(($rating->rating_sum / $rating->rating_count), 2);
2415
2416 $ratings = array(
2417 'rating_count' => $rating->rating_count,
2418 'rating_sum' => $rating->rating_sum,
2419 'rating_avg' => $avg_rating,
2420 );
2421 }
2422
2423 return (object) $ratings;
2424 }
2425
2426 /**
2427 * @param int $course_id
2428 * @param int $user_id
2429 *
2430 * @return object
2431 *
2432 * Get course rating by user
2433 *
2434 * @since v.1.0.0
2435 */
2436 public function get_course_rating_by_user($course_id = 0, $user_id = 0){
2437 $course_id = $this->get_post_id($course_id);
2438 $user_id = $this->get_user_id($user_id);
2439
2440 $ratings = array(
2441 'rating' => 0,
2442 'review' => '',
2443 );
2444
2445 global $wpdb;
2446
2447 $rating = $wpdb->get_row("select meta_value as rating, comment_content as review from {$wpdb->comments}
2448 INNER JOIN {$wpdb->commentmeta}
2449 ON {$wpdb->comments}.comment_ID = {$wpdb->commentmeta}.comment_id
2450 WHERE {$wpdb->comments}.comment_post_ID = {$course_id} AND user_id = {$user_id}
2451 AND meta_key = 'tutor_rating' ;"
2452 );
2453
2454 if ($rating){
2455 $rating_format = number_format($rating->rating, 2);
2456
2457 $ratings = array(
2458 'rating' => $rating_format,
2459 'review' => $rating->review,
2460 );
2461 }
2462 return (object) $ratings;
2463 }
2464
2465 /**
2466 * @param int $user_id
2467 *
2468 * @return null|string
2469 *
2470 * @since v.1.0.0
2471 */
2472 public function count_reviews_wrote_by_user($user_id = 0){
2473 global $wpdb;
2474 $user_id = $this->get_user_id($user_id);
2475
2476 $count_reviews = $wpdb->get_var("SELECT COUNT(comment_ID) from {$wpdb->comments} WHERE user_id = {$user_id} AND comment_type = 'tutor_course_rating' ");
2477 return $count_reviews;
2478 }
2479
2480 /**
2481 * @param $size
2482 *
2483 * @return bool|int|string
2484 *
2485 * This function transforms the php.ini notation for numbers (like '2M') to an integer.
2486 *
2487 * @since v.1.0.0
2488 */
2489
2490 function let_to_num( $size ) {
2491 $l = substr( $size, -1 );
2492 $ret = substr( $size, 0, -1 );
2493 $byte = 1024;
2494
2495 switch ( strtoupper( $l ) ) {
2496 case 'P':
2497 $ret *= 1024;
2498 // No break.
2499 case 'T':
2500 $ret *= 1024;
2501 // No break.
2502 case 'G':
2503 $ret *= 1024;
2504 // No break.
2505 case 'M':
2506 $ret *= 1024;
2507 // No break.
2508 case 'K':
2509 $ret *= 1024;
2510 // No break.
2511 }
2512 return $ret;
2513 }
2514
2515 /**
2516 * @return array
2517 *
2518 * Get Database version
2519 *
2520 * @since v.1.0.0
2521 */
2522 function get_db_version() {
2523 global $wpdb;
2524
2525 if ( empty( $wpdb->is_mysql ) ) {
2526 return array(
2527 'string' => '',
2528 'number' => '',
2529 );
2530 }
2531
2532 if ( $wpdb->use_mysqli ) {
2533 $server_info = mysqli_get_server_info( $wpdb->dbh ); // @codingStandardsIgnoreLine.
2534 } else {
2535 $server_info = mysql_get_server_info( $wpdb->dbh ); // @codingStandardsIgnoreLine.
2536 }
2537
2538 return array(
2539 'string' => $server_info,
2540 'number' => preg_replace( '/([^\d.]+).*/', '', $server_info ),
2541 );
2542 }
2543
2544 public function help_tip($tip = ''){
2545 return '<span class="tutor-help-tip" data-tip="' . $tip . '"></span>';
2546 }
2547
2548 /**
2549 * @param int $course_id
2550 * @param int $user_id
2551 * @param int $offset
2552 * @param int $limit
2553 *
2554 * @return array|null|object
2555 *
2556 * Get top question
2557 *
2558 * @since v.1.0.0
2559 */
2560 public function get_top_question($course_id = 0, $user_id = 0, $offset = 0, $limit = 20){
2561 $course_id = $this->get_post_id($course_id);
2562 $user_id = $this->get_user_id($user_id);
2563
2564 global $wpdb;
2565
2566 $questions = $wpdb->get_results("select {$wpdb->comments}.comment_ID,
2567 {$wpdb->comments}.comment_post_ID,
2568 {$wpdb->comments}.comment_author,
2569 {$wpdb->comments}.comment_date,
2570 {$wpdb->comments}.comment_content,
2571 {$wpdb->comments}.user_id,
2572 {$wpdb->commentmeta}.meta_value as question_title,
2573 {$wpdb->users}.display_name
2574
2575 from {$wpdb->comments}
2576 INNER JOIN {$wpdb->commentmeta}
2577 ON {$wpdb->comments}.comment_ID = {$wpdb->commentmeta}.comment_id
2578 INNER JOIN {$wpdb->users}
2579 ON {$wpdb->comments}.user_id = {$wpdb->users}.ID
2580 WHERE {$wpdb->comments}.comment_post_ID = {$course_id}
2581 AND {$wpdb->comments}.user_id = {$user_id}
2582 AND {$wpdb->comments}.comment_type = 'tutor_q_and_a'
2583 AND meta_key = 'tutor_question_title' ORDER BY comment_ID DESC LIMIT {$offset},{$limit} ;"
2584 );
2585
2586 return $questions;
2587 }
2588
2589 /**
2590 * @param string $search_term
2591 *
2592 * @return int
2593 *
2594 * Get total number of Q&A questions
2595 *
2596 * @since v.1.0.0
2597 */
2598 public function get_total_qa_question($search_term = ''){
2599 global $wpdb;
2600
2601 if ($search_term){
2602 $search_term = " AND {$wpdb->commentmeta}.meta_value LIKE '%{$search_term}%' ";
2603 }
2604
2605 $user_id = get_current_user_id();
2606 $course_type = tutor()->course_post_type;
2607
2608 $in_question_id_query = '';
2609 /**
2610 * Get only assinged courses questions if current user is a
2611 */
2612 if ( ! current_user_can('administrator') && current_user_can(tutor()->instructor_role)) {
2613 $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' " );
2614 $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} " );
2615 $my_course_ids = array_unique( array_merge( $get_course_ids, $get_assigned_courses_ids ) );
2616
2617 if ( $this->count( $my_course_ids ) ) {
2618 $implode_ids = implode( ',', $my_course_ids );
2619 $in_question_id_query = " AND {$wpdb->comments}.comment_post_ID IN($implode_ids) ";
2620 }
2621 }
2622
2623 $count = $wpdb->get_var("SELECT COUNT({$wpdb->comments}.comment_ID) FROM {$wpdb->comments}
2624 INNER JOIN {$wpdb->commentmeta}
2625 ON {$wpdb->comments}.comment_ID = {$wpdb->commentmeta}.comment_id
2626 WHERE comment_type = 'tutor_q_and_a' AND comment_parent = 0 {$in_question_id_query} {$search_term} ");
2627
2628 return (int) $count;
2629 }
2630
2631 /**
2632 * @param int $start
2633 * @param int $limit
2634 * @param string $search_term
2635 *
2636 * @return array|null|object
2637 *
2638 *
2639 * Get question and answer query
2640 *
2641 * @since v.1.0.0
2642 */
2643 public function get_qa_questions($start = 0, $limit = 10, $search_term = '') {
2644 global $wpdb;
2645
2646 if ($search_term){
2647 $search_term = " AND {$wpdb->commentmeta}.meta_value LIKE '%{$search_term}%' ";
2648 }
2649
2650 $user_id = get_current_user_id();
2651 $course_type = tutor()->course_post_type;
2652
2653 $in_question_id_query = '';
2654 /**
2655 * Get only assinged courses questions if current user is a
2656 */
2657 if ( ! current_user_can('administrator') && current_user_can(tutor()->instructor_role)) {
2658 $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' " );
2659 $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} " );
2660 $my_course_ids = array_unique( array_merge( $get_course_ids, $get_assigned_courses_ids ) );
2661
2662 if ( $this->count( $my_course_ids ) ) {
2663 $implode_ids = implode( ',', $my_course_ids );
2664 $in_question_id_query = " AND {$wpdb->comments}.comment_post_ID IN($implode_ids) ";
2665 }
2666 }
2667
2668 $query = $wpdb->get_results("SELECT
2669 {$wpdb->comments}.comment_ID,
2670 {$wpdb->comments}.comment_post_ID,
2671 {$wpdb->comments}.comment_author,
2672 {$wpdb->comments}.comment_date,
2673 {$wpdb->comments}.comment_content,
2674 {$wpdb->comments}.user_id,
2675 {$wpdb->commentmeta}.meta_value as question_title,
2676 {$wpdb->users}.display_name,
2677 {$wpdb->posts}.post_title,
2678
2679 (SELECT COUNT(answers_t.comment_ID) FROM {$wpdb->comments} answers_t
2680 WHERE answers_t.comment_parent = {$wpdb->comments}.comment_ID ) as answer_count
2681
2682 FROM {$wpdb->comments}
2683
2684 INNER JOIN {$wpdb->commentmeta}
2685 ON {$wpdb->comments}.comment_ID = {$wpdb->commentmeta}.comment_id
2686
2687 INNER JOIN {$wpdb->posts}
2688 ON {$wpdb->comments}.comment_post_ID = {$wpdb->posts}.ID
2689
2690 INNER JOIN {$wpdb->users}
2691 ON {$wpdb->comments}.user_id = {$wpdb->users}.ID
2692
2693 WHERE {$wpdb->comments}.comment_type = 'tutor_q_and_a' AND {$wpdb->comments}.comment_parent = 0 {$search_term}
2694 {$in_question_id_query}
2695 ORDER BY {$wpdb->comments}.comment_ID DESC
2696 LIMIT {$start},{$limit}; ");
2697
2698 return $query;
2699 }
2700
2701 /**
2702 * @param $question_id
2703 *
2704 * @return array|null|object|void
2705 *
2706 * Get question for Q&A
2707 *
2708 * @since v.1.0.0
2709 */
2710 public function get_qa_question($question_id){
2711 global $wpdb;
2712 $query = $wpdb->get_row("SELECT
2713 {$wpdb->comments}.comment_ID,
2714 {$wpdb->comments}.comment_post_ID,
2715 {$wpdb->comments}.comment_author,
2716 {$wpdb->comments}.comment_date,
2717 {$wpdb->comments}.comment_content,
2718 {$wpdb->comments}.user_id,
2719 {$wpdb->commentmeta}.meta_value as question_title,
2720 {$wpdb->users}.display_name,
2721 {$wpdb->posts}.post_title
2722
2723 FROM {$wpdb->comments}
2724 INNER JOIN {$wpdb->commentmeta}
2725 ON {$wpdb->comments}.comment_ID = {$wpdb->commentmeta}.comment_id
2726
2727 INNER JOIN {$wpdb->posts}
2728 ON {$wpdb->comments}.comment_post_ID = {$wpdb->posts}.ID
2729
2730 INNER JOIN {$wpdb->users}
2731 ON {$wpdb->comments}.user_id = {$wpdb->users}.ID
2732 WHERE comment_type = 'tutor_q_and_a' AND {$wpdb->comments}.comment_ID = {$question_id}");
2733
2734 return $query;
2735 }
2736
2737 /**
2738 * @param $question_id
2739 *
2740 * @return array|null|object
2741 *
2742 * Get question and asnwer by question
2743 */
2744 public function get_qa_answer_by_question($question_id){
2745 global $wpdb;
2746 $query = $wpdb->get_results("SELECT
2747 {$wpdb->comments}.comment_ID,
2748 {$wpdb->comments}.comment_post_ID,
2749 {$wpdb->comments}.comment_author,
2750 {$wpdb->comments}.comment_date,
2751 {$wpdb->comments}.comment_content,
2752 {$wpdb->comments}.comment_parent,
2753 {$wpdb->comments}.user_id,
2754 {$wpdb->users}.display_name
2755
2756 FROM {$wpdb->comments}
2757
2758 INNER JOIN {$wpdb->users}
2759 ON {$wpdb->comments}.user_id = {$wpdb->users}.ID
2760 WHERE comment_type = 'tutor_q_and_a'
2761 AND {$wpdb->comments}.comment_parent = {$question_id} ORDER BY {$wpdb->comments}.comment_ID ASC ");
2762
2763 return $query;
2764 }
2765
2766 public function unanswered_question_count(){
2767 global $wpdb;
2768
2769 $count = $wpdb->get_var("select COUNT({$wpdb->comments}.comment_ID)
2770 from {$wpdb->comments}
2771 WHERE {$wpdb->comments}.comment_type = 'tutor_q_and_a'
2772 AND {$wpdb->comments}.comment_approved = 'waiting_for_answer'
2773 AND {$wpdb->comments}.comment_parent = 0;");
2774 return (int) $count;
2775 }
2776
2777 /**
2778 * @param int $course_id
2779 *
2780 * @return array|null|object
2781 *
2782 * Return all of announcements for a course
2783 *
2784 * @since v.1.0.0
2785 */
2786 public function get_announcements($course_id = 0){
2787 $course_id = $this->get_post_id($course_id);
2788 global $wpdb;
2789
2790 $query = $wpdb->get_results("select {$wpdb->posts}.ID, post_author, post_date, post_content, post_title, display_name
2791 from {$wpdb->posts}
2792 INNER JOIN {$wpdb->users} ON post_author = {$wpdb->users}.ID
2793 WHERE post_type = 'tutor_announcements'
2794 AND post_parent = {$course_id} ORDER BY {$wpdb->posts}.ID DESC;");
2795 return $query;
2796 }
2797
2798 /**
2799 * @param string $content
2800 *
2801 * @return mixed
2802 *
2803 * Announcement content
2804 *
2805 * @since v.1.0.0
2806 */
2807
2808 public function announcement_content($content = ''){
2809 $search = array('{user_display_name}');
2810
2811 $user_display_name = 'User';
2812 if (is_user_logged_in()){
2813 $user = wp_get_current_user();
2814 $user_display_name = $user->display_name;
2815 }
2816 $replace = array($user_display_name);
2817
2818 return str_replace($search, $replace, $content);
2819 }
2820
2821 /**
2822 * @param int $post_id
2823 * @param string $option_key
2824 * @param bool $default
2825 *
2826 * @return array|bool|mixed
2827 *
2828 * Get the quiz option from meta
2829 */
2830 public function get_quiz_option($post_id = 0, $option_key = '', $default = false){
2831 $post_id = $this->get_post_id($post_id);
2832 $get_option_meta = maybe_unserialize(get_post_meta($post_id, 'tutor_quiz_option', true));
2833
2834 if ( ! $option_key && ! empty($get_option_meta)) {
2835 return $get_option_meta;
2836 }
2837
2838 $value = $this->avalue_dot( $option_key, $get_option_meta );
2839 if ( $value ) {
2840 return $value;
2841 }
2842 return $default;
2843 }
2844
2845
2846 /**
2847 * @param int $quiz_id
2848 *
2849 * @return array|bool|null|object
2850 *
2851 * Get the questions by quiz ID
2852 */
2853 public function get_questions_by_quiz($quiz_id = 0){
2854 $quiz_id = $this->get_post_id($quiz_id);
2855 global $wpdb;
2856
2857 //$questions = $wpdb->get_results("SELECT ID, post_content, post_title, post_parent from {$wpdb->posts} WHERE post_type = 'tutor_question'
2858 // AND post_parent = {$quiz_id} ORDER BY menu_order ASC ");
2859
2860 $questions = $wpdb->get_results("SELECT * from {$wpdb->prefix}tutor_quiz_questions WHERE quiz_id = {$quiz_id} ORDER BY question_order ASC ");
2861
2862 if (is_array($questions) && count($questions)){
2863 return $questions;
2864 }
2865 return false;
2866 }
2867
2868 /**
2869 * @param int $question_id
2870 *
2871 * @return array|bool|object|void|null
2872 *
2873 * Get Quiz question by question id
2874 */
2875 public function get_quiz_question_by_id($question_id = 0){
2876 global $wpdb;
2877
2878 if ($question_id){
2879 $question = $wpdb->get_row("SELECT * from {$wpdb->prefix}tutor_quiz_questions WHERE question_id = {$question_id} LIMIT 0,1 ;");
2880 return $question;
2881 }
2882
2883 return false;
2884 }
2885
2886 /**
2887 * @param null $type
2888 *
2889 * @return array|mixed
2890 *
2891 * Get all question types
2892 *
2893 * @since v.1.0.0
2894 */
2895
2896 public function get_question_types($type = null){
2897 $types = array(
2898 'true_false' => array('name' => __('True/False', 'tutor'), 'icon' => '<i class="tutor-icon-block tutor-icon-yes-no"></i>'),
2899 'single_choice' => array('name' => __('Single Choice', 'tutor'), 'icon' => '<i class="tutor-icon-block tutor-icon-mark"></i>'),
2900 'multiple_choice' => array('name' => __('Multiple Choice', 'tutor'), 'icon' => '<i class="tutor-icon-block tutor-icon-multiple-choice"></i>'),
2901 'open_ended' => array('name' => __('Open Ended/Essay', 'tutor'), 'icon' => '<i class="tutor-icon-block tutor-icon-open-ended"></i>'),
2902 'short_answer' => array('name' => __('Short Answer', 'tutor'), 'icon' => '<i class="tutor-icon-block tutor-icon-short-ans"></i>'),
2903 'fill_in_the_blank' => array('name' => __('Fill In The Blank', 'tutor'), 'icon' => '<i class="tutor-icon-block tutor-icon-fill-gaps"></i>'),
2904 'answer_sorting' => array('name' => __('Answer Sorting', 'tutor'), 'icon' => '<i class="tutor-icon-block tutor-icon-answer-shorting"></i>'),
2905 'assessment' => array('name' => __('Assessment', 'tutor'), 'icon' => '<i class="tutor-icon-block tutor-icon-assesment"></i>'),
2906 'matching' => array('name' => __('Matching', 'tutor'), 'icon' => '<i class="tutor-icon-block tutor-icon-matching"></i>'),
2907 'image_matching' => array('name' => __('Image Matching', 'tutor'), 'icon' => '<i class="tutor-icon-block tutor-icon-image-matching"></i>'),
2908 'image_answering' => array('name' => __('Image Answering', 'tutor'), 'icon' => '<i class="tutor-icon-block tutor-icon-image-ans"></i>'),
2909 'ordering' => array('name' => __('Ordering', 'tutor'), 'icon' => '<i class="tutor-icon-block tutor-icon-ordering"></i>'),
2910 );
2911
2912 if (isset($types[$type])){
2913 return $types[$type];
2914 }
2915 return $types;
2916 }
2917
2918 public function get_quiz_answer_options_by_question($question_id){
2919 global $wpdb;
2920
2921 $answer_options = $wpdb->get_results("select
2922 {$wpdb->comments}.comment_ID,
2923 {$wpdb->comments}.comment_post_ID,
2924 {$wpdb->comments}.comment_content
2925
2926 FROM {$wpdb->comments}
2927 WHERE {$wpdb->comments}.comment_post_ID = {$question_id}
2928 AND {$wpdb->comments}.comment_type = 'quiz_answer_option'
2929 ORDER BY {$wpdb->comments}.comment_karma ASC ;");
2930
2931 if (is_array($answer_options) && count($answer_options)){
2932 return $answer_options;
2933 }
2934 return false;
2935 }
2936
2937 /**
2938 * @param $quiz_id
2939 *
2940 * @return int
2941 *
2942 * Get the next question order ID
2943 *
2944 * @since v.1.0.0
2945 */
2946
2947 public function quiz_next_question_order_id($quiz_id){
2948 global $wpdb;
2949
2950 //$last_order = (int) $wpdb->get_var("SELECT MAX(menu_order) FROM {$wpdb->posts} WHERE post_parent = {$quiz_id} AND post_type =
2951 // 'tutor_question';");
2952
2953 $last_order = (int) $wpdb->get_var("SELECT MAX(question_order) FROM {$wpdb->prefix}tutor_quiz_questions WHERE quiz_id = {$quiz_id} ;");
2954 return $last_order + 1;
2955 }
2956
2957 /**
2958 * @param $quiz_id
2959 *
2960 * @return int
2961 *
2962 * new design quiz question
2963 * @since v.1.0.0
2964 */
2965 public function quiz_next_question_id(){
2966 global $wpdb;
2967
2968 $last_order = (int) $wpdb->get_var("SELECT MAX(question_id) FROM {$wpdb->prefix}tutor_quiz_questions;");
2969 return $last_order + 1;
2970 }
2971
2972 public function get_quiz_id_by_question($question_id){
2973 global $wpdb;
2974
2975 $quiz_id = $wpdb->get_var("SELECT post_parent FROM {$wpdb->posts} WHERE ID = {$question_id} AND post_type = 'tutor_question' ;");
2976 return $quiz_id;
2977 }
2978
2979 /**
2980 * @param array $config
2981 *
2982 * @return array|bool|null|object
2983 *
2984 * It was used in previous quiz algorithm
2985 *
2986 * @deprecated
2987 *
2988 * @since v.1.0.0
2989 */
2990 public function get_unattached_quiz($config = array()){
2991 global $wpdb;
2992
2993 $default_attr = array(
2994 'search_term' => '',
2995 'start' => '0',
2996 'limit' => '10',
2997 'order' => 'DESC',
2998 'order_by' => 'ID',
2999 );
3000 $attr = array_merge($default_attr, $config);
3001 extract($attr);
3002
3003 $search_query = '';
3004 if (! empty($search_term)){
3005 $search_query = "AND post_title LIKE '%{$search_term}%'";
3006 }
3007
3008 $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} ");
3009
3010 if (is_array($questions) && count($questions)){
3011 return $questions;
3012 }
3013 return false;
3014 }
3015
3016 /**
3017 * @param int $post_id
3018 *
3019 * @return array|bool|null|object
3020 *
3021 * @since v.1.0.0
3022 */
3023 public function get_attached_quiz($post_id = 0){
3024 global $wpdb;
3025
3026 $post_id = $this->get_post_id($post_id);
3027
3028 $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}");
3029
3030 if (is_array($questions) && count($questions)){
3031 return $questions;
3032 }
3033 return false;
3034 }
3035
3036 /**
3037 * @param $quiz_id
3038 *
3039 * @return array|bool|null|object|void
3040 *
3041 * Get course by quiz
3042 *
3043 * @since v.1.0.0
3044 */
3045
3046 public function get_course_by_quiz($quiz_id){
3047 global $wpdb;
3048
3049 $quiz_id = $this->get_post_id($quiz_id);
3050 $post = get_post($quiz_id);
3051
3052 if ($post) {
3053 $course_post_type = tutor()->course_post_type;
3054 $course = $wpdb->get_row( "select ID, post_name, post_type, post_parent from {$wpdb->posts} where ID = {$post->post_parent} " );
3055
3056 if ($course) {
3057 //Checking if this topic
3058 if ( $course->post_type !== $course_post_type ) {
3059 $course = $wpdb->get_row( "select ID, post_name, post_type, post_parent from {$wpdb->posts} where ID = {$course->post_parent} " );
3060 }
3061 //Checking if this lesson
3062 if ( $course->post_type !== $course_post_type ) {
3063 $course = $wpdb->get_row( "select ID, post_name, post_type, post_parent from {$wpdb->posts} where ID = {$course->post_parent} " );
3064 }
3065
3066 return $course;
3067 }
3068 }
3069
3070 return false;
3071 }
3072
3073 /**
3074 * @param $quiz_id
3075 *
3076 * @return int
3077 *
3078 * @since v.1.0.0
3079 */
3080 public function total_questions_for_student_by_quiz($quiz_id){
3081 $quiz_id = $this->get_post_id($quiz_id);
3082 global $wpdb;
3083
3084 $total_question = (int) $wpdb->get_var("select count(ID) from {$wpdb->posts} where post_parent = {$quiz_id} AND post_type = 'tutor_question' ");
3085
3086 return $total_question;
3087 }
3088
3089 /**
3090 * @param int $quiz_id
3091 *
3092 * @return array|null|object|void
3093 *
3094 * Determine if there is any started quiz exists
3095 *
3096 * @since v.1.0.0
3097 */
3098
3099 public function is_started_quiz($quiz_id = 0){
3100 global $wpdb;
3101
3102 $quiz_id = $this->get_post_id($quiz_id);
3103 $user_id = get_current_user_id();
3104
3105 $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' ");
3106
3107 return $is_started;
3108 }
3109
3110 /**
3111 * @param $quiz_id
3112 *
3113 * Method for get the total amount of question for a quiz
3114 * Student will answer this amount of question, one quiz have many question
3115 * but student will answer a specific amount of questions
3116 *
3117 * @return int
3118 *
3119 * @since v.1.0.0
3120 */
3121
3122 public function max_questions_for_take_quiz($quiz_id){
3123 $quiz_id = $this->get_post_id($quiz_id);
3124 global $wpdb;
3125
3126 $max_questions = (int) $wpdb->get_var("select count(question_id) from {$wpdb->prefix}tutor_quiz_questions where quiz_id = {$quiz_id} ");
3127 $max_mentioned = (int) $this->get_quiz_option($quiz_id, 'max_questions_for_answer', 10);
3128
3129 if ($max_mentioned < $max_questions ){
3130 return $max_mentioned;
3131 }
3132
3133 return $max_questions;
3134 }
3135
3136 /**
3137 * @param int $attempt_id
3138 *
3139 * @return array|bool|null|object|void
3140 *
3141 * Get single quiz attempt
3142 *
3143 * @since v.1.0.0
3144 */
3145 public function get_attempt($attempt_id = 0){
3146 global $wpdb;
3147 if ( ! $attempt_id){
3148 return false;
3149 }
3150 $attempt = $wpdb->get_row("SELECT * FROM {$wpdb->prefix}tutor_quiz_attempts WHERE attempt_id = {$attempt_id} ");
3151 return $attempt;
3152 }
3153
3154 /**
3155 * @param $attempt_info
3156 *
3157 * @return mixed
3158 *
3159 * Get unserialize attempt info
3160 *
3161 * @since v.1.0.0
3162 */
3163
3164 public function quiz_attempt_info($attempt_info){
3165 return maybe_unserialize($attempt_info);
3166 }
3167
3168 /**
3169 * @param $quiz_attempt_id
3170 * @param array $attempt_info
3171 *
3172 * @return bool|int
3173 *
3174 * Update attempt for various action
3175 *
3176 * @since v.1.0.0
3177 */
3178 public function quiz_update_attempt_info($quiz_attempt_id, $attempt_info = array()){
3179 $answers = tutor_utils()->avalue_dot('answers', $attempt_info);
3180 $total_marks = array_sum(wp_list_pluck($answers, 'question_mark'));
3181 $earned_marks = tutor_utils()->avalue_dot('marks_earned', $attempt_info);
3182 $earned_mark_percent = $earned_marks > 0 ? ( number_format(($earned_marks * 100) / $total_marks)) : 0;
3183 update_comment_meta($quiz_attempt_id, 'earned_mark_percent', $earned_mark_percent);
3184
3185 return update_comment_meta($quiz_attempt_id,'quiz_attempt_info', $attempt_info);
3186 }
3187
3188 /**
3189 * @param int $quiz_id
3190 *
3191 * @return array|null|object
3192 *
3193 * Get random question by quiz id
3194 *
3195 * @since v.1.0.0
3196 */
3197
3198 public function get_random_question_by_quiz($quiz_id = 0){
3199 global $wpdb;
3200
3201 $quiz_id = $this->get_post_id($quiz_id);
3202 $is_attempt = $this->is_started_quiz($quiz_id);
3203
3204 $tempSql = " AND question_type = 'matching' ";
3205 $questions = $wpdb->get_results("SELECT * FROM {$wpdb->prefix}tutor_quiz_questions WHERE quiz_id = {$quiz_id} {$tempSql} ORDER BY RAND() LIMIT 0,1 ");
3206
3207 return $questions;
3208 }
3209
3210 /**
3211 * @param int $quiz_id
3212 *
3213 * @return array|null|object
3214 *
3215 * Get random questions by quiz
3216 */
3217 public function get_random_questions_by_quiz($quiz_id = 0){
3218 global $wpdb;
3219
3220 $quiz_id = $this->get_post_id($quiz_id);
3221 $attempt = $this->is_started_quiz($quiz_id);
3222 if ( ! $attempt){
3223 return false;
3224 }
3225
3226 $questions = $wpdb->get_results("SELECT * FROM {$wpdb->prefix}tutor_quiz_questions WHERE quiz_id = {$quiz_id} ORDER BY RAND() LIMIT {$attempt->total_questions} ");
3227
3228 return $questions;
3229 }
3230
3231 /**
3232 * @param $question_id
3233 * @param bool $rand
3234 *
3235 * @return array|bool|null|object
3236 *
3237 * Get answers list by quiz question
3238 *
3239 * @since v.1.0.0
3240 */
3241 public function get_answers_by_quiz_question($question_id, $rand = false){
3242 global $wpdb;
3243
3244
3245 $question = $wpdb->get_row("SELECT * from {$wpdb->prefix}tutor_quiz_questions WHERE question_id = {$question_id} ;");
3246 if ( ! $question){
3247 return false;
3248 }
3249
3250 $order = " answer_order ASC ";
3251 if ($question->question_type === 'ordering'){
3252 $order = " RAND() ";
3253 }
3254
3255 if ($rand){
3256 $order = " RAND() ";
3257 }
3258
3259 $answers = $wpdb->get_results("SELECT * FROM {$wpdb->prefix}tutor_quiz_question_answers WHERE belongs_question_id = {$question_id} AND belongs_question_type =
3260 '{$question->question_type}' order by {$order} ");
3261 return $answers;
3262 }
3263
3264 /**
3265 * @param int $quiz_id
3266 * @param int $user_id
3267 *
3268 * @return array|bool|null|object
3269 *
3270 * Get all of the attempts by an user of a quiz
3271 *
3272 * @since v.1.0.0
3273 */
3274
3275 public function quiz_attempts($quiz_id = 0, $user_id = 0){
3276 global $wpdb;
3277
3278 $quiz_id = $this->get_post_id($quiz_id);
3279 $user_id = $this->get_user_id($user_id);
3280
3281 $attempts = $wpdb->get_results("SELECT * FROM {$wpdb->prefix}tutor_quiz_attempts WHERE quiz_id = {$quiz_id} AND user_id = {$user_id} ");
3282
3283 if (is_array($attempts) && count($attempts)){
3284 return $attempts;
3285 }
3286
3287 return false;
3288 }
3289
3290 /**
3291 * @param string $search_term
3292 *
3293 * @return int
3294 *
3295 * Total number of quiz attempts
3296 *
3297 * @since v.1.0.0
3298 */
3299
3300 public function get_total_quiz_attempts($search_term = ''){
3301 global $wpdb;
3302
3303 if ($search_term){
3304 $search_term = " AND ( user_email like '%{$search_term}%' OR display_name like '%{$search_term}%' OR post_title like '%{$search_term}%' ) ";
3305 }
3306
3307 $count = $wpdb->get_var("SELECT COUNT(attempt_id)
3308 FROM {$wpdb->prefix}tutor_quiz_attempts quiz_attempts
3309 INNER JOIN {$wpdb->posts} quiz
3310 ON quiz_attempts.quiz_id = quiz.ID
3311 INNER JOIN {$wpdb->users}
3312 ON quiz_attempts.user_id = {$wpdb->users}.ID
3313 WHERE 1=1 AND quiz_attempts.attempt_ended_at <= NOW() {$search_term} ");
3314 return (int) $count;
3315 }
3316
3317 /**
3318 * @param int $start
3319 * @param int $limit
3320 * @param string $search_term
3321 *
3322 * @return array|null|object
3323 *
3324 *
3325 * Get the all quiz attempts
3326 *
3327 * @since v.1.0.0
3328 */
3329 public function get_quiz_attempts($start = 0, $limit = 10, $search_term = '') {
3330 global $wpdb;
3331
3332 if ($search_term){
3333 $search_term = " AND ( user_email like '%{$search_term}%' OR display_name like '%{$search_term}%' OR post_title like '%{$search_term}%' ) ";
3334 }
3335
3336 $query = $wpdb->get_results("SELECT *
3337 FROM {$wpdb->prefix}tutor_quiz_attempts quiz_attempts
3338 INNER JOIN {$wpdb->posts} quiz
3339 ON quiz_attempts.quiz_id = quiz.ID
3340 INNER JOIN {$wpdb->users}
3341 ON quiz_attempts.user_id = {$wpdb->users}.ID
3342 WHERE 1=1 AND quiz_attempts.attempt_ended_at <= NOW() {$search_term}
3343 ORDER BY quiz_attempts.attempt_id DESC
3344 LIMIT {$start},{$limit}; ");
3345 return $query;
3346 }
3347
3348 public function get_quiz_attempts_by_course_ids($start = 0, $limit = 10, $course_ids = array(), $search_term = '') {
3349 global $wpdb;
3350
3351 if ($search_term){
3352 $search_term = " AND ( user_email like '%{$search_term}%' OR display_name like '%{$search_term}%' OR post_title like '%{$search_term}%' ) ";
3353 }
3354
3355 $course_ids_in = implode($course_ids, ',');
3356 $sql = " AND quiz_attempts.course_id IN({$course_ids_in}) ";
3357 $search_term = $sql.$search_term;
3358
3359 $query = $wpdb->get_results("SELECT *
3360 FROM {$wpdb->prefix}tutor_quiz_attempts quiz_attempts
3361 INNER JOIN {$wpdb->posts} quiz
3362 ON quiz_attempts.quiz_id = quiz.ID
3363 INNER JOIN {$wpdb->users}
3364 ON quiz_attempts.user_id = {$wpdb->users}.ID
3365 WHERE 1=1 AND quiz_attempts.attempt_ended_at <= NOW() {$search_term}
3366 ORDER BY quiz_attempts.attempt_id DESC
3367 LIMIT {$start},{$limit}; ");
3368 return $query;
3369 }
3370
3371 public function get_total_quiz_attempts_by_course_ids($course_ids = array(), $search_term = ''){
3372 global $wpdb;
3373
3374 if ($search_term){
3375 $search_term = " AND ( user_email like '%{$search_term}%' OR display_name like '%{$search_term}%' OR post_title like '%{$search_term}%' ) ";
3376 }
3377
3378 $course_ids_in = implode($course_ids, ',');
3379 $sql = " AND quiz_attempts.course_id IN({$course_ids_in}) ";
3380 $search_term = $sql.$search_term;
3381
3382 $count = $wpdb->get_var("SELECT COUNT(attempt_id)
3383 FROM {$wpdb->prefix}tutor_quiz_attempts quiz_attempts
3384 INNER JOIN {$wpdb->posts} quiz
3385 ON quiz_attempts.quiz_id = quiz.ID
3386 INNER JOIN {$wpdb->users}
3387 ON quiz_attempts.user_id = {$wpdb->users}.ID
3388 WHERE 1=1 AND quiz_attempts.attempt_ended_at <= NOW() {$search_term} ");
3389 return (int) $count;
3390 }
3391
3392 /**
3393 * @param $attempt_id
3394 *
3395 * @return array|null|object
3396 *
3397 * Get quiz answers by attempt id
3398 *
3399 * @since v.1.0.0
3400 */
3401 public function get_quiz_answers_by_attempt_id($attempt_id){
3402 global $wpdb;
3403
3404 $results = $wpdb->get_results("SELECT answers.*, question.question_title, question.question_type
3405 FROM {$wpdb->prefix}tutor_quiz_attempt_answers answers
3406 LEFT JOIN {$wpdb->prefix}tutor_quiz_questions question ON answers.question_id = question.question_id
3407 WHERE answers.quiz_attempt_id = {$attempt_id} ");
3408
3409 return $results;
3410 }
3411
3412 /**
3413 * @param $answer_id
3414 *
3415 * @return array|null|object
3416 *
3417 * Get single answer by answer_id
3418 *
3419 * @since v.1.0.0
3420 */
3421 public function get_answer_by_id($answer_id){
3422 global $wpdb;
3423
3424 if (is_array($answer_id)){
3425 $in_ids = implode(",", $answer_id);
3426 $sql = "answer.answer_id IN({$in_ids})";
3427 }else{
3428 $sql = "answer.answer_id = {$answer_id}";
3429 }
3430
3431 $answer = $wpdb->get_results("SELECT answer.*, question.question_title, question.question_type
3432 FROM {$wpdb->prefix}tutor_quiz_question_answers answer
3433 LEFT JOIN {$wpdb->prefix}tutor_quiz_questions question ON answer.belongs_question_id = question.question_id
3434 WHERE 1=1 AND {$sql} ");
3435
3436 return $answer;
3437 }
3438
3439 /**
3440 * @param $ids
3441 *
3442 * @return array|bool|null|object
3443 *
3444 * Get quiz answers by ids
3445 *
3446 * @since v.1.0.0
3447 */
3448
3449 public function get_quiz_answers_by_ids($ids){
3450 $ids = (array) $ids;
3451
3452 if (!count($ids)){
3453 return false;
3454 }
3455
3456 $in_ids = implode(",", $ids);
3457
3458 global $wpdb;
3459 $query = $wpdb->get_results("SELECT
3460 comment_ID,
3461 comment_content
3462 FROM {$wpdb->comments}
3463 WHERE comment_type = 'quiz_answer_option' AND comment_ID IN({$in_ids}) ");
3464
3465 if (is_array($query) && count($query)){
3466 return $query;
3467 }
3468
3469 return false;
3470 }
3471
3472 /**
3473 * @param null $level
3474 *
3475 * @return mixed
3476 *
3477 * Get the users / students / course levels
3478 *
3479 * @since v.1.0.0
3480 */
3481
3482 public function course_levels($level = null){
3483 $levels = apply_filters('tutor_course_level', array(
3484 'all_levels' => __('All Levels', 'tutor'),
3485 'beginner' => __('Beginner', 'tutor'),
3486 'intermediate' => __('Intermediate', 'tutor'),
3487 'expert' => __('Expert', 'tutor'),
3488 ));
3489
3490 if ($level){
3491 if (isset($levels[$level])){
3492 return $levels[$level];
3493 }else{
3494 return '';
3495 }
3496 }
3497
3498 return $levels;
3499 }
3500
3501 /**
3502 * @return mixed|void
3503 *
3504 * Get user permalink for dashboard
3505 *
3506 * @since v.1.0.0
3507 */
3508 public function user_profile_permalinks(){
3509 $permalinks = array(
3510 'enrolled_course' => __('Enrolled Course', 'tutor'),
3511 'courses_taken' => __('Courses Taken', 'tutor'),
3512 'reviews_wrote' => __('Reviews Written', 'tutor'),
3513 );
3514
3515 return apply_filters('tutor_public_profile/permalinks', $permalinks);
3516 }
3517
3518 /**
3519 * @return bool|false|string
3520 *
3521 * Student registration form
3522 *
3523 * @since v.1.0.0
3524 */
3525 public function student_register_url(){
3526 $student_register_page = (int) $this->get_option('student_register_page');
3527
3528 if ($student_register_page){
3529 return get_the_permalink($student_register_page);
3530 }
3531 return false;
3532 }
3533
3534 /**
3535 * @return false|string
3536 *
3537 * Get frontend dashboard URL
3538 */
3539 public function tutor_dashboard_url(){
3540 $page_id = (int) tutor_utils()->get_option('student_dashboard');
3541 $page_id = apply_filters('tutor_dashboard_url', $page_id);
3542 return get_the_permalink($page_id);
3543 }
3544
3545 /**
3546 * @param int $course_id
3547 * @param int $user_id
3548 *
3549 * @return bool
3550 *
3551 * is_wishlisted();
3552 *
3553 * @since v.1.0.0
3554 */
3555 public function is_wishlisted($course_id = 0, $user_id = 0){
3556 $course_id = $this->get_post_id($course_id);
3557 $user_id = $this->get_user_id($user_id);
3558 if ( ! $user_id){
3559 return false;
3560 }
3561
3562 global $wpdb;
3563 $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} ;");
3564
3565 return $if_added_to_list;
3566 }
3567
3568 /**
3569 * @param int $user_id
3570 *
3571 * @return array|null|object
3572 *
3573 * Get the wish lists by an user
3574 *
3575 * @since v.1.0.0
3576 */
3577 public function get_wishlist($user_id = 0){
3578 $user_id = $this->get_user_id($user_id);
3579 global $wpdb;
3580
3581 $query = "SELECT $wpdb->posts.*
3582 FROM $wpdb->posts
3583 LEFT JOIN $wpdb->usermeta ON ($wpdb->posts.ID = $wpdb->usermeta.meta_value)
3584 WHERE $wpdb->usermeta.meta_key = '_tutor_course_wishlist'
3585 AND $wpdb->usermeta.user_id = {$user_id}
3586 ORDER BY $wpdb->usermeta.umeta_id DESC ";
3587 $pageposts = $wpdb->get_results($query, OBJECT);
3588 return $pageposts;
3589 }
3590
3591 /**
3592 * @param int $limit
3593 *
3594 * @return array|null|object
3595 *
3596 * Getting popular courses
3597 *
3598 * @since v.1.0.0
3599 */
3600 public function most_popular_courses($limit = 10){
3601 global $wpdb;
3602
3603 $courses = $wpdb->get_results("
3604 SELECT COUNT(enrolled.ID) as total_enrolled,
3605 enrolled.post_parent as course_id,
3606 course.*
3607 from {$wpdb->posts} enrolled
3608 INNER JOIN {$wpdb->posts} course ON enrolled.post_parent = course.ID
3609 WHERE enrolled.post_type = 'tutor_enrolled' AND enrolled.post_status = 'completed'
3610
3611 GROUP BY course_id
3612 ORDER BY total_enrolled DESC LIMIT 0,{$limit} ;");
3613
3614 return $courses;
3615 }
3616
3617 /**
3618 * @param int $limit
3619 *
3620 * @return array|bool|null|object
3621 *
3622 * Get most rated courses lists
3623 *
3624 * @since v.1.0.0
3625 */
3626 public function most_rated_courses($limit = 10){
3627 global $wpdb;
3628
3629 $result = $wpdb->get_results("
3630 SELECT COUNT(comment_ID) AS total_rating,
3631 comment_ID,
3632 comment_post_ID,
3633 course.*
3634 FROM {$wpdb->comments}
3635 INNER JOIN {$wpdb->posts} course ON comment_post_ID = course.ID
3636 WHERE {$wpdb->comments}.comment_type = 'tutor_course_rating' AND {$wpdb->comments}.comment_approved = 'approved'
3637 GROUP BY comment_post_ID ORDER BY total_rating DESC LIMIT 0,{$limit}
3638 ;");
3639
3640 if (is_array($result) && count($result)){
3641 return $result;
3642 }
3643 return false;
3644 }
3645
3646 /**
3647 * @param null $addon_field
3648 *
3649 * @return bool
3650 *
3651 * Get Addon config
3652 *
3653 * @since v.1.0.0
3654 */
3655 public function get_addon_config($addon_field = null){
3656 if ( ! $addon_field){
3657 return false;
3658 }
3659
3660 $addonsConfig = maybe_unserialize(get_option('tutor_addons_config'));
3661
3662 if (isset($addonsConfig[$addon_field])){
3663 return $addonsConfig[$addon_field];
3664 }
3665
3666 return false;
3667 }
3668
3669 /**
3670 * @return array|false|string
3671 *
3672 * Get the IP from visitor
3673 *
3674 * @since v.1.0.0
3675 */
3676 function get_ip() {
3677 $ipaddress = '';
3678 if (getenv('HTTP_CLIENT_IP'))
3679 $ipaddress = getenv('HTTP_CLIENT_IP');
3680 else if(getenv('HTTP_X_FORWARDED_FOR'))
3681 $ipaddress = getenv('HTTP_X_FORWARDED_FOR');
3682 else if(getenv('HTTP_X_FORWARDED'))
3683 $ipaddress = getenv('HTTP_X_FORWARDED');
3684 else if(getenv('HTTP_FORWARDED_FOR'))
3685 $ipaddress = getenv('HTTP_FORWARDED_FOR');
3686 else if(getenv('HTTP_FORWARDED'))
3687 $ipaddress = getenv('HTTP_FORWARDED');
3688 else if(getenv('REMOTE_ADDR'))
3689 $ipaddress = getenv('REMOTE_ADDR');
3690 else
3691 $ipaddress = 'UNKNOWN';
3692 return $ipaddress;
3693 }
3694
3695 /**
3696 * @return mixed|void
3697 *
3698 * Get the social icons
3699 *
3700 * @since v.1.0.4
3701 */
3702
3703 public function tutor_social_share_icons(){
3704 $icons = array(
3705 'facebook' => array('share_class' => 's_facebook', 'icon_html' => '<i class="tutor-icon-facebook"></i>' ),
3706 'twitter' => array('share_class' => 's_twitter', 'icon_html' => '<i class="tutor-icon-twitter"></i>' ),
3707 'linkedin' => array('share_class' => 's_linkedin', 'icon_html' => '<i class="tutor-icon-linkdin"></i>' ),
3708 'tumblr' => array('share_class' => 's_tumblr', 'icon_html' => '<i class="tutor-icon-tumblr"></i>' ),
3709 );
3710
3711 return apply_filters('tutor_social_share_icons', $icons);
3712 }
3713
3714 /**
3715 * @param array $array
3716 *
3717 * @return bool
3718 *
3719 * count method with check is_array
3720 *
3721 * @since v.1.0.4
3722 */
3723 public function count($array = array()){
3724 return is_array($array) && count($array);
3725 }
3726
3727 }