protected function attempt_counts_and_averages($quizid, \core\dml\sql_join $groupstudentsjoins) {
        global $DB;

        $attempttotals = new \stdClass();
        foreach (array_keys(quiz_get_grading_options()) as $which) {

            list($fromqa, $whereqa, $qaparams) = quiz_statistics_attempts_sql($quizid, $groupstudentsjoins, $which);

            $fromdb = $DB->get_record_sql("SELECT COUNT(*) AS rcount, AVG(sumgrades) AS average FROM $fromqa WHERE $whereqa",
                                            $qaparams);
            $fieldprefix = static::using_attempts_string_id($which);
            $attempttotals->{$fieldprefix.'avg'} = $fromdb->average;
            $attempttotals->{$fieldprefix.'count'} = $fromdb->rcount;
        }
        return $attempttotals;
    }
protected function attempt_counts_and_averages($quizid, \core\dml\sql_join $groupstudentsjoins) {
        global $DB;

        $attempttotals = new \stdClass();
        foreach (array_keys(quiz_get_grading_options()) as $which) {

            list($fromqa, $whereqa, $qaparams) = quiz_statistics_attempts_sql($quizid, $groupstudentsjoins, $which);

            $fromdb = $DB->get_record_sql("SELECT COUNT(*) AS rcount, AVG(sumgrades) AS average FROM $fromqa WHERE $whereqa",
                                            $qaparams);
            $fieldprefix = static::using_attempts_string_id($which);
            $attempttotals->{$fieldprefix.'avg'} = $fromdb->average;
            $attempttotals->{$fieldprefix.'count'} = $fromdb->rcount;
        }
        return $attempttotals;
    }
protected function attempt_counts_and_averages ( $ quizid , \ core \ dml \ sql_join $ groupstudentsjoins ) { global $ DB ; $ attempttotals = new \ stdClass ( ) ; foreach ( array_keys ( quiz_get_grading_options ( ) ) as $ which ) { list ( $ fromqa , $ whereqa , $ qaparams ) = quiz_statistics_attempts_sql ( $ quizid , $ groupstudentsjoins , $ which ) ; $ fromdb = $ DB -> get_record_sql ( "SELECT COUNT(*) AS rcount, AVG(sumgrades) AS average FROM $fromqa WHERE $whereqa" , $ qaparams ) ; $ fieldprefix = static :: using_attempts_string_id ( $ which ) ; $ attempttotals -> { $ fieldprefix . 'avg' } = $ fromdb -> average ; $ attempttotals -> { $ fieldprefix . 'count' } = $ fromdb -> rcount ; } return $ attempttotals ; }
Calculating count and mean of marks for first and ALL attempts by students.

See : http://docs.moodle.org/dev/Quiz_item_analysis_calculations_in_practise
#Calculating_MEAN_of_grades_for_all_attempts_by_students
@param int $quizid
@param \core\dml\sql_join $groupstudentsjoins Contains joins, wheres, params for students in this group.
@return \stdClass with properties with count and avg with prefixes firstattempts, highestattempts, etc.
Calculating count and mean of marks for first and ALL attempts by students .