Ajax::dozent_save_review()


Source

File: classes/Ajax.php

	public function dozent_save_review() {
		global $wpdb;

		$course_id    = (int) dozent_input_text( 'course_id' );
		$rating_value = (int) dozent_input_text( 'rating_value' );
		$review       = dozent_input_textarea( 'review' );
		$user_id      = get_current_user_id();

		$hasReview
			= (int) $wpdb->get_var( "SELECT ID FROM {$wpdb->dozent_reviews} WHERE user_id = {$user_id} AND course_id = {$course_id} " );

		if ( $hasReview ) {
			//Update review

			$data = apply_filters( 'dozent/course_review/update_data', [
				'review'     => $review,
				'rating'     => $rating_value,
				'updated_at' => dozent_mysql_time(),
			] );

			do_action( 'dozent/course_review/update/before', $data );

			$wpdb->update( $wpdb->dozent_reviews, $data, [ 'ID' => $hasReview ] );

			do_action( 'dozent/course_review/update/after', $data );

		} else {
			//Create review

			$data = apply_filters( 'dozent/course_review/create_data', [
				'user_id'    => $user_id,
				'course_id'  => $course_id,
				'review_id'  => 0,
				'review'     => $review,
				'rating'     => $rating_value,
				'status'     => 1,
				'created_at' => dozent_mysql_time(),
				'updated_at' => dozent_mysql_time(),
			] );

			do_action( 'dozent/course_review/create/before', $data );

			$wpdb->insert( $wpdb->dozent_reviews, $data );

			do_action( 'dozent/course_review/create/after', $data );

		}

		//Sync Rating with course meta.
		_dozent_get_course_ratings_and_sync( $course_id );

		wp_send_json_success( [ 'msg' => __( 'Review has been saved', 'dozent' ) ] );
	}