LLMS_Processor_Course_Data::dispatch_calc( int $course_id )

Action triggered to queue queries needed to make the calculation


Parameters Parameters

$course_id

(int) (Required) WP Post ID of the course.


Top ↑

Return Return

(void|null)


Top ↑

Source Source

File: includes/processors/class.llms.processor.course.data.php

	public function dispatch_calc( $course_id ) {
		$this->log( sprintf( 'Course data calculation dispatched for course %d.', $course_id ) );

		// Make sure we have a course.
		$course = llms_get_post( $course_id );
		if ( ! $course instanceof LLMS_Course ) {
			return null;
		}

		// Return early if we're already processing data for the given course.
		if ( $this->is_already_processing_course( $course_id ) ) {
			return $this->dispatch_calc_throttled( $course_id );
		}

		// Retrieve args.
		$args = $this->get_student_query_args( $course_id );

		// Get the results for the current set of arguments.
		$query = new LLMS_Student_Query( $args );

		// No students in the course, run task completion.
		if ( ! $query->get_number_results() ) {
			return $this->task_complete( $course, $this->get_task_data(), true );
		}

		// Get the total number of students as a separate query since deprecating SQL_CALC_FOUND_ROWS usage.
		$count_query = new LLMS_Student_Query( $this->get_student_count_query_from_args( $args ) );
		if ( ! $count_query->get_count_only_result() ) {
			return $this->task_complete( $course, $this->get_task_data(), true );
		}
		$course->set( 'enrolled_students', $count_query->get_count_only_result() );

		// Throttle processing.
		if ( $this->maybe_throttle( $count_query->get_count_only_result(), $course_id ) ) {
			return $this->dispatch_calc_throttled( $course_id );
		}

		// Add each page to the queue.
		$max_pages = absint( ceil( $count_query->get_count_only_result() / $args['per_page'] ) );
		// Pass in the max pages as an argument to the task so we don't need to run the count query each time.
		$args['max_pages'] = $max_pages;
		while ( $args['page'] <= $max_pages ) {
			$this->push_to_queue( $args );
			++$args['page'];
		}

		// Save queue and dispatch the process.
		$this->save()->dispatch();
	}


Top ↑

Changelog Changelog

Changelog
Version Description
6.0.0 Don't access LLMS_Student_Query properties directly.
4.21.0 When there's no students found in the course, run the task_complete() method to ensure data from a previous calculation is cleared.
4.12.0 Add throttling by course in progress and adjust last_run calculation to be specific to the course.<br> Improve performance of the student query by removing unneeded sort columns.
3.15.0 Introduced.

Top ↑

User Contributed Notes User Contributed Notes

You must log in before being able to contribute a note or feedback.