public static function calculate_course_completion($student_id, $course_id, $update = true) { if (empty($course_id)) { return false; } $cache_key = __METHOD__ . '-' . $student_id . '-' . $course_id; if (CoursePress_Cache::cp_cache_get($cache_key)) { return CoursePress_Cache::cp_cache_get($cache_key); } $data = self::get_completion_data($student_id, $course_id); $course = new Course($course_id); $total_units = $course->get_units($course_id, 'publish', true); // No units or no units published if (empty($total_units)) { CoursePress_Cache::cp_cache_set($cache_key, 0); return 0; } $progress = 0.0; if (isset($data['unit']) && is_array($data['unit'])) { foreach ($data['unit'] as $unit_id => $unit) { if ('publish' == get_post_status($unit_id)) { $progress += self::calculate_unit_completion($student_id, $course_id, $unit_id, $update, $data); } } $progress = $progress / $total_units; $progress = $progress > 100 ? 100 : $progress; $data['course_progress'] = $progress; } if ($update) { self::update_completion_data($student_id, $course_id, $data); } if (100 == (int) $progress) { do_action('coursepress_set_course_completed', $student_id, $course_id); } CoursePress_Cache::cp_cache_set($cache_key, $progress); return $progress; }