LP_MARKSECTIONDONE::lp_mark_section_done( string $user_id, array $action_data, string $recipe_id,  $args )

Validation function when the action is hit.


Parameters Parameters

$user_id

(string) (Required) user id.

$action_data

(array) (Required) action data.

$recipe_id

(string) (Required) recipe id.


Source Source

File: src/integrations/learnpress/actions/lp-marksectiondone.php

	public function lp_mark_section_done( $user_id, $action_data, $recipe_id ) {



		if ( ! function_exists( 'learn_press_get_current_user' ) ) {
			$error_message = 'The function learn_press_get_current_user does not exist';
			Automator()->complete_action( $user_id, $action_data, $recipe_id, $error_message );

			return;
		}
		$user = learn_press_get_user( $user_id );

		$course_id  = $action_data['meta']['LPCOURSE'];
		$section_id = $action_data['meta'][ $this->action_meta ];
		// Get All lessons from section.
		$course_curd = new LP_Section_CURD( $course_id );
		$lessons     = $course_curd->get_section_items( $section_id );
		// Mark lesson completed.
		foreach ( $lessons as $lesson ) {
			if ( $lesson['type'] === 'lp_lesson' ) {
				$result = $user->complete_lesson( $lesson['id'], $course_id );
			} elseif ( $lesson['type'] === 'lp_quiz' ) {
				$quiz_id = $lesson['id'];
				$user    = LP_Global::user();

				if ( ! $user->has_item_status( [ 'started', 'completed' ], $quiz_id, $course_id ) ) {
					$quiz_data = $user->start_quiz( $quiz_id, $course_id, false );
					$item      = new LP_User_Item_Course( $quiz_data );
					$item->finish();
				} else {
					$quiz_data = $user->get_item_data( $quiz_id, $course_id );
					$quiz_data->finish();
				}
			}
		}

		if ( ! is_wp_error( $result ) ) {
			Automator()->complete_action( $user_id, $action_data, $recipe_id );
		} else {
			$error_message = $result->get_error_message();
			Automator()->complete_action( $user_id, $action_data, $recipe_id, $error_message );

			return;
		}

	}