function unit_traverse() { $unit_id = $_POST['id']; $course_id = $_POST['course_id']; if (!isset($_POST['security']) || !wp_verify_nonce($_POST['security'], 'security')) { _e('Security check Failed. Contact Administrator.', 'vibe'); die; } //verify unit in course $units = bp_course_get_curriculum_units($course_id); if (!in_array($unit_id, $units)) { _e('Unit not in Course', 'vibe'); die; } // Check if user has taken the course $user_id = get_current_user_id(); $coursetaken = get_user_meta($user_id, $course_id, true); if (!isset($_COOKIE['course'])) { if ($coursetaken > time()) { setcookie('course', $course_id, $expire, '/'); $_COOKIE['course'] = $course_id; } else { $pid = get_post_meta($course_id, 'vibe_product', true); $pid = apply_filters('wplms_course_product_id', $pid, $course_id, -1); // $id checks for Single Course page or Course page in the my courses section if (is_numeric($pid)) { $pid = get_permalink($pid); } echo '<div class="message"><p>' . __('Course Expired.', 'vibe') . '<a href="' . $pid . '" class="link alignright">' . __('Click to renew', 'vibe') . '</a></p></div>'; die; } } if (isset($coursetaken) && $coursetaken) { if (!bp_course_check_unit_complete($unit_id, $user_id)) { // IF unit not completed by user // Drip Feed Check $drip_enable = get_post_meta($course_id, 'vibe_course_drip', true); if (vibe_validate($drip_enable)) { $drip_duration = get_post_meta($course_id, 'vibe_course_drip_duration', true); $drip_duration_parameter = apply_filters('vibe_drip_duration_parameter', 86400, $course_id); $total_drip_turation = apply_filters('vibe_total_drip_duration', $drip_duration * $drip_duration_parameter, $course_id, $unit_id); $unitkey = array_search($unit_id, $units); for ($i = $unitkey - 1; $i >= 0; $i--) { if (get_post_type($units[$i]) == 'unit') { $pre_unit_key = $i; break; } } if ($unitkey == 0) { // Start of Course $pre_unit_time = bp_course_get_drip_access_time($units[$unitkey], $user_id); if (!isset($pre_unit_time) || $pre_unit_time == '') { bp_course_update_drip_access_time($units[$unitkey], $user_id, time()); if (is_numeric($units[1])) { //Parmas : Next Unit, Next timestamp, course_id, userid do_action('wplms_start_unit', $units[$unitkey], $course_id, $user_id, $units[1], time() + $drip_duration * $drip_duration_parameter); } } } else { //Continuation of Course $pre_unit_time = get_post_meta($units[$pre_unit_key], $user_id, true); if (isset($pre_unit_time) && $pre_unit_time) { $value = $pre_unit_time + $drip_duration * $drip_duration_parameter; $value = apply_filters('wplms_drip_value', $value, $units[$pre_unit_key], $course_id, $units[$unitkey]); //print_r(date('l jS \of F Y h:i:s A',$value).' > '.date('l jS \of F Y h:i:s A',time())); if ($value > time()) { echo '<div class="message"><p>' . __('Unit will be available in ', 'vibe') . tofriendlytime($value - time()) . '</p></div>'; die; } else { $pre_unit_time = get_post_meta($units[$unitkey], $user_id, true); if (!isset($pre_unit_time) || $pre_unit_time == '') { update_post_meta($units[$unitkey], $user_id, time()); //Parmas : Next Unit, Next timestamp, course_id, userid do_action('wplms_start_unit', $units[$unitkey], $course_id, $user_id, $units[$unitkey + 1], time() + $drip_duration * $drip_duration_parameter); } } } else { echo '<div class="message"><p>' . __('Unit can not be accessed.', 'vibe') . '</p></div>'; die; } } } // Drip enabled } // END Drip Feed Check echo '<div id="unit" class="' . get_post_type($unit_id) . '_title" data-unit="' . $unit_id . '">'; do_action('wplms_unit_header', $unit_id, $course_id); $duration = get_post_meta($unit_id, 'vibe_duration', true); $unit_duration_parameter = apply_filters('vibe_unit_duration_parameter', 60, $unit_id); if ($duration) { do_action('wplms_course_unit_meta', $unit_id); echo '<span><i class="icon-clock"></i> ' . tofriendlytime($unit_duration_parameter * $duration) . '</span>'; } echo '<br /><h1>' . get_the_title($unit_id) . '</h1>'; the_sub_title($unit_id); echo '<div class="clear"></div>'; echo '</div>'; the_unit($unit_id); $unit_class = 'unit_button'; $hide_unit = 0; $nextunit_access = vibe_get_option('nextunit_access'); $k = array_search($unit_id, $units); $done_flag = get_user_meta($user_id, $unit_id, true); $next = $k + 1; $prev = $k - 1; $max = count($units) - 1; echo '<div class="unit_prevnext"><div class="col-md-3">'; if ($prev >= 0) { if (get_post_type($units[$prev]) == 'quiz') { echo '<a href="#" data-unit="' . $units[$prev] . '" class="unit ' . $unit_class . '">' . __('Previous Quiz', 'vibe') . '</a>'; } else { echo '<a href="#" id="prev_unit" data-unit="' . $units[$prev] . '" class="unit unit_button">' . __('Previous Unit', 'vibe') . '</a>'; } } echo '</div>'; $quiz_passing_flag = true; echo '<div class="col-md-6">'; if (get_post_type($units[$k]) == 'quiz') { $quiz_status = get_user_meta($user_id, $units[$k], true); if (is_numeric($quiz_status)) { $quiz_passing_flag = apply_filters('wplms_next_unit_access', true, $units[$k]); if ($quiz_status < time()) { echo '<a href="' . bp_loggedin_user_domain() . BP_COURSE_SLUG . '/' . BP_COURSE_RESULTS_SLUG . '/?action=' . $units[$k] . '" class="quiz_results_popup">' . __('Check Results', 'vibe') . '</a>'; } else { $quiz_class = apply_filters('wplms_in_course_quiz', ''); echo '<a href="' . get_permalink($units[$k]) . '" class=" unit_button ' . $quiz_class . ' continue">' . __('Continue Quiz', 'vibe') . '</a>'; } } else { $quiz_class = apply_filters('wplms_in_course_quiz', ''); echo '<a href="' . get_permalink($units[$k]) . '" class=" unit_button ' . $quiz_class . '">' . __('Start Quiz', 'vibe') . '</a>'; } } else { echo isset($done_flag) && $done_flag ? '' : apply_filters('wplms_unit_mark_complete', '<a href="#" id="mark-complete" data-unit="' . $units[$k] . '" class="unit_button">' . __('Mark this Unit Complete', 'vibe') . '</a>', $unit_id, $course_id); } echo '</div>'; echo '<div class="col-md-3">'; if ($next <= $max) { if (isset($nextunit_access) && $nextunit_access) { $hide_unit = 1; if (isset($done_flag) && $done_flag) { $unit_class .= ' '; $hide_unit = 0; } else { $unit_class .= ' hide'; $hide_unit = 1; } } if (get_post_type($units[$next]) == 'quiz') { if ($quiz_passing_flag) { echo '<a href="#" id="next_quiz" data-unit="' . $units[$next] . '" class="unit ' . $unit_class . '">' . __('Proceed to Quiz', 'vibe') . '</a>'; } } else { if ($quiz_passing_flag) { echo '<a href="#" id="next_unit" ' . ($hide_unit ? '' : 'data-unit="' . $units[$next] . '"') . ' class="unit ' . $unit_class . '">' . __('Next Unit', 'vibe') . '</a>'; } } } echo '</div></div>'; } die; }
function wplms_course_download_stats() { $course_id = $_POST['course']; if (!isset($_POST['security']) || !wp_verify_nonce($_POST['security'], 'security')) { echo __('Security check failed !', 'vibe'); die; } if (!current_user_can('edit_posts') || !is_numeric($course_id)) { echo __('User does not have capability to download stats !', 'vibe'); die; } $fields = json_decode(stripslashes($_POST['fields'])); $type = stripslashes($_POST['type']); if (!isset($type)) { die; } $users = array(); $csv = array(); $csv_title = array(); global $wpdb, $bp; switch ($type) { case 'all_students': $users = $wpdb->get_results($wpdb->prepare("SELECT user_id FROM {$wpdb->usermeta} WHERE meta_key = %s ", 'course_status' . $course_id), ARRAY_A); break; case 'finished_students': $users = $wpdb->get_results($wpdb->prepare("SELECT user_id FROM {$wpdb->usermeta} WHERE meta_key = %s AND meta_value = %d", 'course_status' . $course_id, 4), ARRAY_A); break; case 'pursuing_students': $users = $wpdb->get_results($wpdb->prepare("SELECT user_id FROM {$wpdb->usermeta} WHERE meta_key = %s AND meta_value < %d", 'course_status' . $course_id, 4), ARRAY_A); break; case 'badge_students': $users = $wpdb->get_results($wpdb->prepare("SELECT user_id FROM {$wpdb->usermeta} WHERE meta_key = %s AND meta_value LIKE %s", 'badges', "%{$course_id}%"), ARRAY_A); break; case 'certificate_students': $users = $wpdb->get_results($wpdb->prepare("SELECT user_id FROM {$wpdb->usermeta} WHERE meta_key = %s AND meta_value LIKE %s", 'certificates', "%{$course_id}%"), ARRAY_A); break; } if (count($users)) { foreach ($users as $user) { $user_id = $user['user_id']; $i = 0; foreach ($fields as $k => $field) { switch ($field) { case 'stats_student_start_date': $title = __('START DATE', 'vibe'); if (!in_array($title, $csv_title)) { $csv_title[$i] = $title; } $date = $wpdb->get_results($wpdb->prepare("SELECT date_recorded FROM {$bp->activity->table_name} WHERE type=%s AND user_id = %d and item_id = %d", 'start_course', $user_id, $course_id)); if (is_array($date) && is_object($date[0]) && isset($date[0]->date_recorded)) { $csv[$i][] = $date[0]->date_recorded; } else { $csv[$i][] = __('N.A', 'vibe'); } break; case 'stats_student_completion_date': $title = __('COMPLETION DATE', 'vibe'); if (!in_array($title, $csv_title)) { $csv_title[$i] = $title; } $date = $wpdb->get_results($wpdb->prepare("SELECT date_recorded FROM {$bp->activity->table_name} WHERE type=%s AND user_id = %d and item_id = %d", 'submit_course', $user_id, $course_id)); if (is_array($date) && is_object($date[0]) && isset($date[0]->date_recorded)) { $csv[$i][] = $date[0]->date_recorded; } else { $csv[$i][] = __('N.A', 'vibe'); } break; case 'stats_student_id': $title = __('ID', 'vibe'); if (!in_array($title, $csv_title)) { $csv_title[$i] = $title; } $csv[$i][] = $user_id; break; case 'stats_student_name': $title = __('NAME', 'vibe'); if (!in_array($title, $csv_title)) { $csv_title[$i] = $title; } $csv[$i][] = bp_core_get_username($user_id); break; case 'stats_student_unit_status': $units = bp_course_get_curriculum_units($course_id); foreach ($units as $unit_id) { if (get_post_type($unit_id) == 'unit') { $title = get_the_title($unit_id); if (!in_array($title, $csv_title)) { $csv_title[$i] = $title; } if (bp_course_check_unit_complete($unit_id, $user_id)) { $csv[$i][] = 1; } else { $csv[$i][] = 0; } $i++; } } break; case 'stats_student_quiz_score': $units = bp_course_get_curriculum_units($course_id); foreach ($units as $unit_id) { if (get_post_type($unit_id) == 'quiz') { $title = get_the_title($unit_id); if (!in_array($title, $csv_title)) { $csv_title[$i] = $title; } $score = get_post_meta($unit_id, $user_id, true); if (!isset($score) || !$score) { $csv[$i][] = __('N.A', 'vibe'); } else { $csv[$i][] = $score; } $i++; } } break; case 'stats_student_badge': $title = __('BADGE', 'vibe'); if (!in_array($title, $csv_title)) { $csv_title[$i] = $title; } $check = $wpdb->get_results($wpdb->prepare("SELECT COUNT(meta_key) as count FROM {$wpdb->usermeta} WHERE meta_key = %s AND user_id = %d AND meta_value LIKE %s", 'badges', $user_id, "%{$course_id}%"), ARRAY_A); if (isset($check) && is_array($check)) { if ($check[0]['count']) { $csv[$i][] = 1; } else { $csv[$i][] = 0; } } break; case 'stats_student_certificate': $title = __('CERTIFICATE', 'vibe'); if (!in_array($title, $csv_title)) { $csv_title[$i] = $title; } $check = $wpdb->get_results($wpdb->prepare("SELECT COUNT(meta_key) as count FROM {$wpdb->usermeta} WHERE meta_key = %s AND user_id = %d AND meta_value LIKE %s", 'certificates', $user_id, "%{$course_id}%"), ARRAY_A); if (isset($check) && is_array($check)) { if ($check[0]['count']) { $csv[$i][] = 1; } else { $csv[$i][] = 0; } } break; case 'stats_student_marks': $title = __('SCORE', 'vibe'); if (!in_array($title, $csv_title)) { $csv_title[$i] = $title; } $score = get_post_meta($course_id, $user_id, true); $csv[$i][] = $score; break; default: do_action_ref_array('wplms_course_stats_process', array(&$csv_title, &$csv, &$i, &$course_id, &$user_id, &$field)); break; } $i++; } } } if (!count($csv) || !is_array($csv[0])) { echo '#'; die; } $dir = wp_upload_dir(); $user_id = get_current_user_id(); $file_name = 'download_' . $course_id . '_' . $user_id . '.csv'; $filepath = $dir['basedir'] . '/stats/'; if (!file_exists($filepath)) { mkdir($filepath, 0755); } $file = $filepath . $file_name; if (file_exists($file)) { unlink($file); } if (($handle = fopen($file, "w")) !== FALSE) { fputcsv($handle, $csv_title); $rows = count($csv[0]); for ($i = 0; $i < $rows; $i++) { $arr = array(); foreach ($csv as $key => $f) { $arr[] = $f[$i]; } fputcsv($handle, $arr); } } fclose($handle); $file_url = $dir['baseurl'] . '/stats/' . $file_name; echo $file_url; die; }