/** * SQL to fetch relevant 'quiz_attempts' records. * * @param int $quizid quiz id to get attempts for * @param array $groupstudents empty array if not using groups or array of students in current group. * @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 whether to fetch ungraded attempts too * @return array FROM and WHERE sql fragments and sql params */ function quiz_statistics_attempts_sql($quizid, $groupstudents, $whichattempts = QUIZ_GRADEAVERAGE, $includeungraded = false) { global $DB; $fromqa = '{quiz_attempts} quiza '; $whereqa = 'quiza.quiz = :quizid AND quiza.preview = 0 AND quiza.state = :quizstatefinished'; $qaparams = array('quizid' => (int) $quizid, 'quizstatefinished' => quiz_attempt::FINISHED); if ($groupstudents) { ksort($groupstudents); list($grpsql, $grpparams) = $DB->get_in_or_equal(array_keys($groupstudents), SQL_PARAMS_NAMED, 'statsuser'); list($grpsql, $grpparams) = quiz_statistics_renumber_placeholders($grpsql, $grpparams, 'statsuser'); $whereqa .= " AND quiza.userid {$grpsql}"; $qaparams += $grpparams; } $whichattemptsql = quiz_report_grade_method_sql($whichattempts); if ($whichattemptsql) { $whereqa .= ' AND ' . $whichattemptsql; } if (!$includeungraded) { $whereqa .= ' AND quiza.sumgrades IS NOT NULL'; } return array($fromqa, $whereqa, $qaparams); }
public function test_quiz_statistics_renumber_placeholders_work_to_do() { list($sql, $params) = quiz_statistics_renumber_placeholders('frog1 IN (:frog100 , :frog101)', array('frog100' => 1, 'frog101' => 2), 'frog'); $this->assertEquals('frog1 IN (:frog1 , :frog2)', $sql); $this->assertEquals(array('frog1' => 1, 'frog2' => 2), $params); }