| 1 |
<?php |
| 2 |
|
| 3 |
// returns a list of requirements |
| 4 |
function wpc_get_ordering_list($post_type = 'wpc-requirement'){ |
| 5 |
$data = ''; |
| 6 |
$args = array( |
| 7 |
'post_type' => $post_type, |
| 8 |
'orderby' => 'menu_order', |
| 9 |
'nopaging' => true, |
| 10 |
'order' => 'ASC', |
| 11 |
'posts_per_page' => -1, |
| 12 |
); |
| 13 |
$query = new WP_Query($args); |
| 14 |
if($query->have_posts()){ |
| 15 |
$data .= '<ul class="lesson-list">'; |
| 16 |
while($query->have_posts()){ |
| 17 |
$query->the_post(); |
| 18 |
$id = get_the_ID(); |
| 19 |
$data .= '<li class="lesson-button wpc-order-lesson-list-lesson" data-post-type="' . esc_attr($post_type) . '" data-id="' . get_the_ID() . '"><i class="fa fa-bars wpc-grab"></i> ' . get_the_title() . '<a href="' . get_edit_post_link($id) . '" style="float:right;"> (Edit)</a></li>'; |
| 20 |
} |
| 21 |
$data .= '</ul>'; |
| 22 |
} else { |
| 23 |
$data = esc_html( __('There is nothing to order', 'wp-courses') ) . '.'; |
| 24 |
} |
| 25 |
wp_reset_postdata(); |
| 26 |
return $data; |
| 27 |
} |
| 28 |
|
| 29 |
// get rule by ID |
| 30 |
function wpc_get_rule_by_ID($rule_id){ |
| 31 |
global $wpdb; |
| 32 |
$table_name = $wpdb->prefix . 'wpc_rules'; |
| 33 |
$sql = "SELECT id, post_id, course_id, module_id, lesson_id, type, action, percent, times FROM {$table_name} WHERE id = {$rule_id}"; |
| 34 |
$results = $wpdb->get_results($sql); |
| 35 |
return $results; |
| 36 |
} |
| 37 |
|
| 38 |
// return rules rows |
| 39 |
function wpc_get_rules($post_id){ |
| 40 |
global $wpdb; |
| 41 |
$table = $wpdb->prefix . 'wpc_rules'; |
| 42 |
$sql = "SELECT id, post_id, course_id, module_id, lesson_id, action, percent, type, times FROM {$table} WHERE post_id = {$post_id}"; |
| 43 |
$results = $wpdb->get_results($sql); |
| 44 |
return $results; |
| 45 |
} |
| 46 |
|
| 47 |
// checks which rules have been completed and returns rule tracking |
| 48 |
function wpc_check_rules($post_id, $user_id){ |
| 49 |
|
| 50 |
if(!is_user_logged_in()){ |
| 51 |
return; |
| 52 |
} |
| 53 |
|
| 54 |
global $wpdb; |
| 55 |
|
| 56 |
// get published courses |
| 57 |
$table_name = $wpdb->prefix . 'posts'; |
| 58 |
$sql = "SELECT ID FROM {$table_name} WHERE post_status = 'publish' AND post_type = 'course'"; |
| 59 |
$courses = $wpdb->get_results($sql); |
| 60 |
|
| 61 |
// get published modules |
| 62 |
$mod_table_name = $wpdb->prefix . 'posts'; |
| 63 |
$mod_sql = "SELECT ID FROM $mod_table_name WHERE post_type = 'wpc-module' AND post_status = 'publish'"; |
| 64 |
$modules = $wpdb->get_results($mod_sql); |
| 65 |
|
| 66 |
$completed_lessons = wpc_get_tracked_lessons_by_user($user_id, 1); |
| 67 |
$viewed_lessons = wpc_get_tracked_lessons_by_user($user_id, 0); |
| 68 |
|
| 69 |
$posts_table = $wpdb->prefix . 'posts'; |
| 70 |
$sql = "SELECT ID FROM $posts_table WHERE post_type = 'wpc-quiz' AND post_status = 'publish'"; |
| 71 |
$quizzes = $wpdb->get_results($sql, ARRAY_N); |
| 72 |
$total_quiz_count = $wpdb->num_rows; |
| 73 |
$quiz_ids = array_map('end', $quizzes); |
| 74 |
$quiz_ids = array_map('strval', $quiz_ids); |
| 75 |
$quiz_ids = "'" . implode("', '", $quiz_ids) . "'"; |
| 76 |
|
| 77 |
$table_name = $wpdb->prefix . "wpc_tracking"; |
| 78 |
$sql = "SELECT post_id FROM $table_name WHERE post_id IN ( {$quiz_ids} ) AND user_id = $user_id"; |
| 79 |
$viewed_quizzes =$wpdb->get_results($sql); |
| 80 |
$viewed_quizzes_count = $wpdb->num_rows; |
| 81 |
|
| 82 |
$quiz_table_name = $wpdb->prefix . "wpc_quiz_results"; |
| 83 |
$sql = "SELECT id FROM $quiz_table_name WHERE user_ID = $user_id"; |
| 84 |
$completed_quizzes = $wpdb->get_results($sql); |
| 85 |
$completed_quizzes_count = $wpdb->num_rows; |
| 86 |
|
| 87 |
$rule_tracking = array(); |
| 88 |
$completed_rule = false; |
| 89 |
|
| 90 |
$rules = wpc_get_rules($post_id); |
| 91 |
|
| 92 |
foreach($rules as $rule) { |
| 93 |
if($rule->action == 'views'){ |
| 94 |
|
| 95 |
if($rule->type == 'specific-lesson'){ |
| 96 |
$completed_rule = wpc_has_done($rule->lesson_id, $viewed_lessons, 0) == true ? true : false; |
| 97 |
} elseif($rule->type == 'any-lesson'){ |
| 98 |
$completed_rule = count($viewed_lessons) >= $rule->times ? true : false; |
| 99 |
} elseif($rule->type == 'specific-course'){ |
| 100 |
$completed_rule = wpc_get_percent_done($rule->course_id, $user_id, 0) >= $rule->percent ? true : false; |
| 101 |
} elseif($rule->type == 'any-course'){ |
| 102 |
$times = 0; |
| 103 |
foreach($courses as $course){ |
| 104 |
if( wpc_get_percent_done($rule->course_id, $user_id, 0) >= $rule->percent ){ |
| 105 |
$times++; |
| 106 |
} |
| 107 |
} |
| 108 |
|
| 109 |
$completed_rule = $times >= $rule->times ? true : false; |
| 110 |
|
| 111 |
} elseif($rule->type == 'specific-module'){ |
| 112 |
$completed_rule = wpc_get_module_percent_done($rule->module_id, $user_id, 0) >= $rule->percent ? true : false; |
| 113 |
} elseif($rule->type == 'any-module'){ |
| 114 |
|
| 115 |
$times = 0; |
| 116 |
|
| 117 |
if(!empty($modules)) { |
| 118 |
foreach($modules as $module) { |
| 119 |
if( wpc_get_module_percent_done($module->ID, $user_id, 0) >= $rule->percent ){ |
| 120 |
$times++; |
| 121 |
} |
| 122 |
} |
| 123 |
} |
| 124 |
|
| 125 |
$completed_rule = $times >= $rule->times ? true : false; |
| 126 |
|
| 127 |
} elseif($rule->type == 'any-quiz'){ |
| 128 |
|
| 129 |
$completed_rule = $viewed_quizzes_count >= $rule->times ? true : false; |
| 130 |
|
| 131 |
} elseif($rule->type == 'specific-quiz'){ |
| 132 |
$completed_rule = wpc_has_done($rule->lesson_id, $viewed_lessons, 0) == true ? true : false; |
| 133 |
} |
| 134 |
|
| 135 |
} elseif($rule->action == 'completes'){ |
| 136 |
|
| 137 |
if($rule->type == 'specific-lesson'){ |
| 138 |
$completed_rule = wpc_has_done($rule->lesson_id, $completed_lessons, 1) == true ? true : false; |
| 139 |
} elseif($rule->type == 'any-lesson'){ |
| 140 |
|
| 141 |
//var_dump($completed_lessons); |
| 142 |
|
| 143 |
$count = count($completed_lessons); |
| 144 |
$completed_rule = $count >= (int) $rule->times ? true : false; |
| 145 |
|
| 146 |
} elseif($rule->type == 'specific-course'){ |
| 147 |
$completed_rule = (int) wpc_get_percent_done($rule->course_id, $user_id, 1) >= (int) $rule->percent ? true : false; |
| 148 |
} elseif($rule->type == 'any-course'){ |
| 149 |
$times = 0; |
| 150 |
foreach($courses as $course){ |
| 151 |
if( wpc_get_percent_done($course->ID, $user_id, 1) >= (int) $rule->percent ){ |
| 152 |
$times++; |
| 153 |
} |
| 154 |
} |
| 155 |
|
| 156 |
$completed_rule = $times >= (int) $rule->times ? true : false; |
| 157 |
|
| 158 |
} elseif($rule->type == 'specific-module'){ |
| 159 |
$completed_rule = wpc_get_module_percent_done($rule->module_id, $user_id, 1) >= (int) $rule->percent ? true : false; |
| 160 |
} elseif($rule->type == 'any-module'){ |
| 161 |
|
| 162 |
$times = 0; |
| 163 |
|
| 164 |
if(!empty($modules)) { |
| 165 |
foreach($modules as $module) { |
| 166 |
//echo wpc_get_module_percent_done($module->ID, $user_id, 1); |
| 167 |
if( wpc_get_module_percent_done($module->ID, $user_id, 1) >= (int) $rule->percent ){ |
| 168 |
$times++; |
| 169 |
} |
| 170 |
} |
| 171 |
} |
| 172 |
|
| 173 |
$completed_rule = $times >= $rule->times ? true : false; |
| 174 |
|
| 175 |
} elseif($rule->type == 'any-quiz'){ |
| 176 |
|
| 177 |
$completed_rule = $completed_quizzes_count >= $rule->times ? true : false; |
| 178 |
|
| 179 |
} elseif($rule->type == 'specific-quiz'){ |
| 180 |
$completed_rule = wpc_has_done($rule->lesson_id, $completed_lessons, 1) == true ? true : false; |
| 181 |
} |
| 182 |
|
| 183 |
} elseif($rule->action == 'scores') { |
| 184 |
|
| 185 |
$quiz_table_name = $wpdb->prefix . 'wpc_quiz_results'; |
| 186 |
|
| 187 |
if($rule->type == 'specific-quiz'){ |
| 188 |
// check if user has met or exceeded score requirements for specific quiz |
| 189 |
$quiz_sql = "SELECT user_ID, quiz_ID, score_percent FROM {$quiz_table_name} WHERE user_ID = {$user_id} AND score_percent >= {$rule->percent} AND quiz_ID = {$rule->lesson_id}"; |
| 190 |
$quiz_results = $wpdb->get_results($quiz_sql); |
| 191 |
$count = $wpdb->num_rows; |
| 192 |
$completed_rule = $count >= 1 ? true : false; |
| 193 |
} elseif($rule->type == 'any-quiz'){ |
| 194 |
// check if user has met or exceeded score and times requirements for any quiz |
| 195 |
$quiz_sql = "SELECT user_ID, score_percent FROM {$quiz_table_name} WHERE user_ID = {$user_id} AND score_percent >= {$rule->percent}"; |
| 196 |
$quiz_results = $wpdb->get_results($quiz_sql); |
| 197 |
$count = $wpdb->num_rows; |
| 198 |
$completed_rule = $count >= $rule->times ? true : false; |
| 199 |
} |
| 200 |
} |
| 201 |
|
| 202 |
// push rule results to tracking array |
| 203 |
array_unshift($rule_tracking, array( |
| 204 |
'rule_id' => $rule->id, |
| 205 |
'rule_status' => $completed_rule, |
| 206 |
)); |
| 207 |
|
| 208 |
} |
| 209 |
|
| 210 |
return $rule_tracking; |
| 211 |
|
| 212 |
} |
| 213 |
|
| 214 |
function wpc_get_awards(){ |
| 215 |
|
| 216 |
if(!is_user_logged_in()){ |
| 217 |
return 'false'; |
| 218 |
} |
| 219 |
|
| 220 |
$data = ''; |
| 221 |
|
| 222 |
$award_badge = false; |
| 223 |
$award_certificate = false; |
| 224 |
$award_email = false; |
| 225 |
$new_status = null; |
| 226 |
|
| 227 |
// get all badges, certificates and emails |
| 228 |
$args = array( |
| 229 |
'post_type' => array('wpc-badge', 'wpc-certificate', 'wpc-email'), |
| 230 |
'posts_per_page' => -1, |
| 231 |
'paged' => false, |
| 232 |
'post_status' => array('publish'), |
| 233 |
); |
| 234 |
|
| 235 |
$query = new WP_Query($args); |
| 236 |
|
| 237 |
$user = wp_get_current_user(); |
| 238 |
$user_id = get_current_user_id(); |
| 239 |
$requirement_tracking = get_user_meta($user_id, 'wpc-requirement-tracking', true); |
| 240 |
$tracking = array(); |
| 241 |
$to_award = array(); |
| 242 |
$home_url = home_url(); |
| 243 |
|
| 244 |
if($query->have_posts()){ |
| 245 |
while($query->have_posts()){ |
| 246 |
$query->the_post(); |
| 247 |
$post_id = get_the_ID(); |
| 248 |
$rule_tracking = wpc_check_rules($post_id, $user_id); |
| 249 |
$old_status = false; |
| 250 |
$award_post_type = get_post_type(); |
| 251 |
$oldTime = time(); |
| 252 |
|
| 253 |
if(!empty($requirement_tracking)){ |
| 254 |
foreach($requirement_tracking as $req) { |
| 255 |
if( $req['id'] == $post_id ){ |
| 256 |
$old_status = $req['status']; |
| 257 |
$oldTime = $req['time']; |
| 258 |
break; |
| 259 |
} |
| 260 |
} |
| 261 |
} |
| 262 |
|
| 263 |
// check if all rules have been met |
| 264 |
foreach($rule_tracking as $rule) { |
| 265 |
if($rule['rule_status'] == true){ |
| 266 |
$new_status = true; |
| 267 |
} else { |
| 268 |
$new_status = false; |
| 269 |
break; |
| 270 |
} |
| 271 |
} |
| 272 |
|
| 273 |
if($old_status == false && $new_status == true){ |
| 274 |
// Requirements have been met. Give award. |
| 275 |
|
| 276 |
$to_award[] = array( |
| 277 |
'id' => $post_id, |
| 278 |
'type' => $award_post_type, |
| 279 |
); |
| 280 |
|
| 281 |
if($award_post_type == 'wpc-badge'){ |
| 282 |
$award_badge = true; |
| 283 |
} elseif($award_post_type == 'wpc-certificate'){ |
| 284 |
$award_certificate = true; |
| 285 |
} elseif($award_post_type == 'wpc-email'){ |
| 286 |
$award_email = true; |
| 287 |
} |
| 288 |
|
| 289 |
$newTime = time(); |
| 290 |
} |
| 291 |
|
| 292 |
$time = $newTime ? $newTime : $oldTime; |
| 293 |
|
| 294 |
// push results to array so we can store in user meta |
| 295 |
array_unshift($tracking, array( |
| 296 |
'id' => $post_id, |
| 297 |
'type' => $award_post_type, |
| 298 |
'status' => $new_status, |
| 299 |
'rules' => $rule_tracking, |
| 300 |
'time' => $time, |
| 301 |
)); |
| 302 |
|
| 303 |
} // end while |
| 304 |
|
| 305 |
wp_reset_postdata(); |
| 306 |
|
| 307 |
update_user_meta( $user_id, 'wpc-requirement-tracking', $tracking ); |
| 308 |
|
| 309 |
$status = get_user_meta($user_id, 'wpc-email-status', true); |
| 310 |
|
| 311 |
$opt_in = get_option('wpc_opt_in'); |
| 312 |
if($opt_in != 'true' && empty($status) || $status == 'false') { |
| 313 |
$send_email = 'false'; |
| 314 |
} else{ |
| 315 |
$send_email = 'true'; |
| 316 |
} |
| 317 |
|
| 318 |
// check if we should send the email(s) |
| 319 |
|
| 320 |
if($award_email == true && $send_email != 'false') { |
| 321 |
|
| 322 |
$global_name = get_option('wpc_email_from_name'); |
| 323 |
$global_from = get_option('wpc_email_from'); |
| 324 |
$global_cc = get_option('wpc_cc'); |
| 325 |
$global_bcc = get_option('wpc_bcc'); |
| 326 |
|
| 327 |
$headers = array('Content-Type: text/html; charset=UTF-8'); |
| 328 |
$headers = apply_filters('wpc_email_headers', $headers); |
| 329 |
|
| 330 |
$name = get_option('wpc_business_name'); |
| 331 |
$unit = get_option('wpc_unit_number'); |
| 332 |
$address = get_option('wpc_physical_address'); |
| 333 |
$city = get_option('wpc_city'); |
| 334 |
$state = get_option('wpc_state'); |
| 335 |
$zip = get_option('wpc_zip_code'); |
| 336 |
$country = get_option('wpc_country'); |
| 337 |
|
| 338 |
$address = '<p><br><b>' . $name . '</b><br>' . $unit . ' ' . $address . '<br>'; |
| 339 |
$address .= $city . ' ' . $state . ' ' . $zip . '<br>'; |
| 340 |
$address .= $country . '</p>'; |
| 341 |
$address = apply_filters('wpc_email_signature', $address); |
| 342 |
$admin_email = get_bloginfo('admin_email'); |
| 343 |
|
| 344 |
foreach($to_award as $email){ |
| 345 |
if($email['type'] == 'wpc-email'){ |
| 346 |
if(!empty($user->user_email)){ |
| 347 |
|
| 348 |
// send the email |
| 349 |
|
| 350 |
$this_name = get_post_meta((int) $email['id'], 'wpc-email-from-name', true); |
| 351 |
$this_from = get_post_meta((int) $email['id'], 'wpc-email-from', true); |
| 352 |
$this_cc = get_post_meta((int) $email['id'], 'wpc-cc', true); |
| 353 |
$this_bcc = get_post_meta((int) $email['id'], 'wpc-bcc', true); |
| 354 |
|
| 355 |
$name = empty($this_name) ? ' ' . $global_name : ' ' . $global_name; |
| 356 |
|
| 357 |
if(!empty($this_from)){ |
| 358 |
$header = !empty($name) ? 'From:' . $name . ' <' . $this_from . '>' : 'From:' . $this_from; |
| 359 |
$headers[] = $header; |
| 360 |
} elseif(!empty($global_from)){ |
| 361 |
$header = !empty($name) ? 'From:' . $name . ' <' . $global_from . '>' : 'From:' . $global_from; |
| 362 |
$headers[] = $header; |
| 363 |
} elseif(!empty($admin_email)) { |
| 364 |
$header = !empty($name) ? 'From:' . $name . ' <' . $admin_email . '>' : 'From:' . $admin_email; |
| 365 |
$headers[] = $header; |
| 366 |
} |
| 367 |
|
| 368 |
if(!empty($this_cc)) { |
| 369 |
$header = 'CC: ' . $this_cc; |
| 370 |
$headers[] = $header; |
| 371 |
} elseif(!empty($global_cc)) { |
| 372 |
$header = 'CC: ' . $global_cc; |
| 373 |
$headers[] = $header; |
| 374 |
} |
| 375 |
|
| 376 |
if(!empty($this_bcc)) { |
| 377 |
$header = 'BCC: ' . $this_bcc; |
| 378 |
$headers[] = $header; |
| 379 |
} elseif(!empty($global_cc)) { |
| 380 |
$header = 'BCC: ' . $global_bcc; |
| 381 |
$headers[] = $header; |
| 382 |
} |
| 383 |
|
| 384 |
$unsub = '<p class="unsub"><a href="' . esc_url($home_url) . '?wpc-user-id=' . (int) $user_id . '&wpc-unsub=true">' . esc_html( __('Click here', 'wpc-emails') ) . '</a> ' . 'to unsubscribe from future emails from ' . esc_url($home_url) . '</p>'; |
| 385 |
$unsub = apply_filters('wpc_email_unsub', $unsub); |
| 386 |
$content = wpautop(get_post_field('post_content', $email['id'])) . $address . $unsub; |
| 387 |
|
| 388 |
// replace placeholders with dynamic content |
| 389 |
$content = str_replace('{email}', $user->user_email, $content); |
| 390 |
$content = str_replace('{fname}', $user->first_name, $content); |
| 391 |
$content = str_replace('{lname}', $user->last_name, $content); |
| 392 |
$content = str_replace('{username}', $user->display_name, $content); |
| 393 |
|
| 394 |
$subject = html_entity_decode(get_the_title($email['id'])); |
| 395 |
|
| 396 |
if(!empty($address) && !empty($city) && !empty($zip) && !empty($country)){ |
| 397 |
wp_mail($user->user_email, $subject , wp_kses($content, 'post'), $headers); |
| 398 |
} |
| 399 |
} |
| 400 |
} |
| 401 |
} |
| 402 |
} |
| 403 |
|
| 404 |
// display awarded badges |
| 405 |
if($award_badge == true || $award_certificate == true){ |
| 406 |
|
| 407 |
$data .= '<div class="wpc-award-lightbox-content wpc-award-slider">'; |
| 408 |
|
| 409 |
foreach($to_award as $award){ |
| 410 |
if($award['type'] == 'wpc-certificate'){ |
| 411 |
$data .= '<div class="wpc-wiggle">' . wpc_render_tiny_certificate($user_id, $award['id']) . '</div>'; |
| 412 |
} elseif($award['type'] == 'wpc-badge') { |
| 413 |
$data .= '<div class="wpc-single-awarded wpc-wiggle">'; |
| 414 |
$data .= wpc_render_badge($award['id']); |
| 415 |
$data .= '</div>'; |
| 416 |
} |
| 417 |
} |
| 418 |
|
| 419 |
$data .= '</div>'; |
| 420 |
|
| 421 |
$fireworks = '<div class="wpc-pyro"><div class="wpc-before"></div><div class="wpc-after"></div></div>'; |
| 422 |
return $data . $fireworks; |
| 423 |
} else { |
| 424 |
return 'false'; |
| 425 |
} |
| 426 |
|
| 427 |
} else { |
| 428 |
return 'false'; // no post results |
| 429 |
} // end if |
| 430 |
} |
| 431 |
|
| 432 |
|
| 433 |
function wpc_has_requirement($post_id, $user_id = null){ |
| 434 |
|
| 435 |
if($user_id === null) { |
| 436 |
$user_id = get_current_user_id(); |
| 437 |
} |
| 438 |
|
| 439 |
$requirements = get_user_meta($user_id, 'wpc-requirement-tracking', true); |
| 440 |
|
| 441 |
if(empty($requirements)){ |
| 442 |
return false; |
| 443 |
} |
| 444 |
|
| 445 |
$has = false; |
| 446 |
|
| 447 |
foreach($requirements as $requirement){ |
| 448 |
if($post_id == $requirement['id']){ |
| 449 |
if($requirement['status'] == true){ |
| 450 |
$has = true; |
| 451 |
break; |
| 452 |
} |
| 453 |
} else { |
| 454 |
$has = false; |
| 455 |
} |
| 456 |
} |
| 457 |
|
| 458 |
return $has; |
| 459 |
|
| 460 |
} |
| 461 |
|
| 462 |
?> |