diff --git a/mod/quiz/lib.php b/mod/quiz/lib.php index bdfc85229cf..7493cc3ff65 100644 --- a/mod/quiz/lib.php +++ b/mod/quiz/lib.php @@ -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); } diff --git a/mod/quiz/report/statistics/report.php b/mod/quiz/report/statistics/report.php index fe6f33a7bba..cdb43b8c036 100644 --- a/mod/quiz/report/statistics/report.php +++ b/mod/quiz/report/statistics/report.php @@ -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; } diff --git a/mod/quiz/report/statistics/upgrade.txt b/mod/quiz/report/statistics/upgrade.txt index 7926b3f81b6..e628ac7a0a3 100644 --- a/mod/quiz/report/statistics/upgrade.txt +++ b/mod/quiz/report/statistics/upgrade.txt @@ -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 diff --git a/question/bank/statistics/tests/behat/statistics_values.feature b/question/bank/statistics/tests/behat/statistics_values.feature index a65a7f58275..9735e2be10d 100644 --- a/question/bank/statistics/tests/behat/statistics_values.feature +++ b/question/bank/statistics/tests/behat/statistics_values.feature @@ -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" diff --git a/question/bank/statistics/tests/helper_test.php b/question/bank/statistics/tests/helper_test.php index f80084e91d9..94e9afad6a9 100644 --- a/question/bank/statistics/tests/helper_test.php +++ b/question/bank/statistics/tests/helper_test.php @@ -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]; } diff --git a/question/tests/local/statistics/statistics_bulk_loader_test.php b/question/tests/local/statistics/statistics_bulk_loader_test.php index 50f26dee858..7c6ec53387d 100644 --- a/question/tests/local/statistics/statistics_bulk_loader_test.php +++ b/question/tests/local/statistics/statistics_bulk_loader_test.php @@ -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]; }