mirror of
https://github.com/moodle/moodle.git
synced 2025-04-14 04:52:36 +02:00
MDL-75576 quiz statistics: don't compute when a user views the qbank
Previously, when users viewed the question bank, if the quiz statistics had not already been calculated, we would try to compute them there an then. This could be very, very slow, leading to session lock problems. Now, we never try to compute the statistics on the fly. Instead, we rely on the existing \quiz_statistics\task\recalculate scheduled task to do it.
This commit is contained in:
parent
db9a0e296a
commit
86932a330b
@ -2483,12 +2483,12 @@ function quiz_delete_references($quizid): void {
|
||||
* This enables quiz statistics to be shown in statistics columns in the database.
|
||||
*
|
||||
* @param context $context return the statistics related to this context (which will be a quiz context).
|
||||
* @return all_calculated_for_qubaid_condition|null The statistics for this quiz, if any, else null.
|
||||
* @return all_calculated_for_qubaid_condition|null The statistics for this quiz, if available, else null.
|
||||
*/
|
||||
function mod_quiz_calculate_question_stats(context $context): ?all_calculated_for_qubaid_condition {
|
||||
global $CFG;
|
||||
require_once($CFG->dirroot . '/mod/quiz/report/statistics/report.php');
|
||||
$cm = get_coursemodule_from_id('quiz', $context->instanceid);
|
||||
$report = new quiz_statistics_report();
|
||||
return $report->calculate_questions_stats_for_question_bank($cm->instance);
|
||||
return $report->calculate_questions_stats_for_question_bank($cm->instance, false);
|
||||
}
|
||||
|
@ -627,11 +627,15 @@ class quiz_statistics_report extends quiz_default_report {
|
||||
* @param \core\dml\sql_join $groupstudentsjoins Contains joins, wheres, params for students in this group.
|
||||
* @param array $questions full question data.
|
||||
* @param \core\progress\base|null $progress
|
||||
* @param bool $calculateifrequired if true (the default) the stats will be calculated if not already stored.
|
||||
* If false, [null, null] will be returned if the stats are not already available.
|
||||
* @return array with 2 elements: - $quizstats The statistics for overall attempt scores.
|
||||
* - $questionstats \core_question\statistics\questions\all_calculated_for_qubaid_condition
|
||||
* Both may be null, if $calculateifrequired is false.
|
||||
*/
|
||||
public function get_all_stats_and_analysis(
|
||||
$quiz, $whichattempts, $whichtries, \core\dml\sql_join $groupstudentsjoins, $questions, $progress = null) {
|
||||
$quiz, $whichattempts, $whichtries, \core\dml\sql_join $groupstudentsjoins,
|
||||
$questions, $progress = null, bool $calculateifrequired = true) {
|
||||
|
||||
if ($progress === null) {
|
||||
$progress = new \core\progress\none();
|
||||
@ -645,6 +649,11 @@ class quiz_statistics_report extends quiz_default_report {
|
||||
|
||||
$progress->start_progress('', 3);
|
||||
if ($quizcalc->get_last_calculated_time($qubaids) === false) {
|
||||
if (!$calculateifrequired) {
|
||||
$progress->progress(3);
|
||||
$progress->end_progress();
|
||||
return [null, null];
|
||||
}
|
||||
|
||||
// Recalculate now.
|
||||
$questionstats = $qcalc->calculate($qubaids);
|
||||
@ -927,15 +936,21 @@ class quiz_statistics_report extends quiz_default_report {
|
||||
* Load question stats for a quiz
|
||||
*
|
||||
* @param int $quizid question usage
|
||||
* @return all_calculated_for_qubaid_condition question stats
|
||||
* @param bool $calculateifrequired if true (the default) the stats will be calculated if not already stored.
|
||||
* If false, null will be returned if the stats are not already available.
|
||||
* @return ?all_calculated_for_qubaid_condition question stats
|
||||
*/
|
||||
public function calculate_questions_stats_for_question_bank(int $quizid): all_calculated_for_qubaid_condition {
|
||||
public function calculate_questions_stats_for_question_bank(
|
||||
int $quizid,
|
||||
bool $calculateifrequired = true
|
||||
): ?all_calculated_for_qubaid_condition {
|
||||
global $DB;
|
||||
$quiz = $DB->get_record('quiz', ['id' => $quizid], '*', MUST_EXIST);
|
||||
$questions = $this->load_and_initialise_questions_for_calculations($quiz);
|
||||
|
||||
[, $questionstats] = $this->get_all_stats_and_analysis($quiz,
|
||||
$quiz->grademethod, question_attempt::ALL_TRIES, new \core\dml\sql_join(), $questions);
|
||||
$quiz->grademethod, question_attempt::ALL_TRIES, new \core\dml\sql_join(),
|
||||
$questions, null, $calculateifrequired);
|
||||
|
||||
return $questionstats;
|
||||
}
|
||||
|
@ -1,6 +1,13 @@
|
||||
This files describes API changes in /mod/quiz/report/statistics/*,
|
||||
information provided here is intended especially for developers.
|
||||
|
||||
=== 4.1.4 ===
|
||||
|
||||
* The methods quiz_statistics_report::calculate_questions_stats_for_question_bank and get_all_stats_and_analysis
|
||||
(which are really private to the quiz, and not part of any API you should be using) now have a new
|
||||
optional argument $calculateifrequired.
|
||||
|
||||
|
||||
=== 3.2 ===
|
||||
|
||||
* The function quiz_statistics_graph_get_new_colour() is deprecated in favour of the
|
||||
|
@ -72,6 +72,7 @@ Feature: Show statistics in question bank
|
||||
| slot | response |
|
||||
| 1 | True |
|
||||
| 2 | True |
|
||||
And I run the scheduled task "\quiz_statistics\task\recalculate"
|
||||
When I am on the "Course 1" "core_question > course question bank" page logged in as "admin"
|
||||
Then I should see "50.00%" in the "TF1" "table_row"
|
||||
And I should see "75.00%" in the "TF2" "table_row"
|
||||
@ -87,6 +88,7 @@ Feature: Show statistics in question bank
|
||||
| slot | response |
|
||||
| 1 | True |
|
||||
| 2 | True |
|
||||
And I run the scheduled task "\quiz_statistics\task\recalculate"
|
||||
When I am on the "Course 1" "core_question > course question bank" page logged in as "admin"
|
||||
Then I should see "50.00%" in the "TF1" "table_row"
|
||||
And I should see "75.00%" in the "TF2" "table_row"
|
||||
@ -102,6 +104,7 @@ Feature: Show statistics in question bank
|
||||
| slot | response |
|
||||
| 1 | True |
|
||||
| 2 | True |
|
||||
And I run the scheduled task "\quiz_statistics\task\recalculate"
|
||||
When I am on the "Course 1" "core_question > course question bank" page logged in as "admin"
|
||||
Then I should see "Likely" in the "TF1" "table_row"
|
||||
And I should see "Unlikely" in the "TF2" "table_row"
|
||||
@ -123,6 +126,7 @@ Feature: Show statistics in question bank
|
||||
| slot | response |
|
||||
| 1 | True |
|
||||
| 2 | False |
|
||||
And I run the scheduled task "\quiz_statistics\task\recalculate"
|
||||
When I am on the "Course 1" "core_question > course question bank" page logged in as "admin"
|
||||
Then I should see "Likely" in the "TF1" "table_row"
|
||||
And I should see "Very likely" in the "TF2" "table_row"
|
||||
|
@ -225,6 +225,12 @@ class helper_test extends \advanced_testcase {
|
||||
foreach ($quiz2attempts as $attempt) {
|
||||
$this->submit_quiz($quiz2, $attempt);
|
||||
}
|
||||
|
||||
// Calculate the statistics.
|
||||
$this->expectOutputRegex('~.*Calculations completed.*~');
|
||||
$statisticstask = new \quiz_statistics\task\recalculate();
|
||||
$statisticstask->execute();
|
||||
|
||||
return [$quiz1, $quiz2, $questions];
|
||||
}
|
||||
|
||||
|
@ -232,6 +232,12 @@ class statistics_bulk_loader_test extends advanced_testcase {
|
||||
foreach ($quiz2attempts as $attempt) {
|
||||
$this->submit_quiz($quiz2, $attempt);
|
||||
}
|
||||
|
||||
// Calculate the statistics.
|
||||
$this->expectOutputRegex('~.*Calculations completed.*~');
|
||||
$statisticstask = new \quiz_statistics\task\recalculate();
|
||||
$statisticstask->execute();
|
||||
|
||||
return [$quiz1, $quiz2, $questions];
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user