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