| 1 |
<?php |
| 2 |
/** |
| 3 |
* Functions |
| 4 |
* |
| 5 |
* @package AutomatorWP\LearnDash\Functions |
| 6 |
* @author AutomatorWP <contact@automatorwp.com>, Ruben Garcia <rubengcdev@gmail.com> |
| 7 |
* @since 1.0.0 |
| 8 |
*/ |
| 9 |
// Exit if accessed directly |
| 10 |
if( !defined( 'ABSPATH' ) ) exit; |
| 11 |
|
| 12 |
/** |
| 13 |
* Retrieves the post ID. |
| 14 |
* |
| 15 |
* @since 1.0.0 |
| 16 |
* |
| 17 |
* @param mixed $thing |
| 18 |
* |
| 19 |
* @return int|false |
| 20 |
*/ |
| 21 |
function automatorwp_learndash_get_post_id( $thing ) { |
| 22 |
|
| 23 |
if( $thing instanceof WP_Post ) { |
| 24 |
return absint( $thing->ID ); |
| 25 |
} |
| 26 |
|
| 27 |
if( is_numeric( $thing ) ) { |
| 28 |
|
| 29 |
if( absint( $thing ) === 0 ) { |
| 30 |
return false; |
| 31 |
} else { |
| 32 |
return absint( $thing ); |
| 33 |
} |
| 34 |
} |
| 35 |
|
| 36 |
return false; |
| 37 |
} |
| 38 |
|
| 39 |
/** |
| 40 |
* Helper function to mark a quiz as completed |
| 41 |
* |
| 42 |
* @since 1.0.0 |
| 43 |
* |
| 44 |
* @param int $user_id |
| 45 |
* @param int $quiz_id |
| 46 |
* @param int $course_id |
| 47 |
*/ |
| 48 |
function automatorwp_learndash_mark_quiz_as_completed( $user_id = 0, $quiz_id = 0, $course_id = 0 ) { |
| 49 |
|
| 50 |
$user_meta = get_user_meta( $user_id, '_sfwd-quizzes', true ); |
| 51 |
$quiz_progress = empty( $user_meta ) ? array() : $user_meta; |
| 52 |
|
| 53 |
$quiz_meta = get_post_meta( $quiz_id, '_sfwd-quiz', true ); |
| 54 |
|
| 55 |
$quiz_data = array( |
| 56 |
'quiz' => $quiz_id, |
| 57 |
'score' => 0, |
| 58 |
'count' => 0, |
| 59 |
'pass' => true, |
| 60 |
'rank' => '-', |
| 61 |
'time' => time(), |
| 62 |
'pro_quizid' => $quiz_meta['sfwd-quiz_quiz_pro'], |
| 63 |
'course' => $course_id, |
| 64 |
'points' => 0, |
| 65 |
'total_points' => 0, |
| 66 |
'percentage' => 0, |
| 67 |
'timespent' => 0, |
| 68 |
'has_graded' => false, |
| 69 |
'statistic_ref_id' => 0, |
| 70 |
'm_edit_by' => 9999999, // Manual Edit By ID. |
| 71 |
'm_edit_time' => time(), // Manual Edit timestamp. |
| 72 |
); |
| 73 |
|
| 74 |
$quiz_progress[] = $quiz_data; |
| 75 |
|
| 76 |
// Add the quiz entry to the user activity |
| 77 |
learndash_update_user_activity( |
| 78 |
array( |
| 79 |
'course_id' => $course_id, |
| 80 |
'user_id' => $user_id, |
| 81 |
'post_id' => $quiz_id, |
| 82 |
'activity_type' => 'quiz', |
| 83 |
'activity_action' => 'insert', |
| 84 |
'activity_status' => $quiz_data['pass'], |
| 85 |
'activity_started' => $quiz_data['time'], |
| 86 |
'activity_completed' => $quiz_data['time'], |
| 87 |
'activity_meta' => $quiz_data, |
| 88 |
) |
| 89 |
); |
| 90 |
|
| 91 |
// Update user quiz progress |
| 92 |
if ( ! empty( $quiz_progress ) ) { |
| 93 |
update_user_meta( $user_id, '_sfwd-quizzes', $quiz_progress ); |
| 94 |
} |
| 95 |
|
| 96 |
} |
| 97 |
|
| 98 |
/** |
| 99 |
* Helper function to mark a topic as completed |
| 100 |
* |
| 101 |
* @since 1.0.0 |
| 102 |
* |
| 103 |
* @param int $user_id |
| 104 |
* @param int $topic_id |
| 105 |
* @param int $course_id |
| 106 |
*/ |
| 107 |
function automatorwp_learndash_mark_topic_as_completed( $user_id = 0, $topic_id = 0, $course_id = 0 ) { |
| 108 |
|
| 109 |
// Get all topic's quizzes |
| 110 |
$quizzes = learndash_get_lesson_quiz_list( $topic_id, $user_id, $course_id ); // learndash_get_lesson_quiz_list() works for topics too |
| 111 |
|
| 112 |
if( is_array( $quizzes ) ) { |
| 113 |
|
| 114 |
foreach( $quizzes as $quiz_data ) { |
| 115 |
// Mark quiz as completed |
| 116 |
automatorwp_learndash_mark_quiz_as_completed( $user_id, $quiz_data['post']->ID, $course_id ); |
| 117 |
} |
| 118 |
|
| 119 |
} |
| 120 |
|
| 121 |
// Mark topic as completed |
| 122 |
learndash_process_mark_complete( $user_id, $topic_id, false, $course_id ); |
| 123 |
|
| 124 |
} |
| 125 |
|
| 126 |
/** |
| 127 |
* Helper function to mark a lesson as completed |
| 128 |
* |
| 129 |
* @since 1.0.0 |
| 130 |
* |
| 131 |
* @param int $user_id |
| 132 |
* @param int $lesson_id |
| 133 |
* @param int $course_id |
| 134 |
*/ |
| 135 |
function automatorwp_learndash_mark_lesson_as_completed( $user_id = 0, $lesson_id = 0, $course_id = 0 ) { |
| 136 |
|
| 137 |
// Get all lesson topics |
| 138 |
$topics = learndash_get_topic_list( $lesson_id, $course_id ); |
| 139 |
|
| 140 |
if( is_array( $topics ) ) { |
| 141 |
|
| 142 |
foreach( $topics as $topic ) { |
| 143 |
// Mark topic as completed |
| 144 |
automatorwp_learndash_mark_topic_as_completed( $user_id, $topic->ID, $course_id ); |
| 145 |
} |
| 146 |
|
| 147 |
} |
| 148 |
|
| 149 |
// Get all lesson's quizzes |
| 150 |
$quizzes = learndash_get_lesson_quiz_list( $lesson_id, $user_id, $course_id ); |
| 151 |
|
| 152 |
if( is_array( $quizzes ) ) { |
| 153 |
|
| 154 |
foreach( $quizzes as $quiz_data ) { |
| 155 |
// Mark quiz as completed |
| 156 |
automatorwp_learndash_mark_quiz_as_completed( $user_id, $quiz_data['post']->ID, $course_id ); |
| 157 |
} |
| 158 |
|
| 159 |
} |
| 160 |
|
| 161 |
// Mark lesson as completed |
| 162 |
learndash_process_mark_complete( $user_id, $lesson_id, false, $course_id ); |
| 163 |
|
| 164 |
} |
| 165 |
|
| 166 |
/** |
| 167 |
* Helper function to mark a course as completed |
| 168 |
* |
| 169 |
* @since 1.0.0 |
| 170 |
* |
| 171 |
* @param int $user_id |
| 172 |
* @param int $course_id |
| 173 |
*/ |
| 174 |
function automatorwp_learndash_mark_course_as_completed( $user_id = 0, $course_id = 0 ) { |
| 175 |
|
| 176 |
// Get all course lessons |
| 177 |
$lessons = learndash_get_lesson_list( $course_id, array( 'num' => 0 ) ); |
| 178 |
|
| 179 |
if( is_array( $lessons ) ) { |
| 180 |
|
| 181 |
foreach( $lessons as $lesson ) { |
| 182 |
// Mark lesson as completed |
| 183 |
automatorwp_learndash_mark_lesson_as_completed( $user_id, $lesson->ID, $course_id ); |
| 184 |
} |
| 185 |
|
| 186 |
} |
| 187 |
|
| 188 |
// Get all course quizzes |
| 189 |
$quizzes = learndash_get_course_quiz_list( $course_id, $user_id ); |
| 190 |
|
| 191 |
if( is_array( $quizzes ) ) { |
| 192 |
|
| 193 |
foreach( $quizzes as $quiz_data ) { |
| 194 |
// Mark course quizzes as completed |
| 195 |
automatorwp_learndash_mark_quiz_as_completed( $user_id, $quiz_data['post']->ID, $course_id ); |
| 196 |
} |
| 197 |
|
| 198 |
} |
| 199 |
|
| 200 |
// Mark course as completed |
| 201 |
$completed = learndash_process_mark_complete( $user_id, $course_id, false, $course_id ); |
| 202 |
|
| 203 |
} |