LLMS_Student::get_overall_grade( boolean $use_cache = true )

Retrieve the student’s overall grade


Description Description

Grade = sum of grades for all courses divided by number of enrolled courses if a course has no quizzes in it, it cannot be graded and is therefore excluded from the calculation.

Cached data is automatically cleared when a student completes a quiz.


Top ↑

Parameters Parameters

$use_cache

(boolean) (Optional) If false, calculates the grade, otherwise utilizes cached data (if available)

Default value: true


Top ↑

Return Return

(float|string) Grade as float or "N/A"


Top ↑

Source Source

File: includes/models/model.llms.student.php

	public function get_overall_grade( $use_cache = true ) {

		$grade = null;

		// Attempt to pull from the cache first.
		if ( $use_cache ) {

			$grade = $this->get( $this->meta_prefix . 'overall_grade' );

			if ( is_numeric( $grade ) ) {
				$grade = floatval( $grade );
			}
		}

		// Cache disabled or no cached data available.
		if ( ! $use_cache || null === $grade || '' === $grade ) {

			$grades = array();

			// Get courses.
			$courses = $this->get_courses(
				array(
					'limit' => 9999,
				)
			);

			// Loop through courses.
			foreach ( $courses['results'] as $course_id ) {

				// Get course grade.
				$g = $this->get_grade( $course_id );

				// If an actual grade (not N/A) is returned.
				if ( is_numeric( $g ) ) {
					array_push( $grades, $g );
				}
			}

			// If we have at least one grade.
			$count = count( $grades );
			if ( $count ) {

				$grade = round( array_sum( $grades ) / $count, 2 );

			} else {

				$grade = _x( 'N/A', 'overall grade when no quizzes', 'lifterlms' );

			}

			// Cache the grade.
			$this->set( 'overall_grade', $grade );

		}

		return apply_filters( 'llms_student_get_overall_grade', $grade, $this );

	}


Top ↑

Changelog Changelog

Changelog
Version Description
3.2.0 Introduced.

Top ↑

User Contributed Notes User Contributed Notes

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