/** * Return a {@link qubaid_condition} from the values returned by {@link quiz_statistics_attempts_sql}. * * @param int $quizid * @param array $groupstudents * @param string $whichattempts which attempts to use, represented internally as one of the constants as used in * $quiz->grademethod ie. * QUIZ_GRADEAVERAGE, QUIZ_GRADEHIGHEST, QUIZ_ATTEMPTLAST or QUIZ_ATTEMPTFIRST * we calculate stats based on which attempts would affect the grade for each student. * @param bool $includeungraded * @return \qubaid_join */ function quiz_statistics_qubaids_condition($quizid, $groupstudents, $whichattempts = QUIZ_GRADEAVERAGE, $includeungraded = false) { list($fromqa, $whereqa, $qaparams) = quiz_statistics_attempts_sql($quizid, $groupstudents, $whichattempts, $includeungraded); return new qubaid_join($fromqa, 'quiza.uniqueid', $whereqa, $qaparams); }
/** * Generate the snipped of HTML that says when the stats were last caculated, * with a recalcuate now button. * @param object $quizstats the overall quiz statistics. * @param int $quizid the quiz id. * @param int $currentgroup the id of the currently selected group, or 0. * @param array $groupstudents ids of students in the group. * @param bool $useallattempts whether to use all attempts, instead of just * first attempts. * @return string a HTML snipped saying when the stats were last computed, * or blank if that is not appropriate. */ protected function output_caching_info($quizstats, $quizid, $currentgroup, $groupstudents, $useallattempts, $reporturl) { global $DB, $OUTPUT; if (empty($quizstats->timemodified)) { return ''; } // Find the number of attempts since the cached statistics were computed. list($fromqa, $whereqa, $qaparams) = quiz_statistics_attempts_sql( $quizid, $currentgroup, $groupstudents, $useallattempts, true); $count = $DB->count_records_sql(" SELECT COUNT(1) FROM $fromqa WHERE $whereqa AND quiza.timefinish > {$quizstats->timemodified}", $qaparams); if (!$count) { $count = 0; } // Generate the output. $a = new stdClass(); $a->lastcalculated = format_time(time() - $quizstats->timemodified); $a->count = $count; $recalcualteurl = new moodle_url($reporturl, array('recalculate' => 1, 'sesskey' => sesskey())); $output = ''; $output .= $OUTPUT->box_start( 'boxaligncenter generalbox boxwidthnormal mdl-align', 'cachingnotice'); $output .= get_string('lastcalculated', 'quiz_statistics', $a); $output .= $OUTPUT->single_button($recalcualteurl, get_string('recalculatenow', 'quiz_statistics')); $output .= $OUTPUT->box_end(true); return $output; }
/** * Load the data that will be needed to perform the calculations. * * @param int $quizid the quiz id. * @param int $currentgroup the current group. 0 for none. * @param array $groupstudents students in this group. * @param bool $allattempts use all attempts, or just first attempts. */ public function load_step_data($quizid, $currentgroup, $groupstudents, $allattempts) { global $DB; $this->allattempts = $allattempts; list($qsql, $qparams) = $DB->get_in_or_equal(array_keys($this->questions), SQL_PARAMS_NAMED, 'q'); list($fromqa, $whereqa, $qaparams) = quiz_statistics_attempts_sql($quizid, $currentgroup, $groupstudents, $allattempts, false); $this->lateststeps = $DB->get_records_sql("\n SELECT\n qas.id,\n quiza.sumgrades,\n qa.questionid,\n qa.slot,\n qa.maxmark,\n qas.fraction * qa.maxmark as mark\n\n FROM {$fromqa}\n JOIN {question_attempts} qa ON qa.questionusageid = quiza.uniqueid\n JOIN (\n SELECT questionattemptid, MAX(id) AS latestid\n FROM {question_attempt_steps}\n GROUP BY questionattemptid\n ) lateststepid ON lateststepid.questionattemptid = qa.id\n JOIN {question_attempt_steps} qas ON qas.id = lateststepid.latestid\n\n WHERE\n qa.slot {$qsql} AND\n {$whereqa}", $qparams + $qaparams); }
/** * 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. */ 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 array $groupstudents * @return stdClass with properties with count and avg with prefixes firstattempts, highestattempts, etc. */ protected function attempt_counts_and_averages($quizid, $groupstudents) { global $DB; list($fromqa, $whereqa, $qaparams) = quiz_statistics_attempts_sql($quizid, $groupstudents); $selects = array(); foreach (array_keys(quiz_get_grading_options()) as $which) { $fieldprefix = static::using_attempts_string_id($which); $condition = quiz_report_grade_method_sql($which); if ($condition == '') { $case = '1'; } else { $case = "CASE WHEN ({$condition}) THEN 1 ELSE 0 END"; } $selects[] = "\n SUM({$case}) AS {$fieldprefix}count,\n SUM(sumgrades * {$case}) AS {$fieldprefix}total"; } $select = join(',', $selects); $attempttotals = $DB->get_record_sql("\n SELECT {$select}\n FROM {$fromqa}\n WHERE {$whereqa}", $qaparams); foreach (array_keys(quiz_get_grading_options()) as $which) { $fieldprefix = static::using_attempts_string_id($which); $attempttotals->{$fieldprefix . 'avg'} = $attempttotals->{$fieldprefix . 'total'} / $attempttotals->{$fieldprefix . 'count'}; unset($attempttotals->{$fieldprefix . 'total'}); } return $attempttotals; }