complete-course.php
3 months ago
complete-lesson.php
10 months ago
reset-user-progress.php
10 months ago
complete-course.php
236 lines
| 1 | <?php |
| 2 | /** |
| 3 | * CompleteCourse. |
| 4 | * php version 5.6 |
| 5 | * |
| 6 | * @category CompleteCourse |
| 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\MemberPressCourse\Actions; |
| 15 | |
| 16 | use Exception; |
| 17 | use memberpress\courses\lib as lib; |
| 18 | use SureTriggers\Integrations\MemberPressCourse\MemberPressCourse; |
| 19 | use memberpress\courses as base; |
| 20 | use memberpress\courses\models as models; |
| 21 | use SureTriggers\Integrations\AutomateAction; |
| 22 | use SureTriggers\Traits\SingletonLoader; |
| 23 | |
| 24 | /** |
| 25 | * CompleteCourse |
| 26 | * |
| 27 | * @category CompleteCourse |
| 28 | * @package SureTriggers |
| 29 | * @author BSF <username@example.com> |
| 30 | * @license https://www.gnu.org/licenses/gpl-3.0.html GPLv3 |
| 31 | * @link https://www.brainstormforce.com/ |
| 32 | * @since 1.0.0 |
| 33 | */ |
| 34 | class CompleteCourse extends AutomateAction { |
| 35 | |
| 36 | |
| 37 | /** |
| 38 | * Integration type. |
| 39 | * |
| 40 | * @var string |
| 41 | */ |
| 42 | public $integration = 'MemberPressCourse'; |
| 43 | |
| 44 | /** |
| 45 | * Action name. |
| 46 | * |
| 47 | * @var string |
| 48 | */ |
| 49 | public $action = 'mpc_complete_course'; |
| 50 | |
| 51 | use SingletonLoader; |
| 52 | |
| 53 | /** |
| 54 | * Register a action. |
| 55 | * |
| 56 | * @param array $actions actions. |
| 57 | * @return array |
| 58 | */ |
| 59 | public function register( $actions ) { |
| 60 | $actions[ $this->integration ][ $this->action ] = [ |
| 61 | 'label' => __( 'Complete Course', 'suretriggers' ), |
| 62 | 'action' => $this->action, |
| 63 | 'function' => [ $this, 'action_listener' ], |
| 64 | ]; |
| 65 | return $actions; |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * Action listener. |
| 70 | * |
| 71 | * @param int $user_id user_id. |
| 72 | * @param int $automation_id automation_id. |
| 73 | * @param array $fields fields. |
| 74 | * @param array $selected_options selectedOptions. |
| 75 | * @throws Exception Throws exception. |
| 76 | * |
| 77 | * @return array |
| 78 | */ |
| 79 | public function _action_listener( $user_id, $automation_id, $fields, $selected_options ) { |
| 80 | if ( ! $user_id ) { |
| 81 | return [ |
| 82 | 'status' => 'error', |
| 83 | 'message' => 'User not found with this email address.', |
| 84 | ]; |
| 85 | } |
| 86 | |
| 87 | $sections = []; |
| 88 | $lessons = []; |
| 89 | $course_id = $selected_options['mpcs-course']; |
| 90 | $sections = $this->find_all_by_course( $course_id ); |
| 91 | |
| 92 | if ( is_array( $sections ) && count( $sections ) > 0 ) { |
| 93 | foreach ( $sections as $section ) { |
| 94 | $lessons = MemberpressCourse::find_all_by_section( $section ); |
| 95 | if ( is_array( $lessons ) && count( $lessons ) > 0 ) { |
| 96 | foreach ( $lessons as $lesson ) { |
| 97 | $this->mark_lesson_completed( $user_id, $course_id, $lesson, $section ); |
| 98 | } |
| 99 | } |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | return [ |
| 104 | 'user_email' => $selected_options['wp_user_email'], |
| 105 | 'course_id' => $course_id, |
| 106 | 'course_title' => get_the_title( $course_id ), |
| 107 | 'course_url' => get_permalink( $course_id ), |
| 108 | 'course_featured_image_id' => get_post_meta( $course_id, '_thumbnail_id', true ), |
| 109 | 'course_featured_image_url' => get_the_post_thumbnail_url( $course_id ), |
| 110 | ]; |
| 111 | } |
| 112 | |
| 113 | /** |
| 114 | * Mark lesson completed. |
| 115 | * |
| 116 | * @param int $user_id user id. |
| 117 | * @param int $course_id course id. |
| 118 | * @param int $lesson_id lesson id. |
| 119 | * @param int $section section. |
| 120 | * @return array|void |
| 121 | */ |
| 122 | public function mark_lesson_completed( $user_id, $course_id, $lesson_id, $section ) { |
| 123 | if ( ! class_exists( '\memberpress\courses\models\UserProgress' ) ) { |
| 124 | return [ |
| 125 | 'status' => 'error', |
| 126 | 'message' => __( '\memberpress\courses\models\UserProgress class not found.', 'suretriggers' ), |
| 127 | |
| 128 | ]; |
| 129 | } |
| 130 | if ( empty( $section ) && empty( $course_id ) ) { |
| 131 | return; |
| 132 | } |
| 133 | |
| 134 | if ( models\UserProgress::has_completed_course( $user_id, $course_id ) ) { |
| 135 | return; |
| 136 | } |
| 137 | |
| 138 | $has_started_course = models\UserProgress::has_started_course( $user_id, $course_id ); |
| 139 | $has_started_section = models\UserProgress::has_started_section( $user_id, $section ); |
| 140 | |
| 141 | $user_progress = new models\UserProgress(); |
| 142 | $user_progress->lesson_id = $lesson_id; |
| 143 | $user_progress->course_id = $course_id; |
| 144 | $user_progress->user_id = $user_id; |
| 145 | if ( class_exists( '\memberpress\courses\lib\Utils' ) ) { |
| 146 | $user_progress->created_at = lib\Utils::ts_to_mysql_date( time() ); |
| 147 | $user_progress->completed_at = lib\Utils::ts_to_mysql_date( time() ); |
| 148 | } |
| 149 | |
| 150 | $user_progress->store(); |
| 151 | |
| 152 | do_action( 'mpcs_completed_lesson', $user_progress ); |
| 153 | |
| 154 | if ( models\UserProgress::has_completed_course( $user_id, $course_id ) ) { |
| 155 | do_action( 'mpcs_completed_course', $user_progress ); |
| 156 | } |
| 157 | |
| 158 | if ( models\UserProgress::has_completed_section( $user_id, $section ) ) { |
| 159 | do_action( 'mpcs_completed_section', $user_progress ); |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | /** |
| 164 | * Find all sections |
| 165 | * |
| 166 | * @param int $course_id course id. |
| 167 | * @return array |
| 168 | */ |
| 169 | public function find_all_by_course( $course_id ) { |
| 170 | global $wpdb; |
| 171 | $result = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}mpcs_sections WHERE course_id =%s", $course_id ) ); |
| 172 | |
| 173 | $sections = []; |
| 174 | foreach ( $result as $rec ) { |
| 175 | $sections[] = $rec->id; |
| 176 | } |
| 177 | |
| 178 | return $sections; |
| 179 | } |
| 180 | |
| 181 | /** |
| 182 | * Find lessons. |
| 183 | * |
| 184 | * @param int $section_id section id. |
| 185 | * @return array|void |
| 186 | */ |
| 187 | public function find_all_by_section( $section_id ) { |
| 188 | if ( ! class_exists( '\memberpress\courses\models\Lesson' ) ) { |
| 189 | return [ |
| 190 | 'status' => 'error', |
| 191 | 'message' => __( '\memberpress\courses\models\Lesson class not found.', 'suretriggers' ), |
| 192 | |
| 193 | ]; |
| 194 | } |
| 195 | global $wpdb; |
| 196 | $post_types = models\Lesson::lesson_cpts(); |
| 197 | |
| 198 | $prepare_args = [ |
| 199 | models\Lesson::$section_id_str, |
| 200 | $section_id, |
| 201 | models\Lesson::$lesson_order_str, |
| 202 | ]; |
| 203 | $prepare_args = array_merge( $prepare_args, $post_types ); |
| 204 | |
| 205 | $query = $wpdb->prepare( |
| 206 | "SELECT ID, post_type FROM {$wpdb->posts} AS p |
| 207 | JOIN {$wpdb->postmeta} AS pm |
| 208 | ON p.ID = pm.post_id |
| 209 | AND pm.meta_key = %s |
| 210 | AND pm.meta_value = %s |
| 211 | JOIN {$wpdb->postmeta} AS pm_order |
| 212 | ON p.ID = pm_order.post_id |
| 213 | AND pm_order.meta_key = %s |
| 214 | WHERE p.post_type IN ( " . implode( ',', array_fill( 0, count( $post_types ), '%s' ) ) . " ) AND p.post_status <> 'trash' |
| 215 | ORDER BY pm_order.meta_value * 1", |
| 216 | $prepare_args |
| 217 | ); |
| 218 | |
| 219 | $db_lessons = $wpdb->get_results( $query ); //phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared |
| 220 | $lessons = []; |
| 221 | |
| 222 | foreach ( $db_lessons as $lesson ) { |
| 223 | if ( class_exists( '\memberpress\courses\models\Quiz' ) && models\Quiz::$cpt === $lesson->post_type ) { |
| 224 | $lessons[] = $lesson->ID; |
| 225 | } else { |
| 226 | $lessons[] = $lesson->ID; |
| 227 | } |
| 228 | } |
| 229 | |
| 230 | return $lessons; |
| 231 | } |
| 232 | |
| 233 | } |
| 234 | |
| 235 | CompleteCourse::get_instance(); |
| 236 |