Alert: This function’s access is marked private. This means it is not intended for use by plugin or theme developers, only in other core functions. It is listed here for completeness.

LLMS_Grades::calculate_grade_from_children( array $children, LLMS_Student $student )

Calculates the grades for elements that have a list of children which are averaged / weighted to come up with the total grade


Parameters Parameters

$children

(array) (Required) list of child objects

$student

(LLMS_Student) (Required) A LLMS_Student object.


Top ↑

Return Return

(float|null)


Top ↑

Source Source

File: includes/class-llms-grades.php

52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
private function calculate_grade_from_children( $children, $student ) {
 
    $grade  = null;
    $grades = array();
 
    // Loop through all the children and compile the overall grade & points data.
    foreach ( $children as $child_id ) {
 
        $child = llms_get_post( $child_id );
        $grade = $this->get_grade( $child_id, $student, false );
 
        // Non numeric grade (null) hasn't been taken yet or no gradable elements exist on the child.
        if ( ! is_numeric( $grade ) ) {
            continue;
        }
 
        $points = $child->get( 'points' );
 
        // If no points assigned to the child, the grade doesn't count towards the overall grade.
        if ( ! $points ) {
            continue;
        }
 
        // Add the grade & points for further processing after we have all the data.
        $grades[] = array(
            'grade'  => $grade,
            'points' => $points,
        );
 
    }
 
    // If we have at least one grade.
    if ( count( $grades ) ) {
 
        // Get the total available points for all children with a numeric grade & a points value.
        $total_points = array_sum( wp_list_pluck( $grades, 'points' ) );
 
        // If we don't have any points this element can't have an overall grade.
        if ( $total_points ) {
 
            // Sum up the adjusted grade.
            $grade = 0;
            foreach ( $grades as $data ) {
                // Calculate the adjusted the grade.
                // Grade multiplied by available points over total points.
                $grade += $data['grade'] * ( $data['points'] / $total_points );
            }
        }
    }
 
    return $grade;
 
}


Top ↑

Changelog Changelog

Changelog
Version Description
3.24.0 Introduced.

Top ↑

User Contributed Notes User Contributed Notes

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