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