add-user-group.php
11 months ago
enroll-to-course.php
11 months ago
find-course-taxonomy.php
11 months ago
find-user-group-leader-groups.php
5 months ago
find-user-groups.php
11 months ago
list-user-enrolled-courses.php
11 months ago
make-user-group-leader.php
2 years ago
mark-course-complete-for-user.php
11 months ago
mark-lesson-complete-for-user.php
11 months ago
mark-quiz-complete-for-user.php
11 months ago
mark-topic-complete-for-user.php
11 months ago
remove-from-course.php
11 months ago
remove-user-from-group-leader.php
11 months ago
remove-user-group.php
11 months ago
reset-user-course-progress.php
11 months ago
mark-course-complete-for-user.php
286 lines
| 1 | <?php |
| 2 | /** |
| 3 | * MarkCourseCompleteForUser. |
| 4 | * php version 5.6 |
| 5 | * |
| 6 | * @category MarkCourseCompleteForUser |
| 7 | * @package SureTriggers |
| 8 | * @author BSF <username@example.com> |
| 9 | * @license https://www.gnu.org/licenses/gpl-3.0.html GPLv3 |
| 10 | * @link https://www.brainstormforce.com/ |
| 11 | * @since 1.0.0 |
| 12 | */ |
| 13 | |
| 14 | namespace SureTriggers\Integrations\LearnDash\Actions; |
| 15 | |
| 16 | use SureTriggers\Integrations\AutomateAction; |
| 17 | use SureTriggers\Integrations\LearnDash\LearnDash; |
| 18 | use SureTriggers\Traits\SingletonLoader; |
| 19 | use Exception; |
| 20 | use WP_Query; |
| 21 | |
| 22 | /** |
| 23 | * MarkCourseCompleteForUser |
| 24 | * |
| 25 | * @category MarkCourseCompleteForUser |
| 26 | * @package SureTriggers |
| 27 | * @author BSF <username@example.com> |
| 28 | * @license https://www.gnu.org/licenses/gpl-3.0.html GPLv3 |
| 29 | * @link https://www.brainstormforce.com/ |
| 30 | * @since 1.0.0 |
| 31 | */ |
| 32 | class MarkCourseCompleteForUser extends AutomateAction { |
| 33 | |
| 34 | /** |
| 35 | * Integration type. |
| 36 | * |
| 37 | * @var string |
| 38 | */ |
| 39 | public $integration = 'LearnDash'; |
| 40 | /** |
| 41 | * The quiz_list. |
| 42 | * |
| 43 | * @var array |
| 44 | */ |
| 45 | private $quiz_list; |
| 46 | /** |
| 47 | * Action name. |
| 48 | * |
| 49 | * @var string |
| 50 | */ |
| 51 | public $action = 'ld_mark_course_complete_for_user'; |
| 52 | |
| 53 | use SingletonLoader; |
| 54 | |
| 55 | /** |
| 56 | * Register a action. |
| 57 | * |
| 58 | * @param array $actions actions. |
| 59 | * @return array |
| 60 | */ |
| 61 | public function register( $actions ) { |
| 62 | $actions[ $this->integration ][ $this->action ] = [ |
| 63 | 'label' => __( 'Mark Course Complete For User', 'suretriggers' ), |
| 64 | 'action' => $this->action, |
| 65 | 'function' => [ $this, 'action_listener' ], |
| 66 | ]; |
| 67 | return $actions; |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * Action listener. |
| 72 | * |
| 73 | * @param int $user_id user_id. |
| 74 | * @param int $automation_id automation_id. |
| 75 | * @param array $fields fields. |
| 76 | * @param array $selected_options selectedOptions. |
| 77 | * @psalm-suppress UndefinedMethod |
| 78 | * @throws Exception Error. |
| 79 | * @return bool|array |
| 80 | */ |
| 81 | public function _action_listener( $user_id, $automation_id, $fields, $selected_options ) { |
| 82 | if ( ! function_exists( 'learndash_process_mark_complete' ) ) { |
| 83 | return false; |
| 84 | } |
| 85 | if ( ! $user_id ) { |
| 86 | return [ |
| 87 | 'status' => 'error', |
| 88 | 'message' => 'User email not exists.', |
| 89 | ]; |
| 90 | } |
| 91 | |
| 92 | $course_id = ( isset( $selected_options['sfwd-courses'] ) ) ? $selected_options['sfwd-courses'] : '0'; |
| 93 | |
| 94 | if ( 'all' === $course_id ) { |
| 95 | |
| 96 | // Get all courses. |
| 97 | $query = new WP_Query( |
| 98 | [ |
| 99 | 'post_type' => 'sfwd-courses', |
| 100 | 'post_status' => 'publish', |
| 101 | 'fields' => 'ids', |
| 102 | 'nopaging' => true, //phpcs:ignore |
| 103 | ] |
| 104 | ); |
| 105 | |
| 106 | $courses = $query->get_posts(); |
| 107 | } else { |
| 108 | |
| 109 | $course = get_post( (int) $course_id ); |
| 110 | if ( ! $course ) { |
| 111 | return [ |
| 112 | 'status' => 'error', |
| 113 | 'message' => 'No course is available.', |
| 114 | ]; |
| 115 | } |
| 116 | |
| 117 | $courses = [ $course_id ]; |
| 118 | } |
| 119 | |
| 120 | $added_to_courses = []; |
| 121 | |
| 122 | // Enroll user in courses. |
| 123 | $count = 1; |
| 124 | foreach ( $courses as $course_id ) { |
| 125 | self::mark_steps_done( $user_id, $course_id ); |
| 126 | learndash_process_mark_complete( $user_id, $course_id ); |
| 127 | $arr_key = count( $courses ) > 1 ? 'course_' . $count : 'course'; |
| 128 | $added_to_courses[ $arr_key ] = LearnDash::get_course_pluggable_data( $course_id ); |
| 129 | $count++; |
| 130 | } |
| 131 | |
| 132 | $user_data = LearnDash::get_user_pluggable_data( $user_id ); |
| 133 | |
| 134 | return [ |
| 135 | 'user' => $user_data, |
| 136 | 'courses' => $added_to_courses, |
| 137 | ]; |
| 138 | } |
| 139 | |
| 140 | /** |
| 141 | * Mark steps done |
| 142 | * |
| 143 | * @param int $user_id user id. |
| 144 | * @param int $course_id course id. |
| 145 | * @return array|void |
| 146 | */ |
| 147 | public function mark_steps_done( $user_id, $course_id ) { |
| 148 | if ( ! function_exists( 'learndash_get_lesson_list' ) || ! function_exists( 'learndash_process_mark_complete' ) || ! function_exists( 'learndash_get_lesson_quiz_list' ) ) { |
| 149 | return [ |
| 150 | 'status' => 'error', |
| 151 | 'message' => __( 'Required functions not found.', 'suretriggers' ), |
| 152 | |
| 153 | ]; |
| 154 | } |
| 155 | $lessons = learndash_get_lesson_list( $course_id, [ 'num' => 0 ] ); |
| 156 | foreach ( $lessons as $lesson ) { |
| 157 | self::mark_topics_done( $user_id, $lesson->ID, $course_id ); |
| 158 | $lesson_quiz_list = learndash_get_lesson_quiz_list( $lesson->ID, $user_id, $course_id ); |
| 159 | |
| 160 | if ( $lesson_quiz_list ) { |
| 161 | foreach ( $lesson_quiz_list as $ql ) { |
| 162 | $this->quiz_list[ $ql['post']->ID ] = 0; |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | learndash_process_mark_complete( $user_id, $lesson->ID, false, $course_id ); |
| 167 | } |
| 168 | |
| 169 | self::mark_quiz_complete( $user_id, $course_id ); |
| 170 | } |
| 171 | |
| 172 | /** |
| 173 | * Marks topics done |
| 174 | * |
| 175 | * @param int $user_id User Id. |
| 176 | * @param int $lesson_id Lesson ID. |
| 177 | * @param int $course_id Course ID. |
| 178 | * @return array|void |
| 179 | */ |
| 180 | public function mark_topics_done( $user_id, $lesson_id, $course_id ) { |
| 181 | if ( ! function_exists( 'learndash_get_topic_list' ) || ! function_exists( 'learndash_process_mark_complete' ) || ! function_exists( 'learndash_get_lesson_quiz_list' ) ) { |
| 182 | return [ |
| 183 | 'status' => 'error', |
| 184 | 'message' => __( 'Required functions not found.', 'suretriggers' ), |
| 185 | |
| 186 | ]; |
| 187 | } |
| 188 | $topic_list = learndash_get_topic_list( $lesson_id, $course_id ); |
| 189 | if ( $topic_list ) { |
| 190 | foreach ( $topic_list as $topic ) { |
| 191 | learndash_process_mark_complete( $user_id, $topic->ID, false, $course_id ); |
| 192 | $topic_quiz_list = learndash_get_lesson_quiz_list( $topic->ID, $user_id, $course_id ); |
| 193 | if ( $topic_quiz_list ) { |
| 194 | foreach ( $topic_quiz_list as $ql ) { |
| 195 | $this->quiz_list[ $ql['post']->ID ] = 0; |
| 196 | } |
| 197 | } |
| 198 | } |
| 199 | } |
| 200 | } |
| 201 | |
| 202 | |
| 203 | /** |
| 204 | * Marks quiz complete |
| 205 | * |
| 206 | * @param int $user_id User ID. |
| 207 | * @param int $course_id Course ID. |
| 208 | * @return array|void |
| 209 | */ |
| 210 | public function mark_quiz_complete( $user_id, $course_id = null ) { |
| 211 | if ( ! function_exists( 'learndash_get_course_quiz_list' ) || ! function_exists( 'learndash_is_quiz_complete' ) || ! function_exists( 'learndash_update_user_activity' ) ) { |
| 212 | return [ |
| 213 | 'status' => 'error', |
| 214 | 'message' => __( 'Required functions not found.', 'suretriggers' ), |
| 215 | |
| 216 | ]; |
| 217 | } |
| 218 | $quizzes = learndash_get_course_quiz_list( $course_id, $user_id ); |
| 219 | if ( $quizzes ) { |
| 220 | |
| 221 | foreach ( $quizzes as $quiz ) { |
| 222 | $this->quiz_list[ $quiz['post']->ID ] = 0; |
| 223 | } |
| 224 | } |
| 225 | $quizz_progress = []; |
| 226 | if ( ! empty( $this->quiz_list ) ) { |
| 227 | |
| 228 | $usermeta = get_user_meta( $user_id, '_sfwd-quizzes', true ); |
| 229 | $quizz_progress = (array) ( empty( $usermeta ) ? [] : $usermeta ); |
| 230 | |
| 231 | foreach ( $this->quiz_list as $quiz_id => $quiz ) { |
| 232 | $quiz_meta = (array) get_post_meta( $quiz_id, '_sfwd-quiz', true ); |
| 233 | |
| 234 | if ( learndash_is_quiz_complete( $user_id, $quiz_id, $course_id ) ) { |
| 235 | continue; |
| 236 | } |
| 237 | |
| 238 | $quizdata = [ |
| 239 | 'quiz' => $quiz_id, |
| 240 | 'score' => 0, |
| 241 | 'count' => 0, |
| 242 | 'pass' => true, |
| 243 | 'rank' => '-', |
| 244 | 'time' => time(), |
| 245 | 'pro_quizid' => $quiz_meta['sfwd-quiz_quiz_pro'], |
| 246 | 'course' => $course_id, |
| 247 | 'points' => 0, |
| 248 | 'total_points' => 0, |
| 249 | 'percentage' => 100, |
| 250 | 'timespent' => 0, |
| 251 | 'has_graded' => false, |
| 252 | 'statistic_ref_id' => 0, |
| 253 | 'm_edit_by' => 9999999, // Manual Edit By ID. |
| 254 | 'm_edit_time' => time(), |
| 255 | // Manual Edit timestamp. |
| 256 | ]; |
| 257 | |
| 258 | $quizz_progress[] = $quizdata; |
| 259 | |
| 260 | // Then we add the quiz entry to the activity database. |
| 261 | learndash_update_user_activity( |
| 262 | [ |
| 263 | 'course_id' => $course_id, |
| 264 | 'user_id' => $user_id, |
| 265 | 'post_id' => $quiz_id, |
| 266 | 'activity_type' => 'quiz', |
| 267 | 'activity_action' => 'insert', |
| 268 | 'activity_status' => true, |
| 269 | 'activity_started' => $quizdata['time'], |
| 270 | 'activity_completed' => $quizdata['time'], |
| 271 | 'activity_meta' => $quizdata, |
| 272 | ] |
| 273 | ); |
| 274 | |
| 275 | } |
| 276 | } |
| 277 | |
| 278 | if ( ! empty( $quizz_progress ) ) { |
| 279 | update_user_meta( $user_id, '_sfwd-quizzes', $quizz_progress ); |
| 280 | } |
| 281 | } |
| 282 | |
| 283 | } |
| 284 | |
| 285 | MarkCourseCompleteForUser::get_instance(); |
| 286 |